Code

screen_browser: added wrapper function screen_browser_paint()
[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 "screen_file.h"
21 #include "screen_browser.h"
22 #include "screen_interface.h"
23 #include "screen_message.h"
24 #include "screen.h"
25 #include "config.h"
26 #include "i18n.h"
27 #include "charset.h"
28 #include "mpdclient.h"
29 #include "filelist.h"
30 #include "screen_utils.h"
31 #include "screen_play.h"
32 #include "screen_client.h"
34 #include <mpd/client.h>
36 #include <ctype.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <glib.h>
41 static struct screen_browser browser;
42 static char *current_path;
44 static void
45 screen_file_paint(void);
47 static void
48 screen_file_repaint(void)
49 {
50         screen_file_paint();
51         wrefresh(browser.lw->w);
52 }
54 static void
55 screen_file_reload(struct mpdclient *c)
56 {
57         struct mpd_connection *connection;
59         if (browser.filelist != NULL)
60                 filelist_free(browser.filelist);
62         browser.filelist = filelist_new();
63         if (*current_path != 0)
64                 /* add a dummy entry for ./.. */
65                 filelist_append(browser.filelist, NULL);
67         if (!mpdclient_is_connected(c))
68                 return;
70         connection = mpdclient_get_connection(c);
72         mpd_send_list_meta(connection, current_path);
73         filelist_recv(browser.filelist, connection);
75         if (mpd_response_finish(connection))
76                 filelist_sort_dir_play(browser.filelist,
77                                        compare_filelist_entry_path);
78         else
79                 mpdclient_handle_error(c);
81         list_window_set_length(browser.lw,
82                                filelist_length(browser.filelist));
83 }
85 /**
86  * Change to the specified absolute directory.
87  */
88 static bool
89 change_directory(struct mpdclient *c, const char *new_path)
90 {
91         g_free(current_path);
92         current_path = g_strdup(new_path);
94         screen_file_reload(c);
96 #ifndef NCMPC_MINI
97         screen_browser_sync_highlights(browser.filelist, &c->playlist);
98 #endif
100         list_window_reset(browser.lw);
102         return browser.filelist != NULL;
105 /**
106  * Change to the parent directory of the current directory.
107  */
108 static bool
109 change_to_parent(struct mpdclient *c)
111         char *parent = g_path_get_dirname(current_path);
112         char *old_path;
113         int idx;
114         bool success;
116         if (strcmp(parent, ".") == 0)
117                 parent[0] = '\0';
119         old_path = current_path;
120         current_path = NULL;
122         success = change_directory(c, parent);
123         g_free(parent);
125         idx = success
126                 ? filelist_find_directory(browser.filelist, old_path)
127                 : -1;
128         g_free(old_path);
130         if (success && idx >= 0) {
131                 /* set the cursor on the previous working directory */
132                 list_window_set_cursor(browser.lw, idx);
133                 list_window_center(browser.lw, idx);
134         }
136         return success;
139 /**
140  * Change to the directory referred by the specified #filelist_entry
141  * object.
142  */
143 static bool
144 change_to_entry(struct mpdclient *c, const struct filelist_entry *entry)
146         assert(entry != NULL);
148         if (entry->entity == NULL)
149                 return change_to_parent(c);
150         else if (mpd_entity_get_type(entry->entity) == MPD_ENTITY_TYPE_DIRECTORY)
151                 return change_directory(c, mpd_directory_get_path(mpd_entity_get_directory(entry->entity)));
152         else
153                 return false;
156 static bool
157 screen_file_handle_enter(struct mpdclient *c)
159         const struct filelist_entry *entry = browser_get_selected_entry(&browser);
161         if (entry == NULL)
162                 return false;
164         return change_to_entry(c, entry);
167 static int
168 handle_save(struct mpdclient *c)
170         struct list_window_range range;
171         const char *defaultname = NULL;
172         char *defaultname_utf8 = NULL;
173         int ret;
175         list_window_get_range(browser.lw, &range);
176         if (range.start == range.end)
177                 return -1;
179         for (unsigned i = range.start; i < range.end; ++i) {
180                 struct filelist_entry *entry =
181                         filelist_get(browser.filelist, i);
182                 if( entry && entry->entity ) {
183                         struct mpd_entity *entity = entry->entity;
184                         if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_PLAYLIST) {
185                                 const struct mpd_playlist *playlist =
186                                         mpd_entity_get_playlist(entity);
187                                 defaultname = mpd_playlist_get_path(playlist);
188                         }
189                 }
190         }
192         if(defaultname)
193                 defaultname_utf8 = utf8_to_locale(defaultname);
194         ret = playlist_save(c, NULL, defaultname_utf8);
195         g_free(defaultname_utf8);
197         return ret;
200 static int
201 handle_delete(struct mpdclient *c)
203         struct mpd_connection *connection = mpdclient_get_connection(c);
204         struct list_window_range range;
205         struct mpd_entity *entity;
206         const struct mpd_playlist *playlist;
207         char *str, *buf;
208         int key;
210         list_window_get_range(browser.lw, &range);
211         for (unsigned i = range.start; i < range.end; ++i) {
212                 struct filelist_entry *entry =
213                         filelist_get(browser.filelist, i);
214                 if( entry==NULL || entry->entity==NULL )
215                         continue;
217                 entity = entry->entity;
219                 if (mpd_entity_get_type(entity) != MPD_ENTITY_TYPE_PLAYLIST) {
220                         /* translators: the "delete" command is only possible
221                            for playlists; the user attempted to delete a song
222                            or a directory or something else */
223                         screen_status_printf(_("Deleting this item is not possible"));
224                         screen_bell();
225                         continue;
226                 }
228                 playlist = mpd_entity_get_playlist(entity);
229                 str = utf8_to_locale(g_basename(mpd_playlist_get_path(playlist)));
230                 buf = g_strdup_printf(_("Delete playlist %s [%s/%s] ? "), str, YES, NO);
231                 g_free(str);
232                 key = tolower(screen_getch(buf));
233                 g_free(buf);
234                 if( key != YES[0] ) {
235                         /* translators: a dialog was aborted by the user */
236                         screen_status_printf(_("Aborted"));
237                         return 0;
238                 }
240                 if (!mpd_run_rm(connection, mpd_playlist_get_path(playlist))) {
241                         mpdclient_handle_error(c);
242                         break;
243                 }
245                 c->events |= MPD_IDLE_STORED_PLAYLIST;
247                 /* translators: MPD deleted the playlist, as requested by the
248                    user */
249                 screen_status_printf(_("Playlist deleted"));
250         }
251         return 0;
254 static void
255 screen_file_init(WINDOW *w, int cols, int rows)
257         current_path = g_strdup("");
259         browser.lw = list_window_init(w, cols, rows);
262 static void
263 screen_file_resize(int cols, int rows)
265         list_window_resize(browser.lw, cols, rows);
268 static void
269 screen_file_exit(void)
271         if (browser.filelist)
272                 filelist_free(browser.filelist);
273         list_window_free(browser.lw);
275         g_free(current_path);
278 static void
279 screen_file_open(struct mpdclient *c)
281         screen_file_reload(c);
284 static const char *
285 screen_file_get_title(char *str, size_t size)
287         const char *path = NULL, *prev = NULL, *slash = current_path;
288         char *path_locale;
290         /* determine the last 2 parts of the path */
291         while ((slash = strchr(slash, '/')) != NULL) {
292                 path = prev;
293                 prev = ++slash;
294         }
296         if (path == NULL)
297                 /* fall back to full path */
298                 path = current_path;
300         path_locale = utf8_to_locale(path);
301         g_snprintf(str, size, "%s: %s",
302                    /* translators: caption of the browser screen */
303                    _("Browse"), path_locale);
304         g_free(path_locale);
305         return str;
308 static void
309 screen_file_paint(void)
311         screen_browser_paint(&browser);
314 static void
315 screen_file_update(struct mpdclient *c)
317         if (c->events & (MPD_IDLE_DATABASE | MPD_IDLE_STORED_PLAYLIST)) {
318                 /* the db has changed -> update the filelist */
319                 screen_file_reload(c);
320         }
322 #ifndef NCMPC_MINI
323         if (c->events & (MPD_IDLE_DATABASE | MPD_IDLE_STORED_PLAYLIST |
324                          MPD_IDLE_PLAYLIST))
325                 screen_browser_sync_highlights(browser.filelist, &c->playlist);
326 #endif
328         if (c->events & (MPD_IDLE_DATABASE | MPD_IDLE_STORED_PLAYLIST
329 #ifndef NCMPC_MINI
330                          | MPD_IDLE_PLAYLIST
331 #endif
332                          ))
333                 screen_file_repaint();
336 static bool
337 screen_file_cmd(struct mpdclient *c, command_t cmd)
339         switch(cmd) {
340         case CMD_PLAY:
341                 if (screen_file_handle_enter(c)) {
342                         screen_file_repaint();
343                         return true;
344                 }
346                 break;
348         case CMD_GO_ROOT_DIRECTORY:
349                 change_directory(c, "");
350                 screen_file_repaint();
351                 return true;
352         case CMD_GO_PARENT_DIRECTORY:
353                 change_to_parent(c);
354                 screen_file_repaint();
355                 return true;
357         case CMD_LOCATE:
358                 /* don't let browser_cmd() evaluate the locate command
359                    - it's a no-op, and by the way, leads to a
360                    segmentation fault in the current implementation */
361                 return false;
363         case CMD_SCREEN_UPDATE:
364                 screen_file_reload(c);
365 #ifndef NCMPC_MINI
366                 screen_browser_sync_highlights(browser.filelist, &c->playlist);
367 #endif
368                 screen_file_repaint();
369                 return false;
371         default:
372                 break;
373         }
375         if (browser_cmd(&browser, c, cmd)) {
376                 if (screen_is_visible(&screen_browse))
377                         screen_file_repaint();
378                 return true;
379         }
381         if (!mpdclient_is_connected(c))
382                 return false;
384         switch(cmd) {
385         case CMD_DELETE:
386                 handle_delete(c);
387                 screen_file_repaint();
388                 break;
390         case CMD_SAVE_PLAYLIST:
391                 handle_save(c);
392                 break;
394         case CMD_DB_UPDATE:
395                 screen_database_update(c, current_path);
396                 return true;
398         default:
399                 break;
400         }
402         return false;
405 const struct screen_functions screen_browse = {
406         .init = screen_file_init,
407         .exit = screen_file_exit,
408         .open = screen_file_open,
409         .resize = screen_file_resize,
410         .paint = screen_file_paint,
411         .update = screen_file_update,
412         .cmd = screen_file_cmd,
413         .get_title = screen_file_get_title,
414 };
416 bool
417 screen_file_goto_song(struct mpdclient *c, const struct mpd_song *song)
419         const char *uri, *slash, *parent;
420         char *allocated = NULL;
421         bool ret;
422         int i;
424         assert(song != NULL);
426         uri = mpd_song_get_uri(song);
428         if (strstr(uri, "//") != NULL)
429                 /* an URL? */
430                 return false;
432         /* determine the song's parent directory and go there */
434         slash = strrchr(uri, '/');
435         if (slash != NULL)
436                 parent = allocated = g_strndup(uri, slash - uri);
437         else
438                 parent = "";
440         ret = change_directory(c, parent);
441         g_free(allocated);
442         if (!ret)
443                 return false;
445         /* select the specified song */
447         i = filelist_find_song(browser.filelist, song);
448         if (i < 0)
449                 i = 0;
451         list_window_set_cursor(browser.lw, i);
453         /* finally, switch to the file screen */
454         screen_switch(&screen_browse, c);
455         return true;