Code

9742410d9b610277a2b0a6c97a64694f46b53a81
[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 screen_search_reload(struct mpdclient *c)
327         if (pattern == NULL)
328                 return;
330         if (browser.filelist != NULL) {
331                 filelist_free(browser.filelist);
332                 browser.filelist = NULL;
333         }
335         browser.filelist = do_search(c, pattern);
336         if (browser.filelist == NULL)
337                 browser.filelist = filelist_new();
339         screen_browser_sync_highlights(browser.filelist, &c->playlist);
340         list_window_check_selected(browser.lw,
341                                    filelist_length(browser.filelist));
344 static void
345 search_new(struct mpdclient *c)
347         if (!mpdclient_is_connected(c))
348                 return;
350         search_clear(true);
352         g_free(pattern);
353         pattern = screen_readln(_("Search"),
354                                 NULL,
355                                 &search_history,
356                                 NULL);
358         if (pattern == NULL) {
359                 list_window_reset(browser.lw);
360                 return;
361         }
363         screen_search_reload(c);
366 static void
367 screen_search_init(WINDOW *w, int cols, int rows)
369         browser.lw = list_window_init(w, cols, rows);
372 static void
373 screen_search_quit(void)
375         if (search_history)
376                 string_list_free(search_history);
377         if (browser.filelist)
378                 filelist_free(browser.filelist);
379         list_window_free(browser.lw);
381         if (pattern) {
382                 g_free(pattern);
383                 pattern = NULL;
384         }
387 static void
388 screen_search_open(G_GNUC_UNUSED struct mpdclient *c)
390         //  if( pattern==NULL )
391         //    search_new(screen, c);
392         // else
393         screen_status_printf(_("Press %s for a new search"),
394                              get_key_names(CMD_SCREEN_SEARCH,0));
395         search_check_mode();
398 static void
399 screen_search_resize(int cols, int rows)
401         browser.lw->cols = cols;
402         browser.lw->rows = rows;
405 static void
406 screen_search_paint(void)
408         if (browser.filelist) {
409                 browser.lw->hide_cursor = false;
410                 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
411         } else {
412                 browser.lw->hide_cursor = true;
413                 list_window_paint(browser.lw, lw_search_help_callback, NULL);
414         }
417 static const char *
418 screen_search_get_title(char *str, size_t size)
420         if (advanced_search_mode && pattern)
421                 g_snprintf(str, size, _("Search: %s"), pattern);
422         else if (pattern)
423                 g_snprintf(str, size,
424                            _("Search: Results for %s [%s]"),
425                            pattern,
426                            _(mode[options.search_mode].label));
427         else
428                 g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
429                            get_key_names(CMD_SCREEN_SEARCH,0),
430                            _(mode[options.search_mode].label));
432         return str;
435 static void
436 screen_search_update(struct mpdclient *c)
438         if (browser.filelist != NULL && c->events & MPD_IDLE_PLAYLIST) {
439                 screen_browser_sync_highlights(browser.filelist, &c->playlist);
440                 search_repaint();
441         }
444 static bool
445 screen_search_cmd(struct mpdclient *c, command_t cmd)
447         switch (cmd) {
448         case CMD_SEARCH_MODE:
449                 options.search_mode++;
450                 if (mode[options.search_mode].label == NULL)
451                         options.search_mode = 0;
452                 screen_status_printf(_("Search mode: %s"),
453                                      _(mode[options.search_mode].label));
454                 /* continue and update... */
455         case CMD_SCREEN_UPDATE:
456                 screen_search_reload(c);
457                 search_repaint();
458                 return true;
460         case CMD_SCREEN_SEARCH:
461                 search_new(c);
462                 search_repaint();
463                 return true;
465         case CMD_CLEAR:
466                 search_clear(true);
467                 list_window_reset(browser.lw);
468                 search_repaint();
469                 return true;
471         default:
472                 break;
473         }
475         if (browser.filelist != NULL &&
476             browser_cmd(&browser, c, cmd)) {
477                 if (screen_is_visible(&screen_search))
478                         search_repaint();
479                 return true;
480         }
482         return false;
485 const struct screen_functions screen_search = {
486         .init = screen_search_init,
487         .exit = screen_search_quit,
488         .open = screen_search_open,
489         .resize = screen_search_resize,
490         .paint = screen_search_paint,
491         .update = screen_search_update,
492         .cmd = screen_search_cmd,
493         .get_title = screen_search_get_title,
494 };