Code

e4f301aa7dfc934204f5dc367e4ab03c45fb9796
[ncmpc.git] / src / screen_artist.c
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_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;
45 static struct screen_browser browser;
47 static gint
48 compare_utf8(gconstpointer s1, gconstpointer s2)
49 {
50         const char *const*t1 = s1, *const*t2 = s2;
51         char *key1, *key2;
52         int n;
54         key1 = g_utf8_collate_key(*t1,-1);
55         key2 = g_utf8_collate_key(*t2,-1);
56         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;
67         static char buf[BUFSIZE];
68         char *str, *str_utf8;
70         if (mode == LIST_ALBUMS) {
71                 if (idx == 0)
72                         return "[..]";
73                 else if (idx == list->len + 1) {
74                         g_snprintf(buf, BUFSIZE, "[%s]", _("All tracks"));
75                         return buf;
76                 }
78                 --idx;
79         }
81         assert(idx < list->len);
83         str_utf8 = g_ptr_array_index(list, idx);
84         assert(str_utf8 != NULL);
86         str = utf8_to_locale(str_utf8);
87         g_snprintf(buf, BUFSIZE, "[%s]", str);
88         g_free(str);
90         return buf;
91 }
93 static void
94 screen_artist_paint(void);
96 static void
97 artist_repaint(void)
98 {
99         screen_artist_paint();
100         wrefresh(browser.lw->w);
103 static void
104 string_array_free(GPtrArray *array)
106         unsigned i;
108         for (i = 0; i < array->len; ++i) {
109                 char *value = g_ptr_array_index(array, i);
110                 g_free(value);
111         }
113         g_ptr_array_free(array, TRUE);
116 static void
117 free_lists(void)
119         if (artist_list != NULL) {
120                 string_array_free(artist_list);
121                 artist_list = NULL;
122         }
124         if (album_list != NULL) {
125                 string_array_free(album_list);
126                 album_list = NULL;
127         }
129         if (browser.filelist) {
130                 filelist_free(browser.filelist);
131                 browser.filelist = NULL;
132         }
135 static void
136 recv_tag_values(struct mpd_connection *connection, enum mpd_tag_type tag,
137                 GPtrArray *list)
139         struct mpd_pair *pair;
141         while ((pair = mpd_recv_pair_tag(connection, tag)) != NULL) {
142                 g_ptr_array_add(list, g_strdup(pair->value));
143                 mpd_return_pair(connection, pair);
144         }
147 static void
148 load_artist_list(struct mpdclient *c)
150         struct mpd_connection *connection = mpdclient_get_connection(c);
152         assert(mode == LIST_ARTISTS);
153         assert(artist == NULL);
154         assert(album == NULL);
155         assert(artist_list == NULL);
156         assert(album_list == NULL);
157         assert(browser.filelist == NULL);
159         artist_list = g_ptr_array_new();
161         if (connection != NULL) {
162                 mpd_search_db_tags(connection, MPD_TAG_ARTIST);
163                 mpd_search_commit(connection);
164                 recv_tag_values(connection, MPD_TAG_ARTIST, artist_list);
166                 if (!mpd_response_finish(connection))
167                         mpdclient_handle_error(c);
168         }
170         /* sort list */
171         g_ptr_array_sort(artist_list, compare_utf8);
172         list_window_set_length(browser.lw, artist_list->len);
175 static void
176 load_album_list(struct mpdclient *c)
178         struct mpd_connection *connection = mpdclient_get_connection(c);
180         assert(mode == LIST_ALBUMS);
181         assert(artist != NULL);
182         assert(album == NULL);
183         assert(album_list == NULL);
184         assert(browser.filelist == NULL);
186         album_list = g_ptr_array_new();
188         if (connection != NULL) {
189                 mpd_search_db_tags(connection, MPD_TAG_ALBUM);
190                 mpd_search_add_tag_constraint(connection,
191                                               MPD_OPERATOR_DEFAULT,
192                                               MPD_TAG_ARTIST, artist);
193                 mpd_search_commit(connection);
195                 recv_tag_values(connection, MPD_TAG_ALBUM, album_list);
197                 if (!mpd_response_finish(connection))
198                         mpdclient_handle_error(c);
199         }
201         /* sort list */
202         g_ptr_array_sort(album_list, compare_utf8);
204         list_window_set_length(browser.lw, album_list->len + 2);
207 static void
208 load_song_list(struct mpdclient *c)
210         struct mpd_connection *connection = mpdclient_get_connection(c);
212         assert(mode == LIST_SONGS);
213         assert(artist != NULL);
214         assert(album != NULL);
215         assert(browser.filelist == NULL);
217         browser.filelist = filelist_new();
218         /* add a dummy entry for ".." */
219         filelist_append(browser.filelist, NULL);
221         if (connection != NULL) {
222                 mpd_search_db_songs(connection, true);
223                 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
224                                               MPD_TAG_ARTIST, artist);
225                 if (album[0] != 0)
226                         mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
227                                                       MPD_TAG_ALBUM, album);
228                 mpd_search_commit(connection);
230                 filelist_recv(browser.filelist, connection);
232                 if (!mpd_response_finish(connection))
233                         mpdclient_handle_error(c);
234         }
236         /* fix highlights */
237         screen_browser_sync_highlights(browser.filelist, &c->playlist);
238         list_window_set_length(browser.lw, filelist_length(browser.filelist));
241 static void
242 free_state(void)
244         g_free(artist);
245         g_free(album);
246         artist = NULL;
247         album = NULL;
249         free_lists();
252 static void
253 open_artist_list(struct mpdclient *c)
255         free_state();
257         mode = LIST_ARTISTS;
258         load_artist_list(c);
261 static void
262 open_album_list(struct mpdclient *c, char *_artist)
264         assert(_artist != NULL);
266         free_state();
268         mode = LIST_ALBUMS;
269         artist = _artist;
270         load_album_list(c);
273 static void
274 open_song_list(struct mpdclient *c, char *_artist, char *_album)
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);
287 static void
288 reload_lists(struct mpdclient *c)
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         }
307 static void
308 screen_artist_init(WINDOW *w, int cols, int rows)
310         browser.lw = list_window_init(w, cols, rows);
311         artist = NULL;
312         album = NULL;
315 static void
316 screen_artist_quit(void)
318         free_state();
319         list_window_free(browser.lw);
322 static void
323 screen_artist_open(struct mpdclient *c)
325         if (artist_list == NULL && album_list == NULL &&
326             browser.filelist == NULL)
327                 reload_lists(c);
330 static void
331 screen_artist_resize(int cols, int rows)
333         list_window_resize(browser.lw, cols, rows);
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)
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);
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)
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);
377 static void
378 screen_artist_paint(void)
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         }
394 static const char *
395 screen_artist_get_title(char *str, size_t size)
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);
412                 if (*album != 0) {
413                         s2 = utf8_to_locale(album);
414                         g_snprintf(str, size,
415                                    _("Album: %s - %s"), s1, s2);
416                         g_free(s2);
417                 } else
418                         g_snprintf(str, size,
419                                    _("All tracks of artist: %s"), s1);
420                 g_free(s1);
421                 break;
422         }
424         return str;
427 static void
428 screen_artist_update(struct mpdclient *c)
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();
448 static void
449 add_query(struct mpdclient *c, enum mpd_tag_type table, char *_filter)
451         struct mpd_connection *connection = mpdclient_get_connection(c);
452         char *str;
453         struct filelist *addlist;
455         assert(filter != NULL);
457         if (connection == NULL)
458                 return;
460         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         mpd_search_commit(connection);
472         addlist = filelist_new_recv(connection);
474         if (mpd_response_finish(connection))
475                 mpdclient_filelist_add_all(c, addlist);
476         else
477                 mpdclient_handle_error(c);
479         filelist_free(addlist);
482 static int
483 screen_artist_lw_cmd(struct mpdclient *c, command_t cmd)
485         switch (mode) {
486         case LIST_ARTISTS:
487         case LIST_ALBUMS:
488                 return list_window_cmd(browser.lw, cmd);
490         case LIST_SONGS:
491                 return browser_cmd(&browser, c, cmd);
492         }
494         assert(0);
495         return 0;
498 static int
499 string_array_find(GPtrArray *array, const char *value)
501         guint i;
503         for (i = 0; i < array->len; ++i)
504                 if (strcmp((const char*)g_ptr_array_index(array, i),
505                            value) == 0)
506                         return i;
508         return -1;
511 static bool
512 screen_artist_cmd(struct mpdclient *c, command_t cmd)
514         struct list_window_range range;
515         char *selected;
516         char *old;
517         int idx;
519         switch(cmd) {
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), g_strdup("\0"));
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);
569                                 open_album_list(c, g_strdup(artist));
570                                 list_window_reset(browser.lw);
571                                 /* restore previous list window state */
572                                 idx = *old == 0
573                                         ? (int)album_list->len
574                                         : string_array_find(album_list, old);
575                                 g_free(old);
577                                 if (idx >= 0) {
578                                         ++idx;
579                                         list_window_set_cursor(browser.lw, idx);
580                                         list_window_center(browser.lw, idx);
581                                 }
583                                 artist_repaint();
584                                 return true;
585                         }
586                         break;
587                 }
588                 break;
590                 /* FIXME? CMD_GO_* handling duplicates code from CMD_PLAY */
592         case CMD_GO_PARENT_DIRECTORY:
593                 switch (mode) {
594                 case LIST_ARTISTS:
595                         break;
597                 case LIST_ALBUMS:
598                         old = g_strdup(artist);
600                         open_artist_list(c);
601                         list_window_reset(browser.lw);
602                         /* restore previous list window state */
603                         idx = string_array_find(artist_list, old);
604                         g_free(old);
606                         if (idx >= 0) {
607                                 list_window_set_cursor(browser.lw, idx);
608                                 list_window_center(browser.lw, idx);
609                         }
610                         break;
612                 case LIST_SONGS:
613                         old = g_strdup(album);
615                         open_album_list(c, g_strdup(artist));
616                         list_window_reset(browser.lw);
617                         /* restore previous list window state */
618                         idx = *old == 0
619                                 ? (int)album_list->len
620                                 : string_array_find(album_list, old);
621                         g_free(old);
623                         if (idx >= 0) {
624                                 ++idx;
625                                 list_window_set_cursor(browser.lw, idx);
626                                 list_window_center(browser.lw, idx);
627                         }
628                         break;
629                 }
631                 artist_repaint();
632                 break;
634         case CMD_GO_ROOT_DIRECTORY:
635                 switch (mode) {
636                 case LIST_ARTISTS:
637                         break;
639                 case LIST_ALBUMS:
640                 case LIST_SONGS:
641                         open_artist_list(c);
642                         list_window_reset(browser.lw);
643                         /* restore first list window state (pop while returning true) */
644                         /* XXX */
645                         break;
646                 }
648                 artist_repaint();
649                 break;
651         case CMD_SELECT:
652         case CMD_ADD:
653                 switch(mode) {
654                 case LIST_ARTISTS:
655                         if (browser.lw->selected >= artist_list->len)
656                                 return true;
658                         list_window_get_range(browser.lw, &range);
659                         for (unsigned i = range.start; i < range.end; ++i) {
660                                 selected = g_ptr_array_index(artist_list, i);
661                                 add_query(c, MPD_TAG_ARTIST, selected);
662                                 cmd = CMD_LIST_NEXT; /* continue and select next item... */
663                         }
664                         break;
666                 case LIST_ALBUMS:
667                         list_window_get_range(browser.lw, &range);
668                         for (unsigned i = range.start; i < range.end; ++i) {
669                                 if(i == album_list->len + 1)
670                                         add_query(c, MPD_TAG_ARTIST, artist);
671                                 else if (i > 0)
672                                 {
673                                         selected = g_ptr_array_index(album_list,
674                                                                      browser.lw->selected - 1);
675                                         add_query(c, MPD_TAG_ALBUM, selected);
676                                         cmd = CMD_LIST_NEXT; /* continue and select next item... */
677                                 }
678                         }
679                         break;
681                 case LIST_SONGS:
682                         /* handled by browser_cmd() */
683                         break;
684                 }
685                 break;
687                 /* continue and update... */
688         case CMD_SCREEN_UPDATE:
689                 reload_lists(c);
690                 return false;
692         case CMD_LIST_FIND:
693         case CMD_LIST_RFIND:
694         case CMD_LIST_FIND_NEXT:
695         case CMD_LIST_RFIND_NEXT:
696                 switch (mode) {
697                 case LIST_ARTISTS:
698                         screen_find(browser.lw, cmd,
699                                     screen_artist_lw_callback, artist_list);
700                         artist_repaint();
701                         return true;
703                 case LIST_ALBUMS:
704                         screen_find(browser.lw, cmd,
705                                     screen_artist_lw_callback, album_list);
706                         artist_repaint();
707                         return true;
709                 case LIST_SONGS:
710                         /* handled by browser_cmd() */
711                         break;
712                 }
713         case CMD_LIST_JUMP:
714                 switch (mode) {
715                 case LIST_ARTISTS:
716                         screen_jump(browser.lw, screen_artist_lw_callback,
717                                     paint_artist_callback, artist_list);
718                         artist_repaint();
719                         return true;
721                 case LIST_ALBUMS:
722                         screen_jump(browser.lw, screen_artist_lw_callback,
723                                     paint_album_callback, album_list);
724                         artist_repaint();
725                         return true;
727                 case LIST_SONGS:
728                         /* handled by browser_cmd() */
729                         break;
730                 }
732                 break;
734         default:
735                 break;
736         }
738         if (screen_artist_lw_cmd(c, cmd)) {
739                 if (screen_is_visible(&screen_artist))
740                         artist_repaint();
741                 return true;
742         }
744         return false;
747 const struct screen_functions screen_artist = {
748         .init = screen_artist_init,
749         .exit = screen_artist_quit,
750         .open = screen_artist_open,
751         .resize = screen_artist_resize,
752         .paint = screen_artist_paint,
753         .update = screen_artist_update,
754         .cmd = screen_artist_cmd,
755         .get_title = screen_artist_get_title,
756 };