Code

AUTHORS: add Jonathan Neuschäfer
[ncmpc.git] / src / screen_search.c
index 3deaf09b6c427810e810baf3f8d8047e60ce52ba..6af4937da6be9e2ab34543b89563bf673e8b0075 100644 (file)
@@ -1,5 +1,5 @@
 /* ncmpc (Ncurses MPD Client)
- * (c) 2004-2009 The Music Player Daemon Project
+ * (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
@@ -83,7 +83,7 @@ static search_type_t mode[] = {
        { MPD_TAG_TITLE, N_("Title") },
        { MPD_TAG_ARTIST, N_("Artist") },
        { MPD_TAG_ALBUM, N_("Album") },
-       { SEARCH_URI, N_("file") },
+       { SEARCH_URI, N_("Filename") },
        { SEARCH_ARTIST_TITLE, N_("Artist + Title") },
        { 0, NULL }
 };
@@ -94,33 +94,25 @@ 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  -  <tag>:<search term> [<tag>:<search term>...]",
+       "               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 char** sc, G_GNUC_UNUSED void *data)
+lw_search_help_callback(unsigned idx, G_GNUC_UNUSED void *data)
 {
-       unsigned text_rows;
-       static const char *text[] = {
-               "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",
-               "",
-               "               Available 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
@@ -153,6 +145,7 @@ search_clear(bool clear_pattern)
        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);
@@ -161,30 +154,43 @@ search_clear(bool clear_pattern)
 }
 
 static struct filelist *
-search_simple_query(struct mpdclient *c, G_GNUC_UNUSED int exact_match,
+search_simple_query(struct mpd_connection *connection, bool exact_match,
                    int table, gchar *local_pattern)
 {
-       struct filelist *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_TAG_ARTIST,
-                                                filter_utf8);
-               if (list == NULL)
-                       list = filelist_new();
-
-               list2 = mpdclient_filelist_search(c, FALSE, MPD_TAG_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);
 
-               filelist_sort_all(list, compare_filelistentry_format);
+               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 {
-               list = mpdclient_filelist_search(c, FALSE, table, filter_utf8);
-               if (list == NULL)
-                       list = filelist_new();
+               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);
@@ -197,14 +203,13 @@ search_simple_query(struct mpdclient *c, G_GNUC_UNUSED int exact_match,
  *-----------------------------------------------------------------------
  */
 static struct filelist *
-search_advanced_query(char *query, struct mpdclient *c)
+search_advanced_query(struct mpd_connection *connection, char *query)
 {
        int i,j;
        char **strv;
        int table[10];
        char *arg[10];
        struct filelist *fl = NULL;
-       struct mpd_entity *entity;
 
        advanced_search_mode = FALSE;
        if (strchr(query, ':') == NULL)
@@ -259,31 +264,24 @@ search_advanced_query(char *query, struct mpdclient *c)
         *-----------------------------------------------------------------------
         */
        /** stupid - but this is just a test...... (fulhack)  */
-       mpd_search_db_songs(c->connection, false);
+       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(c->connection,
+                       mpd_search_add_uri_constraint(connection,
                                                      MPD_OPERATOR_DEFAULT,
                                                      arg[i]);
                else
-                       mpd_search_add_tag_constraint(c->connection,
+                       mpd_search_add_tag_constraint(connection,
                                                      MPD_OPERATOR_DEFAULT,
                                                      table[i], arg[i]);
        }
 
-       mpd_search_commit(c->connection);
-
-       fl = filelist_new();
-
-       while ((entity = mpd_recv_entity(c->connection)) != NULL)
-               filelist_append(fl, entity);
-
-       if (!mpd_response_finish(c->connection)) {
+       mpd_search_commit(connection);
+       fl = filelist_new_recv(connection);
+       if (!mpd_response_finish(connection)) {
                filelist_free(fl);
                fl = NULL;
-
-               mpdclient_handle_error(c);
        }
 
        for (i = 0; arg[i] != NULL; ++i)
@@ -295,17 +293,45 @@ search_advanced_query(char *query, struct mpdclient *c)
 static struct filelist *
 do_search(struct mpdclient *c, char *query)
 {
+       struct mpd_connection *connection = mpdclient_get_connection(c);
        struct filelist *fl;
 
-       fl = search_advanced_query(query, c);
-       if (!advanced_search_mode && browser.filelist == NULL)
-               return search_simple_query(c, FALSE,
-                                          mode[options.search_mode].table,
-                                          query);
+       fl = search_advanced_query(connection, query);
+       if (fl != NULL)
+               return fl;
+
+       if (mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS) {
+               mpdclient_handle_error(c);
+               return NULL;
+       }
 
+       fl = search_simple_query(connection, FALSE,
+                                mode[options.search_mode].table,
+                                query);
+       if (fl == NULL)
+               mpdclient_handle_error(c);
        return fl;
 }
 
+static void
+screen_search_reload(struct mpdclient *c)
+{
+       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));
+
+       screen_browser_sync_highlights(browser.filelist, &c->playlist);
+}
+
 static void
 search_new(struct mpdclient *c)
 {
@@ -325,23 +351,14 @@ search_new(struct mpdclient *c)
                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();
-
-       screen_browser_sync_highlights(browser.filelist, &c->playlist);
-       list_window_check_selected(browser.lw, filelist_length(browser.filelist));
+       screen_search_reload(c);
 }
 
 static void
 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
@@ -373,8 +390,7 @@ screen_search_open(G_GNUC_UNUSED struct mpdclient *c)
 static void
 screen_search_resize(int cols, int rows)
 {
-       browser.lw->cols = cols;
-       browser.lw->rows = rows;
+       list_window_resize(browser.lw, cols, rows);
 }
 
 static void
@@ -382,7 +398,7 @@ 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);
@@ -410,7 +426,7 @@ screen_search_get_title(char *str, size_t size)
 static void
 screen_search_update(struct mpdclient *c)
 {
-       if (browser.filelist != NULL && c->events & MPD_IDLE_PLAYLIST) {
+       if (browser.filelist != NULL && c->events & MPD_IDLE_QUEUE) {
                screen_browser_sync_highlights(browser.filelist, &c->playlist);
                search_repaint();
        }
@@ -428,12 +444,7 @@ screen_search_cmd(struct mpdclient *c, command_t cmd)
                                     _(mode[options.search_mode].label));
                /* continue and update... */
        case CMD_SCREEN_UPDATE:
-               if (pattern) {
-                       search_clear(false);
-                       browser.filelist = do_search(c, pattern);
-                       screen_browser_sync_highlights(browser.filelist,
-                                                      &c->playlist);
-               }
+               screen_search_reload(c);
                search_repaint();
                return true;