Code

an i18n fix
[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
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;
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 static void
452 add_query(struct mpdclient *c, enum mpd_tag_type table, char *_filter)
454         struct mpd_connection *connection = mpdclient_get_connection(c);
455         char *str;
456         struct filelist *addlist;
458         assert(filter != NULL);
460         if (connection == NULL)
461                 return;
463         str = utf8_to_locale(_filter);
464         if (table == MPD_TAG_ALBUM)
465                 screen_status_printf(_("Adding album %s..."), str);
466         else
467                 screen_status_printf(_("Adding %s..."), str);
468         g_free(str);
470         mpd_search_db_songs(connection, true);
471         mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
472                                       table, _filter);
473         if (table == MPD_TAG_ALBUM)
474                 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
475                                               MPD_TAG_ARTIST, artist);
476         mpd_search_commit(connection);
478         addlist = filelist_new_recv(connection);
480         if (mpd_response_finish(connection))
481                 mpdclient_filelist_add_all(c, addlist);
482         else
483                 mpdclient_handle_error(c);
485         filelist_free(addlist);
488 static int
489 screen_artist_lw_cmd(struct mpdclient *c, command_t cmd)
491         switch (mode) {
492         case LIST_ARTISTS:
493         case LIST_ALBUMS:
494                 return list_window_cmd(browser.lw, cmd);
496         case LIST_SONGS:
497                 return browser_cmd(&browser, c, cmd);
498         }
500         assert(0);
501         return 0;
504 static int
505 string_array_find(GPtrArray *array, const char *value)
507         guint i;
509         for (i = 0; i < array->len; ++i)
510                 if (strcmp((const char*)g_ptr_array_index(array, i),
511                            value) == 0)
512                         return i;
514         return -1;
517 static bool
518 screen_artist_cmd(struct mpdclient *c, command_t cmd)
520         struct list_window_range range;
521         char *selected;
522         char *old;
523         char *old_ptr;
524         int idx;
526         switch(cmd) {
527         case CMD_PLAY:
528                 switch (mode) {
529                 case LIST_ARTISTS:
530                         if (browser.lw->selected >= artist_list->len)
531                                 return true;
533                         selected = g_ptr_array_index(artist_list,
534                                                      browser.lw->selected);
535                         open_album_list(c, g_strdup(selected));
536                         list_window_reset(browser.lw);
538                         artist_repaint();
539                         return true;
541                 case LIST_ALBUMS:
542                         if (browser.lw->selected == 0) {
543                                 /* handle ".." */
544                                 old = g_strdup(artist);
546                                 open_artist_list(c);
547                                 list_window_reset(browser.lw);
548                                 /* restore previous list window state */
549                                 idx = string_array_find(artist_list, old);
550                                 g_free(old);
552                                 if (idx >= 0) {
553                                         list_window_set_cursor(browser.lw, idx);
554                                         list_window_center(browser.lw, idx);
555                                 }
556                         } else if (browser.lw->selected == album_list->len + 1) {
557                                 /* handle "show all" */
558                                 open_song_list(c, g_strdup(artist), ALL_TRACKS);
559                                 list_window_reset(browser.lw);
560                         } else {
561                                 /* select album */
562                                 selected = g_ptr_array_index(album_list,
563                                                              browser.lw->selected - 1);
564                                 open_song_list(c, g_strdup(artist), g_strdup(selected));
565                                 list_window_reset(browser.lw);
566                         }
568                         artist_repaint();
569                         return true;
571                 case LIST_SONGS:
572                         if (browser.lw->selected == 0) {
573                                 /* handle ".." */
574                                 old = g_strdup(album);
575                                 old_ptr = album;
577                                 open_album_list(c, g_strdup(artist));
578                                 list_window_reset(browser.lw);
579                                 /* restore previous list window state */
580                                 idx = old_ptr == ALL_TRACKS
581                                         ? (int)album_list->len
582                                         : string_array_find(album_list, old);
583                                 g_free(old);
585                                 if (idx >= 0) {
586                                         ++idx;
587                                         list_window_set_cursor(browser.lw, idx);
588                                         list_window_center(browser.lw, idx);
589                                 }
591                                 artist_repaint();
592                                 return true;
593                         }
594                         break;
595                 }
596                 break;
598                 /* FIXME? CMD_GO_* handling duplicates code from CMD_PLAY */
600         case CMD_GO_PARENT_DIRECTORY:
601                 switch (mode) {
602                 case LIST_ARTISTS:
603                         break;
605                 case LIST_ALBUMS:
606                         old = g_strdup(artist);
608                         open_artist_list(c);
609                         list_window_reset(browser.lw);
610                         /* restore previous list window state */
611                         idx = string_array_find(artist_list, old);
612                         g_free(old);
614                         if (idx >= 0) {
615                                 list_window_set_cursor(browser.lw, idx);
616                                 list_window_center(browser.lw, idx);
617                         }
618                         break;
620                 case LIST_SONGS:
621                         old = g_strdup(album);
622                         old_ptr = album;
624                         open_album_list(c, g_strdup(artist));
625                         list_window_reset(browser.lw);
626                         /* restore previous list window state */
627                         idx = old_ptr == ALL_TRACKS
628                                 ? (int)album_list->len
629                                 : string_array_find(album_list, old);
630                         g_free(old);
632                         if (idx >= 0) {
633                                 ++idx;
634                                 list_window_set_cursor(browser.lw, idx);
635                                 list_window_center(browser.lw, idx);
636                         }
637                         break;
638                 }
640                 artist_repaint();
641                 break;
643         case CMD_GO_ROOT_DIRECTORY:
644                 switch (mode) {
645                 case LIST_ARTISTS:
646                         break;
648                 case LIST_ALBUMS:
649                 case LIST_SONGS:
650                         open_artist_list(c);
651                         list_window_reset(browser.lw);
652                         /* restore first list window state (pop while returning true) */
653                         /* XXX */
654                         break;
655                 }
657                 artist_repaint();
658                 break;
660         case CMD_SELECT:
661         case CMD_ADD:
662                 switch(mode) {
663                 case LIST_ARTISTS:
664                         if (browser.lw->selected >= artist_list->len)
665                                 return true;
667                         list_window_get_range(browser.lw, &range);
668                         for (unsigned i = range.start; i < range.end; ++i) {
669                                 selected = g_ptr_array_index(artist_list, i);
670                                 add_query(c, MPD_TAG_ARTIST, selected);
671                                 cmd = CMD_LIST_NEXT; /* continue and select next item... */
672                         }
673                         break;
675                 case LIST_ALBUMS:
676                         list_window_get_range(browser.lw, &range);
677                         for (unsigned i = range.start; i < range.end; ++i) {
678                                 if(i == album_list->len + 1)
679                                         add_query(c, MPD_TAG_ARTIST, artist);
680                                 else if (i > 0)
681                                 {
682                                         selected = g_ptr_array_index(album_list,
683                                                                      browser.lw->selected - 1);
684                                         add_query(c, MPD_TAG_ALBUM, selected);
685                                         cmd = CMD_LIST_NEXT; /* continue and select next item... */
686                                 }
687                         }
688                         break;
690                 case LIST_SONGS:
691                         /* handled by browser_cmd() */
692                         break;
693                 }
694                 break;
696                 /* continue and update... */
697         case CMD_SCREEN_UPDATE:
698                 reload_lists(c);
699                 return false;
701         case CMD_LIST_FIND:
702         case CMD_LIST_RFIND:
703         case CMD_LIST_FIND_NEXT:
704         case CMD_LIST_RFIND_NEXT:
705                 switch (mode) {
706                 case LIST_ARTISTS:
707                         screen_find(browser.lw, cmd,
708                                     screen_artist_lw_callback, artist_list);
709                         artist_repaint();
710                         return true;
712                 case LIST_ALBUMS:
713                         screen_find(browser.lw, cmd,
714                                     screen_artist_lw_callback, album_list);
715                         artist_repaint();
716                         return true;
718                 case LIST_SONGS:
719                         /* handled by browser_cmd() */
720                         break;
721                 }
722         case CMD_LIST_JUMP:
723                 switch (mode) {
724                 case LIST_ARTISTS:
725                         screen_jump(browser.lw, screen_artist_lw_callback,
726                                     paint_artist_callback, artist_list);
727                         artist_repaint();
728                         return true;
730                 case LIST_ALBUMS:
731                         screen_jump(browser.lw, screen_artist_lw_callback,
732                                     paint_album_callback, album_list);
733                         artist_repaint();
734                         return true;
736                 case LIST_SONGS:
737                         /* handled by browser_cmd() */
738                         break;
739                 }
741                 break;
743         default:
744                 break;
745         }
747         if (screen_artist_lw_cmd(c, cmd)) {
748                 if (screen_is_visible(&screen_artist))
749                         artist_repaint();
750                 return true;
751         }
753         return false;
756 const struct screen_functions screen_artist = {
757         .init = screen_artist_init,
758         .exit = screen_artist_quit,
759         .open = screen_artist_open,
760         .resize = screen_artist_resize,
761         .paint = screen_artist_paint,
762         .update = screen_artist_update,
763         .cmd = screen_artist_cmd,
764         .get_title = screen_artist_get_title,
765 };