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 "config.h"
22 #include "i18n.h"
23 #include "options.h"
24 #include "charset.h"
25 #include "mpdclient.h"
26 #include "filelist.h"
27 #include "command.h"
28 #include "screen.h"
29 #include "screen_utils.h"
30 #include "screen_browser.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 browse_paint(void);
47 static void
48 file_repaint(void)
49 {
50 browse_paint();
51 wrefresh(browser.lw->w);
52 }
54 static void
55 file_reload(struct mpdclient *c)
56 {
57 if (browser.filelist != NULL)
58 filelist_free(browser.filelist);
60 browser.filelist = mpdclient_filelist_get(c, current_path);
61 if (browser.filelist == NULL)
62 browser.filelist = filelist_new();
64 if (*current_path != 0)
65 /* add a dummy entry for ./.. */
66 filelist_prepend(browser.filelist, NULL);
67 }
69 /**
70 * Change to the specified absolute directory.
71 */
72 static bool
73 file_change_directory(struct mpdclient *c, const char *new_path)
74 {
75 g_free(current_path);
76 current_path = g_strdup(new_path);
78 file_reload(c);
80 #ifndef NCMPC_MINI
81 sync_highlights(c, browser.filelist);
82 #endif
84 list_window_reset(browser.lw);
86 return browser.filelist != NULL;
87 }
89 /**
90 * Change to the parent directory of the current directory.
91 */
92 static bool
93 file_change_to_parent(struct mpdclient *c)
94 {
95 char *parent = g_path_get_dirname(current_path);
96 char *old_path;
97 int idx;
98 bool success;
100 if (strcmp(parent, ".") == 0)
101 parent[0] = '\0';
103 old_path = current_path;
104 current_path = NULL;
106 success = file_change_directory(c, parent);
107 g_free(parent);
109 idx = success
110 ? filelist_find_directory(browser.filelist, old_path)
111 : -1;
112 g_free(old_path);
114 if (success && idx >= 0) {
115 /* set the cursor on the previous working directory */
116 list_window_set_selected(browser.lw, idx);
117 list_window_center(browser.lw,
118 filelist_length(browser.filelist), idx);
119 }
121 return success;
122 }
124 /**
125 * Change to the directory referred by the specified #filelist_entry
126 * object.
127 */
128 static bool
129 file_change_to_entry(struct mpdclient *c, const struct filelist_entry *entry)
130 {
131 assert(entry != NULL);
133 if (entry->entity == NULL)
134 return file_change_to_parent(c);
135 else if (mpd_entity_get_type(entry->entity) == MPD_ENTITY_TYPE_DIRECTORY)
136 return file_change_directory(c, mpd_directory_get_path(mpd_entity_get_directory(entry->entity)));
137 else
138 return false;
139 }
141 static bool
142 file_handle_enter(struct mpdclient *c)
143 {
144 const struct filelist_entry *entry = browser_get_selected_entry(&browser);
146 if (entry == NULL)
147 return false;
149 return file_change_to_entry(c, entry);
150 }
152 static int
153 handle_save(struct mpdclient *c)
154 {
155 struct filelist_entry *entry;
156 const char *defaultname = NULL;
157 char *defaultname_utf8 = NULL;
158 int ret;
159 unsigned selected;
161 if (browser.lw->selected >= filelist_length(browser.filelist))
162 return -1;
164 for(selected = browser.lw->selected_start; selected <= browser.lw->selected_end; ++selected)
165 {
166 entry = filelist_get(browser.filelist, selected);
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 if(defaultname)
178 defaultname_utf8 = utf8_to_locale(defaultname);
179 ret = playlist_save(c, NULL, defaultname_utf8);
180 g_free(defaultname_utf8);
182 return ret;
183 }
185 static int
186 handle_delete(struct mpdclient *c)
187 {
188 struct filelist_entry *entry;
189 struct mpd_entity *entity;
190 const struct mpd_playlist *playlist;
191 char *str, *buf;
192 int key;
193 unsigned selected;
195 for(selected = browser.lw->selected_start; selected <= browser.lw->selected_end; ++selected)
196 {
197 if (selected >= filelist_length(browser.filelist))
198 return -1;
200 entry = filelist_get(browser.filelist, selected);
201 if( entry==NULL || entry->entity==NULL )
202 continue;
204 entity = entry->entity;
206 if (mpd_entity_get_type(entity) != MPD_ENTITY_TYPE_PLAYLIST) {
207 /* translators: the "delete" command is only possible
208 for playlists; the user attempted to delete a song
209 or a directory or something else */
210 screen_status_printf(_("Deleting this item is not possible"));
211 screen_bell();
212 continue;
213 }
215 playlist = mpd_entity_get_playlist(entity);
216 str = utf8_to_locale(g_basename(mpd_playlist_get_path(playlist)));
217 buf = g_strdup_printf(_("Delete playlist %s [%s/%s] ? "), str, YES, NO);
218 g_free(str);
219 key = tolower(screen_getch(buf));
220 g_free(buf);
221 if( key != YES[0] ) {
222 /* translators: a dialog was aborted by the user */
223 screen_status_printf(_("Aborted"));
224 return 0;
225 }
227 if (mpdclient_cmd_delete_playlist(c, mpd_playlist_get_path(playlist)))
228 continue;
230 /* translators: MPD deleted the playlist, as requested by the
231 user */
232 screen_status_printf(_("Playlist deleted"));
233 }
234 return 0;
235 }
237 static void
238 browse_init(WINDOW *w, int cols, int rows)
239 {
240 current_path = g_strdup("");
242 browser.lw = list_window_init(w, cols, rows);
243 }
245 static void
246 browse_resize(int cols, int rows)
247 {
248 browser.lw->cols = cols;
249 browser.lw->rows = rows;
250 }
252 static void
253 browse_exit(void)
254 {
255 if (browser.filelist)
256 filelist_free(browser.filelist);
257 list_window_free(browser.lw);
259 g_free(current_path);
260 }
262 static void
263 browse_open(struct mpdclient *c)
264 {
265 file_reload(c);
266 }
268 static const char *
269 browse_title(char *str, size_t size)
270 {
271 const char *path = NULL, *prev = NULL, *slash = current_path;
272 char *path_locale;
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 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;
290 }
292 static void
293 browse_paint(void)
294 {
295 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
296 }
298 static void
299 screen_file_update(struct mpdclient *c)
300 {
301 if (c->events & (MPD_IDLE_DATABASE | MPD_IDLE_STORED_PLAYLIST)) {
302 /* the db has changed -> update the filelist */
303 file_reload(c);
304 list_window_check_selected(browser.lw,
305 filelist_length(browser.filelist));
306 }
308 #ifndef NCMPC_MINI
309 if (c->events & (MPD_IDLE_DATABASE | MPD_IDLE_STORED_PLAYLIST |
310 MPD_IDLE_PLAYLIST))
311 sync_highlights(c, browser.filelist);
312 #endif
314 if (c->events & (MPD_IDLE_DATABASE | MPD_IDLE_STORED_PLAYLIST
315 #ifndef NCMPC_MINI
316 | MPD_IDLE_PLAYLIST
317 #endif
318 ))
319 file_repaint();
320 }
322 static bool
323 browse_cmd(struct mpdclient *c, command_t cmd)
324 {
325 switch(cmd) {
326 case CMD_PLAY:
327 if (file_handle_enter(c)) {
328 file_repaint();
329 return true;
330 }
332 break;
334 case CMD_GO_ROOT_DIRECTORY:
335 file_change_directory(c, "");
336 file_repaint();
337 return true;
338 case CMD_GO_PARENT_DIRECTORY:
339 file_change_to_parent(c);
340 file_repaint();
341 return true;
343 case CMD_LOCATE:
344 /* don't let browser_cmd() evaluate the locate command
345 - it's a no-op, and by the way, leads to a
346 segmentation fault in the current implementation */
347 return false;
349 case CMD_DELETE:
350 handle_delete(c);
351 file_repaint();
352 break;
353 case CMD_SAVE_PLAYLIST:
354 handle_save(c);
355 break;
356 case CMD_SCREEN_UPDATE:
357 file_reload(c);
358 #ifndef NCMPC_MINI
359 sync_highlights(c, browser.filelist);
360 #endif
361 list_window_check_selected(browser.lw,
362 filelist_length(browser.filelist));
363 file_repaint();
364 return false;
366 case CMD_DB_UPDATE:
367 if (c->status == NULL)
368 return true;
370 screen_database_update(c, current_path);
371 return true;
373 default:
374 break;
375 }
377 if (browser_cmd(&browser, c, cmd)) {
378 if (screen_is_visible(&screen_browse))
379 file_repaint();
380 return true;
381 }
383 return false;
384 }
386 const struct screen_functions screen_browse = {
387 .init = browse_init,
388 .exit = browse_exit,
389 .open = browse_open,
390 .resize = browse_resize,
391 .paint = browse_paint,
392 .update = screen_file_update,
393 .cmd = browse_cmd,
394 .get_title = browse_title,
395 };
397 bool
398 screen_file_goto_song(struct mpdclient *c, const struct mpd_song *song)
399 {
400 const char *uri, *slash, *parent;
401 char *allocated = NULL;
402 bool ret;
403 int i;
405 assert(song != NULL);
407 uri = mpd_song_get_uri(song);
409 if (strstr(uri, "//") != NULL)
410 /* an URL? */
411 return false;
413 /* determine the song's parent directory and go there */
415 slash = strrchr(uri, '/');
416 if (slash != NULL)
417 parent = allocated = g_strndup(uri, slash - uri);
418 else
419 parent = "";
421 ret = file_change_directory(c, parent);
422 g_free(allocated);
423 if (!ret)
424 return false;
426 /* select the specified song */
428 i = filelist_find_song(browser.filelist, song);
429 if (i < 0)
430 i = 0;
432 list_window_set_selected(browser.lw, i);
434 /* finally, switch to the file screen */
435 screen_switch(&screen_browse, c);
436 return true;
437 }