Code

screen_search: check for SEARCH_URI in search_simple_query()
[ncmpc.git] / src / screen_search.c
index b1d480ddb207c3fb3e4bde5b42b9befbd88c15d5..2dd911d09b3a8cc3c9cccb5518c62176889845a8 100644 (file)
@@ -161,30 +161,45 @@ 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);
+
+               list = filelist_new_recv(connection);
 
-               filelist_sort_all(list, compare_filelistentry_format);
+               if (list != NULL)
+                       filelist_sort_all(list, compare_filelistentry_format);
+       } 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,7 +212,7 @@ 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;
@@ -245,49 +260,41 @@ search_advanced_query(char *query, struct mpdclient *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;
-               struct mpd_entity *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(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_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)) {
-                       filelist_free(fl);
-                       fl = NULL;
+       /*-----------------------------------------------------------------------
+        * 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]);
+       }
 
-                       mpdclient_handle_error(c);
-               }
+       mpd_search_commit(connection);
+       fl = filelist_new_recv(connection);
+       if (!mpd_response_finish(connection)) {
+               filelist_free(fl);
+               fl = NULL;
        }
 
-       i=0;
-       while( arg[i] )
-               g_free(arg[i++]);
+       for (i = 0; arg[i] != NULL; ++i)
+               g_free(arg[i]);
 
        return fl;
 }
@@ -297,12 +304,20 @@ do_search(struct mpdclient *c, char *query)
 {
        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(c->connection, query);
+       if (fl != NULL)
+               return fl;
+
+       if (mpd_connection_get_error(c->connection) != MPD_ERROR_SUCCESS) {
+               mpdclient_handle_error(c);
+               return NULL;
+       }
 
+       fl = search_simple_query(c->connection, FALSE,
+                                mode[options.search_mode].table,
+                                query);
+       if (fl == NULL)
+               mpdclient_handle_error(c);
        return fl;
 }