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 */
19 #include "i18n.h"
20 #include "options.h"
21 #include "charset.h"
22 #include "mpdclient.h"
23 #include "strfsong.h"
24 #include "command.h"
25 #include "screen.h"
26 #include "utils.h"
27 #include "screen_utils.h"
28 #include "screen_browser.h"
30 #include <ctype.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <glib.h>
35 static const struct {
36 const char *name;
37 const char *localname;
38 } search_tag[MPD_TAG_NUM_OF_ITEM_TYPES] = {
39 [MPD_TAG_ITEM_ARTIST] = { "artist", N_("artist") },
40 [MPD_TAG_ITEM_ALBUM] = { "album", N_("album") },
41 [MPD_TAG_ITEM_TITLE] = { "title", N_("title") },
42 [MPD_TAG_ITEM_TRACK] = { "track", N_("track") },
43 [MPD_TAG_ITEM_NAME] = { "name", N_("name") },
44 [MPD_TAG_ITEM_GENRE] = { "genre", N_("genre") },
45 [MPD_TAG_ITEM_DATE] = { "date", N_("date") },
46 [MPD_TAG_ITEM_COMPOSER] = { "composer", N_("composer") },
47 [MPD_TAG_ITEM_PERFORMER] = { "performer", N_("performer") },
48 [MPD_TAG_ITEM_COMMENT] = { "comment", N_("comment") },
49 [MPD_TAG_ITEM_FILENAME] = { "filename", N_("file") },
50 };
52 static int
53 search_get_tag_id(char *name)
54 {
55 unsigned i;
57 for (i = 0; i < MPD_TAG_NUM_OF_ITEM_TYPES; ++i)
58 if (search_tag[i].name != NULL &&
59 (strcasecmp(search_tag[i].name, name) == 0 ||
60 strcasecmp(search_tag[i].localname, name) == 0))
61 return i;
63 return -1;
64 }
66 #define SEARCH_TITLE 0
67 #define SEARCH_ARTIST 1
68 #define SEARCH_ALBUM 2
69 #define SEARCH_FILE 3
71 #define SEARCH_ARTIST_TITLE 999
73 typedef struct {
74 int table;
75 const char *label;
76 } search_type_t;
78 static search_type_t mode[] = {
79 { MPD_TABLE_TITLE, N_("Title") },
80 { MPD_TABLE_ARTIST, N_("Artist") },
81 { MPD_TABLE_ALBUM, N_("Album") },
82 { MPD_TABLE_FILENAME, N_("Filename") },
83 { SEARCH_ARTIST_TITLE, N_("Artist + Title") },
84 { 0, NULL }
85 };
87 static GList *search_history = NULL;
88 static gchar *pattern = NULL;
89 static gboolean advanced_search_mode = FALSE;
91 static struct screen_browser browser;
94 /* search info */
95 static const char *
96 lw_search_help_callback(unsigned idx, G_GNUC_UNUSED bool *highlight,
97 G_GNUC_UNUSED void *data)
98 {
99 unsigned text_rows;
100 static const char *text[] = {
101 "Quick - Enter a string and ncmpc will search according",
102 " to the current search mode (displayed above).",
103 "",
104 "Advanced - <tag>:<search term> [<tag>:<search term>...]",
105 " Example: artist:radiohead album:pablo honey",
106 "",
107 " Available tags: artist, album, title, track,",
108 " name, genre, date composer, performer, comment, file",
109 "",
110 NULL
111 };
113 text_rows=0;
114 while (text[text_rows])
115 text_rows++;
117 if (idx < text_rows)
118 return text[idx];
119 return NULL;
120 }
122 static void
123 paint(void);
125 static void
126 search_repaint(void)
127 {
128 paint();
129 wrefresh(browser.lw->w);
130 }
132 static void
133 search_repaint_if_active(void)
134 {
135 if (screen_is_visible(&screen_search))
136 search_repaint();
137 }
139 /* the playlist has been updated -> fix highlights */
140 static void
141 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
142 {
143 browser_playlist_changed(&browser, c, event, data);
144 search_repaint_if_active();
145 }
147 /* sanity check search mode value */
148 static void
149 search_check_mode(void)
150 {
151 int max = 0;
153 while (mode[max].label != NULL)
154 max++;
155 if (options.search_mode < 0)
156 options.search_mode = 0;
157 else if (options.search_mode >= max)
158 options.search_mode = max-1;
159 }
161 static void
162 search_clear(mpdclient_t *c,
163 gboolean clear_pattern)
164 {
165 if (browser.filelist) {
166 mpdclient_remove_playlist_callback(c, playlist_changed_callback);
167 filelist_free(browser.filelist);
168 browser.filelist = filelist_new(NULL);
169 }
170 if (clear_pattern && pattern) {
171 g_free(pattern);
172 pattern = NULL;
173 }
174 }
176 static mpdclient_filelist_t *
177 filelist_search(mpdclient_t *c, G_GNUC_UNUSED int exact_match, int table,
178 gchar *local_pattern)
179 {
180 mpdclient_filelist_t *list, *list2;
181 gchar *filter_utf8 = locale_to_utf8(local_pattern);
183 if (table == SEARCH_ARTIST_TITLE) {
184 list = mpdclient_filelist_search(c, FALSE, MPD_TABLE_ARTIST,
185 filter_utf8);
186 if (list == NULL)
187 list = filelist_new(NULL);
189 list2 = mpdclient_filelist_search(c, FALSE, MPD_TABLE_TITLE,
190 filter_utf8);
191 if (list2 != NULL) {
192 filelist_move(list, list2);
193 filelist_free(list2);
194 }
196 filelist_sort_all(list, compare_filelistentry_format);
197 } else {
198 list = mpdclient_filelist_search(c, FALSE, table, filter_utf8);
199 if (list == NULL)
200 list = filelist_new(NULL);
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 mpdclient_filelist_t *
213 search_advanced_query(char *query, mpdclient_t *c)
214 {
215 int i,j;
216 char **strv;
217 int table[10];
218 char *arg[10];
219 mpdclient_filelist_t *fl = NULL;
221 advanced_search_mode = FALSE;
222 if( g_strrstr(query, ":") == NULL )
223 return NULL;
225 strv = g_strsplit_set(query, ": ", 0);
227 i=0;
228 while (strv[i]) {
229 i++;
230 }
232 memset(table, 0, 10*sizeof(int));
233 memset(arg, 0, 10*sizeof(char *));
235 i=0;
236 j=0;
237 while (strv[i] && strlen(strv[i]) > 0 && i < 9) {
238 int id = search_get_tag_id(strv[i]);
239 if (id == -1) {
240 if (table[j]) {
241 char *tmp = arg[j];
242 arg[j] = g_strdup_printf("%s %s", arg[j], strv[i]);
243 g_free(tmp);
244 } else {
245 screen_status_printf(_("Bad search tag %s"), strv[i]);
246 }
247 i++;
248 } else if (strv[i+1] == NULL || strlen(strv[i+1]) == 0) {
249 screen_status_printf(_("No argument for search tag %s"), strv[i]);
250 i++;
251 // j--;
252 //table[j] = -1;
253 } else {
254 table[j] = id;
255 arg[j] = locale_to_utf8(strv[i+1]); // FREE ME
256 j++;
257 table[j] = -1;
258 arg[j] = NULL;
259 i = i + 2;
260 advanced_search_mode = TRUE;
261 }
262 }
264 g_strfreev(strv);
267 if (advanced_search_mode && j > 0) {
268 int iter;
269 mpd_InfoEntity *entity;
271 /*-----------------------------------------------------------------------
272 * NOTE (again): This code exists to test a new search ui,
273 * Its ugly and MUST be redesigned before the next release!
274 * + the code below should live in mpdclient.c
275 *-----------------------------------------------------------------------
276 */
277 /** stupid - but this is just a test...... (fulhack) */
278 mpd_startSearch(c->connection, FALSE);
280 for(iter = 0; iter < 10; iter++) {
281 mpd_addConstraintSearch(c->connection, table[iter], arg[iter]);
282 }
284 mpd_commitSearch(c->connection);
286 fl = filelist_new(NULL);
288 while ((entity=mpd_getNextInfoEntity(c->connection)))
289 filelist_append(fl, entity);
291 if (mpdclient_finish_command(c) && fl)
292 filelist_free(fl);
293 }
295 i=0;
296 while( arg[i] )
297 g_free(arg[i++]);
299 return fl;
300 }
302 static void
303 search_new(mpdclient_t *c)
304 {
305 search_clear(c, TRUE);
307 g_free(pattern);
308 pattern = screen_readln(screen.status_window.w,
309 _("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 if (!MPD_VERSION_LT(c, 0, 12, 0))
325 browser.filelist = search_advanced_query(pattern, c);
327 if (!advanced_search_mode && browser.filelist == NULL)
328 browser.filelist = filelist_search(c, FALSE,
329 mode[options.search_mode].table,
330 pattern);
332 if (browser.filelist == NULL)
333 browser.filelist = filelist_new(NULL);
335 sync_highlights(c, browser.filelist);
336 mpdclient_install_playlist_callback(c, playlist_changed_callback);
337 list_window_check_selected(browser.lw, filelist_length(browser.filelist));
338 }
342 static void
343 init(WINDOW *w, int cols, int rows)
344 {
345 browser.lw = list_window_init(w, cols, rows);
346 }
348 static void
349 quit(void)
350 {
351 if (search_history)
352 string_list_free(search_history);
353 if (browser.filelist)
354 filelist_free(browser.filelist);
355 list_window_free(browser.lw);
357 if (pattern) {
358 g_free(pattern);
359 pattern = NULL;
360 }
361 }
363 static void
364 open(G_GNUC_UNUSED mpdclient_t *c)
365 {
366 // if( pattern==NULL )
367 // search_new(screen, c);
368 // else
369 screen_status_printf(_("Press %s for a new search"),
370 get_key_names(CMD_SCREEN_SEARCH,0));
371 search_check_mode();
372 }
374 static void
375 resize(int cols, int rows)
376 {
377 browser.lw->cols = cols;
378 browser.lw->rows = rows;
379 }
381 static void
382 paint(void)
383 {
384 if (browser.filelist) {
385 browser.lw->hide_cursor = false;
386 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
387 } else {
388 browser.lw->hide_cursor = true;
389 list_window_paint(browser.lw, lw_search_help_callback, NULL);
390 }
391 }
393 static const char *
394 get_title(char *str, size_t size)
395 {
396 if (advanced_search_mode && pattern)
397 g_snprintf(str, size, _("Search: %s"), pattern);
398 else if (pattern)
399 g_snprintf(str, size,
400 _("Search: Results for %s [%s]"),
401 pattern,
402 _(mode[options.search_mode].label));
403 else
404 g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
405 get_key_names(CMD_SCREEN_SEARCH,0),
406 _(mode[options.search_mode].label));
408 return str;
409 }
411 static bool
412 search_cmd(mpdclient_t *c, command_t cmd)
413 {
414 switch (cmd) {
415 case CMD_SEARCH_MODE:
416 options.search_mode++;
417 if (mode[options.search_mode].label == NULL)
418 options.search_mode = 0;
419 screen_status_printf(_("Search mode: %s"),
420 _(mode[options.search_mode].label));
421 /* continue and update... */
422 case CMD_SCREEN_UPDATE:
423 if (pattern) {
424 search_clear(c, FALSE);
425 browser.filelist = filelist_search(c,
426 FALSE,
427 mode[options.search_mode].table,
428 pattern);
429 sync_highlights(c, browser.filelist);
430 }
431 search_repaint();
432 return true;
434 case CMD_SCREEN_SEARCH:
435 search_new(c);
436 search_repaint();
437 return true;
439 case CMD_CLEAR:
440 search_clear(c, TRUE);
441 list_window_reset(browser.lw);
442 search_repaint();
443 return true;
445 default:
446 break;
447 }
449 if (browser.filelist != NULL &&
450 browser_cmd(&browser, c, cmd)) {
451 if (screen_is_visible(&screen_search))
452 search_repaint();
453 return true;
454 }
456 return false;
457 }
459 const struct screen_functions screen_search = {
460 .init = init,
461 .exit = quit,
462 .open = open,
463 .resize = resize,
464 .paint = paint,
465 .cmd = search_cmd,
466 .get_title = get_title,
467 };