X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fscreen_search.c;h=6af4937da6be9e2ab34543b89563bf673e8b0075;hb=572f9c39c5e0947a95d126f7feedcf2d7d770503;hp=a74ef6092ccedf1669ada7f2a0940e22f97867b5;hpb=76a8fab4adc293982f355609b89dde88d5f355e0;p=ncmpc.git diff --git a/src/screen_search.c b/src/screen_search.c index a74ef60..6af4937 100644 --- a/src/screen_search.c +++ b/src/screen_search.c @@ -1,214 +1,200 @@ -/* - * $Id$ - * - * (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 * 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 - * - */ -#include -#include -#include -#include -#include + * 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 "config.h" -#ifndef DISABLE_SEARCH_SCREEN -#include "ncmpc.h" +#include "screen_search.h" +#include "screen_interface.h" +#include "screen_message.h" +#include "screen.h" +#include "i18n.h" #include "options.h" -#include "support.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_browse.h" - -/* new search stuff with qball's libmpdclient */ -#define FUTURE - +#include "screen_browser.h" +#include "filelist.h" -#ifdef FUTURE +#include +#include -extern gint mpdclient_finish_command(mpdclient_t *c); +enum { + SEARCH_URI = MPD_TAG_COUNT + 100, +}; -typedef struct { - int id; +static const struct { const char *name; const char *localname; -} search_tag_t; - -static search_tag_t search_tag[] = { - { 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") }, - { -1, NULL, NULL } +} 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) { - int i; - - i=0; - while( search_tag[i].name ) - { - if( strcasecmp(search_tag[i].name, name)==0 || - strcasecmp(search_tag[i].localname, name)==0 ) - return search_tag[i].id; - i++; - } - return -1; -} + unsigned i; -#endif + 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)) + return i; -#define SEARCH_TITLE 0 -#define SEARCH_ARTIST 1 -#define SEARCH_ALBUM 2 -#define SEARCH_FILE 3 + return -1; +} #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") }, - { SEARCH_ARTIST_TITLE, N_("Artist + Title") }, - { 0, NULL } + { 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 } }; -static list_window_t *lw = NULL; -static mpdclient_filelist_t *filelist = NULL; static GList *search_history = NULL; static gchar *pattern = NULL; 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(int index, int *highlight, void *data) +lw_search_help_callback(unsigned idx, G_GNUC_UNUSED void *data) { - int 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 (index < text_rows) - return text[index]; - return NULL; + assert(idx < G_N_ELEMENTS(help_text)); + + return help_text[idx]; } -/* the playlist have been updated -> fix highlights */ -static void -playlist_changed_callback(mpdclient_t *c, int event, gpointer data) +static void +screen_search_paint(void); + +static void +search_repaint(void) { - if( filelist==NULL ) - return; - D("screen_search.c> playlist_callback() [%d]\n", event); - switch(event) - { - case PLAYLIST_EVENT_CLEAR: - clear_highlights(filelist); - break; - default: - sync_highlights(c, filelist); - break; - } + screen_search_paint(); + wrefresh(browser.lw->w); } /* sanity check search mode value */ static void search_check_mode(void) { - int max = 0; - - while( mode[max].label != NULL ) - max++; - if( options.search_mode<0 ) - options.search_mode = 0; - else if( options.search_mode>=max ) - options.search_mode = max-1; + int max = 0; + + while (mode[max].label != NULL) + max++; + if (options.search_mode < 0) + options.search_mode = 0; + else if (options.search_mode >= max) + options.search_mode = max-1; } static void -search_clear(screen_t *screen, mpdclient_t *c, gboolean clear_pattern) +search_clear(bool clear_pattern) { - if( filelist ) - { - mpdclient_remove_playlist_callback(c, playlist_changed_callback); - filelist = mpdclient_filelist_free(filelist); - } - if( clear_pattern && pattern ) - { - g_free(pattern); - pattern = NULL; - } + if (browser.filelist) { + filelist_free(browser.filelist); + browser.filelist = filelist_new(); + list_window_set_length(browser.lw, 0); + } + if (clear_pattern && pattern) { + g_free(pattern); + pattern = NULL; + } } -#ifdef FUTURE -static mpdclient_filelist_t * -filelist_search(mpdclient_t *c, int exact_match, int table, gchar *pattern) +static struct filelist * +search_simple_query(struct mpd_connection *connection, bool exact_match, + int table, gchar *local_pattern) { - mpdclient_filelist_t *list, *list2; - - if( table == SEARCH_ARTIST_TITLE ) - { - list = mpdclient_filelist_search(c, FALSE, MPD_TABLE_ARTIST, pattern); - list2 = mpdclient_filelist_search(c, FALSE, MPD_TABLE_TITLE, pattern); - - list->length += list2->length; - list->list = g_list_concat(list->list, list2->list); - list->list = g_list_sort(list->list, compare_filelistentry_format); - list->updated = TRUE; - } - else - { - list = mpdclient_filelist_search(c, FALSE, table, pattern); - } - - return list; + struct filelist *list; + gchar *filter_utf8 = locale_to_utf8(local_pattern); + + if (table == SEARCH_ARTIST_TITLE) { + 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); + + list = filelist_new_recv(connection); + } else { + 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); + return list; } /*----------------------------------------------------------------------- @@ -216,35 +202,27 @@ filelist_search(mpdclient_t *c, int exact_match, int table, gchar *pattern) * 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 *filelist = 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]) { - D("strv[%d] = \"%s\"\n", i, strv[i]); - i++; - } - memset(table, 0, 10*sizeof(int)); memset(arg, 0, 10*sizeof(char *)); i=0; j=0; while (strv[i] && strlen(strv[i]) > 0 && i < 9) { - D("strv[%d] = \"%s\"\n", i, strv[i]); - int id = search_get_tag_id(strv[i]); if (id == -1) { if (table[j]) { @@ -252,12 +230,10 @@ search_advanced_query(char *query, mpdclient_t *c) arg[j] = g_strdup_printf("%s %s", arg[j], strv[i]); g_free(tmp); } else { - D("Bad search tag %s\n", strv[i]); screen_status_printf(_("Bad search tag %s"), strv[i]); } i++; } else if (strv[i+1] == NULL || strlen(strv[i+1]) == 0) { - D("No argument for search tag %s\n", strv[i]); screen_status_printf(_("No argument for search tag %s"), strv[i]); i++; // j--; @@ -275,282 +251,235 @@ 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) { - /*----------------------------------------------------------------------- - * 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); - - int iter; - for(iter = 0; iter < 10; iter++) { - mpd_addConstraintSearch(c->connection, table[iter], arg[iter]); - } - - mpd_commitSearch(c->connection); + /*----------------------------------------------------------------------- + * 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]); + } - filelist = g_malloc0(sizeof(mpdclient_filelist_t)); + mpd_search_commit(connection); + fl = filelist_new_recv(connection); + if (!mpd_response_finish(connection)) { + filelist_free(fl); + fl = NULL; + } - mpd_InfoEntity *entity; + for (i = 0; arg[i] != NULL; ++i) + g_free(arg[i]); - while ((entity=mpd_getNextInfoEntity(c->connection))) { - filelist_entry_t *entry = g_malloc0(sizeof(filelist_entry_t)); + return fl; +} - entry->entity = entity; - filelist->list = g_list_append(filelist->list, (gpointer) entry); - filelist->length++; - } +static struct filelist * +do_search(struct mpdclient *c, char *query) +{ + struct mpd_connection *connection = mpdclient_get_connection(c); + struct filelist *fl; - if (mpdclient_finish_command(c) && filelist) - filelist = mpdclient_filelist_free(filelist); + fl = search_advanced_query(connection, query); + if (fl != NULL) + return fl; - filelist->updated = TRUE; + if (mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS) { + mpdclient_handle_error(c); + return NULL; } - i=0; - while( arg[i] ) - g_free(arg[i++]); - - return filelist; + fl = search_simple_query(connection, FALSE, + mode[options.search_mode].table, + query); + if (fl == NULL) + mpdclient_handle_error(c); + return fl; } -#else -#define search_advanced_query(pattern,c) (NULL) -#endif static void -search_new(screen_t *screen, mpdclient_t *c) +screen_search_reload(struct mpdclient *c) { - search_clear(screen, c, TRUE); - - pattern = screen_readln(screen->status_window.w, - _("Search: "), - NULL, - &search_history, - NULL); - - if( pattern && strcmp(pattern,"")==0 ) - { - g_free(pattern); - pattern=NULL; - } - - if( pattern==NULL ) - { - list_window_reset(lw); - return; - } - - if( !MPD_VERSION_LT(c, 0, 12, 0) ) - filelist = search_advanced_query(pattern, c); - if( !advanced_search_mode && filelist==NULL ) - filelist = filelist_search(c, - FALSE, - mode[options.search_mode].table, - pattern); - sync_highlights(c, filelist); - mpdclient_install_playlist_callback(c, playlist_changed_callback); - list_window_check_selected(lw, filelist->length); -} + if (pattern == NULL) + return; + if (browser.filelist != NULL) { + filelist_free(browser.filelist); + browser.filelist = NULL; + } + browser.filelist = do_search(c, pattern); + if (browser.filelist == NULL) + browser.filelist = filelist_new(); + list_window_set_length(browser.lw, filelist_length(browser.filelist)); -static void -init(WINDOW *w, int cols, int rows) -{ - lw = list_window_init(w, cols, rows); + screen_browser_sync_highlights(browser.filelist, &c->playlist); } static void -quit(void) +search_new(struct mpdclient *c) { - if( search_history ) - string_list_free(search_history); - if( filelist ) - filelist = mpdclient_filelist_free(filelist); - list_window_free(lw); - if( pattern ) - g_free(pattern); - pattern = NULL; + 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 -open(screen_t *screen, mpdclient_t *c) +screen_search_init(WINDOW *w, int cols, int rows) { - // if( pattern==NULL ) - // search_new(screen, c); - // else - screen_status_printf(_("Press %s for a new search"), - get_key_names(CMD_SCREEN_SEARCH,0)); - search_check_mode(); + browser.lw = list_window_init(w, cols, rows); + list_window_set_length(browser.lw, G_N_ELEMENTS(help_text)); } static void -resize(int cols, int rows) +screen_search_quit(void) { - lw->cols = cols; - lw->rows = rows; + if (search_history) + string_list_free(search_history); + if (browser.filelist) + filelist_free(browser.filelist); + list_window_free(browser.lw); + + if (pattern) { + g_free(pattern); + pattern = NULL; + } } static void -close(void) +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)); + search_check_mode(); } -static void -paint(screen_t *screen, mpdclient_t *c) +static void +screen_search_resize(int cols, int rows) { - lw->clear = 1; - - if( filelist ) - { - lw->flags = 0; - list_window_paint(lw, browse_lw_callback, (void *) filelist); - filelist->updated = FALSE; - } - else - { - lw->flags = LW_HIDE_CURSOR; - list_window_paint(lw, lw_search_help_callback, NULL); - if( !MPD_VERSION_LT(c, 0, 12, 0) ) - g_strdup_printf("Advanced search disabled (MPD version < 0.12.0"); - // wmove(lw->w, 0, 0); - //wclrtobot(lw->w); - } - wnoutrefresh(lw->w); + list_window_resize(browser.lw, cols, rows); } -static void -update(screen_t *screen, mpdclient_t *c) +static void +screen_search_paint(void) { - if( filelist==NULL || filelist->updated ) - { - paint(screen, c); - return; - } - list_window_paint(lw, browse_lw_callback, (void *) filelist); - wnoutrefresh(lw->w); + if (browser.filelist) { + browser.lw->hide_cursor = false; + screen_browser_paint(&browser); + } else { + browser.lw->hide_cursor = true; + list_window_paint(browser.lw, lw_search_help_callback, NULL); + } } 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); - else if( pattern ) - g_snprintf(str, size, - _("Search: Results for %s [%s]"), - pattern, - _(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), - _(mode[options.search_mode].label)); - - return str; + if (advanced_search_mode && pattern) + g_snprintf(str, size, _("Search: %s"), pattern); + else if (pattern) + g_snprintf(str, size, + _("Search: Results for %s [%s]"), + pattern, + _(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), + _(mode[options.search_mode].label)); + + return str; } -static list_window_t * -get_filelist_window(void) +static void +screen_search_update(struct mpdclient *c) { - return lw; + if (browser.filelist != NULL && c->events & MPD_IDLE_QUEUE) { + screen_browser_sync_highlights(browser.filelist, &c->playlist); + search_repaint(); + } } -static int -search_cmd(screen_t *screen, mpdclient_t *c, command_t cmd) +static bool +screen_search_cmd(struct mpdclient *c, command_t cmd) { - switch(cmd) - { - case CMD_PLAY: - browse_handle_enter(screen, c, lw, filelist); - return 1; - - case CMD_SELECT: - if( browse_handle_select(screen, c, lw, filelist) == 0 ) - { - /* continue and select next item... */ - cmd = CMD_LIST_NEXT; + switch (cmd) { + case CMD_SEARCH_MODE: + options.search_mode++; + if (mode[options.search_mode].label == NULL) + options.search_mode = 0; + screen_status_printf(_("Search mode: %s"), + _(mode[options.search_mode].label)); + /* continue and update... */ + case CMD_SCREEN_UPDATE: + screen_search_reload(c); + search_repaint(); + return true; + + case CMD_SCREEN_SEARCH: + search_new(c); + search_repaint(); + return true; + + case CMD_CLEAR: + search_clear(true); + list_window_reset(browser.lw); + search_repaint(); + return true; + + default: + break; } - /* call list_window_cmd to go to the next item */ - return list_window_cmd(lw, filelist->length, cmd); - - case CMD_SELECT_ALL: - browse_handle_select_all (screen, c, lw, filelist); - paint (screen, c); - return 0; - - case CMD_SEARCH_MODE: - options.search_mode++; - if( mode[options.search_mode].label == NULL ) - options.search_mode = 0; - screen_status_printf(_("Search mode: %s"), - _(mode[options.search_mode].label)); - /* continue and update... */ - case CMD_SCREEN_UPDATE: - if( pattern ) - { - search_clear(screen, c, FALSE); - filelist = filelist_search(c, - FALSE, - mode[options.search_mode].table, - pattern); - sync_highlights(c, filelist); + + if (browser.filelist != NULL && + browser_cmd(&browser, c, cmd)) { + if (screen_is_visible(&screen_search)) + search_repaint(); + return true; } - return 1; - - case CMD_SCREEN_SEARCH: - search_new(screen, c); - return 1; - - case CMD_CLEAR: - search_clear(screen, c, TRUE); - list_window_reset(lw); - return 1; - - case CMD_LIST_FIND: - case CMD_LIST_RFIND: - case CMD_LIST_FIND_NEXT: - case CMD_LIST_RFIND_NEXT: - if( filelist ) - return screen_find(screen, - lw, filelist->length, - cmd, browse_lw_callback, (void *) filelist); - else - return 1; - - case CMD_MOUSE_EVENT: - return browse_handle_mouse_event(screen,c,lw,filelist); - - default: - if( filelist ) - return list_window_cmd(lw, filelist->length, cmd); - } - - return 0; -} -screen_functions_t * -get_screen_search(void) -{ - static screen_functions_t functions; - - memset(&functions, 0, sizeof(screen_functions_t)); - functions.init = init; - functions.exit = quit; - functions.open = open; - functions.close = close; - functions.resize = resize; - functions.paint = paint; - functions.update = update; - functions.cmd = search_cmd; - functions.get_lw = get_filelist_window; - functions.get_title = get_title; - - return &functions; + return false; } - -#endif /* ENABLE_SEARCH_SCREEN */ +const struct screen_functions screen_search = { + .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, +};