From 8f092d549064d85153db86f5d45347fd7367be48 Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Wed, 4 Mar 2009 22:47:48 +0100 Subject: [PATCH] move some functions to TagEditor class / outside --- src/helpers.cpp | 29 +++++ src/helpers.h | 4 + src/misc.cpp | 33 ++++++ src/misc.h | 6 +- src/song.cpp | 1 + src/song.h | 1 - src/tag_editor.cpp | 263 +++++++++++++++++---------------------------- src/tag_editor.h | 18 ++-- 8 files changed, 184 insertions(+), 171 deletions(-) diff --git a/src/helpers.cpp b/src/helpers.cpp index a5ee5ecb..f4f3c409 100644 --- a/src/helpers.cpp +++ b/src/helpers.cpp @@ -239,6 +239,35 @@ bool Keypressed(int in, const int *key) return in == key[0] || in == key[1]; } +#ifdef HAVE_TAGLIB_H +string FindSharedDir(Menu *menu) +{ + SongList list; + for (size_t i = 0; i < menu->Size(); i++) + list.push_back(&menu->at(i)); + return FindSharedDir(list); +} + +string FindSharedDir(const SongList &v) +{ + string result; + if (!v.empty()) + { + result = v.front()->GetFile(); + for (SongList::const_iterator it = v.begin()+1; it != v.end(); it++) + { + int i = 1; + while (result.substr(0, i) == (*it)->GetFile().substr(0, i)) + i++; + result = result.substr(0, i); + } + size_t slash = result.rfind("/"); + result = slash != string::npos ? result.substr(0, slash) : "/"; + } + return result; +} +#endif // HAVE_TAGLIB_H + string FindSharedDir(const string &one, const string &two) { if (one == two) diff --git a/src/helpers.h b/src/helpers.h index 4984bd61..2f2b3976 100644 --- a/src/helpers.h +++ b/src/helpers.h @@ -55,6 +55,10 @@ void UpdateSongList(Menu *); bool Keypressed(int, const int *); +#ifdef HAVE_TAGLIB_H +std::string FindSharedDir(Menu *); +std::string FindSharedDir(const MPD::SongList &); +#endif // HAVE_TAGLIB_H std::string FindSharedDir(const std::string &, const std::string &); std::string GetLineValue(std::string &, char = '"', char = '"', bool = 0); diff --git a/src/misc.cpp b/src/misc.cpp index c1ed9132..5d670b63 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -145,6 +145,39 @@ mpd_TagItems IntoTagItem(char c) } } +#ifdef HAVE_TAGLIB_H +MPD::Song::SetFunction IntoSetFunction(mpd_TagItems tag) +{ + switch (tag) + { + case MPD_TAG_ITEM_ARTIST: + return &MPD::Song::SetArtist; + case MPD_TAG_ITEM_ALBUM: + return &MPD::Song::SetAlbum; + case MPD_TAG_ITEM_TITLE: + return &MPD::Song::SetTitle; + case MPD_TAG_ITEM_TRACK: + return &MPD::Song::SetTrack; + case MPD_TAG_ITEM_GENRE: + return &MPD::Song::SetGenre; + case MPD_TAG_ITEM_DATE: + return &MPD::Song::SetYear; + case MPD_TAG_ITEM_COMPOSER: + return &MPD::Song::SetComposer; + case MPD_TAG_ITEM_PERFORMER: + return &MPD::Song::SetPerformer; + case MPD_TAG_ITEM_COMMENT: + return &MPD::Song::SetComment; + case MPD_TAG_ITEM_DISC: + return &MPD::Song::SetDisc; + case MPD_TAG_ITEM_FILENAME: + return &MPD::Song::SetNewName; + default: + return 0; + } +} +#endif // HAVE_TAGLIB_H + void EscapeUnallowedChars(std::string &s) { for (std::string::const_iterator it = unallowed_chars.begin(); it != unallowed_chars.end(); it++) diff --git a/src/misc.h b/src/misc.h index c22fa40c..57617616 100644 --- a/src/misc.h +++ b/src/misc.h @@ -24,7 +24,7 @@ #include #include "window.h" -#include "libmpdclient.h" +#include "song.h" void ToLower(std::string &); @@ -40,6 +40,10 @@ Color IntoColor(const std::string &); mpd_TagItems IntoTagItem(char); +#ifdef HAVE_TAGLIB_H +MPD::Song::SetFunction IntoSetFunction(mpd_TagItems); +#endif // HAVE_TAGLIB_H + void EscapeUnallowedChars(std::string &); void EscapeHtml(std::string &s); diff --git a/src/song.cpp b/src/song.cpp index d3f387db..5144ff4d 100644 --- a/src/song.cpp +++ b/src/song.cpp @@ -27,6 +27,7 @@ #include #include "charset.h" +#include "misc.h" #include "song.h" using MPD::Song; diff --git a/src/song.h b/src/song.h index ffb7b221..239f929b 100644 --- a/src/song.h +++ b/src/song.h @@ -23,7 +23,6 @@ #include -#include "misc.h" #include "libmpdclient.h" namespace MPD diff --git a/src/tag_editor.cpp b/src/tag_editor.cpp index 7b6c4e25..d41660dc 100644 --- a/src/tag_editor.cpp +++ b/src/tag_editor.cpp @@ -287,6 +287,9 @@ bool TinyTagEditor::GetTags() TagEditor *myTagEditor = new TagEditor; +const string TagEditor::PatternsFile = config_dir + "patterns.list"; +vector TagEditor::Patterns; + const size_t TagEditor::MiddleColumnWidth = 26; size_t TagEditor::LeftColumnWidth; size_t TagEditor::MiddleColumnStartX; @@ -1046,186 +1049,122 @@ std::string TagEditor::TagToString(const MPD::Song &s, void *data) return result.empty() ? Config.empty_tag : result; } -namespace +void TagEditor::GetPatternList() { - const string patterns_list_file = config_dir + "patterns.list"; - vector patterns_list; - - void GetPatternList() + if (Patterns.empty()) { - if (patterns_list.empty()) + std::ifstream input(PatternsFile.c_str()); + if (input.is_open()) { - std::ifstream input(patterns_list_file.c_str()); - if (input.is_open()) + string line; + while (getline(input, line)) { - string line; - while (getline(input, line)) - { - if (!line.empty()) - patterns_list.push_back(line); - } - input.close(); + if (!line.empty()) + Patterns.push_back(line); } + input.close(); } } - - void SavePatternList() - { - std::ofstream output(patterns_list_file.c_str()); - if (output.is_open()) - { - for (vector::const_iterator it = patterns_list.begin(); it != patterns_list.end() && it != patterns_list.begin()+30; it++) - output << *it << std::endl; - output.close(); - } - } - - Song::SetFunction IntoSetFunction(char c) - { - switch (c) - { - case 'a': - return &Song::SetArtist; - case 't': - return &Song::SetTitle; - case 'b': - return &Song::SetAlbum; - case 'y': - return &Song::SetYear; - case 'n': - return &Song::SetTrack; - case 'g': - return &Song::SetGenre; - case 'c': - return &Song::SetComposer; - case 'p': - return &Song::SetPerformer; - case 'd': - return &Song::SetDisc; - case 'C': - return &Song::SetComment; - default: - return NULL; - } - } - - string GenerateFilename(const Song &s, string &pattern) - { - string result = s.toString(pattern); - EscapeUnallowedChars(result); - return result; - } - - string ParseFilename(Song &s, string mask, bool preview) - { - std::ostringstream result; - vector separators; - vector< std::pair > tags; - string file = s.GetName().substr(0, s.GetName().rfind(".")); - - try - { - for (size_t i = mask.find("%"); i != string::npos; i = mask.find("%")) - { - tags.push_back(make_pair(mask.at(i+1), "")); - mask = mask.substr(i+2); - i = mask.find("%"); - if (!mask.empty()) - separators.push_back(mask.substr(0, i)); - } - int i = 0; - for (vector::const_iterator it = separators.begin(); it != separators.end(); it++, i++) - { - int j = file.find(*it); - tags.at(i).second = file.substr(0, j); - file = file.substr(j+it->length()); - } - if (!file.empty()) - tags.at(i).second = file; - } - catch (std::out_of_range) - { - return "Error while parsing filename!"; - } - - for (vector< std::pair >::iterator it = tags.begin(); it != tags.end(); it++) - { - for (string::iterator j = it->second.begin(); j != it->second.end(); j++) - if (*j == '_') - *j = ' '; - - if (!preview) - { - Song::SetFunction set = IntoSetFunction(it->first); - if (set) - (s.*set)(it->second); - } - else - result << "%" << it->first << ": " << it->second << "\n"; - } - return result.str(); - } } -Song::SetFunction IntoSetFunction(mpd_TagItems tag) +void TagEditor::SavePatternList() { - switch (tag) + std::ofstream output(PatternsFile.c_str()); + if (output.is_open()) { - case MPD_TAG_ITEM_ARTIST: + for (vector::const_iterator it = Patterns.begin(); it != Patterns.end() && it != Patterns.begin()+30; it++) + output << *it << std::endl; + output.close(); + } +} + +Song::SetFunction TagEditor::IntoSetFunction(char c) +{ + switch (c) + { + case 'a': return &Song::SetArtist; - case MPD_TAG_ITEM_ALBUM: - return &Song::SetAlbum; - case MPD_TAG_ITEM_TITLE: + case 't': return &Song::SetTitle; - case MPD_TAG_ITEM_TRACK: - return &Song::SetTrack; - case MPD_TAG_ITEM_GENRE: - return &Song::SetGenre; - case MPD_TAG_ITEM_DATE: + case 'b': + return &Song::SetAlbum; + case 'y': return &Song::SetYear; - case MPD_TAG_ITEM_COMPOSER: + case 'n': + return &Song::SetTrack; + case 'g': + return &Song::SetGenre; + case 'c': return &Song::SetComposer; - case MPD_TAG_ITEM_PERFORMER: + case 'p': return &Song::SetPerformer; - case MPD_TAG_ITEM_COMMENT: - return &Song::SetComment; - case MPD_TAG_ITEM_DISC: + case 'd': return &Song::SetDisc; - case MPD_TAG_ITEM_FILENAME: - return &Song::SetNewName; + case 'C': + return &Song::SetComment; default: return NULL; } } -string FindSharedDir(Menu *menu) +string TagEditor::GenerateFilename(const Song &s, string &pattern) { - SongList list; - for (size_t i = 0; i < menu->Size(); i++) - list.push_back(&menu->at(i)); - return FindSharedDir(list); -} - -string FindSharedDir(const SongList &v) -{ - string result; - if (!v.empty()) - { - result = v.front()->GetFile(); - for (SongList::const_iterator it = v.begin()+1; it != v.end(); it++) - { - int i = 1; - while (result.substr(0, i) == (*it)->GetFile().substr(0, i)) - i++; - result = result.substr(0, i); - } - size_t slash = result.rfind("/"); - result = slash != string::npos ? result.substr(0, slash) : "/"; - } + string result = s.toString(pattern); + EscapeUnallowedChars(result); return result; } -void DealWithFilenames(SongList &v) +string TagEditor::ParseFilename(Song &s, string mask, bool preview) +{ + std::ostringstream result; + vector separators; + vector< std::pair > tags; + string file = s.GetName().substr(0, s.GetName().rfind(".")); + + try + { + for (size_t i = mask.find("%"); i != string::npos; i = mask.find("%")) + { + tags.push_back(make_pair(mask.at(i+1), "")); + mask = mask.substr(i+2); + i = mask.find("%"); + if (!mask.empty()) + separators.push_back(mask.substr(0, i)); + } + int i = 0; + for (vector::const_iterator it = separators.begin(); it != separators.end(); it++, i++) + { + int j = file.find(*it); + tags.at(i).second = file.substr(0, j); + file = file.substr(j+it->length()); + } + if (!file.empty()) + tags.at(i).second = file; + } + catch (std::out_of_range) + { + return "Error while parsing filename!"; + } + + for (vector< std::pair >::iterator it = tags.begin(); it != tags.end(); it++) + { + for (string::iterator j = it->second.begin(); j != it->second.end(); j++) + if (*j == '_') + *j = ' '; + + if (!preview) + { + Song::SetFunction set = IntoSetFunction(it->first); + if (set) + (s.*set)(it->second); + } + else + result << "%" << it->first << ": " << it->second << "\n"; + } + return result.str(); +} + +void TagEditor::DealWithFilenames(SongList &v) { int width = 30; int height = 6; @@ -1296,20 +1235,20 @@ void DealWithFilenames(SongList &v) Main->SetTimeout(ncmpcpp_window_timeout); Main->SetItemDisplayer(Display::Generic); - if (!patterns_list.empty()) - Config.pattern = patterns_list.front(); + if (!Patterns.empty()) + Config.pattern = Patterns.front(); Main->AddOption("Pattern: " + Config.pattern); Main->AddOption("Preview"); Main->AddOption("Legend"); Main->AddSeparator(); Main->AddOption("Proceed"); Main->AddOption("Cancel"); - if (!patterns_list.empty()) + if (!Patterns.empty()) { Main->AddSeparator(); Main->AddOption("Recent patterns", 1, 1); Main->AddSeparator(); - for (vector::const_iterator it = patterns_list.begin(); it != patterns_list.end(); it++) + for (vector::const_iterator it = Patterns.begin(); it != Patterns.end(); it++) Main->AddOption(*it); } @@ -1406,15 +1345,15 @@ void DealWithFilenames(SongList &v) } else { - for (size_t i = 0; i < patterns_list.size(); i++) + for (size_t i = 0; i < Patterns.size(); i++) { - if (patterns_list[i] == Config.pattern) + if (Patterns[i] == Config.pattern) { - patterns_list.erase(patterns_list.begin()+i); + Patterns.erase(Patterns.begin()+i); i--; } } - patterns_list.insert(patterns_list.begin(), Config.pattern); + Patterns.insert(Patterns.begin(), Config.pattern); } ShowMessage("Operation finished!"); if (preview) diff --git a/src/tag_editor.h b/src/tag_editor.h index bd2d2316..d9f9cdea 100644 --- a/src/tag_editor.h +++ b/src/tag_editor.h @@ -105,11 +105,22 @@ class TagEditor : public Screen static void LowerAllLetters(MPD::Song &); static void GetTagList(TagLib::StringList &, const std::string &); + static void GetPatternList(); + static void SavePatternList(); + static MPD::Song::SetFunction IntoSetFunction(char); + static std::string GenerateFilename(const MPD::Song &, std::string &); + static std::string ParseFilename(MPD::Song &, std::string, bool); + + static void DealWithFilenames(MPD::SongList &); + static std::string TagToString(const MPD::Song &, void *); std::string itsBrowsedDir; std::string itsHighlightedDir; + static const std::string PatternsFile; + static std::vector Patterns; + static const size_t MiddleColumnWidth; static size_t LeftColumnWidth; static size_t MiddleColumnStartX; @@ -119,13 +130,6 @@ class TagEditor : public Screen extern TagEditor *myTagEditor; -std::string FindSharedDir(Menu *); -std::string FindSharedDir(const MPD::SongList &); - -MPD::Song::SetFunction IntoSetFunction(mpd_TagItems); - -void DealWithFilenames(MPD::SongList &); - #endif #endif