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
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\
filesystem_u.o network.o progress.o -lcurl -lsqlite3
filesystem.o network.o progress.o -lcurl -lsqlite3
filesystem_u.o: unix/filesystem.cpp
$(CXX) $(CFLAGS) -c unix/filesystem.cpp -o filesystem_u.o
filesystem.o: unix/filesystem.cpp
$(CXX) $(CFLAGS) -c unix/filesystem.cpp -o filesystem.o
functions.o: functions.cpp
$(CXX) $(CFLAGS) -c functions.cpp
network.o: network.cpp
$(CXX) $(CFLAGS) -c network.cpp
network.o: unix/network.cpp
$(CXX) $(CFLAGS) -c unix/network.cpp
tv_rename.o: tv_rename.cpp
$(CXX) $(CFLAGS) -c tv_rename.cpp
@ -80,10 +80,10 @@ tv_rename_gui.o: tv_rename.cpp
.PHONY: windows
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
$(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\
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
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\
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\
user32.lib tv_rename_gui.res

View File

@ -23,10 +23,6 @@ using char_t = char;
#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
// CLI functions
void printHelp();
@ -52,10 +48,10 @@ string userHome();
void prepareDB( const std::string &_pattern = "" );
#ifndef GUI
void addToDB( string &show, const string &path, const string &language,
bool linux );
bool linux, bool dvd );
#else
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();
#endif
void removeFromDB( const string &path );

View File

@ -41,6 +41,8 @@ using string = std::string;
#define DB_REMOVE 0x0010
#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,
// -1 - error
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;
break;
case 'l':
if ( findLanguage( optional ) ) {
language = optional;
} else {
cerr << "Invalid language choice" << std::endl;
printLangs();
return -1;
}
language = optional;
i++;
break;
case 'h':
printHelp();
return 1;
case '0':
printLangs();
return 1;
return 2;
case '1':
pattern = optional;
i++;
@ -261,16 +256,24 @@ int main
string pattern{ TEXT( "%filename - %epname" ) };
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;
{
auto tmp =
parseCommandLine( show, seasons_num, path, language, pattern,
tv_flags, db_flags, db_pattern, argc, argv );
if ( tmp == -1 )
return 1;
else if ( tmp == 1 )
return 0;
authenticate( API_KEY );
if ( ret == 2 ) {
printLangs();
return 0;
}
if ( !findLanguage( language.c_str() ) ) {
cerr << "Invalid language choice" << std::endl;
printLangs();
return -1;
}
if ( !FSLib::isDirectory( path ) && FSLib::exists( path ) ) {
@ -305,7 +308,7 @@ int main
removeFromDB( FSLib::canonical( path ) );
}
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;
}
if ( db_flags & DB_REFRESH ) {

View File

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

View File

@ -9,7 +9,7 @@ size_t writeCallback( void *contents, size_t size, size_t nmemb,
return size * nmemb;
}
Request::Request( const string &server ) : _server( server ) {
Request::Request() {
curl_global_init( CURL_GLOBAL_ALL );
_curl_handle = curl_easy_init();
if ( _curl_handle == NULL ) {
@ -79,3 +79,7 @@ void Request::clearHeader() {
bool Request::initSuccessful() {
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 };
Request::Request( const string &server ) {
Request::Request() {
// Start connection
_hInternet = InternetOpen( L"WinInet/1.0", INTERNET_OPEN_TYPE_PRECONFIG,
nullptr, nullptr, 0 );
if ( !_hInternet )
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() {
@ -81,5 +75,14 @@ void Request::clearHeader() {
}
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;
}