diff --git a/src/screen_artist.c b/src/screen_artist.c
index 2c228834e4227dbcbd0fdf99c94d4c5774dc05ce..d6011a7bc13d078cb1c2feffa96efcf580c08f67 100644 (file)
--- a/src/screen_artist.c
+++ b/src/screen_artist.c
-/*
- * (c) 2005 by Kalle Wallin <kaw@linux.se>
- *
+/* ncmpc (Ncurses MPD Client)
+ * (c) 2004-2009 The Music Player Daemon Project
+ * Project homepage: http://musicpd.org
+
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
- *
+
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- */
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include "screen_artist.h"
+#include "screen_interface.h"
+#include "screen_message.h"
+#include "screen_find.h"
+#include "screen.h"
#include "i18n.h"
-#include "options.h"
#include "charset.h"
#include "mpdclient.h"
-#include "utils.h"
-#include "strfsong.h"
-#include "command.h"
-#include "screen.h"
-#include "screen_utils.h"
#include "screen_browser.h"
+#include "filelist.h"
-#include <ctype.h>
-#include <stdlib.h>
+#include <assert.h>
#include <string.h>
#include <glib.h>
static gint
compare_utf8(gconstpointer s1, gconstpointer s2)
{
+ const char *const*t1 = s1, *const*t2 = s2;
char *key1, *key2;
int n;
- key1 = g_utf8_collate_key(s1,-1);
- key2 = g_utf8_collate_key(s2,-1);
+ key1 = g_utf8_collate_key(*t1,-1);
+ key2 = g_utf8_collate_key(*t2,-1);
n = strcmp(key1,key2);
g_free(key1);
g_free(key2);
/* list_window callback */
static const char *
-artist_lw_callback(unsigned idx, G_GNUC_UNUSED int *highlight,
- G_GNUC_UNUSED void *data)
+screen_artist_lw_callback(unsigned idx, void *data)
{
GPtrArray *list = data;
static char buf[BUFSIZE];
--idx;
}
- if (idx >= list->len)
- return NULL;
+ assert(idx < list->len);
str_utf8 = g_ptr_array_index(list, idx);
assert(str_utf8 != NULL);
}
static void
-paint(void);
+screen_artist_paint(void);
static void
artist_repaint(void)
{
- paint();
+ screen_artist_paint();
wrefresh(browser.lw->w);
}
-static void
-artist_repaint_if_active(void)
-{
- if (screen_is_visible(&screen_artist))
- artist_repaint();
-}
-
-/* the playlist have been updated -> fix highlights */
-static void
-playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
-{
- browser_playlist_changed(&browser, c, event, data);
-
- artist_repaint_if_active();
-}
-
-static GPtrArray *
-g_list_to_ptr_array(GList *in)
-{
- GPtrArray *out = g_ptr_array_sized_new(g_list_length(in));
- GList *head = in;
-
- while (in != NULL) {
- g_ptr_array_add(out, in->data);
- in = g_list_next(in);
- }
-
- g_list_free(head);
- return out;
-}
-
static void
string_array_free(GPtrArray *array)
{
for (i = 0; i < array->len; ++i) {
char *value = g_ptr_array_index(array, i);
- free(value);
+ g_free(value);
}
g_ptr_array_free(array, TRUE);
}
static void
-free_lists(struct mpdclient *c)
+free_lists(void)
{
if (artist_list != NULL) {
string_array_free(artist_list);
}
if (browser.filelist) {
- if (c != NULL)
- mpdclient_remove_playlist_callback(c, playlist_changed_callback);
filelist_free(browser.filelist);
browser.filelist = NULL;
}
}
+static void
+recv_tag_values(struct mpd_connection *connection, enum mpd_tag_type tag,
+ GPtrArray *list)
+{
+ struct mpd_pair *pair;
+
+ while ((pair = mpd_recv_pair_tag(connection, tag)) != NULL) {
+ g_ptr_array_add(list, g_strdup(pair->value));
+ mpd_return_pair(connection, pair);
+ }
+}
+
static void
load_artist_list(struct mpdclient *c)
{
- GList *list;
+ struct mpd_connection *connection = mpdclient_get_connection(c);
assert(mode == LIST_ARTISTS);
assert(artist == NULL);
assert(album_list == NULL);
assert(browser.filelist == NULL);
- list = mpdclient_get_artists(c);
- /* sort list */
- list = g_list_sort(list, compare_utf8);
+ artist_list = g_ptr_array_new();
+
+ if (connection != NULL) {
+ mpd_search_db_tags(connection, MPD_TAG_ARTIST);
+ mpd_search_commit(connection);
+ recv_tag_values(connection, MPD_TAG_ARTIST, artist_list);
- artist_list = g_list_to_ptr_array(list);
+ if (!mpd_response_finish(connection))
+ mpdclient_handle_error(c);
+ }
+
+ /* sort list */
+ g_ptr_array_sort(artist_list, compare_utf8);
+ list_window_set_length(browser.lw, artist_list->len);
}
static void
load_album_list(struct mpdclient *c)
{
- GList *list;
+ struct mpd_connection *connection = mpdclient_get_connection(c);
assert(mode == LIST_ALBUMS);
assert(artist != NULL);
assert(album_list == NULL);
assert(browser.filelist == NULL);
- list = mpdclient_get_albums(c, artist);
+ album_list = g_ptr_array_new();
+
+ if (connection != NULL) {
+ mpd_search_db_tags(connection, MPD_TAG_ALBUM);
+ mpd_search_add_tag_constraint(connection,
+ MPD_OPERATOR_DEFAULT,
+ MPD_TAG_ARTIST, artist);
+ mpd_search_commit(connection);
+
+ recv_tag_values(connection, MPD_TAG_ALBUM, album_list);
+
+ if (!mpd_response_finish(connection))
+ mpdclient_handle_error(c);
+ }
+
/* sort list */
- list = g_list_sort(list, compare_utf8);
+ g_ptr_array_sort(album_list, compare_utf8);
- album_list = g_list_to_ptr_array(list);
+ list_window_set_length(browser.lw, album_list->len + 2);
}
static void
load_song_list(struct mpdclient *c)
{
+ struct mpd_connection *connection = mpdclient_get_connection(c);
+
assert(mode == LIST_SONGS);
assert(artist != NULL);
assert(album != NULL);
assert(browser.filelist == NULL);
- if (album[0] == 0)
- browser.filelist =
- mpdclient_filelist_search(c, TRUE,
- MPD_TABLE_ARTIST,
- artist);
- else
- browser.filelist =
- mpdclient_filelist_search(c, TRUE,
- MPD_TABLE_ALBUM,
- album);
- if (browser.filelist == NULL)
- browser.filelist = filelist_new(NULL);
-
+ browser.filelist = filelist_new();
/* add a dummy entry for ".." */
- filelist_prepend(browser.filelist, NULL);
+ filelist_append(browser.filelist, NULL);
+
+ if (connection != NULL) {
+ mpd_search_db_songs(connection, true);
+ mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
+ MPD_TAG_ARTIST, artist);
+ if (album[0] != 0)
+ mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
+ MPD_TAG_ALBUM, album);
+ mpd_search_commit(connection);
+
+ filelist_recv(browser.filelist, connection);
+
+ if (!mpd_response_finish(connection))
+ mpdclient_handle_error(c);
+ }
- /* install playlist callback and fix highlights */
- sync_highlights(c, browser.filelist);
- mpdclient_install_playlist_callback(c, playlist_changed_callback);
+ /* fix highlights */
+ screen_browser_sync_highlights(browser.filelist, &c->playlist);
+ list_window_set_length(browser.lw, filelist_length(browser.filelist));
}
static void
-free_state(struct mpdclient *c)
+free_state(void)
{
g_free(artist);
g_free(album);
artist = NULL;
album = NULL;
- free_lists(c);
+ free_lists();
}
static void
open_artist_list(struct mpdclient *c)
{
- free_state(c);
+ free_state();
mode = LIST_ARTISTS;
load_artist_list(c);
{
assert(_artist != NULL);
- free_state(c);
+ free_state();
mode = LIST_ALBUMS;
artist = _artist;
assert(_artist != NULL);
assert(_album != NULL);
- free_state(c);
+ free_state();
mode = LIST_SONGS;
artist = _artist;
static void
reload_lists(struct mpdclient *c)
{
- free_lists(c);
+ free_lists();
switch (mode) {
case LIST_ARTISTS:
}
}
-/* db updated */
-static void
-browse_callback(mpdclient_t *c, int event, G_GNUC_UNUSED gpointer data)
-{
- switch(event) {
- case BROWSE_DB_UPDATED:
- reload_lists(c);
- break;
- default:
- break;
- }
-
- artist_repaint_if_active();
-}
-
static void
-init(WINDOW *w, int cols, int rows)
+screen_artist_init(WINDOW *w, int cols, int rows)
{
browser.lw = list_window_init(w, cols, rows);
artist = NULL;
}
static void
-quit(void)
+screen_artist_quit(void)
{
- free_state(NULL);
+ free_state();
list_window_free(browser.lw);
}
static void
-open(mpdclient_t *c)
+screen_artist_open(struct mpdclient *c)
{
- static gboolean callback_installed = FALSE;
-
if (artist_list == NULL && album_list == NULL &&
browser.filelist == NULL)
reload_lists(c);
- if (!callback_installed) {
- mpdclient_install_browse_callback(c, browse_callback);
- callback_installed = TRUE;
- }
}
static void
-resize(int cols, int rows)
+screen_artist_resize(int cols, int rows)
{
- browser.lw->cols = cols;
- browser.lw->rows = rows;
+ list_window_resize(browser.lw, cols, rows);
}
static void
-paint(void)
+screen_artist_paint(void)
{
if (browser.filelist) {
- list_window_paint(browser.lw, browser_lw_callback,
- browser.filelist);
+ screen_browser_paint(&browser);
} else if (album_list != NULL)
- list_window_paint(browser.lw, artist_lw_callback, album_list);
+ list_window_paint(browser.lw, screen_artist_lw_callback,
+ album_list);
else if (artist_list != NULL)
- list_window_paint(browser.lw, artist_lw_callback, artist_list);
+ list_window_paint(browser.lw, screen_artist_lw_callback,
+ artist_list);
else {
wmove(browser.lw->w, 0, 0);
wclrtobot(browser.lw->w);
}
static const char *
-get_title(char *str, size_t size)
+screen_artist_get_title(char *str, size_t size)
{
char *s1, *s2;
}
static void
-add_query(mpdclient_t *c, int table, char *_filter)
+screen_artist_update(struct mpdclient *c)
{
+ if (browser.filelist == NULL)
+ return;
+
+ if (c->events & MPD_IDLE_DATABASE)
+ /* the db has changed -> update the list */
+ reload_lists(c);
+
+ if (c->events & (MPD_IDLE_DATABASE | MPD_IDLE_QUEUE))
+ screen_browser_sync_highlights(browser.filelist, &c->playlist);
+
+ if (c->events & (MPD_IDLE_DATABASE
+#ifndef NCMPC_MINI
+ | MPD_IDLE_QUEUE
+#endif
+ ))
+ artist_repaint();
+}
+
+static void
+add_query(struct mpdclient *c, enum mpd_tag_type table, char *_filter)
+{
+ struct mpd_connection *connection = mpdclient_get_connection(c);
char *str;
- mpdclient_filelist_t *addlist;
+ struct filelist *addlist;
assert(filter != NULL);
+ if (connection == NULL)
+ return;
+
str = utf8_to_locale(_filter);
- if (table== MPD_TABLE_ALBUM)
+ if (table == MPD_TAG_ALBUM)
screen_status_printf("Adding album %s...", str);
else
screen_status_printf("Adding %s...", str);
g_free(str);
- addlist = mpdclient_filelist_search(c, TRUE, table, _filter);
- if (addlist) {
- mpdclient_filelist_add_all(c, addlist);
- filelist_free(addlist);
- }
-}
+ mpd_search_db_songs(connection, true);
+ mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
+ table, _filter);
+ mpd_search_commit(connection);
-static unsigned
-metalist_length(void)
-{
- assert(mode != LIST_ARTISTS || artist_list != NULL);
- assert(mode != LIST_ALBUMS || album_list != NULL);
+ addlist = filelist_new_recv(connection);
+
+ if (mpd_response_finish(connection))
+ mpdclient_filelist_add_all(c, addlist);
+ else
+ mpdclient_handle_error(c);
- return mode == LIST_ALBUMS
- ? album_list->len + 2
- : artist_list->len;
+ filelist_free(addlist);
}
static int
-artist_lw_cmd(struct mpdclient *c, command_t cmd)
+screen_artist_lw_cmd(struct mpdclient *c, command_t cmd)
{
switch (mode) {
case LIST_ARTISTS:
case LIST_ALBUMS:
- return list_window_cmd(browser.lw, metalist_length(), cmd);
+ return list_window_cmd(browser.lw, cmd);
case LIST_SONGS:
return browser_cmd(&browser, c, cmd);
}
static bool
-artist_cmd(mpdclient_t *c, command_t cmd)
+screen_artist_cmd(struct mpdclient *c, command_t cmd)
{
+ struct list_window_range range;
char *selected;
char *old;
int idx;
case CMD_PLAY:
switch (mode) {
case LIST_ARTISTS:
+ if (browser.lw->selected >= artist_list->len)
+ return true;
+
selected = g_ptr_array_index(artist_list,
browser.lw->selected);
open_album_list(c, g_strdup(selected));
g_free(old);
if (idx >= 0) {
- list_window_set_selected(browser.lw, idx);
- list_window_center(browser.lw,
- artist_list->len, idx);
+ list_window_set_cursor(browser.lw, idx);
+ list_window_center(browser.lw, idx);
}
} else if (browser.lw->selected == album_list->len + 1) {
/* handle "show all" */
if (idx >= 0) {
++idx;
- list_window_set_selected(browser.lw, idx);
- list_window_center(browser.lw,
- album_list->len, idx);
+ list_window_set_cursor(browser.lw, idx);
+ list_window_center(browser.lw, idx);
}
artist_repaint();
g_free(old);
if (idx >= 0) {
- list_window_set_selected(browser.lw, idx);
- list_window_center(browser.lw,
- artist_list->len, idx);
+ list_window_set_cursor(browser.lw, idx);
+ list_window_center(browser.lw, idx);
}
break;
if (idx >= 0) {
++idx;
- list_window_set_selected(browser.lw, idx);
- list_window_center(browser.lw,
- album_list->len, idx);
+ list_window_set_cursor(browser.lw, idx);
+ list_window_center(browser.lw, idx);
}
break;
}
case CMD_ADD:
switch(mode) {
case LIST_ARTISTS:
- selected = g_ptr_array_index(artist_list,
- browser.lw->selected);
- add_query(c, MPD_TABLE_ARTIST, selected);
- cmd = CMD_LIST_NEXT; /* continue and select next item... */
+ if (browser.lw->selected >= artist_list->len)
+ return true;
+
+ list_window_get_range(browser.lw, &range);
+ for (unsigned i = range.start; i < range.end; ++i) {
+ selected = g_ptr_array_index(artist_list, i);
+ add_query(c, MPD_TAG_ARTIST, selected);
+ cmd = CMD_LIST_NEXT; /* continue and select next item... */
+ }
break;
case LIST_ALBUMS:
- if (browser.lw->selected == album_list->len + 1)
- add_query(c, MPD_TABLE_ARTIST, artist);
- else if (browser.lw->selected > 0) {
- selected = g_ptr_array_index(album_list,
- browser.lw->selected - 1);
- add_query(c, MPD_TABLE_ALBUM, selected);
- cmd = CMD_LIST_NEXT; /* continue and select next item... */
+ list_window_get_range(browser.lw, &range);
+ for (unsigned i = range.start; i < range.end; ++i) {
+ if(i == album_list->len + 1)
+ add_query(c, MPD_TAG_ARTIST, artist);
+ else if (i > 0)
+ {
+ selected = g_ptr_array_index(album_list,
+ browser.lw->selected - 1);
+ add_query(c, MPD_TAG_ALBUM, selected);
+ cmd = CMD_LIST_NEXT; /* continue and select next item... */
+ }
}
break;
case CMD_LIST_RFIND_NEXT:
switch (mode) {
case LIST_ARTISTS:
- screen_find(browser.lw, artist_list->len,
- cmd, artist_lw_callback, artist_list);
+ screen_find(browser.lw, cmd,
+ screen_artist_lw_callback, artist_list);
+ artist_repaint();
+ return true;
+
+ case LIST_ALBUMS:
+ screen_find(browser.lw, cmd,
+ screen_artist_lw_callback, album_list);
+ artist_repaint();
+ return true;
+
+ case LIST_SONGS:
+ /* handled by browser_cmd() */
+ break;
+ }
+ case CMD_LIST_JUMP:
+ switch (mode) {
+ case LIST_ARTISTS:
+ screen_jump(browser.lw, screen_artist_lw_callback,
+ NULL, artist_list);
artist_repaint();
return true;
case LIST_ALBUMS:
- screen_find(browser.lw, album_list->len + 2,
- cmd, artist_lw_callback, album_list);
+ screen_jump(browser.lw, screen_artist_lw_callback,
+ NULL, album_list);
artist_repaint();
return true;
break;
}
- if (artist_lw_cmd(c, cmd)) {
+ if (screen_artist_lw_cmd(c, cmd)) {
if (screen_is_visible(&screen_artist))
artist_repaint();
return true;
}
const struct screen_functions screen_artist = {
- .init = init,
- .exit = quit,
- .open = open,
- .resize = resize,
- .paint = paint,
- .cmd = artist_cmd,
- .get_title = get_title,
+ .init = screen_artist_init,
+ .exit = screen_artist_quit,
+ .open = screen_artist_open,
+ .resize = screen_artist_resize,
+ .paint = screen_artist_paint,
+ .update = screen_artist_update,
+ .cmd = screen_artist_cmd,
+ .get_title = screen_artist_get_title,
};