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;
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 mpdclient_finish_command(c);
166 }
168 /* sort list */
169 g_ptr_array_sort(artist_list, compare_utf8);
170 list_window_set_length(browser.lw, artist_list->len);
171 }
173 static void
174 load_album_list(struct mpdclient *c)
175 {
176 struct mpd_connection *connection = mpdclient_get_connection(c);
178 assert(mode == LIST_ALBUMS);
179 assert(artist != NULL);
180 assert(album == NULL);
181 assert(album_list == NULL);
182 assert(browser.filelist == NULL);
184 album_list = g_ptr_array_new();
186 if (connection != NULL) {
187 mpd_search_db_tags(connection, MPD_TAG_ALBUM);
188 mpd_search_add_tag_constraint(connection,
189 MPD_OPERATOR_DEFAULT,
190 MPD_TAG_ARTIST, artist);
191 mpd_search_commit(connection);
193 recv_tag_values(connection, MPD_TAG_ALBUM, album_list);
195 mpdclient_finish_command(c);
196 }
198 /* sort list */
199 g_ptr_array_sort(album_list, compare_utf8);
201 list_window_set_length(browser.lw, album_list->len + 2);
202 }
204 static void
205 load_song_list(struct mpdclient *c)
206 {
207 struct mpd_connection *connection = mpdclient_get_connection(c);
209 assert(mode == LIST_SONGS);
210 assert(artist != NULL);
211 assert(album != NULL);
212 assert(browser.filelist == NULL);
214 browser.filelist = filelist_new();
215 /* add a dummy entry for ".." */
216 filelist_append(browser.filelist, NULL);
218 if (connection != NULL) {
219 mpd_search_db_songs(connection, true);
220 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
221 MPD_TAG_ARTIST, artist);
222 if (album != ALL_TRACKS)
223 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
224 MPD_TAG_ALBUM, album);
225 mpd_search_commit(connection);
227 filelist_recv(browser.filelist, connection);
229 mpdclient_finish_command(c);
230 }
232 /* fix highlights */
233 screen_browser_sync_highlights(browser.filelist, &c->playlist);
234 list_window_set_length(browser.lw, filelist_length(browser.filelist));
235 }
237 static void
238 free_state(void)
239 {
240 g_free(artist);
241 if (album != ALL_TRACKS)
242 g_free(album);
243 artist = NULL;
244 album = NULL;
246 free_lists();
247 }
249 static void
250 open_artist_list(struct mpdclient *c)
251 {
252 free_state();
254 mode = LIST_ARTISTS;
255 load_artist_list(c);
256 }
258 static void
259 open_album_list(struct mpdclient *c, char *_artist)
260 {
261 assert(_artist != NULL);
263 free_state();
265 mode = LIST_ALBUMS;
266 artist = _artist;
267 load_album_list(c);
268 }
270 static void
271 open_song_list(struct mpdclient *c, char *_artist, char *_album)
272 {
273 assert(_artist != NULL);
274 assert(_album != NULL);
276 free_state();
278 mode = LIST_SONGS;
279 artist = _artist;
280 album = _album;
281 load_song_list(c);
282 }
284 static void
285 reload_lists(struct mpdclient *c)
286 {
287 free_lists();
289 switch (mode) {
290 case LIST_ARTISTS:
291 load_artist_list(c);
292 break;
294 case LIST_ALBUMS:
295 load_album_list(c);
296 break;
298 case LIST_SONGS:
299 load_song_list(c);
300 break;
301 }
302 }
304 static void
305 screen_artist_init(WINDOW *w, int cols, int rows)
306 {
307 browser.lw = list_window_init(w, cols, rows);
308 artist = NULL;
309 album = NULL;
310 }
312 static void
313 screen_artist_quit(void)
314 {
315 free_state();
316 list_window_free(browser.lw);
317 }
319 static void
320 screen_artist_open(struct mpdclient *c)
321 {
322 if (artist_list == NULL && album_list == NULL &&
323 browser.filelist == NULL)
324 reload_lists(c);
325 }
327 static void
328 screen_artist_resize(int cols, int rows)
329 {
330 list_window_resize(browser.lw, cols, rows);
331 }
333 /**
334 * Paint one item in the artist list.
335 */
336 static void
337 paint_artist_callback(WINDOW *w, unsigned i,
338 G_GNUC_UNUSED unsigned y, unsigned width,
339 bool selected, void *data)
340 {
341 GPtrArray *list = data;
342 char *p = utf8_to_locale(g_ptr_array_index(list, i));
344 screen_browser_paint_directory(w, width, selected, p);
345 g_free(p);
346 }
348 /**
349 * Paint one item in the album list. There are two virtual items
350 * inserted: at the beginning, there's the special item ".." to go to
351 * the parent directory, and at the end, there's the item "All tracks"
352 * to view the tracks of all albums.
353 */
354 static void
355 paint_album_callback(WINDOW *w, unsigned i,
356 G_GNUC_UNUSED unsigned y, unsigned width,
357 bool selected, void *data)
358 {
359 GPtrArray *list = data;
360 const char *p;
361 char *q = NULL;
363 if (i == 0)
364 p = "..";
365 else if (i == list->len + 1)
366 p = _("All tracks");
367 else
368 p = q = utf8_to_locale(g_ptr_array_index(list, i - 1));
370 screen_browser_paint_directory(w, width, selected, p);
371 g_free(q);
372 }
374 static void
375 screen_artist_paint(void)
376 {
377 if (browser.filelist) {
378 screen_browser_paint(&browser);
379 } else if (album_list != NULL)
380 list_window_paint2(browser.lw,
381 paint_album_callback, album_list);
382 else if (artist_list != NULL)
383 list_window_paint2(browser.lw,
384 paint_artist_callback, artist_list);
385 else {
386 wmove(browser.lw->w, 0, 0);
387 wclrtobot(browser.lw->w);
388 }
389 }
391 static const char *
392 screen_artist_get_title(char *str, size_t size)
393 {
394 char *s1, *s2;
396 switch(mode) {
397 case LIST_ARTISTS:
398 g_snprintf(str, size, _("All artists"));
399 break;
401 case LIST_ALBUMS:
402 s1 = utf8_to_locale(artist);
403 g_snprintf(str, size, _("Albums of artist: %s"), s1);
404 g_free(s1);
405 break;
407 case LIST_SONGS:
408 s1 = utf8_to_locale(artist);
410 if (album == ALL_TRACKS)
411 g_snprintf(str, size,
412 _("All tracks of artist: %s"), s1);
413 else if (*album != 0) {
414 s2 = utf8_to_locale(album);
415 g_snprintf(str, size, _("Album: %s - %s"), s1, s2);
416 g_free(s2);
417 } else
418 g_snprintf(str, size,
419 _("Tracks of no album of artist: %s"), s1);
420 g_free(s1);
421 break;
422 }
424 return str;
425 }
427 static void
428 screen_artist_update(struct mpdclient *c)
429 {
430 if (browser.filelist == NULL)
431 return;
433 if (c->events & MPD_IDLE_DATABASE)
434 /* the db has changed -> update the list */
435 reload_lists(c);
437 if (c->events & (MPD_IDLE_DATABASE | MPD_IDLE_QUEUE))
438 screen_browser_sync_highlights(browser.filelist, &c->playlist);
440 if (c->events & (MPD_IDLE_DATABASE
441 #ifndef NCMPC_MINI
442 | MPD_IDLE_QUEUE
443 #endif
444 ))
445 artist_repaint();
446 }
448 /* add_query - Add all songs satisfying specified criteria.
449 _artist is actually only used in the ALBUM case to distinguish albums with
450 the same name from different artists. */
451 static void
452 add_query(struct mpdclient *c, enum mpd_tag_type table, const char *_filter,
453 const char *_artist)
454 {
455 struct mpd_connection *connection = mpdclient_get_connection(c);
456 char *str;
457 struct filelist *addlist;
459 assert(_filter != NULL);
461 if (connection == NULL)
462 return;
464 str = utf8_to_locale(_filter);
465 if (table == MPD_TAG_ALBUM)
466 screen_status_printf(_("Adding album %s..."), str);
467 else
468 screen_status_printf(_("Adding %s..."), str);
469 g_free(str);
471 mpd_search_db_songs(connection, true);
472 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
473 table, _filter);
474 if (table == MPD_TAG_ALBUM)
475 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
476 MPD_TAG_ARTIST, _artist);
477 mpd_search_commit(connection);
479 addlist = filelist_new_recv(connection);
481 if (mpdclient_finish_command(c))
482 mpdclient_filelist_add_all(c, addlist);
484 filelist_free(addlist);
485 }
487 static int
488 screen_artist_lw_cmd(struct mpdclient *c, command_t cmd)
489 {
490 switch (mode) {
491 case LIST_ARTISTS:
492 case LIST_ALBUMS:
493 return list_window_cmd(browser.lw, cmd);
495 case LIST_SONGS:
496 return browser_cmd(&browser, c, cmd);
497 }
499 assert(0);
500 return 0;
501 }
503 static int
504 string_array_find(GPtrArray *array, const char *value)
505 {
506 guint i;
508 for (i = 0; i < array->len; ++i)
509 if (strcmp((const char*)g_ptr_array_index(array, i),
510 value) == 0)
511 return i;
513 return -1;
514 }
516 static bool
517 screen_artist_cmd(struct mpdclient *c, command_t cmd)
518 {
519 struct list_window_range range;
520 char *selected;
521 char *old;
522 char *old_ptr;
523 int idx;
525 switch(cmd) {
526 case CMD_PLAY:
527 switch (mode) {
528 case LIST_ARTISTS:
529 if (browser.lw->selected >= artist_list->len)
530 return true;
532 selected = g_ptr_array_index(artist_list,
533 browser.lw->selected);
534 open_album_list(c, g_strdup(selected));
535 list_window_reset(browser.lw);
537 artist_repaint();
538 return true;
540 case LIST_ALBUMS:
541 if (browser.lw->selected == 0) {
542 /* handle ".." */
543 old = g_strdup(artist);
545 open_artist_list(c);
546 list_window_reset(browser.lw);
547 /* restore previous list window state */
548 idx = string_array_find(artist_list, old);
549 g_free(old);
551 if (idx >= 0) {
552 list_window_set_cursor(browser.lw, idx);
553 list_window_center(browser.lw, idx);
554 }
555 } else if (browser.lw->selected == album_list->len + 1) {
556 /* handle "show all" */
557 open_song_list(c, g_strdup(artist), ALL_TRACKS);
558 list_window_reset(browser.lw);
559 } else {
560 /* select album */
561 selected = g_ptr_array_index(album_list,
562 browser.lw->selected - 1);
563 open_song_list(c, g_strdup(artist), g_strdup(selected));
564 list_window_reset(browser.lw);
565 }
567 artist_repaint();
568 return true;
570 case LIST_SONGS:
571 if (browser.lw->selected == 0) {
572 /* handle ".." */
573 old = g_strdup(album);
574 old_ptr = album;
576 open_album_list(c, g_strdup(artist));
577 list_window_reset(browser.lw);
578 /* restore previous list window state */
579 idx = old_ptr == ALL_TRACKS
580 ? (int)album_list->len
581 : string_array_find(album_list, old);
582 g_free(old);
584 if (idx >= 0) {
585 ++idx;
586 list_window_set_cursor(browser.lw, idx);
587 list_window_center(browser.lw, idx);
588 }
590 artist_repaint();
591 return true;
592 }
593 break;
594 }
595 break;
597 /* FIXME? CMD_GO_* handling duplicates code from CMD_PLAY */
599 case CMD_GO_PARENT_DIRECTORY:
600 switch (mode) {
601 case LIST_ARTISTS:
602 break;
604 case LIST_ALBUMS:
605 old = g_strdup(artist);
607 open_artist_list(c);
608 list_window_reset(browser.lw);
609 /* restore previous list window state */
610 idx = string_array_find(artist_list, old);
611 g_free(old);
613 if (idx >= 0) {
614 list_window_set_cursor(browser.lw, idx);
615 list_window_center(browser.lw, idx);
616 }
617 break;
619 case LIST_SONGS:
620 old = g_strdup(album);
621 old_ptr = album;
623 open_album_list(c, g_strdup(artist));
624 list_window_reset(browser.lw);
625 /* restore previous list window state */
626 idx = old_ptr == ALL_TRACKS
627 ? (int)album_list->len
628 : string_array_find(album_list, old);
629 g_free(old);
631 if (idx >= 0) {
632 ++idx;
633 list_window_set_cursor(browser.lw, idx);
634 list_window_center(browser.lw, idx);
635 }
636 break;
637 }
639 artist_repaint();
640 break;
642 case CMD_GO_ROOT_DIRECTORY:
643 switch (mode) {
644 case LIST_ARTISTS:
645 break;
647 case LIST_ALBUMS:
648 case LIST_SONGS:
649 open_artist_list(c);
650 list_window_reset(browser.lw);
651 /* restore first list window state (pop while returning true) */
652 /* XXX */
653 break;
654 }
656 artist_repaint();
657 break;
659 case CMD_SELECT:
660 case CMD_ADD:
661 switch(mode) {
662 case LIST_ARTISTS:
663 if (browser.lw->selected >= artist_list->len)
664 return true;
666 list_window_get_range(browser.lw, &range);
667 for (unsigned i = range.start; i < range.end; ++i) {
668 selected = g_ptr_array_index(artist_list, i);
669 add_query(c, MPD_TAG_ARTIST, selected, NULL);
670 cmd = CMD_LIST_NEXT; /* continue and select next item... */
671 }
672 break;
674 case LIST_ALBUMS:
675 list_window_get_range(browser.lw, &range);
676 for (unsigned i = range.start; i < range.end; ++i) {
677 if(i == album_list->len + 1)
678 add_query(c, MPD_TAG_ARTIST, artist, NULL);
679 else if (i > 0)
680 {
681 selected = g_ptr_array_index(album_list,
682 browser.lw->selected - 1);
683 add_query(c, MPD_TAG_ALBUM, selected, artist);
684 cmd = CMD_LIST_NEXT; /* continue and select next item... */
685 }
686 }
687 break;
689 case LIST_SONGS:
690 /* handled by browser_cmd() */
691 break;
692 }
693 break;
695 /* continue and update... */
696 case CMD_SCREEN_UPDATE:
697 reload_lists(c);
698 return false;
700 case CMD_LIST_FIND:
701 case CMD_LIST_RFIND:
702 case CMD_LIST_FIND_NEXT:
703 case CMD_LIST_RFIND_NEXT:
704 switch (mode) {
705 case LIST_ARTISTS:
706 screen_find(browser.lw, cmd,
707 screen_artist_lw_callback, artist_list);
708 artist_repaint();
709 return true;
711 case LIST_ALBUMS:
712 screen_find(browser.lw, cmd,
713 screen_artist_lw_callback, album_list);
714 artist_repaint();
715 return true;
717 case LIST_SONGS:
718 /* handled by browser_cmd() */
719 break;
720 }
721 case CMD_LIST_JUMP:
722 switch (mode) {
723 case LIST_ARTISTS:
724 screen_jump(browser.lw, screen_artist_lw_callback,
725 paint_artist_callback, artist_list);
726 artist_repaint();
727 return true;
729 case LIST_ALBUMS:
730 screen_jump(browser.lw, screen_artist_lw_callback,
731 paint_album_callback, album_list);
732 artist_repaint();
733 return true;
735 case LIST_SONGS:
736 /* handled by browser_cmd() */
737 break;
738 }
740 break;
742 default:
743 break;
744 }
746 if (screen_artist_lw_cmd(c, cmd)) {
747 if (screen_is_visible(&screen_artist))
748 artist_repaint();
749 return true;
750 }
752 return false;
753 }
755 const struct screen_functions screen_artist = {
756 .init = screen_artist_init,
757 .exit = screen_artist_quit,
758 .open = screen_artist_open,
759 .resize = screen_artist_resize,
760 .paint = screen_artist_paint,
761 .update = screen_artist_update,
762 .cmd = screen_artist_cmd,
763 .get_title = screen_artist_get_title,
764 };