Code

mpdclient: expect UTF-8 strings
authorMax Kellermann <max@duempel.org>
Fri, 7 Nov 2008 15:14:39 +0000 (16:14 +0100)
committerMax Kellermann <max@duempel.org>
Fri, 7 Nov 2008 15:14:39 +0000 (16:14 +0100)
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
src/mpdclient.c
src/mpdclient.h
src/screen_artist.c
src/screen_browser.c
src/screen_file.c
src/screen_play.c
src/screen_search.c

index 1e65cfeb8bf16145b6415dfb2737ae2a28e690fa..b9fb3cd0e58642c95dd19d3f2c8f2a8b274ef80d 100644 (file)
@@ -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
index df636a566caea042d455aa648d673d8293c24c6c..7907eead5d293862265a95b2bfea884d40b70036 100644 (file)
@@ -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)
 {
index a78aafa345d9278214bdfea68d9dbdc09cd535f8..d0d5cdfd23f62488d034eb2237c462571d68637b 100644 (file)
@@ -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);
 
index 5fec3c04c980c178b2172e001694eb471dadb3ca..35db917762e1ec76f1dfaa094edc7091d760baac 100644 (file)
@@ -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);
index a09e2554ccd5ef48e67d95badbd191c9c727d264..69e39ffaf57f2023944f794a561116e69b731378 100644 (file)
@@ -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);
index 75e4d163111e4fadec0d503f7577c3bd4aecff46..f4229797137d598498f675e0b0df5266e8a7d1fd 100644 (file)
@@ -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
index d4fb7cee239f3076f6202f889379c4270fd98300..8855ffc7816e93912f932e253d3ee160a4be9e1c 100644 (file)
@@ -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;
index 51a8a2966c064f29f873ef2d799f6d9f1ff4468f..7cb04e85fb6ea29634874dbae1de902f40151ffb 100644 (file)
@@ -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;
 }