Code

filelist: added constructor filelist_new_recv()
[ncmpc.git] / src / screen_search.c
index 9f2fee13f0861aaa31cd46b2c26d5f5827af7d35..8dfec1ff920c1bce707bfec565b7afbacb67cfc4 100644 (file)
@@ -197,7 +197,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;
@@ -206,7 +206,7 @@ search_advanced_query(char *query, struct mpdclient *c)
        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);
@@ -245,49 +245,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,13 +289,18 @@ 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;
 
-       return fl;
+       if (mpd_connection_get_error(c->connection) != MPD_ERROR_SUCCESS) {
+               mpdclient_handle_error(c);
+               return NULL;
+       }
+
+       return search_simple_query(c, FALSE,
+                                  mode[options.search_mode].table,
+                                  query);
 }
 
 static void