Code

screen_search: check for SEARCH_URI in search_simple_query()
[ncmpc.git] / src / screen_search.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2009 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
20 #include "screen_search.h"
21 #include "screen_interface.h"
22 #include "screen_message.h"
23 #include "screen.h"
24 #include "i18n.h"
25 #include "options.h"
26 #include "charset.h"
27 #include "mpdclient.h"
28 #include "strfsong.h"
29 #include "utils.h"
30 #include "screen_utils.h"
31 #include "screen_browser.h"
32 #include "filelist.h"
34 #include <string.h>
35 #include <glib.h>
37 enum {
38         SEARCH_URI = MPD_TAG_COUNT + 100,
39 };
41 static const struct {
42         const char *name;
43         const char *localname;
44 } search_tag[MPD_TAG_COUNT] = {
45         [MPD_TAG_ARTIST] = { "artist", N_("artist") },
46         [MPD_TAG_ALBUM] = { "album", N_("album") },
47         [MPD_TAG_TITLE] = { "title", N_("title") },
48         [MPD_TAG_TRACK] = { "track", N_("track") },
49         [MPD_TAG_NAME] = { "name", N_("name") },
50         [MPD_TAG_GENRE] = { "genre", N_("genre") },
51         [MPD_TAG_DATE] = { "date", N_("date") },
52         [MPD_TAG_COMPOSER] = { "composer", N_("composer") },
53         [MPD_TAG_PERFORMER] = { "performer", N_("performer") },
54         [MPD_TAG_COMMENT] = { "comment", N_("comment") },
55 };
57 static int
58 search_get_tag_id(const char *name)
59 {
60         unsigned i;
62         if (g_ascii_strcasecmp(name, "file") == 0 ||
63             strcasecmp(name, _("file")) == 0)
64                 return SEARCH_URI;
66         for (i = 0; i < MPD_TAG_COUNT; ++i)
67                 if (search_tag[i].name != NULL &&
68                     (strcasecmp(search_tag[i].name, name) == 0 ||
69                      strcasecmp(search_tag[i].localname, name) == 0))
70                         return i;
72         return -1;
73 }
75 #define SEARCH_ARTIST_TITLE 999
77 typedef struct {
78         enum mpd_tag_type table;
79         const char *label;
80 } search_type_t;
82 static search_type_t mode[] = {
83         { MPD_TAG_TITLE, N_("Title") },
84         { MPD_TAG_ARTIST, N_("Artist") },
85         { MPD_TAG_ALBUM, N_("Album") },
86         { SEARCH_URI, N_("file") },
87         { SEARCH_ARTIST_TITLE, N_("Artist + Title") },
88         { 0, NULL }
89 };
91 static GList *search_history = NULL;
92 static gchar *pattern = NULL;
93 static gboolean advanced_search_mode = FALSE;
95 static struct screen_browser browser;
98 /* search info */
99 static const char *
100 lw_search_help_callback(unsigned idx, G_GNUC_UNUSED bool *highlight,
101                         G_GNUC_UNUSED char** sc, G_GNUC_UNUSED void *data)
103         unsigned text_rows;
104         static const char *text[] = {
105                 "Quick     -  Enter a string and ncmpc will search according",
106                 "             to the current search mode (displayed above).",
107                 "",
108                 "Advanced  -  <tag>:<search term> [<tag>:<search term>...]",
109                 "               Example: artist:radiohead album:pablo honey",
110                 "",
111                 "               Available tags: artist, album, title, track,",
112                 "               name, genre, date composer, performer, comment, file",
113                 "",
114                 NULL
115         };
117         text_rows=0;
118         while (text[text_rows])
119                 text_rows++;
121         if (idx < text_rows)
122                 return text[idx];
123         return NULL;
126 static void
127 screen_search_paint(void);
129 static void
130 search_repaint(void)
132         screen_search_paint();
133         wrefresh(browser.lw->w);
136 /* sanity check search mode value */
137 static void
138 search_check_mode(void)
140         int max = 0;
142         while (mode[max].label != NULL)
143                 max++;
144         if (options.search_mode < 0)
145                 options.search_mode = 0;
146         else if (options.search_mode >= max)
147                 options.search_mode = max-1;
150 static void
151 search_clear(bool clear_pattern)
153         if (browser.filelist) {
154                 filelist_free(browser.filelist);
155                 browser.filelist = filelist_new();
156         }
157         if (clear_pattern && pattern) {
158                 g_free(pattern);
159                 pattern = NULL;
160         }
163 static struct filelist *
164 search_simple_query(struct mpd_connection *connection, bool exact_match,
165                     int table, gchar *local_pattern)
167         struct filelist *list;
168         gchar *filter_utf8 = locale_to_utf8(local_pattern);
170         if (table == SEARCH_ARTIST_TITLE) {
171                 mpd_command_list_begin(connection, false);
173                 mpd_search_db_songs(connection, exact_match);
174                 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
175                                               MPD_TAG_ARTIST, filter_utf8);
176                 mpd_search_commit(connection);
178                 mpd_search_db_songs(connection, exact_match);
179                 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
180                                               MPD_TAG_TITLE, filter_utf8);
181                 mpd_search_commit(connection);
183                 mpd_command_list_end(connection);
185                 list = filelist_new_recv(connection);
187                 if (list != NULL)
188                         filelist_sort_all(list, compare_filelistentry_format);
189         } else if (table == SEARCH_URI) {
190                 mpd_search_db_songs(connection, exact_match);
191                 mpd_search_add_uri_constraint(connection, MPD_OPERATOR_DEFAULT,
192                                               filter_utf8);
193                 mpd_search_commit(connection);
195                 list = filelist_new_recv(connection);
196         } else {
197                 mpd_search_db_songs(connection, exact_match);
198                 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
199                                               table, filter_utf8);
200                 mpd_search_commit(connection);
202                 list = filelist_new_recv(connection);
203         }
205         g_free(filter_utf8);
206         return list;
209 /*-----------------------------------------------------------------------
210  * NOTE: This code exists to test a new search ui,
211  *       Its ugly and MUST be redesigned before the next release!
212  *-----------------------------------------------------------------------
213  */
214 static struct filelist *
215 search_advanced_query(struct mpd_connection *connection, char *query)
217         int i,j;
218         char **strv;
219         int table[10];
220         char *arg[10];
221         struct filelist *fl = NULL;
223         advanced_search_mode = FALSE;
224         if (strchr(query, ':') == NULL)
225                 return NULL;
227         strv = g_strsplit_set(query, ": ", 0);
229         memset(table, 0, 10*sizeof(int));
230         memset(arg, 0, 10*sizeof(char *));
232         i=0;
233         j=0;
234         while (strv[i] && strlen(strv[i]) > 0 && i < 9) {
235                 int id = search_get_tag_id(strv[i]);
236                 if (id == -1) {
237                         if (table[j]) {
238                                 char *tmp = arg[j];
239                                 arg[j] = g_strdup_printf("%s %s", arg[j], strv[i]);
240                                 g_free(tmp);
241                         } else {
242                                 screen_status_printf(_("Bad search tag %s"), strv[i]);
243                         }
244                         i++;
245                 } else if (strv[i+1] == NULL || strlen(strv[i+1]) == 0) {
246                         screen_status_printf(_("No argument for search tag %s"), strv[i]);
247                         i++;
248                         //        j--;
249                         //table[j] = -1;
250                 } else {
251                         table[j] = id;
252                         arg[j] = locale_to_utf8(strv[i+1]); // FREE ME
253                         j++;
254                         table[j] = -1;
255                         arg[j] = NULL;
256                         i = i + 2;
257                         advanced_search_mode = TRUE;
258                 }
259         }
261         g_strfreev(strv);
263         if (!advanced_search_mode || j == 0) {
264                 for (i = 0; arg[i] != NULL; ++i)
265                         g_free(arg[i]);
266                 return NULL;
267         }
269         /*-----------------------------------------------------------------------
270          * NOTE (again): This code exists to test a new search ui,
271          *               Its ugly and MUST be redesigned before the next release!
272          *             + the code below should live in mpdclient.c
273          *-----------------------------------------------------------------------
274          */
275         /** stupid - but this is just a test...... (fulhack)  */
276         mpd_search_db_songs(connection, false);
278         for (i = 0; i < 10 && arg[i] != NULL; i++) {
279                 if (table[i] == SEARCH_URI)
280                         mpd_search_add_uri_constraint(connection,
281                                                       MPD_OPERATOR_DEFAULT,
282                                                       arg[i]);
283                 else
284                         mpd_search_add_tag_constraint(connection,
285                                                       MPD_OPERATOR_DEFAULT,
286                                                       table[i], arg[i]);
287         }
289         mpd_search_commit(connection);
290         fl = filelist_new_recv(connection);
291         if (!mpd_response_finish(connection)) {
292                 filelist_free(fl);
293                 fl = NULL;
294         }
296         for (i = 0; arg[i] != NULL; ++i)
297                 g_free(arg[i]);
299         return fl;
302 static struct filelist *
303 do_search(struct mpdclient *c, char *query)
305         struct filelist *fl;
307         fl = search_advanced_query(c->connection, query);
308         if (fl != NULL)
309                 return fl;
311         if (mpd_connection_get_error(c->connection) != MPD_ERROR_SUCCESS) {
312                 mpdclient_handle_error(c);
313                 return NULL;
314         }
316         fl = search_simple_query(c->connection, FALSE,
317                                  mode[options.search_mode].table,
318                                  query);
319         if (fl == NULL)
320                 mpdclient_handle_error(c);
321         return fl;
324 static void
325 search_new(struct mpdclient *c)
327         if (!mpdclient_is_connected(c))
328                 return;
330         search_clear(true);
332         g_free(pattern);
333         pattern = screen_readln(_("Search"),
334                                 NULL,
335                                 &search_history,
336                                 NULL);
338         if (pattern == NULL) {
339                 list_window_reset(browser.lw);
340                 return;
341         }
343         if (browser.filelist != NULL) {
344                 filelist_free(browser.filelist);
345                 browser.filelist = NULL;
346         }
348         browser.filelist = do_search(c, pattern);
349         if (browser.filelist == NULL)
350                 browser.filelist = filelist_new();
352         screen_browser_sync_highlights(browser.filelist, &c->playlist);
353         list_window_check_selected(browser.lw, filelist_length(browser.filelist));
356 static void
357 screen_search_init(WINDOW *w, int cols, int rows)
359         browser.lw = list_window_init(w, cols, rows);
362 static void
363 screen_search_quit(void)
365         if (search_history)
366                 string_list_free(search_history);
367         if (browser.filelist)
368                 filelist_free(browser.filelist);
369         list_window_free(browser.lw);
371         if (pattern) {
372                 g_free(pattern);
373                 pattern = NULL;
374         }
377 static void
378 screen_search_open(G_GNUC_UNUSED struct mpdclient *c)
380         //  if( pattern==NULL )
381         //    search_new(screen, c);
382         // else
383         screen_status_printf(_("Press %s for a new search"),
384                              get_key_names(CMD_SCREEN_SEARCH,0));
385         search_check_mode();
388 static void
389 screen_search_resize(int cols, int rows)
391         browser.lw->cols = cols;
392         browser.lw->rows = rows;
395 static void
396 screen_search_paint(void)
398         if (browser.filelist) {
399                 browser.lw->hide_cursor = false;
400                 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
401         } else {
402                 browser.lw->hide_cursor = true;
403                 list_window_paint(browser.lw, lw_search_help_callback, NULL);
404         }
407 static const char *
408 screen_search_get_title(char *str, size_t size)
410         if (advanced_search_mode && pattern)
411                 g_snprintf(str, size, _("Search: %s"), pattern);
412         else if (pattern)
413                 g_snprintf(str, size,
414                            _("Search: Results for %s [%s]"),
415                            pattern,
416                            _(mode[options.search_mode].label));
417         else
418                 g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
419                            get_key_names(CMD_SCREEN_SEARCH,0),
420                            _(mode[options.search_mode].label));
422         return str;
425 static void
426 screen_search_update(struct mpdclient *c)
428         if (browser.filelist != NULL && c->events & MPD_IDLE_PLAYLIST) {
429                 screen_browser_sync_highlights(browser.filelist, &c->playlist);
430                 search_repaint();
431         }
434 static bool
435 screen_search_cmd(struct mpdclient *c, command_t cmd)
437         switch (cmd) {
438         case CMD_SEARCH_MODE:
439                 options.search_mode++;
440                 if (mode[options.search_mode].label == NULL)
441                         options.search_mode = 0;
442                 screen_status_printf(_("Search mode: %s"),
443                                      _(mode[options.search_mode].label));
444                 /* continue and update... */
445         case CMD_SCREEN_UPDATE:
446                 if (pattern) {
447                         search_clear(false);
448                         browser.filelist = do_search(c, pattern);
449                         screen_browser_sync_highlights(browser.filelist,
450                                                        &c->playlist);
451                 }
452                 search_repaint();
453                 return true;
455         case CMD_SCREEN_SEARCH:
456                 search_new(c);
457                 search_repaint();
458                 return true;
460         case CMD_CLEAR:
461                 search_clear(true);
462                 list_window_reset(browser.lw);
463                 search_repaint();
464                 return true;
466         default:
467                 break;
468         }
470         if (browser.filelist != NULL &&
471             browser_cmd(&browser, c, cmd)) {
472                 if (screen_is_visible(&screen_search))
473                         search_repaint();
474                 return true;
475         }
477         return false;
480 const struct screen_functions screen_search = {
481         .init = screen_search_init,
482         .exit = screen_search_quit,
483         .open = screen_search_open,
484         .resize = screen_search_resize,
485         .paint = screen_search_paint,
486         .update = screen_search_update,
487         .cmd = screen_search_cmd,
488         .get_title = screen_search_get_title,
489 };