00d1388ceb6f425067495c1fda3d2bd9dcd0cffc
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_ARTIST_TITLE 999
68 typedef struct {
69 int table;
70 const char *label;
71 } search_type_t;
73 static search_type_t mode[] = {
74 { MPD_TABLE_TITLE, N_("Title") },
75 { MPD_TABLE_ARTIST, N_("Artist") },
76 { MPD_TABLE_ALBUM, N_("Album") },
77 { MPD_TABLE_FILENAME, N_("Filename") },
78 { SEARCH_ARTIST_TITLE, N_("Artist + Title") },
79 { 0, NULL }
80 };
82 static GList *search_history = NULL;
83 static gchar *pattern = NULL;
84 static gboolean advanced_search_mode = FALSE;
86 static struct screen_browser browser;
89 /* search info */
90 static const char *
91 lw_search_help_callback(unsigned idx, G_GNUC_UNUSED bool *highlight,
92 G_GNUC_UNUSED char** sc, G_GNUC_UNUSED void *data)
93 {
94 unsigned text_rows;
95 static const char *text[] = {
96 "Quick - Enter a string and ncmpc will search according",
97 " to the current search mode (displayed above).",
98 "",
99 "Advanced - <tag>:<search term> [<tag>:<search term>...]",
100 " Example: artist:radiohead album:pablo honey",
101 "",
102 " Available tags: artist, album, title, track,",
103 " name, genre, date composer, performer, comment, file",
104 "",
105 NULL
106 };
108 text_rows=0;
109 while (text[text_rows])
110 text_rows++;
112 if (idx < text_rows)
113 return text[idx];
114 return NULL;
115 }
117 static void
118 paint(void);
120 static void
121 search_repaint(void)
122 {
123 paint();
124 wrefresh(browser.lw->w);
125 }
127 static void
128 search_repaint_if_active(void)
129 {
130 if (screen_is_visible(&screen_search))
131 search_repaint();
132 }
134 /* the playlist has been updated -> fix highlights */
135 static void
136 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
137 {
138 browser_playlist_changed(&browser, c, event, data);
139 search_repaint_if_active();
140 }
142 /* sanity check search mode value */
143 static void
144 search_check_mode(void)
145 {
146 int max = 0;
148 while (mode[max].label != NULL)
149 max++;
150 if (options.search_mode < 0)
151 options.search_mode = 0;
152 else if (options.search_mode >= max)
153 options.search_mode = max-1;
154 }
156 static void
157 search_clear(mpdclient_t *c,
158 gboolean clear_pattern)
159 {
160 if (browser.filelist) {
161 mpdclient_remove_playlist_callback(c, playlist_changed_callback);
162 filelist_free(browser.filelist);
163 browser.filelist = filelist_new(NULL);
164 }
165 if (clear_pattern && pattern) {
166 g_free(pattern);
167 pattern = NULL;
168 }
169 }
171 static mpdclient_filelist_t *
172 filelist_search(mpdclient_t *c, G_GNUC_UNUSED int exact_match, int table,
173 gchar *local_pattern)
174 {
175 mpdclient_filelist_t *list, *list2;
176 gchar *filter_utf8 = locale_to_utf8(local_pattern);
178 if (table == SEARCH_ARTIST_TITLE) {
179 list = mpdclient_filelist_search(c, FALSE, MPD_TABLE_ARTIST,
180 filter_utf8);
181 if (list == NULL)
182 list = filelist_new(NULL);
184 list2 = mpdclient_filelist_search(c, FALSE, MPD_TABLE_TITLE,
185 filter_utf8);
186 if (list2 != NULL) {
187 filelist_move(list, list2);
188 filelist_free(list2);
189 }
191 filelist_sort_all(list, compare_filelistentry_format);
192 } else {
193 list = mpdclient_filelist_search(c, FALSE, table, filter_utf8);
194 if (list == NULL)
195 list = filelist_new(NULL);
196 }
198 g_free(filter_utf8);
199 return list;
200 }
202 /*-----------------------------------------------------------------------
203 * NOTE: This code exists to test a new search ui,
204 * Its ugly and MUST be redesigned before the next release!
205 *-----------------------------------------------------------------------
206 */
207 static mpdclient_filelist_t *
208 search_advanced_query(char *query, mpdclient_t *c)
209 {
210 int i,j;
211 char **strv;
212 int table[10];
213 char *arg[10];
214 mpdclient_filelist_t *fl = NULL;
216 advanced_search_mode = FALSE;
217 if( g_strrstr(query, ":") == NULL )
218 return NULL;
220 strv = g_strsplit_set(query, ": ", 0);
222 i=0;
223 while (strv[i]) {
224 i++;
225 }
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);
262 if (advanced_search_mode && j > 0) {
263 int iter;
264 mpd_InfoEntity *entity;
266 /*-----------------------------------------------------------------------
267 * NOTE (again): This code exists to test a new search ui,
268 * Its ugly and MUST be redesigned before the next release!
269 * + the code below should live in mpdclient.c
270 *-----------------------------------------------------------------------
271 */
272 /** stupid - but this is just a test...... (fulhack) */
273 mpd_startSearch(c->connection, FALSE);
275 for(iter = 0; iter < 10; iter++) {
276 mpd_addConstraintSearch(c->connection, table[iter], arg[iter]);
277 }
279 mpd_commitSearch(c->connection);
281 fl = filelist_new(NULL);
283 while ((entity=mpd_getNextInfoEntity(c->connection)))
284 filelist_append(fl, entity);
286 if (mpdclient_finish_command(c) && fl)
287 filelist_free(fl);
288 }
290 i=0;
291 while( arg[i] )
292 g_free(arg[i++]);
294 return fl;
295 }
297 static void
298 search_new(mpdclient_t *c)
299 {
300 search_clear(c, TRUE);
302 g_free(pattern);
303 pattern = screen_readln(screen.status_window.w,
304 _("Search"),
305 NULL,
306 &search_history,
307 NULL);
309 if (pattern == NULL) {
310 list_window_reset(browser.lw);
311 return;
312 }
314 if (browser.filelist != NULL) {
315 filelist_free(browser.filelist);
316 browser.filelist = NULL;
317 }
319 if (!MPD_VERSION_LT(c, 0, 12, 0))
320 browser.filelist = search_advanced_query(pattern, c);
322 if (!advanced_search_mode && browser.filelist == NULL)
323 browser.filelist = filelist_search(c, FALSE,
324 mode[options.search_mode].table,
325 pattern);
327 if (browser.filelist == NULL)
328 browser.filelist = filelist_new(NULL);
330 sync_highlights(c, browser.filelist);
331 mpdclient_install_playlist_callback(c, playlist_changed_callback);
332 list_window_check_selected(browser.lw, filelist_length(browser.filelist));
333 }
337 static void
338 init(WINDOW *w, int cols, int rows)
339 {
340 browser.lw = list_window_init(w, cols, rows);
341 }
343 static void
344 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 open(G_GNUC_UNUSED mpdclient_t *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 resize(int cols, int rows)
371 {
372 browser.lw->cols = cols;
373 browser.lw->rows = rows;
374 }
376 static void
377 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 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 bool
407 search_cmd(mpdclient_t *c, command_t cmd)
408 {
409 switch (cmd) {
410 case CMD_SEARCH_MODE:
411 options.search_mode++;
412 if (mode[options.search_mode].label == NULL)
413 options.search_mode = 0;
414 screen_status_printf(_("Search mode: %s"),
415 _(mode[options.search_mode].label));
416 /* continue and update... */
417 case CMD_SCREEN_UPDATE:
418 if (pattern) {
419 search_clear(c, FALSE);
420 browser.filelist = filelist_search(c,
421 FALSE,
422 mode[options.search_mode].table,
423 pattern);
424 sync_highlights(c, browser.filelist);
425 }
426 search_repaint();
427 return true;
429 case CMD_SCREEN_SEARCH:
430 search_new(c);
431 search_repaint();
432 return true;
434 case CMD_CLEAR:
435 search_clear(c, TRUE);
436 list_window_reset(browser.lw);
437 search_repaint();
438 return true;
440 default:
441 break;
442 }
444 if (browser.filelist != NULL &&
445 browser_cmd(&browser, c, cmd)) {
446 if (screen_is_visible(&screen_search))
447 search_repaint();
448 return true;
449 }
451 return false;
452 }
454 const struct screen_functions screen_search = {
455 .init = init,
456 .exit = quit,
457 .open = open,
458 .resize = resize,
459 .paint = paint,
460 .cmd = search_cmd,
461 .get_title = get_title,
462 };