7afc865333d30a35a24ccc553745b7ff6246b07d
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_browser.h"
21 #include "i18n.h"
22 #include "options.h"
23 #include "charset.h"
24 #include "strfsong.h"
25 #include "screen_utils.h"
27 #include <string.h>
29 #define BUFSIZE 1024
31 #ifndef NCMPC_MINI
32 #define HIGHLIGHT (0x01)
33 #endif
35 static const char playlist_format[] = "*%s*";
37 #ifndef NCMPC_MINI
39 /* clear the highlight flag for all items in the filelist */
40 static void
41 clear_highlights(mpdclient_filelist_t *fl)
42 {
43 guint i;
45 for (i = 0; i < filelist_length(fl); ++i) {
46 struct filelist_entry *entry = filelist_get(fl, i);
48 entry->flags &= ~HIGHLIGHT;
49 }
50 }
52 /* change the highlight flag for a song */
53 static void
54 set_highlight(mpdclient_filelist_t *fl, mpd_Song *song, int highlight)
55 {
56 int i = filelist_find_song(fl, song);
57 struct filelist_entry *entry;
59 if (i < 0)
60 return;
62 entry = filelist_get(fl, i);
63 if (highlight)
64 entry->flags |= HIGHLIGHT;
65 else
66 entry->flags &= ~HIGHLIGHT;
67 }
69 /* sync highlight flags with playlist */
70 void
71 sync_highlights(mpdclient_t *c, mpdclient_filelist_t *fl)
72 {
73 guint i;
75 for (i = 0; i < filelist_length(fl); ++i) {
76 struct filelist_entry *entry = filelist_get(fl, i);
77 mpd_InfoEntity *entity = entry->entity;
79 if ( entity && entity->type==MPD_INFO_ENTITY_TYPE_SONG ) {
80 mpd_Song *song = entity->info.song;
82 if (playlist_get_index_from_same_song(c, song) >= 0)
83 entry->flags |= HIGHLIGHT;
84 else
85 entry->flags &= ~HIGHLIGHT;
86 }
87 }
88 }
90 /* the playlist has been updated -> fix highlights */
91 void
92 browser_playlist_changed(struct screen_browser *browser, mpdclient_t *c,
93 int event, gpointer data)
94 {
95 if (browser->filelist == NULL)
96 return;
98 switch(event) {
99 case PLAYLIST_EVENT_CLEAR:
100 clear_highlights(browser->filelist);
101 break;
102 case PLAYLIST_EVENT_ADD:
103 set_highlight(browser->filelist, (mpd_Song *) data, 1);
104 break;
105 case PLAYLIST_EVENT_DELETE:
106 set_highlight(browser->filelist, (mpd_Song *) data, 0);
107 break;
108 case PLAYLIST_EVENT_MOVE:
109 break;
110 default:
111 sync_highlights(c, browser->filelist);
112 break;
113 }
114 }
116 #endif
118 /* list_window callback */
119 const char *
120 browser_lw_callback(unsigned idx, bool *highlight, G_GNUC_UNUSED char **second_column, void *data)
121 {
122 static char buf[BUFSIZE];
123 mpdclient_filelist_t *fl = (mpdclient_filelist_t *) data;
124 filelist_entry_t *entry;
125 mpd_InfoEntity *entity;
127 if (idx >= filelist_length(fl))
128 return NULL;
130 entry = filelist_get(fl, idx);
131 assert(entry != NULL);
133 entity = entry->entity;
134 #ifndef NCMPC_MINI
135 *highlight = (entry->flags & HIGHLIGHT) != 0;
136 #else
137 *highlight = false;
138 #endif
140 if( entity == NULL )
141 return "[..]";
143 if( entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY ) {
144 mpd_Directory *dir = entity->info.directory;
145 char *directory = utf8_to_locale(g_basename(dir->path));
147 g_snprintf(buf, BUFSIZE, "[%s]", directory);
148 g_free(directory);
149 return buf;
150 } else if( entity->type==MPD_INFO_ENTITY_TYPE_SONG ) {
151 mpd_Song *song = entity->info.song;
153 strfsong(buf, BUFSIZE, options.list_format, song);
154 return buf;
155 } else if( entity->type==MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) {
156 mpd_PlaylistFile *plf = entity->info.playlistFile;
157 char *filename = utf8_to_locale(g_basename(plf->path));
159 g_snprintf(buf, BUFSIZE, playlist_format, filename);
160 g_free(filename);
161 return buf;
162 }
164 return "Error: Unknown entry!";
165 }
167 /* chdir */
168 bool
169 browser_change_directory(struct screen_browser *browser, mpdclient_t *c,
170 filelist_entry_t *entry, const char *new_path)
171 {
172 mpd_InfoEntity *entity = NULL;
173 gchar *path = NULL;
174 char *old_path;
175 int idx;
177 if( entry!=NULL )
178 entity = entry->entity;
179 else if( new_path==NULL )
180 return false;
182 if( entity==NULL ) {
183 if( entry || 0==strcmp(new_path, "..") ) {
184 /* return to parent */
185 char *parent = g_path_get_dirname(browser->filelist->path);
186 if( strcmp(parent, ".") == 0 )
187 parent[0] = '\0';
188 path = g_strdup(parent);
189 g_free(parent);
190 } else {
191 /* entry==NULL, then new_path ("" is root) */
192 path = g_strdup(new_path);
193 }
194 } else if( entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY) {
195 /* enter sub */
196 mpd_Directory *dir = entity->info.directory;
197 path = g_strdup(dir->path);
198 } else
199 return false;
201 if (browser->filelist != NULL) {
202 old_path = g_strdup(browser->filelist->path);
203 filelist_free(browser->filelist);
204 } else
205 old_path = NULL;
207 browser->filelist = mpdclient_filelist_get(c, path);
208 #ifndef NCMPC_MINI
209 sync_highlights(c, browser->filelist);
210 #endif
212 idx = old_path != NULL
213 ? filelist_find_directory(browser->filelist, old_path)
214 : -1;
215 g_free(old_path);
217 list_window_reset(browser->lw);
218 if (idx >= 0) {
219 list_window_set_selected(browser->lw, idx);
220 list_window_center(browser->lw,
221 filelist_length(browser->filelist), idx);
222 }
224 g_free(path);
225 return true;
226 }
228 static bool
229 load_playlist(mpdclient_t *c, const mpd_PlaylistFile *plf)
230 {
231 char *filename = utf8_to_locale(plf->path);
233 if (mpdclient_cmd_load_playlist(c, plf->path) == 0)
234 screen_status_printf(_("Loading playlist %s..."),
235 g_basename(filename));
236 g_free(filename);
237 return true;
238 }
240 static bool
241 enqueue_and_play(mpdclient_t *c, filelist_entry_t *entry)
242 {
243 int idx;
244 mpd_InfoEntity *entity = entry->entity;
245 mpd_Song *song = entity->info.song;
247 #ifndef NCMPC_MINI
248 if (!(entry->flags & HIGHLIGHT)) {
249 #endif
250 if (mpdclient_cmd_add(c, song) == 0) {
251 char buf[BUFSIZE];
253 #ifndef NCMPC_MINI
254 entry->flags |= HIGHLIGHT;
255 #endif
256 strfsong(buf, BUFSIZE, options.list_format, song);
257 screen_status_printf(_("Adding \'%s\' to playlist"), buf);
258 mpdclient_update(c); /* get song id */
259 } else
260 return false;
261 #ifndef NCMPC_MINI
262 }
263 #endif
265 idx = playlist_get_index_from_same_song(c, song);
266 mpdclient_cmd_play(c, idx);
267 return true;
268 }
270 static struct filelist_entry *
271 browser_get_selected(const struct screen_browser *browser)
272 {
273 if (browser->filelist == NULL ||
274 browser->lw->selected_start < browser->lw->selected_end ||
275 browser->lw->selected >= filelist_length(browser->filelist))
276 return NULL;
278 return filelist_get(browser->filelist, browser->lw->selected);
279 }
281 static struct filelist_entry *
282 browser_get_index(const struct screen_browser *browser, unsigned i)
283 {
284 if (browser->filelist == NULL ||
285 i >= filelist_length(browser->filelist))
286 return NULL;
288 return filelist_get(browser->filelist, i);
289 }
291 static bool
292 browser_handle_enter(struct screen_browser *browser, mpdclient_t *c)
293 {
294 struct filelist_entry *entry = browser_get_selected(browser);
295 mpd_InfoEntity *entity;
297 if (entry == NULL)
298 return false;
300 entity = entry->entity;
301 if (entity == NULL || entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY)
302 return browser_change_directory(browser, c, entry, NULL);
303 else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE)
304 return load_playlist(c, entity->info.playlistFile);
305 else if (entity->type == MPD_INFO_ENTITY_TYPE_SONG)
306 return enqueue_and_play(c, entry);
307 return false;
308 }
310 static bool
311 browser_select_entry(mpdclient_t *c, filelist_entry_t *entry,
312 G_GNUC_UNUSED gboolean toggle)
313 {
314 assert(entry != NULL);
315 assert(entry->entity != NULL);
317 if (entry->entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE)
318 return load_playlist(c, entry->entity->info.playlistFile);
320 if (entry->entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
321 mpd_Directory *dir = entry->entity->info.directory;
323 if (mpdclient_cmd_add_path(c, dir->path) == 0) {
324 char *tmp = utf8_to_locale(dir->path);
326 screen_status_printf(_("Adding \'%s\' to playlist"), tmp);
327 g_free(tmp);
328 }
330 return true;
331 }
333 if (entry->entity->type != MPD_INFO_ENTITY_TYPE_SONG)
334 return false;
336 assert(entry->entity->info.song != NULL);
338 #ifndef NCMPC_MINI
339 if (!toggle || (entry->flags & HIGHLIGHT) == 0)
340 #endif
341 {
342 mpd_Song *song = entry->entity->info.song;
344 #ifndef NCMPC_MINI
345 entry->flags |= HIGHLIGHT;
346 #endif
348 if (mpdclient_cmd_add(c, song) == 0) {
349 char buf[BUFSIZE];
351 strfsong(buf, BUFSIZE, options.list_format, song);
352 screen_status_printf(_("Adding \'%s\' to playlist"), buf);
353 }
354 #ifndef NCMPC_MINI
355 } else {
356 /* remove song from playlist */
357 mpd_Song *song = entry->entity->info.song;
358 int idx;
360 entry->flags &= ~HIGHLIGHT;
362 while ((idx = playlist_get_index_from_same_song(c, song)) >= 0)
363 mpdclient_cmd_delete(c, idx);
364 #endif
365 }
367 return true;
368 }
370 static bool
371 browser_handle_select(struct screen_browser *browser, mpdclient_t *c)
372 {
373 struct filelist_entry *entry;
375 if (browser->lw->range_selection) {
376 for (unsigned i = browser->lw->selected_start;
377 i <= browser->lw->selected_end; i++) {
378 entry = browser_get_index(browser, i);
380 if (entry != NULL && entry->entity != NULL)
381 browser_select_entry(c, entry, TRUE);
382 }
383 return false;
384 } else {
385 entry = browser_get_selected(browser);
387 if (entry == NULL || entry->entity == NULL)
388 return false;
390 return browser_select_entry(c, entry, TRUE);
391 }
392 }
394 static bool
395 browser_handle_add(struct screen_browser *browser, mpdclient_t *c)
396 {
397 struct filelist_entry *entry;
399 if (browser->lw->range_selection) {
400 for (unsigned i = browser->lw->selected_start;
401 i <= browser->lw->selected_end; i++) {
402 entry = browser_get_index(browser, i);
404 if (entry != NULL && entry->entity != NULL)
405 browser_select_entry(c, entry, FALSE);
406 }
407 return false;
408 } else {
409 entry = browser_get_selected(browser);
411 if (entry == NULL || entry->entity == NULL)
412 return false;
414 return browser_select_entry(c, entry, FALSE);
415 }
416 }
418 static void
419 browser_handle_select_all(struct screen_browser *browser, mpdclient_t *c)
420 {
421 guint i;
423 if (browser->filelist == NULL)
424 return;
426 for (i = 0; i < filelist_length(browser->filelist); ++i) {
427 struct filelist_entry *entry = filelist_get(browser->filelist, i);
429 if (entry != NULL && entry->entity != NULL)
430 browser_select_entry(c, entry, FALSE);
431 }
432 }
434 #ifdef HAVE_GETMOUSE
435 static int
436 browser_handle_mouse_event(struct screen_browser *browser, mpdclient_t *c)
437 {
438 int row;
439 unsigned prev_selected = browser->lw->selected;
440 unsigned long bstate;
441 int length;
443 if (browser->filelist)
444 length = filelist_length(browser->filelist);
445 else
446 length = 0;
448 if (screen_get_mouse_event(c, &bstate, &row) ||
449 list_window_mouse(browser->lw, length, bstate, row))
450 return 1;
452 browser->lw->selected = browser->lw->start + row;
453 list_window_check_selected(browser->lw, length);
455 if( bstate & BUTTON1_CLICKED ) {
456 if (prev_selected == browser->lw->selected)
457 browser_handle_enter(browser, c);
458 } else if (bstate & BUTTON3_CLICKED) {
459 if (prev_selected == browser->lw->selected)
460 browser_handle_select(browser, c);
461 }
463 return 1;
464 }
465 #endif
467 bool
468 browser_cmd(struct screen_browser *browser,
469 struct mpdclient *c, command_t cmd)
470 {
471 struct filelist_entry *entry;
473 switch (cmd) {
474 case CMD_PLAY:
475 browser_handle_enter(browser, c);
476 return true;
478 case CMD_SELECT:
479 if (browser_handle_select(browser, c))
480 /* continue and select next item... */
481 cmd = CMD_LIST_NEXT;
483 /* call list_window_cmd to go to the next item */
484 break;
486 case CMD_ADD:
487 if (browser_handle_add(browser, c))
488 /* continue and select next item... */
489 cmd = CMD_LIST_NEXT;
491 /* call list_window_cmd to go to the next item */
492 break;
494 case CMD_SELECT_ALL:
495 browser_handle_select_all(browser, c);
496 return true;
498 case CMD_LIST_FIND:
499 case CMD_LIST_RFIND:
500 case CMD_LIST_FIND_NEXT:
501 case CMD_LIST_RFIND_NEXT:
502 screen_find(browser->lw, filelist_length(browser->filelist),
503 cmd, browser_lw_callback,
504 browser->filelist);
505 return true;
506 case CMD_LIST_JUMP:
507 screen_jump(browser->lw, browser_lw_callback, browser->filelist);
508 return true;
510 #ifdef HAVE_GETMOUSE
511 case CMD_MOUSE_EVENT:
512 browser_handle_mouse_event(browser, c);
513 return true;
514 #endif
516 #ifdef ENABLE_SONG_SCREEN
517 case CMD_SCREEN_SONG:
518 entry = browser_get_selected(browser);
519 if (entry == NULL || entry->entity == NULL ||
520 entry->entity->type != MPD_INFO_ENTITY_TYPE_SONG)
521 return false;
523 screen_song_switch(c, entry->entity->info.song);
524 return true;
525 #endif
527 case CMD_LOCATE:
528 entry = browser_get_selected(browser);
529 if (entry == NULL || entry->entity == NULL ||
530 entry->entity->type != MPD_INFO_ENTITY_TYPE_SONG)
531 return false;
533 screen_file_goto_song(c, entry->entity->info.song);
534 return true;
536 #ifdef ENABLE_LYRICS_SCREEN
537 case CMD_SCREEN_LYRICS:
538 entry = browser_get_selected(browser);
539 if (entry == NULL)
540 return false;
542 if (entry->entity == NULL ||
543 entry->entity->type != MPD_INFO_ENTITY_TYPE_SONG)
544 return true;
546 screen_lyrics_switch(c, entry->entity->info.song, false);
547 return true;
548 #endif
549 case CMD_SCREEN_SWAP:
550 entry = browser_get_selected(browser);
551 if (entry->entity != NULL &&
552 entry->entity->type == MPD_INFO_ENTITY_TYPE_SONG)
553 screen_swap(c, entry->entity->info.song);
554 else
555 screen_swap(c, NULL);
556 return true;
558 default:
559 break;
560 }
562 if (list_window_cmd(browser->lw, filelist_length(browser->filelist),
563 cmd))
564 return true;
566 return false;
567 }