1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2010 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_("Filename") },
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;
97 static const char *const help_text[] = {
98 "Quick - Enter a string and ncmpc will search according",
99 " to the current search mode (displayed above).",
100 "",
101 "Advanced - <tag>:<search term> [<tag>:<search term>...]",
102 " Example: artist:radiohead album:pablo honey",
103 "",
104 " Available tags: artist, album, title, track,",
105 " name, genre, date composer, performer, comment, file",
106 "",
107 };
109 /* search info */
110 static const char *
111 lw_search_help_callback(unsigned idx, G_GNUC_UNUSED void *data)
112 {
113 assert(idx < G_N_ELEMENTS(help_text));
115 return help_text[idx];
116 }
118 static void
119 screen_search_paint(void);
121 static void
122 search_repaint(void)
123 {
124 screen_search_paint();
125 wrefresh(browser.lw->w);
126 }
128 /* sanity check search mode value */
129 static void
130 search_check_mode(void)
131 {
132 int max = 0;
134 while (mode[max].label != NULL)
135 max++;
136 if (options.search_mode < 0)
137 options.search_mode = 0;
138 else if (options.search_mode >= max)
139 options.search_mode = max-1;
140 }
142 static void
143 search_clear(bool clear_pattern)
144 {
145 if (browser.filelist) {
146 filelist_free(browser.filelist);
147 browser.filelist = filelist_new();
148 list_window_set_length(browser.lw, 0);
149 }
150 if (clear_pattern && pattern) {
151 g_free(pattern);
152 pattern = NULL;
153 }
154 }
156 static struct filelist *
157 search_simple_query(struct mpd_connection *connection, bool exact_match,
158 int table, gchar *local_pattern)
159 {
160 struct filelist *list;
161 gchar *filter_utf8 = locale_to_utf8(local_pattern);
163 if (table == SEARCH_ARTIST_TITLE) {
164 mpd_command_list_begin(connection, false);
166 mpd_search_db_songs(connection, exact_match);
167 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
168 MPD_TAG_ARTIST, filter_utf8);
169 mpd_search_commit(connection);
171 mpd_search_db_songs(connection, exact_match);
172 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
173 MPD_TAG_TITLE, filter_utf8);
174 mpd_search_commit(connection);
176 mpd_command_list_end(connection);
178 list = filelist_new_recv(connection);
179 filelist_no_duplicates(list);
180 } else if (table == SEARCH_URI) {
181 mpd_search_db_songs(connection, exact_match);
182 mpd_search_add_uri_constraint(connection, MPD_OPERATOR_DEFAULT,
183 filter_utf8);
184 mpd_search_commit(connection);
186 list = filelist_new_recv(connection);
187 } else {
188 mpd_search_db_songs(connection, exact_match);
189 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
190 table, filter_utf8);
191 mpd_search_commit(connection);
193 list = filelist_new_recv(connection);
194 }
196 g_free(filter_utf8);
197 return list;
198 }
200 /*-----------------------------------------------------------------------
201 * NOTE: This code exists to test a new search ui,
202 * Its ugly and MUST be redesigned before the next release!
203 *-----------------------------------------------------------------------
204 */
205 static struct filelist *
206 search_advanced_query(struct mpd_connection *connection, char *query)
207 {
208 int i,j;
209 char **strv;
210 int table[10];
211 char *arg[10];
212 struct filelist *fl = NULL;
214 advanced_search_mode = FALSE;
215 if (strchr(query, ':') == NULL)
216 return NULL;
218 strv = g_strsplit_set(query, ": ", 0);
220 memset(table, 0, 10*sizeof(int));
221 memset(arg, 0, 10*sizeof(char *));
223 i=0;
224 j=0;
225 while (strv[i] && strlen(strv[i]) > 0 && i < 9) {
226 int id = search_get_tag_id(strv[i]);
227 if (id == -1) {
228 if (table[j]) {
229 char *tmp = arg[j];
230 arg[j] = g_strdup_printf("%s %s", arg[j], strv[i]);
231 g_free(tmp);
232 } else {
233 screen_status_printf(_("Bad search tag %s"), strv[i]);
234 }
235 i++;
236 } else if (strv[i+1] == NULL || strlen(strv[i+1]) == 0) {
237 screen_status_printf(_("No argument for search tag %s"), strv[i]);
238 i++;
239 // j--;
240 //table[j] = -1;
241 } else {
242 table[j] = id;
243 arg[j] = locale_to_utf8(strv[i+1]); // FREE ME
244 j++;
245 table[j] = -1;
246 arg[j] = NULL;
247 i = i + 2;
248 advanced_search_mode = TRUE;
249 }
250 }
252 g_strfreev(strv);
254 if (!advanced_search_mode || j == 0) {
255 for (i = 0; arg[i] != NULL; ++i)
256 g_free(arg[i]);
257 return NULL;
258 }
260 /*-----------------------------------------------------------------------
261 * NOTE (again): This code exists to test a new search ui,
262 * Its ugly and MUST be redesigned before the next release!
263 * + the code below should live in mpdclient.c
264 *-----------------------------------------------------------------------
265 */
266 /** stupid - but this is just a test...... (fulhack) */
267 mpd_search_db_songs(connection, false);
269 for (i = 0; i < 10 && arg[i] != NULL; i++) {
270 if (table[i] == SEARCH_URI)
271 mpd_search_add_uri_constraint(connection,
272 MPD_OPERATOR_DEFAULT,
273 arg[i]);
274 else
275 mpd_search_add_tag_constraint(connection,
276 MPD_OPERATOR_DEFAULT,
277 table[i], arg[i]);
278 }
280 mpd_search_commit(connection);
281 fl = filelist_new_recv(connection);
282 if (!mpd_response_finish(connection)) {
283 filelist_free(fl);
284 fl = NULL;
285 }
287 for (i = 0; arg[i] != NULL; ++i)
288 g_free(arg[i]);
290 return fl;
291 }
293 static struct filelist *
294 do_search(struct mpdclient *c, char *query)
295 {
296 struct mpd_connection *connection = mpdclient_get_connection(c);
297 struct filelist *fl;
299 fl = search_advanced_query(connection, query);
300 if (fl != NULL)
301 return fl;
303 if (mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS) {
304 mpdclient_handle_error(c);
305 return NULL;
306 }
308 fl = search_simple_query(connection, FALSE,
309 mode[options.search_mode].table,
310 query);
311 if (fl == NULL)
312 mpdclient_handle_error(c);
313 return fl;
314 }
316 static void
317 screen_search_reload(struct mpdclient *c)
318 {
319 if (pattern == NULL)
320 return;
322 if (browser.filelist != NULL) {
323 filelist_free(browser.filelist);
324 browser.filelist = NULL;
325 }
327 browser.filelist = do_search(c, pattern);
328 if (browser.filelist == NULL)
329 browser.filelist = filelist_new();
330 list_window_set_length(browser.lw, filelist_length(browser.filelist));
332 screen_browser_sync_highlights(browser.filelist, &c->playlist);
333 }
335 static void
336 search_new(struct mpdclient *c)
337 {
338 if (!mpdclient_is_connected(c))
339 return;
341 search_clear(true);
343 g_free(pattern);
344 pattern = screen_readln(_("Search"),
345 NULL,
346 &search_history,
347 NULL);
349 if (pattern == NULL) {
350 list_window_reset(browser.lw);
351 return;
352 }
354 screen_search_reload(c);
355 }
357 static void
358 screen_search_init(WINDOW *w, int cols, int rows)
359 {
360 browser.lw = list_window_init(w, cols, rows);
361 list_window_set_length(browser.lw, G_N_ELEMENTS(help_text));
362 }
364 static void
365 screen_search_quit(void)
366 {
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 }
377 }
379 static void
380 screen_search_open(G_GNUC_UNUSED struct mpdclient *c)
381 {
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();
388 }
390 static void
391 screen_search_resize(int cols, int rows)
392 {
393 list_window_resize(browser.lw, cols, rows);
394 }
396 static void
397 screen_search_paint(void)
398 {
399 if (browser.filelist) {
400 browser.lw->hide_cursor = false;
401 screen_browser_paint(&browser);
402 } else {
403 browser.lw->hide_cursor = true;
404 list_window_paint(browser.lw, lw_search_help_callback, NULL);
405 }
406 }
408 static const char *
409 screen_search_get_title(char *str, size_t size)
410 {
411 if (advanced_search_mode && pattern)
412 g_snprintf(str, size, _("Search: %s"), pattern);
413 else if (pattern)
414 g_snprintf(str, size,
415 _("Search: Results for %s [%s]"),
416 pattern,
417 _(mode[options.search_mode].label));
418 else
419 g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
420 get_key_names(CMD_SCREEN_SEARCH,0),
421 _(mode[options.search_mode].label));
423 return str;
424 }
426 static void
427 screen_search_update(struct mpdclient *c)
428 {
429 if (browser.filelist != NULL && c->events & MPD_IDLE_QUEUE) {
430 screen_browser_sync_highlights(browser.filelist, &c->playlist);
431 search_repaint();
432 }
433 }
435 static bool
436 screen_search_cmd(struct mpdclient *c, command_t cmd)
437 {
438 switch (cmd) {
439 case CMD_SEARCH_MODE:
440 options.search_mode++;
441 if (mode[options.search_mode].label == NULL)
442 options.search_mode = 0;
443 screen_status_printf(_("Search mode: %s"),
444 _(mode[options.search_mode].label));
445 /* continue and update... */
446 case CMD_SCREEN_UPDATE:
447 screen_search_reload(c);
448 search_repaint();
449 return true;
451 case CMD_SCREEN_SEARCH:
452 search_new(c);
453 search_repaint();
454 return true;
456 case CMD_CLEAR:
457 search_clear(true);
458 list_window_reset(browser.lw);
459 search_repaint();
460 return true;
462 default:
463 break;
464 }
466 if (browser.filelist != NULL &&
467 browser_cmd(&browser, c, cmd)) {
468 if (screen_is_visible(&screen_search))
469 search_repaint();
470 return true;
471 }
473 return false;
474 }
476 const struct screen_functions screen_search = {
477 .init = screen_search_init,
478 .exit = screen_search_quit,
479 .open = screen_search_open,
480 .resize = screen_search_resize,
481 .paint = screen_search_paint,
482 .update = screen_search_update,
483 .cmd = screen_search_cmd,
484 .get_title = screen_search_get_title,
485 };