Code

apply whitespace cosmetics to the license comments
[ncmpc.git] / src / screen_artist.c
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_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);
102 static void
103 string_array_free(GPtrArray *array)
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);
115 static void
116 free_lists(void)
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         }
134 static void
135 recv_tag_values(struct mpd_connection *connection, enum mpd_tag_type tag,
136                 GPtrArray *list)
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         }
146 static void
147 load_artist_list(struct mpdclient *c)
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);
174 static void
175 load_album_list(struct mpdclient *c)
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);
206 static void
207 load_song_list(struct mpdclient *c)
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));
240 static void
241 free_state(void)
243         g_free(artist);
244         if (album != ALL_TRACKS)
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);
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;
430 static void
431 screen_artist_update(struct mpdclient *c)
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();
451 /* add_query - Add all songs satisfying specified criteria.
452    _artist is actually only used in the ALBUM case to distinguish albums with
453    the same name from different artists. */
454 static void
455 add_query(struct mpdclient *c, enum mpd_tag_type table, const char *_filter,
456           const char *_artist)
458         struct mpd_connection *connection = mpdclient_get_connection(c);
459         char *str;
460         struct filelist *addlist;
462         assert(_filter != NULL);
464         if (connection == NULL)
465                 return;
467         str = utf8_to_locale(_filter);
468         if (table == MPD_TAG_ALBUM)
469                 screen_status_printf(_("Adding album %s..."), str);
470         else
471                 screen_status_printf(_("Adding %s..."), str);
472         g_free(str);
474         mpd_search_db_songs(connection, true);
475         mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
476                                       table, _filter);
477         if (table == MPD_TAG_ALBUM)
478                 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
479                                               MPD_TAG_ARTIST, _artist);
480         mpd_search_commit(connection);
482         addlist = filelist_new_recv(connection);
484         if (mpd_response_finish(connection))
485                 mpdclient_filelist_add_all(c, addlist);
486         else
487                 mpdclient_handle_error(c);
489         filelist_free(addlist);
492 static int
493 screen_artist_lw_cmd(struct mpdclient *c, command_t cmd)
495         switch (mode) {
496         case LIST_ARTISTS:
497         case LIST_ALBUMS:
498                 return list_window_cmd(browser.lw, cmd);
500         case LIST_SONGS:
501                 return browser_cmd(&browser, c, cmd);
502         }
504         assert(0);
505         return 0;
508 static int
509 string_array_find(GPtrArray *array, const char *value)
511         guint i;
513         for (i = 0; i < array->len; ++i)
514                 if (strcmp((const char*)g_ptr_array_index(array, i),
515                            value) == 0)
516                         return i;
518         return -1;
521 static bool
522 screen_artist_cmd(struct mpdclient *c, command_t cmd)
524         struct list_window_range range;
525         char *selected;
526         char *old;
527         char *old_ptr;
528         int idx;
530         switch(cmd) {
531         case CMD_PLAY:
532                 switch (mode) {
533                 case LIST_ARTISTS:
534                         if (browser.lw->selected >= artist_list->len)
535                                 return true;
537                         selected = g_ptr_array_index(artist_list,
538                                                      browser.lw->selected);
539                         open_album_list(c, g_strdup(selected));
540                         list_window_reset(browser.lw);
542                         artist_repaint();
543                         return true;
545                 case LIST_ALBUMS:
546                         if (browser.lw->selected == 0) {
547                                 /* handle ".." */
548                                 old = g_strdup(artist);
550                                 open_artist_list(c);
551                                 list_window_reset(browser.lw);
552                                 /* restore previous list window state */
553                                 idx = string_array_find(artist_list, old);
554                                 g_free(old);
556                                 if (idx >= 0) {
557                                         list_window_set_cursor(browser.lw, idx);
558                                         list_window_center(browser.lw, idx);
559                                 }
560                         } else if (browser.lw->selected == album_list->len + 1) {
561                                 /* handle "show all" */
562                                 open_song_list(c, g_strdup(artist), ALL_TRACKS);
563                                 list_window_reset(browser.lw);
564                         } else {
565                                 /* select album */
566                                 selected = g_ptr_array_index(album_list,
567                                                              browser.lw->selected - 1);
568                                 open_song_list(c, g_strdup(artist), g_strdup(selected));
569                                 list_window_reset(browser.lw);
570                         }
572                         artist_repaint();
573                         return true;
575                 case LIST_SONGS:
576                         if (browser.lw->selected == 0) {
577                                 /* handle ".." */
578                                 old = g_strdup(album);
579                                 old_ptr = album;
581                                 open_album_list(c, g_strdup(artist));
582                                 list_window_reset(browser.lw);
583                                 /* restore previous list window state */
584                                 idx = old_ptr == ALL_TRACKS
585                                         ? (int)album_list->len
586                                         : string_array_find(album_list, old);
587                                 g_free(old);
589                                 if (idx >= 0) {
590                                         ++idx;
591                                         list_window_set_cursor(browser.lw, idx);
592                                         list_window_center(browser.lw, idx);
593                                 }
595                                 artist_repaint();
596                                 return true;
597                         }
598                         break;
599                 }
600                 break;
602                 /* FIXME? CMD_GO_* handling duplicates code from CMD_PLAY */
604         case CMD_GO_PARENT_DIRECTORY:
605                 switch (mode) {
606                 case LIST_ARTISTS:
607                         break;
609                 case LIST_ALBUMS:
610                         old = g_strdup(artist);
612                         open_artist_list(c);
613                         list_window_reset(browser.lw);
614                         /* restore previous list window state */
615                         idx = string_array_find(artist_list, old);
616                         g_free(old);
618                         if (idx >= 0) {
619                                 list_window_set_cursor(browser.lw, idx);
620                                 list_window_center(browser.lw, idx);
621                         }
622                         break;
624                 case LIST_SONGS:
625                         old = g_strdup(album);
626                         old_ptr = album;
628                         open_album_list(c, g_strdup(artist));
629                         list_window_reset(browser.lw);
630                         /* restore previous list window state */
631                         idx = old_ptr == ALL_TRACKS
632                                 ? (int)album_list->len
633                                 : string_array_find(album_list, old);
634                         g_free(old);
636                         if (idx >= 0) {
637                                 ++idx;
638                                 list_window_set_cursor(browser.lw, idx);
639                                 list_window_center(browser.lw, idx);
640                         }
641                         break;
642                 }
644                 artist_repaint();
645                 break;
647         case CMD_GO_ROOT_DIRECTORY:
648                 switch (mode) {
649                 case LIST_ARTISTS:
650                         break;
652                 case LIST_ALBUMS:
653                 case LIST_SONGS:
654                         open_artist_list(c);
655                         list_window_reset(browser.lw);
656                         /* restore first list window state (pop while returning true) */
657                         /* XXX */
658                         break;
659                 }
661                 artist_repaint();
662                 break;
664         case CMD_SELECT:
665         case CMD_ADD:
666                 switch(mode) {
667                 case LIST_ARTISTS:
668                         if (browser.lw->selected >= artist_list->len)
669                                 return true;
671                         list_window_get_range(browser.lw, &range);
672                         for (unsigned i = range.start; i < range.end; ++i) {
673                                 selected = g_ptr_array_index(artist_list, i);
674                                 add_query(c, MPD_TAG_ARTIST, selected, NULL);
675                                 cmd = CMD_LIST_NEXT; /* continue and select next item... */
676                         }
677                         break;
679                 case LIST_ALBUMS:
680                         list_window_get_range(browser.lw, &range);
681                         for (unsigned i = range.start; i < range.end; ++i) {
682                                 if(i == album_list->len + 1)
683                                         add_query(c, MPD_TAG_ARTIST, artist, NULL);
684                                 else if (i > 0)
685                                 {
686                                         selected = g_ptr_array_index(album_list,
687                                                                      browser.lw->selected - 1);
688                                         add_query(c, MPD_TAG_ALBUM, selected, artist);
689                                         cmd = CMD_LIST_NEXT; /* continue and select next item... */
690                                 }
691                         }
692                         break;
694                 case LIST_SONGS:
695                         /* handled by browser_cmd() */
696                         break;
697                 }
698                 break;
700                 /* continue and update... */
701         case CMD_SCREEN_UPDATE:
702                 reload_lists(c);
703                 return false;
705         case CMD_LIST_FIND:
706         case CMD_LIST_RFIND:
707         case CMD_LIST_FIND_NEXT:
708         case CMD_LIST_RFIND_NEXT:
709                 switch (mode) {
710                 case LIST_ARTISTS:
711                         screen_find(browser.lw, cmd,
712                                     screen_artist_lw_callback, artist_list);
713                         artist_repaint();
714                         return true;
716                 case LIST_ALBUMS:
717                         screen_find(browser.lw, cmd,
718                                     screen_artist_lw_callback, album_list);
719                         artist_repaint();
720                         return true;
722                 case LIST_SONGS:
723                         /* handled by browser_cmd() */
724                         break;
725                 }
726         case CMD_LIST_JUMP:
727                 switch (mode) {
728                 case LIST_ARTISTS:
729                         screen_jump(browser.lw, screen_artist_lw_callback,
730                                     paint_artist_callback, artist_list);
731                         artist_repaint();
732                         return true;
734                 case LIST_ALBUMS:
735                         screen_jump(browser.lw, screen_artist_lw_callback,
736                                     paint_album_callback, album_list);
737                         artist_repaint();
738                         return true;
740                 case LIST_SONGS:
741                         /* handled by browser_cmd() */
742                         break;
743                 }
745                 break;
747         default:
748                 break;
749         }
751         if (screen_artist_lw_cmd(c, cmd)) {
752                 if (screen_is_visible(&screen_artist))
753                         artist_repaint();
754                 return true;
755         }
757         return false;
760 const struct screen_functions screen_artist = {
761         .init = screen_artist_init,
762         .exit = screen_artist_quit,
763         .open = screen_artist_open,
764         .resize = screen_artist_resize,
765         .paint = screen_artist_paint,
766         .update = screen_artist_update,
767         .cmd = screen_artist_cmd,
768         .get_title = screen_artist_get_title,
769 };