Code

mpdclient: expect UTF-8 strings
[ncmpc.git] / src / screen_file.c
index fa741cd275b39997f099ed641ace302e03f12ea9..f4229797137d598498f675e0b0df5266e8a7d1fd 100644 (file)
@@ -17,9 +17,9 @@
  */
 
 #include "config.h"
-#include "ncmpc.h"
+#include "i18n.h"
 #include "options.h"
-#include "support.h"
+#include "charset.h"
 #include "mpdclient.h"
 #include "command.h"
 #include "screen.h"
 #include <stdlib.h>
 #include <string.h>
 #include <glib.h>
-#include <ncurses.h>
 
 static struct screen_browser browser;
 
 static void
-browse_paint(mpdclient_t *c);
+browse_paint(void);
 
 static void
 file_repaint(void)
 {
-       browse_paint(NULL);
+       browse_paint();
        wrefresh(browser.lw->w);
 }
 
@@ -59,12 +58,15 @@ file_changed_callback(mpdclient_t *c, mpd_unused int event,
                      mpd_unused gpointer data)
 {
        browser.filelist = mpdclient_filelist_update(c, browser.filelist);
+#ifndef NCMPC_MINI
        sync_highlights(c, browser.filelist);
+#endif
        list_window_check_selected(browser.lw, filelist_length(browser.filelist));
 
        file_repaint_if_active();
 }
 
+#ifndef NCMPC_MINI
 /* the playlist have been updated -> fix highlights */
 static void
 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
@@ -73,12 +75,14 @@ playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
 
        file_repaint_if_active();
 }
+#endif
 
 static int
-handle_save(screen_t *screen, mpdclient_t *c)
+handle_save(mpdclient_t *c)
 {
        filelist_entry_t *entry;
        char *defaultname = NULL;
+       int ret;
 
        if (browser.lw->selected >= filelist_length(browser.filelist))
                return -1;
@@ -92,11 +96,15 @@ handle_save(screen_t *screen, mpdclient_t *c)
                }
        }
 
-       return playlist_save(screen, c, NULL, defaultname);
+       defaultname = utf8_to_locale(defaultname);
+       ret = playlist_save(c, NULL, defaultname);
+       g_free(defaultname);
+
+       return ret;
 }
 
 static int
-handle_delete(screen_t *screen, mpdclient_t *c)
+handle_delete(mpdclient_t *c)
 {
        filelist_entry_t *entry;
        mpd_InfoEntity *entity;
@@ -120,19 +128,17 @@ handle_delete(screen_t *screen, mpdclient_t *c)
        }
 
        plf = entity->info.playlistFile;
-       str = utf8_to_locale(basename(plf->path));
+       str = utf8_to_locale(g_basename(plf->path));
        buf = g_strdup_printf(_("Delete playlist %s [%s/%s] ? "), str, YES, NO);
        g_free(str);
-       key = tolower(screen_getch(screen->status_window.w, buf));
+       key = tolower(screen_getch(screen.status_window.w, buf));
        g_free(buf);
-       if( key==KEY_RESIZE )
-               screen_resize();
        if( key != YES[0] ) {
                screen_status_printf(_("Aborted!"));
                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!"));
@@ -143,7 +149,6 @@ static void
 browse_init(WINDOW *w, int cols, int rows)
 {
        browser.lw = list_window_init(w, cols, rows);
-       browser.lw_state = list_window_init_state();
 }
 
 static void
@@ -159,15 +164,16 @@ browse_exit(void)
        if (browser.filelist)
                filelist_free(browser.filelist);
        list_window_free(browser.lw);
-       list_window_free_state(browser.lw_state);
 }
 
 static void
-browse_open(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
+browse_open(mpd_unused mpdclient_t *c)
 {
        if (browser.filelist == NULL) {
                browser.filelist = mpdclient_filelist_get(c, "");
+#ifndef NCMPC_MINI
                mpdclient_install_playlist_callback(c, playlist_changed_callback);
+#endif
                mpdclient_install_browse_callback(c, file_changed_callback);
        }
 }
@@ -175,33 +181,33 @@ browse_open(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
 static const char *
 browse_title(char *str, size_t size)
 {
-       char *pathcopy;
-       char *parentdir;
-
-       pathcopy = strdup(browser.filelist->path);
-       parentdir = dirname(pathcopy);
-       parentdir = basename(parentdir);
+       const char *path = NULL, *prev = NULL, *slash = browser.filelist->path;
+       char *path_locale;
 
-       if( parentdir[0] == '.' && strlen(parentdir) == 1 ) {
-               parentdir = NULL;
+       /* determine the last 2 parts of the path */
+       while ((slash = strchr(slash, '/')) != NULL) {
+               path = prev;
+               prev = ++slash;
        }
 
-       g_snprintf(str, size, _("Browse: %s%s%s"),
-                  parentdir ? parentdir : "",
-                  parentdir ? "/" : "",
-                  basename(browser.filelist->path));
-       free(pathcopy);
+       if (path == NULL)
+               /* fall back to full path */
+               path = browser.filelist->path;
+
+       path_locale = utf8_to_locale(path);
+       g_snprintf(str, size, _("Browse: %s"), path_locale);
+       g_free(path_locale);
        return str;
 }
 
 static void
-browse_paint(mpd_unused mpdclient_t *c)
+browse_paint(void)
 {
        list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
 }
 
 static int
-browse_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
+browse_cmd(mpdclient_t *c, command_t cmd)
 {
        switch(cmd) {
        case CMD_GO_ROOT_DIRECTORY:
@@ -214,15 +220,17 @@ browse_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
                return 1;
 
        case CMD_DELETE:
-               handle_delete(screen, c);
+               handle_delete(c);
                file_repaint();
                break;
        case CMD_SAVE_PLAYLIST:
-               handle_save(screen, c);
+               handle_save(c);
                break;
        case CMD_SCREEN_UPDATE:
                browser.filelist = mpdclient_filelist_update(c, browser.filelist);
+#ifndef NCMPC_MINI
                sync_highlights(c, browser.filelist);
+#endif
                list_window_check_selected(browser.lw,
                                           filelist_length(browser.filelist));
                file_repaint();
@@ -236,10 +244,13 @@ browse_cmd(screen_t *screen, 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
@@ -254,7 +265,7 @@ browse_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
                break;
        }
 
-       if (browser_cmd(&browser, screen, c, cmd)) {
+       if (browser_cmd(&browser, c, cmd)) {
                file_repaint();
                return 1;
        }
@@ -271,4 +282,3 @@ const struct screen_functions screen_browse = {
        .cmd = browse_cmd,
        .get_title = browse_title,
 };
-