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