Code

screen_browser: browser_change_directory() to screen_file.c
[ncmpc.git] / src / screen_file.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2009 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 #include "config.h"
20 #include "i18n.h"
21 #include "options.h"
22 #include "charset.h"
23 #include "mpdclient.h"
24 #include "command.h"
25 #include "screen.h"
26 #include "screen_utils.h"
27 #include "screen_browser.h"
28 #include "screen_play.h"
30 #include <ctype.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <glib.h>
35 static struct screen_browser browser;
37 static void
38 browse_paint(void);
40 static void
41 file_repaint(void)
42 {
43         browse_paint();
44         wrefresh(browser.lw->w);
45 }
47 static void
48 file_repaint_if_active(void)
49 {
50         if (screen_is_visible(&screen_browse))
51                 file_repaint();
52 }
54 /* the db has changed -> update the filelist */
55 static void
56 file_changed_callback(mpdclient_t *c, G_GNUC_UNUSED int event,
57                       G_GNUC_UNUSED gpointer data)
58 {
59         browser.filelist = mpdclient_filelist_update(c, browser.filelist);
60 #ifndef NCMPC_MINI
61         sync_highlights(c, browser.filelist);
62 #endif
63         list_window_check_selected(browser.lw, filelist_length(browser.filelist));
65         file_repaint_if_active();
66 }
68 #ifndef NCMPC_MINI
69 /* the playlist has been updated -> fix highlights */
70 static void
71 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
72 {
73         browser_playlist_changed(&browser, c, event, data);
75         file_repaint_if_active();
76 }
77 #endif
79 static bool
80 file_change_directory(mpdclient_t *c, filelist_entry_t *entry,
81                       const char *new_path)
82 {
83         mpd_InfoEntity *entity = NULL;
84         gchar *path = NULL;
85         char *old_path;
86         int idx;
88         if( entry!=NULL )
89                 entity = entry->entity;
90         else if( new_path==NULL )
91                 return false;
93         if( entity==NULL ) {
94                 if( entry || 0==strcmp(new_path, "..") ) {
95                         /* return to parent */
96                         char *parent = g_path_get_dirname(browser.filelist->path);
97                         if( strcmp(parent, ".") == 0 )
98                                 parent[0] = '\0';
99                         path = g_strdup(parent);
100                         g_free(parent);
101                 } else {
102                         /* entry==NULL, then new_path ("" is root) */
103                         path = g_strdup(new_path);
104                 }
105         } else if( entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY) {
106                 /* enter sub */
107                 mpd_Directory *dir = entity->info.directory;
108                 path = g_strdup(dir->path);
109         } else
110                 return false;
112         if (browser.filelist != NULL) {
113                 old_path = g_strdup(browser.filelist->path);
114                 filelist_free(browser.filelist);
115         } else
116                 old_path = NULL;
118         browser.filelist = mpdclient_filelist_get(c, path);
119 #ifndef NCMPC_MINI
120         sync_highlights(c, browser.filelist);
121 #endif
123         idx = old_path != NULL
124                 ? filelist_find_directory(browser.filelist, old_path)
125                 : -1;
126         g_free(old_path);
128         list_window_reset(browser.lw);
129         if (idx >= 0) {
130                 list_window_set_selected(browser.lw, idx);
131                 list_window_center(browser.lw,
132                                    filelist_length(browser.filelist), idx);
133         }
135         g_free(path);
136         return true;
139 static bool
140 file_handle_enter(struct mpdclient *c)
142         struct filelist_entry *entry = browser_get_selected_entry(&browser);
143         struct mpd_InfoEntity *entity;
145         if (entry == NULL)
146                 return false;
148         entity = entry->entity;
149         if (entity == NULL || entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY)
150                 return file_change_directory(c, entry, NULL);
151         else
152                 return false;
155 static int
156 handle_save(mpdclient_t *c)
158         filelist_entry_t *entry;
159         char *defaultname = NULL;
160         int ret;
161         unsigned selected;
163         if (browser.lw->selected >= filelist_length(browser.filelist))
164                 return -1;
166         for(selected = browser.lw->selected_start; selected <= browser.lw->selected_end; ++selected)
167         {
168                 entry = filelist_get(browser.filelist, selected);
169                 if( entry && entry->entity ) {
170                         mpd_InfoEntity *entity = entry->entity;
171                         if( entity->type==MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) {
172                                 mpd_PlaylistFile *plf = entity->info.playlistFile;
173                                 defaultname = plf->path;
174                         }
175                 }
176         }
178         if(defaultname)
179                 defaultname = utf8_to_locale(defaultname);
180         ret = playlist_save(c, NULL, defaultname);
181         g_free(defaultname);
183         return ret;
186 static int
187 handle_delete(mpdclient_t *c)
189         filelist_entry_t *entry;
190         mpd_InfoEntity *entity;
191         mpd_PlaylistFile *plf;
192         char *str, *buf;
193         int key;
194         unsigned selected;
196         for(selected = browser.lw->selected_start; selected <= browser.lw->selected_end; ++selected)
197         {
198                 if (selected >= filelist_length(browser.filelist))
199                         return -1;
201                 entry = filelist_get(browser.filelist, selected);
202                 if( entry==NULL || entry->entity==NULL )
203                         continue;
205                 entity = entry->entity;
207                 if( entity->type!=MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) {
208                         /* translators: the "delete" command is only possible
209                            for playlists; the user attempted to delete a song
210                            or a directory or something else */
211                         screen_status_printf(_("Deleting this item is not possible"));
212                         screen_bell();
213                         continue;
214                 }
216                 plf = entity->info.playlistFile;
217                 str = utf8_to_locale(g_basename(plf->path));
218                 buf = g_strdup_printf(_("Delete playlist %s [%s/%s] ? "), str, YES, NO);
219                 g_free(str);
220                 key = tolower(screen_getch(screen.status_window.w, buf));
221                 g_free(buf);
222                 if( key != YES[0] ) {
223                         /* translators: a dialog was aborted by the user */
224                         screen_status_printf(_("Aborted"));
225                         return 0;
226                 }
228                 if( mpdclient_cmd_delete_playlist(c, plf->path) )
229                         continue;
231                 /* translators: MPD deleted the playlist, as requested by the
232                    user */
233                 screen_status_printf(_("Playlist deleted"));
234         }
235         return 0;
238 static void
239 browse_init(WINDOW *w, int cols, int rows)
241         browser.lw = list_window_init(w, cols, rows);
244 static void
245 browse_resize(int cols, int rows)
247         browser.lw->cols = cols;
248         browser.lw->rows = rows;
251 static void
252 browse_exit(void)
254         if (browser.filelist)
255                 filelist_free(browser.filelist);
256         list_window_free(browser.lw);
259 static void
260 browse_open(G_GNUC_UNUSED mpdclient_t *c)
262         if (browser.filelist == NULL) {
263                 browser.filelist = mpdclient_filelist_get(c, "");
264 #ifndef NCMPC_MINI
265                 mpdclient_install_playlist_callback(c, playlist_changed_callback);
266 #endif
267                 mpdclient_install_browse_callback(c, file_changed_callback);
268         }
271 static const char *
272 browse_title(char *str, size_t size)
274         const char *path = NULL, *prev = NULL, *slash = browser.filelist->path;
275         char *path_locale;
277         /* determine the last 2 parts of the path */
278         while ((slash = strchr(slash, '/')) != NULL) {
279                 path = prev;
280                 prev = ++slash;
281         }
283         if (path == NULL)
284                 /* fall back to full path */
285                 path = browser.filelist->path;
287         path_locale = utf8_to_locale(path);
288         g_snprintf(str, size, "%s: %s",
289                    /* translators: caption of the browser screen */
290                    _("Browse"), path_locale);
291         g_free(path_locale);
292         return str;
295 static void
296 browse_paint(void)
298         list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
301 static bool
302 browse_cmd(mpdclient_t *c, command_t cmd)
304         switch(cmd) {
305         case CMD_PLAY:
306                 if (file_handle_enter(c))
307                         return true;
308                 break;
310         case CMD_GO_ROOT_DIRECTORY:
311                 file_change_directory(c, NULL, "");
312                 file_repaint();
313                 return true;
314         case CMD_GO_PARENT_DIRECTORY:
315                 file_change_directory(c, NULL, "..");
316                 file_repaint();
317                 return true;
319         case CMD_LOCATE:
320                 /* don't let browser_cmd() evaluate the locate command
321                    - it's a no-op, and by the way, leads to a
322                    segmentation fault in the current implementation */
323                 return false;
325         case CMD_DELETE:
326                 handle_delete(c);
327                 file_repaint();
328                 break;
329         case CMD_SAVE_PLAYLIST:
330                 handle_save(c);
331                 break;
332         case CMD_SCREEN_UPDATE:
333                 browser.filelist = mpdclient_filelist_update(c, browser.filelist);
334 #ifndef NCMPC_MINI
335                 sync_highlights(c, browser.filelist);
336 #endif
337                 list_window_check_selected(browser.lw,
338                                            filelist_length(browser.filelist));
339                 file_repaint();
340                 return false;
342         case CMD_DB_UPDATE:
343                 if (c->status == NULL)
344                         return true;
346                 if (!c->status->updatingDb) {
347                         if (mpdclient_cmd_db_update(c, browser.filelist->path) == 0) {
348                                 if (strcmp(browser.filelist->path, "")) {
349                                         char *path_locale =
350                                                 utf8_to_locale(browser.filelist->path);
351                                         screen_status_printf(_("Database update of %s started"),
352                                                              path_locale);
353                                         g_free(path_locale);
354                                 } else
355                                         screen_status_printf(_("Database update started"));
356                         }
357                 } else
358                         screen_status_printf(_("Database update running..."));
359                 return true;
361         default:
362                 break;
363         }
365         if (browser_cmd(&browser, c, cmd)) {
366                 if (screen_is_visible(&screen_browse))
367                         file_repaint();
368                 return true;
369         }
371         return false;
374 const struct screen_functions screen_browse = {
375         .init = browse_init,
376         .exit = browse_exit,
377         .open = browse_open,
378         .resize = browse_resize,
379         .paint = browse_paint,
380         .cmd = browse_cmd,
381         .get_title = browse_title,
382 };
384 bool
385 screen_file_goto_song(struct mpdclient *c, const struct mpd_song *song)
387         const char *slash, *parent;
388         char *allocated = NULL;
389         bool ret;
390         int i;
392         assert(song != NULL);
393         assert(song->file != NULL);
395         if (strstr(song->file, "//") != NULL)
396                 /* an URL? */
397                 return false;
399         /* determine the song's parent directory and go there */
401         slash = strrchr(song->file, '/');
402         if (slash != NULL)
403                 parent = allocated = g_strndup(song->file, slash - song->file);
404         else
405                 parent = "";
407         ret = file_change_directory(c, NULL, parent);
408         g_free(allocated);
409         if (!ret)
410                 return false;
412         /* select the specified song */
414         i = filelist_find_song(browser.filelist, song);
415         if (i < 0)
416                 i = 0;
418         list_window_set_selected(browser.lw, i);
420         /* finally, switch to the file screen */
421         screen_switch(&screen_browse, c);
422         return true;