Code

01fbc9c1fbc35184f6d0ff5200446ff19bbfb711
[ncmpc.git] / src / screen_artist.c
1 /*
2  * (c) 2005 by Kalle Wallin <kaw@linux.se>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  *
17  */
19 #include "i18n.h"
20 #include "options.h"
21 #include "charset.h"
22 #include "mpdclient.h"
23 #include "utils.h"
24 #include "strfsong.h"
25 #include "command.h"
26 #include "screen.h"
27 #include "screen_utils.h"
28 #include "screen_browser.h"
29 #include "gcc.h"
31 #include <ctype.h>
32 #include <stdlib.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         char *key1, *key2;
51         int n;
53         key1 = g_utf8_collate_key(s1,-1);
54         key2 = g_utf8_collate_key(s2,-1);
55         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 artist_lw_callback(unsigned idx, mpd_unused int *highlight, mpd_unused void *data)
64 {
65         GPtrArray *list = data;
66         static char buf[BUFSIZE];
67         char *str, *str_utf8;
69         if (mode == LIST_ALBUMS) {
70                 if (idx == 0)
71                         return "[..]";
72                 else if (idx == list->len + 1) {
73                         str = utf8_to_locale(_("All tracks"));
74                         g_snprintf(buf, BUFSIZE, "[%s]", str);
75                         g_free(str);
76                         return buf;
77                 }
79                 --idx;
80         }
82         if (idx >= list->len)
83                 return NULL;
85         str_utf8 = g_ptr_array_index(list, idx);
86         assert(str_utf8 != NULL);
88         str = utf8_to_locale(str_utf8);
89         g_snprintf(buf, BUFSIZE, "[%s]", str);
90         g_free(str);
92         return buf;
93 }
95 static void
96 paint(void);
98 static void
99 artist_repaint(void)
101         paint();
102         wrefresh(browser.lw->w);
105 static void
106 artist_repaint_if_active(void)
108         if (screen_is_visible(&screen_artist))
109                 artist_repaint();
112 /* the playlist have been updated -> fix highlights */
113 static void
114 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
116         browser_playlist_changed(&browser, c, event, data);
118         artist_repaint_if_active();
121 static GPtrArray *
122 g_list_to_ptr_array(GList *in)
124         GPtrArray *out = g_ptr_array_sized_new(g_list_length(in));
125         GList *head = in;
127         while (in != NULL) {
128                 g_ptr_array_add(out, in->data);
129                 in = g_list_next(in);
130         }
132         g_list_free(head);
133         return out;
136 static void
137 string_array_free(GPtrArray *array)
139         unsigned i;
141         for (i = 0; i < array->len; ++i) {
142                 char *value = g_ptr_array_index(array, i);
143                 free(value);
144         }
146         g_ptr_array_free(array, TRUE);
149 static void
150 free_lists(struct mpdclient *c)
152         if (artist_list != NULL) {
153                 string_array_free(artist_list);
154                 artist_list = NULL;
155         }
157         if (album_list != NULL) {
158                 string_array_free(album_list);
159                 album_list = NULL;
160         }
162         if (browser.filelist) {
163                 if (c != NULL)
164                         mpdclient_remove_playlist_callback(c, playlist_changed_callback);
165                 filelist_free(browser.filelist);
166                 browser.filelist = NULL;
167         }
170 static void
171 load_artist_list(struct mpdclient *c)
173         GList *list;
175         assert(mode == LIST_ARTISTS);
176         assert(artist == NULL);
177         assert(album == NULL);
178         assert(artist_list == NULL);
179         assert(album_list == NULL);
180         assert(browser.filelist == NULL);
182         list = mpdclient_get_artists(c);
183         /* sort list */
184         list = g_list_sort(list, compare_utf8);
186         artist_list = g_list_to_ptr_array(list);
189 static void
190 load_album_list(struct mpdclient *c)
192         GList *list;
194         assert(mode == LIST_ALBUMS);
195         assert(artist != NULL);
196         assert(album == NULL);
197         assert(album_list == NULL);
198         assert(browser.filelist == NULL);
200         list = mpdclient_get_albums(c, artist);
201         /* sort list */
202         list = g_list_sort(list, compare_utf8);
204         album_list = g_list_to_ptr_array(list);
207 static void
208 load_song_list(struct mpdclient *c)
210         assert(mode == LIST_SONGS);
211         assert(artist != NULL);
212         assert(album != NULL);
213         assert(browser.filelist == NULL);
215         if (album[0] == 0)
216                 browser.filelist =
217                         mpdclient_filelist_search(c, TRUE,
218                                                   MPD_TABLE_ARTIST,
219                                                   artist);
220         else
221                 browser.filelist =
222                         mpdclient_filelist_search(c, TRUE,
223                                                   MPD_TABLE_ALBUM,
224                                                   album);
225         if (browser.filelist == NULL)
226                 browser.filelist = filelist_new(NULL);
228         /* add a dummy entry for ".." */
229         filelist_prepend(browser.filelist, NULL);
231         /* install playlist callback and fix highlights */
232         sync_highlights(c, browser.filelist);
233         mpdclient_install_playlist_callback(c, playlist_changed_callback);
236 static void
237 free_state(struct mpdclient *c)
239         g_free(artist);
240         g_free(album);
241         artist = NULL;
242         album = NULL;
244         free_lists(c);
247 static void
248 open_artist_list(struct mpdclient *c)
250         free_state(c);
252         mode = LIST_ARTISTS;
253         load_artist_list(c);
256 static void
257 open_album_list(struct mpdclient *c, char *_artist)
259         assert(_artist != NULL);
261         free_state(c);
263         mode = LIST_ALBUMS;
264         artist = _artist;
265         load_album_list(c);
268 static void
269 open_song_list(struct mpdclient *c, char *_artist, char *_album)
271         assert(_artist != NULL);
272         assert(_album != NULL);
274         free_state(c);
276         mode = LIST_SONGS;
277         artist = _artist;
278         album = _album;
279         load_song_list(c);
282 static void
283 reload_lists(struct mpdclient *c)
285         free_lists(c);
287         switch (mode) {
288         case LIST_ARTISTS:
289                 load_artist_list(c);
290                 break;
292         case LIST_ALBUMS:
293                 load_album_list(c);
294                 break;
296         case LIST_SONGS:
297                 load_song_list(c);
298                 break;
299         }
302 /* db updated */
303 static void
304 browse_callback(mpdclient_t *c, int event, mpd_unused gpointer data)
306         switch(event) {
307         case BROWSE_DB_UPDATED:
308                 reload_lists(c);
309                 break;
310         default:
311                 break;
312         }
314         artist_repaint_if_active();
317 static void
318 init(WINDOW *w, int cols, int rows)
320         browser.lw = list_window_init(w, cols, rows);
321         artist = NULL;
322         album = NULL;
325 static void
326 quit(void)
328         free_state(NULL);
329         list_window_free(browser.lw);
332 static void
333 open(mpdclient_t *c)
335         static gboolean callback_installed = FALSE;
337         if (artist_list == NULL && album_list == NULL &&
338             browser.filelist == NULL)
339                 reload_lists(c);
340         if (!callback_installed) {
341                 mpdclient_install_browse_callback(c, browse_callback);
342                 callback_installed = TRUE;
343         }
346 static void
347 resize(int cols, int rows)
349         browser.lw->cols = cols;
350         browser.lw->rows = rows;
353 static void
354 paint(void)
356         if (browser.filelist) {
357                 list_window_paint(browser.lw, browser_lw_callback,
358                                   browser.filelist);
359         } else if (album_list != NULL)
360                 list_window_paint(browser.lw, artist_lw_callback, album_list);
361         else if (artist_list != NULL)
362                 list_window_paint(browser.lw, artist_lw_callback, artist_list);
363         else {
364                 wmove(browser.lw->w, 0, 0);
365                 wclrtobot(browser.lw->w);
366         }
369 static const char *
370 get_title(char *str, size_t size)
372         char *s1, *s2;
374         switch(mode) {
375         case LIST_ARTISTS:
376                 g_snprintf(str, size, _("All artists"));
377                 break;
379         case LIST_ALBUMS:
380                 s1 = utf8_to_locale(artist);
381                 g_snprintf(str, size, _("Albums of artist: %s"), s1);
382                 g_free(s1);
383                 break;
385         case LIST_SONGS:
386                 s1 = utf8_to_locale(artist);
387                 if (*album != 0) {
388                         s2 = utf8_to_locale(album);
389                         g_snprintf(str, size,
390                                    _("Album: %s - %s"), s1, s2);
391                         g_free(s2);
392                 } else
393                         g_snprintf(str, size,
394                                    _("All tracks of artist: %s"), s1);
395                 g_free(s1);
396                 break;
397         }
399         return str;
402 static void
403 add_query(mpdclient_t *c, int table, char *_filter)
405         char *str;
406         mpdclient_filelist_t *addlist;
408         assert(filter != NULL);
410         str = utf8_to_locale(_filter);
411         if (table== MPD_TABLE_ALBUM)
412                 screen_status_printf("Adding album %s...", str);
413         else
414                 screen_status_printf("Adding %s...", str);
415         g_free(str);
417         addlist = mpdclient_filelist_search(c, TRUE, table, _filter);
418         if (addlist) {
419                 mpdclient_filelist_add_all(c, addlist);
420                 filelist_free(addlist);
421         }
424 static unsigned
425 metalist_length(void)
427         assert(mode != LIST_ARTISTS || artist_list != NULL);
428         assert(mode != LIST_ALBUMS || album_list != NULL);
430         return mode == LIST_ALBUMS
431                 ? album_list->len + 2
432                 : artist_list->len;
435 static int
436 artist_lw_cmd(struct mpdclient *c, command_t cmd)
438         switch (mode) {
439         case LIST_ARTISTS:
440         case LIST_ALBUMS:
441                 return list_window_cmd(browser.lw, metalist_length(), cmd);
443         case LIST_SONGS:
444                 return browser_cmd(&browser, c, cmd);
445         }
447         assert(0);
448         return 0;
451 static int
452 string_array_find(GPtrArray *array, const char *value)
454         guint i;
456         for (i = 0; i < array->len; ++i)
457                 if (strcmp((const char*)g_ptr_array_index(array, i),
458                            value) == 0)
459                         return i;
461         return -1;
464 static bool
465 artist_cmd(mpdclient_t *c, command_t cmd)
467         char *selected;
468         char *old;
469         int idx;
471         switch(cmd) {
472         case CMD_PLAY:
473                 switch (mode) {
474                 case LIST_ARTISTS:
475                         selected = g_ptr_array_index(artist_list,
476                                                      browser.lw->selected);
477                         open_album_list(c, g_strdup(selected));
478                         list_window_reset(browser.lw);
480                         artist_repaint();
481                         return true;
483                 case LIST_ALBUMS:
484                         if (browser.lw->selected == 0) {
485                                 /* handle ".." */
486                                 old = g_strdup(artist);
488                                 open_artist_list(c);
489                                 list_window_reset(browser.lw);
490                                 /* restore previous list window state */
491                                 idx = string_array_find(artist_list, old);
492                                 g_free(old);
494                                 if (idx >= 0) {
495                                         list_window_set_selected(browser.lw, idx);
496                                         list_window_center(browser.lw,
497                                                            artist_list->len, idx);
498                                 }
499                         } else if (browser.lw->selected == album_list->len + 1) {
500                                 /* handle "show all" */
501                                 open_song_list(c, g_strdup(artist), g_strdup("\0"));
502                                 list_window_reset(browser.lw);
503                         } else {
504                                 /* select album */
505                                 selected = g_ptr_array_index(album_list,
506                                                              browser.lw->selected - 1);
507                                 open_song_list(c, g_strdup(artist), g_strdup(selected));
508                                 list_window_reset(browser.lw);
509                         }
511                         artist_repaint();
512                         return true;
514                 case LIST_SONGS:
515                         if (browser.lw->selected == 0) {
516                                 /* handle ".." */
517                                 old = g_strdup(album);
519                                 open_album_list(c, g_strdup(artist));
520                                 list_window_reset(browser.lw);
521                                 /* restore previous list window state */
522                                 idx = *old == 0
523                                         ? (int)album_list->len
524                                         : string_array_find(album_list, old);
525                                 g_free(old);
527                                 if (idx >= 0) {
528                                         ++idx;
529                                         list_window_set_selected(browser.lw, idx);
530                                         list_window_center(browser.lw,
531                                                            album_list->len, idx);
532                                 }
534                                 artist_repaint();
535                                 return true;
536                         }
537                         break;
538                 }
539                 break;
541                 /* FIXME? CMD_GO_* handling duplicates code from CMD_PLAY */
543         case CMD_GO_PARENT_DIRECTORY:
544                 switch (mode) {
545                 case LIST_ARTISTS:
546                         break;
548                 case LIST_ALBUMS:
549                         old = g_strdup(artist);
551                         open_artist_list(c);
552                         list_window_reset(browser.lw);
553                         /* restore previous list window state */
554                         idx = string_array_find(artist_list, old);
555                         g_free(old);
557                         if (idx >= 0) {
558                                 list_window_set_selected(browser.lw, idx);
559                                 list_window_center(browser.lw,
560                                                    artist_list->len, idx);
561                         }
562                         break;
564                 case LIST_SONGS:
565                         old = g_strdup(album);
567                         open_album_list(c, g_strdup(artist));
568                         list_window_reset(browser.lw);
569                         /* restore previous list window state */
570                         idx = *old == 0
571                                 ? (int)album_list->len
572                                 : string_array_find(album_list, old);
573                         g_free(old);
575                         if (idx >= 0) {
576                                 ++idx;
577                                 list_window_set_selected(browser.lw, idx);
578                                 list_window_center(browser.lw,
579                                                    album_list->len, idx);
580                         }
581                         break;
582                 }
584                 artist_repaint();
585                 break;
587         case CMD_GO_ROOT_DIRECTORY:
588                 switch (mode) {
589                 case LIST_ARTISTS:
590                         break;
592                 case LIST_ALBUMS:
593                 case LIST_SONGS:
594                         open_artist_list(c);
595                         list_window_reset(browser.lw);
596                         /* restore first list window state (pop while returning true) */
597                         /* XXX */
598                         break;
599                 }
601                 artist_repaint();
602                 break;
604         case CMD_SELECT:
605         case CMD_ADD:
606                 switch(mode) {
607                 case LIST_ARTISTS:
608                         selected = g_ptr_array_index(artist_list,
609                                                      browser.lw->selected);
610                         add_query(c, MPD_TABLE_ARTIST, selected);
611                         cmd = CMD_LIST_NEXT; /* continue and select next item... */
612                         break;
614                 case LIST_ALBUMS:
615                         if (browser.lw->selected == album_list->len + 1)
616                                 add_query(c, MPD_TABLE_ARTIST, artist);
617                         else if (browser.lw->selected > 0) {
618                                 selected = g_ptr_array_index(album_list,
619                                                              browser.lw->selected - 1);
620                                 add_query(c, MPD_TABLE_ALBUM, selected);
621                                 cmd = CMD_LIST_NEXT; /* continue and select next item... */
622                         }
623                         break;
625                 case LIST_SONGS:
626                         /* handled by browser_cmd() */
627                         break;
628                 }
629                 break;
631                 /* continue and update... */
632         case CMD_SCREEN_UPDATE:
633                 reload_lists(c);
634                 screen_status_printf(_("Screen updated!"));
635                 return false;
637         case CMD_LIST_FIND:
638         case CMD_LIST_RFIND:
639         case CMD_LIST_FIND_NEXT:
640         case CMD_LIST_RFIND_NEXT:
641                 switch (mode) {
642                 case LIST_ARTISTS:
643                         screen_find(browser.lw, artist_list->len,
644                                     cmd, artist_lw_callback, artist_list);
645                         artist_repaint();
646                         return true;
648                 case LIST_ALBUMS:
649                         screen_find(browser.lw, album_list->len + 2,
650                                     cmd, artist_lw_callback, album_list);
651                         artist_repaint();
652                         return true;
654                 case LIST_SONGS:
655                         /* handled by browser_cmd() */
656                         break;
657                 }
659                 break;
661         default:
662                 break;
663         }
665         if (artist_lw_cmd(c, cmd)) {
666                 if (screen_is_visible(&screen_artist))
667                         artist_repaint();
668                 return true;
669         }
671         return false;
674 const struct screen_functions screen_artist = {
675         .init = init,
676         .exit = quit,
677         .open = open,
678         .resize = resize,
679         .paint = paint,
680         .cmd = artist_cmd,
681         .get_title = get_title,
682 };