Code

screen_play, status_bar: update scrolling with a GLib timer
[ncmpc.git] / src / screen_search.c
index 11ffd619f134bcbe50b3ef16ad3fcd6c6d0a52ec..85f817964008ebe0b7f93050c4d3c6c31d42bee3 100644 (file)
  * 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>
 
@@ -124,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(struct mpdclient *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)
@@ -163,11 +148,9 @@ search_check_mode(void)
 }
 
 static void
-search_clear(struct mpdclient *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();
        }
@@ -178,30 +161,43 @@ search_clear(struct mpdclient *c,
 }
 
 static struct filelist *
-filelist_search(struct mpdclient *c, G_GNUC_UNUSED int exact_match, int table,
-               gchar *local_pattern)
+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);
 
-               filelist_sort_all(list, compare_filelistentry_format);
+               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 {
-               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);
@@ -214,7 +210,7 @@ filelist_search(struct mpdclient *c, G_GNUC_UNUSED int exact_match, int table,
  *-----------------------------------------------------------------------
  */
 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;
@@ -223,16 +219,11 @@ 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);
 
-       i=0;
-       while (strv[i]) {
-               i++;
-       }
-
        memset(table, 0, 10*sizeof(int));
        memset(arg, 0, 10*sizeof(char *));
 
@@ -267,64 +258,97 @@ 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]);
-               }
+       /*-----------------------------------------------------------------------
+        * 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]);
+       }
 
-               mpd_search_commit(c->connection);
+       mpd_search_commit(connection);
+       fl = filelist_new_recv(connection);
+       if (!mpd_response_finish(connection)) {
+               filelist_free(fl);
+               fl = NULL;
+       }
 
-               fl = filelist_new();
+       for (i = 0; arg[i] != NULL; ++i)
+               g_free(arg[i]);
 
-               while ((entity = mpd_recv_entity(c->connection)) != NULL)
-                       filelist_append(fl, entity);
+       return fl;
+}
 
-               if (!mpd_response_finish(c->connection)) {
-                       filelist_free(fl);
-                       fl = NULL;
+static struct filelist *
+do_search(struct mpdclient *c, char *query)
+{
+       struct filelist *fl;
 
-                       mpdclient_handle_error(c);
-               }
-       }
+       fl = search_advanced_query(c->connection, query);
+       if (fl != NULL)
+               return fl;
 
-       i=0;
-       while( arg[i] )
-               g_free(arg[i++]);
+       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;
 }
 
+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();
+
+       screen_browser_sync_highlights(browser.filelist, &c->playlist);
+       list_window_check_selected(browser.lw,
+                                  filelist_length(browser.filelist));
+}
+
 static void
 search_new(struct mpdclient *c)
 {
-       if (c->connection == NULL)
+       if (!mpdclient_is_connected(c))
                return;
 
-       search_clear(c, TRUE);
+       search_clear(true);
 
        g_free(pattern);
-       pattern = screen_readln(screen.status_window.w,
-                               _("Search"),
+       pattern = screen_readln(_("Search"),
                                NULL,
                                &search_history,
                                NULL);
@@ -334,37 +358,17 @@ search_new(struct mpdclient *c)
                return;
        }
 
-       if (browser.filelist != NULL) {
-               filelist_free(browser.filelist);
-               browser.filelist = NULL;
-       }
-
-       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)
-               browser.filelist = filelist_search(c, FALSE,
-                                                 mode[options.search_mode].table,
-                                                 pattern);
-
-       if (browser.filelist == NULL)
-               browser.filelist = filelist_new();
-
-       sync_highlights(c, browser.filelist);
-       mpdclient_install_playlist_callback(c, playlist_changed_callback);
-       list_window_check_selected(browser.lw, filelist_length(browser.filelist));
+       screen_search_reload(c);
 }
 
-
-
 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);
@@ -379,7 +383,7 @@ quit(void)
 }
 
 static void
-open(G_GNUC_UNUSED struct mpdclient *c)
+screen_search_open(G_GNUC_UNUSED struct mpdclient *c)
 {
        //  if( pattern==NULL )
        //    search_new(screen, c);
@@ -390,14 +394,14 @@ open(G_GNUC_UNUSED struct mpdclient *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;
@@ -409,7 +413,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);
@@ -426,8 +430,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(struct mpdclient *c, command_t cmd)
+screen_search_cmd(struct mpdclient *c, command_t cmd)
 {
        switch (cmd) {
        case CMD_SEARCH_MODE:
@@ -438,14 +451,7 @@ search_cmd(struct mpdclient *c, command_t cmd)
                                     _(mode[options.search_mode].label));
                /* continue and update... */
        case CMD_SCREEN_UPDATE:
-               if (pattern) {
-                       search_clear(c, FALSE);
-                       browser.filelist = filelist_search(c,
-                                                         FALSE,
-                                                         mode[options.search_mode].table,
-                                                         pattern);
-                       sync_highlights(c, browser.filelist);
-               }
+               screen_search_reload(c);
                search_repaint();
                return true;
 
@@ -455,7 +461,7 @@ search_cmd(struct mpdclient *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;
@@ -475,11 +481,12 @@ search_cmd(struct mpdclient *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,
 };