From: Max Kellermann Date: Sun, 13 Sep 2009 09:06:05 +0000 (+0200) Subject: screen_browser: browser_change_directory() to screen_file.c X-Git-Tag: release-0.15~15 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=129559ceb21d87bf474580fea3a3d1afa49c208b;p=ncmpc.git screen_browser: browser_change_directory() to screen_file.c This is completely specific to screen_file, and should live there. --- diff --git a/src/screen_browser.c b/src/screen_browser.c index 7af1f25..6f29bd7 100644 --- a/src/screen_browser.c +++ b/src/screen_browser.c @@ -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); diff --git a/src/screen_browser.h b/src/screen_browser.h index 7f61954..093ece7 100644 --- a/src/screen_browser.h +++ b/src/screen_browser.h @@ -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); diff --git a/src/screen_file.c b/src/screen_file.c index a149e91..9507272 100644 --- a/src/screen_file.c +++ b/src/screen_file.c @@ -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;