Code

Rename variable sun as it is predefined (to 1) on solaris
[ncmpc.git] / src / screen_file.c
index 5a81e8fb8c0eccaeaf79f83acfee22d2736da4a4..1f39395c9d839e1c96d896609ccf054e6f3d25ac 100644 (file)
@@ -26,7 +26,6 @@
 #include "screen_utils.h"
 #include "screen_browser.h"
 #include "screen_play.h"
-#include "gcc.h"
 
 #include <ctype.h>
 #include <stdlib.h>
@@ -54,8 +53,8 @@ file_repaint_if_active(void)
 
 /* the db have changed -> update the filelist */
 static void
-file_changed_callback(mpdclient_t *c, mpd_unused int event,
-                     mpd_unused gpointer data)
+file_changed_callback(mpdclient_t *c, G_GNUC_UNUSED int event,
+                     G_GNUC_UNUSED gpointer data)
 {
        browser.filelist = mpdclient_filelist_update(c, browser.filelist);
 #ifndef NCMPC_MINI
@@ -82,6 +81,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 +95,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
@@ -117,7 +121,7 @@ handle_delete(mpdclient_t *c)
        entity = entry->entity;
 
        if( entity->type!=MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) {
-               screen_status_printf(_("You can only delete playlists!"));
+               screen_status_printf(_("You can only delete playlists"));
                screen_bell();
                return -1;
        }
@@ -129,14 +133,14 @@ handle_delete(mpdclient_t *c)
        key = tolower(screen_getch(screen.status_window.w, buf));
        g_free(buf);
        if( key != YES[0] ) {
-               screen_status_printf(_("Aborted!"));
+               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!"));
+       screen_status_printf(_("Playlist deleted"));
        return 0;
 }
 
@@ -162,7 +166,7 @@ browse_exit(void)
 }
 
 static void
-browse_open(mpd_unused mpdclient_t *c)
+browse_open(G_GNUC_UNUSED mpdclient_t *c)
 {
        if (browser.filelist == NULL) {
                browser.filelist = mpdclient_filelist_get(c, "");
@@ -176,21 +180,22 @@ browse_open(mpd_unused mpdclient_t *c)
 static const char *
 browse_title(char *str, size_t size)
 {
-       char *dirname, *parentdir;
-
-       dirname = g_path_get_dirname(browser.filelist->path);
-       parentdir = g_path_get_basename(dirname);
+       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 ? "/" : "",
-                  g_basename(browser.filelist->path));
-       free(dirname);
-       free(parentdir);
+       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;
 }
 
@@ -200,18 +205,24 @@ browse_paint(void)
        list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
 }
 
-static int
+static bool
 browse_cmd(mpdclient_t *c, command_t cmd)
 {
        switch(cmd) {
        case CMD_GO_ROOT_DIRECTORY:
                browser_change_directory(&browser, c, NULL, "");
                file_repaint();
-               return 1;
+               return true;
        case CMD_GO_PARENT_DIRECTORY:
                browser_change_directory(&browser, c, NULL, "..");
                file_repaint();
-               return 1;
+               return true;
+
+       case CMD_LOCATE:
+               /* don't let browser_cmd() evaluate the locate command
+                  - it's a no-op, and by the way, leads to a
+                  segmentation fault in the current implementation */
+               return false;
 
        case CMD_DELETE:
                handle_delete(c);
@@ -228,21 +239,22 @@ browse_cmd(mpdclient_t *c, command_t cmd)
                list_window_check_selected(browser.lw,
                                           filelist_length(browser.filelist));
                file_repaint();
-
-               screen_status_printf(_("Screen updated!"));
-               return 0;
+               return false;
 
        case CMD_DB_UPDATE:
                if (c->status == NULL)
-                       return 1;
+                       return true;
 
                if (!c->status->updatingDb) {
-                       if (mpdclient_cmd_db_update_utf8(c, browser.filelist->path) == 0) {
-                               if (strcmp(browser.filelist->path, ""))
-                                       screen_status_printf(_("Database update of %s started!"),
-                                                            browser.filelist->path);
-                               else
-                                       screen_status_printf(_("Database update started!"));
+                       if (mpdclient_cmd_db_update(c, browser.filelist->path) == 0) {
+                               if (strcmp(browser.filelist->path, "")) {
+                                       char *path_locale =
+                                               utf8_to_locale(browser.filelist->path);
+                                       screen_status_printf(_("Database update of %s started"),
+                                                            path_locale);
+                                       g_free(path_locale);
+                               } else
+                                       screen_status_printf(_("Database update started"));
 
                                /* set updatingDb to make shure the browse callback gets called
                                 * even if the updated has finished before status is updated */
@@ -250,18 +262,19 @@ browse_cmd(mpdclient_t *c, command_t cmd)
                        }
                } else
                        screen_status_printf(_("Database update running..."));
-               return 1;
+               return true;
 
        default:
                break;
        }
 
        if (browser_cmd(&browser, c, cmd)) {
-               file_repaint();
-               return 1;
+               if (screen_is_visible(&screen_browse))
+                       file_repaint();
+               return true;
        }
 
-       return 0;
+       return false;
 }
 
 const struct screen_functions screen_browse = {
@@ -273,3 +286,44 @@ const struct screen_functions screen_browse = {
        .cmd = browse_cmd,
        .get_title = browse_title,
 };
+
+bool
+screen_file_goto_song(struct mpdclient *c, const struct mpd_song *song)
+{
+       const char *slash, *parent;
+       char *allocated = NULL;
+       bool ret;
+       int i;
+
+       assert(song != NULL);
+       assert(song->file != NULL);
+
+       if (strstr(song->file, "//") != NULL)
+               /* an URL? */
+               return false;
+
+       /* determine the song's parent directory and go there */
+
+       slash = strrchr(song->file, '/');
+       if (slash != NULL)
+               parent = allocated = g_strndup(song->file, slash - song->file);
+       else
+               parent = "";
+
+       ret = browser_change_directory(&browser, c, NULL, parent);
+       g_free(allocated);
+       if (!ret)
+               return false;
+
+       /* select the specified song */
+
+       i = filelist_find_song(browser.filelist, song);
+       if (i < 0)
+               i = 0;
+
+       list_window_set_selected(browser.lw, i);
+
+       /* finally, switch to the file screen */
+       screen_switch(&screen_browse, c);
+       return true;
+}