1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2010 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_artist.h"
21 #include "screen_interface.h"
22 #include "screen_message.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;
52 char *key1, *key2;
53 int n;
55 key1 = g_utf8_collate_key(*t1,-1);
56 key2 = g_utf8_collate_key(*t2,-1);
57 n = strcmp(key1,key2);
58 g_free(key1);
59 g_free(key2);
60 return n;
61 }
63 /* list_window callback */
64 static const char *
65 screen_artist_lw_callback(unsigned idx, void *data)
66 {
67 GPtrArray *list = data;
68 static char buf[BUFSIZE];
69 char *str, *str_utf8;
71 if (mode == LIST_ALBUMS) {
72 if (idx == 0)
73 return "..";
74 else if (idx == list->len + 1)
75 return _("All tracks");
77 --idx;
78 }
80 assert(idx < list->len);
82 str_utf8 = g_ptr_array_index(list, idx);
83 assert(str_utf8 != NULL);
85 str = utf8_to_locale(str_utf8);
86 g_strlcpy(buf, str, sizeof(buf));
87 g_free(str);
89 return buf;
90 }
92 static void
93 screen_artist_paint(void);
95 static void
96 artist_repaint(void)
97 {
98 screen_artist_paint();
99 wrefresh(browser.lw->w);
100 }
102 static void
103 string_array_free(GPtrArray *array)
104 {
105 unsigned i;
107 for (i = 0; i < array->len; ++i) {
108 char *value = g_ptr_array_index(array, i);
109 g_free(value);
110 }
112 g_ptr_array_free(array, TRUE);
113 }
115 static void
116 free_lists(void)
117 {
118 if (artist_list != NULL) {
119 string_array_free(artist_list);
120 artist_list = NULL;
121 }
123 if (album_list != NULL) {
124 string_array_free(album_list);
125 album_list = NULL;
126 }
128 if (browser.filelist) {
129 filelist_free(browser.filelist);
130 browser.filelist = NULL;
131 }
132 }
134 static void
135 recv_tag_values(struct mpd_connection *connection, enum mpd_tag_type tag,
136 GPtrArray *list)
137 {
138 struct mpd_pair *pair;
140 while ((pair = mpd_recv_pair_tag(connection, tag)) != NULL) {
141 g_ptr_array_add(list, g_strdup(pair->value));
142 mpd_return_pair(connection, pair);
143 }
144 }
146 static void
147 load_artist_list(struct mpdclient *c)
148 {
149 struct mpd_connection *connection = mpdclient_get_connection(c);
151 assert(mode == LIST_ARTISTS);
152 assert(artist == NULL);
153 assert(album == NULL);
154 assert(artist_list == NULL);
155 assert(album_list == NULL);
156 assert(browser.filelist == NULL);
158 artist_list = g_ptr_array_new();
160 if (connection != NULL) {
161 mpd_search_db_tags(connection, MPD_TAG_ARTIST);
162 mpd_search_commit(connection);
163 recv_tag_values(connection, MPD_TAG_ARTIST, artist_list);
165 if (!mpd_response_finish(connection))
166 mpdclient_handle_error(c);
167 }
169 /* sort list */
170 g_ptr_array_sort(artist_list, compare_utf8);
171 list_window_set_length(browser.lw, artist_list->len);
172 }
174 static void
175 load_album_list(struct mpdclient *c)
176 {
177 struct mpd_connection *connection = mpdclient_get_connection(c);
179 assert(mode == LIST_ALBUMS);
180 assert(artist != NULL);
181 assert(album == NULL);
182 assert(album_list == NULL);
183 assert(browser.filelist == NULL);
185 album_list = g_ptr_array_new();
187 if (connection != NULL) {
188 mpd_search_db_tags(connection, MPD_TAG_ALBUM);
189 mpd_search_add_tag_constraint(connection,
190 MPD_OPERATOR_DEFAULT,
191 MPD_TAG_ARTIST, artist);
192 mpd_search_commit(connection);
194 recv_tag_values(connection, MPD_TAG_ALBUM, album_list);
196 if (!mpd_response_finish(connection))
197 mpdclient_handle_error(c);
198 }
200 /* sort list */
201 g_ptr_array_sort(album_list, compare_utf8);
203 list_window_set_length(browser.lw, album_list->len + 2);
204 }
206 static void
207 load_song_list(struct mpdclient *c)
208 {
209 struct mpd_connection *connection = mpdclient_get_connection(c);
211 assert(mode == LIST_SONGS);
212 assert(artist != NULL);
213 assert(album != NULL);
214 assert(browser.filelist == NULL);
216 browser.filelist = filelist_new();
217 /* add a dummy entry for ".." */
218 filelist_append(browser.filelist, NULL);
220 if (connection != NULL) {
221 mpd_search_db_songs(connection, true);
222 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
223 MPD_TAG_ARTIST, artist);
224 if (album != ALL_TRACKS)
225 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
226 MPD_TAG_ALBUM, album);
227 mpd_search_commit(connection);
229 filelist_recv(browser.filelist, connection);
231 if (!mpd_response_finish(connection))
232 mpdclient_handle_error(c);
233 }
235 /* fix highlights */
236 screen_browser_sync_highlights(browser.filelist, &c->playlist);
237 list_window_set_length(browser.lw, filelist_length(browser.filelist));
238 }
240 static void
241 free_state(void)
242 {
243 g_free(artist);
244 if (album != ALL_TRACKS)
245 g_free(album);
246 artist = NULL;
247 album = NULL;
249 free_lists();
250 }
252 static void
253 open_artist_list(struct mpdclient *c)
254 {
255 free_state();
257 mode = LIST_ARTISTS;
258 load_artist_list(c);
259 }
261 static void
262 open_album_list(struct mpdclient *c, char *_artist)
263 {
264 assert(_artist != NULL);
266 free_state();
268 mode = LIST_ALBUMS;
269 artist = _artist;
270 load_album_list(c);
271 }
273 static void
274 open_song_list(struct mpdclient *c, char *_artist, char *_album)
275 {
276 assert(_artist != NULL);
277 assert(_album != NULL);
279 free_state();
281 mode = LIST_SONGS;
282 artist = _artist;
283 album = _album;
284 load_song_list(c);
285 }
287 static void
288 reload_lists(struct mpdclient *c)
289 {
290 free_lists();
292 switch (mode) {
293 case LIST_ARTISTS:
294 load_artist_list(c);
295 break;
297 case LIST_ALBUMS:
298 load_album_list(c);
299 break;
301 case LIST_SONGS:
302 load_song_list(c);
303 break;
304 }
305 }
307 static void
308 screen_artist_init(WINDOW *w, int cols, int rows)
309 {
310 browser.lw = list_window_init(w, cols, rows);
311 artist = NULL;
312 album = NULL;
313 }
315 static void
316 screen_artist_quit(void)
317 {
318 free_state();
319 list_window_free(browser.lw);
320 }
322 static void
323 screen_artist_open(struct mpdclient *c)
324 {
325 if (artist_list == NULL && album_list == NULL &&
326 browser.filelist == NULL)
327 reload_lists(c);
328 }
330 static void
331 screen_artist_resize(int cols, int rows)
332 {
333 list_window_resize(browser.lw, cols, rows);
334 }
336 /**
337 * Paint one item in the artist list.
338 */
339 static void
340 paint_artist_callback(WINDOW *w, unsigned i,
341 G_GNUC_UNUSED unsigned y, unsigned width,
342 bool selected, void *data)
343 {
344 GPtrArray *list = data;
345 char *p = utf8_to_locale(g_ptr_array_index(list, i));
347 screen_browser_paint_directory(w, width, selected, p);
348 g_free(p);
349 }
351 /**
352 * Paint one item in the album list. There are two virtual items
353 * inserted: at the beginning, there's the special item ".." to go to
354 * the parent directory, and at the end, there's the item "All tracks"
355 * to view the tracks of all albums.
356 */
357 static void
358 paint_album_callback(WINDOW *w, unsigned i,
359 G_GNUC_UNUSED unsigned y, unsigned width,
360 bool selected, void *data)
361 {
362 GPtrArray *list = data;
363 const char *p;
364 char *q = NULL;
366 if (i == 0)
367 p = "..";
368 else if (i == list->len + 1)
369 p = _("All tracks");
370 else
371 p = q = utf8_to_locale(g_ptr_array_index(list, i - 1));
373 screen_browser_paint_directory(w, width, selected, p);
374 g_free(q);
375 }
377 static void
378 screen_artist_paint(void)
379 {
380 if (browser.filelist) {
381 screen_browser_paint(&browser);
382 } else if (album_list != NULL)
383 list_window_paint2(browser.lw,
384 paint_album_callback, album_list);
385 else if (artist_list != NULL)
386 list_window_paint2(browser.lw,
387 paint_artist_callback, artist_list);
388 else {
389 wmove(browser.lw->w, 0, 0);
390 wclrtobot(browser.lw->w);
391 }
392 }
394 static const char *
395 screen_artist_get_title(char *str, size_t size)
396 {
397 char *s1, *s2;
399 switch(mode) {
400 case LIST_ARTISTS:
401 g_snprintf(str, size, _("All artists"));
402 break;
404 case LIST_ALBUMS:
405 s1 = utf8_to_locale(artist);
406 g_snprintf(str, size, _("Albums of artist: %s"), s1);
407 g_free(s1);
408 break;
410 case LIST_SONGS:
411 s1 = utf8_to_locale(artist);
413 if (album == ALL_TRACKS)
414 g_snprintf(str, size,
415 _("All tracks of artist: %s"), s1);
416 else if (*album != 0) {
417 s2 = utf8_to_locale(album);
418 g_snprintf(str, size, _("Album: %s - %s"), s1, s2);
419 g_free(s2);
420 } else
421 g_snprintf(str, size,
422 _("Tracks of no album of artist: %s"), s1);
423 g_free(s1);
424 break;
425 }
427 return str;
428 }
430 static void
431 screen_artist_update(struct mpdclient *c)
432 {
433 if (browser.filelist == NULL)
434 return;
436 if (c->events & MPD_IDLE_DATABASE)
437 /* the db has changed -> update the list */
438 reload_lists(c);
440 if (c->events & (MPD_IDLE_DATABASE | MPD_IDLE_QUEUE))
441 screen_browser_sync_highlights(browser.filelist, &c->playlist);
443 if (c->events & (MPD_IDLE_DATABASE
444 #ifndef NCMPC_MINI
445 | MPD_IDLE_QUEUE
446 #endif
447 ))
448 artist_repaint();
449 }
451 /* _artist is actually only used in the ALBUM case to distinguish albums with
452 the same name from different artists. */
453 static void
454 add_query(struct mpdclient *c, enum mpd_tag_type table, const char *_filter,
455 const char *_artist)
456 {
457 struct mpd_connection *connection = mpdclient_get_connection(c);
458 char *str;
459 struct filelist *addlist;
461 assert(_filter != NULL);
463 if (connection == NULL)
464 return;
466 str = utf8_to_locale(_filter);
467 if (table == MPD_TAG_ALBUM)
468 screen_status_printf(_("Adding album %s..."), str);
469 else
470 screen_status_printf(_("Adding %s..."), str);
471 g_free(str);
473 mpd_search_db_songs(connection, true);
474 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
475 table, _filter);
476 if (table == MPD_TAG_ALBUM)
477 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
478 MPD_TAG_ARTIST, _artist);
479 mpd_search_commit(connection);
481 addlist = filelist_new_recv(connection);
483 if (mpd_response_finish(connection))
484 mpdclient_filelist_add_all(c, addlist);
485 else
486 mpdclient_handle_error(c);
488 filelist_free(addlist);
489 }
491 static int
492 screen_artist_lw_cmd(struct mpdclient *c, command_t cmd)
493 {
494 switch (mode) {
495 case LIST_ARTISTS:
496 case LIST_ALBUMS:
497 return list_window_cmd(browser.lw, cmd);
499 case LIST_SONGS:
500 return browser_cmd(&browser, c, cmd);
501 }
503 assert(0);
504 return 0;
505 }
507 static int
508 string_array_find(GPtrArray *array, const char *value)
509 {
510 guint i;
512 for (i = 0; i < array->len; ++i)
513 if (strcmp((const char*)g_ptr_array_index(array, i),
514 value) == 0)
515 return i;
517 return -1;
518 }
520 static bool
521 screen_artist_cmd(struct mpdclient *c, command_t cmd)
522 {
523 struct list_window_range range;
524 char *selected;
525 char *old;
526 char *old_ptr;
527 int idx;
529 switch(cmd) {
530 case CMD_PLAY:
531 switch (mode) {
532 case LIST_ARTISTS:
533 if (browser.lw->selected >= artist_list->len)
534 return true;
536 selected = g_ptr_array_index(artist_list,
537 browser.lw->selected);
538 open_album_list(c, g_strdup(selected));
539 list_window_reset(browser.lw);
541 artist_repaint();
542 return true;
544 case LIST_ALBUMS:
545 if (browser.lw->selected == 0) {
546 /* handle ".." */
547 old = g_strdup(artist);
549 open_artist_list(c);
550 list_window_reset(browser.lw);
551 /* restore previous list window state */
552 idx = string_array_find(artist_list, old);
553 g_free(old);
555 if (idx >= 0) {
556 list_window_set_cursor(browser.lw, idx);
557 list_window_center(browser.lw, idx);
558 }
559 } else if (browser.lw->selected == album_list->len + 1) {
560 /* handle "show all" */
561 open_song_list(c, g_strdup(artist), ALL_TRACKS);
562 list_window_reset(browser.lw);
563 } else {
564 /* select album */
565 selected = g_ptr_array_index(album_list,
566 browser.lw->selected - 1);
567 open_song_list(c, g_strdup(artist), g_strdup(selected));
568 list_window_reset(browser.lw);
569 }
571 artist_repaint();
572 return true;
574 case LIST_SONGS:
575 if (browser.lw->selected == 0) {
576 /* handle ".." */
577 old = g_strdup(album);
578 old_ptr = album;
580 open_album_list(c, g_strdup(artist));
581 list_window_reset(browser.lw);
582 /* restore previous list window state */
583 idx = old_ptr == ALL_TRACKS
584 ? (int)album_list->len
585 : string_array_find(album_list, old);
586 g_free(old);
588 if (idx >= 0) {
589 ++idx;
590 list_window_set_cursor(browser.lw, idx);
591 list_window_center(browser.lw, idx);
592 }
594 artist_repaint();
595 return true;
596 }
597 break;
598 }
599 break;
601 /* FIXME? CMD_GO_* handling duplicates code from CMD_PLAY */
603 case CMD_GO_PARENT_DIRECTORY:
604 switch (mode) {
605 case LIST_ARTISTS:
606 break;
608 case LIST_ALBUMS:
609 old = g_strdup(artist);
611 open_artist_list(c);
612 list_window_reset(browser.lw);
613 /* restore previous list window state */
614 idx = string_array_find(artist_list, old);
615 g_free(old);
617 if (idx >= 0) {
618 list_window_set_cursor(browser.lw, idx);
619 list_window_center(browser.lw, idx);
620 }
621 break;
623 case LIST_SONGS:
624 old = g_strdup(album);
625 old_ptr = album;
627 open_album_list(c, g_strdup(artist));
628 list_window_reset(browser.lw);
629 /* restore previous list window state */
630 idx = old_ptr == ALL_TRACKS
631 ? (int)album_list->len
632 : string_array_find(album_list, old);
633 g_free(old);
635 if (idx >= 0) {
636 ++idx;
637 list_window_set_cursor(browser.lw, idx);
638 list_window_center(browser.lw, idx);
639 }
640 break;
641 }
643 artist_repaint();
644 break;
646 case CMD_GO_ROOT_DIRECTORY:
647 switch (mode) {
648 case LIST_ARTISTS:
649 break;
651 case LIST_ALBUMS:
652 case LIST_SONGS:
653 open_artist_list(c);
654 list_window_reset(browser.lw);
655 /* restore first list window state (pop while returning true) */
656 /* XXX */
657 break;
658 }
660 artist_repaint();
661 break;
663 case CMD_SELECT:
664 case CMD_ADD:
665 switch(mode) {
666 case LIST_ARTISTS:
667 if (browser.lw->selected >= artist_list->len)
668 return true;
670 list_window_get_range(browser.lw, &range);
671 for (unsigned i = range.start; i < range.end; ++i) {
672 selected = g_ptr_array_index(artist_list, i);
673 add_query(c, MPD_TAG_ARTIST, selected, NULL);
674 cmd = CMD_LIST_NEXT; /* continue and select next item... */
675 }
676 break;
678 case LIST_ALBUMS:
679 list_window_get_range(browser.lw, &range);
680 for (unsigned i = range.start; i < range.end; ++i) {
681 if(i == album_list->len + 1)
682 add_query(c, MPD_TAG_ARTIST, artist, NULL);
683 else if (i > 0)
684 {
685 selected = g_ptr_array_index(album_list,
686 browser.lw->selected - 1);
687 add_query(c, MPD_TAG_ALBUM, selected, artist);
688 cmd = CMD_LIST_NEXT; /* continue and select next item... */
689 }
690 }
691 break;
693 case LIST_SONGS:
694 /* handled by browser_cmd() */
695 break;
696 }
697 break;
699 /* continue and update... */
700 case CMD_SCREEN_UPDATE:
701 reload_lists(c);
702 return false;
704 case CMD_LIST_FIND:
705 case CMD_LIST_RFIND:
706 case CMD_LIST_FIND_NEXT:
707 case CMD_LIST_RFIND_NEXT:
708 switch (mode) {
709 case LIST_ARTISTS:
710 screen_find(browser.lw, cmd,
711 screen_artist_lw_callback, artist_list);
712 artist_repaint();
713 return true;
715 case LIST_ALBUMS:
716 screen_find(browser.lw, cmd,
717 screen_artist_lw_callback, album_list);
718 artist_repaint();
719 return true;
721 case LIST_SONGS:
722 /* handled by browser_cmd() */
723 break;
724 }
725 case CMD_LIST_JUMP:
726 switch (mode) {
727 case LIST_ARTISTS:
728 screen_jump(browser.lw, screen_artist_lw_callback,
729 paint_artist_callback, artist_list);
730 artist_repaint();
731 return true;
733 case LIST_ALBUMS:
734 screen_jump(browser.lw, screen_artist_lw_callback,
735 paint_album_callback, album_list);
736 artist_repaint();
737 return true;
739 case LIST_SONGS:
740 /* handled by browser_cmd() */
741 break;
742 }
744 break;
746 default:
747 break;
748 }
750 if (screen_artist_lw_cmd(c, cmd)) {
751 if (screen_is_visible(&screen_artist))
752 artist_repaint();
753 return true;
754 }
756 return false;
757 }
759 const struct screen_functions screen_artist = {
760 .init = screen_artist_init,
761 .exit = screen_artist_quit,
762 .open = screen_artist_open,
763 .resize = screen_artist_resize,
764 .paint = screen_artist_paint,
765 .update = screen_artist_update,
766 .cmd = screen_artist_cmd,
767 .get_title = screen_artist_get_title,
768 };