85f817964008ebe0b7f93050c4d3c6c31d42bee3
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 filelist *fl;
305 fl = search_advanced_query(c->connection, query);
306 if (fl != NULL)
307 return fl;
309 if (mpd_connection_get_error(c->connection) != MPD_ERROR_SUCCESS) {
310 mpdclient_handle_error(c);
311 return NULL;
312 }
314 fl = search_simple_query(c->connection, FALSE,
315 mode[options.search_mode].table,
316 query);
317 if (fl == NULL)
318 mpdclient_handle_error(c);
319 return fl;
320 }
322 static void
323 screen_search_reload(struct mpdclient *c)
324 {
325 if (pattern == NULL)
326 return;
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,
339 filelist_length(browser.filelist));
340 }
342 static void
343 search_new(struct mpdclient *c)
344 {
345 if (!mpdclient_is_connected(c))
346 return;
348 search_clear(true);
350 g_free(pattern);
351 pattern = screen_readln(_("Search"),
352 NULL,
353 &search_history,
354 NULL);
356 if (pattern == NULL) {
357 list_window_reset(browser.lw);
358 return;
359 }
361 screen_search_reload(c);
362 }
364 static void
365 screen_search_init(WINDOW *w, int cols, int rows)
366 {
367 browser.lw = list_window_init(w, cols, rows);
368 }
370 static void
371 screen_search_quit(void)
372 {
373 if (search_history)
374 string_list_free(search_history);
375 if (browser.filelist)
376 filelist_free(browser.filelist);
377 list_window_free(browser.lw);
379 if (pattern) {
380 g_free(pattern);
381 pattern = NULL;
382 }
383 }
385 static void
386 screen_search_open(G_GNUC_UNUSED struct mpdclient *c)
387 {
388 // if( pattern==NULL )
389 // search_new(screen, c);
390 // else
391 screen_status_printf(_("Press %s for a new search"),
392 get_key_names(CMD_SCREEN_SEARCH,0));
393 search_check_mode();
394 }
396 static void
397 screen_search_resize(int cols, int rows)
398 {
399 browser.lw->cols = cols;
400 browser.lw->rows = rows;
401 }
403 static void
404 screen_search_paint(void)
405 {
406 if (browser.filelist) {
407 browser.lw->hide_cursor = false;
408 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
409 } else {
410 browser.lw->hide_cursor = true;
411 list_window_paint(browser.lw, lw_search_help_callback, NULL);
412 }
413 }
415 static const char *
416 screen_search_get_title(char *str, size_t size)
417 {
418 if (advanced_search_mode && pattern)
419 g_snprintf(str, size, _("Search: %s"), pattern);
420 else if (pattern)
421 g_snprintf(str, size,
422 _("Search: Results for %s [%s]"),
423 pattern,
424 _(mode[options.search_mode].label));
425 else
426 g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
427 get_key_names(CMD_SCREEN_SEARCH,0),
428 _(mode[options.search_mode].label));
430 return str;
431 }
433 static void
434 screen_search_update(struct mpdclient *c)
435 {
436 if (browser.filelist != NULL && c->events & MPD_IDLE_PLAYLIST) {
437 screen_browser_sync_highlights(browser.filelist, &c->playlist);
438 search_repaint();
439 }
440 }
442 static bool
443 screen_search_cmd(struct mpdclient *c, command_t cmd)
444 {
445 switch (cmd) {
446 case CMD_SEARCH_MODE:
447 options.search_mode++;
448 if (mode[options.search_mode].label == NULL)
449 options.search_mode = 0;
450 screen_status_printf(_("Search mode: %s"),
451 _(mode[options.search_mode].label));
452 /* continue and update... */
453 case CMD_SCREEN_UPDATE:
454 screen_search_reload(c);
455 search_repaint();
456 return true;
458 case CMD_SCREEN_SEARCH:
459 search_new(c);
460 search_repaint();
461 return true;
463 case CMD_CLEAR:
464 search_clear(true);
465 list_window_reset(browser.lw);
466 search_repaint();
467 return true;
469 default:
470 break;
471 }
473 if (browser.filelist != NULL &&
474 browser_cmd(&browser, c, cmd)) {
475 if (screen_is_visible(&screen_search))
476 search_repaint();
477 return true;
478 }
480 return false;
481 }
483 const struct screen_functions screen_search = {
484 .init = screen_search_init,
485 .exit = screen_search_quit,
486 .open = screen_search_open,
487 .resize = screen_search_resize,
488 .paint = screen_search_paint,
489 .update = screen_search_update,
490 .cmd = screen_search_cmd,
491 .get_title = screen_search_get_title,
492 };