Code

screen_file: use idle events instead of playlist/browse callback
[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 */
20 #include "config.h"
21 #include "i18n.h"
22 #include "options.h"
23 #include "charset.h"
24 #include "mpdclient.h"
25 #include "filelist.h"
26 #include "command.h"
27 #include "screen.h"
28 #include "screen_utils.h"
29 #include "screen_browser.h"
30 #include "screen_play.h"
31 #include "screen_client.h"
33 #include <mpd/client.h>
35 #include <ctype.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <glib.h>
40 static struct screen_browser browser;
41 static char *current_path;
43 static void
44 browse_paint(void);
46 static void
47 file_repaint(void)
48 {
49         browse_paint();
50         wrefresh(browser.lw->w);
51 }
53 static void
54 file_reload(struct mpdclient *c)
55 {
56         if (browser.filelist != NULL)
57                 filelist_free(browser.filelist);
59         browser.filelist = mpdclient_filelist_get(c, current_path);
60         if (browser.filelist == NULL)
61                 browser.filelist = filelist_new();
63         if (*current_path != 0)
64                 /* add a dummy entry for ./.. */
65                 filelist_prepend(browser.filelist, NULL);
66 }
68 /**
69  * Change to the specified absolute directory.
70  */
71 static bool
72 file_change_directory(struct mpdclient *c, const char *new_path)
73 {
74         g_free(current_path);
75         current_path = g_strdup(new_path);
77         file_reload(c);
79 #ifndef NCMPC_MINI
80         sync_highlights(c, browser.filelist);
81 #endif
83         list_window_reset(browser.lw);
85         return browser.filelist != NULL;
86 }
88 /**
89  * Change to the parent directory of the current directory.
90  */
91 static bool
92 file_change_to_parent(struct mpdclient *c)
93 {
94         char *parent = g_path_get_dirname(current_path);
95         char *old_path;
96         int idx;
97         bool success;
99         if (strcmp(parent, ".") == 0)
100                 parent[0] = '\0';
102         old_path = current_path;
103         current_path = NULL;
105         success = file_change_directory(c, parent);
106         g_free(parent);
108         idx = success
109                 ? filelist_find_directory(browser.filelist, old_path)
110                 : -1;
111         g_free(old_path);
113         if (success && idx >= 0) {
114                 /* set the cursor on the previous working directory */
115                 list_window_set_selected(browser.lw, idx);
116                 list_window_center(browser.lw,
117                                    filelist_length(browser.filelist), idx);
118         }
120         return success;
123 /**
124  * Change to the directory referred by the specified #filelist_entry
125  * object.
126  */
127 static bool
128 file_change_to_entry(struct mpdclient *c, const struct filelist_entry *entry)
130         assert(entry != NULL);
132         if (entry->entity == NULL)
133                 return file_change_to_parent(c);
134         else if (mpd_entity_get_type(entry->entity) == MPD_ENTITY_TYPE_DIRECTORY)
135                 return file_change_directory(c, mpd_directory_get_path(mpd_entity_get_directory(entry->entity)));
136         else
137                 return false;
140 static bool
141 file_handle_enter(struct mpdclient *c)
143         const struct filelist_entry *entry = browser_get_selected_entry(&browser);
145         if (entry == NULL)
146                 return false;
148         return file_change_to_entry(c, entry);
151 static int
152 handle_save(struct mpdclient *c)
154         struct filelist_entry *entry;
155         const char *defaultname = NULL;
156         char *defaultname_utf8 = NULL;
157         int ret;
158         unsigned selected;
160         if (browser.lw->selected >= filelist_length(browser.filelist))
161                 return -1;
163         for(selected = browser.lw->selected_start; selected <= browser.lw->selected_end; ++selected)
164         {
165                 entry = filelist_get(browser.filelist, selected);
166                 if( entry && entry->entity ) {
167                         struct mpd_entity *entity = entry->entity;
168                         if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_PLAYLIST) {
169                                 const struct mpd_playlist *playlist =
170                                         mpd_entity_get_playlist(entity);
171                                 defaultname = mpd_playlist_get_path(playlist);
172                         }
173                 }
174         }
176         if(defaultname)
177                 defaultname_utf8 = utf8_to_locale(defaultname);
178         ret = playlist_save(c, NULL, defaultname_utf8);
179         g_free(defaultname_utf8);
181         return ret;
184 static int
185 handle_delete(struct mpdclient *c)
187         struct filelist_entry *entry;
188         struct mpd_entity *entity;
189         const struct mpd_playlist *playlist;
190         char *str, *buf;
191         int key;
192         unsigned selected;
194         for(selected = browser.lw->selected_start; selected <= browser.lw->selected_end; ++selected)
195         {
196                 if (selected >= filelist_length(browser.filelist))
197                         return -1;
199                 entry = filelist_get(browser.filelist, selected);
200                 if( entry==NULL || entry->entity==NULL )
201                         continue;
203                 entity = entry->entity;
205                 if (mpd_entity_get_type(entity) != MPD_ENTITY_TYPE_PLAYLIST) {
206                         /* translators: the "delete" command is only possible
207                            for playlists; the user attempted to delete a song
208                            or a directory or something else */
209                         screen_status_printf(_("Deleting this item is not possible"));
210                         screen_bell();
211                         continue;
212                 }
214                 playlist = mpd_entity_get_playlist(entity);
215                 str = utf8_to_locale(g_basename(mpd_playlist_get_path(playlist)));
216                 buf = g_strdup_printf(_("Delete playlist %s [%s/%s] ? "), str, YES, NO);
217                 g_free(str);
218                 key = tolower(screen_getch(buf));
219                 g_free(buf);
220                 if( key != YES[0] ) {
221                         /* translators: a dialog was aborted by the user */
222                         screen_status_printf(_("Aborted"));
223                         return 0;
224                 }
226                 if (mpdclient_cmd_delete_playlist(c, mpd_playlist_get_path(playlist)))
227                         continue;
229                 /* translators: MPD deleted the playlist, as requested by the
230                    user */
231                 screen_status_printf(_("Playlist deleted"));
232         }
233         return 0;
236 static void
237 browse_init(WINDOW *w, int cols, int rows)
239         current_path = g_strdup("");
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);
258         g_free(current_path);
261 static void
262 browse_open(struct mpdclient *c)
264         file_reload(c);
267 static const char *
268 browse_title(char *str, size_t size)
270         const char *path = NULL, *prev = NULL, *slash = current_path;
271         char *path_locale;
273         /* determine the last 2 parts of the path */
274         while ((slash = strchr(slash, '/')) != NULL) {
275                 path = prev;
276                 prev = ++slash;
277         }
279         if (path == NULL)
280                 /* fall back to full path */
281                 path = current_path;
283         path_locale = utf8_to_locale(path);
284         g_snprintf(str, size, "%s: %s",
285                    /* translators: caption of the browser screen */
286                    _("Browse"), path_locale);
287         g_free(path_locale);
288         return str;
291 static void
292 browse_paint(void)
294         list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
297 static void
298 screen_file_update(struct mpdclient *c)
300         if (c->events & (MPD_IDLE_DATABASE | MPD_IDLE_STORED_PLAYLIST)) {
301                 /* the db has changed -> update the filelist */
302                 file_reload(c);
303                 list_window_check_selected(browser.lw,
304                                            filelist_length(browser.filelist));
305         }
307 #ifndef NCMPC_MINI
308         if (c->events & (MPD_IDLE_DATABASE | MPD_IDLE_STORED_PLAYLIST |
309                          MPD_IDLE_PLAYLIST))
310                 sync_highlights(c, browser.filelist);
311 #endif
313         if (c->events & (MPD_IDLE_DATABASE | MPD_IDLE_STORED_PLAYLIST
314 #ifndef NCMPC_MINI
315                          | MPD_IDLE_PLAYLIST
316 #endif
317                          ))
318                 file_repaint();
321 static bool
322 browse_cmd(struct mpdclient *c, command_t cmd)
324         switch(cmd) {
325         case CMD_PLAY:
326                 if (file_handle_enter(c)) {
327                         file_repaint();
328                         return true;
329                 }
331                 break;
333         case CMD_GO_ROOT_DIRECTORY:
334                 file_change_directory(c, "");
335                 file_repaint();
336                 return true;
337         case CMD_GO_PARENT_DIRECTORY:
338                 file_change_to_parent(c);
339                 file_repaint();
340                 return true;
342         case CMD_LOCATE:
343                 /* don't let browser_cmd() evaluate the locate command
344                    - it's a no-op, and by the way, leads to a
345                    segmentation fault in the current implementation */
346                 return false;
348         case CMD_DELETE:
349                 handle_delete(c);
350                 file_repaint();
351                 break;
352         case CMD_SAVE_PLAYLIST:
353                 handle_save(c);
354                 break;
355         case CMD_SCREEN_UPDATE:
356                 file_reload(c);
357 #ifndef NCMPC_MINI
358                 sync_highlights(c, browser.filelist);
359 #endif
360                 list_window_check_selected(browser.lw,
361                                            filelist_length(browser.filelist));
362                 file_repaint();
363                 return false;
365         case CMD_DB_UPDATE:
366                 if (c->status == NULL)
367                         return true;
369                 screen_database_update(c, current_path);
370                 return true;
372         default:
373                 break;
374         }
376         if (browser_cmd(&browser, c, cmd)) {
377                 if (screen_is_visible(&screen_browse))
378                         file_repaint();
379                 return true;
380         }
382         return false;
385 const struct screen_functions screen_browse = {
386         .init = browse_init,
387         .exit = browse_exit,
388         .open = browse_open,
389         .resize = browse_resize,
390         .paint = browse_paint,
391         .update = screen_file_update,
392         .cmd = browse_cmd,
393         .get_title = browse_title,
394 };
396 bool
397 screen_file_goto_song(struct mpdclient *c, const struct mpd_song *song)
399         const char *uri, *slash, *parent;
400         char *allocated = NULL;
401         bool ret;
402         int i;
404         assert(song != NULL);
406         uri = mpd_song_get_uri(song);
408         if (strstr(uri, "//") != NULL)
409                 /* an URL? */
410                 return false;
412         /* determine the song's parent directory and go there */
414         slash = strrchr(uri, '/');
415         if (slash != NULL)
416                 parent = allocated = g_strndup(uri, slash - uri);
417         else
418                 parent = "";
420         ret = file_change_directory(c, parent);
421         g_free(allocated);
422         if (!ret)
423                 return false;
425         /* select the specified song */
427         i = filelist_find_song(browser.filelist, song);
428         if (i < 0)
429                 i = 0;
431         list_window_set_selected(browser.lw, i);
433         /* finally, switch to the file screen */
434         screen_switch(&screen_browse, c);
435         return true;