More refactoring

This commit is contained in:
zvon 2020-01-15 22:23:11 +01:00
parent c596acb2be
commit 2a42f679ba
6 changed files with 53 additions and 47 deletions

View File

@ -38,18 +38,18 @@ uninstall_gui:
rm $(ICONDIR)/scalable/apps/tv_rename.svg rm $(ICONDIR)/scalable/apps/tv_rename.svg
gtk-update-icon-cache -f $(ICONDIR) gtk-update-icon-cache -f $(ICONDIR)
tv_rename: functions.o filesystem_u.o network.o tv_rename.o progress.o main.cpp tv_rename: functions.o filesystem.o network.o tv_rename.o progress.o main.cpp
$(CXX) $(CFLAGS) -o tv_rename main.cpp tv_rename.o functions.o\ $(CXX) $(CFLAGS) -o tv_rename main.cpp tv_rename.o functions.o\
filesystem_u.o network.o progress.o -lcurl -lsqlite3 filesystem.o network.o progress.o -lcurl -lsqlite3
filesystem_u.o: unix/filesystem.cpp filesystem.o: unix/filesystem.cpp
$(CXX) $(CFLAGS) -c unix/filesystem.cpp -o filesystem_u.o $(CXX) $(CFLAGS) -c unix/filesystem.cpp -o filesystem.o
functions.o: functions.cpp functions.o: functions.cpp
$(CXX) $(CFLAGS) -c functions.cpp $(CXX) $(CFLAGS) -c functions.cpp
network.o: network.cpp network.o: unix/network.cpp
$(CXX) $(CFLAGS) -c network.cpp $(CXX) $(CFLAGS) -c unix/network.cpp
tv_rename.o: tv_rename.cpp tv_rename.o: tv_rename.cpp
$(CXX) $(CFLAGS) -c tv_rename.cpp $(CXX) $(CFLAGS) -c tv_rename.cpp
@ -80,10 +80,10 @@ tv_rename_gui.o: tv_rename.cpp
.PHONY: windows .PHONY: windows
windows: tv_rename.exe windows: tv_rename.exe
tv_rename.exe: tv_rename.cpp functions.cpp windows/filesystem.cpp network.cpp\ tv_rename.exe: tv_rename.cpp functions.cpp windows/filesystem.cpp windows/network.cpp\
progress.cpp sqlite3.c main.cpp progress.cpp sqlite3.c main.cpp
$(CXX) -MD -EHsc -Fe"tv_rename" tv_rename.cpp windows/filesystem.cpp\ $(CXX) -MD -EHsc -Fe"tv_rename" tv_rename.cpp windows/filesystem.cpp\
functions.cpp network.cpp progress.cpp sqlite3.c main.cpp\ functions.cpp windows/network.cpp progress.cpp sqlite3.c main.cpp\
-D_WIN32 -DUNICODE -link wininet.lib shlwapi.lib ole32.lib\ -D_WIN32 -DUNICODE -link wininet.lib shlwapi.lib ole32.lib\
shell32.lib user32.lib shell32.lib user32.lib
@ -91,9 +91,9 @@ tv_rename.exe: tv_rename.cpp functions.cpp windows/filesystem.cpp network.cpp\
windows_gui: tv_rename_gui.exe windows_gui: tv_rename_gui.exe
tv_rename_gui.exe: tv_rename_gui.res tv_rename_gui.cpp tv_rename.cpp\ tv_rename_gui.exe: tv_rename_gui.res tv_rename_gui.cpp tv_rename.cpp\
windows/filesystem.cpp functions.cpp network.cpp windows/filesystem.cpp functions.cpp windows/network.cpp
$(CXX) -MD -EHsc -Fe"tv_rename_gui" tv_rename_gui.cpp tv_rename.cpp\ $(CXX) -MD -EHsc -Fe"tv_rename_gui" tv_rename_gui.cpp tv_rename.cpp\
windows/filesystem.cpp functions.cpp network.cpp -D_WIN32 -DUNICODE\ windows/filesystem.cpp functions.cpp windows/network.cpp -D_WIN32 -DUNICODE\
-DGUI -link wininet.lib shlwapi.lib ole32.lib shell32.lib gdi32.lib\ -DGUI -link wininet.lib shlwapi.lib ole32.lib shell32.lib gdi32.lib\
user32.lib tv_rename_gui.res user32.lib tv_rename_gui.res

