Code

screen_search: use strchr() instead of strrstr()
[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 (strchr(query, ':') == NULL)
210                 return NULL;
212         strv = g_strsplit_set(query, ": ", 0);
214         memset(table, 0, 10*sizeof(int));
215         memset(arg, 0, 10*sizeof(char *));
217         i=0;
218         j=0;
219         while (strv[i] && strlen(strv[i]) > 0 && i < 9) {
220                 int id = search_get_tag_id(strv[i]);
221                 if (id == -1) {
222                         if (table[j]) {
223                                 char *tmp = arg[j];
224                                 arg[j] = g_strdup_printf("%s %s", arg[j], strv[i]);
225                                 g_free(tmp);
226                         } else {
227                                 screen_status_printf(_("Bad search tag %s"), strv[i]);
228                         }
229                         i++;
230                 } else if (strv[i+1] == NULL || strlen(strv[i+1]) == 0) {
231                         screen_status_printf(_("No argument for search tag %s"), strv[i]);
232                         i++;
233                         //        j--;
234                         //table[j] = -1;
235                 } else {
236                         table[j] = id;
237                         arg[j] = locale_to_utf8(strv[i+1]); // FREE ME
238                         j++;
239                         table[j] = -1;
240                         arg[j] = NULL;
241                         i = i + 2;
242                         advanced_search_mode = TRUE;
243                 }
244         }
246         g_strfreev(strv);
249         if (advanced_search_mode && j > 0) {
250                 int iter;
251                 struct mpd_entity *entity;
253                 /*-----------------------------------------------------------------------
254                  * NOTE (again): This code exists to test a new search ui,
255                  *               Its ugly and MUST be redesigned before the next release!
256                  *             + the code below should live in mpdclient.c
257                  *-----------------------------------------------------------------------
258                  */
259                 /** stupid - but this is just a test...... (fulhack)  */
260                 mpd_search_db_songs(c->connection, false);
262                 for(iter = 0; iter < 10 && arg[iter] != NULL; iter++) {
263                         if (table[iter] == SEARCH_URI)
264                                 mpd_search_add_uri_constraint(c->connection,
265                                                               MPD_OPERATOR_DEFAULT,
266                                                               arg[iter]);
267                         else
268                                 mpd_search_add_tag_constraint(c->connection,
269                                                               MPD_OPERATOR_DEFAULT,
270                                                               table[iter], arg[iter]);
271                 }
273                 mpd_search_commit(c->connection);
275                 fl = filelist_new();
277                 while ((entity = mpd_recv_entity(c->connection)) != NULL)
278                         filelist_append(fl, entity);
280                 if (!mpd_response_finish(c->connection)) {
281                         filelist_free(fl);
282                         fl = NULL;
284                         mpdclient_handle_error(c);
285                 }
286         }
288         i=0;
289         while( arg[i] )
290                 g_free(arg[i++]);
292         return fl;
295 static struct filelist *
296 do_search(struct mpdclient *c, char *query)
298         struct filelist *fl;
300         fl = search_advanced_query(query, c);
301         if (!advanced_search_mode && browser.filelist == NULL)
302                 return search_simple_query(c, FALSE,
303                                            mode[options.search_mode].table,
304                                            query);
306         return fl;
309 static void
310 search_new(struct mpdclient *c)
312         if (!mpdclient_is_connected(c))
313                 return;
315         search_clear(true);
317         g_free(pattern);
318         pattern = screen_readln(_("Search"),
319                                 NULL,
320                                 &search_history,
321                                 NULL);
323         if (pattern == NULL) {
324                 list_window_reset(browser.lw);
325                 return;
326         }
328         if (browser.filelist != NULL) {
329                 filelist_free(browser.filelist);
330                 browser.filelist = NULL;
331         }
333         browser.filelist = do_search(c, pattern);
334         if (browser.filelist == NULL)
335                 browser.filelist = filelist_new();
337         screen_browser_sync_highlights(browser.filelist, &c->playlist);
338         list_window_check_selected(browser.lw, filelist_length(browser.filelist));
341 static void
342 screen_search_init(WINDOW *w, int cols, int rows)
344         browser.lw = list_window_init(w, cols, rows);
347 static void
348 screen_search_quit(void)
350         if (search_history)
351                 string_list_free(search_history);
352         if (browser.filelist)
353                 filelist_free(browser.filelist);
354         list_window_free(browser.lw);
356         if (pattern) {
357                 g_free(pattern);
358                 pattern = NULL;
359         }
362 static void
363 screen_search_open(G_GNUC_UNUSED struct mpdclient *c)
365         //  if( pattern==NULL )
366         //    search_new(screen, c);
367         // else
368         screen_status_printf(_("Press %s for a new search"),
369                              get_key_names(CMD_SCREEN_SEARCH,0));
370         search_check_mode();
373 static void
374 screen_search_resize(int cols, int rows)
376         browser.lw->cols = cols;
377         browser.lw->rows = rows;
380 static void
381 screen_search_paint(void)
383         if (browser.filelist) {
384                 browser.lw->hide_cursor = false;
385                 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
386         } else {
387                 browser.lw->hide_cursor = true;
388                 list_window_paint(browser.lw, lw_search_help_callback, NULL);
389         }
392 static const char *
393 screen_search_get_title(char *str, size_t size)
395         if (advanced_search_mode && pattern)
396                 g_snprintf(str, size, _("Search: %s"), pattern);
397         else if (pattern)
398                 g_snprintf(str, size,
399                            _("Search: Results for %s [%s]"),
400                            pattern,
401                            _(mode[options.search_mode].label));
402         else
403                 g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
404                            get_key_names(CMD_SCREEN_SEARCH,0),
405                            _(mode[options.search_mode].label));
407         return str;
410 static void
411 screen_search_update(struct mpdclient *c)
413         if (browser.filelist != NULL && c->events & MPD_IDLE_PLAYLIST) {
414                 screen_browser_sync_highlights(browser.filelist, &c->playlist);
415                 search_repaint();
416         }
419 static bool
420 screen_search_cmd(struct mpdclient *c, command_t cmd)
422         switch (cmd) {
423         case CMD_SEARCH_MODE:
424                 options.search_mode++;
425                 if (mode[options.search_mode].label == NULL)
426                         options.search_mode = 0;
427                 screen_status_printf(_("Search mode: %s"),
428                                      _(mode[options.search_mode].label));
429                 /* continue and update... */
430         case CMD_SCREEN_UPDATE:
431                 if (pattern) {
432                         search_clear(false);
433                         browser.filelist = do_search(c, pattern);
434                         screen_browser_sync_highlights(browser.filelist,
435                                                        &c->playlist);
436                 }
437                 search_repaint();
438                 return true;
440         case CMD_SCREEN_SEARCH:
441                 search_new(c);
442                 search_repaint();
443                 return true;
445         case CMD_CLEAR:
446                 search_clear(true);
447                 list_window_reset(browser.lw);
448                 search_repaint();
449                 return true;
451         default:
452                 break;
453         }
455         if (browser.filelist != NULL &&
456             browser_cmd(&browser, c, cmd)) {
457                 if (screen_is_visible(&screen_search))
458                         search_repaint();
459                 return true;
460         }
462         return false;
465 const struct screen_functions screen_search = {
466         .init = screen_search_init,
467         .exit = screen_search_quit,
468         .open = screen_search_open,
469         .resize = screen_search_resize,
470         .paint = screen_search_paint,
471         .update = screen_search_update,
472         .cmd = screen_search_cmd,
473         .get_title = screen_search_get_title,
474 };