1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2017 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
4 *
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.
9 *
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.
14 *
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_status.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 SEARCH_ARTIST_TITLE
40 };
42 static const struct {
43 const char *name;
44 const char *localname;
45 } search_tag[MPD_TAG_COUNT] = {
46 [MPD_TAG_ARTIST] = { "artist", N_("artist") },
47 [MPD_TAG_ALBUM] = { "album", N_("album") },
48 [MPD_TAG_TITLE] = { "title", N_("title") },
49 [MPD_TAG_TRACK] = { "track", N_("track") },
50 [MPD_TAG_NAME] = { "name", N_("name") },
51 [MPD_TAG_GENRE] = { "genre", N_("genre") },
52 [MPD_TAG_DATE] = { "date", N_("date") },
53 [MPD_TAG_COMPOSER] = { "composer", N_("composer") },
54 [MPD_TAG_PERFORMER] = { "performer", N_("performer") },
55 [MPD_TAG_COMMENT] = { "comment", N_("comment") },
56 };
58 static int
59 search_get_tag_id(const char *name)
60 {
61 if (g_ascii_strcasecmp(name, "file") == 0 ||
62 strcasecmp(name, _("file")) == 0)
63 return SEARCH_URI;
65 for (unsigned i = 0; i < MPD_TAG_COUNT; ++i)
66 if (search_tag[i].name != NULL &&
67 (strcasecmp(search_tag[i].name, name) == 0 ||
68 strcasecmp(search_tag[i].localname, name) == 0))
69 return i;
71 return -1;
72 }
74 typedef struct {
75 enum mpd_tag_type table;
76 const char *label;
77 } search_type_t;
79 static search_type_t mode[] = {
80 { MPD_TAG_TITLE, N_("Title") },
81 { MPD_TAG_ARTIST, N_("Artist") },
82 { MPD_TAG_ALBUM, N_("Album") },
83 { SEARCH_URI, N_("Filename") },
84 { SEARCH_ARTIST_TITLE, N_("Artist + Title") },
85 { 0, NULL }
86 };
88 static GList *search_history = NULL;
89 static gchar *pattern = NULL;
90 static gboolean advanced_search_mode = FALSE;
92 static struct screen_browser browser;
94 static const char *const help_text[] = {
95 "Quick - Enter a string and ncmpc will search according",
96 " to the current search mode (displayed above).",
97 "",
98 "Advanced - <tag>:<search term> [<tag>:<search term>...]",
99 " Example: artist:radiohead album:pablo honey",
100 "",
101 " Available tags: artist, album, title, track,",
102 " name, genre, date composer, performer, comment, file",
103 "",
104 };
106 /* search info */
107 static const char *
108 lw_search_help_callback(unsigned idx, gcc_unused void *data)
109 {
110 assert(idx < G_N_ELEMENTS(help_text));
112 return help_text[idx];
113 }
115 static void
116 screen_search_paint(void);
118 static void
119 search_repaint(void)
120 {
121 screen_search_paint();
122 wrefresh(browser.lw->w);
123 }
125 /* sanity check search mode value */
126 static void
127 search_check_mode(void)
128 {
129 int max = 0;
131 while (mode[max].label != NULL)
132 max++;
133 if (options.search_mode < 0)
134 options.search_mode = 0;
135 else if (options.search_mode >= max)
136 options.search_mode = max-1;
137 }
139 static void
140 search_clear(bool clear_pattern)
141 {
142 if (browser.filelist) {
143 filelist_free(browser.filelist);
144 browser.filelist = filelist_new();
145 list_window_set_length(browser.lw, 0);
146 }
147 if (clear_pattern && pattern) {
148 g_free(pattern);
149 pattern = NULL;
150 }
151 }
153 static struct filelist *
154 search_simple_query(struct mpd_connection *connection, bool exact_match,
155 int table, gchar *local_pattern)
156 {
157 struct filelist *list;
158 gchar *filter_utf8 = locale_to_utf8(local_pattern);
160 if (table == SEARCH_ARTIST_TITLE) {
161 mpd_command_list_begin(connection, false);
163 mpd_search_db_songs(connection, exact_match);
164 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
165 MPD_TAG_ARTIST, filter_utf8);
166 mpd_search_commit(connection);
168 mpd_search_db_songs(connection, exact_match);
169 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
170 MPD_TAG_TITLE, filter_utf8);
171 mpd_search_commit(connection);
173 mpd_command_list_end(connection);
175 list = filelist_new_recv(connection);
176 filelist_no_duplicates(list);
177 } else if (table == SEARCH_URI) {
178 mpd_search_db_songs(connection, exact_match);
179 mpd_search_add_uri_constraint(connection, MPD_OPERATOR_DEFAULT,
180 filter_utf8);
181 mpd_search_commit(connection);
183 list = filelist_new_recv(connection);
184 } else {
185 mpd_search_db_songs(connection, exact_match);
186 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
187 table, filter_utf8);
188 mpd_search_commit(connection);
190 list = filelist_new_recv(connection);
191 }
193 g_free(filter_utf8);
194 return list;
195 }
197 /*-----------------------------------------------------------------------
198 * NOTE: This code exists to test a new search ui,
199 * Its ugly and MUST be redesigned before the next release!
200 *-----------------------------------------------------------------------
201 */
202 static struct filelist *
203 search_advanced_query(struct mpd_connection *connection, char *query)
204 {
205 advanced_search_mode = FALSE;
206 if (strchr(query, ':') == NULL)
207 return NULL;
209 char **strv = g_strsplit_set(query, ": ", 0);
211 int table[10];
212 memset(table, 0, 10*sizeof(int));
214 char *arg[10];
215 memset(arg, 0, 10*sizeof(char *));
217 int i = 0, j = 0;
218 while (strv[i] && strlen(strv[i]) > 0 && i < 9) {
219 int id = search_get_tag_id(strv[i]);
220 if (id == -1) {
221 if (table[j]) {
222 char *tmp = arg[j];
223 arg[j] = g_strdup_printf("%s %s", arg[j], strv[i]);
224 g_free(tmp);
225 } else {
226 screen_status_printf(_("Bad search tag %s"), strv[i]);
227 }
228 i++;
229 } else if (strv[i+1] == NULL || strlen(strv[i+1]) == 0) {
230 screen_status_printf(_("No argument for search tag %s"), strv[i]);
231 i++;
232 // j--;
233 //table[j] = -1;
234 } else {
235 table[j] = id;
236 arg[j] = locale_to_utf8(strv[i+1]); // FREE ME
237 j++;
238 table[j] = -1;
239 arg[j] = NULL;
240 i = i + 2;
241 advanced_search_mode = TRUE;
242 }
243 }
245 g_strfreev(strv);
247 if (!advanced_search_mode || j == 0) {
248 for (i = 0; arg[i] != NULL; ++i)
249 g_free(arg[i]);
250 return NULL;
251 }
253 /*-----------------------------------------------------------------------
254 * NOTE (again): This code exists to test a new search ui,
255 * Its ugly and MUST be redesigned before the next release!
256 * + the code below should live in mpdclient.c
257 *-----------------------------------------------------------------------
258 */
259 /** stupid - but this is just a test...... (fulhack) */
260 mpd_search_db_songs(connection, false);
262 for (i = 0; i < 10 && arg[i] != NULL; i++) {
263 if (table[i] == SEARCH_URI)
264 mpd_search_add_uri_constraint(connection,
265 MPD_OPERATOR_DEFAULT,
266 arg[i]);
267 else
268 mpd_search_add_tag_constraint(connection,
269 MPD_OPERATOR_DEFAULT,
270 table[i], arg[i]);
271 }
273 mpd_search_commit(connection);
274 struct filelist *fl = filelist_new_recv(connection);
275 if (!mpd_response_finish(connection)) {
276 filelist_free(fl);
277 fl = NULL;
278 }
280 for (i = 0; arg[i] != NULL; ++i)
281 g_free(arg[i]);
283 return fl;
284 }
286 static struct filelist *
287 do_search(struct mpdclient *c, char *query)
288 {
289 struct mpd_connection *connection = mpdclient_get_connection(c);
290 if (connection == NULL)
291 return NULL;
293 struct filelist *fl = search_advanced_query(connection, query);
294 if (fl != NULL)
295 return fl;
297 if (mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS) {
298 mpdclient_handle_error(c);
299 return NULL;
300 }
302 fl = search_simple_query(connection, FALSE,
303 mode[options.search_mode].table,
304 query);
305 if (fl == NULL)
306 mpdclient_handle_error(c);
307 return fl;
308 }
310 static void
311 screen_search_reload(struct mpdclient *c)
312 {
313 if (pattern == NULL)
314 return;
316 if (browser.filelist != NULL) {
317 filelist_free(browser.filelist);
318 browser.filelist = NULL;
319 }
321 browser.filelist = do_search(c, pattern);
322 if (browser.filelist == NULL)
323 browser.filelist = filelist_new();
324 list_window_set_length(browser.lw, filelist_length(browser.filelist));
326 screen_browser_sync_highlights(browser.filelist, &c->playlist);
327 }
329 static void
330 search_new(struct mpdclient *c)
331 {
332 if (!mpdclient_is_connected(c))
333 return;
335 search_clear(true);
337 g_free(pattern);
338 pattern = screen_readln(_("Search"),
339 NULL,
340 &search_history,
341 NULL);
343 if (pattern == NULL) {
344 list_window_reset(browser.lw);
345 return;
346 }
348 screen_search_reload(c);
349 }
351 static void
352 screen_search_init(WINDOW *w, int cols, int rows)
353 {
354 browser.lw = list_window_init(w, cols, rows);
355 if (options.search_format != NULL) {
356 browser.song_format = options.search_format;
357 } else {
358 browser.song_format = options.list_format;
359 }
360 list_window_set_length(browser.lw, G_N_ELEMENTS(help_text));
361 }
363 static void
364 screen_search_quit(void)
365 {
366 if (search_history)
367 string_list_free(search_history);
368 if (browser.filelist)
369 filelist_free(browser.filelist);
370 list_window_free(browser.lw);
372 if (pattern) {
373 g_free(pattern);
374 pattern = NULL;
375 }
376 }
378 static void
379 screen_search_open(gcc_unused struct mpdclient *c)
380 {
381 // if( pattern==NULL )
382 // search_new(screen, c);
383 // else
384 screen_status_printf(_("Press %s for a new search"),
385 get_key_names(CMD_SCREEN_SEARCH, false));
386 search_check_mode();
387 }
389 static void
390 screen_search_resize(int cols, int rows)
391 {
392 list_window_resize(browser.lw, cols, rows);
393 }
395 static void
396 screen_search_paint(void)
397 {
398 if (browser.filelist) {
399 browser.lw->hide_cursor = false;
400 screen_browser_paint(&browser);
401 } else {
402 browser.lw->hide_cursor = true;
403 list_window_paint(browser.lw, lw_search_help_callback, NULL);
404 }
405 }
407 static const char *
408 screen_search_get_title(char *str, size_t size)
409 {
410 if (advanced_search_mode && pattern)
411 g_snprintf(str, size, _("Search: %s"), pattern);
412 else if (pattern)
413 g_snprintf(str, size,
414 _("Search: Results for %s [%s]"),
415 pattern,
416 _(mode[options.search_mode].label));
417 else
418 g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
419 get_key_names(CMD_SCREEN_SEARCH, false),
420 _(mode[options.search_mode].label));
422 return str;
423 }
425 static void
426 screen_search_update(struct mpdclient *c)
427 {
428 if (browser.filelist != NULL && c->events & MPD_IDLE_QUEUE) {
429 screen_browser_sync_highlights(browser.filelist, &c->playlist);
430 search_repaint();
431 }
432 }
434 static bool
435 screen_search_cmd(struct mpdclient *c, command_t cmd)
436 {
437 switch (cmd) {
438 case CMD_SEARCH_MODE:
439 options.search_mode++;
440 if (mode[options.search_mode].label == NULL)
441 options.search_mode = 0;
442 screen_status_printf(_("Search mode: %s"),
443 _(mode[options.search_mode].label));
444 /* fall through */
445 case CMD_SCREEN_UPDATE:
446 screen_search_reload(c);
447 search_repaint();
448 return true;
450 case CMD_SCREEN_SEARCH:
451 search_new(c);
452 search_repaint();
453 return true;
455 case CMD_CLEAR:
456 search_clear(true);
457 list_window_reset(browser.lw);
458 search_repaint();
459 return true;
461 default:
462 break;
463 }
465 if (browser.filelist != NULL &&
466 browser_cmd(&browser, c, cmd)) {
467 if (screen_is_visible(&screen_search))
468 search_repaint();
469 return true;
470 }
472 return false;
473 }
475 const struct screen_functions screen_search = {
476 .init = screen_search_init,
477 .exit = screen_search_quit,
478 .open = screen_search_open,
479 .resize = screen_search_resize,
480 .paint = screen_search_paint,
481 .update = screen_search_update,
482 .cmd = screen_search_cmd,
483 .get_title = screen_search_get_title,
484 };