Code

screen_browser: browser_change_directory() to screen_file.c
authorMax Kellermann <max@duempel.org>
Sun, 13 Sep 2009 09:06:05 +0000 (11:06 +0200)
committerMax Kellermann <max@duempel.org>
Sun, 13 Sep 2009 09:06:05 +0000 (11:06 +0200)
This is completely specific to screen_file, and should live there.

src/screen_browser.c
src/screen_browser.h
src/screen_file.c

index 7af1f2509715fcfdfbd53aaf9f9bddd796bef369..6f29bd70ae04567b776d8f23834c54faf2308c07 100644 (file)
@@ -164,67 +164,6 @@ browser_lw_callback(unsigned idx, bool *highlight, G_GNUC_UNUSED char **second_c
        return "Error: Unknown entry!";
 }
 
-/* chdir */
-bool
-browser_change_directory(struct screen_browser *browser, mpdclient_t *c,
-                        filelist_entry_t *entry, const char *new_path)
-{
-       mpd_InfoEntity *entity = NULL;
-       gchar *path = NULL;
-       char *old_path;
-       int idx;
-
-       if( entry!=NULL )
-               entity = entry->entity;
-       else if( new_path==NULL )
-               return false;
-
-       if( entity==NULL ) {
-               if( entry || 0==strcmp(new_path, "..") ) {
-                       /* return to parent */
-                       char *parent = g_path_get_dirname(browser->filelist->path);
-                       if( strcmp(parent, ".") == 0 )
-                               parent[0] = '\0';
-                       path = g_strdup(parent);
-                       g_free(parent);
-               } else {
-                       /* entry==NULL, then new_path ("" is root) */
-                       path = g_strdup(new_path);
-               }
-       } else if( entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY) {
-               /* enter sub */
-               mpd_Directory *dir = entity->info.directory;
-               path = g_strdup(dir->path);
-       } else
-               return false;
-
-       if (browser->filelist != NULL) {
-               old_path = g_strdup(browser->filelist->path);
-               filelist_free(browser->filelist);
-       } else
-               old_path = NULL;
-
-       browser->filelist = mpdclient_filelist_get(c, path);
-#ifndef NCMPC_MINI
-       sync_highlights(c, browser->filelist);
-#endif
-
-       idx = old_path != NULL
-               ? filelist_find_directory(browser->filelist, old_path)
-               : -1;
-       g_free(old_path);
-
-       list_window_reset(browser->lw);
-       if (idx >= 0) {
-               list_window_set_selected(browser->lw, idx);
-               list_window_center(browser->lw,
-                                  filelist_length(browser->filelist), idx);
-       }
-
-       g_free(path);
-       return true;
-}
-
 static bool
 load_playlist(mpdclient_t *c, const mpd_PlaylistFile *plf)
 {
@@ -318,9 +257,10 @@ browser_handle_enter(struct screen_browser *browser, mpdclient_t *c)
                return false;
 
        entity = entry->entity;
-       if (entity == NULL || entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY)
-               return browser_change_directory(browser, c, entry, NULL);
-       else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE)
+       if (entity == NULL)
+               return false;
+
+       if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE)
                return load_playlist(c, entity->info.playlistFile);
        else if (entity->type == MPD_INFO_ENTITY_TYPE_SONG)
                return enqueue_and_play(c, entry);
index 7f619546ca6847f5d481a488c67d48104d6ab5bc..093ece7cf30dab942db30c78f3734c8e4125b740 100644 (file)
@@ -51,10 +51,6 @@ const char *browser_lw_callback(unsigned index, bool *highlight, char** second_c
 struct filelist_entry *
 browser_get_selected_entry(const struct screen_browser *browser);
 
-bool
-browser_change_directory(struct screen_browser *browser, mpdclient_t *c,
-                        filelist_entry_t *entry, const char *new_path);
-
 bool
 browser_cmd(struct screen_browser *browser,
            struct mpdclient *c, command_t cmd);
index a149e9131791fd82ae6bafdf30b9ad3fbb75936e..9507272f376922d1bc7e93e0d7499798f105870e 100644 (file)
@@ -76,6 +76,82 @@ playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
 }
 #endif
 
+static bool
+file_change_directory(mpdclient_t *c, filelist_entry_t *entry,
+                     const char *new_path)
+{
+       mpd_InfoEntity *entity = NULL;
+       gchar *path = NULL;
+       char *old_path;
+       int idx;
+
+       if( entry!=NULL )
+               entity = entry->entity;
+       else if( new_path==NULL )
+               return false;
+
+       if( entity==NULL ) {
+               if( entry || 0==strcmp(new_path, "..") ) {
+                       /* return to parent */
+                       char *parent = g_path_get_dirname(browser.filelist->path);
+                       if( strcmp(parent, ".") == 0 )
+                               parent[0] = '\0';
+                       path = g_strdup(parent);
+                       g_free(parent);
+               } else {
+                       /* entry==NULL, then new_path ("" is root) */
+                       path = g_strdup(new_path);
+               }
+       } else if( entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY) {
+               /* enter sub */
+               mpd_Directory *dir = entity->info.directory;
+               path = g_strdup(dir->path);
+       } else
+               return false;
+
+       if (browser.filelist != NULL) {
+               old_path = g_strdup(browser.filelist->path);
+               filelist_free(browser.filelist);
+       } else
+               old_path = NULL;
+
+       browser.filelist = mpdclient_filelist_get(c, path);
+#ifndef NCMPC_MINI
+       sync_highlights(c, browser.filelist);
+#endif
+
+       idx = old_path != NULL
+               ? filelist_find_directory(browser.filelist, old_path)
+               : -1;
+       g_free(old_path);
+
+       list_window_reset(browser.lw);
+       if (idx >= 0) {
+               list_window_set_selected(browser.lw, idx);
+               list_window_center(browser.lw,
+                                  filelist_length(browser.filelist), idx);
+       }
+
+       g_free(path);
+       return true;
+}
+
+static bool
+file_handle_enter(struct mpdclient *c)
+{
+       struct filelist_entry *entry = browser_get_selected_entry(&browser);
+       struct mpd_InfoEntity *entity;
+
+       if (entry == NULL)
+               return false;
+
+       entity = entry->entity;
+       if (entity == NULL || entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY)
+               return file_change_directory(c, entry, NULL);
+       else
+               return false;
+}
+
 static int
 handle_save(mpdclient_t *c)
 {
@@ -226,12 +302,17 @@ static bool
 browse_cmd(mpdclient_t *c, command_t cmd)
 {
        switch(cmd) {
+       case CMD_PLAY:
+               if (file_handle_enter(c))
+                       return true;
+               break;
+
        case CMD_GO_ROOT_DIRECTORY:
-               browser_change_directory(&browser, c, NULL, "");
+               file_change_directory(c, NULL, "");
                file_repaint();
                return true;
        case CMD_GO_PARENT_DIRECTORY:
-               browser_change_directory(&browser, c, NULL, "..");
+               file_change_directory(c, NULL, "..");
                file_repaint();
                return true;
 
@@ -323,7 +404,7 @@ screen_file_goto_song(struct mpdclient *c, const struct mpd_song *song)
        else
                parent = "";
 
-       ret = browser_change_directory(&browser, c, NULL, parent);
+       ret = file_change_directory(c, NULL, parent);
        g_free(allocated);
        if (!ret)
                return false;