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 */
19 #include "config.h"
20 #include "i18n.h"
21 #include "options.h"
22 #include "charset.h"
23 #include "mpdclient.h"
24 #include "command.h"
25 #include "screen.h"
26 #include "screen_utils.h"
27 #include "screen_browser.h"
28 #include "screen_play.h"
30 #include <ctype.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <glib.h>
35 static struct screen_browser browser;
36 static char *current_path;
38 static void
39 browse_paint(void);
41 static void
42 file_repaint(void)
43 {
44 browse_paint();
45 wrefresh(browser.lw->w);
46 }
48 static void
49 file_repaint_if_active(void)
50 {
51 if (screen_is_visible(&screen_browse))
52 file_repaint();
53 }
55 static void
56 file_reload(struct mpdclient *c)
57 {
58 if (browser.filelist != NULL)
59 filelist_free(browser.filelist);
61 browser.filelist = mpdclient_filelist_get(c, current_path);
62 }
64 /* the db has changed -> update the filelist */
65 static void
66 file_changed_callback(mpdclient_t *c, G_GNUC_UNUSED int event,
67 G_GNUC_UNUSED gpointer data)
68 {
69 file_reload(c);
71 #ifndef NCMPC_MINI
72 sync_highlights(c, browser.filelist);
73 #endif
74 list_window_check_selected(browser.lw, filelist_length(browser.filelist));
76 file_repaint_if_active();
77 }
79 #ifndef NCMPC_MINI
80 /* the playlist has been updated -> fix highlights */
81 static void
82 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
83 {
84 browser_playlist_changed(&browser, c, event, data);
86 file_repaint_if_active();
87 }
88 #endif
90 /**
91 * Change to the specified absolute directory.
92 */
93 static bool
94 file_change_directory(mpdclient_t *c, const char *new_path)
95 {
96 g_free(current_path);
97 current_path = g_strdup(new_path);
99 file_reload(c);
101 #ifndef NCMPC_MINI
102 sync_highlights(c, browser.filelist);
103 #endif
105 list_window_reset(browser.lw);
107 return browser.filelist != NULL;
108 }
110 /**
111 * Change to the parent directory of the current directory.
112 */
113 static bool
114 file_change_to_parent(mpdclient_t *c)
115 {
116 char *parent = g_path_get_dirname(current_path);
117 char *old_path;
118 int idx;
119 bool success;
121 if (strcmp(parent, ".") == 0)
122 parent[0] = '\0';
124 old_path = current_path;
125 current_path = NULL;
127 success = file_change_directory(c, parent);
128 g_free(parent);
130 idx = success
131 ? filelist_find_directory(browser.filelist, old_path)
132 : -1;
133 g_free(old_path);
135 if (success && idx >= 0) {
136 /* set the cursor on the previous working directory */
137 list_window_set_selected(browser.lw, idx);
138 list_window_center(browser.lw,
139 filelist_length(browser.filelist), idx);
140 }
142 return success;
143 }
145 /**
146 * Change to the directory referred by the specified filelist_entry_t
147 * object.
148 */
149 static bool
150 file_change_to_entry(mpdclient_t *c, const filelist_entry_t *entry)
151 {
152 assert(entry != NULL);
154 if (entry->entity == NULL)
155 return file_change_to_parent(c);
156 else if (entry->entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY)
157 return file_change_directory(c, entry->entity->info.directory->path);
158 else
159 return false;
160 }
162 static bool
163 file_handle_enter(struct mpdclient *c)
164 {
165 const struct filelist_entry *entry = browser_get_selected_entry(&browser);
167 if (entry == NULL)
168 return false;
170 return file_change_to_entry(c, entry);
171 }
173 static int
174 handle_save(mpdclient_t *c)
175 {
176 filelist_entry_t *entry;
177 char *defaultname = NULL;
178 int ret;
179 unsigned selected;
181 if (browser.lw->selected >= filelist_length(browser.filelist))
182 return -1;
184 for(selected = browser.lw->selected_start; selected <= browser.lw->selected_end; ++selected)
185 {
186 entry = filelist_get(browser.filelist, selected);
187 if( entry && entry->entity ) {
188 mpd_InfoEntity *entity = entry->entity;
189 if( entity->type==MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) {
190 mpd_PlaylistFile *plf = entity->info.playlistFile;
191 defaultname = plf->path;
192 }
193 }
194 }
196 if(defaultname)
197 defaultname = utf8_to_locale(defaultname);
198 ret = playlist_save(c, NULL, defaultname);
199 g_free(defaultname);
201 return ret;
202 }
204 static int
205 handle_delete(mpdclient_t *c)
206 {
207 filelist_entry_t *entry;
208 mpd_InfoEntity *entity;
209 mpd_PlaylistFile *plf;
210 char *str, *buf;
211 int key;
212 unsigned selected;
214 for(selected = browser.lw->selected_start; selected <= browser.lw->selected_end; ++selected)
215 {
216 if (selected >= filelist_length(browser.filelist))
217 return -1;
219 entry = filelist_get(browser.filelist, selected);
220 if( entry==NULL || entry->entity==NULL )
221 continue;
223 entity = entry->entity;
225 if( entity->type!=MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) {
226 /* translators: the "delete" command is only possible
227 for playlists; the user attempted to delete a song
228 or a directory or something else */
229 screen_status_printf(_("Deleting this item is not possible"));
230 screen_bell();
231 continue;
232 }
234 plf = entity->info.playlistFile;
235 str = utf8_to_locale(g_basename(plf->path));
236 buf = g_strdup_printf(_("Delete playlist %s [%s/%s] ? "), str, YES, NO);
237 g_free(str);
238 key = tolower(screen_getch(screen.status_window.w, buf));
239 g_free(buf);
240 if( key != YES[0] ) {
241 /* translators: a dialog was aborted by the user */
242 screen_status_printf(_("Aborted"));
243 return 0;
244 }
246 if( mpdclient_cmd_delete_playlist(c, plf->path) )
247 continue;
249 /* translators: MPD deleted the playlist, as requested by the
250 user */
251 screen_status_printf(_("Playlist deleted"));
252 }
253 return 0;
254 }
256 static void
257 browse_init(WINDOW *w, int cols, int rows)
258 {
259 current_path = g_strdup("");
261 browser.lw = list_window_init(w, cols, rows);
262 }
264 static void
265 browse_resize(int cols, int rows)
266 {
267 browser.lw->cols = cols;
268 browser.lw->rows = rows;
269 }
271 static void
272 browse_exit(void)
273 {
274 if (browser.filelist)
275 filelist_free(browser.filelist);
276 list_window_free(browser.lw);
278 g_free(current_path);
279 }
281 static void
282 browse_open(G_GNUC_UNUSED mpdclient_t *c)
283 {
284 if (browser.filelist == NULL) {
285 browser.filelist = mpdclient_filelist_get(c, "");
286 #ifndef NCMPC_MINI
287 mpdclient_install_playlist_callback(c, playlist_changed_callback);
288 #endif
289 mpdclient_install_browse_callback(c, file_changed_callback);
290 }
291 }
293 static const char *
294 browse_title(char *str, size_t size)
295 {
296 const char *path = NULL, *prev = NULL, *slash = current_path;
297 char *path_locale;
299 /* determine the last 2 parts of the path */
300 while ((slash = strchr(slash, '/')) != NULL) {
301 path = prev;
302 prev = ++slash;
303 }
305 if (path == NULL)
306 /* fall back to full path */
307 path = current_path;
309 path_locale = utf8_to_locale(path);
310 g_snprintf(str, size, "%s: %s",
311 /* translators: caption of the browser screen */
312 _("Browse"), path_locale);
313 g_free(path_locale);
314 return str;
315 }
317 static void
318 browse_paint(void)
319 {
320 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
321 }
323 static bool
324 browse_cmd(mpdclient_t *c, command_t cmd)
325 {
326 switch(cmd) {
327 case CMD_PLAY:
328 if (file_handle_enter(c)) {
329 file_repaint();
330 return true;
331 }
333 break;
335 case CMD_GO_ROOT_DIRECTORY:
336 file_change_directory(c, "");
337 file_repaint();
338 return true;
339 case CMD_GO_PARENT_DIRECTORY:
340 file_change_to_parent(c);
341 file_repaint();
342 return true;
344 case CMD_LOCATE:
345 /* don't let browser_cmd() evaluate the locate command
346 - it's a no-op, and by the way, leads to a
347 segmentation fault in the current implementation */
348 return false;
350 case CMD_DELETE:
351 handle_delete(c);
352 file_repaint();
353 break;
354 case CMD_SAVE_PLAYLIST:
355 handle_save(c);
356 break;
357 case CMD_SCREEN_UPDATE:
358 file_reload(c);
359 #ifndef NCMPC_MINI
360 sync_highlights(c, browser.filelist);
361 #endif
362 list_window_check_selected(browser.lw,
363 filelist_length(browser.filelist));
364 file_repaint();
365 return false;
367 case CMD_DB_UPDATE:
368 if (c->status == NULL)
369 return true;
371 if (!c->status->updatingDb) {
372 if (mpdclient_cmd_db_update(c, current_path) == 0) {
373 if (strcmp(current_path, "") != 0) {
374 char *path_locale =
375 utf8_to_locale(current_path);
376 screen_status_printf(_("Database update of %s started"),
377 path_locale);
378 g_free(path_locale);
379 } else
380 screen_status_printf(_("Database update started"));
381 }
382 } else
383 screen_status_printf(_("Database update running..."));
384 return true;
386 default:
387 break;
388 }
390 if (browser_cmd(&browser, c, cmd)) {
391 if (screen_is_visible(&screen_browse))
392 file_repaint();
393 return true;
394 }
396 return false;
397 }
399 const struct screen_functions screen_browse = {
400 .init = browse_init,
401 .exit = browse_exit,
402 .open = browse_open,
403 .resize = browse_resize,
404 .paint = browse_paint,
405 .cmd = browse_cmd,
406 .get_title = browse_title,
407 };
409 bool
410 screen_file_goto_song(struct mpdclient *c, const struct mpd_song *song)
411 {
412 const char *slash, *parent;
413 char *allocated = NULL;
414 bool ret;
415 int i;
417 assert(song != NULL);
418 assert(song->file != NULL);
420 if (strstr(song->file, "//") != NULL)
421 /* an URL? */
422 return false;
424 /* determine the song's parent directory and go there */
426 slash = strrchr(song->file, '/');
427 if (slash != NULL)
428 parent = allocated = g_strndup(song->file, slash - song->file);
429 else
430 parent = "";
432 ret = file_change_directory(c, parent);
433 g_free(allocated);
434 if (!ret)
435 return false;
437 /* select the specified song */
439 i = filelist_find_song(browser.filelist, song);
440 if (i < 0)
441 i = 0;
443 list_window_set_selected(browser.lw, i);
445 /* finally, switch to the file screen */
446 screen_switch(&screen_browse, c);
447 return true;
448 }