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 mpd_connection *connection, bool exact_match,
165 int table, gchar *local_pattern)
166 {
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);
186 filelist_no_duplicates(list);
187 } else if (table == SEARCH_URI) {
188 mpd_search_db_songs(connection, exact_match);
189 mpd_search_add_uri_constraint(connection, MPD_OPERATOR_DEFAULT,
190 filter_utf8);
191 mpd_search_commit(connection);
193 list = filelist_new_recv(connection);
194 } else {
195 mpd_search_db_songs(connection, exact_match);
196 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
197 table, filter_utf8);
198 mpd_search_commit(connection);
200 list = filelist_new_recv(connection);
201 }
203 g_free(filter_utf8);
204 return list;
205 }
207 /*-----------------------------------------------------------------------
208 * NOTE: This code exists to test a new search ui,
209 * Its ugly and MUST be redesigned before the next release!
210 *-----------------------------------------------------------------------
211 */
212 static struct filelist *
213 search_advanced_query(struct mpd_connection *connection, char *query)
214 {
215 int i,j;
216 char **strv;
217 int table[10];
218 char *arg[10];
219 struct filelist *fl = NULL;
221 advanced_search_mode = FALSE;
222 if (strchr(query, ':') == NULL)
223 return NULL;
225 strv = g_strsplit_set(query, ": ", 0);
227 memset(table, 0, 10*sizeof(int));
228 memset(arg, 0, 10*sizeof(char *));
230 i=0;
231 j=0;
232 while (strv[i] && strlen(strv[i]) > 0 && i < 9) {
233 int id = search_get_tag_id(strv[i]);
234 if (id == -1) {
235 if (table[j]) {
236 char *tmp = arg[j];
237 arg[j] = g_strdup_printf("%s %s", arg[j], strv[i]);
238 g_free(tmp);
239 } else {
240 screen_status_printf(_("Bad search tag %s"), strv[i]);
241 }
242 i++;
243 } else if (strv[i+1] == NULL || strlen(strv[i+1]) == 0) {
244 screen_status_printf(_("No argument for search tag %s"), strv[i]);
245 i++;
246 // j--;
247 //table[j] = -1;
248 } else {
249 table[j] = id;
250 arg[j] = locale_to_utf8(strv[i+1]); // FREE ME
251 j++;
252 table[j] = -1;
253 arg[j] = NULL;
254 i = i + 2;
255 advanced_search_mode = TRUE;
256 }
257 }
259 g_strfreev(strv);
261 if (!advanced_search_mode || j == 0) {
262 for (i = 0; arg[i] != NULL; ++i)
263 g_free(arg[i]);
264 return NULL;
265 }
267 /*-----------------------------------------------------------------------
268 * NOTE (again): This code exists to test a new search ui,
269 * Its ugly and MUST be redesigned before the next release!
270 * + the code below should live in mpdclient.c
271 *-----------------------------------------------------------------------
272 */
273 /** stupid - but this is just a test...... (fulhack) */
274 mpd_search_db_songs(connection, false);
276 for (i = 0; i < 10 && arg[i] != NULL; i++) {
277 if (table[i] == SEARCH_URI)
278 mpd_search_add_uri_constraint(connection,
279 MPD_OPERATOR_DEFAULT,
280 arg[i]);
281 else
282 mpd_search_add_tag_constraint(connection,
283 MPD_OPERATOR_DEFAULT,
284 table[i], arg[i]);
285 }
287 mpd_search_commit(connection);
288 fl = filelist_new_recv(connection);
289 if (!mpd_response_finish(connection)) {
290 filelist_free(fl);
291 fl = NULL;
292 }
294 for (i = 0; arg[i] != NULL; ++i)
295 g_free(arg[i]);
297 return fl;
298 }
300 static struct filelist *
301 do_search(struct mpdclient *c, char *query)
302 {
303 struct mpd_connection *connection = mpdclient_get_connection(c);
304 struct filelist *fl;
306 fl = search_advanced_query(connection, query);
307 if (fl != NULL)
308 return fl;
310 if (mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS) {
311 mpdclient_handle_error(c);
312 return NULL;
313 }
315 fl = search_simple_query(connection, FALSE,
316 mode[options.search_mode].table,
317 query);
318 if (fl == NULL)
319 mpdclient_handle_error(c);
320 return fl;
321 }
323 static void
324 screen_search_reload(struct mpdclient *c)
325 {
326 if (pattern == NULL)
327 return;
329 if (browser.filelist != NULL) {
330 filelist_free(browser.filelist);
331 browser.filelist = NULL;
332 }
334 browser.filelist = do_search(c, pattern);
335 if (browser.filelist == NULL)
336 browser.filelist = filelist_new();
338 screen_browser_sync_highlights(browser.filelist, &c->playlist);
339 list_window_check_selected(browser.lw,
340 filelist_length(browser.filelist));
341 }
343 static void
344 search_new(struct mpdclient *c)
345 {
346 if (!mpdclient_is_connected(c))
347 return;
349 search_clear(true);
351 g_free(pattern);
352 pattern = screen_readln(_("Search"),
353 NULL,
354 &search_history,
355 NULL);
357 if (pattern == NULL) {
358 list_window_reset(browser.lw);
359 return;
360 }
362 screen_search_reload(c);
363 }
365 static void
366 screen_search_init(WINDOW *w, int cols, int rows)
367 {
368 browser.lw = list_window_init(w, cols, rows);
369 }
371 static void
372 screen_search_quit(void)
373 {
374 if (search_history)
375 string_list_free(search_history);
376 if (browser.filelist)
377 filelist_free(browser.filelist);
378 list_window_free(browser.lw);
380 if (pattern) {
381 g_free(pattern);
382 pattern = NULL;
383 }
384 }
386 static void
387 screen_search_open(G_GNUC_UNUSED struct mpdclient *c)
388 {
389 // if( pattern==NULL )
390 // search_new(screen, c);
391 // else
392 screen_status_printf(_("Press %s for a new search"),
393 get_key_names(CMD_SCREEN_SEARCH,0));
394 search_check_mode();
395 }
397 static void
398 screen_search_resize(int cols, int rows)
399 {
400 browser.lw->cols = cols;
401 browser.lw->rows = rows;
402 }
404 static void
405 screen_search_paint(void)
406 {
407 if (browser.filelist) {
408 browser.lw->hide_cursor = false;
409 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
410 } else {
411 browser.lw->hide_cursor = true;
412 list_window_paint(browser.lw, lw_search_help_callback, NULL);
413 }
414 }
416 static const char *
417 screen_search_get_title(char *str, size_t size)
418 {
419 if (advanced_search_mode && pattern)
420 g_snprintf(str, size, _("Search: %s"), pattern);
421 else if (pattern)
422 g_snprintf(str, size,
423 _("Search: Results for %s [%s]"),
424 pattern,
425 _(mode[options.search_mode].label));
426 else
427 g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
428 get_key_names(CMD_SCREEN_SEARCH,0),
429 _(mode[options.search_mode].label));
431 return str;
432 }
434 static void
435 screen_search_update(struct mpdclient *c)
436 {
437 if (browser.filelist != NULL && c->events & MPD_IDLE_PLAYLIST) {
438 screen_browser_sync_highlights(browser.filelist, &c->playlist);
439 search_repaint();
440 }
441 }
443 static bool
444 screen_search_cmd(struct mpdclient *c, command_t cmd)
445 {
446 switch (cmd) {
447 case CMD_SEARCH_MODE:
448 options.search_mode++;
449 if (mode[options.search_mode].label == NULL)
450 options.search_mode = 0;
451 screen_status_printf(_("Search mode: %s"),
452 _(mode[options.search_mode].label));
453 /* continue and update... */
454 case CMD_SCREEN_UPDATE:
455 screen_search_reload(c);
456 search_repaint();
457 return true;
459 case CMD_SCREEN_SEARCH:
460 search_new(c);
461 search_repaint();
462 return true;
464 case CMD_CLEAR:
465 search_clear(true);
466 list_window_reset(browser.lw);
467 search_repaint();
468 return true;
470 default:
471 break;
472 }
474 if (browser.filelist != NULL &&
475 browser_cmd(&browser, c, cmd)) {
476 if (screen_is_visible(&screen_search))
477 search_repaint();
478 return true;
479 }
481 return false;
482 }
484 const struct screen_functions screen_search = {
485 .init = screen_search_init,
486 .exit = screen_search_quit,
487 .open = screen_search_open,
488 .resize = screen_search_resize,
489 .paint = screen_search_paint,
490 .update = screen_search_update,
491 .cmd = screen_search_cmd,
492 .get_title = screen_search_get_title,
493 };