8dfec1ff920c1bce707bfec565b7afbacb67cfc4
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)
102 {
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;
124 }
126 static void
127 screen_search_paint(void);
129 static void
130 search_repaint(void)
131 {
132 screen_search_paint();
133 wrefresh(browser.lw->w);
134 }
136 /* sanity check search mode value */
137 static void
138 search_check_mode(void)
139 {
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;
148 }
150 static void
151 search_clear(bool clear_pattern)
152 {
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 }
161 }
163 static struct filelist *
164 search_simple_query(struct mpdclient *c, G_GNUC_UNUSED int exact_match,
165 int table, gchar *local_pattern)
166 {
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;
192 }
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(struct mpd_connection *connection, char *query)
201 {
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);
248 if (!advanced_search_mode || j == 0) {
249 for (i = 0; arg[i] != NULL; ++i)
250 g_free(arg[i]);
251 return NULL;
252 }
254 /*-----------------------------------------------------------------------
255 * NOTE (again): This code exists to test a new search ui,
256 * Its ugly and MUST be redesigned before the next release!
257 * + the code below should live in mpdclient.c
258 *-----------------------------------------------------------------------
259 */
260 /** stupid - but this is just a test...... (fulhack) */
261 mpd_search_db_songs(connection, false);
263 for (i = 0; i < 10 && arg[i] != NULL; i++) {
264 if (table[i] == SEARCH_URI)
265 mpd_search_add_uri_constraint(connection,
266 MPD_OPERATOR_DEFAULT,
267 arg[i]);
268 else
269 mpd_search_add_tag_constraint(connection,
270 MPD_OPERATOR_DEFAULT,
271 table[i], arg[i]);
272 }
274 mpd_search_commit(connection);
275 fl = filelist_new_recv(connection);
276 if (!mpd_response_finish(connection)) {
277 filelist_free(fl);
278 fl = NULL;
279 }
281 for (i = 0; arg[i] != NULL; ++i)
282 g_free(arg[i]);
284 return fl;
285 }
287 static struct filelist *
288 do_search(struct mpdclient *c, char *query)
289 {
290 struct filelist *fl;
292 fl = search_advanced_query(c->connection, query);
293 if (fl != NULL)
294 return fl;
296 if (mpd_connection_get_error(c->connection) != MPD_ERROR_SUCCESS) {
297 mpdclient_handle_error(c);
298 return NULL;
299 }
301 return search_simple_query(c, FALSE,
302 mode[options.search_mode].table,
303 query);
304 }
306 static void
307 search_new(struct mpdclient *c)
308 {
309 if (!mpdclient_is_connected(c))
310 return;
312 search_clear(true);
314 g_free(pattern);
315 pattern = screen_readln(_("Search"),
316 NULL,
317 &search_history,
318 NULL);
320 if (pattern == NULL) {
321 list_window_reset(browser.lw);
322 return;
323 }
325 if (browser.filelist != NULL) {
326 filelist_free(browser.filelist);
327 browser.filelist = NULL;
328 }
330 browser.filelist = do_search(c, pattern);
331 if (browser.filelist == NULL)
332 browser.filelist = filelist_new();
334 screen_browser_sync_highlights(browser.filelist, &c->playlist);
335 list_window_check_selected(browser.lw, filelist_length(browser.filelist));
336 }
338 static void
339 screen_search_init(WINDOW *w, int cols, int rows)
340 {
341 browser.lw = list_window_init(w, cols, rows);
342 }
344 static void
345 screen_search_quit(void)
346 {
347 if (search_history)
348 string_list_free(search_history);
349 if (browser.filelist)
350 filelist_free(browser.filelist);
351 list_window_free(browser.lw);
353 if (pattern) {
354 g_free(pattern);
355 pattern = NULL;
356 }
357 }
359 static void
360 screen_search_open(G_GNUC_UNUSED struct mpdclient *c)
361 {
362 // if( pattern==NULL )
363 // search_new(screen, c);
364 // else
365 screen_status_printf(_("Press %s for a new search"),
366 get_key_names(CMD_SCREEN_SEARCH,0));
367 search_check_mode();
368 }
370 static void
371 screen_search_resize(int cols, int rows)
372 {
373 browser.lw->cols = cols;
374 browser.lw->rows = rows;
375 }
377 static void
378 screen_search_paint(void)
379 {
380 if (browser.filelist) {
381 browser.lw->hide_cursor = false;
382 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
383 } else {
384 browser.lw->hide_cursor = true;
385 list_window_paint(browser.lw, lw_search_help_callback, NULL);
386 }
387 }
389 static const char *
390 screen_search_get_title(char *str, size_t size)
391 {
392 if (advanced_search_mode && pattern)
393 g_snprintf(str, size, _("Search: %s"), pattern);
394 else if (pattern)
395 g_snprintf(str, size,
396 _("Search: Results for %s [%s]"),
397 pattern,
398 _(mode[options.search_mode].label));
399 else
400 g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
401 get_key_names(CMD_SCREEN_SEARCH,0),
402 _(mode[options.search_mode].label));
404 return str;
405 }
407 static void
408 screen_search_update(struct mpdclient *c)
409 {
410 if (browser.filelist != NULL && c->events & MPD_IDLE_PLAYLIST) {
411 screen_browser_sync_highlights(browser.filelist, &c->playlist);
412 search_repaint();
413 }
414 }
416 static bool
417 screen_search_cmd(struct mpdclient *c, command_t cmd)
418 {
419 switch (cmd) {
420 case CMD_SEARCH_MODE:
421 options.search_mode++;
422 if (mode[options.search_mode].label == NULL)
423 options.search_mode = 0;
424 screen_status_printf(_("Search mode: %s"),
425 _(mode[options.search_mode].label));
426 /* continue and update... */
427 case CMD_SCREEN_UPDATE:
428 if (pattern) {
429 search_clear(false);
430 browser.filelist = do_search(c, pattern);
431 screen_browser_sync_highlights(browser.filelist,
432 &c->playlist);
433 }
434 search_repaint();
435 return true;
437 case CMD_SCREEN_SEARCH:
438 search_new(c);
439 search_repaint();
440 return true;
442 case CMD_CLEAR:
443 search_clear(true);
444 list_window_reset(browser.lw);
445 search_repaint();
446 return true;
448 default:
449 break;
450 }
452 if (browser.filelist != NULL &&
453 browser_cmd(&browser, c, cmd)) {
454 if (screen_is_visible(&screen_search))
455 search_repaint();
456 return true;
457 }
459 return false;
460 }
462 const struct screen_functions screen_search = {
463 .init = screen_search_init,
464 .exit = screen_search_quit,
465 .open = screen_search_open,
466 .resize = screen_search_resize,
467 .paint = screen_search_paint,
468 .update = screen_search_update,
469 .cmd = screen_search_cmd,
470 .get_title = screen_search_get_title,
471 };