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>
35 #include <ncurses.h>
37 #define BUFSIZE 1024
39 typedef enum { LIST_ARTISTS, LIST_ALBUMS, LIST_SONGS } artist_mode_t;
41 static artist_mode_t mode = LIST_ARTISTS;
42 static GPtrArray *artist_list, *album_list;
43 static char *artist = NULL;
44 static char *album = NULL;
46 static struct screen_browser browser;
48 static gint
49 compare_utf8(gconstpointer s1, gconstpointer s2)
50 {
51 char *key1, *key2;
52 int n;
54 key1 = g_utf8_collate_key(s1,-1);
55 key2 = g_utf8_collate_key(s2,-1);
56 n = strcmp(key1,key2);
57 g_free(key1);
58 g_free(key2);
59 return n;
60 }
62 /* list_window callback */
63 static const char *
64 artist_lw_callback(unsigned idx, mpd_unused int *highlight, mpd_unused void *data)
65 {
66 GPtrArray *list = data;
67 static char buf[BUFSIZE];
68 char *str, *str_utf8;
70 if (mode == LIST_ALBUMS) {
71 if (idx == 0)
72 return "[..]";
73 else if (idx == list->len + 1) {
74 str = utf8_to_locale(_("All tracks"));
75 g_snprintf(buf, BUFSIZE, "[%s]", str);
76 g_free(str);
77 return buf;
78 }
80 --idx;
81 }
83 if (idx >= list->len)
84 return NULL;
86 str_utf8 = g_ptr_array_index(list, idx);
87 assert(str_utf8 != NULL);
89 str = utf8_to_locale(str_utf8);
90 g_snprintf(buf, BUFSIZE, "[%s]", str);
91 g_free(str);
93 return buf;
94 }
96 static void
97 paint(void);
99 static void
100 artist_repaint(void)
101 {
102 paint();
103 wrefresh(browser.lw->w);
104 }
106 static void
107 artist_repaint_if_active(void)
108 {
109 if (screen_is_visible(&screen_artist))
110 artist_repaint();
111 }
113 /* the playlist have been updated -> fix highlights */
114 static void
115 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
116 {
117 browser_playlist_changed(&browser, c, event, data);
119 artist_repaint_if_active();
120 }
122 static GPtrArray *
123 g_list_to_ptr_array(GList *in)
124 {
125 GPtrArray *out = g_ptr_array_sized_new(g_list_length(in));
126 GList *head = in;
128 while (in != NULL) {
129 g_ptr_array_add(out, in->data);
130 in = g_list_next(in);
131 }
133 g_list_free(head);
134 return out;
135 }
137 static void
138 string_array_free(GPtrArray *array)
139 {
140 unsigned i;
142 for (i = 0; i < array->len; ++i) {
143 char *value = g_ptr_array_index(array, i);
144 free(value);
145 }
147 g_ptr_array_free(array, TRUE);
148 }
150 static void
151 free_lists(struct mpdclient *c)
152 {
153 if (artist_list != NULL) {
154 string_array_free(artist_list);
155 artist_list = NULL;
156 }
158 if (album_list != NULL) {
159 string_array_free(album_list);
160 album_list = NULL;
161 }
163 if (browser.filelist) {
164 if (c != NULL)
165 mpdclient_remove_playlist_callback(c, playlist_changed_callback);
166 filelist_free(browser.filelist);
167 browser.filelist = NULL;
168 }
169 }
171 static void
172 load_artist_list(struct mpdclient *c)
173 {
174 GList *list;
176 assert(mode == LIST_ARTISTS);
177 assert(artist == NULL);
178 assert(album == NULL);
179 assert(artist_list == NULL);
180 assert(album_list == NULL);
181 assert(browser.filelist == NULL);
183 list = mpdclient_get_artists_utf8(c);
184 /* sort list */
185 list = g_list_sort(list, compare_utf8);
187 artist_list = g_list_to_ptr_array(list);
188 }
190 static void
191 load_album_list(struct mpdclient *c)
192 {
193 GList *list;
195 assert(mode == LIST_ALBUMS);
196 assert(artist != NULL);
197 assert(album == NULL);
198 assert(album_list == NULL);
199 assert(browser.filelist == NULL);
201 list = mpdclient_get_albums_utf8(c, artist);
202 /* sort list */
203 list = g_list_sort(list, compare_utf8);
205 album_list = g_list_to_ptr_array(list);
206 }
208 static void
209 load_song_list(struct mpdclient *c)
210 {
211 assert(mode == LIST_SONGS);
212 assert(artist != NULL);
213 assert(album != NULL);
214 assert(browser.filelist == NULL);
216 if (album[0] == 0)
217 browser.filelist =
218 mpdclient_filelist_search_utf8(c, TRUE,
219 MPD_TABLE_ARTIST,
220 artist);
221 else
222 browser.filelist =
223 mpdclient_filelist_search_utf8(c, TRUE,
224 MPD_TABLE_ALBUM,
225 album);
226 if (browser.filelist == NULL)
227 browser.filelist = filelist_new(NULL);
229 /* add a dummy entry for ".." */
230 filelist_prepend(browser.filelist, NULL);
232 /* install playlist callback and fix highlights */
233 sync_highlights(c, browser.filelist);
234 mpdclient_install_playlist_callback(c, playlist_changed_callback);
235 }
237 static void
238 free_state(struct mpdclient *c)
239 {
240 g_free(artist);
241 g_free(album);
242 artist = NULL;
243 album = NULL;
245 free_lists(c);
246 }
248 static void
249 open_artist_list(struct mpdclient *c)
250 {
251 free_state(c);
253 mode = LIST_ARTISTS;
254 load_artist_list(c);
255 }
257 static void
258 open_album_list(struct mpdclient *c, char *_artist)
259 {
260 assert(_artist != NULL);
262 free_state(c);
264 mode = LIST_ALBUMS;
265 artist = _artist;
266 load_album_list(c);
267 }
269 static void
270 open_song_list(struct mpdclient *c, char *_artist, char *_album)
271 {
272 assert(_artist != NULL);
273 assert(_album != NULL);
275 free_state(c);
277 mode = LIST_SONGS;
278 artist = _artist;
279 album = _album;
280 load_song_list(c);
281 }
283 static void
284 reload_lists(struct mpdclient *c)
285 {
286 free_lists(c);
288 switch (mode) {
289 case LIST_ARTISTS:
290 load_artist_list(c);
291 break;
293 case LIST_ALBUMS:
294 load_album_list(c);
295 break;
297 case LIST_SONGS:
298 load_song_list(c);
299 break;
300 }
301 }
303 /* db updated */
304 static void
305 browse_callback(mpdclient_t *c, int event, mpd_unused gpointer data)
306 {
307 switch(event) {
308 case BROWSE_DB_UPDATED:
309 reload_lists(c);
310 break;
311 default:
312 break;
313 }
315 artist_repaint_if_active();
316 }
318 static void
319 init(WINDOW *w, int cols, int rows)
320 {
321 browser.lw = list_window_init(w, cols, rows);
322 artist = NULL;
323 album = NULL;
324 }
326 static void
327 quit(void)
328 {
329 free_state(NULL);
330 list_window_free(browser.lw);
331 }
333 static void
334 open(mpdclient_t *c)
335 {
336 static gboolean callback_installed = FALSE;
338 if (artist_list == NULL && album_list == NULL &&
339 browser.filelist == NULL)
340 reload_lists(c);
341 if (!callback_installed) {
342 mpdclient_install_browse_callback(c, browse_callback);
343 callback_installed = TRUE;
344 }
345 }
347 static void
348 resize(int cols, int rows)
349 {
350 browser.lw->cols = cols;
351 browser.lw->rows = rows;
352 }
354 static void
355 paint(void)
356 {
357 if (browser.filelist) {
358 list_window_paint(browser.lw, browser_lw_callback,
359 browser.filelist);
360 } else if (album_list != NULL)
361 list_window_paint(browser.lw, artist_lw_callback, album_list);
362 else if (artist_list != NULL)
363 list_window_paint(browser.lw, artist_lw_callback, artist_list);
364 else {
365 wmove(browser.lw->w, 0, 0);
366 wclrtobot(browser.lw->w);
367 }
368 }
370 static const char *
371 get_title(char *str, size_t size)
372 {
373 char *s1, *s2;
375 switch(mode) {
376 case LIST_ARTISTS:
377 g_snprintf(str, size, _("All artists"));
378 break;
380 case LIST_ALBUMS:
381 s1 = utf8_to_locale(artist);
382 g_snprintf(str, size, _("Albums of artist: %s"), s1);
383 g_free(s1);
384 break;
386 case LIST_SONGS:
387 s1 = utf8_to_locale(artist);
388 if (*album != 0) {
389 s2 = utf8_to_locale(album);
390 g_snprintf(str, size,
391 _("Album: %s - %s"), s1, s2);
392 g_free(s2);
393 } else
394 g_snprintf(str, size,
395 _("All tracks of artist: %s"), s1);
396 g_free(s1);
397 break;
398 }
400 return str;
401 }
403 static void
404 add_query(mpdclient_t *c, int table, char *_filter)
405 {
406 char *str;
407 mpdclient_filelist_t *addlist;
409 assert(filter != NULL);
411 str = utf8_to_locale(_filter);
412 if (table== MPD_TABLE_ALBUM)
413 screen_status_printf("Adding album %s...", str);
414 else
415 screen_status_printf("Adding %s...", str);
416 g_free(str);
418 addlist = mpdclient_filelist_search_utf8(c, TRUE, table, _filter);
419 if (addlist) {
420 mpdclient_filelist_add_all(c, addlist);
421 filelist_free(addlist);
422 }
423 }
425 static unsigned
426 metalist_length(void)
427 {
428 assert(mode != LIST_ARTISTS || artist_list != NULL);
429 assert(mode != LIST_ALBUMS || album_list != NULL);
431 return mode == LIST_ALBUMS
432 ? album_list->len + 2
433 : artist_list->len;
434 }
436 static int
437 artist_lw_cmd(struct mpdclient *c, command_t cmd)
438 {
439 switch (mode) {
440 case LIST_ARTISTS:
441 case LIST_ALBUMS:
442 return list_window_cmd(browser.lw, metalist_length(), cmd);
444 case LIST_SONGS:
445 return browser_cmd(&browser, c, cmd);
446 }
448 assert(0);
449 return 0;
450 }
452 static int
453 string_array_find(GPtrArray *array, const char *value)
454 {
455 guint i;
457 for (i = 0; i < array->len; ++i)
458 if (strcmp((const char*)g_ptr_array_index(array, i),
459 value) == 0)
460 return i;
462 return -1;
463 }
465 static int
466 artist_cmd(mpdclient_t *c, command_t cmd)
467 {
468 char *selected;
469 char *old;
470 int idx;
472 switch(cmd) {
473 case CMD_PLAY:
474 switch (mode) {
475 case LIST_ARTISTS:
476 selected = g_ptr_array_index(artist_list,
477 browser.lw->selected);
478 open_album_list(c, g_strdup(selected));
479 list_window_reset(browser.lw);
481 artist_repaint();
482 return true;
484 case LIST_ALBUMS:
485 if (browser.lw->selected == 0) {
486 /* handle ".." */
487 old = g_strdup(artist);
489 open_artist_list(c);
490 list_window_reset(browser.lw);
491 /* restore previous list window state */
492 idx = string_array_find(artist_list, old);
493 g_free(old);
495 if (idx >= 0) {
496 list_window_set_selected(browser.lw, idx);
497 list_window_center(browser.lw,
498 artist_list->len, idx);
499 }
500 } else if (browser.lw->selected == album_list->len + 1) {
501 /* handle "show all" */
502 open_song_list(c, g_strdup(artist), g_strdup("\0"));
503 list_window_reset(browser.lw);
504 } else {
505 /* select album */
506 selected = g_ptr_array_index(album_list,
507 browser.lw->selected - 1);
508 open_song_list(c, g_strdup(artist), g_strdup(selected));
509 list_window_reset(browser.lw);
510 }
512 artist_repaint();
513 return true;
515 case LIST_SONGS:
516 if (browser.lw->selected == 0) {
517 /* handle ".." */
518 old = g_strdup(album);
520 open_album_list(c, g_strdup(artist));
521 list_window_reset(browser.lw);
522 /* restore previous list window state */
523 idx = *old == 0
524 ? (int)album_list->len
525 : string_array_find(album_list, old);
526 g_free(old);
528 if (idx >= 0) {
529 ++idx;
530 list_window_set_selected(browser.lw, idx);
531 list_window_center(browser.lw,
532 album_list->len, idx);
533 }
535 artist_repaint();
536 return true;
537 }
538 break;
539 }
540 break;
542 /* FIXME? CMD_GO_* handling duplicates code from CMD_PLAY */
544 case CMD_GO_PARENT_DIRECTORY:
545 switch (mode) {
546 case LIST_ARTISTS:
547 break;
549 case LIST_ALBUMS:
550 old = g_strdup(artist);
552 open_artist_list(c);
553 list_window_reset(browser.lw);
554 /* restore previous list window state */
555 idx = string_array_find(artist_list, old);
556 g_free(old);
558 if (idx >= 0) {
559 list_window_set_selected(browser.lw, idx);
560 list_window_center(browser.lw,
561 artist_list->len, idx);
562 }
563 break;
565 case LIST_SONGS:
566 old = g_strdup(album);
568 open_album_list(c, g_strdup(artist));
569 list_window_reset(browser.lw);
570 /* restore previous list window state */
571 idx = *old == 0
572 ? (int)album_list->len
573 : string_array_find(album_list, old);
574 g_free(old);
576 if (idx >= 0) {
577 ++idx;
578 list_window_set_selected(browser.lw, idx);
579 list_window_center(browser.lw,
580 album_list->len, idx);
581 }
582 break;
583 }
585 artist_repaint();
586 break;
588 case CMD_GO_ROOT_DIRECTORY:
589 switch (mode) {
590 case LIST_ARTISTS:
591 break;
593 case LIST_ALBUMS:
594 case LIST_SONGS:
595 open_artist_list(c);
596 list_window_reset(browser.lw);
597 /* restore first list window state (pop while returning true) */
598 /* XXX */
599 break;
600 }
602 artist_repaint();
603 break;
605 case CMD_SELECT:
606 case CMD_ADD:
607 switch(mode) {
608 case LIST_ARTISTS:
609 selected = g_ptr_array_index(artist_list,
610 browser.lw->selected);
611 add_query(c, MPD_TABLE_ARTIST, selected);
612 cmd = CMD_LIST_NEXT; /* continue and select next item... */
613 break;
615 case LIST_ALBUMS:
616 if (browser.lw->selected == album_list->len + 1)
617 add_query(c, MPD_TABLE_ARTIST, artist);
618 else if (browser.lw->selected > 0) {
619 selected = g_ptr_array_index(album_list,
620 browser.lw->selected - 1);
621 add_query(c, MPD_TABLE_ALBUM, selected);
622 cmd = CMD_LIST_NEXT; /* continue and select next item... */
623 }
624 break;
626 case LIST_SONGS:
627 /* handled by browser_cmd() */
628 break;
629 }
630 break;
632 /* continue and update... */
633 case CMD_SCREEN_UPDATE:
634 reload_lists(c);
635 screen_status_printf(_("Screen updated!"));
636 return 0;
638 case CMD_LIST_FIND:
639 case CMD_LIST_RFIND:
640 case CMD_LIST_FIND_NEXT:
641 case CMD_LIST_RFIND_NEXT:
642 switch (mode) {
643 case LIST_ARTISTS:
644 screen_find(browser.lw, artist_list->len,
645 cmd, artist_lw_callback, artist_list);
646 artist_repaint();
647 return 1;
649 case LIST_ALBUMS:
650 screen_find(browser.lw, album_list->len + 2,
651 cmd, artist_lw_callback, album_list);
652 artist_repaint();
653 return 1;
655 case LIST_SONGS:
656 /* handled by browser_cmd() */
657 break;
658 }
660 break;
662 default:
663 break;
664 }
666 if (artist_lw_cmd(c, cmd)) {
667 artist_repaint();
668 return 1;
669 }
671 return 0;
672 }
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 };