Code

screen_search: renamed filelist_search() to 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 mpdclient *c, G_GNUC_UNUSED int exact_match,
165                     int table, gchar *local_pattern)
167         struct filelist *list, *list2;
168         gchar *filter_utf8 = locale_to_utf8(local_pattern);
170         if (table == SEARCH_ARTIST_TITLE) {
171                 list = mpdclient_filelist_search(c, FALSE, MPD_TAG_ARTIST,
172                                                  filter_utf8);
173                 if (list == NULL)
174                         list = filelist_new();
176                 list2 = mpdclient_filelist_search(c, FALSE, MPD_TAG_TITLE,
177                                                   filter_utf8);
178                 if (list2 != NULL) {
179                         filelist_move(list, list2);
180                         filelist_free(list2);
181                 }
183                 filelist_sort_all(list, compare_filelistentry_format);
184         } else {
185                 list = mpdclient_filelist_search(c, FALSE, table, filter_utf8);
186                 if (list == NULL)
187                         list = filelist_new();
188         }
190         g_free(filter_utf8);
191         return list;
194 /*-----------------------------------------------------------------------
195  * NOTE: This code exists to test a new search ui,
196  *       Its ugly and MUST be redesigned before the next release!
197  *-----------------------------------------------------------------------
198  */
199 static struct filelist *
200 search_advanced_query(char *query, struct mpdclient *c)
202         int i,j;
203         char **strv;
204         int table[10];
205         char *arg[10];
206         struct filelist *fl = NULL;
208         advanced_search_mode = FALSE;
209         if( g_strrstr(query, ":") == NULL )
210                 return NULL;
212         strv = g_strsplit_set(query, ": ", 0);
214         i=0;
215         while (strv[i]) {
216                 i++;
217         }
219         memset(table, 0, 10*sizeof(int));
220         memset(arg, 0, 10*sizeof(char *));
222         i=0;
223         j=0;
224         while (strv[i] && strlen(strv[i]) > 0 && i < 9) {
225                 int id = search_get_tag_id(strv[i]);
226                 if (id == -1) {
227                         if (table[j]) {
228                                 char *tmp = arg[j];
229                                 arg[j] = g_strdup_printf("%s %s", arg[j], strv[i]);
230                                 g_free(tmp);
231                         } else {
232                                 screen_status_printf(_("Bad search tag %s"), strv[i]);
233                         }
234                         i++;
235                 } else if (strv[i+1] == NULL || strlen(strv[i+1]) == 0) {
236                         screen_status_printf(_("No argument for search tag %s"), strv[i]);
237                         i++;
238                         //        j--;
239                         //table[j] = -1;
240                 } else {
241                         table[j] = id;
242                         arg[j] = locale_to_utf8(strv[i+1]); // FREE ME
243                         j++;
244                         table[j] = -1;
245                         arg[j] = NULL;
246                         i = i + 2;
247                         advanced_search_mode = TRUE;
248                 }
249         }
251         g_strfreev(strv);
254         if (advanced_search_mode && j > 0) {
255                 int iter;
256                 struct mpd_entity *entity;
258                 /*-----------------------------------------------------------------------
259                  * NOTE (again): This code exists to test a new search ui,
260                  *               Its ugly and MUST be redesigned before the next release!
261                  *             + the code below should live in mpdclient.c
262                  *-----------------------------------------------------------------------
263                  */
264                 /** stupid - but this is just a test...... (fulhack)  */
265                 mpd_search_db_songs(c->connection, false);
267                 for(iter = 0; iter < 10 && arg[iter] != NULL; iter++) {
268                         if (table[iter] == SEARCH_URI)
269                                 mpd_search_add_uri_constraint(c->connection,
270                                                               MPD_OPERATOR_DEFAULT,
271                                                               arg[iter]);
272                         else
273                                 mpd_search_add_tag_constraint(c->connection,
274                                                               MPD_OPERATOR_DEFAULT,
275                                                               table[iter], arg[iter]);
276                 }
278                 mpd_search_commit(c->connection);
280                 fl = filelist_new();
282                 while ((entity = mpd_recv_entity(c->connection)) != NULL)
283                         filelist_append(fl, entity);
285                 if (!mpd_response_finish(c->connection)) {
286                         filelist_free(fl);
287                         fl = NULL;
289                         mpdclient_handle_error(c);
290                 }
291         }
293         i=0;
294         while( arg[i] )
295                 g_free(arg[i++]);
297         return fl;
300 static struct filelist *
301 do_search(struct mpdclient *c, char *query)
303         struct filelist *fl;
305         fl = search_advanced_query(query, c);
306         if (!advanced_search_mode && browser.filelist == NULL)
307                 return search_simple_query(c, FALSE,
308                                            mode[options.search_mode].table,
309                                            query);
311         return fl;
314 static void
315 search_new(struct mpdclient *c)
317         if (!mpdclient_is_connected(c))
318                 return;
320         search_clear(true);
322         g_free(pattern);
323         pattern = screen_readln(_("Search"),
324                                 NULL,
325                                 &search_history,
326                                 NULL);
328         if (pattern == NULL) {
329                 list_window_reset(browser.lw);
330                 return;
331         }
333         if (browser.filelist != NULL) {
334                 filelist_free(browser.filelist);
335                 browser.filelist = NULL;
336         }
338         browser.filelist = do_search(c, pattern);
339         if (browser.filelist == NULL)
340                 browser.filelist = filelist_new();
342         screen_browser_sync_highlights(browser.filelist, &c->playlist);
343         list_window_check_selected(browser.lw, filelist_length(browser.filelist));
346 static void
347 screen_search_init(WINDOW *w, int cols, int rows)
349         browser.lw = list_window_init(w, cols, rows);
352 static void
353 screen_search_quit(void)
355         if (search_history)
356                 string_list_free(search_history);
357         if (browser.filelist)
358                 filelist_free(browser.filelist);
359         list_window_free(browser.lw);
361         if (pattern) {
362                 g_free(pattern);
363                 pattern = NULL;
364         }
367 static void
368 screen_search_open(G_GNUC_UNUSED struct mpdclient *c)
370         //  if( pattern==NULL )
371         //    search_new(screen, c);
372         // else
373         screen_status_printf(_("Press %s for a new search"),
374                              get_key_names(CMD_SCREEN_SEARCH,0));
375         search_check_mode();
378 static void
379 screen_search_resize(int cols, int rows)
381         browser.lw->cols = cols;
382         browser.lw->rows = rows;
385 static void
386 screen_search_paint(void)
388         if (browser.filelist) {
389                 browser.lw->hide_cursor = false;
390                 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
391         } else {
392                 browser.lw->hide_cursor = true;
393                 list_window_paint(browser.lw, lw_search_help_callback, NULL);
394         }
397 static const char *
398 screen_search_get_title(char *str, size_t size)
400         if (advanced_search_mode && pattern)
401                 g_snprintf(str, size, _("Search: %s"), pattern);
402         else if (pattern)
403                 g_snprintf(str, size,
404                            _("Search: Results for %s [%s]"),
405                            pattern,
406                            _(mode[options.search_mode].label));
407         else
408                 g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
409                            get_key_names(CMD_SCREEN_SEARCH,0),
410                            _(mode[options.search_mode].label));
412         return str;
415 static void
416 screen_search_update(struct mpdclient *c)
418         if (browser.filelist != NULL && c->events & MPD_IDLE_PLAYLIST) {
419                 screen_browser_sync_highlights(browser.filelist, &c->playlist);
420                 search_repaint();
421         }
424 static bool
425 screen_search_cmd(struct mpdclient *c, command_t cmd)
427         switch (cmd) {
428         case CMD_SEARCH_MODE:
429                 options.search_mode++;
430                 if (mode[options.search_mode].label == NULL)
431                         options.search_mode = 0;
432                 screen_status_printf(_("Search mode: %s"),
433                                      _(mode[options.search_mode].label));
434                 /* continue and update... */
435         case CMD_SCREEN_UPDATE:
436                 if (pattern) {
437                         search_clear(false);
438                         browser.filelist = do_search(c, pattern);
439                         screen_browser_sync_highlights(browser.filelist,
440                                                        &c->playlist);
441                 }
442                 search_repaint();
443                 return true;
445         case CMD_SCREEN_SEARCH:
446                 search_new(c);
447                 search_repaint();
448                 return true;
450         case CMD_CLEAR:
451                 search_clear(true);
452                 list_window_reset(browser.lw);
453                 search_repaint();
454                 return true;
456         default:
457                 break;
458         }
460         if (browser.filelist != NULL &&
461             browser_cmd(&browser, c, cmd)) {
462                 if (screen_is_visible(&screen_search))
463                         search_repaint();
464                 return true;
465         }
467         return false;
470 const struct screen_functions screen_search = {
471         .init = screen_search_init,
472         .exit = screen_search_quit,
473         .open = screen_search_open,
474         .resize = screen_search_resize,
475         .paint = screen_search_paint,
476         .update = screen_search_update,
477         .cmd = screen_search_cmd,
478         .get_title = screen_search_get_title,
479 };