1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2017 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"
31 #include "options.h"
33 #include <assert.h>
34 #include <string.h>
35 #include <glib.h>
37 #define BUFSIZE 1024
39 typedef enum { LIST_ARTISTS, LIST_ALBUMS, LIST_SONGS } artist_mode_t;
41 static artist_mode_t mode = LIST_ARTISTS;
42 static GPtrArray *artist_list, *album_list;
43 static char *artist = NULL;
44 static char *album = NULL;
45 static char ALL_TRACKS[] = "";
47 static struct screen_browser browser;
49 static gint
50 compare_utf8(gconstpointer s1, gconstpointer s2)
51 {
52 const char *const*t1 = s1, *const*t2 = s2;
54 char *key1 = g_utf8_collate_key(*t1,-1);
55 char *key2 = g_utf8_collate_key(*t2,-1);
56 int n = strcmp(key1,key2);
57 g_free(key1);
58 g_free(key2);
59 return n;
60 }
62 /* list_window callback */
63 static const char *
64 screen_artist_lw_callback(unsigned idx, void *data)
65 {
66 GPtrArray *list = data;
68 if (mode == LIST_ALBUMS) {
69 if (idx == 0)
70 return "..";
71 else if (idx == list->len + 1)
72 return _("All tracks");
74 --idx;
75 }
77 assert(idx < list->len);
79 char *str_utf8 = g_ptr_array_index(list, idx);
80 assert(str_utf8 != NULL);
82 char *str = utf8_to_locale(str_utf8);
84 static char buf[BUFSIZE];
85 g_strlcpy(buf, str, sizeof(buf));
86 g_free(str);
88 return buf;
89 }
91 static void
92 screen_artist_paint(void);
94 static void
95 artist_repaint(void)
96 {
97 screen_artist_paint();
98 wrefresh(browser.lw->w);
99 }
101 static void
102 string_array_free(GPtrArray *array)
103 {
104 for (unsigned i = 0; i < array->len; ++i) {
105 char *value = g_ptr_array_index(array, i);
106 g_free(value);
107 }
109 g_ptr_array_free(array, TRUE);
110 }
112 static void
113 free_lists(void)
114 {
115 if (artist_list != NULL) {
116 string_array_free(artist_list);
117 artist_list = NULL;
118 }
120 if (album_list != NULL) {
121 string_array_free(album_list);
122 album_list = NULL;
123 }
125 if (browser.filelist) {
126 filelist_free(browser.filelist);
127 browser.filelist = NULL;
128 }
129 }
131 static void
132 recv_tag_values(struct mpd_connection *connection, enum mpd_tag_type tag,
133 GPtrArray *list)
134 {
135 struct mpd_pair *pair;
137 while ((pair = mpd_recv_pair_tag(connection, tag)) != NULL) {
138 g_ptr_array_add(list, g_strdup(pair->value));
139 mpd_return_pair(connection, pair);
140 }
141 }
143 static void
144 load_artist_list(struct mpdclient *c)
145 {
146 struct mpd_connection *connection = mpdclient_get_connection(c);
148 assert(mode == LIST_ARTISTS);
149 assert(artist == NULL);
150 assert(album == NULL);
151 assert(artist_list == NULL);
152 assert(album_list == NULL);
153 assert(browser.filelist == NULL);
155 artist_list = g_ptr_array_new();
157 if (connection != NULL) {
158 mpd_search_db_tags(connection, MPD_TAG_ARTIST);
159 mpd_search_commit(connection);
160 recv_tag_values(connection, MPD_TAG_ARTIST, artist_list);
162 mpdclient_finish_command(c);
163 }
165 /* sort list */
166 g_ptr_array_sort(artist_list, compare_utf8);
167 list_window_set_length(browser.lw, artist_list->len);
168 }
170 static void
171 load_album_list(struct mpdclient *c)
172 {
173 struct mpd_connection *connection = mpdclient_get_connection(c);
175 assert(mode == LIST_ALBUMS);
176 assert(artist != NULL);
177 assert(album == NULL);
178 assert(album_list == NULL);
179 assert(browser.filelist == NULL);
181 album_list = g_ptr_array_new();
183 if (connection != NULL) {
184 mpd_search_db_tags(connection, MPD_TAG_ALBUM);
185 mpd_search_add_tag_constraint(connection,
186 MPD_OPERATOR_DEFAULT,
187 MPD_TAG_ARTIST, artist);
188 mpd_search_commit(connection);
190 recv_tag_values(connection, MPD_TAG_ALBUM, album_list);
192 mpdclient_finish_command(c);
193 }
195 /* sort list */
196 g_ptr_array_sort(album_list, compare_utf8);
198 list_window_set_length(browser.lw, album_list->len + 2);
199 }
201 static void
202 load_song_list(struct mpdclient *c)
203 {
204 struct mpd_connection *connection = mpdclient_get_connection(c);
206 assert(mode == LIST_SONGS);
207 assert(artist != NULL);
208 assert(album != NULL);
209 assert(browser.filelist == NULL);
211 browser.filelist = filelist_new();
212 /* add a dummy entry for ".." */
213 filelist_append(browser.filelist, NULL);
215 if (connection != NULL) {
216 mpd_search_db_songs(connection, true);
217 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
218 MPD_TAG_ARTIST, artist);
219 if (album != ALL_TRACKS)
220 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
221 MPD_TAG_ALBUM, album);
222 mpd_search_commit(connection);
224 filelist_recv(browser.filelist, connection);
226 mpdclient_finish_command(c);
227 }
229 /* fix highlights */
230 screen_browser_sync_highlights(browser.filelist, &c->playlist);
231 list_window_set_length(browser.lw, filelist_length(browser.filelist));
232 }
234 static void
235 free_state(void)
236 {
237 g_free(artist);
238 if (album != ALL_TRACKS)
239 g_free(album);
240 artist = NULL;
241 album = NULL;
243 free_lists();
244 }
246 static void
247 open_artist_list(struct mpdclient *c)
248 {
249 free_state();
251 mode = LIST_ARTISTS;
252 load_artist_list(c);
253 }
255 static void
256 open_album_list(struct mpdclient *c, char *_artist)
257 {
258 assert(_artist != NULL);
260 free_state();
262 mode = LIST_ALBUMS;
263 artist = _artist;
264 load_album_list(c);
265 }
267 static void
268 open_song_list(struct mpdclient *c, char *_artist, char *_album)
269 {
270 assert(_artist != NULL);
271 assert(_album != NULL);
273 free_state();
275 mode = LIST_SONGS;
276 artist = _artist;
277 album = _album;
278 load_song_list(c);
279 }
281 static void
282 reload_lists(struct mpdclient *c)
283 {
284 free_lists();
286 switch (mode) {
287 case LIST_ARTISTS:
288 load_artist_list(c);
289 break;
291 case LIST_ALBUMS:
292 load_album_list(c);
293 break;
295 case LIST_SONGS:
296 load_song_list(c);
297 break;
298 }
299 }
301 static void
302 screen_artist_init(WINDOW *w, int cols, int rows)
303 {
304 browser.lw = list_window_init(w, cols, rows);
305 browser.song_format = options.list_format;
306 artist = NULL;
307 album = NULL;
308 }
310 static void
311 screen_artist_quit(void)
312 {
313 free_state();
314 list_window_free(browser.lw);
315 }
317 static void
318 screen_artist_open(struct mpdclient *c)
319 {
320 if (artist_list == NULL && album_list == NULL &&
321 browser.filelist == NULL)
322 reload_lists(c);
323 }
325 static void
326 screen_artist_resize(int cols, int rows)
327 {
328 list_window_resize(browser.lw, cols, rows);
329 }
331 /**
332 * Paint one item in the artist list.
333 */
334 static void
335 paint_artist_callback(WINDOW *w, unsigned i,
336 gcc_unused unsigned y, unsigned width,
337 bool selected, const void *data)
338 {
339 const GPtrArray *list = data;
340 char *p = utf8_to_locale(g_ptr_array_index(list, i));
342 screen_browser_paint_directory(w, width, selected, p);
343 g_free(p);
344 }
346 /**
347 * Paint one item in the album list. There are two virtual items
348 * inserted: at the beginning, there's the special item ".." to go to
349 * the parent directory, and at the end, there's the item "All tracks"
350 * to view the tracks of all albums.
351 */
352 static void
353 paint_album_callback(WINDOW *w, unsigned i,
354 gcc_unused unsigned y, unsigned width,
355 bool selected, const void *data)
356 {
357 const GPtrArray *list = data;
358 const char *p;
359 char *q = NULL;
361 if (i == 0)
362 p = "..";
363 else if (i == list->len + 1)
364 p = _("All tracks");
365 else
366 p = q = utf8_to_locale(g_ptr_array_index(list, i - 1));
368 screen_browser_paint_directory(w, width, selected, p);
369 g_free(q);
370 }
372 static void
373 screen_artist_paint(void)
374 {
375 if (browser.filelist) {
376 screen_browser_paint(&browser);
377 } else if (album_list != NULL)
378 list_window_paint2(browser.lw,
379 paint_album_callback, album_list);
380 else if (artist_list != NULL)
381 list_window_paint2(browser.lw,
382 paint_artist_callback, artist_list);
383 else {
384 wmove(browser.lw->w, 0, 0);
385 wclrtobot(browser.lw->w);
386 }
387 }
389 static const char *
390 screen_artist_get_title(char *str, size_t size)
391 {
392 switch(mode) {
393 char *s1, *s2;
395 case LIST_ARTISTS:
396 g_snprintf(str, size, _("All artists"));
397 break;
399 case LIST_ALBUMS:
400 s1 = utf8_to_locale(artist);
401 g_snprintf(str, size, _("Albums of artist: %s"), s1);
402 g_free(s1);
403 break;
405 case LIST_SONGS:
406 s1 = utf8_to_locale(artist);
408 if (album == ALL_TRACKS)
409 g_snprintf(str, size,
410 _("All tracks of artist: %s"), s1);
411 else if (*album != 0) {
412 s2 = utf8_to_locale(album);
413 g_snprintf(str, size, _("Album: %s - %s"), s1, s2);
414 g_free(s2);
415 } else
416 g_snprintf(str, size,
417 _("Tracks of no album of artist: %s"), s1);
418 g_free(s1);
419 break;
420 }
422 return str;
423 }
425 static void
426 screen_artist_update(struct mpdclient *c)
427 {
428 if (browser.filelist == NULL)
429 return;
431 if (c->events & MPD_IDLE_DATABASE)
432 /* the db has changed -> update the list */
433 reload_lists(c);
435 if (c->events & (MPD_IDLE_DATABASE | MPD_IDLE_QUEUE))
436 screen_browser_sync_highlights(browser.filelist, &c->playlist);
438 if (c->events & (MPD_IDLE_DATABASE
439 #ifndef NCMPC_MINI
440 | MPD_IDLE_QUEUE
441 #endif
442 ))
443 artist_repaint();
444 }
446 /* add_query - Add all songs satisfying specified criteria.
447 _artist is actually only used in the ALBUM case to distinguish albums with
448 the same name from different artists. */
449 static void
450 add_query(struct mpdclient *c, enum mpd_tag_type table, const char *_filter,
451 const char *_artist)
452 {
453 struct mpd_connection *connection = mpdclient_get_connection(c);
455 assert(_filter != NULL);
457 if (connection == NULL)
458 return;
460 char *str = utf8_to_locale(_filter);
461 if (table == MPD_TAG_ALBUM)
462 screen_status_printf(_("Adding album %s..."), str);
463 else
464 screen_status_printf(_("Adding %s..."), str);
465 g_free(str);
467 mpd_search_db_songs(connection, true);
468 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
469 table, _filter);
470 if (table == MPD_TAG_ALBUM)
471 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
472 MPD_TAG_ARTIST, _artist);
473 mpd_search_commit(connection);
475 struct filelist *addlist = filelist_new_recv(connection);
477 if (mpdclient_finish_command(c))
478 mpdclient_filelist_add_all(c, addlist);
480 filelist_free(addlist);
481 }
483 static int
484 screen_artist_lw_cmd(struct mpdclient *c, command_t cmd)
485 {
486 switch (mode) {
487 case LIST_ARTISTS:
488 case LIST_ALBUMS:
489 return list_window_cmd(browser.lw, cmd);
491 case LIST_SONGS:
492 return browser_cmd(&browser, c, cmd);
493 }
495 assert(0);
496 return 0;
497 }
499 static int
500 string_array_find(GPtrArray *array, const char *value)
501 {
502 guint i;
504 for (i = 0; i < array->len; ++i)
505 if (strcmp((const char*)g_ptr_array_index(array, i),
506 value) == 0)
507 return i;
509 return -1;
510 }
512 static bool
513 screen_artist_cmd(struct mpdclient *c, command_t cmd)
514 {
515 switch(cmd) {
516 struct list_window_range range;
517 char *selected;
518 char *old;
519 char *old_ptr;
520 int idx;
522 case CMD_PLAY:
523 switch (mode) {
524 case LIST_ARTISTS:
525 if (browser.lw->selected >= artist_list->len)
526 return true;
528 selected = g_ptr_array_index(artist_list,
529 browser.lw->selected);
530 open_album_list(c, g_strdup(selected));
531 list_window_reset(browser.lw);
533 artist_repaint();
534 return true;
536 case LIST_ALBUMS:
537 if (browser.lw->selected == 0) {
538 /* handle ".." */
539 old = g_strdup(artist);
541 open_artist_list(c);
542 list_window_reset(browser.lw);
543 /* restore previous list window state */
544 idx = string_array_find(artist_list, old);
545 g_free(old);
547 if (idx >= 0) {
548 list_window_set_cursor(browser.lw, idx);
549 list_window_center(browser.lw, idx);
550 }
551 } else if (browser.lw->selected == album_list->len + 1) {
552 /* handle "show all" */
553 open_song_list(c, g_strdup(artist), ALL_TRACKS);
554 list_window_reset(browser.lw);
555 } else {
556 /* select album */
557 selected = g_ptr_array_index(album_list,
558 browser.lw->selected - 1);
559 open_song_list(c, g_strdup(artist), g_strdup(selected));
560 list_window_reset(browser.lw);
561 }
563 artist_repaint();
564 return true;
566 case LIST_SONGS:
567 if (browser.lw->selected == 0) {
568 /* handle ".." */
569 old = g_strdup(album);
570 old_ptr = album;
572 open_album_list(c, g_strdup(artist));
573 list_window_reset(browser.lw);
574 /* restore previous list window state */
575 idx = old_ptr == ALL_TRACKS
576 ? (int)album_list->len
577 : string_array_find(album_list, old);
578 g_free(old);
580 if (idx >= 0) {
581 ++idx;
582 list_window_set_cursor(browser.lw, idx);
583 list_window_center(browser.lw, idx);
584 }
586 artist_repaint();
587 return true;
588 }
589 break;
590 }
591 break;
593 /* FIXME? CMD_GO_* handling duplicates code from CMD_PLAY */
595 case CMD_GO_PARENT_DIRECTORY:
596 switch (mode) {
597 case LIST_ARTISTS:
598 break;
600 case LIST_ALBUMS:
601 old = g_strdup(artist);
603 open_artist_list(c);
604 list_window_reset(browser.lw);
605 /* restore previous list window state */
606 idx = string_array_find(artist_list, old);
607 g_free(old);
609 if (idx >= 0) {
610 list_window_set_cursor(browser.lw, idx);
611 list_window_center(browser.lw, idx);
612 }
613 break;
615 case LIST_SONGS:
616 old = g_strdup(album);
617 old_ptr = album;
619 open_album_list(c, g_strdup(artist));
620 list_window_reset(browser.lw);
621 /* restore previous list window state */
622 idx = old_ptr == ALL_TRACKS
623 ? (int)album_list->len
624 : string_array_find(album_list, old);
625 g_free(old);
627 if (idx >= 0) {
628 ++idx;
629 list_window_set_cursor(browser.lw, idx);
630 list_window_center(browser.lw, idx);
631 }
632 break;
633 }
635 artist_repaint();
636 break;
638 case CMD_GO_ROOT_DIRECTORY:
639 switch (mode) {
640 case LIST_ARTISTS:
641 break;
643 case LIST_ALBUMS:
644 case LIST_SONGS:
645 open_artist_list(c);
646 list_window_reset(browser.lw);
647 /* restore first list window state (pop while returning true) */
648 /* XXX */
649 break;
650 }
652 artist_repaint();
653 break;
655 case CMD_SELECT:
656 case CMD_ADD:
657 switch(mode) {
658 case LIST_ARTISTS:
659 if (browser.lw->selected >= artist_list->len)
660 return true;
662 list_window_get_range(browser.lw, &range);
663 for (unsigned i = range.start; i < range.end; ++i) {
664 selected = g_ptr_array_index(artist_list, i);
665 add_query(c, MPD_TAG_ARTIST, selected, NULL);
666 cmd = CMD_LIST_NEXT; /* continue and select next item... */
667 }
668 break;
670 case LIST_ALBUMS:
671 list_window_get_range(browser.lw, &range);
672 for (unsigned i = range.start; i < range.end; ++i) {
673 if(i == album_list->len + 1)
674 add_query(c, MPD_TAG_ARTIST, artist, NULL);
675 else if (i > 0)
676 {
677 selected = g_ptr_array_index(album_list,
678 browser.lw->selected - 1);
679 add_query(c, MPD_TAG_ALBUM, selected, artist);
680 cmd = CMD_LIST_NEXT; /* continue and select next item... */
681 }
682 }
683 break;
685 case LIST_SONGS:
686 /* handled by browser_cmd() */
687 break;
688 }
689 break;
691 /* continue and update... */
692 case CMD_SCREEN_UPDATE:
693 reload_lists(c);
694 return false;
696 case CMD_LIST_FIND:
697 case CMD_LIST_RFIND:
698 case CMD_LIST_FIND_NEXT:
699 case CMD_LIST_RFIND_NEXT:
700 switch (mode) {
701 case LIST_ARTISTS:
702 screen_find(browser.lw, cmd,
703 screen_artist_lw_callback, artist_list);
704 artist_repaint();
705 return true;
707 case LIST_ALBUMS:
708 screen_find(browser.lw, cmd,
709 screen_artist_lw_callback, album_list);
710 artist_repaint();
711 return true;
713 case LIST_SONGS:
714 /* handled by browser_cmd() */
715 break;
716 }
717 case CMD_LIST_JUMP:
718 switch (mode) {
719 case LIST_ARTISTS:
720 screen_jump(browser.lw,
721 screen_artist_lw_callback, artist_list,
722 paint_artist_callback, artist_list);
723 artist_repaint();
724 return true;
726 case LIST_ALBUMS:
727 screen_jump(browser.lw,
728 screen_artist_lw_callback, album_list,
729 paint_album_callback, album_list);
730 artist_repaint();
731 return true;
733 case LIST_SONGS:
734 /* handled by browser_cmd() */
735 break;
736 }
738 break;
740 default:
741 break;
742 }
744 if (screen_artist_lw_cmd(c, cmd)) {
745 if (screen_is_visible(&screen_artist))
746 artist_repaint();
747 return true;
748 }
750 return false;
751 }
753 const struct screen_functions screen_artist = {
754 .init = screen_artist_init,
755 .exit = screen_artist_quit,
756 .open = screen_artist_open,
757 .resize = screen_artist_resize,
758 .paint = screen_artist_paint,
759 .update = screen_artist_update,
760 .cmd = screen_artist_cmd,
761 .get_title = screen_artist_get_title,
762 };