Code

use mpdclient_is_connected()
[ncmpc.git] / src / screen_search.c
index f290e04d38a0d80af2e4f88296a37d78dd5165b9..3829f8dfa1bcbbd3d037265db62906c4058cd6f7 100644 (file)
@@ -1,7 +1,7 @@
 /* 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
  * 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_message.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 <ctype.h>
-#include <stdlib.h>
 #include <string.h>
 #include <glib.h>
 
+enum {
+       SEARCH_URI = MPD_TAG_COUNT + 100,
+};
+
 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 +72,18 @@ 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_("file") },
        { SEARCH_ARTIST_TITLE, N_("Artist + Title") },
        { 0, NULL }
 };
@@ -94,18 +98,18 @@ static struct screen_browser browser;
 /* search info */
 static const char *
 lw_search_help_callback(unsigned idx, G_GNUC_UNUSED bool *highlight,
-                       G_GNUC_UNUSED void *data)
+                       G_GNUC_UNUSED char** sc, 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).",
+               "Quick     -  Enter a string and ncmpc will search according",
+               "             to the current search mode (displayed above).",
                "",
                "Advanced  -  <tag>:<search term> [<tag>:<search term>...]",
-               "                       Example: artist:radiohead album:pablo honey",
+               "               Example: artist:radiohead album:pablo honey",
                "",
-               "                  avalible tags: artist, album, title, track,",
-               "                  name, genre, date composer, performer, comment, file",
+               "               Available tags: artist, album, title, track,",
+               "               name, genre, date composer, performer, comment, file",
                "",
                NULL
        };
@@ -120,30 +124,15 @@ lw_search_help_callback(unsigned idx, G_GNUC_UNUSED bool *highlight,
 }
 
 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 has 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 +148,11 @@ 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();
        }
        if (clear_pattern && pattern) {
                g_free(pattern);
@@ -173,20 +160,20 @@ search_clear(mpdclient_t *c,
        }
 }
 
