d9faa69ac66430032a736b971de0211b9a8db7dc
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 if (!mpdclient_is_connected(c))
60 return;
62 connection = mpdclient_get_connection(c);
64 mpd_send_list_meta(connection, current_path);
65 filelist_recv(filelist, connection);
67 if (mpd_response_finish(connection))
68 filelist_sort_dir_play(filelist, compare_filelist_entry_path);
69 else
70 mpdclient_handle_error(c);
71 }
73 static void
74 screen_file_reload(struct mpdclient *c)
75 {
76 if (browser.filelist != NULL)
77 filelist_free(browser.filelist);
79 browser.filelist = filelist_new();
80 if (*current_path != 0)
81 /* add a dummy entry for ./.. */
82 filelist_append(browser.filelist, NULL);
84 screen_file_load_list(c, browser.filelist);
86 list_window_set_length(browser.lw,
87 filelist_length(browser.filelist));
88 }
90 /**
91 * Change to the specified absolute directory.
92 */
93 static bool
94 change_directory(struct mpdclient *c, const char *new_path)
95 {
96 g_free(current_path);
97 current_path = g_strdup(new_path);
99 screen_file_reload(c);
101 #ifndef NCMPC_MINI
102 screen_browser_sync_highlights(browser.filelist, &c->playlist);
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 change_to_parent(struct mpdclient *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 = 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_cursor(browser.lw, idx);
138 list_window_center(browser.lw, idx);
139 }
141 return success;
142 }
144 /**
145 * Change to the directory referred by the specified #filelist_entry
146 * object.
147 */
148 static bool
149 change_to_entry(struct mpdclient *c, const struct filelist_entry *entry)
150 {
151 assert(entry != NULL);
153 if (entry->entity == NULL)
154 return change_to_parent(c);
155 else if (mpd_entity_get_type(entry->entity) == MPD_ENTITY_TYPE_DIRECTORY)
156 return change_directory(c, mpd_directory_get_path(mpd_entity_get_directory(entry->entity)));
157 else
158 return false;
159 }
161 static bool
162 screen_file_handle_enter(struct mpdclient *c)
163 {
164 const struct filelist_entry *entry = browser_get_selected_entry(&browser);
166 if (entry == NULL)
167 return false;
169 return change_to_entry(c, entry);
170 }
172 static void
173 handle_save(struct mpdclient *c)
174 {
175 struct list_window_range range;
176 const char *defaultname = NULL;
177 char *defaultname_utf8 = NULL;
179 list_window_get_range(browser.lw, &range);
180 if (range.start == range.end)
181 return;
183 for (unsigned i = range.start; i < range.end; ++i) {
184 struct filelist_entry *entry =
185 filelist_get(browser.filelist, i);
186 if( entry && entry->entity ) {
187 struct mpd_entity *entity = entry->entity;
188 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_PLAYLIST) {
189 const struct mpd_playlist *playlist =
190 mpd_entity_get_playlist(entity);
191 defaultname = mpd_playlist_get_path(playlist);
192 }
193 }
194 }
196 if(defaultname)
197 defaultname_utf8 = utf8_to_locale(defaultname);
198 playlist_save(c, NULL, defaultname_utf8);
199 g_free(defaultname_utf8);
200 }
202 static void
203 handle_delete(struct mpdclient *c)
204 {
205 struct mpd_connection *connection = mpdclient_get_connection(c);
206 struct list_window_range range;
207 struct mpd_entity *entity;
208 const struct mpd_playlist *playlist;
209 char *str, *buf;
210 int key;
212 list_window_get_range(browser.lw, &range);
213 for (unsigned i = range.start; i < range.end; ++i) {
214 struct filelist_entry *entry =
215 filelist_get(browser.filelist, i);
216 if( entry==NULL || entry->entity==NULL )
217 continue;
219 entity = entry->entity;
221 if (mpd_entity_get_type(entity) != MPD_ENTITY_TYPE_PLAYLIST) {
222 /* translators: the "delete" command is only possible
223 for playlists; the user attempted to delete a song
224 or a directory or something else */
225 screen_status_printf(_("Deleting this item is not possible"));
226 screen_bell();
227 continue;
228 }
230 playlist = mpd_entity_get_playlist(entity);
231 str = utf8_to_locale(g_basename(mpd_playlist_get_path(playlist)));
232 buf = g_strdup_printf(_("Delete playlist %s [%s/%s] ? "), str, YES, NO);
233 g_free(str);
234 key = tolower(screen_getch(buf));
235 g_free(buf);
236 if( key != YES[0] ) {
237 /* translators: a dialog was aborted by the user */
238 screen_status_printf(_("Aborted"));
239 return;
240 }
242 if (!mpd_run_rm(connection, mpd_playlist_get_path(playlist))) {
243 mpdclient_handle_error(c);
244 break;
245 }
247 c->events |= MPD_IDLE_STORED_PLAYLIST;
249 /* translators: MPD deleted the playlist, as requested by the
250 user */
251 screen_status_printf(_("Playlist deleted"));
252 }
253 }
255 static void
256 screen_file_init(WINDOW *w, int cols, int rows)
257 {
258 current_path = g_strdup("");
260 browser.lw = list_window_init(w, cols, rows);
261 }
263 static void
264 screen_file_resize(int cols, int rows)
265 {
266 list_window_resize(browser.lw, cols, 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 screen_browser_paint(&browser);
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 }
323 #ifndef NCMPC_MINI
324 if (c->events & (MPD_IDLE_DATABASE | MPD_IDLE_STORED_PLAYLIST |
325 MPD_IDLE_PLAYLIST))
326 screen_browser_sync_highlights(browser.filelist, &c->playlist);
327 #endif
329 if (c->events & (MPD_IDLE_DATABASE | MPD_IDLE_STORED_PLAYLIST
330 #ifndef NCMPC_MINI
331 | MPD_IDLE_PLAYLIST
332 #endif
333 ))
334 screen_file_repaint();
335 }
337 static bool
338 screen_file_cmd(struct mpdclient *c, command_t cmd)
339 {
340 switch(cmd) {
341 case CMD_PLAY:
342 if (screen_file_handle_enter(c)) {
343 screen_file_repaint();
344 return true;
345 }
347 break;
349 case CMD_GO_ROOT_DIRECTORY:
350 change_directory(c, "");
351 screen_file_repaint();
352 return true;
353 case CMD_GO_PARENT_DIRECTORY:
354 change_to_parent(c);
355 screen_file_repaint();
356 return true;
358 case CMD_LOCATE:
359 /* don't let browser_cmd() evaluate the locate command
360 - it's a no-op, and by the way, leads to a
361 segmentation fault in the current implementation */
362 return false;
364 case CMD_SCREEN_UPDATE:
365 screen_file_reload(c);
366 #ifndef NCMPC_MINI
367 screen_browser_sync_highlights(browser.filelist, &c->playlist);
368 #endif
369 screen_file_repaint();
370 return false;
372 default:
373 break;
374 }
376 if (browser_cmd(&browser, c, cmd)) {
377 if (screen_is_visible(&screen_browse))
378 screen_file_repaint();
379 return true;
380 }
382 if (!mpdclient_is_connected(c))
383 return false;
385 switch(cmd) {
386 case CMD_DELETE:
387 handle_delete(c);
388 screen_file_repaint();
389 break;
391 case CMD_SAVE_PLAYLIST:
392 handle_save(c);
393 break;
395 case CMD_DB_UPDATE:
396 screen_database_update(c, current_path);
397 return true;
399 default:
400 break;
401 }
403 return false;
404 }
406 const struct screen_functions screen_browse = {
407 .init = screen_file_init,
408 .exit = screen_file_exit,
409 .open = screen_file_open,
410 .resize = screen_file_resize,
411 .paint = screen_file_paint,
412 .update = screen_file_update,
413 .cmd = screen_file_cmd,
414 .get_title = screen_file_get_title,
415 };
417 bool
418 screen_file_goto_song(struct mpdclient *c, const struct mpd_song *song)
419 {
420 const char *uri, *slash, *parent;
421 char *allocated = NULL;
422 bool ret;
423 int i;
425 assert(song != NULL);
427 uri = mpd_song_get_uri(song);
429 if (strstr(uri, "//") != NULL)
430 /* an URL? */
431 return false;
433 /* determine the song's parent directory and go there */
435 slash = strrchr(uri, '/');
436 if (slash != NULL)
437 parent = allocated = g_strndup(uri, slash - uri);
438 else
439 parent = "";
441 ret = change_directory(c, parent);
442 g_free(allocated);
443 if (!ret)
444 return false;
446 /* select the specified song */
448 i = filelist_find_song(browser.filelist, song);
449 if (i < 0)
450 i = 0;
452 list_window_set_cursor(browser.lw, i);
454 /* finally, switch to the file screen */
455 screen_switch(&screen_browse, c);
456 return true;
457 }