X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fscreen_search.c;h=81ca8e4840c3fe7e9a71e753575146b152c78fad;hb=a023de1cdf0ebcd19b1236c4132175402fc7d4b6;hp=235eeeecccec1749c84291e762fadae5db437707;hpb=b512802eb2babc879a109d16ce4a32b2fb1c22ef;p=ncmpc.git diff --git a/src/screen_search.c b/src/screen_search.c index 235eeee..81ca8e4 100644 --- a/src/screen_search.c +++ b/src/screen_search.c @@ -1,5 +1,6 @@ -/* - * (c) 2004 by Kalle Wallin +/* ncmpc (Ncurses MPD Client) + * (c) 2004-2010 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 @@ -10,51 +11,60 @@ * 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_search.h" +#include "screen_interface.h" +#include "screen_status.h" +#include "screen.h" #include "i18n.h" #include "options.h" #include "charset.h" #include "mpdclient.h" #include "strfsong.h" -#include "command.h" -#include "screen.h" #include "utils.h" #include "screen_utils.h" #include "screen_browser.h" +#include "filelist.h" -#include -#include #include #include +enum { + SEARCH_URI = MPD_TAG_COUNT + 100, + SEARCH_ARTIST_TITLE +}; + static const struct { const char *name; const char *localname; -} search_tag[MPD_TAG_NUM_OF_ITEM_TYPES] = { - [MPD_TAG_ITEM_ARTIST] = { "artist", N_("artist") }, - [MPD_TAG_ITEM_ALBUM] = { "album", N_("album") }, - [MPD_TAG_ITEM_TITLE] = { "title", N_("title") }, - [MPD_TAG_ITEM_TRACK] = { "track", N_("track") }, - [MPD_TAG_ITEM_NAME] = { "name", N_("name") }, - [MPD_TAG_ITEM_GENRE] = { "genre", N_("genre") }, - [MPD_TAG_ITEM_DATE] = { "date", N_("date") }, - [MPD_TAG_ITEM_COMPOSER] = { "composer", N_("composer") }, - [MPD_TAG_ITEM_PERFORMER] = { "performer", N_("performer") }, - [MPD_TAG_ITEM_COMMENT] = { "comment", N_("comment") }, - [MPD_TAG_ITEM_FILENAME] = { "filename", N_("file") }, +} search_tag[MPD_TAG_COUNT] = { + [MPD_TAG_ARTIST] = { "artist", N_("artist") }, + [MPD_TAG_ALBUM] = { "album", N_("album") }, + [MPD_TAG_TITLE] = { "title", N_("title") }, + [MPD_TAG_TRACK] = { "track", N_("track") }, + [MPD_TAG_NAME] = { "name", N_("name") }, + [MPD_TAG_GENRE] = { "genre", N_("genre") }, + [MPD_TAG_DATE] = { "date", N_("date") }, + [MPD_TAG_COMPOSER] = { "composer", N_("composer") }, + [MPD_TAG_PERFORMER] = { "performer", N_("performer") }, + [MPD_TAG_COMMENT] = { "comment", N_("comment") }, }; static int -search_get_tag_id(char *name) +search_get_tag_id(const char *name) { unsigned i; - for (i = 0; i < MPD_TAG_NUM_OF_ITEM_TYPES; ++i) + if (g_ascii_strcasecmp(name, "file") == 0 || + strcasecmp(name, _("file")) == 0) + return SEARCH_URI; + + for (i = 0; i < MPD_TAG_COUNT; ++i) if (search_tag[i].name != NULL && (strcasecmp(search_tag[i].name, name) == 0 || strcasecmp(search_tag[i].localname, name) == 0)) @@ -63,23 +73,16 @@ search_get_tag_id(char *name) return -1; } -#define SEARCH_TITLE 0 -#define SEARCH_ARTIST 1 -#define SEARCH_ALBUM 2 -#define SEARCH_FILE 3 - -#define SEARCH_ARTIST_TITLE 999 - typedef struct { - int table; + enum mpd_tag_type table; const char *label; } search_type_t; static search_type_t mode[] = { - { MPD_TABLE_TITLE, N_("Title") }, - { MPD_TABLE_ARTIST, N_("Artist") }, - { MPD_TABLE_ALBUM, N_("Album") }, - { MPD_TABLE_FILENAME, N_("Filename") }, + { MPD_TAG_TITLE, N_("Title") }, + { MPD_TAG_ARTIST, N_("Artist") }, + { MPD_TAG_ALBUM, N_("Album") }, + { SEARCH_URI, N_("Filename") }, { SEARCH_ARTIST_TITLE, N_("Artist + Title") }, { 0, NULL } }; @@ -90,60 +93,37 @@ static gboolean advanced_search_mode = FALSE; static struct screen_browser browser; +static const char *const help_text[] = { + "Quick - Enter a string and ncmpc will search according", + " to the current search mode (displayed above).", + "", + "Advanced - : [:...]", + " Example: artist:radiohead album:pablo honey", + "", + " Available tags: artist, album, title, track,", + " name, genre, date composer, performer, comment, file", + "", +}; /* search info */ static const char * -lw_search_help_callback(unsigned idx, G_GNUC_UNUSED bool *highlight, - G_GNUC_UNUSED void *data) +lw_search_help_callback(unsigned idx, G_GNUC_UNUSED void *data) { - unsigned text_rows; - static const char *text[] = { - "Quick - just enter a string and ncmpc will search according", - " to the current search mode (displayed above).", - "", - "Advanced - : [:...]", - " Example: artist:radiohead album:pablo honey", - "", - " avalible tags: artist, album, title, track,", - " name, genre, date composer, performer, comment, file", - "", - NULL - }; - - text_rows=0; - while (text[text_rows]) - text_rows++; - - if (idx < text_rows) - return text[idx]; - return NULL; + assert(idx < G_N_ELEMENTS(help_text)); + + return help_text[idx]; } static void -paint(void); +screen_search_paint(void); static void search_repaint(void) { - paint(); + screen_search_paint(); wrefresh(browser.lw->w); } -static void -search_repaint_if_active(void) -{ - if (screen_is_visible(&screen_search)) - search_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); - search_repaint_if_active(); -} - /* sanity check search mode value */ static void search_check_mode(void) @@ -159,13 +139,12 @@ search_check_mode(void) } static void -search_clear(mpdclient_t *c, - gboolean clear_pattern) +search_clear(bool clear_pattern) { if (browser.filelist) { - mpdclient_remove_playlist_callback(c, playlist_changed_callback); filelist_free(browser.filelist); - browser.filelist = filelist_new(NULL); + browser.filelist = filelist_new(); + list_window_set_length(browser.lw, 0); } if (clear_pattern && pattern) { g_free(pattern); @@ -173,31 +152,44 @@ search_clear(mpdclient_t *c, } } -static mpdclient_filelist_t * -filelist_search(mpdclient_t *c, G_GNUC_UNUSED int exact_match, int table, - gchar *local_pattern) +static struct filelist * +search_simple_query(struct mpd_connection *connection, bool exact_match, + int table, gchar *local_pattern) { - mpdclient_filelist_t *list, *list2; + struct filelist *list; gchar *filter_utf8 = locale_to_utf8(local_pattern); if (table == SEARCH_ARTIST_TITLE) { - list = mpdclient_filelist_search(c, FALSE, MPD_TABLE_ARTIST, - filter_utf8); - if (list == NULL) - list = filelist_new(NULL); - - list2 = mpdclient_filelist_search(c, FALSE, MPD_TABLE_TITLE, - filter_utf8); - if (list2 != NULL) { - filelist_move(list, list2); - filelist_free(list2); - } + mpd_command_list_begin(connection, false); + + mpd_search_db_songs(connection, exact_match); + mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT, + MPD_TAG_ARTIST, filter_utf8); + mpd_search_commit(connection); + + mpd_search_db_songs(connection, exact_match); + mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT, + MPD_TAG_TITLE, filter_utf8); + mpd_search_commit(connection); + + mpd_command_list_end(connection); + + list = filelist_new_recv(connection); + filelist_no_duplicates(list); + } else if (table == SEARCH_URI) { + mpd_search_db_songs(connection, exact_match); + mpd_search_add_uri_constraint(connection, MPD_OPERATOR_DEFAULT, + filter_utf8); + mpd_search_commit(connection); - filelist_sort(list, compare_filelistentry_format); + list = filelist_new_recv(connection); } else { - list = mpdclient_filelist_search(c, FALSE, table, filter_utf8); - if (list == NULL) - list = filelist_new(NULL); + mpd_search_db_songs(connection, exact_match); + mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT, + table, filter_utf8); + mpd_search_commit(connection); + + list = filelist_new_recv(connection); } g_free(filter_utf8); @@ -209,26 +201,21 @@ filelist_search(mpdclient_t *c, G_GNUC_UNUSED int exact_match, int table, * Its ugly and MUST be redesigned before the next release! *----------------------------------------------------------------------- */ -static mpdclient_filelist_t * -search_advanced_query(char *query, mpdclient_t *c) +static struct filelist * +search_advanced_query(struct mpd_connection *connection, char *query) { int i,j; char **strv; int table[10]; char *arg[10]; - mpdclient_filelist_t *fl = NULL; + struct filelist *fl = NULL; advanced_search_mode = FALSE; - if( g_strrstr(query, ":") == NULL ) + if (strchr(query, ':') == NULL) return NULL; strv = g_strsplit_set(query, ": ", 0); - i=0; - while (strv[i]) { - i++; - } - memset(table, 0, 10*sizeof(int)); memset(arg, 0, 10*sizeof(char *)); @@ -263,90 +250,118 @@ search_advanced_query(char *query, mpdclient_t *c) g_strfreev(strv); + if (!advanced_search_mode || j == 0) { + for (i = 0; arg[i] != NULL; ++i) + g_free(arg[i]); + return NULL; + } - if (advanced_search_mode && j > 0) { - int iter; - mpd_InfoEntity *entity; + /*----------------------------------------------------------------------- + * NOTE (again): This code exists to test a new search ui, + * Its ugly and MUST be redesigned before the next release! + * + the code below should live in mpdclient.c + *----------------------------------------------------------------------- + */ + /** stupid - but this is just a test...... (fulhack) */ + mpd_search_db_songs(connection, false); + + for (i = 0; i < 10 && arg[i] != NULL; i++) { + if (table[i] == SEARCH_URI) + mpd_search_add_uri_constraint(connection, + MPD_OPERATOR_DEFAULT, + arg[i]); + else + mpd_search_add_tag_constraint(connection, + MPD_OPERATOR_DEFAULT, + table[i], arg[i]); + } - /*----------------------------------------------------------------------- - * NOTE (again): This code exists to test a new search ui, - * Its ugly and MUST be redesigned before the next release! - * + the code below should live in mpdclient.c - *----------------------------------------------------------------------- - */ - /** stupid - but this is just a test...... (fulhack) */ - mpd_startSearch(c->connection, FALSE); + mpd_search_commit(connection); + fl = filelist_new_recv(connection); + if (!mpd_response_finish(connection)) { + filelist_free(fl); + fl = NULL; + } - for(iter = 0; iter < 10; iter++) { - mpd_addConstraintSearch(c->connection, table[iter], arg[iter]); - } + for (i = 0; arg[i] != NULL; ++i) + g_free(arg[i]); - mpd_commitSearch(c->connection); + return fl; +} - fl = g_malloc0(sizeof(mpdclient_filelist_t)); +static struct filelist * +do_search(struct mpdclient *c, char *query) +{ + struct mpd_connection *connection = mpdclient_get_connection(c); + struct filelist *fl; - while ((entity=mpd_getNextInfoEntity(c->connection))) - filelist_append(fl, entity); + fl = search_advanced_query(connection, query); + if (fl != NULL) + return fl; - if (mpdclient_finish_command(c) && fl) - filelist_free(fl); + if (mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS) { + mpdclient_handle_error(c); + return NULL; } - i=0; - while( arg[i] ) - g_free(arg[i++]); - + fl = search_simple_query(connection, FALSE, + mode[options.search_mode].table, + query); + if (fl == NULL) + mpdclient_handle_error(c); return fl; } static void -search_new(mpdclient_t *c) +screen_search_reload(struct mpdclient *c) { - search_clear(c, TRUE); - - g_free(pattern); - pattern = screen_readln(screen.status_window.w, - _("Search: "), - NULL, - &search_history, - NULL); - - if (pattern == NULL) { - list_window_reset(browser.lw); + if (pattern == NULL) return; - } if (browser.filelist != NULL) { filelist_free(browser.filelist); browser.filelist = NULL; } - if (!MPD_VERSION_LT(c, 0, 12, 0)) - browser.filelist = search_advanced_query(pattern, c); - - if (!advanced_search_mode && browser.filelist == NULL) - browser.filelist = filelist_search(c, FALSE, - mode[options.search_mode].table, - pattern); - + browser.filelist = do_search(c, pattern); if (browser.filelist == NULL) - browser.filelist = filelist_new(NULL); + browser.filelist = filelist_new(); + list_window_set_length(browser.lw, filelist_length(browser.filelist)); - sync_highlights(c, browser.filelist); - mpdclient_install_playlist_callback(c, playlist_changed_callback); - list_window_check_selected(browser.lw, filelist_length(browser.filelist)); + screen_browser_sync_highlights(browser.filelist, &c->playlist); } +static void +search_new(struct mpdclient *c) +{ + if (!mpdclient_is_connected(c)) + return; + search_clear(true); + + g_free(pattern); + pattern = screen_readln(_("Search"), + NULL, + &search_history, + NULL); + + if (pattern == NULL) { + list_window_reset(browser.lw); + return; + } + + screen_search_reload(c); +} static void -init(WINDOW *w, int cols, int rows) +screen_search_init(WINDOW *w, int cols, int rows) { browser.lw = list_window_init(w, cols, rows); + list_window_set_length(browser.lw, G_N_ELEMENTS(help_text)); } static void -quit(void) +screen_search_quit(void) { if (search_history) string_list_free(search_history); @@ -361,29 +376,28 @@ quit(void) } static void -open(G_GNUC_UNUSED mpdclient_t *c) +screen_search_open(G_GNUC_UNUSED struct mpdclient *c) { // if( pattern==NULL ) // search_new(screen, c); // else screen_status_printf(_("Press %s for a new search"), - get_key_names(CMD_SCREEN_SEARCH,0)); + get_key_names(CMD_SCREEN_SEARCH, false)); search_check_mode(); } static void -resize(int cols, int rows) +screen_search_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_search_paint(void) { if (browser.filelist) { browser.lw->hide_cursor = false; - list_window_paint(browser.lw, browser_lw_callback, browser.filelist); + screen_browser_paint(&browser); } else { browser.lw->hide_cursor = true; list_window_paint(browser.lw, lw_search_help_callback, NULL); @@ -391,7 +405,7 @@ paint(void) } static const char * -get_title(char *str, size_t size) +screen_search_get_title(char *str, size_t size) { if (advanced_search_mode && pattern) g_snprintf(str, size, _("Search: %s"), pattern); @@ -402,14 +416,23 @@ get_title(char *str, size_t size) _(mode[options.search_mode].label)); else g_snprintf(str, size, _("Search: Press %s for a new search [%s]"), - get_key_names(CMD_SCREEN_SEARCH,0), + get_key_names(CMD_SCREEN_SEARCH, false), _(mode[options.search_mode].label)); return str; } +static void +screen_search_update(struct mpdclient *c) +{ + if (browser.filelist != NULL && c->events & MPD_IDLE_QUEUE) { + screen_browser_sync_highlights(browser.filelist, &c->playlist); + search_repaint(); + } +} + static bool -search_cmd(mpdclient_t *c, command_t cmd) +screen_search_cmd(struct mpdclient *c, command_t cmd) { switch (cmd) { case CMD_SEARCH_MODE: @@ -420,14 +443,7 @@ search_cmd(mpdclient_t *c, command_t cmd) _(mode[options.search_mode].label)); /* continue and update... */ case CMD_SCREEN_UPDATE: - if (pattern) { - search_clear(c, FALSE); - browser.filelist = filelist_search(c, - FALSE, - mode[options.search_mode].table, - pattern); - sync_highlights(c, browser.filelist); - } + screen_search_reload(c); search_repaint(); return true; @@ -437,7 +453,7 @@ search_cmd(mpdclient_t *c, command_t cmd) return true; case CMD_CLEAR: - search_clear(c, TRUE); + search_clear(true); list_window_reset(browser.lw); search_repaint(); return true; @@ -457,11 +473,12 @@ search_cmd(mpdclient_t *c, command_t cmd) } const struct screen_functions screen_search = { - .init = init, - .exit = quit, - .open = open, - .resize = resize, - .paint = paint, - .cmd = search_cmd, - .get_title = get_title, + .init = screen_search_init, + .exit = screen_search_quit, + .open = screen_search_open, + .resize = screen_search_resize, + .paint = screen_search_paint, + .update = screen_search_update, + .cmd = screen_search_cmd, + .get_title = screen_search_get_title, };