diff --git a/src/charset.cpp b/src/charset.cpp index fcec9a19..678bcd2e 100644 --- a/src/charset.cpp +++ b/src/charset.cpp @@ -127,6 +127,13 @@ void utf_to_locale(std::string &s) str_pool_put(tmp); } +std::string utf_to_locale_cpy(const std::string &s) +{ + std::string result = s; + utf_to_locale(result); + return result; +} + void locale_to_utf(std::string &s) { if (s.empty() || !locale_charset || !has_non_ascii_chars(s)) @@ -137,6 +144,13 @@ void locale_to_utf(std::string &s) str_pool_put(tmp); } +std::string locale_to_utf_cpy(const std::string &s) +{ + std::string result = s; + locale_to_utf(result); + return result; +} + void str_pool_utf_to_locale(char *&s) { if (!has_non_ascii_chars(s)) diff --git a/src/charset.h b/src/charset.h index b6c98375..24fe08cd 100644 --- a/src/charset.h +++ b/src/charset.h @@ -25,15 +25,18 @@ #include #endif -#if !defined(_UTF8) && defined(HAVE_ICONV_H) - #include +#if !defined(_UTF8) && defined(HAVE_ICONV_H) + void init_current_locale(); void utf_to_locale(std::string &); void locale_to_utf(std::string &); +std::string utf_to_locale_cpy(const std::string &s); +std::string locale_to_utf_cpy(const std::string &s); + void str_pool_utf_to_locale(char *&); void str_pool_locale_to_utf(char *&); @@ -44,6 +47,9 @@ void str_pool_locale_to_utf(char *&); #define utf_to_locale(x); #define locale_to_utf(x); +#define utf_to_locale_cpy(x) (x) +#define locale_to_utf_cpy(x) (x) + #define str_pool_utf_to_locale(x); #define str_pool_locale_to_utf(x); diff --git a/src/helpers.cpp b/src/helpers.cpp index 45427303..a8ed4b66 100644 --- a/src/helpers.cpp +++ b/src/helpers.cpp @@ -533,7 +533,7 @@ void DisplaySongInColumns(const Song &s, void *s_template, Menu *menu) void DisplaySong(const Song &s, void *data, Menu *menu) { - const_cast(&s)->LocalizeTags(); + const_cast(&s)->Localize(); const string &song_template = data ? *static_cast(data) : ""; basic_buffer buf; diff --git a/src/mpdpp.cpp b/src/mpdpp.cpp index b2ebfea0..71324e70 100644 --- a/src/mpdpp.cpp +++ b/src/mpdpp.cpp @@ -18,6 +18,7 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ +#include "charset.h" #include "mpdpp.h" using namespace MPD; @@ -482,7 +483,7 @@ int Connection::AddSong(const string &path) int Connection::AddSong(const Song &s) { - return !s.Empty() ? (s.IsFromDB() ? AddSong(s.GetFile()) : AddSong("file://" + string(s.GetFile()))) : -1; + return !s.Empty() ? (s.IsFromDB() ? (AddSong(s.Localized() ? locale_to_utf_cpy(s.GetFile()) : s.GetFile())) : AddSong("file://" + (s.Localized() ? locale_to_utf_cpy(s.GetFile()) : s.GetFile()))) : -1; } void Connection::QueueAddSong(const string &path) @@ -499,7 +500,7 @@ void Connection::QueueAddSong(const string &path) void Connection::QueueAddSong(const Song &s) { if (!s.Empty()) - QueueAddSong(s.GetFile()); + QueueAddSong(s.Localized() ? locale_to_utf_cpy(s.GetFile()) : s.GetFile()); } void Connection::QueueAddToPlaylist(const string &playlist, const string &path) diff --git a/src/ncmpcpp.cpp b/src/ncmpcpp.cpp index cad750e5..6b463968 100644 --- a/src/ncmpcpp.cpp +++ b/src/ncmpcpp.cpp @@ -527,7 +527,7 @@ int main(int argc, char *argv[]) if (!l.empty() && !l[0]->GetAlbum().empty()) { utf_to_locale(*it); - l[0]->LocalizeTags(); + l[0]->Localize(); maplist[l[0]->toString(Config.media_lib_album_format)] = *it; } FreeSongList(l); @@ -556,9 +556,7 @@ int main(int argc, char *argv[]) mLibAlbums->Refresh(); mLibSongs->Clear(0); Mpd->StartSearch(1); - locale_to_utf(mLibArtists->Current()); - Mpd->AddSearch(Config.media_lib_primary_tag, mLibArtists->Current()); - utf_to_locale(mLibArtists->Current()); + Mpd->AddSearch(Config.media_lib_primary_tag, locale_to_utf_cpy(mLibArtists->Current())); Mpd->CommitSearch(list); } else @@ -566,9 +564,7 @@ int main(int argc, char *argv[]) mLibSongs->Clear(0); Mpd->StartSearch(1); Mpd->AddSearch(Config.media_lib_primary_tag, mLibArtists->Current()); - locale_to_utf(mLibAlbums->Current().second); - Mpd->AddSearch(MPD_TAG_ITEM_ALBUM, mLibAlbums->Current().second); - utf_to_locale(mLibAlbums->Current().second); + Mpd->AddSearch(MPD_TAG_ITEM_ALBUM, locale_to_utf_cpy(mLibAlbums->Current().second)); Mpd->CommitSearch(list); } sort(list.begin(), list.end(), SortSongsByTrack); @@ -615,9 +611,7 @@ int main(int argc, char *argv[]) { mPlaylistEditor->Reset(); SongList list; - locale_to_utf(mPlaylistList->Current()); - Mpd->GetPlaylistContent(mPlaylistList->Current(), list); - utf_to_locale(mPlaylistList->Current()); + Mpd->GetPlaylistContent(locale_to_utf_cpy(mPlaylistList->Current()), list); if (!list.empty()) mPlaylistEditor->SetTitle("Playlist's content (" + IntoStr(list.size()) + " item" + (list.size() == 1 ? ")" : "s)")); else @@ -679,7 +673,7 @@ int main(int argc, char *argv[]) Mpd->CommitSearch(l); if (!l.empty()) { - l[0]->LocalizeTags(); + l[0]->Localize(); maplist[l[0]->toString(Config.tag_editor_album_format)] = *it; } FreeSongList(l); @@ -730,7 +724,7 @@ int main(int argc, char *argv[]) sort(list.begin(), list.end(), CaseInsensitiveSorting()); for (SongList::iterator it = list.begin(); it != list.end(); it++) { - (*it)->LocalizeTags(); + (*it)->Localize(); mEditorTags->AddOption(**it); } } @@ -740,7 +734,7 @@ int main(int argc, char *argv[]) sort(list.begin(), list.end(), CaseInsensitiveSorting()); for (SongList::const_iterator it = list.begin(); it != list.end(); it++) { - (*it)->LocalizeTags(); + (*it)->Localize(); mEditorTags->AddOption(**it); } } @@ -1034,9 +1028,7 @@ int main(int argc, char *argv[]) case itPlaylist: { SongList list; - string playlist_real_name = item.name; - locale_to_utf(playlist_real_name); - Mpd->GetPlaylistContent(playlist_real_name, list); + Mpd->GetPlaylistContent(locale_to_utf_cpy(item.name), list); for (SongList::const_iterator it = list.begin(); it != list.end(); it++) Mpd->QueueAddSong(**it); if (Mpd->CommitQueue()) @@ -1156,7 +1148,7 @@ int main(int argc, char *argv[]) ShowMessage("Tags updated!"); if (s.IsFromDB()) { - Mpd->UpdateDirectory(s.GetDirectory()); + Mpd->UpdateDirectory(locale_to_utf_cpy(s.GetDirectory())); if (prev_screen == csSearcher) *mSearcher->Current().second = s; } @@ -1355,10 +1347,8 @@ int main(int argc, char *argv[]) if (!mLibArtists->Empty() && wCurrent == mLibArtists) { - string tag = mLibArtists->Current(); - locale_to_utf(tag); Mpd->StartSearch(1); - Mpd->AddSearch(Config.media_lib_primary_tag, tag); + Mpd->AddSearch(Config.media_lib_primary_tag, locale_to_utf_cpy(mLibArtists->Current())); Mpd->CommitSearch(list); for (SongList::const_iterator it = list.begin(); it != list.end(); it++) Mpd->QueueAddSong(**it); @@ -1465,9 +1455,7 @@ int main(int argc, char *argv[]) if (wCurrent == mPlaylistList && !mPlaylistList->Empty()) { - string playlist = mPlaylistList->Current(); - locale_to_utf(playlist); - Mpd->GetPlaylistContent(playlist, list); + Mpd->GetPlaylistContent(locale_to_utf_cpy(mPlaylistList->Current()), list); for (SongList::const_iterator it = list.begin(); it != list.end(); it++) Mpd->QueueAddSong(**it); if (Mpd->CommitQueue()) @@ -1776,11 +1764,8 @@ int main(int argc, char *argv[]) if (browsed_dir != "/" && !mBrowser->Choice()) continue; // do not let add parent dir. - string real_dir_name = item.name; - locale_to_utf(real_dir_name); - SongList list; - Mpd->GetDirectoryRecursive(real_dir_name, list); + Mpd->GetDirectoryRecursive(locale_to_utf_cpy(item.name), list); for (SongList::const_iterator it = list.begin(); it != list.end(); it++) Mpd->QueueAddSong(**it); @@ -1827,9 +1812,7 @@ int main(int argc, char *argv[]) case itPlaylist: { SongList list; - string playlist_real_name = item.name; - locale_to_utf(playlist_real_name); - Mpd->GetPlaylistContent(playlist_real_name, list); + Mpd->GetPlaylistContent(locale_to_utf_cpy(item.name), list); for (SongList::const_iterator it = list.begin(); it != list.end(); it++) Mpd->QueueAddSong(**it); if (Mpd->CommitQueue()) @@ -2067,9 +2050,7 @@ int main(int argc, char *argv[]) while (in != 'y' && in != 'n'); if (in == 'y') { - locale_to_utf(name); - Mpd->DeletePlaylist(name); - utf_to_locale(name); + Mpd->DeletePlaylist(locale_to_utf_cpy(name)); ShowMessage("Playlist %s deleted!", name.c_str()); if (!Config.local_browser) GetDirectory("/"); @@ -2549,16 +2530,14 @@ int main(int argc, char *argv[]) SongList list; ShowMessage("Updating tags..."); Mpd->StartSearch(1); - locale_to_utf(mLibArtists->Current()); - Mpd->AddSearch(Config.media_lib_primary_tag, mLibArtists->Current()); - utf_to_locale(mLibArtists->Current()); + Mpd->AddSearch(Config.media_lib_primary_tag, locale_to_utf_cpy(mLibArtists->Current())); Mpd->CommitSearch(list); SongSetFunction set = IntoSetFunction(Config.media_lib_primary_tag); if (!set) continue; for (SongList::iterator it = list.begin(); it != list.end(); it++) { - (*it)->LocalizeTags(); + (*it)->Localize(); ((*it)->*set)(new_tag); ShowMessage("Updating tags in '%s'...", (*it)->GetName().c_str()); string path = Config.mpd_music_dir + (*it)->GetFile(); @@ -2589,7 +2568,7 @@ int main(int argc, char *argv[]) ShowMessage("Updating tags..."); for (size_t i = 0; i < mLibSongs->Size(); i++) { - (*mLibSongs)[i].LocalizeTags(); + (*mLibSongs)[i].Localize(); ShowMessage("Updating tags in '%s'...", (*mLibSongs)[i].GetName().c_str()); string path = Config.mpd_music_dir + (*mLibSongs)[i].GetFile(); TagLib::FileRef f(path.c_str()); @@ -2673,18 +2652,14 @@ int main(int argc, char *argv[]) UnlockStatusbar(); if (!new_dir.empty() && new_dir != old_dir) { - locale_to_utf(old_dir); - locale_to_utf(new_dir); - string full_old_dir = Config.mpd_music_dir + editor_browsed_dir + "/" + old_dir; - string full_new_dir = Config.mpd_music_dir + editor_browsed_dir + "/" + new_dir; + string full_old_dir = Config.mpd_music_dir + editor_browsed_dir + "/" + locale_to_utf_cpy(old_dir); + string full_new_dir = Config.mpd_music_dir + editor_browsed_dir + "/" + locale_to_utf_cpy(new_dir); if (rename(full_old_dir.c_str(), full_new_dir.c_str()) == 0) { Mpd->UpdateDirectory(editor_browsed_dir); } else { - utf_to_locale(old_dir); - utf_to_locale(new_dir); ShowMessage("Cannot rename '%s' to '%s'!", old_dir.c_str(), new_dir.c_str()); } } @@ -2700,19 +2675,15 @@ int main(int argc, char *argv[]) UnlockStatusbar(); if (!new_dir.empty() && new_dir != old_dir) { - locale_to_utf(old_dir); - locale_to_utf(new_dir); string full_old_dir; if (!Config.local_browser) full_old_dir += Config.mpd_music_dir; - full_old_dir += old_dir; + full_old_dir += locale_to_utf_cpy(old_dir); string full_new_dir; if (!Config.local_browser) full_new_dir += Config.mpd_music_dir; - full_new_dir += new_dir; + full_new_dir += locale_to_utf_cpy(new_dir); int rename_result = rename(full_old_dir.c_str(), full_new_dir.c_str()); - utf_to_locale(old_dir); - utf_to_locale(new_dir); if (rename_result == 0) { ShowMessage("'%s' renamed to '%s'", old_dir.c_str(), new_dir.c_str()); @@ -2733,11 +2704,7 @@ int main(int argc, char *argv[]) UnlockStatusbar(); if (!new_name.empty() && new_name != old_name) { - locale_to_utf(old_name); - locale_to_utf(new_name); - Mpd->Rename(old_name, new_name); - utf_to_locale(old_name); - utf_to_locale(new_name); + Mpd->Rename(locale_to_utf_cpy(old_name), locale_to_utf_cpy(new_name)); ShowMessage("Playlist '%s' renamed to '%s'", old_name.c_str(), new_name.c_str()); if (!Config.local_browser) GetDirectory("/"); @@ -2782,10 +2749,8 @@ int main(int argc, char *argv[]) Config.local_browser = !s->IsFromDB(); string option = s->toString(Config.song_status_format); - string localed_dir = s->GetDirectory(); locale_to_utf(option); - utf_to_locale(localed_dir); - GetDirectory(localed_dir); + GetDirectory(s->GetDirectory()); for (size_t i = 0; i < mBrowser->Size(); i++) { if (mBrowser->at(i).type == itSong && option == mBrowser->at(i).song->toString(Config.song_status_format)) diff --git a/src/song.cpp b/src/song.cpp index 0a44114b..7dbb9e90 100644 --- a/src/song.cpp +++ b/src/song.cpp @@ -75,11 +75,13 @@ string Song::GetLength() const return ShowTime(itsSong->time); } -void Song::LocalizeTags() +void Song::Localize() { # if !defined(_UTF8) && defined(HAVE_ICONV_H) if (isLocalised) return; + str_pool_utf_to_locale(itsSong->file); + __Count_Last_Slash_Position(); str_pool_utf_to_locale(itsSong->artist); str_pool_utf_to_locale(itsSong->title); str_pool_utf_to_locale(itsSong->album); @@ -95,11 +97,13 @@ void Song::LocalizeTags() # endif // !_UTF8 && HAVE_ICONV_H } -void Song::DelocalizeTags() +/*void Song::Delocalize() { # if !defined(_UTF8) && defined(HAVE_ICONV_H) if (!isLocalised) return; + str_pool_locale_to_utf(itsSong->file); + __Count_Last_Slash_Position(); str_pool_locale_to_utf(itsSong->artist); str_pool_locale_to_utf(itsSong->title); str_pool_locale_to_utf(itsSong->album); @@ -113,7 +117,7 @@ void Song::DelocalizeTags() str_pool_locale_to_utf(itsSong->comment); isLocalised = 0; # endif // !_UTF8 && HAVE_ICONV_H -} +}*/ void Song::Clear() { diff --git a/src/song.h b/src/song.h index 5b6d09ef..aa2910b7 100644 --- a/src/song.h +++ b/src/song.h @@ -82,12 +82,13 @@ class Song void CopyPtr(bool copy) { copyPtr = copy; } //void GetEmptyFields(bool get) { itsGetEmptyFields = get; } - void LocalizeTags(); - void DelocalizeTags(); + void Localize(); + //void Delocalize(); void Clear(); bool Empty() const; bool IsFromDB() const; bool IsStream() const { return isStream; } + bool Localized() const { return isLocalised; } Song & operator=(const Song &); bool operator==(const Song &) const; diff --git a/src/status_checker.cpp b/src/status_checker.cpp index a6dfe684..7e567cc6 100644 --- a/src/status_checker.cpp +++ b/src/status_checker.cpp @@ -341,9 +341,7 @@ void NcmpcppStatusChanged(Connection *Mpd, StatusChanges changed, void *) if (s.GetTotalLength() && elapsed == s.GetTotalLength()-1) repeat_one_allowed = 1; - string song_str = s.toString(Config.song_window_title_format); - utf_to_locale(song_str); - WindowTitle(song_str); + WindowTitle(utf_to_locale_cpy(s.toString(Config.song_window_title_format))); if (!block_statusbar_update && Config.statusbar_visibility) { @@ -364,9 +362,7 @@ void NcmpcppStatusChanged(Connection *Mpd, StatusChanges changed, void *) } wFooter->WriteXY(0, 1, 1, "%s", player_state.c_str()); wFooter->Bold(0); - song_str = s.toString(Config.song_status_format); - utf_to_locale(song_str); - Scroller(*wFooter, song_str, wFooter->GetWidth()-player_state.length()-tracklength.length(), playing_song_scroll_begin); + Scroller(*wFooter, utf_to_locale_cpy(s.toString(Config.song_status_format)), wFooter->GetWidth()-player_state.length()-tracklength.length(), playing_song_scroll_begin); wFooter->Bold(1); wFooter->WriteXY(wFooter->GetWidth()-tracklength.length(), 1, 1, "%s", tracklength.c_str()); diff --git a/src/tag_editor.cpp b/src/tag_editor.cpp index d5840724..7eb3fdbc 100644 --- a/src/tag_editor.cpp +++ b/src/tag_editor.cpp @@ -28,6 +28,7 @@ #include "textidentificationframe.h" #include "mpegfile.h" +#include "charset.h" #include "helpers.h" #include "status_checker.h" @@ -323,6 +324,7 @@ bool GetSongTags(Song &s) if (s.IsFromDB()) path_to_file += Config.mpd_music_dir; path_to_file += s.GetFile(); + locale_to_utf(path_to_file); TagLib::FileRef f(path_to_file.c_str()); if (f.isNull()) @@ -384,6 +386,7 @@ bool WriteTags(Song &s) if (file_is_from_db) path_to_file += Config.mpd_music_dir; path_to_file += s.GetFile(); + locale_to_utf(path_to_file); FileRef f(path_to_file.c_str()); if (!f.isNull()) { @@ -423,15 +426,12 @@ bool WriteTags(Song &s) } if (!s.GetNewName().empty()) { - string old_name; - if (file_is_from_db) - old_name += Config.mpd_music_dir; - old_name += s.GetFile(); string new_name; if (file_is_from_db) new_name += Config.mpd_music_dir; new_name += s.GetDirectory() + "/" + s.GetNewName(); - if (rename(old_name.c_str(), new_name.c_str()) == 0 && !file_is_from_db) + locale_to_utf(new_name); + if (rename(path_to_file.c_str(), new_name.c_str()) == 0 && !file_is_from_db) { if (wPrev == mPlaylist) {