From 72706a5c0e2cb5d0176ecd852b59986229c2d2c9 Mon Sep 17 00:00:00 2001 From: zvon Date: Wed, 23 Jan 2019 22:57:52 +0100 Subject: [PATCH] Fix bug where the program relied on episodes sorted by the episode number --- mainwindow.cpp | 33 +++++++++++------- tv_rename.cpp | 91 ++++++++++++++++++++++---------------------------- tv_rename.hpp | 2 +- 3 files changed, 61 insertions(+), 65 deletions(-) diff --git a/mainwindow.cpp b/mainwindow.cpp index 1f8f7f7..3601faf 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -147,13 +147,12 @@ void MainWindow::getNames() { * orig - original filenames * renamed - renamed filenames (sorted in the same order as `orig`) */ -void renameFiles(const std::set &orig, const std::vector> &renamed) { - auto orig_it = orig.begin(); - +void renameFiles(const std::vector>> &renamed) { for(auto renamed_it = renamed.begin(); renamed_it != renamed.end(); ++renamed_it) { - std::cout << *orig_it << " --> " << renamed_it->first + "/" + renamed_it->second << std::endl; - FSLib::rename(*orig_it, renamed_it->first + "/" + renamed_it->second); - ++orig_it; + std::cout << renamed_it->first << "/" << renamed_it->second.first << " --> " + << renamed_it->first << "/" << renamed_it->second.second << std::endl; + FSLib::rename(renamed_it->first + "/" + renamed_it->second.first + , renamed_it->first + "/" + renamed_it->second.second); } } @@ -167,7 +166,13 @@ void MainWindow::finishedSelection() { // debug output std::cout << (*iter)[m_columns_url.m_col_show] << " " << language_code << std::endl; - std::cout << "https://www.thetvdb.com" << static_cast((*iter)[m_columns_url.m_col_url]) << std::endl; + std::string url = static_cast((*iter)[m_columns_url.m_col_url]); + + // shouldn't ever happen, but just to be sure + if( url.empty() ) + return; + + std::cout << "https://www.thetvdb.com" << url << std::endl; std::string input_pattern = m_entry_pattern.get_text(); @@ -181,7 +186,7 @@ void MainWindow::finishedSelection() { for( auto &x : selected ) { // get renamed files for given season auto renamed_files = singleSeason( static_cast((*iter)[m_columns_url.m_col_show]), x, - "https://www.thetvdb.com" + static_cast((*iter)[m_columns_url.m_col_url]), + "https://www.thetvdb.com" + url, language_code, (input_pattern.empty() ? default_pattern : input_pattern), !m_check_linux.get_active(), c, files[x] ); @@ -191,7 +196,7 @@ void MainWindow::finishedSelection() { // if trust checkbox is ticked, rename files if( m_check_trust.get_active() ) { - renameFiles(files[x], renamed_files); + renameFiles(renamed_files); continue; } @@ -208,11 +213,15 @@ void MainWindow::finishedSelection() { auto buff = tx.get_buffer(); buff->place_cursor(buff->begin()); - buff->insert_at_cursor(renamed_files[0].second.c_str()); + buff->insert_at_cursor(renamed_files[0].second.first.c_str()); + buff->insert_at_cursor(" --> "); + buff->insert_at_cursor(renamed_files[0].second.second.c_str()); for( size_t i = 1; i < renamed_files.size(); i++ ) { buff->insert_at_cursor("\n"); - buff->insert_at_cursor(renamed_files[i].second.c_str()); + buff->insert_at_cursor(renamed_files[i].second.first.c_str()); + buff->insert_at_cursor(" --> "); + buff->insert_at_cursor(renamed_files[i].second.second.c_str()); } auto response = dialog.run(); @@ -220,7 +229,7 @@ void MainWindow::finishedSelection() { // if user clicked "Yes" in dialog, rename files switch(response) { case Gtk::RESPONSE_OK: - renameFiles(files[x], renamed_files); + renameFiles(renamed_files); default: break; } diff --git a/tv_rename.cpp b/tv_rename.cpp index f172eed..16e2f47 100644 --- a/tv_rename.cpp +++ b/tv_rename.cpp @@ -36,18 +36,25 @@ std::vector parseEpisodeNames( const std::string &season_code, cons return episodes; } -#ifndef GUI -void singleSeason( const std::string &path, const std::string &show, int season, std::string url, const std::string &language, const std::string &pattern, const bool &linux, const bool &trust, Curl &c, std::set const *files) { +#ifdef GUI +std::vector>> defaultSingleSeasonReturn() { + return {}; +} #else -std::vector> singleSeason( const std::string &show, int season, std::string url, const std::string &language, const std::string &pattern, const bool &linux, Curl &c, const std::set &files) { - if( files.empty() ) - return {}; +void defaultSingleSeasonReturn() { + return; +} #endif - if( url.empty() ) -#ifndef GUI - url = getDefUrl(show, language, c); + +#ifdef GUI +std::vector>> singleSeason( const std::string &show, int season, std::string url, const std::string &language, const std::string &pattern, const bool &linux, Curl &c, const std::set &files) { #else - return {}; +void singleSeason( const std::string &path, const std::string &show, int season, std::string url, const std::string &language, const std::string &pattern, const bool &linux, const bool &trust, Curl &c, std::set const *files_ptr) { +#endif + +#ifndef GUI + if( url.empty() ) + url = getDefUrl(show, language, c); #endif url += "/seasons/" + std::to_string(season); @@ -65,54 +72,41 @@ std::vector> singleSeason( const std::string if( pos != std::string::npos ) season_code = season_code.substr(0,pos); else -#ifndef GUI - return; -#else - return {}; -#endif + return defaultSingleSeasonReturn(); } else { -#ifndef GUI - return; -#else - return {}; -#endif + return defaultSingleSeasonReturn(); } auto episodes = parseEpisodeNames(season_code, language); if( episodes.empty() ) -#ifndef GUI - return; -#else - return {}; -#endif + return defaultSingleSeasonReturn(); + + std::vector>> renamed_files; #ifndef GUI std::set found_files; - std::set renamed_files; - if( files == nullptr ) { + if( files_ptr == nullptr ) { findSeason(found_files, season, path); - files = &found_files; + files_ptr = &found_files; } - if( files->empty() ) - return; + const std::set &files = *files_ptr; +#endif - for( const auto &x : *files ) { -#else - std::vector> renamed_files; + if( files.empty() ) + return defaultSingleSeasonReturn(); for( const auto &x : files ) { -#endif auto last = x.find_last_of("/"); - std::string name; + std::string og_name; std::string dir; - if( last == static_cast(-1) ) { - name = x; + if( last == std::string::npos ) { + og_name = x; dir = "."; } else { - name = x.substr(last+1); + og_name = x.substr(last+1); dir = x.substr(0, last); } unsigned long num; @@ -124,9 +118,9 @@ std::vector> singleSeason( const std::string } num -= 1; if( num < episodes.size() ) { - auto pos = name.find_last_of('.'); + auto pos = og_name.find_last_of('.'); // get desired filename - name = compilePattern(pattern, season, num+1, name.substr(0, pos), episodes[num], show) + name.substr(pos); + auto name = compilePattern(pattern, season, num+1, og_name.substr(0, pos), episodes[num], show) + og_name.substr(pos); // replace characters illegal in windows if desired if( !linux ) { season_code.erase(std::remove_if(name.begin(), name.end(), [](char x) {return x == '?' || x == '"' || x == '\\' || x == '|' || x == '*';}), season_code.end()); @@ -147,17 +141,15 @@ std::vector> singleSeason( const std::string } } } -#ifndef GUI - renamed_files.insert(dir + "/" + name); -#else - renamed_files.emplace_back(dir, name); -#endif + renamed_files.emplace_back(dir, std::pair(og_name, name)); } } -#ifndef GUI +#ifdef GUI + return renamed_files; +#else for(auto renamed = renamed_files.begin(); renamed != renamed_files.end(); ++renamed) { - std::cout << *renamed << std::endl; + std::cout << renamed->second.first << " --> " << renamed->second.second << std::endl; } if( !trust ) { @@ -170,14 +162,9 @@ std::vector> singleSeason( const std::string return; } - auto orig = files->begin(); - for(auto renamed = renamed_files.begin(); renamed != renamed_files.end(); ++renamed) { - FSLib::rename(*orig, *renamed); - ++orig; + FSLib::rename(renamed->first + "/" + renamed->second.first, renamed->first + "/" + renamed->second.second); } -#else - return renamed_files; #endif } diff --git a/tv_rename.hpp b/tv_rename.hpp index 4fbdb41..ff791d1 100644 --- a/tv_rename.hpp +++ b/tv_rename.hpp @@ -8,7 +8,7 @@ #include -std::vector> singleSeason( const std::string &show, int season, std::string url, const std::string &language, const std::string &pattern, const bool &linux, Curl &c, const std::set &files); +std::vector>> singleSeason( const std::string &show, int season, std::string url, const std::string &language, const std::string &pattern, const bool &linux, Curl &c, const std::set &files); #else