get rid of locale_to_utf where it's not needed anymore

This commit is contained in:
Andrzej Rybczak
2012-09-11 22:41:42 +02:00
parent 63413368c8
commit c8097a898e
10 changed files with 83 additions and 89 deletions

View File

@@ -901,7 +901,6 @@ void SavePlaylist::Run()
Statusbar::lock();
Statusbar::put() << "Save playlist as: ";
std::string playlist_name = wFooter->getString();
std::string real_playlist_name = locale_to_utf_cpy(playlist_name);
Statusbar::unlock();
if (playlist_name.find("/") != std::string::npos)
{
@@ -914,14 +913,14 @@ void SavePlaylist::Run()
{
Mpd.StartCommandsList();
for (size_t i = 0; i < myPlaylist->Items->size(); ++i)
Mpd.AddToPlaylist(real_playlist_name, (*myPlaylist->Items)[i].value());
Mpd.AddToPlaylist(playlist_name, (*myPlaylist->Items)[i].value());
Mpd.CommitCommandsList();
if (Mpd.GetErrorMessage().empty())
Statusbar::msg("Filtered items added to playlist \"%s\"", playlist_name.c_str());
}
else
{
int result = Mpd.SavePlaylist(real_playlist_name);
int result = Mpd.SavePlaylist(playlist_name);
if (result == MPD_ERROR_SUCCESS)
{
Statusbar::msg("Playlist saved as \"%s\"", playlist_name.c_str());
@@ -933,8 +932,8 @@ void SavePlaylist::Run()
bool yes = AskYesNoQuestion("Playlist \"" + playlist_name + "\" already exists, overwrite?", TraceMpdStatus);
if (yes)
{
Mpd.DeletePlaylist(real_playlist_name);
if (Mpd.SavePlaylist(real_playlist_name) == MPD_ERROR_SUCCESS)
Mpd.DeletePlaylist(playlist_name);
if (Mpd.SavePlaylist(playlist_name) == MPD_ERROR_SUCCESS)
Statusbar::msg("Playlist overwritten");
}
else
@@ -1062,7 +1061,6 @@ void Add::Run()
Statusbar::lock();
Statusbar::put() << (myScreen == myPlaylistEditor ? "Add to playlist: " : "Add: ");
std::string path = wFooter->getString();
locale_to_utf(path);
Statusbar::unlock();
if (!path.empty())
{
@@ -1210,7 +1208,7 @@ void TogglePlayingSongCentering::Run()
void UpdateDatabase::Run()
{
if (myScreen == myBrowser)
Mpd.UpdateDirectory(locale_to_utf_cpy(myBrowser->CurrentDir()));
Mpd.UpdateDirectory(myBrowser->CurrentDir());
# ifdef HAVE_TAGLIB_H
else if (myScreen == myTagEditor)
Mpd.UpdateDirectory(myTagEditor->CurrentDir());
@@ -1370,7 +1368,7 @@ void EditLibraryTag::Run()
{
Statusbar::msg("Updating tags...");
Mpd.StartSearch(1);
Mpd.AddSearch(Config.media_lib_primary_tag, locale_to_utf_cpy(myLibrary->Tags->current().value()));
Mpd.AddSearch(Config.media_lib_primary_tag, myLibrary->Tags->current().value());
MPD::MutableSong::SetFunction set = tagTypeToSetFunction(Config.media_lib_primary_tag);
assert(set);
bool success = true;
@@ -1482,18 +1480,18 @@ void EditDirectoryName::Run()
std::string full_old_dir;
if (!myBrowser->isLocal())
full_old_dir += Config.mpd_music_dir;
full_old_dir += locale_to_utf_cpy(old_dir);
full_old_dir += old_dir;
std::string full_new_dir;
if (!myBrowser->isLocal())
full_new_dir += Config.mpd_music_dir;
full_new_dir += locale_to_utf_cpy(new_dir);
full_new_dir += new_dir;
int rename_result = rename(full_old_dir.c_str(), full_new_dir.c_str());
if (rename_result == 0)
{
const char msg[] = "Directory renamed to \"%ls\"";
Statusbar::msg(msg, wideShorten(ToWString(new_dir), COLS-const_strlen(msg)).c_str());
if (!myBrowser->isLocal())
Mpd.UpdateDirectory(locale_to_utf_cpy(getSharedDirectory(old_dir, new_dir)));
Mpd.UpdateDirectory(getSharedDirectory(old_dir, new_dir));
myBrowser->GetDirectory(myBrowser->CurrentDir());
}
else
@@ -1513,8 +1511,8 @@ void EditDirectoryName::Run()
Statusbar::unlock();
if (!new_dir.empty() && new_dir != old_dir)
{
std::string full_old_dir = Config.mpd_music_dir + myTagEditor->CurrentDir() + "/" + locale_to_utf_cpy(old_dir);
std::string full_new_dir = Config.mpd_music_dir + myTagEditor->CurrentDir() + "/" + locale_to_utf_cpy(new_dir);
std::string full_old_dir = Config.mpd_music_dir + myTagEditor->CurrentDir() + "/" + old_dir;
std::string full_new_dir = Config.mpd_music_dir + myTagEditor->CurrentDir() + "/" + new_dir;
if (rename(full_old_dir.c_str(), full_new_dir.c_str()) == 0)
{
const char msg[] = "Directory renamed to \"%ls\"";
@@ -1555,7 +1553,7 @@ void EditPlaylistName::Run()
Statusbar::unlock();
if (!new_name.empty() && new_name != old_name)
{
if (Mpd.Rename(locale_to_utf_cpy(old_name), locale_to_utf_cpy(new_name)))
if (Mpd.Rename(old_name, new_name))
{
const char msg[] = "Playlist renamed to \"%ls\"";
Statusbar::msg(msg, wideShorten(ToWString(new_name), COLS-const_strlen(msg)).c_str());

View File

@@ -157,7 +157,7 @@ void Browser::EnterPressed()
}
case itPlaylist:
{
if (Mpd.LoadPlaylist(locale_to_utf_cpy(item.name)))
if (Mpd.LoadPlaylist(item.name))
{
Statusbar::msg("Playlist \"%s\" loaded", item.name.c_str());
myPlaylist->PlayNewlyAddedSongs();
@@ -204,7 +204,7 @@ void Browser::SpacePressed()
}
else
# endif // !WIN32
result = Mpd.Add(locale_to_utf_cpy(item.name));
result = Mpd.Add(item.name);
if (result)
Statusbar::msg("Directory \"%s\" added", item.name.c_str());
break;
@@ -216,7 +216,7 @@ void Browser::SpacePressed()
}
case itPlaylist:
{
if (Mpd.LoadPlaylist(locale_to_utf_cpy(item.name)))
if (Mpd.LoadPlaylist(item.name))
Statusbar::msg("Playlist \"%s\" loaded", item.name.c_str());
break;
}
@@ -403,8 +403,6 @@ void Browser::GetDirectory(std::string dir, std::string subdir)
w->reset();
itsBrowsedDir = dir;
locale_to_utf(dir);
w->clear();
if (dir != "/")
@@ -434,13 +432,11 @@ void Browser::GetDirectory(std::string dir, std::string subdir)
{
case itPlaylist:
{
utf_to_locale(it->name);
w->addItem(*it);
break;
}
case itDirectory:
{
utf_to_locale(it->name);
if (it->name == subdir)
highlightme = w->size();
w->addItem(*it);
@@ -589,7 +585,7 @@ bool Browser::deleteItem(const MPD::Item &item)
// playlist created by mpd
if (!isLocal() && item.type == itPlaylist && CurrentDir() == "/")
return Mpd.DeletePlaylist(locale_to_utf_cpy(item.name));
return Mpd.DeletePlaylist(item.name);
std::string path;
if (!isLocal())

View File

@@ -31,8 +31,8 @@
#include "settings.h"
namespace
{
namespace {//
bool is_utf8(const char *s)
{
for (; *s; ++s)
@@ -61,36 +61,43 @@ namespace
return false;
}
void charset_convert(const char *from, const char *to, const char *&inbuf, bool delete_old, size_t len = 0)
void charset_convert(const char *from, const char *to, const char *&inbuf,
bool delete_old, size_t len = 0)
{
if (!inbuf || !from || !to)
return;
assert(inbuf);
assert(from);
assert(to);
iconv_t cd = iconv_open(to, from);
if (cd == iconv_t(-1))
{
std::cerr << "Error while executing iconv_open: " << strerror(errno) << "\n";
return;
}
if (!len)
len = strlen(inbuf);
size_t buflen = len*6+1;
char *outbuf = new char[buflen];
size_t buflen = len*MB_CUR_MAX+1;
char *outbuf = static_cast<char *>(malloc(buflen));
char *outstart = outbuf;
const char *instart = inbuf;
if (iconv(cd, const_cast<ICONV_CONST char **>(&inbuf), &len, &outbuf, &buflen) == size_t(-1))
{
std::cerr << "Error while executing iconv: " << strerror(errno) << "\n";
inbuf = instart;
delete [] outstart;
iconv_close(cd);
return;
}
iconv_close(cd);
else
{
*outbuf = 0;
if (delete_old)
free(const_cast<char *>(instart));
inbuf = outstart;
}
iconv_close(cd);
}
}
void iconv_convert_from_to(const char *from, const char *to, std::string &s)
@@ -103,7 +110,7 @@ void iconv_convert_from_to(const char *from, const char *to, std::string &s)
void utf_to_locale(std::string &s)
{
if (s.empty() || Config.system_encoding.empty() || !has_non_ascii_chars(s.c_str()))
if (Config.system_encoding.empty() || !has_non_ascii_chars(s.c_str()))
return;
const char *tmp = strdup(s.c_str());
charset_convert("utf-8", Config.system_encoding.c_str(), tmp, 1, s.length());
@@ -120,7 +127,7 @@ std::string utf_to_locale_cpy(const std::string &s)
void locale_to_utf(std::string &s)
{
if (s.empty() || Config.system_encoding.empty() || !has_non_ascii_chars(s.c_str()) || is_utf8(s.c_str()))
if (Config.system_encoding.empty() || !has_non_ascii_chars(s.c_str()) || is_utf8(s.c_str()))
return;
const char *tmp = strdup(s.c_str());
charset_convert(Config.system_encoding.c_str(), "utf-8", tmp, 1, s.length());

View File

@@ -21,9 +21,7 @@
#ifndef _CHARSET_H
#define _CHARSET_H
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "config.h"
#ifdef HAVE_ICONV_H

View File

@@ -123,8 +123,6 @@ void Lastfm::Load()
w->reset();
std::string artist = itsArgs.find("artist")->second;
locale_to_utf(artist);
std::string file = lowercase(artist + ".txt");
removeInvalidCharsFromFilename(file);

View File

@@ -244,8 +244,8 @@ void Lyrics::DownloadInBackgroundImplHelper(const MPD::Song &s)
void *Lyrics::Download()
{
std::string artist = Curl::escape(locale_to_utf_cpy(itsSong.getArtist()));
std::string title = Curl::escape(locale_to_utf_cpy(itsSong.getTitle()));
std::string artist = Curl::escape(itsSong.getArtist());
std::string title = Curl::escape(itsSong.getTitle());
LyricsFetcher::Result result;
@@ -267,9 +267,8 @@ void *Lyrics::Download()
if (result.first == true)
{
Save(itsFilename, result.second);
utf_to_locale(result.second);
w->clear();
utf_to_locale(result.second);
*w << result.second;
}
else
@@ -306,9 +305,9 @@ std::string Lyrics::GenerateFilename(const MPD::Song &s)
}
else
{
std::string file = locale_to_utf_cpy(s.getArtist());
std::string file = s.getArtist();
file += " - ";
file += locale_to_utf_cpy(s.getTitle());
file += s.getTitle();
file += ".txt";
removeInvalidCharsFromFilename(file);
filename = Config.lyrics_directory;

View File

@@ -350,12 +350,12 @@ void MediaLibrary::Update()
Songs->reset();
Mpd.StartSearch(1);
Mpd.AddSearch(Config.media_lib_primary_tag, locale_to_utf_cpy(hasTwoColumns ? Albums->current().value().PrimaryTag : Tags->current().value()));
Mpd.AddSearch(Config.media_lib_primary_tag, hasTwoColumns ? Albums->current().value().PrimaryTag : Tags->current().value());
if (Albums->current().value().Date != AllTracksMarker)
{
Mpd.AddSearch(MPD_TAG_ALBUM, locale_to_utf_cpy(Albums->current().value().Album));
Mpd.AddSearch(MPD_TAG_ALBUM, Albums->current().value().Album);
if (Config.media_library_display_date)
Mpd.AddSearch(MPD_TAG_DATE, locale_to_utf_cpy(Albums->current().value().Date));
Mpd.AddSearch(MPD_TAG_DATE, Albums->current().value().Date);
}
auto songs = Mpd.CommitSearchSongs();
for (auto s = songs.begin(); s != songs.end(); ++s)

View File

@@ -277,7 +277,7 @@ void PlaylistEditor::AddToPlaylist(bool add_n_play)
if (w == Playlists && !Playlists->empty())
{
if (Mpd.LoadPlaylist(utf_to_locale_cpy(Playlists->current().value())))
if (Mpd.LoadPlaylist(Playlists->current().value()))
{
Statusbar::msg("Playlist \"%s\" loaded", Playlists->current().value().c_str());
if (add_n_play)

View File

@@ -179,17 +179,16 @@ void SelectedItemsAdder::EnterPressed()
Statusbar::unlock();
if (!playlist.empty())
{
std::string utf_playlist = locale_to_utf_cpy(playlist);
Mpd.StartCommandsList();
for (auto it = list.begin(); it != list.end(); ++it)
Mpd.AddToPlaylist(utf_playlist, *it);
Mpd.AddToPlaylist(playlist, *it);
if (Mpd.CommitCommandsList())
Statusbar::msg("Selected item(s) added to playlist \"%s\"", playlist.c_str());
}
}
else if (pos > 1 && pos < w->size()-1) // add items to existing playlist
{
std::string playlist = locale_to_utf_cpy(w->current().value());
std::string playlist = w->current().value();
Mpd.StartCommandsList();
for (auto it = list.begin(); it != list.end(); ++it)
Mpd.AddToPlaylist(playlist, *it);

View File

@@ -1078,7 +1078,6 @@ bool TagEditor::WriteTags(MPD::MutableSong &s)
if (file_is_from_db)
new_name += Config.mpd_music_dir;
new_name += s.getDirectory() + "/" + s.getNewURI();
locale_to_utf(new_name);
if (rename(path_to_file.c_str(), new_name.c_str()) == 0 && !file_is_from_db)
{
if (Global::myOldScreen == myPlaylist)