From c238364f6833320fee4256226af650347d5a751b Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 7 Nov 2008 16:14:39 +0100 Subject: [PATCH] mpdclient: expect UTF-8 strings Don't convert the character set of strings to and from the current locale. This library cannot know what the strings are going to be used for, so it should not mess with them. --- src/main.c | 6 +++- src/mpdclient.c | 75 ++++++-------------------------------------- src/mpdclient.h | 11 ++----- src/screen_artist.c | 14 ++++----- src/screen_browser.c | 4 +-- src/screen_file.c | 23 ++++++++++---- src/screen_play.c | 23 +++++++++++--- src/screen_search.c | 8 +++-- 8 files changed, 65 insertions(+), 99 deletions(-) diff --git a/src/main.c b/src/main.c index 1e65cfe..b9fb3cd 100644 --- a/src/main.c +++ b/src/main.c @@ -74,8 +74,10 @@ error_msg(const gchar *msg) } static void -error_callback(mpd_unused mpdclient_t *c, gint error, const gchar *msg) +error_callback(mpd_unused mpdclient_t *c, gint error, const gchar *_msg) { + char *msg = utf8_to_locale(_msg); + error = error & 0xFF; switch (error) { case MPD_ERROR_CONNPORT: @@ -91,6 +93,8 @@ error_callback(mpd_unused mpdclient_t *c, gint error, const gchar *msg) doupdate(); connected = FALSE; } + + g_free(msg); } #ifndef NCMPC_MINI diff --git a/src/mpdclient.c b/src/mpdclient.c index df636a5..7907eea 100644 --- a/src/mpdclient.c +++ b/src/mpdclient.c @@ -19,7 +19,6 @@ #include "mpdclient.h" #include "screen_utils.h" #include "config.h" -#include "charset.h" #include "options.h" #include "strfsong.h" @@ -123,7 +122,6 @@ mpdclient_finish_command(mpdclient_t *c) if (c->connection->error) { gint error = c->connection->error; - gchar *msg; if (error == MPD_ERROR_ACK && c->connection->errorCode == MPD_ACK_ERROR_PERMISSION && @@ -133,9 +131,7 @@ mpdclient_finish_command(mpdclient_t *c) if (error == MPD_ERROR_ACK) error = error | (c->connection->errorCode << 8); - msg = locale_to_utf8(c->connection->errorStr); - error_cb(c, error, msg); - g_free(msg); + error_cb(c, error, c->connection->errorStr); return error; } @@ -399,23 +395,12 @@ mpdclient_cmd_volume(mpdclient_t *c, gint value) } gint -mpdclient_cmd_add_path_utf8(mpdclient_t *c, gchar *path_utf8) +mpdclient_cmd_add_path(mpdclient_t *c, gchar *path_utf8) { mpd_sendAddCommand(c->connection, path_utf8); return mpdclient_finish_command(c); } -gint -mpdclient_cmd_add_path(mpdclient_t *c, gchar *path) -{ - gint retval; - gchar *path_utf8 = locale_to_utf8(path); - - retval=mpdclient_cmd_add_path_utf8(c, path_utf8); - g_free(path_utf8); - return retval; -} - gint mpdclient_cmd_add(mpdclient_t *c, struct mpd_song *song) { @@ -530,7 +515,7 @@ mpdclient_cmd_move(mpdclient_t *c, gint old_index, gint new_index) } gint -mpdclient_cmd_save_playlist_utf8(mpdclient_t *c, gchar *filename_utf8) +mpdclient_cmd_save_playlist(mpdclient_t *c, gchar *filename_utf8) { gint retval = 0; @@ -540,17 +525,6 @@ mpdclient_cmd_save_playlist_utf8(mpdclient_t *c, gchar *filename_utf8) return retval; } -gint -mpdclient_cmd_save_playlist(mpdclient_t *c, gchar *filename) -{ - gint retval = 0; - gchar *filename_utf8 = locale_to_utf8(filename); - - retval = mpdclient_cmd_save_playlist_utf8(c, filename); - g_free(filename_utf8); - return retval; -} - gint mpdclient_cmd_load_playlist_utf8(mpdclient_t *c, gchar *filename_utf8) { @@ -560,7 +534,7 @@ mpdclient_cmd_load_playlist_utf8(mpdclient_t *c, gchar *filename_utf8) } gint -mpdclient_cmd_delete_playlist_utf8(mpdclient_t *c, gchar *filename_utf8) +mpdclient_cmd_delete_playlist(mpdclient_t *c, gchar *filename_utf8) { gint retval = 0; @@ -570,17 +544,6 @@ mpdclient_cmd_delete_playlist_utf8(mpdclient_t *c, gchar *filename_utf8) return retval; } -gint -mpdclient_cmd_delete_playlist(mpdclient_t *c, gchar *filename) -{ - gint retval = 0; - gchar *filename_utf8 = locale_to_utf8(filename); - - retval = mpdclient_cmd_delete_playlist_utf8(c, filename_utf8); - g_free(filename_utf8); - return retval; -} - /****************************************************************************/ /*** Callback managment functions *******************************************/ @@ -740,10 +703,9 @@ mpdclient_filelist_get(mpdclient_t *c, const gchar *path) { mpdclient_filelist_t *filelist; mpd_InfoEntity *entity; - gchar *path_utf8 = locale_to_utf8(path); gboolean has_dirs_only = TRUE; - mpd_sendLsInfoCommand(c->connection, path_utf8); + mpd_sendLsInfoCommand(c->connection, path); filelist = filelist_new(path); if (path && path[0] && strcmp(path, "/")) /* add a dummy entry for ./.. */ @@ -760,8 +722,6 @@ mpdclient_filelist_get(mpdclient_t *c, const gchar *path) /* If there's an error, ignore it. We'll return an empty filelist. */ mpdclient_finish_command(c); - g_free(path_utf8); - // If there are only directory entities in the filelist, we sort it if (has_dirs_only) filelist_sort(filelist, compare_filelistentry_dir); @@ -770,10 +730,10 @@ mpdclient_filelist_get(mpdclient_t *c, const gchar *path) } mpdclient_filelist_t * -mpdclient_filelist_search_utf8(mpdclient_t *c, - int exact_match, - int table, - gchar *filter_utf8) +mpdclient_filelist_search(mpdclient_t *c, + int exact_match, + int table, + gchar *filter_utf8) { mpdclient_filelist_t *filelist; mpd_InfoEntity *entity; @@ -795,23 +755,6 @@ mpdclient_filelist_search_utf8(mpdclient_t *c, return filelist; } - -mpdclient_filelist_t * -mpdclient_filelist_search(mpdclient_t *c, - int exact_match, - int table, - gchar *_filter) -{ - mpdclient_filelist_t *filelist; - gchar *filter_utf8 = locale_to_utf8(_filter); - - filelist = mpdclient_filelist_search_utf8(c, exact_match, table, - filter_utf8); - g_free(filter_utf8); - - return filelist; -} - mpdclient_filelist_t * mpdclient_filelist_update(mpdclient_t *c, mpdclient_filelist_t *filelist) { diff --git a/src/mpdclient.h b/src/mpdclient.h index a78aafa..d0d5cdf 100644 --- a/src/mpdclient.h +++ b/src/mpdclient.h @@ -60,17 +60,14 @@ gint mpdclient_cmd_crossfade(mpdclient_t *c, gint value); gint mpdclient_cmd_db_update_utf8(mpdclient_t *c, gchar *path); gint mpdclient_cmd_volume(mpdclient_t *c, gint value); gint mpdclient_cmd_add_path(mpdclient_t *c, gchar *path); -gint mpdclient_cmd_add_path_utf8(mpdclient_t *c, gchar *path); gint mpdclient_cmd_add(mpdclient_t *c, struct mpd_song *song); gint mpdclient_cmd_delete(mpdclient_t *c, gint index); gint mpdclient_cmd_move(mpdclient_t *c, gint old_index, gint new_index); gint mpdclient_cmd_save_playlist(mpdclient_t *c, gchar *filename); -gint mpdclient_cmd_save_playlist_utf8(mpdclient_t *c, gchar *filename); gint mpdclient_cmd_load_playlist_utf8(mpdclient_t *c, gchar *filename_utf8); -gint mpdclient_cmd_delete_playlist(mpdclient_t *c, gchar *filename); -gint mpdclient_cmd_delete_playlist_utf8(mpdclient_t *c, gchar *filename_utf8); +gint mpdclient_cmd_delete_playlist(mpdclient_t *c, gchar *filename_utf8); /* list functions */ GList *mpdclient_get_artists_utf8(mpdclient_t *c); @@ -124,11 +121,7 @@ mpdclient_filelist_t *mpdclient_filelist_get(mpdclient_t *c, const gchar *path); mpdclient_filelist_t *mpdclient_filelist_search(mpdclient_t *c, int exact_match, int table, - gchar *path); -mpdclient_filelist_t *mpdclient_filelist_search_utf8(mpdclient_t *c, - int exact_match, - int table, - gchar *path); + gchar *filter_utf8); mpdclient_filelist_t *mpdclient_filelist_update(mpdclient_t *c, mpdclient_filelist_t *flist); diff --git a/src/screen_artist.c b/src/screen_artist.c index 5fec3c0..35db917 100644 --- a/src/screen_artist.c +++ b/src/screen_artist.c @@ -214,14 +214,14 @@ load_song_list(struct mpdclient *c) if (album[0] == 0) browser.filelist = - mpdclient_filelist_search_utf8(c, TRUE, - MPD_TABLE_ARTIST, - artist); + mpdclient_filelist_search(c, TRUE, + MPD_TABLE_ARTIST, + artist); else browser.filelist = - mpdclient_filelist_search_utf8(c, TRUE, - MPD_TABLE_ALBUM, - album); + mpdclient_filelist_search(c, TRUE, + MPD_TABLE_ALBUM, + album); if (browser.filelist == NULL) browser.filelist = filelist_new(NULL); @@ -414,7 +414,7 @@ add_query(mpdclient_t *c, int table, char *_filter) screen_status_printf("Adding %s...", str); g_free(str); - addlist = mpdclient_filelist_search_utf8(c, TRUE, table, _filter); + addlist = mpdclient_filelist_search(c, TRUE, table, _filter); if (addlist) { mpdclient_filelist_add_all(c, addlist); filelist_free(addlist); diff --git a/src/screen_browser.c b/src/screen_browser.c index a09e255..69e39ff 100644 --- a/src/screen_browser.c +++ b/src/screen_browser.c @@ -197,7 +197,7 @@ browser_change_directory(struct screen_browser *browser, mpdclient_t *c, } else if( entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY) { /* enter sub */ mpd_Directory *dir = entity->info.directory; - path = utf8_to_locale(dir->path); + path = g_strdup(dir->path); } else return -1; @@ -360,7 +360,7 @@ browser_select_entry(mpdclient_t *c, filelist_entry_t *entry, #ifdef USE_OLD_ADD add_directory(c, tmp); #else - if (mpdclient_cmd_add_path_utf8(c, dir->path) == 0) { + if (mpdclient_cmd_add_path(c, dir->path) == 0) { char *tmp = utf8_to_locale(dir->path); screen_status_printf(_("Adding \'%s\' to playlist\n"), tmp); diff --git a/src/screen_file.c b/src/screen_file.c index 75e4d16..f422979 100644 --- a/src/screen_file.c +++ b/src/screen_file.c @@ -82,6 +82,7 @@ handle_save(mpdclient_t *c) { filelist_entry_t *entry; char *defaultname = NULL; + int ret; if (browser.lw->selected >= filelist_length(browser.filelist)) return -1; @@ -95,7 +96,11 @@ handle_save(mpdclient_t *c) } } - return playlist_save(c, NULL, defaultname); + defaultname = utf8_to_locale(defaultname); + ret = playlist_save(c, NULL, defaultname); + g_free(defaultname); + + return ret; } static int @@ -133,7 +138,7 @@ handle_delete(mpdclient_t *c) return 0; } - if( mpdclient_cmd_delete_playlist_utf8(c, plf->path) ) + if( mpdclient_cmd_delete_playlist(c, plf->path) ) return -1; screen_status_printf(_("Playlist deleted!")); @@ -177,6 +182,7 @@ static const char * browse_title(char *str, size_t size) { const char *path = NULL, *prev = NULL, *slash = browser.filelist->path; + char *path_locale; /* determine the last 2 parts of the path */ while ((slash = strchr(slash, '/')) != NULL) { @@ -188,7 +194,9 @@ browse_title(char *str, size_t size) /* fall back to full path */ path = browser.filelist->path; - g_snprintf(str, size, _("Browse: %s"), path); + path_locale = utf8_to_locale(path); + g_snprintf(str, size, _("Browse: %s"), path_locale); + g_free(path_locale); return str; } @@ -236,10 +244,13 @@ browse_cmd(mpdclient_t *c, command_t cmd) if (!c->status->updatingDb) { if (mpdclient_cmd_db_update_utf8(c, browser.filelist->path) == 0) { - if (strcmp(browser.filelist->path, "")) + if (strcmp(browser.filelist->path, "")) { + char *path_locale = + utf8_to_locale(browser.filelist->path); screen_status_printf(_("Database update of %s started!"), - browser.filelist->path); - else + path_locale); + g_free(path_locale); + } else screen_status_printf(_("Database update started!")); /* set updatingDb to make shure the browse callback gets called diff --git a/src/screen_play.c b/src/screen_play.c index d4fb7ce..8855ffc 100644 --- a/src/screen_play.c +++ b/src/screen_play.c @@ -18,6 +18,7 @@ #include "config.h" #include "i18n.h" +#include "charset.h" #include "options.h" #include "support.h" #include "mpdclient.h" @@ -156,7 +157,7 @@ save_post_completion_cb(mpd_unused GCompletion *gcmp, mpd_unused gchar *line, int playlist_save(mpdclient_t *c, char *name, char *defaultname) { - gchar *filename; + gchar *filename, *filename_utf8; gint error; #ifndef NCMPC_MINI GCompletion *gcmp; @@ -204,7 +205,12 @@ playlist_save(mpdclient_t *c, char *name, char *defaultname) return -1; /* send save command to mpd */ - if ((error = mpdclient_cmd_save_playlist(c, filename))) { + + filename_utf8 = locale_to_utf8(filename); + error = mpdclient_cmd_save_playlist(c, filename_utf8); + g_free(filename_utf8); + + if (error) { gint code = GET_ACK_ERROR_CODE(error); if (code == MPD_ACK_ERROR_EXIST) { @@ -218,7 +224,11 @@ playlist_save(mpdclient_t *c, char *name, char *defaultname) g_free(buf); if (key == YES[0]) { - if (mpdclient_cmd_delete_playlist(c, filename)) { + filename_utf8 = locale_to_utf8(filename); + error = mpdclient_cmd_delete_playlist(c, filename_utf8); + g_free(filename_utf8); + + if (error) { g_free(filename); return -1; } @@ -333,8 +343,11 @@ handle_add_to_playlist(mpdclient_t *c) #endif /* add the path to the playlist */ - if (path && path[0]) - mpdclient_cmd_add_path(c, path); + if (path && path[0]) { + char *path_utf8 = locale_to_utf8(path); + mpdclient_cmd_add_path(c, path_utf8); + g_free(path_utf8); + } g_free(path); return 0; diff --git a/src/screen_search.c b/src/screen_search.c index 51a8a29..7cb04e8 100644 --- a/src/screen_search.c +++ b/src/screen_search.c @@ -180,15 +180,16 @@ filelist_search(mpdclient_t *c, mpd_unused int exact_match, int table, gchar *local_pattern) { mpdclient_filelist_t *list, *list2; + gchar *filter_utf8 = locale_to_utf8(local_pattern); if (table == SEARCH_ARTIST_TITLE) { list = mpdclient_filelist_search(c, FALSE, MPD_TABLE_ARTIST, - local_pattern); + filter_utf8); if (list == NULL) list = filelist_new(NULL); list2 = mpdclient_filelist_search(c, FALSE, MPD_TABLE_TITLE, - local_pattern); + filter_utf8); if (list2 != NULL) { filelist_move(list, list2); filelist_free(list2); @@ -196,11 +197,12 @@ filelist_search(mpdclient_t *c, mpd_unused int exact_match, int table, filelist_sort(list, compare_filelistentry_format); } else { - list = mpdclient_filelist_search(c, FALSE, table, local_pattern); + list = mpdclient_filelist_search(c, FALSE, table, filter_utf8); if (list == NULL) list = filelist_new(NULL); } + g_free(filter_utf8); return list; } -- 2.30.2