077fca52bd61ccf442e88d1cafba8229feb347ad
1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2010 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
4 *
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.
9 *
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.
14 *
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_artist.h"
21 #include "screen_interface.h"
22 #include "screen_status.h"
23 #include "screen_find.h"
24 #include "screen_browser.h"
25 #include "screen.h"
26 #include "i18n.h"
27 #include "charset.h"
28 #include "mpdclient.h"
29 #include "screen_browser.h"
30 #include "filelist.h"
32 #include <assert.h>
33 #include <string.h>
34 #include <glib.h>
36 #define BUFSIZE 1024
38 typedef enum { LIST_ARTISTS, LIST_ALBUMS, LIST_SONGS } artist_mode_t;
40 static artist_mode_t mode = LIST_ARTISTS;
41 static GPtrArray *artist_list, *album_list;
42 static char *artist = NULL;
43 static char *album = NULL;
44 static char ALL_TRACKS[] = "";
46 static struct screen_browser browser;
48 static gint
49 compare_utf8(gconstpointer s1, gconstpointer s2)
50 {
51 const char *const*t1 = s1, *const*t2 = s2;
53 char *key1 = g_utf8_collate_key(*t1,-1);
54 char *key2 = g_utf8_collate_key(*t2,-1);
55 int n = strcmp(key1,key2);
56 g_free(key1);
57 g_free(key2);
58 return n;
59 }
61 /* list_window callback */
62 static const char *
63 screen_artist_lw_callback(unsigned idx, void *data)
64 {
65 GPtrArray *list = data;
67 if (mode == LIST_ALBUMS) {
68 if (idx == 0)
69 return "..";
70 else if (idx == list->len + 1)
71 return _("All tracks");
73 --idx;
74 }
76 assert(idx < list->len);
78 char *str_utf8 = g_ptr_array_index(list, idx);
79 assert(str_utf8 != NULL);
81 char *str = utf8_to_locale(str_utf8);
83 static char buf[BUFSIZE];
84 g_strlcpy(buf, str, sizeof(buf));
85 g_free(str);
87 return buf;
88 }
90 static void
91 screen_artist_paint(void);
93 static void
94 artist_repaint(void)
95 {
96 screen_artist_paint();
97 wrefresh(browser.lw->w);
98 }
100 static void
101 string_array_free(GPtrArray *array)
102 {
103 for (unsigned i = 0; i < array->len; ++i) {
104 char *value = g_ptr_array_index(array, i);
105 g_free(value);
106 }
108 g_ptr_array_free(array, TRUE);
109 }
111 static void
112 free_lists(void)
113 {
114 if (artist_list != NULL) {
115 string_array_free(artist_list);
116 artist_list = NULL;
117 }
119 if (album_list != NULL) {
120 string_array_free(album_list);
121 album_list = NULL;
122 }
124 if (browser.filelist) {
125 filelist_free(browser.filelist);
126 browser.filelist = NULL;
127 }
128 }
130 static void
131 recv_tag_values(struct mpd_connection *connection, enum mpd_tag_type tag,
132 GPtrArray *list)
133 {
134 struct mpd_pair *pair;
136 while ((pair = mpd_recv_pair_tag(connection, tag)) != NULL) {
137 g_ptr_array_add(list, g_strdup(pair->value));
138 mpd_return_pair(connection, pair);
139 }
140 }
142 static void
143 load_artist_list(struct mpdclient *c)
144 {
145 struct mpd_connection *connection = mpdclient_get_connection(c);
147 assert(mode == LIST_ARTISTS);
148 assert(artist == NULL);
149 assert(album == NULL);
150 assert(artist_list == NULL);
151 assert(album_list == NULL);
152 assert(browser.filelist == NULL);
154 artist_list = g_ptr_array_new();
156 if (connection != NULL) {
157 mpd_search_db_tags(connection, MPD_TAG_ARTIST);
158 mpd_search_commit(connection);
159 recv_tag_values(connection, MPD_TAG_ARTIST, artist_list);
161 mpdclient_finish_command(c);
162 }
164 /* sort list */
165 g_ptr_array_sort(artist_list, compare_utf8);
166 list_window_set_length(browser.lw, artist_list->len);
167 }
169 static void
170 load_album_list(struct mpdclient *c)
171 {
172 struct mpd_connection *connection = mpdclient_get_connection(c);
174 assert(mode == LIST_ALBUMS);
175 assert(artist != NULL);
176 assert(album == NULL);
177 assert(album_list == NULL);
178 assert(browser.filelist == NULL);
180 album_list = g_ptr_array_new();
182 if (connection != NULL) {
183 mpd_search_db_tags(connection, MPD_TAG_ALBUM);
184 mpd_search_add_tag_constraint(connection,
185 MPD_OPERATOR_DEFAULT,
186 MPD_TAG_ARTIST, artist);
187 mpd_search_commit(connection);
189 recv_tag_values(connection, MPD_TAG_ALBUM, album_list);
191 mpdclient_finish_command(c);
192 }
194 /* sort list */
195 g_ptr_array_sort(album_list, compare_utf8);
197 list_window_set_length(browser.lw, album_list->len + 2);
198 }
200 static void
201 load_song_list(struct mpdclient *c)
202 {
203 struct mpd_connection *connection = mpdclient_get_connection(c);
205 assert(mode == LIST_SONGS);
206 assert(artist != NULL);
207 assert(album != NULL);
208 assert(browser.filelist == NULL);
210 browser.filelist = filelist_new();
211 /* add a dummy entry for ".." */
212 filelist_append(browser.filelist, NULL);
214 if (connection != NULL) {
215 mpd_search_db_songs(connection, true);
216 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
217 MPD_TAG_ARTIST, artist);
218 if (album != ALL_TRACKS)
219 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
220 MPD_TAG_ALBUM, album);
221 mpd_search_commit(connection);
223 filelist_recv(browser.filelist, connection);
225 mpdclient_finish_command(c);
226 }
228 /* fix highlights */
229 screen_browser_sync_highlights(browser.filelist, &c->playlist);
230 list_window_set_length(browser.lw, filelist_length(browser.filelist));
231 }
233 static void
234 free_state(void)
235 {
236 g_free(artist);
237 if (album != ALL_TRACKS)
238 g_free(album);
239 artist = NULL;
240 album = NULL;
242 free_lists();
243 }
245 static void
246 open_artist_list(struct mpdclient *c)
247 {
248 free_state();
250 mode = LIST_ARTISTS;
251 load_artist_list(c);
252 }
254 static void
255 open_album_list(struct mpdclient *c, char *_artist)
256 {
257 assert(_artist != NULL);
259 free_state();
261 mode = LIST_ALBUMS;
262 artist = _artist;
263 load_album_list(c);
264 }
266 static void
267 open_song_list(struct mpdclient *c, char *_artist, char *_album)
268 {
269 assert(_artist != NULL);
270 assert(_album != NULL);
272 free_state();
274 mode = LIST_SONGS;
275 artist = _artist;
276 album = _album;
277 load_song_list(c);
278 }
280 static void
281 reload_lists(struct mpdclient *c)
282 {
283 free_lists();
285 switch (mode) {
286 case LIST_ARTISTS:
287 load_artist_list(c);
288 break;
290 case LIST_ALBUMS:
291 load_album_list(c);
292 break;
294 case LIST_SONGS:
295 load_song_list(c);
296 break;
297 }
298 }
300 static void
301 screen_artist_init(WINDOW *w, int cols, int rows)
302 {
303 browser.lw = list_window_init(w, cols, rows);
304 artist = NULL;
305 album = NULL;
306 }
308 static void
309 screen_artist_quit(void)
310 {
311 free_state();
312 list_window_free(browser.lw);
313 }
315 static void
316 screen_artist_open(struct mpdclient *c)
317 {
318 if (artist_list == NULL && album_list == NULL &&
319 browser.filelist == NULL)
320 reload_lists(c);
321 }
323 static void
324 screen_artist_resize(int cols, int rows)
325 {
326 list_window_resize(browser.lw, cols, rows);
327 }
329 /**
330 * Paint one item in the artist list.
331 */
332 static void
333 paint_artist_callback(WINDOW *w, unsigned i,
334 gcc_unused unsigned y, unsigned width,
335 bool selected, void *data)
336 {
337 GPtrArray *list = data;
338 char *p = utf8_to_locale(g_ptr_array_index(list, i));
340 screen_browser_paint_directory(w, width, selected, p);
341 g_free(p);
342 }
344 /**
345 * Paint one item in the album list. There are two virtual items
346 * inserted: at the beginning, there's the special item ".." to go to
347 * the parent directory, and at the end, there's the item "All tracks"
348 * to view the tracks of all albums.
349 */
350 static void
351 paint_album_callback(WINDOW *w, unsigned i,
352 gcc_unused unsigned y, unsigned width,
353 bool selected, void *data)
354 {
355 GPtrArray *list = data;
356 const char *p;
357 char *q = NULL;
359 if (i == 0)
360 p = "..";
361 else if (i == list->len + 1)
362 p = _("All tracks");
363 else
364 p = q = utf8_to_locale(g_ptr_array_index(list, i - 1));
366 screen_browser_paint_directory(w, width, selected, p);
367 g_free(q);
368 }
370 static void
371 screen_artist_paint(void)
372 {
373 if (browser.filelist) {
374 screen_browser_paint(&browser);
375 } else if (album_list != NULL)
376 list_window_paint2(browser.lw,
377 paint_album_callback, album_list);
378 else if (artist_list != NULL)
379 list_window_paint2(browser.lw,
380 paint_artist_callback, artist_list);
381 else {
382 wmove(browser.lw->w, 0, 0);
383 wclrtobot(browser.lw->w);
384 }
385 }
387 static const char *
388 screen_artist_get_title(char *str, size_t size)
389 {
390 switch(mode) {
391 char *s1, *s2;
393 case LIST_ARTISTS:
394 g_snprintf(str, size, _("All artists"));
395 break;
397 case LIST_ALBUMS:
398 s1 = utf8_to_locale(artist);
399 g_snprintf(str, size, _("Albums of artist: %s"), s1);
400 g_free(s1);
401 break;
403 case LIST_SONGS:
404 s1 = utf8_to_locale(artist);
406 if (album == ALL_TRACKS)
407 g_snprintf(str, size,
408 _("All tracks of artist: %s"), s1);
409 else if (*album != 0) {
410 s2 = utf8_to_locale(album);
411 g_snprintf(str, size, _("Album: %s - %s"), s1, s2);
412 g_free(s2);
413 } else
414 g_snprintf(str, size,
415 _("Tracks of no album of artist: %s"), s1);
416 g_free(s1);
417 break;
418 }
420 return str;
421 }
423 static void
424 screen_artist_update(struct mpdclient *c)
425 {
426 if (browser.filelist == NULL)
427 return;
429 if (c->events & MPD_IDLE_DATABASE)
430 /* the db has changed -> update the list */
431 reload_lists(c);
433 if (c->events & (MPD_IDLE_DATABASE | MPD_IDLE_QUEUE))
434 screen_browser_sync_highlights(browser.filelist, &c->playlist);
436 if (c->events & (MPD_IDLE_DATABASE
437 #ifndef NCMPC_MINI
438 | MPD_IDLE_QUEUE
439 #endif
440 ))
441 artist_repaint();
442 }
444 /* add_query - Add all songs satisfying specified criteria.
445 _artist is actually only used in the ALBUM case to distinguish albums with
446 the same name from different artists. */
447 static void
448 add_query(struct mpdclient *c, enum mpd_tag_type table, const char *_filter,
449 const char *_artist)
450 {
451 struct mpd_connection *connection = mpdclient_get_connection(c);
453 assert(_filter != NULL);
455 if (connection == NULL)
456 return;
458 char *str = utf8_to_locale(_filter);
459 if (table == MPD_TAG_ALBUM)
460 screen_status_printf(_("Adding album %s..."), str);
461 else
462 screen_status_printf(_("Adding %s..."), str);
463 g_free(str);
465 mpd_search_db_songs(connection, true);
466 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
467 table, _filter);
468 if (table == MPD_TAG_ALBUM)
469 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
470 MPD_TAG_ARTIST, _artist);
471 mpd_search_commit(connection);
473 struct filelist *addlist = filelist_new_recv(connection);
475 if (mpdclient_finish_command(c))
476 mpdclient_filelist_add_all(c, addlist);
478 filelist_free(addlist);
479 }
481 static int
482 screen_artist_lw_cmd(struct mpdclient *c, command_t cmd)
483 {
484 switch (mode) {
485 case LIST_ARTISTS:
486 case LIST_ALBUMS:
487 return list_window_cmd(browser.lw, cmd);
489 case LIST_SONGS:
490 return browser_cmd(&browser, c, cmd);
491 }
493 assert(0);
494 return 0;
495 }
497 static int
498 string_array_find(GPtrArray *array, const char *value)
499 {
500 guint i;
502 for (i = 0; i < array->len; ++i)
503 if (strcmp((const char*)g_ptr_array_index(array, i),
504 value) == 0)
505 return i;
507 return -1;
508 }
510 static bool
511 screen_artist_cmd(struct mpdclient *c, command_t cmd)
512 {
513 switch(cmd) {
514 struct list_window_range range;
515 char *selected;
516 char *old;
517 char *old_ptr;
518 int idx;
520 case CMD_PLAY:
521 switch (mode) {
522 case LIST_ARTISTS:
523 if (browser.lw->selected >= artist_list->len)
524 return true;
526 selected = g_ptr_array_index(artist_list,
527 browser.lw->selected);
528 open_album_list(c, g_strdup(selected));
529 list_window_reset(browser.lw);
531 artist_repaint();
532 return true;
534 case LIST_ALBUMS:
535 if (browser.lw->selected == 0) {
536 /* handle ".." */
537 old = g_strdup(artist);
539 open_artist_list(c);
540 list_window_reset(browser.lw);
541 /* restore previous list window state */
542 idx = string_array_find(artist_list, old);
543 g_free(old);
545 if (idx >= 0) {
546 list_window_set_cursor(browser.lw, idx);
547 list_window_center(browser.lw, idx);
548 }
549 } else if (browser.lw->selected == album_list->len + 1) {
550 /* handle "show all" */
551 open_song_list(c, g_strdup(artist), ALL_TRACKS);
552 list_window_reset(browser.lw);
553 } else {
554 /* select album */
555 selected = g_ptr_array_index(album_list,
556 browser.lw->selected - 1);
557 open_song_list(c, g_strdup(artist), g_strdup(selected));
558 list_window_reset(browser.lw);
559 }
561 artist_repaint();
562 return true;
564 case LIST_SONGS:
565 if (browser.lw->selected == 0) {
566 /* handle ".." */
567 old = g_strdup(album);
568 old_ptr = album;
570 open_album_list(c, g_strdup(artist));
571 list_window_reset(browser.lw);
572 /* restore previous list window state */
573 idx = old_ptr == ALL_TRACKS
574 ? (int)album_list->len
575 : string_array_find(album_list, old);
576 g_free(old);
578 if (idx >= 0) {
579 ++idx;
580 list_window_set_cursor(browser.lw, idx);
581 list_window_center(browser.lw, idx);
582 }
584 artist_repaint();
585 return true;
586 }
587 break;
588 }
589 break;
591 /* FIXME? CMD_GO_* handling duplicates code from CMD_PLAY */
593 case CMD_GO_PARENT_DIRECTORY:
594 switch (mode) {
595 case LIST_ARTISTS:
596 break;
598 case LIST_ALBUMS:
599 old = g_strdup(artist);
601 open_artist_list(c);
602 list_window_reset(browser.lw);
603 /* restore previous list window state */
604 idx = string_array_find(artist_list, old);
605 g_free(old);
607 if (idx >= 0) {
608 list_window_set_cursor(browser.lw, idx);
609 list_window_center(browser.lw, idx);
610 }
611 break;
613 case LIST_SONGS:
614 old = g_strdup(album);
615 old_ptr = album;
617 open_album_list(c, g_strdup(artist));
618 list_window_reset(browser.lw);
619 /* restore previous list window state */
620 idx = old_ptr == ALL_TRACKS
621 ? (int)album_list->len
622 : string_array_find(album_list, old);
623 g_free(old);
625 if (idx >= 0) {
626 ++idx;
627 list_window_set_cursor(browser.lw, idx);
628 list_window_center(browser.lw, idx);
629 }
630 break;
631 }
633 artist_repaint();
634 break;
636 case CMD_GO_ROOT_DIRECTORY:
637 switch (mode) {
638 case LIST_ARTISTS:
639 break;
641 case LIST_ALBUMS:
642 case LIST_SONGS:
643 open_artist_list(c);
644 list_window_reset(browser.lw);
645 /* restore first list window state (pop while returning true) */
646 /* XXX */
647 break;
648 }
650 artist_repaint();
651 break;
653 case CMD_SELECT:
654 case CMD_ADD:
655 switch(mode) {
656 case LIST_ARTISTS:
657 if (browser.lw->selected >= artist_list->len)
658 return true;
660 list_window_get_range(browser.lw, &range);
661 for (unsigned i = range.start; i < range.end; ++i) {
662 selected = g_ptr_array_index(artist_list, i);
663 add_query(c, MPD_TAG_ARTIST, selected, NULL);
664 cmd = CMD_LIST_NEXT; /* continue and select next item... */
665 }
666 break;
668 case LIST_ALBUMS:
669 list_window_get_range(browser.lw, &range);
670 for (unsigned i = range.start; i < range.end; ++i) {
671 if(i == album_list->len + 1)
672 add_query(c, MPD_TAG_ARTIST, artist, NULL);
673 else if (i > 0)
674 {
675 selected = g_ptr_array_index(album_list,
676 browser.lw->selected - 1);
677 add_query(c, MPD_TAG_ALBUM, selected, artist);
678 cmd = CMD_LIST_NEXT; /* continue and select next item... */
679 }
680 }
681 break;
683 case LIST_SONGS:
684 /* handled by browser_cmd() */
685 break;
686 }
687 break;
689 /* continue and update... */
690 case CMD_SCREEN_UPDATE:
691 reload_lists(c);
692 return false;
694 case CMD_LIST_FIND:
695 case CMD_LIST_RFIND:
696 case CMD_LIST_FIND_NEXT:
697 case CMD_LIST_RFIND_NEXT:
698 switch (mode) {
699 case LIST_ARTISTS:
700 screen_find(browser.lw, cmd,
701 screen_artist_lw_callback, artist_list);
702 artist_repaint();
703 return true;
705 case LIST_ALBUMS:
706 screen_find(browser.lw, cmd,
707 screen_artist_lw_callback, album_list);
708 artist_repaint();
709 return true;
711 case LIST_SONGS:
712 /* handled by browser_cmd() */
713 break;
714 }
715 case CMD_LIST_JUMP:
716 switch (mode) {
717 case LIST_ARTISTS:
718 screen_jump(browser.lw, screen_artist_lw_callback,
719 paint_artist_callback, artist_list);
720 artist_repaint();
721 return true;
723 case LIST_ALBUMS:
724 screen_jump(browser.lw, screen_artist_lw_callback,
725 paint_album_callback, album_list);
726 artist_repaint();
727 return true;
729 case LIST_SONGS:
730 /* handled by browser_cmd() */
731 break;
732 }
734 break;
736 default:
737 break;
738 }
740 if (screen_artist_lw_cmd(c, cmd)) {
741 if (screen_is_visible(&screen_artist))
742 artist_repaint();
743 return true;
744 }
746 return false;
747 }
749 const struct screen_functions screen_artist = {
750 .init = screen_artist_init,
751 .exit = screen_artist_quit,
752 .open = screen_artist_open,
753 .resize = screen_artist_resize,
754 .paint = screen_artist_paint,
755 .update = screen_artist_update,
756 .cmd = screen_artist_cmd,
757 .get_title = screen_artist_get_title,
758 };