Code

67dc3e081182da8addac2436b3b77b54103b1e8b
[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_queue.h"
25 #include "screen.h"
26 #include "config.h"
27 #include "i18n.h"
28 #include "charset.h"
29 #include "mpdclient.h"
30 #include "filelist.h"
31 #include "screen_utils.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_load_list(struct mpdclient *c, struct filelist *filelist)
56 {
57         struct mpd_connection *connection;
59         connection = mpdclient_get_connection(c);
60         if (connection == NULL)
61                 return;
63         mpd_send_list_meta(connection, current_path);
64         filelist_recv(filelist, connection);
66         if (mpd_response_finish(connection))
67                 filelist_sort_dir_play(filelist, compare_filelist_entry_path);
68         else
69                 mpdclient_handle_error(c);
70 }
72 static void
73 screen_file_reload(struct mpdclient *c)
74 {
75         if (browser.filelist != NULL)
76                 filelist_free(browser.filelist);
78         browser.filelist = filelist_new();
79         if (*current_path != 0)
80                 /* add a dummy entry for ./.. */
81                 filelist_append(browser.filelist, NULL);
83         screen_file_load_list(c, browser.filelist);
85         list_window_set_length(browser.lw,
86                                filelist_length(browser.filelist));
87 }
89 /**
90  * Change to the specified absolute directory.
91  */
92 static bool
93 change_directory(struct mpdclient *c, const char *new_path)
94 {
95         g_free(current_path);
96         current_path = g_strdup(new_path);
98         screen_file_reload(c);
100 #ifndef NCMPC_MINI
101         screen_browser_sync_highlights(browser.filelist, &c->playlist);
102 #endif
104         list_window_reset(browser.lw);
106         return browser.filelist != NULL;
109 /**
110  * Change to the parent directory of the current directory.
111  */
112 static bool
113 change_to_parent(struct mpdclient *c)
115         char *parent = g_path_get_dirname(current_path);
116         char *old_path;
117         int idx;
118         bool success;
120         if (strcmp(parent, ".") == 0)
121                 parent[0] = '\0';
123         old_path = current_path;
124         current_path = NULL;
126         success = change_directory(c, parent);
127         g_free(parent);
129         idx = success
130                 ? filelist_find_directory(browser.filelist, old_path)
131                 : -1;
132         g_free(old_path);
134         if (success && idx >= 0) {
135                 /* set the cursor on the previous working directory */
136                 list_window_set_cursor(browser.lw, idx);
137                 list_window_center(browser.lw, idx);
138         }
140         return success;
143 /**
144  * Change to the directory referred by the specified #filelist_entry
145  * object.
146  */
147 static bool
148 change_to_entry(struct mpdclient *c, const struct filelist_entry *entry)
150         assert(entry != NULL);
152         if (entry->entity == NULL)
153                 return change_to_parent(c);
154         else if (mpd_entity_get_type(entry->entity) == MPD_ENTITY_TYPE_DIRECTORY)
155                 return change_directory(c, mpd_directory_get_path(mpd_entity_get_directory(entry->entity)));
156         else
157                 return false;
160 static bool
161 screen_file_handle_enter(struct mpdclient *c)
163         const struct filelist_entry *entry = browser_get_selected_entry(&browser);
165         if (entry == NULL)
166                 return false;
168         return change_to_entry(c, entry);
171 static void
172 handle_save(struct mpdclient *c)
174         struct list_window_range range;
175         const char *defaultname = NULL;
176         char *defaultname_utf8 = NULL;
178         list_window_get_range(browser.lw, &range);
179         if (range.start == range.end)
180                 return;
182         for (unsigned i = range.start; i < range.end; ++i) {
183                 struct filelist_entry *entry =
184                         filelist_get(browser.filelist, i);
185                 if( entry && entry->entity ) {
186                         struct mpd_entity *entity = entry->entity;
187                         if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_PLAYLIST) {
188                                 const struct mpd_playlist *playlist =
189                                         mpd_entity_get_playlist(entity);
190                                 defaultname = mpd_playlist_get_path(playlist);
191                         }
192                 }
193         }
195         if(defaultname)
196                 defaultname_utf8 = utf8_to_locale(defaultname);
197         playlist_save(c, NULL, defaultname_utf8);
198         g_free(defaultname_utf8);
201 static void
202 handle_delete(struct mpdclient *c)
204         struct mpd_connection *connection = mpdclient_get_connection(c);
205         struct list_window_range range;
206         struct mpd_entity *entity;
207         const struct mpd_playlist *playlist;
208         char *str, *buf;
209         int key;
211         if (connection == NULL)
212                 return;
214         list_window_get_range(browser.lw, &range);
215         for (unsigned i = range.start; i < range.end; ++i) {
216                 struct filelist_entry *entry =
217                         filelist_get(browser.filelist, i);
218                 if( entry==NULL || entry->entity==NULL )
219                         continue;
221                 entity = entry->entity;
223                 if (mpd_entity_get_type(entity) != MPD_ENTITY_TYPE_PLAYLIST) {
224                         /* translators: the "delete" command is only possible
225                            for playlists; the user attempted to delete a song
226                            or a directory or something else */
227                         screen_status_printf(_("Deleting this item is not possible"));
228                         screen_bell();
229                         continue;
230                 }
232                 playlist = mpd_entity_get_playlist(entity);
233                 str = utf8_to_locale(g_basename(mpd_playlist_get_path(playlist)));
234                 buf = g_strdup_printf(_("Delete playlist %s [%s/%s] ? "), str, YES, NO);
235                 g_free(str);
236                 key = tolower(screen_getch(buf));
237                 g_free(buf);
238                 if( key != YES[0] ) {
239                         /* translators: a dialog was aborted by the user */
240                         screen_status_printf(_("Aborted"));
241                         return;
242                 }
244                 if (!mpd_run_rm(connection, mpd_playlist_get_path(playlist))) {
245                         mpdclient_handle_error(c);
246                         break;
247                 }
249                 c->events |= MPD_IDLE_STORED_PLAYLIST;
251                 /* translators: MPD deleted the playlist, as requested by the
252                    user */
253                 screen_status_printf(_("Playlist deleted"));
254         }
257 static void
258 screen_file_init(WINDOW *w, int cols, int rows)
260         current_path = g_strdup("");
262         browser.lw = list_window_init(w, cols, rows);
265 static void
266 screen_file_resize(int cols, int rows)
268         list_window_resize(browser.lw, cols, rows);
271 static void
272 screen_file_exit(void)
274         if (browser.filelist)
275                 filelist_free(browser.filelist);
276         list_window_free(browser.lw);
278         g_free(current_path);
281 static void
282 screen_file_open(struct mpdclient *c)
284         screen_file_reload(c);
287 static const char *
288 screen_file_get_title(char *str, size_t size)
290         const char *path = NULL, *prev = NULL, *slash = current_path;
291         char *path_locale;
293         /* determine the last 2 parts of the path */
294         while ((slash = strchr(slash, '/')) != NULL) {
295                 path = prev;
296                 prev = ++slash;
297         }
299         if (path == NULL)
300                 /* fall back to full path */
301                 path = current_path;
303         path_locale = utf8_to_locale(path);
304         g_snprintf(str, size, "%s: %s",
305                    /* translators: caption of the browser screen */
306                    _("Browse"), path_locale);
307         g_free(path_locale);
308         return str;
311 static void
312 screen_file_paint(void)
314         screen_browser_paint(&browser);
317 static void
318 screen_file_update(struct mpdclient *c)
320         if (c->events & (MPD_IDLE_DATABASE | MPD_IDLE_STORED_PLAYLIST)) {
321                 /* the db has changed -> update the filelist */
322                 screen_file_reload(c);
323         }
325 #ifndef NCMPC_MINI
326         if (c->events & (MPD_IDLE_DATABASE | MPD_IDLE_STORED_PLAYLIST |
327                          MPD_IDLE_PLAYLIST))
328                 screen_browser_sync_highlights(browser.filelist, &c->playlist);
329 #endif
331         if (c->events & (MPD_IDLE_DATABASE | MPD_IDLE_STORED_PLAYLIST
332 #ifndef NCMPC_MINI
333                          | MPD_IDLE_PLAYLIST
334 #endif
335                          ))
336                 screen_file_repaint();
339 static bool
340 screen_file_cmd(struct mpdclient *c, command_t cmd)
342         switch(cmd) {
343         case CMD_PLAY:
344                 if (screen_file_handle_enter(c)) {
345                         screen_file_repaint();
346                         return true;
347                 }
349                 break;
351         case CMD_GO_ROOT_DIRECTORY:
352                 change_directory(c, "");
353                 screen_file_repaint();
354                 return true;
355         case CMD_GO_PARENT_DIRECTORY:
356                 change_to_parent(c);
357                 screen_file_repaint();
358                 return true;
360         case CMD_LOCATE:
361                 /* don't let browser_cmd() evaluate the locate command
362                    - it's a no-op, and by the way, leads to a
363                    segmentation fault in the current implementation */
364                 return false;
366         case CMD_SCREEN_UPDATE:
367                 screen_file_reload(c);
368 #ifndef NCMPC_MINI
369                 screen_browser_sync_highlights(browser.filelist, &c->playlist);
370 #endif
371                 screen_file_repaint();
372                 return false;
374         default:
375                 break;
376         }
378         if (browser_cmd(&browser, c, cmd)) {
379                 if (screen_is_visible(&screen_browse))
380                         screen_file_repaint();
381                 return true;
382         }
384         if (!mpdclient_is_connected(c))
385                 return false;
387         switch(cmd) {
388         case CMD_DELETE:
389                 handle_delete(c);
390                 screen_file_repaint();
391                 break;
393         case CMD_SAVE_PLAYLIST:
394                 handle_save(c);
395                 break;
397         case CMD_DB_UPDATE:
398                 screen_database_update(c, current_path);
399                 return true;
401         default:
402                 break;
403         }
405         return false;
408 const struct screen_functions screen_browse = {
409         .init = screen_file_init,
410         .exit = screen_file_exit,
411         .open = screen_file_open,
412         .resize = screen_file_resize,
413         .paint = screen_file_paint,
414         .update = screen_file_update,
415         .cmd = screen_file_cmd,
416         .get_title = screen_file_get_title,
417 };
419 bool
420 screen_file_goto_song(struct mpdclient *c, const struct mpd_song *song)
422         const char *uri, *slash, *parent;
423         char *allocated = NULL;
424         bool ret;
425         int i;
427         assert(song != NULL);
429         uri = mpd_song_get_uri(song);
431         if (strstr(uri, "//") != NULL)
432                 /* an URL? */
433                 return false;
435         /* determine the song's parent directory and go there */
437         slash = strrchr(uri, '/');
438         if (slash != NULL)
439                 parent = allocated = g_strndup(uri, slash - uri);
440         else
441                 parent = "";
443         ret = change_directory(c, parent);
444         g_free(allocated);
445         if (!ret)
446                 return false;
448         /* select the specified song */
450         i = filelist_find_song(browser.filelist, song);
451         if (i < 0)
452                 i = 0;
454         list_window_set_cursor(browser.lw, i);
456         /* finally, switch to the file screen */
457         screen_switch(&screen_browse, c);
458         return true;