-static mpdclient_filelist_t *
-filelist_search(mpdclient_t *c, G_GNUC_UNUSED int exact_match, int table,
+static struct filelist *
+filelist_search(struct mpdclient *c, G_GNUC_UNUSED int exact_match, int table,
                gchar *local_pattern)
 {
-       mpdclient_filelist_t *list, *list2;
+       struct filelist *list, *list2;
        gchar *filter_utf8 = locale_to_utf8(local_pattern);
 
        if (table == SEARCH_ARTIST_TITLE) {
-               list = mpdclient_filelist_search(c, FALSE, MPD_TABLE_ARTIST,
+               list = mpdclient_filelist_search(c, FALSE, MPD_TAG_ARTIST,
                                                 filter_utf8);
                if (list == NULL)
-                       list = filelist_new(NULL);
+                       list = filelist_new();
 
-               list2 = mpdclient_filelist_search(c, FALSE, MPD_TABLE_TITLE,
+               list2 = mpdclient_filelist_search(c, FALSE, MPD_TAG_TITLE,
                                                  filter_utf8);
                if (list2 != NULL) {
                        filelist_move(list, list2);
@@ -197,7 +184,7 @@ filelist_search(mpdclient_t *c, G_GNUC_UNUSED int exact_match, int table,
        } else {
                list = mpdclient_filelist_search(c, FALSE, table, filter_utf8);
                if (list == NULL)
-                       list = filelist_new(NULL);
+                       list = filelist_new();
        }
 
        g_free(filter_utf8);
@@ -209,14 +196,14 @@ 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(char *query, struct mpdclient *c)
 {
        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 )
@@ -266,7 +253,7 @@ search_advanced_query(char *query, mpdclient_t *c)
 
        if (advanced_search_mode && j > 0) {
                int iter;
-               mpd_InfoEntity *entity;
+               struct mpd_entity *entity;
 
                /*-----------------------------------------------------------------------
                 * NOTE (again): This code exists to test a new search ui,
@@ -275,21 +262,32 @@ search_advanced_query(char *query, mpdclient_t *c)
                 *-----------------------------------------------------------------------
                 */
                /** stupid - but this is just a test...... (fulhack)  */
-               mpd_startSearch(c->connection, FALSE);
-
-               for(iter = 0; iter < 10; iter++) {
-                       mpd_addConstraintSearch(c->connection, table[iter], arg[iter]);
+               mpd_search_db_songs(c->connection, false);
+
+               for(iter = 0; iter < 10 && arg[iter] != NULL; iter++) {
+                       if (table[iter] == SEARCH_URI)
+                               mpd_search_add_uri_constraint(c->connection,
+                                                             MPD_OPERATOR_DEFAULT,
+                                                             arg[iter]);
+                       else
+                               mpd_search_add_tag_constraint(c->connection,
+                                                             MPD_OPERATOR_DEFAULT,
+                                                             table[iter], arg[iter]);
                }
 
-               mpd_commitSearch(c->connection);
+               mpd_search_commit(c->connection);
 
-               fl = filelist_new(NULL);
+               fl = filelist_new();
 
-               while ((entity=mpd_getNextInfoEntity(c->connection)))
+               while ((entity = mpd_recv_entity(c->connection)) != NULL)
                        filelist_append(fl, entity);
 
-               if (mpdclient_finish_command(c) && fl)
+               if (!mpd_response_finish(c->connection)) {
                        filelist_free(fl);
+                       fl = NULL;
+
+                       mpdclient_handle_error(c);
+               }
        }
 
        i=0;
@@ -300,13 +298,15 @@ search_advanced_query(char *query, mpdclient_t *c)
 }
 
 static void
-search_new(mpdclient_t *c)
+search_new(struct mpdclient *c)
 {
-       search_clear(c, TRUE);
+       if (!mpdclient_is_connected(c))
+               return;
+
+       search_clear(true);
 
        g_free(pattern);
-       pattern = screen_readln(screen.status_window.w,
-                               _("Search"),
+       pattern = screen_readln(_("Search"),
                                NULL,
                                &search_history,
                                NULL);
@@ -321,7 +321,7 @@ search_new(mpdclient_t *c)
                browser.filelist = NULL;
        }
 
-       if (!MPD_VERSION_LT(c, 0, 12, 0))
+       if (mpd_connection_cmp_server_version(c->connection, 0, 12, 0) >= 0)
                browser.filelist = search_advanced_query(pattern, c);
 
        if (!advanced_search_mode && browser.filelist == NULL)
@@ -330,23 +330,20 @@ search_new(mpdclient_t *c)
                                                  pattern);
 
        if (browser.filelist == NULL)
-               browser.filelist = filelist_new(NULL);
+               browser.filelist = filelist_new();
 
-       sync_highlights(c, browser.filelist);
-       mpdclient_install_playlist_callback(c, playlist_changed_callback);
+       screen_browser_sync_highlights(browser.filelist, &c->playlist);
        list_window_check_selected(browser.lw, filelist_length(browser.filelist));
 }
 
-
-
 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);
 }
 
 static void
-quit(void)
+screen_search_quit(void)
 {
        if (search_history)
                string_list_free(search_history);
@@ -361,7 +358,7 @@ 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);
@@ -372,14 +369,14 @@ open(G_GNUC_UNUSED mpdclient_t *c)
 }
 
 static void
-resize(int cols, int rows)
+screen_search_resize(int cols, int rows)
 {
        browser.lw->cols = cols;
        browser.lw->rows = rows;
 }
 
 static void
-paint(void)
+screen_search_paint(void)
 {
        if (browser.filelist) {
                browser.lw->hide_cursor = false;
@@ -391,7 +388,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);
@@ -408,8 +405,17 @@ get_title(char *str, size_t size)
        return str;
 }
 
+static void
+screen_search_update(struct mpdclient *c)
+{
+       if (browser.filelist != NULL && c->events & MPD_IDLE_PLAYLIST) {
+               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:
@@ -421,12 +427,13 @@ search_cmd(mpdclient_t *c, command_t cmd)
                /* continue and update... */
        case CMD_SCREEN_UPDATE:
                if (pattern) {
-                       search_clear(c, FALSE);
+                       search_clear(false);
                        browser.filelist = filelist_search(c,
                                                          FALSE,
                                                          mode[options.search_mode].table,
                                                          pattern);
-                       sync_highlights(c, browser.filelist);
+                       screen_browser_sync_highlights(browser.filelist,
+                                                      &c->playlist);
                }
                search_repaint();
                return true;
@@ -437,7 +444,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 +464,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,
 };