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 = c->connection;
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);
80 }
82 /**
83 * Change to the specified absolute directory.
84 */
85 static bool
86 change_directory(struct mpdclient *c, const char *new_path)
87 {
88 g_free(current_path);
89 current_path = g_strdup(new_path);
91 screen_file_reload(c);
93 #ifndef NCMPC_MINI
94 screen_browser_sync_highlights(browser.filelist, &c->playlist);
95 #endif
97 list_window_reset(browser.lw);
99 return browser.filelist != NULL;
100 }
102 /**
103 * Change to the parent directory of the current directory.
104 */
105 static bool
106 change_to_parent(struct mpdclient *c)
107 {
108 char *parent = g_path_get_dirname(current_path);
109 char *old_path;
110 int idx;
111 bool success;
113 if (strcmp(parent, ".") == 0)
114 parent[0] = '\0';
116 old_path = current_path;
117 current_path = NULL;
119 success = change_directory(c, parent);
120 g_free(parent);
122 idx = success
123 ? filelist_find_directory(browser.filelist, old_path)
124 : -1;
125 g_free(old_path);
127 if (success && idx >= 0) {
128 /* set the cursor on the previous working directory */
129 list_window_set_selected(browser.lw, idx);
130 list_window_center(browser.lw,
131 filelist_length(browser.filelist), idx);
132 }
134 return success;
135 }
137 /**
138 * Change to the directory referred by the specified #filelist_entry
139 * object.
140 */
141 static bool
142 change_to_entry(struct mpdclient *c, const struct filelist_entry *entry)
143 {
144 assert(entry != NULL);
146 if (entry->entity == NULL)
147 return change_to_parent(c);
148 else if (mpd_entity_get_type(entry->entity) == MPD_ENTITY_TYPE_DIRECTORY)
149 return change_directory(c, mpd_directory_get_path(mpd_entity_get_directory(entry->entity)));
150 else
151 return false;
152 }
154 static bool
155 screen_file_handle_enter(struct mpdclient *c)
156 {
157 const struct filelist_entry *entry = browser_get_selected_entry(&browser);
159 if (entry == NULL)
160 return false;
162 return change_to_entry(c, entry);
163 }
165 static int
166 handle_save(struct mpdclient *c)
167 {
168 struct filelist_entry *entry;
169 const char *defaultname = NULL;
170 char *defaultname_utf8 = NULL;
171 int ret;
172 unsigned selected;
174 if (browser.lw->selected >= filelist_length(browser.filelist))
175 return -1;
177 for(selected = browser.lw->selected_start; selected <= browser.lw->selected_end; ++selected)
178 {
179 entry = filelist_get(browser.filelist, selected);
180 if( entry && entry->entity ) {
181 struct mpd_entity *entity = entry->entity;
182 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_PLAYLIST) {
183 const struct mpd_playlist *playlist =
184 mpd_entity_get_playlist(entity);
185 defaultname = mpd_playlist_get_path(playlist);
186 }
187 }
188 }
190 if(defaultname)
191 defaultname_utf8 = utf8_to_locale(defaultname);
192 ret = playlist_save(c, NULL, defaultname_utf8);
193 g_free(defaultname_utf8);
195 return ret;
196 }
198 static int
199 handle_delete(struct mpdclient *c)
200 {
201 struct filelist_entry *entry;
202 struct mpd_entity *entity;
203 const struct mpd_playlist *playlist;
204 char *str, *buf;
205 int key;
206 unsigned selected;
208 for(selected = browser.lw->selected_start; selected <= browser.lw->selected_end; ++selected)
209 {
210 if (selected >= filelist_length(browser.filelist))
211 return -1;
213 entry = filelist_get(browser.filelist, selected);
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(c->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;
252 }
254 static void
255 screen_file_init(WINDOW *w, int cols, int rows)
256 {
257 current_path = g_strdup("");
259 browser.lw = list_window_init(w, cols, rows);
260 }
262 static void
263 screen_file_resize(int cols, int rows)
264 {
265 browser.lw->cols = cols;
266 browser.lw->rows = rows;
267 }
269 static void
270 screen_file_exit(void)
271 {
272 if (browser.filelist)
273 filelist_free(browser.filelist);
274 list_window_free(browser.lw);
276 g_free(current_path);
277 }
279 static void
280 screen_file_open(struct mpdclient *c)
281 {
282 screen_file_reload(c);
283 }
285 static const char *
286 screen_file_get_title(char *str, size_t size)
287 {
288 const char *path = NULL, *prev = NULL, *slash = current_path;
289 char *path_locale;
291 /* determine the last 2 parts of the path */
292 while ((slash = strchr(slash, '/')) != NULL) {
293 path = prev;
294 prev = ++slash;
295 }
297 if (path == NULL)
298 /* fall back to full path */
299 path = current_path;
301 path_locale = utf8_to_locale(path);
302 g_snprintf(str, size, "%s: %s",
303 /* translators: caption of the browser screen */
304 _("Browse"), path_locale);
305 g_free(path_locale);
306 return str;
307 }
309 static void
310 screen_file_paint(void)
311 {
312 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
313 }
315 static void
316 screen_file_update(struct mpdclient *c)
317 {
318 if (c->events & (MPD_IDLE_DATABASE | MPD_IDLE_STORED_PLAYLIST)) {
319 /* the db has changed -> update the filelist */
320 screen_file_reload(c);
321 list_window_check_selected(browser.lw,
322 filelist_length(browser.filelist));
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();
337 }
339 static bool
340 screen_file_cmd(struct mpdclient *c, command_t cmd)
341 {
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 list_window_check_selected(browser.lw,
372 filelist_length(browser.filelist));
373 screen_file_repaint();
374 return false;
376 default:
377 break;
378 }
380 if (browser_cmd(&browser, c, cmd)) {
381 if (screen_is_visible(&screen_browse))
382 screen_file_repaint();
383 return true;
384 }
386 if (!mpdclient_is_connected(c))
387 return false;
389 switch(cmd) {
390 case CMD_DELETE:
391 handle_delete(c);
392 screen_file_repaint();
393 break;
395 case CMD_SAVE_PLAYLIST:
396 handle_save(c);
397 break;
399 case CMD_DB_UPDATE:
400 screen_database_update(c, current_path);
401 return true;
403 default:
404 break;
405 }
407 return false;
408 }
410 const struct screen_functions screen_browse = {
411 .init = screen_file_init,
412 .exit = screen_file_exit,
413 .open = screen_file_open,
414 .resize = screen_file_resize,
415 .paint = screen_file_paint,
416 .update = screen_file_update,
417 .cmd = screen_file_cmd,
418 .get_title = screen_file_get_title,
419 };
421 bool
422 screen_file_goto_song(struct mpdclient *c, const struct mpd_song *song)
423 {
424 const char *uri, *slash, *parent;
425 char *allocated = NULL;
426 bool ret;
427 int i;
429 assert(song != NULL);
431 uri = mpd_song_get_uri(song);
433 if (strstr(uri, "//") != NULL)
434 /* an URL? */
435 return false;
437 /* determine the song's parent directory and go there */
439 slash = strrchr(uri, '/');
440 if (slash != NULL)
441 parent = allocated = g_strndup(uri, slash - uri);
442 else
443 parent = "";
445 ret = change_directory(c, parent);
446 g_free(allocated);
447 if (!ret)
448 return false;
450 /* select the specified song */
452 i = filelist_find_song(browser.filelist, song);
453 if (i < 0)
454 i = 0;
456 list_window_set_selected(browser.lw, i);
458 /* finally, switch to the file screen */
459 screen_switch(&screen_browse, c);
460 return true;
461 }