Code

screen_artist: fixes for the ncmpc-mini build
[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"
30 #include <ctype.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <glib.h>
35 #define BUFSIZE 1024
37 typedef enum { LIST_ARTISTS, LIST_ALBUMS, LIST_SONGS } artist_mode_t;
39 static artist_mode_t mode = LIST_ARTISTS;
40 static GPtrArray *artist_list, *album_list;
41 static char *artist = NULL;
42 static char *album  = NULL;
44 static struct screen_browser browser;
46 static gint
47 compare_utf8(gconstpointer s1, gconstpointer s2)
48 {
49         char *key1, *key2;
50         int n;
52         key1 = g_utf8_collate_key(s1,-1);
53         key2 = g_utf8_collate_key(s2,-1);
54         n = strcmp(key1,key2);
55         g_free(key1);
56         g_free(key2);
57         return n;
58 }
60 /* list_window callback */
61 static const char *
62 artist_lw_callback(unsigned idx, G_GNUC_UNUSED int *highlight,
63                    G_GNUC_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 #ifndef NCMPC_MINI
113 /* the playlist have been updated -> fix highlights */
114 static void
115 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
117         browser_playlist_changed(&browser, c, event, data);
119         artist_repaint_if_active();
121 #endif
123 static GPtrArray *
124 g_list_to_ptr_array(GList *in)
126         GPtrArray *out = g_ptr_array_sized_new(g_list_length(in));
127         GList *head = in;
129         while (in != NULL) {
130                 g_ptr_array_add(out, in->data);
131                 in = g_list_next(in);
132         }
134         g_list_free(head);
135         return out;
138 static void
139 string_array_free(GPtrArray *array)
141         unsigned i;
143         for (i = 0; i < array->len; ++i) {
144                 char *value = g_ptr_array_index(array, i);
145                 free(value);
146         }
148         g_ptr_array_free(array, TRUE);
151 static void
152 free_lists(G_GNUC_UNUSED struct mpdclient *c)
154         if (artist_list != NULL) {
155                 string_array_free(artist_list);
156                 artist_list = NULL;
157         }
159         if (album_list != NULL) {
160                 string_array_free(album_list);
161                 album_list = NULL;
162         }
164         if (browser.filelist) {
165 #ifndef NCMPC_MINI
166                 if (c != NULL)
167                         mpdclient_remove_playlist_callback(c, playlist_changed_callback);
168 #endif
169                 filelist_free(browser.filelist);
170                 browser.filelist = NULL;
171         }
174 static void
175 load_artist_list(struct mpdclient *c)
177         GList *list;
179         assert(mode == LIST_ARTISTS);
180         assert(artist == NULL);
181         assert(album == NULL);
182         assert(artist_list == NULL);
183         assert(album_list == NULL);
184         assert(browser.filelist == NULL);
186         list = mpdclient_get_artists(c);
187         /* sort list */
188         list = g_list_sort(list, compare_utf8);
190         artist_list = g_list_to_ptr_array(list);
193 static void
194 load_album_list(struct mpdclient *c)
196         GList *list;
198         assert(mode == LIST_ALBUMS);
199         assert(artist != NULL);
200         assert(album == NULL);
201         assert(album_list == NULL);
202         assert(browser.filelist == NULL);
204         list = mpdclient_get_albums(c, artist);
205         /* sort list */
206         list = g_list_sort(list, compare_utf8);
208         album_list = g_list_to_ptr_array(list);
211 static void
212 load_song_list(struct mpdclient *c)
214         assert(mode == LIST_SONGS);
215         assert(artist != NULL);
216         assert(album != NULL);
217         assert(browser.filelist == NULL);
219         if (album[0] == 0)
220                 browser.filelist =
221                         mpdclient_filelist_search(c, TRUE,
222                                                   MPD_TABLE_ARTIST,
223                                                   artist);
224         else
225                 browser.filelist =
226                         mpdclient_filelist_search(c, TRUE,
227                                                   MPD_TABLE_ALBUM,
228                                                   album);
229         if (browser.filelist == NULL)
230                 browser.filelist = filelist_new(NULL);
232         /* add a dummy entry for ".." */
233         filelist_prepend(browser.filelist, NULL);
235 #ifndef NCMPC_MINI
236         /* install playlist callback and fix highlights */
237         sync_highlights(c, browser.filelist);
238         mpdclient_install_playlist_callback(c, playlist_changed_callback);
239 #endif
242 static void
243 free_state(struct mpdclient *c)
245         g_free(artist);
246         g_free(album);
247         artist = NULL;
248         album = NULL;
250         free_lists(c);
253 static void
254 open_artist_list(struct mpdclient *c)
256         free_state(c);
258         mode = LIST_ARTISTS;
259         load_artist_list(c);
262 static void
263 open_album_list(struct mpdclient *c, char *_artist)
265         assert(_artist != NULL);
267         free_state(c);
269         mode = LIST_ALBUMS;
270         artist = _artist;
271         load_album_list(c);
274 static void
275 open_song_list(struct mpdclient *c, char *_artist, char *_album)
277         assert(_artist != NULL);
278         assert(_album != NULL);
280         free_state(c);
282         mode = LIST_SONGS;
283         artist = _artist;
284         album = _album;
285         load_song_list(c);
288 static void
289 reload_lists(struct mpdclient *c)
291         free_lists(c);
293         switch (mode) {
294         case LIST_ARTISTS:
295                 load_artist_list(c);
296                 break;
298         case LIST_ALBUMS:
299                 load_album_list(c);
300                 break;
302         case LIST_SONGS:
303                 load_song_list(c);
304                 break;
305         }
308 /* db updated */
309 static void
310 browse_callback(mpdclient_t *c, int event, G_GNUC_UNUSED gpointer data)
312         switch(event) {
313         case BROWSE_DB_UPDATED:
314                 reload_lists(c);
315                 break;
316         default:
317                 break;
318         }
320         artist_repaint_if_active();
323 static void
324 init(WINDOW *w, int cols, int rows)
326         browser.lw = list_window_init(w, cols, rows);
327         artist = NULL;
328         album = NULL;
331 static void
332 quit(void)
334         free_state(NULL);
335         list_window_free(browser.lw);
338 static void
339 open(mpdclient_t *c)
341         static gboolean callback_installed = FALSE;
343         if (artist_list == NULL && album_list == NULL &&
344             browser.filelist == NULL)
345                 reload_lists(c);
346         if (!callback_installed) {
347                 mpdclient_install_browse_callback(c, browse_callback);
348                 callback_installed = TRUE;
349         }
352 static void
353 resize(int cols, int rows)
355         browser.lw->cols = cols;
356         browser.lw->rows = rows;
359 static void
360 paint(void)
362         if (browser.filelist) {
363                 list_window_paint(browser.lw, browser_lw_callback,
364                                   browser.filelist);
365         } else if (album_list != NULL)
366                 list_window_paint(browser.lw, artist_lw_callback, album_list);
367         else if (artist_list != NULL)
368                 list_window_paint(browser.lw, artist_lw_callback, artist_list);
369         else {
370                 wmove(browser.lw->w, 0, 0);
371                 wclrtobot(browser.lw->w);
372         }
375 static const char *
376 get_title(char *str, size_t size)
378         char *s1, *s2;
380         switch(mode) {
381         case LIST_ARTISTS:
382                 g_snprintf(str, size, _("All artists"));
383                 break;
385         case LIST_ALBUMS:
386                 s1 = utf8_to_locale(artist);
387                 g_snprintf(str, size, _("Albums of artist: %s"), s1);
388                 g_free(s1);
389                 break;
391         case LIST_SONGS:
392                 s1 = utf8_to_locale(artist);
393                 if (*album != 0) {
394                         s2 = utf8_to_locale(album);
395                         g_snprintf(str, size,
396                                    _("Album: %s - %s"), s1, s2);
397                         g_free(s2);
398                 } else
399                         g_snprintf(str, size,
400                                    _("All tracks of artist: %s"), s1);
401                 g_free(s1);
402                 break;
403         }
405         return str;
408 static void
409 add_query(mpdclient_t *c, int table, char *_filter)
411         char *str;
412         mpdclient_filelist_t *addlist;
414         assert(filter != NULL);
416         str = utf8_to_locale(_filter);
417         if (table== MPD_TABLE_ALBUM)
418                 screen_status_printf("Adding album %s...", str);
419         else
420                 screen_status_printf("Adding %s...", str);
421         g_free(str);
423         addlist = mpdclient_filelist_search(c, TRUE, table, _filter);
424         if (addlist) {
425                 mpdclient_filelist_add_all(c, addlist);
426                 filelist_free(addlist);
427         }
430 static unsigned
431 metalist_length(void)
433         assert(mode != LIST_ARTISTS || artist_list != NULL);
434         assert(mode != LIST_ALBUMS || album_list != NULL);
436         return mode == LIST_ALBUMS
437                 ? album_list->len + 2
438                 : artist_list->len;
441 static int
442 artist_lw_cmd(struct mpdclient *c, command_t cmd)
444         switch (mode) {
445         case LIST_ARTISTS:
446         case LIST_ALBUMS:
447                 return list_window_cmd(browser.lw, metalist_length(), cmd);
449         case LIST_SONGS:
450                 return browser_cmd(&browser, c, cmd);
451         }
453         assert(0);
454         return 0;
457 static int
458 string_array_find(GPtrArray *array, const char *value)
460         guint i;
462         for (i = 0; i < array->len; ++i)
463                 if (strcmp((const char*)g_ptr_array_index(array, i),
464                            value) == 0)
465                         return i;
467         return -1;
470 static bool
471 artist_cmd(mpdclient_t *c, command_t cmd)
473         char *selected;
474         char *old;
475         int idx;
477         switch(cmd) {
478         case CMD_PLAY:
479                 switch (mode) {
480                 case LIST_ARTISTS:
481                         selected = g_ptr_array_index(artist_list,
482                                                      browser.lw->selected);
483                         open_album_list(c, g_strdup(selected));
484                         list_window_reset(browser.lw);
486                         artist_repaint();
487                         return true;
489                 case LIST_ALBUMS:
490                         if (browser.lw->selected == 0) {
491                                 /* handle ".." */
492                                 old = g_strdup(artist);
494                                 open_artist_list(c);
495                                 list_window_reset(browser.lw);
496                                 /* restore previous list window state */
497                                 idx = string_array_find(artist_list, old);
498                                 g_free(old);
500                                 if (idx >= 0) {
501                                         list_window_set_selected(browser.lw, idx);
502                                         list_window_center(browser.lw,
503                                                            artist_list->len, idx);
504                                 }
505                         } else if (browser.lw->selected == album_list->len + 1) {
506                                 /* handle "show all" */
507                                 open_song_list(c, g_strdup(artist), g_strdup("\0"));
508                                 list_window_reset(browser.lw);
509                         } else {
510                                 /* select album */
511                                 selected = g_ptr_array_index(album_list,
512                                                              browser.lw->selected - 1);
513                                 open_song_list(c, g_strdup(artist), g_strdup(selected));
514                                 list_window_reset(browser.lw);
515                         }
517                         artist_repaint();
518                         return true;
520                 case LIST_SONGS:
521                         if (browser.lw->selected == 0) {
522                                 /* handle ".." */
523                                 old = g_strdup(album);
525                                 open_album_list(c, g_strdup(artist));
526                                 list_window_reset(browser.lw);
527                                 /* restore previous list window state */
528                                 idx = *old == 0
529                                         ? (int)album_list->len
530                                         : string_array_find(album_list, old);
531                                 g_free(old);
533                                 if (idx >= 0) {
534                                         ++idx;
535                                         list_window_set_selected(browser.lw, idx);
536                                         list_window_center(browser.lw,
537                                                            album_list->len, idx);
538                                 }
540                                 artist_repaint();
541                                 return true;
542                         }
543                         break;
544                 }
545                 break;
547                 /* FIXME? CMD_GO_* handling duplicates code from CMD_PLAY */
549         case CMD_GO_PARENT_DIRECTORY:
550                 switch (mode) {
551                 case LIST_ARTISTS:
552                         break;
554                 case LIST_ALBUMS:
555                         old = g_strdup(artist);
557                         open_artist_list(c);
558                         list_window_reset(browser.lw);
559                         /* restore previous list window state */
560                         idx = string_array_find(artist_list, old);
561                         g_free(old);
563                         if (idx >= 0) {
564                                 list_window_set_selected(browser.lw, idx);
565                                 list_window_center(browser.lw,
566                                                    artist_list->len, idx);
567                         }
568                         break;
570                 case LIST_SONGS:
571                         old = g_strdup(album);
573                         open_album_list(c, g_strdup(artist));
574                         list_window_reset(browser.lw);
575                         /* restore previous list window state */
576                         idx = *old == 0
577                                 ? (int)album_list->len
578                                 : string_array_find(album_list, old);
579                         g_free(old);
581                         if (idx >= 0) {
582                                 ++idx;
583                                 list_window_set_selected(browser.lw, idx);
584                                 list_window_center(browser.lw,
585                                                    album_list->len, idx);
586                         }
587                         break;
588                 }
590                 artist_repaint();
591                 break;
593         case CMD_GO_ROOT_DIRECTORY:
594                 switch (mode) {
595                 case LIST_ARTISTS:
596                         break;
598                 case LIST_ALBUMS:
599                 case LIST_SONGS:
600                         open_artist_list(c);
601                         list_window_reset(browser.lw);
602                         /* restore first list window state (pop while returning true) */
603                         /* XXX */
604                         break;
605                 }
607                 artist_repaint();
608                 break;
610         case CMD_SELECT:
611         case CMD_ADD:
612                 switch(mode) {
613                 case LIST_ARTISTS:
614                         selected = g_ptr_array_index(artist_list,
615                                                      browser.lw->selected);
616                         add_query(c, MPD_TABLE_ARTIST, selected);
617                         cmd = CMD_LIST_NEXT; /* continue and select next item... */
618                         break;
620                 case LIST_ALBUMS:
621                         if (browser.lw->selected == album_list->len + 1)
622                                 add_query(c, MPD_TABLE_ARTIST, artist);
623                         else if (browser.lw->selected > 0) {
624                                 selected = g_ptr_array_index(album_list,
625                                                              browser.lw->selected - 1);
626                                 add_query(c, MPD_TABLE_ALBUM, selected);
627                                 cmd = CMD_LIST_NEXT; /* continue and select next item... */
628                         }
629                         break;
631                 case LIST_SONGS:
632                         /* handled by browser_cmd() */
633                         break;
634                 }
635                 break;
637                 /* continue and update... */
638         case CMD_SCREEN_UPDATE:
639                 reload_lists(c);
640                 return false;
642         case CMD_LIST_FIND:
643         case CMD_LIST_RFIND:
644         case CMD_LIST_FIND_NEXT:
645         case CMD_LIST_RFIND_NEXT:
646                 switch (mode) {
647                 case LIST_ARTISTS:
648                         screen_find(browser.lw, artist_list->len,
649                                     cmd, artist_lw_callback, artist_list);
650                         artist_repaint();
651                         return true;
653                 case LIST_ALBUMS:
654                         screen_find(browser.lw, album_list->len + 2,
655                                     cmd, artist_lw_callback, album_list);
656                         artist_repaint();
657                         return true;
659                 case LIST_SONGS:
660                         /* handled by browser_cmd() */
661                         break;
662                 }
664                 break;
666         default:
667                 break;
668         }
670         if (artist_lw_cmd(c, cmd)) {
671                 if (screen_is_visible(&screen_artist))
672                         artist_repaint();
673                 return true;
674         }
676         return false;
679 const struct screen_functions screen_artist = {
680         .init = init,
681         .exit = quit,
682         .open = open,
683         .resize = resize,
684         .paint = paint,
685         .cmd = artist_cmd,
686         .get_title = get_title,
687 };