View File

@ -23,10 +23,6 @@ using char_t = char;
#endif #endif
void findSeason( std::set< string > &files, int season, const string &path );
void findSeasons( std::map< int, std::set< string > > &seasons,
const string &path, const std::set< int > &season_numbers );
#ifndef GUI #ifndef GUI
// CLI functions // CLI functions
void printHelp(); void printHelp();
@ -52,10 +48,10 @@ string userHome();
void prepareDB( const std::string &_pattern = "" ); void prepareDB( const std::string &_pattern = "" );
#ifndef GUI #ifndef GUI
void addToDB( string &show, const string &path, const string &language, void addToDB( string &show, const string &path, const string &language,
bool linux ); bool linux, bool dvd );
#else #else
void addToDB( string &show, const string &path, const string &language, void addToDB( string &show, const string &path, const string &language,
const string &url, const string &pattern, bool linux ); const string &url, const string &pattern, bool linux, bool dvd );
std::vector< std::unordered_map< std::string, std::string > > dbGetShows(); std::vector< std::unordered_map< std::string, std::string > > dbGetShows();
#endif #endif
void removeFromDB( const string &path ); void removeFromDB( const string &path );

View File

@ -41,6 +41,8 @@ using string = std::string;
#define DB_REMOVE 0x0010 #define DB_REMOVE 0x0010
#define DB_PATTERN 0x0020 #define DB_PATTERN 0x0020
#define API_KEY "42B66F5E-C6BF-423F-ADF9-CC97163472F6"
// return 0 - all went as expected, 1 - request of help or print languages, // return 0 - all went as expected, 1 - request of help or print languages,
// -1 - error // -1 - error
int handleArgument( char_t c, string &show, std::set< int > &seasons_num, int handleArgument( char_t c, string &show, std::set< int > &seasons_num,
@ -75,21 +77,14 @@ int handleArgument( char_t c, string &show, std::set< int > &seasons_num,
tv_flags |= TV_DVD; tv_flags |= TV_DVD;
break; break;
case 'l': case 'l':
if ( findLanguage( optional ) ) { language = optional;
language = optional;
} else {
cerr << "Invalid language choice" << std::endl;
printLangs();
return -1;
}
i++; i++;
break; break;
case 'h': case 'h':
printHelp(); printHelp();
return 1; return 1;
case '0': case '0':
printLangs(); return 2;
return 1;
case '1': case '1':
pattern = optional; pattern = optional;
i++; i++;
@ -261,16 +256,24 @@ int main
string pattern{ TEXT( "%filename - %epname" ) }; string pattern{ TEXT( "%filename - %epname" ) };
string db_pattern{}; string db_pattern{};
authenticate( "42B66F5E-C6BF-423F-ADF9-CC97163472F6" ); auto ret = parseCommandLine( show, seasons_num, path, language, pattern,
tv_flags, db_flags, db_pattern, argc, argv );
if ( ret == -1 )
return 1;
else if ( ret == 1 )
return 0;
{ authenticate( API_KEY );
auto tmp =
parseCommandLine( show, seasons_num, path, language, pattern, if ( ret == 2 ) {
tv_flags, db_flags, db_pattern, argc, argv ); printLangs();
if ( tmp == -1 ) return 0;
return 1; }
else if ( tmp == 1 )
return 0; if ( !findLanguage( language.c_str() ) ) {
cerr << "Invalid language choice" << std::endl;
printLangs();
return -1;
} }
if ( !FSLib::isDirectory( path ) && FSLib::exists( path ) ) { if ( !FSLib::isDirectory( path ) && FSLib::exists( path ) ) {
@ -305,7 +308,7 @@ int main
removeFromDB( FSLib::canonical( path ) ); removeFromDB( FSLib::canonical( path ) );
} }
if ( db_flags & DB_ADD ) { if ( db_flags & DB_ADD ) {
addToDB( show, path, language, tv_flags & TV_LINUX ); addToDB( show, path, language, tv_flags & TV_LINUX, tv_flags & TV_DVD );
cout << "Added to database" << std::endl; cout << "Added to database" << std::endl;
} }
if ( db_flags & DB_REFRESH ) { if ( db_flags & DB_REFRESH ) {

View File

@ -20,14 +20,14 @@ using string = std::string;
class Request { class Request {
public: public:
Request() = delete; Request();
Request( const string &server );
~Request(); ~Request();
std::string get( const string &url ); std::string get( const string &url );
std::string post( const string &url, const std::string &data ); std::string post( const string &url, const std::string &data );
void addHeader( const string &header ); void addHeader( const string &header );
void clearHeader(); void clearHeader();
bool initSuccessful(); bool initSuccessful();
void setServer( const string &server );
private: private:
#ifdef _WIN32 #ifdef _WIN32
@ -37,7 +37,7 @@ private:
#else #else
CURL *_curl_handle = nullptr; CURL *_curl_handle = nullptr;
struct curl_slist *_chunk = nullptr; struct curl_slist *_chunk = nullptr;
const string _server; string _server;
#endif #endif
}; };

View File

@ -9,7 +9,7 @@ size_t writeCallback( void *contents, size_t size, size_t nmemb,
return size * nmemb; return size * nmemb;
} }
Request::Request( const string &server ) : _server( server ) { Request::Request() {
curl_global_init( CURL_GLOBAL_ALL ); curl_global_init( CURL_GLOBAL_ALL );
_curl_handle = curl_easy_init(); _curl_handle = curl_easy_init();
if ( _curl_handle == NULL ) { if ( _curl_handle == NULL ) {
@ -79,3 +79,7 @@ void Request::clearHeader() {
bool Request::initSuccessful() { bool Request::initSuccessful() {
return _curl_handle != nullptr; return _curl_handle != nullptr;
} }
void Request::setServer( const string &server ) {
_server = server;
}

View File

@ -4,18 +4,12 @@
const wchar_t *acceptTypes[] = { L"text/*", L"application/json", nullptr }; const wchar_t *acceptTypes[] = { L"text/*", L"application/json", nullptr };
Request::Request( const string &server ) { Request::Request() {
// Start connection // Start connection
_hInternet = InternetOpen( L"WinInet/1.0", INTERNET_OPEN_TYPE_PRECONFIG, _hInternet = InternetOpen( L"WinInet/1.0", INTERNET_OPEN_TYPE_PRECONFIG,
nullptr, nullptr, 0 ); nullptr, nullptr, 0 );
if ( !_hInternet ) if ( !_hInternet )
std::wcerr << "ERROR InternetOpen: " << GetLastError() << std::endl; std::wcerr << "ERROR InternetOpen: " << GetLastError() << std::endl;
// connect to server
_hConnect = InternetConnect( _hInternet, server.c_str(),
INTERNET_DEFAULT_HTTPS_PORT, nullptr, nullptr,
INTERNET_SERVICE_HTTP, 0, 0 );
if ( !_hConnect )
std::wcerr << "ERROR InternetConnect: " << GetLastError() << std::endl;
} }
Request::~Request() { Request::~Request() {
@ -81,5 +75,14 @@ void Request::clearHeader() {
} }
bool Request::initSuccessful() { bool Request::initSuccessful() {
return _hInternet != nullptr && _hConnect != nullptr; return _hInternet != nullptr;
}
void Request::setServer( const string &server ) {
// connect to server
_hConnect = InternetConnect( _hInternet, server.c_str(),
INTERNET_DEFAULT_HTTPS_PORT, nullptr, nullptr,
INTERNET_SERVICE_HTTP, 0, 0 );
if ( !_hConnect )
std::wcerr << "ERROR InternetConnect: " << GetLastError() << std::endl;
} }