Code

screen_*: eliminate redundant wrefresh() calls
[ncmpc.git] / src / screen_file.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2017 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
4  *
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.
9  *
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.
14  *
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_status.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"
33 #include "options.h"
35 #include <mpd/client.h>
37 #include <ctype.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <glib.h>
42 static struct screen_browser browser;
43 static char *current_path;
45 static void
46 screen_file_load_list(struct mpdclient *c, struct filelist *filelist)
47 {
48         struct mpd_connection *connection;
50         connection = mpdclient_get_connection(c);
51         if (connection == NULL)
52                 return;
54         mpd_send_list_meta(connection, current_path);
55         filelist_recv(filelist, connection);
57         if (mpdclient_finish_command(c))
58                 filelist_sort_dir_play(filelist, compare_filelist_entry_path);
59 }
61 static void
62 screen_file_reload(struct mpdclient *c)
63 {
64         if (browser.filelist != NULL)
65                 filelist_free(browser.filelist);
67         browser.filelist = filelist_new();
68         if (*current_path != 0)
69                 /* add a dummy entry for ./.. */
70                 filelist_append(browser.filelist, NULL);
72         screen_file_load_list(c, browser.filelist);
74         list_window_set_length(browser.lw,
75                                filelist_length(browser.filelist));
76 }
78 /**
79  * Change to the specified absolute directory.
80  */
81 static bool
82 change_directory(struct mpdclient *c, const char *new_path)
83 {
84         g_free(current_path);
85         current_path = g_strdup(new_path);
87         screen_file_reload(c);
89         screen_browser_sync_highlights(browser.filelist, &c->playlist);
91         list_window_reset(browser.lw);
93         return browser.filelist != NULL;
94 }
96 /**
97  * Change to the parent directory of the current directory.
98  */
99 static bool
100 change_to_parent(struct mpdclient *c)
102         char *parent = g_path_get_dirname(current_path);
103         if (strcmp(parent, ".") == 0)
104                 parent[0] = '\0';
106         char *old_path = current_path;
107         current_path = NULL;
109         bool success = change_directory(c, parent);
110         g_free(parent);
112         int idx = success
113                 ? filelist_find_directory(browser.filelist, old_path)
114                 : -1;
115         g_free(old_path);
117         if (success && idx >= 0) {
118                 /* set the cursor on the previous working directory */
119                 list_window_set_cursor(browser.lw, idx);
120                 list_window_center(browser.lw, idx);
121         }
123         return success;
126 /**
127  * Change to the directory referred by the specified #filelist_entry
128  * object.
129  */
130 static bool
131 change_to_entry(struct mpdclient *c, const struct filelist_entry *entry)
133         assert(entry != NULL);
135         if (entry->entity == NULL)
136                 return change_to_parent(c);
137         else if (mpd_entity_get_type(entry->entity) == MPD_ENTITY_TYPE_DIRECTORY)
138                 return change_directory(c, mpd_directory_get_path(mpd_entity_get_directory(entry->entity)));
139         else
140                 return false;
143 static bool
144 screen_file_handle_enter(struct mpdclient *c)
146         const struct filelist_entry *entry = browser_get_selected_entry(&browser);
148         if (entry == NULL)
149                 return false;
151         return change_to_entry(c, entry);
154 static void
155 handle_save(struct mpdclient *c)
157         struct list_window_range range;
158         const char *defaultname = NULL;
160         list_window_get_range(browser.lw, &range);
161         if (range.start == range.end)
162                 return;
164         for (unsigned i = range.start; i < range.end; ++i) {
165                 struct filelist_entry *entry =
166                         filelist_get(browser.filelist, i);
167                 if( entry && entry->entity ) {
168                         struct mpd_entity *entity = entry->entity;
169                         if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_PLAYLIST) {
170                                 const struct mpd_playlist *playlist =
171                                         mpd_entity_get_playlist(entity);
172                                 defaultname = mpd_playlist_get_path(playlist);
173                         }
174                 }
175         }
177         char *defaultname_utf8 = NULL;
178         if(defaultname)
179                 defaultname_utf8 = utf8_to_locale(defaultname);
180         playlist_save(c, NULL, defaultname_utf8);
181         g_free(defaultname_utf8);
184 static void
185 handle_delete(struct mpdclient *c)
187         struct mpd_connection *connection = mpdclient_get_connection(c);
189         if (connection == NULL)
190                 return;
192         struct list_window_range range;
193         list_window_get_range(browser.lw, &range);
194         for (unsigned i = range.start; i < range.end; ++i) {
195                 struct filelist_entry *entry =
196                         filelist_get(browser.filelist, i);
197                 if( entry==NULL || entry->entity==NULL )
198                         continue;
200                 struct mpd_entity *entity = entry->entity;
202                 if (mpd_entity_get_type(entity) != MPD_ENTITY_TYPE_PLAYLIST) {
203                         /* translators: the "delete" command is only possible
204                            for playlists; the user attempted to delete a song
205                            or a directory or something else */
206                         screen_status_printf(_("Deleting this item is not possible"));
207                         screen_bell();
208                         continue;
209                 }
211                 const struct mpd_playlist *playlist = mpd_entity_get_playlist(entity);
212                 char *str = utf8_to_locale(g_basename(mpd_playlist_get_path(playlist)));
213                 char *buf = g_strdup_printf(_("Delete playlist %s [%s/%s] ? "), str, YES, NO);
214                 g_free(str);
215                 bool delete = screen_get_yesno(buf, false);
216                 g_free(buf);
218                 if (!delete) {
219                         /* translators: a dialog was aborted by the user */
220                         screen_status_printf(_("Aborted"));
221                         return;
222                 }
224                 if (!mpd_run_rm(connection, mpd_playlist_get_path(playlist))) {
225                         mpdclient_handle_error(c);
226                         break;
227                 }
229                 c->events |= MPD_IDLE_STORED_PLAYLIST;
231                 /* translators: MPD deleted the playlist, as requested by the
232                    user */
233                 screen_status_printf(_("Playlist deleted"));
234         }
237 static void
238 screen_file_init(WINDOW *w, int cols, int rows)
240         current_path = g_strdup("");
242         browser.lw = list_window_init(w, cols, rows);
243         browser.song_format = options.list_format;
246 static void
247 screen_file_resize(int cols, int rows)
249         list_window_resize(browser.lw, cols, rows);
252 static void
253 screen_file_exit(void)
255         if (browser.filelist)
256                 filelist_free(browser.filelist);
257         list_window_free(browser.lw);
259         g_free(current_path);
262 static void
263 screen_file_open(struct mpdclient *c)
265         screen_file_reload(c);
266         screen_browser_sync_highlights(browser.filelist, &c->playlist);
269 static const char *
270 screen_file_get_title(char *str, size_t size)
272         const char *path = NULL, *prev = NULL, *slash = current_path;
274         /* determine the last 2 parts of the path */
275         while ((slash = strchr(slash, '/')) != NULL) {
276                 path = prev;
277                 prev = ++slash;
278         }
280         if (path == NULL)
281                 /* fall back to full path */
282                 path = current_path;
284         char *path_locale = utf8_to_locale(path);
285         g_snprintf(str, size, "%s: %s",
286                    /* translators: caption of the browser screen */
287                    _("Browse"), path_locale);
288         g_free(path_locale);
289         return str;
292 static void
293 screen_file_paint(void)
295         screen_browser_paint(&browser);
298 static void
299 screen_file_update(struct mpdclient *c)
301         if (c->events & (MPD_IDLE_DATABASE | MPD_IDLE_STORED_PLAYLIST)) {
302                 /* the db has changed -> update the filelist */
303                 screen_file_reload(c);
304         }
306         if (c->events & (MPD_IDLE_DATABASE | MPD_IDLE_STORED_PLAYLIST
307 #ifndef NCMPC_MINI
308                          | MPD_IDLE_QUEUE
309 #endif
310                          )) {
311                 screen_browser_sync_highlights(browser.filelist, &c->playlist);
312                 screen_file_paint();
313         }
316 static bool
317 screen_file_cmd(struct mpdclient *c, command_t cmd)
319         switch(cmd) {
320         case CMD_PLAY:
321                 if (screen_file_handle_enter(c)) {
322                         screen_file_paint();
323                         return true;
324                 }
326                 break;
328         case CMD_GO_ROOT_DIRECTORY:
329                 change_directory(c, "");
330                 screen_file_paint();
331                 return true;
332         case CMD_GO_PARENT_DIRECTORY:
333                 change_to_parent(c);
334                 screen_file_paint();
335                 return true;
337         case CMD_LOCATE:
338                 /* don't let browser_cmd() evaluate the locate command
339                    - it's a no-op, and by the way, leads to a
340                    segmentation fault in the current implementation */
341                 return false;
343         case CMD_SCREEN_UPDATE:
344                 screen_file_reload(c);
345                 screen_browser_sync_highlights(browser.filelist, &c->playlist);
346                 screen_file_paint();
347                 return false;
349         default:
350                 break;
351         }
353         if (browser_cmd(&browser, c, cmd)) {
354                 if (screen_is_visible(&screen_browse))
355                         screen_file_paint();
356                 return true;
357         }
359         if (!mpdclient_is_connected(c))
360                 return false;
362         switch(cmd) {
363         case CMD_DELETE:
364                 handle_delete(c);
365                 screen_file_paint();
366                 break;
368         case CMD_SAVE_PLAYLIST:
369                 handle_save(c);
370                 break;
372         case CMD_DB_UPDATE:
373                 screen_database_update(c, current_path);
374                 return true;
376         default:
377                 break;
378         }
380         return false;
383 const struct screen_functions screen_browse = {
384         .init = screen_file_init,
385         .exit = screen_file_exit,
386         .open = screen_file_open,
387         .resize = screen_file_resize,
388         .paint = screen_file_paint,
389         .update = screen_file_update,
390         .cmd = screen_file_cmd,
391         .get_title = screen_file_get_title,
392 };
394 bool
395 screen_file_goto_song(struct mpdclient *c, const struct mpd_song *song)
397         const char *uri, *slash, *parent;
398         char *allocated = NULL;
400         assert(song != NULL);
402         uri = mpd_song_get_uri(song);
404         if (strstr(uri, "//") != NULL)
405                 /* an URL? */
406                 return false;
408         /* determine the song's parent directory and go there */
410         slash = strrchr(uri, '/');
411         if (slash != NULL)
412                 parent = allocated = g_strndup(uri, slash - uri);
413         else
414                 parent = "";
416         bool ret = change_directory(c, parent);
417         g_free(allocated);
418         if (!ret)
419                 return false;
421         /* select the specified song */
423         int i = filelist_find_song(browser.filelist, song);
424         if (i < 0)
425                 i = 0;
427         list_window_set_cursor(browser.lw, i);
429         /* finally, switch to the file screen */
430         screen_switch(&screen_browse, c);
431         return true;