d7e49aadea5960de2b88afe0e4322a95fe435790
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 "i18n.h"
23 #include "options.h"
24 #include "charset.h"
25 #include "mpdclient.h"
26 #include "strfsong.h"
27 #include "utils.h"
28 #include "screen_utils.h"
29 #include "screen_browser.h"
30 #include "filelist.h"
32 #include <string.h>
33 #include <glib.h>
35 enum {
36 SEARCH_URI = MPD_TAG_COUNT + 100,
37 };
39 static const struct {
40 const char *name;
41 const char *localname;
42 } search_tag[MPD_TAG_COUNT] = {
43 [MPD_TAG_ARTIST] = { "artist", N_("artist") },
44 [MPD_TAG_ALBUM] = { "album", N_("album") },
45 [MPD_TAG_TITLE] = { "title", N_("title") },
46 [MPD_TAG_TRACK] = { "track", N_("track") },
47 [MPD_TAG_NAME] = { "name", N_("name") },
48 [MPD_TAG_GENRE] = { "genre", N_("genre") },
49 [MPD_TAG_DATE] = { "date", N_("date") },
50 [MPD_TAG_COMPOSER] = { "composer", N_("composer") },
51 [MPD_TAG_PERFORMER] = { "performer", N_("performer") },
52 [MPD_TAG_COMMENT] = { "comment", N_("comment") },
53 };
55 static int
56 search_get_tag_id(const char *name)
57 {
58 unsigned i;
60 if (g_ascii_strcasecmp(name, "file") == 0 ||
61 strcasecmp(name, _("file")) == 0)
62 return SEARCH_URI;
64 for (i = 0; i < MPD_TAG_COUNT; ++i)
65 if (search_tag[i].name != NULL &&
66 (strcasecmp(search_tag[i].name, name) == 0 ||
67 strcasecmp(search_tag[i].localname, name) == 0))
68 return i;
70 return -1;
71 }
73 #define SEARCH_ARTIST_TITLE 999
75 typedef struct {
76 enum mpd_tag_type table;
77 const char *label;
78 } search_type_t;
80 static search_type_t mode[] = {
81 { MPD_TAG_TITLE, N_("Title") },
82 { MPD_TAG_ARTIST, N_("Artist") },
83 { MPD_TAG_ALBUM, N_("Album") },
84 { SEARCH_URI, N_("file") },
85 { SEARCH_ARTIST_TITLE, N_("Artist + Title") },
86 { 0, NULL }
87 };
89 static GList *search_history = NULL;
90 static gchar *pattern = NULL;
91 static gboolean advanced_search_mode = FALSE;
93 static struct screen_browser browser;
96 /* search info */
97 static const char *
98 lw_search_help_callback(unsigned idx, G_GNUC_UNUSED bool *highlight,
99 G_GNUC_UNUSED char** sc, G_GNUC_UNUSED void *data)
100 {
101 unsigned text_rows;
102 static const char *text[] = {
103 "Quick - Enter a string and ncmpc will search according",
104 " to the current search mode (displayed above).",
105 "",
106 "Advanced - <tag>:<search term> [<tag>:<search term>...]",
107 " Example: artist:radiohead album:pablo honey",
108 "",
109 " Available tags: artist, album, title, track,",
110 " name, genre, date composer, performer, comment, file",
111 "",
112 NULL
113 };
115 text_rows=0;
116 while (text[text_rows])
117 text_rows++;
119 if (idx < text_rows)
120 return text[idx];
121 return NULL;
122 }
124 static void
125 screen_search_paint(void);
127 static void
128 search_repaint(void)
129 {
130 screen_search_paint();
131 wrefresh(browser.lw->w);
132 }
134 /* sanity check search mode value */
135 static void
136 search_check_mode(void)
137 {
138 int max = 0;
140 while (mode[max].label != NULL)
141 max++;
142 if (options.search_mode < 0)
143 options.search_mode = 0;
144 else if (options.search_mode >= max)
145 options.search_mode = max-1;
146 }
148 static void
149 search_clear(bool clear_pattern)
150 {
151 if (browser.filelist) {
152 filelist_free(browser.filelist);
153 browser.filelist = filelist_new();
154 }
155 if (clear_pattern && pattern) {
156 g_free(pattern);
157 pattern = NULL;
158 }
159 }
161 static struct filelist *
162 filelist_search(struct mpdclient *c, G_GNUC_UNUSED int exact_match, int table,
163 gchar *local_pattern)
164 {
165 struct filelist *list, *list2;
166 gchar *filter_utf8 = locale_to_utf8(local_pattern);
168 if (table == SEARCH_ARTIST_TITLE) {
169 list = mpdclient_filelist_search(c, FALSE, MPD_TAG_ARTIST,
170 filter_utf8);
171 if (list == NULL)
172 list = filelist_new();
174 list2 = mpdclient_filelist_search(c, FALSE, MPD_TAG_TITLE,
175 filter_utf8);
176 if (list2 != NULL) {
177 filelist_move(list, list2);
178 filelist_free(list2);
179 }
181 filelist_sort_all(list, compare_filelistentry_format);
182 } else {
183 list = mpdclient_filelist_search(c, FALSE, table, filter_utf8);
184 if (list == NULL)
185 list = filelist_new();
186 }
188 g_free(filter_utf8);
189 return list;
190 }
192 /*-----------------------------------------------------------------------
193 * NOTE: This code exists to test a new search ui,
194 * Its ugly and MUST be redesigned before the next release!
195 *-----------------------------------------------------------------------
196 */
197 static struct filelist *
198 search_advanced_query(char *query, struct mpdclient *c)
199 {
200 int i,j;
201 char **strv;
202 int table[10];
203 char *arg[10];
204 struct filelist *fl = NULL;
206 advanced_search_mode = FALSE;
207 if( g_strrstr(query, ":") == NULL )
208 return NULL;
210 strv = g_strsplit_set(query, ": ", 0);
212 i=0;
213 while (strv[i]) {
214 i++;
215 }
217 memset(table, 0, 10*sizeof(int));
218 memset(arg, 0, 10*sizeof(char *));
220 i=0;
221 j=0;
222 while (strv[i] && strlen(strv[i]) > 0 && i < 9) {
223 int id = search_get_tag_id(strv[i]);
224 if (id == -1) {
225 if (table[j]) {
226 char *tmp = arg[j];
227 arg[j] = g_strdup_printf("%s %s", arg[j], strv[i]);
228 g_free(tmp);
229 } else {
230 screen_status_printf(_("Bad search tag %s"), strv[i]);
231 }
232 i++;
233 } else if (strv[i+1] == NULL || strlen(strv[i+1]) == 0) {
234 screen_status_printf(_("No argument for search tag %s"), strv[i]);
235 i++;
236 // j--;
237 //table[j] = -1;
238 } else {
239 table[j] = id;
240 arg[j] = locale_to_utf8(strv[i+1]); // FREE ME
241 j++;
242 table[j] = -1;
243 arg[j] = NULL;
244 i = i + 2;
245 advanced_search_mode = TRUE;
246 }
247 }
249 g_strfreev(strv);
252 if (advanced_search_mode && j > 0) {
253 int iter;
254 struct mpd_entity *entity;
256 /*-----------------------------------------------------------------------
257 * NOTE (again): This code exists to test a new search ui,
258 * Its ugly and MUST be redesigned before the next release!
259 * + the code below should live in mpdclient.c
260 *-----------------------------------------------------------------------
261 */
262 /** stupid - but this is just a test...... (fulhack) */
263 mpd_search_db_songs(c->connection, false);
265 for(iter = 0; iter < 10 && arg[iter] != NULL; iter++) {
266 if (table[iter] == SEARCH_URI)
267 mpd_search_add_uri_constraint(c->connection,
268 MPD_OPERATOR_DEFAULT,
269 arg[iter]);
270 else
271 mpd_search_add_tag_constraint(c->connection,
272 MPD_OPERATOR_DEFAULT,
273 table[iter], arg[iter]);
274 }
276 mpd_search_commit(c->connection);
278 fl = filelist_new();
280 while ((entity = mpd_recv_entity(c->connection)) != NULL)
281 filelist_append(fl, entity);
283 if (!mpd_response_finish(c->connection)) {
284 filelist_free(fl);
285 fl = NULL;
287 mpdclient_handle_error(c);
288 }
289 }
291 i=0;
292 while( arg[i] )
293 g_free(arg[i++]);
295 return fl;
296 }
298 static void
299 search_new(struct mpdclient *c)
300 {
301 if (c->connection == NULL)
302 return;
304 search_clear(true);
306 g_free(pattern);
307 pattern = screen_readln(_("Search"),
308 NULL,
309 &search_history,
310 NULL);
312 if (pattern == NULL) {
313 list_window_reset(browser.lw);
314 return;
315 }
317 if (browser.filelist != NULL) {
318 filelist_free(browser.filelist);
319 browser.filelist = NULL;
320 }
322 if (mpd_connection_cmp_server_version(c->connection, 0, 12, 0) >= 0)
323 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 };