Code

screen_search: clear return value on error
[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 "i18n.h"
21 #include "options.h"
22 #include "charset.h"
23 #include "mpdclient.h"
24 #include "strfsong.h"
25 #include "command.h"
26 #include "screen.h"
27 #include "utils.h"
28 #include "screen_utils.h"
29 #include "screen_browser.h"
30 #include "filelist.h"
32 #include <ctype.h>
33 #include <stdlib.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 paint(void);
129 static void
130 search_repaint(void)
132         paint();
133         wrefresh(browser.lw->w);
136 static void
137 search_repaint_if_active(void)
139         if (screen_is_visible(&screen_search))
140                 search_repaint();
143 /* the playlist has been updated -> fix highlights */
144 static void
145 playlist_changed_callback(struct mpdclient *c, int event, gpointer data)
147         browser_playlist_changed(&browser, c, event, data);
148         search_repaint_if_active();
151 /* sanity check search mode value */
152 static void
153 search_check_mode(void)
155         int max = 0;
157         while (mode[max].label != NULL)
158                 max++;
159         if (options.search_mode < 0)
160                 options.search_mode = 0;
161         else if (options.search_mode >= max)
162                 options.search_mode = max-1;
165 static void
166 search_clear(struct mpdclient *c,
167              gboolean clear_pattern)
169         if (browser.filelist) {
170                 mpdclient_remove_playlist_callback(c, playlist_changed_callback);
171                 filelist_free(browser.filelist);
172                 browser.filelist = filelist_new();
173         }
174         if (clear_pattern && pattern) {
175                 g_free(pattern);
176                 pattern = NULL;
177         }
180 static struct filelist *
181 filelist_search(struct mpdclient *c, G_GNUC_UNUSED int exact_match, int table,
182                 gchar *local_pattern)
184         struct filelist *list, *list2;
185         gchar *filter_utf8 = locale_to_utf8(local_pattern);
187         if (table == SEARCH_ARTIST_TITLE) {
188                 list = mpdclient_filelist_search(c, FALSE, MPD_TAG_ARTIST,
189                                                  filter_utf8);
190                 if (list == NULL)
191                         list = filelist_new();
193                 list2 = mpdclient_filelist_search(c, FALSE, MPD_TAG_TITLE,
194                                                   filter_utf8);
195                 if (list2 != NULL) {
196                         filelist_move(list, list2);
197                         filelist_free(list2);
198                 }
200                 filelist_sort_all(list, compare_filelistentry_format);
201         } else {
202                 list = mpdclient_filelist_search(c, FALSE, table, filter_utf8);
203                 if (list == NULL)
204                         list = filelist_new();
205         }
207         g_free(filter_utf8);
208         return list;
211 /*-----------------------------------------------------------------------
212  * NOTE: This code exists to test a new search ui,
213  *       Its ugly and MUST be redesigned before the next release!
214  *-----------------------------------------------------------------------
215  */
216 static struct filelist *
217 search_advanced_query(char *query, struct mpdclient *c)
219         int i,j;
220         char **strv;
221         int table[10];
222         char *arg[10];
223         struct filelist *fl = NULL;
225         advanced_search_mode = FALSE;
226         if( g_strrstr(query, ":") == NULL )
227                 return NULL;
229         strv = g_strsplit_set(query, ": ", 0);
231         i=0;
232         while (strv[i]) {
233                 i++;
234         }
236         memset(table, 0, 10*sizeof(int));
237         memset(arg, 0, 10*sizeof(char *));
239         i=0;
240         j=0;
241         while (strv[i] && strlen(strv[i]) > 0 && i < 9) {
242                 int id = search_get_tag_id(strv[i]);
243                 if (id == -1) {
244                         if (table[j]) {
245                                 char *tmp = arg[j];
246                                 arg[j] = g_strdup_printf("%s %s", arg[j], strv[i]);
247                                 g_free(tmp);
248                         } else {
249                                 screen_status_printf(_("Bad search tag %s"), strv[i]);
250                         }
251                         i++;
252                 } else if (strv[i+1] == NULL || strlen(strv[i+1]) == 0) {
253                         screen_status_printf(_("No argument for search tag %s"), strv[i]);
254                         i++;
255                         //        j--;
256                         //table[j] = -1;
257                 } else {
258                         table[j] = id;
259                         arg[j] = locale_to_utf8(strv[i+1]); // FREE ME
260                         j++;
261                         table[j] = -1;
262                         arg[j] = NULL;
263                         i = i + 2;
264                         advanced_search_mode = TRUE;
265                 }
266         }
268         g_strfreev(strv);
271         if (advanced_search_mode && j > 0) {
272                 int iter;
273                 struct mpd_entity *entity;
275                 /*-----------------------------------------------------------------------
276                  * NOTE (again): This code exists to test a new search ui,
277                  *               Its ugly and MUST be redesigned before the next release!
278                  *             + the code below should live in mpdclient.c
279                  *-----------------------------------------------------------------------
280                  */
281                 /** stupid - but this is just a test...... (fulhack)  */
282                 mpd_search_db_songs(c->connection, false);
284                 for(iter = 0; iter < 10 && arg[iter] != NULL; iter++) {
285                         if (table[iter] == SEARCH_URI)
286                                 mpd_search_add_uri_constraint(c->connection,
287                                                               MPD_OPERATOR_DEFAULT,
288                                                               arg[iter]);
289                         else
290                                 mpd_search_add_tag_constraint(c->connection,
291                                                               MPD_OPERATOR_DEFAULT,
292                                                               table[iter], arg[iter]);
293                 }
295                 mpd_search_commit(c->connection);
297                 fl = filelist_new();
299                 while ((entity = mpd_recv_entity(c->connection)) != NULL)
300                         filelist_append(fl, entity);
302                 if (mpdclient_finish_command(c)) {
303                         filelist_free(fl);
304                         fl = NULL;
305                 }
306         }
308         i=0;
309         while( arg[i] )
310                 g_free(arg[i++]);
312         return fl;
315 static void
316 search_new(struct mpdclient *c)
318         if (c->connection == NULL)
319                 return;
321         search_clear(c, TRUE);
323         g_free(pattern);
324         pattern = screen_readln(screen.status_window.w,
325                                 _("Search"),
326                                 NULL,
327                                 &search_history,
328                                 NULL);
330         if (pattern == NULL) {
331                 list_window_reset(browser.lw);
332                 return;
333         }
335         if (browser.filelist != NULL) {
336                 filelist_free(browser.filelist);
337                 browser.filelist = NULL;
338         }
340         if (mpd_connection_cmp_server_version(c->connection, 0, 12, 0) >= 0)
341                 browser.filelist = search_advanced_query(pattern, c);
343         if (!advanced_search_mode && browser.filelist == NULL)
344                 browser.filelist = filelist_search(c, FALSE,
345                                                   mode[options.search_mode].table,
346                                                   pattern);
348         if (browser.filelist == NULL)
349                 browser.filelist = filelist_new();
351         sync_highlights(c, browser.filelist);
352         mpdclient_install_playlist_callback(c, playlist_changed_callback);
353         list_window_check_selected(browser.lw, filelist_length(browser.filelist));
358 static void
359 init(WINDOW *w, int cols, int rows)
361         browser.lw = list_window_init(w, cols, rows);
364 static void
365 quit(void)
367         if (search_history)
368                 string_list_free(search_history);
369         if (browser.filelist)
370                 filelist_free(browser.filelist);
371         list_window_free(browser.lw);
373         if (pattern) {
374                 g_free(pattern);
375                 pattern = NULL;
376         }
379 static void
380 open(G_GNUC_UNUSED struct mpdclient *c)
382         //  if( pattern==NULL )
383         //    search_new(screen, c);
384         // else
385         screen_status_printf(_("Press %s for a new search"),
386                              get_key_names(CMD_SCREEN_SEARCH,0));
387         search_check_mode();
390 static void
391 resize(int cols, int rows)
393         browser.lw->cols = cols;
394         browser.lw->rows = rows;
397 static void
398 paint(void)
400         if (browser.filelist) {
401                 browser.lw->hide_cursor = false;
402                 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
403         } else {
404                 browser.lw->hide_cursor = true;
405                 list_window_paint(browser.lw, lw_search_help_callback, NULL);
406         }
409 static const char *
410 get_title(char *str, size_t size)
412         if (advanced_search_mode && pattern)
413                 g_snprintf(str, size, _("Search: %s"), pattern);
414         else if (pattern)
415                 g_snprintf(str, size,
416                            _("Search: Results for %s [%s]"),
417                            pattern,
418                            _(mode[options.search_mode].label));
419         else
420                 g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
421                            get_key_names(CMD_SCREEN_SEARCH,0),
422                            _(mode[options.search_mode].label));
424         return str;
427 static bool
428 search_cmd(struct mpdclient *c, command_t cmd)
430         switch (cmd) {
431         case CMD_SEARCH_MODE:
432                 options.search_mode++;
433                 if (mode[options.search_mode].label == NULL)
434                         options.search_mode = 0;
435                 screen_status_printf(_("Search mode: %s"),
436                                      _(mode[options.search_mode].label));
437                 /* continue and update... */
438         case CMD_SCREEN_UPDATE:
439                 if (pattern) {
440                         search_clear(c, FALSE);
441                         browser.filelist = filelist_search(c,
442                                                           FALSE,
443                                                           mode[options.search_mode].table,
444                                                           pattern);
445                         sync_highlights(c, browser.filelist);
446                 }
447                 search_repaint();
448                 return true;
450         case CMD_SCREEN_SEARCH:
451                 search_new(c);
452                 search_repaint();
453                 return true;
455         case CMD_CLEAR:
456                 search_clear(c, TRUE);
457                 list_window_reset(browser.lw);
458                 search_repaint();
459                 return true;
461         default:
462                 break;
463         }
465         if (browser.filelist != NULL &&
466             browser_cmd(&browser, c, cmd)) {
467                 if (screen_is_visible(&screen_search))
468                         search_repaint();
469                 return true;
470         }
472         return false;
475 const struct screen_functions screen_search = {
476         .init = init,
477         .exit = quit,
478         .open = open,
479         .resize = resize,
480         .paint = paint,
481         .cmd = search_cmd,
482         .get_title = get_title,
483 };