12bc5f92af755d69c563a0e3832eb06c2420843a
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();
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();
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();
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();
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 if (c->connection == NULL)
301 return;
303 search_clear(c, TRUE);
305 g_free(pattern);
306 pattern = screen_readln(screen.status_window.w,
307 _("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_VERSION_LT(c, 0, 12, 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 sync_highlights(c, browser.filelist);
334 mpdclient_install_playlist_callback(c, playlist_changed_callback);
335 list_window_check_selected(browser.lw, filelist_length(browser.filelist));
336 }
340 static void
341 init(WINDOW *w, int cols, int rows)
342 {
343 browser.lw = list_window_init(w, cols, rows);
344 }
346 static void
347 quit(void)
348 {
349 if (search_history)
350 string_list_free(search_history);
351 if (browser.filelist)
352 filelist_free(browser.filelist);
353 list_window_free(browser.lw);
355 if (pattern) {
356 g_free(pattern);
357 pattern = NULL;
358 }
359 }
361 static void
362 open(G_GNUC_UNUSED mpdclient_t *c)
363 {
364 // if( pattern==NULL )
365 // search_new(screen, c);
366 // else
367 screen_status_printf(_("Press %s for a new search"),
368 get_key_names(CMD_SCREEN_SEARCH,0));
369 search_check_mode();
370 }
372 static void
373 resize(int cols, int rows)
374 {
375 browser.lw->cols = cols;
376 browser.lw->rows = rows;
377 }
379 static void
380 paint(void)
381 {
382 if (browser.filelist) {
383 browser.lw->hide_cursor = false;
384 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
385 } else {
386 browser.lw->hide_cursor = true;
387 list_window_paint(browser.lw, lw_search_help_callback, NULL);
388 }
389 }
391 static const char *
392 get_title(char *str, size_t size)
393 {
394 if (advanced_search_mode && pattern)
395 g_snprintf(str, size, _("Search: %s"), pattern);
396 else if (pattern)
397 g_snprintf(str, size,
398 _("Search: Results for %s [%s]"),
399 pattern,
400 _(mode[options.search_mode].label));
401 else
402 g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
403 get_key_names(CMD_SCREEN_SEARCH,0),
404 _(mode[options.search_mode].label));
406 return str;
407 }
409 static bool
410 search_cmd(mpdclient_t *c, command_t cmd)
411 {
412 switch (cmd) {
413 case CMD_SEARCH_MODE:
414 options.search_mode++;
415 if (mode[options.search_mode].label == NULL)
416 options.search_mode = 0;
417 screen_status_printf(_("Search mode: %s"),
418 _(mode[options.search_mode].label));
419 /* continue and update... */
420 case CMD_SCREEN_UPDATE:
421 if (pattern) {
422 search_clear(c, FALSE);
423 browser.filelist = filelist_search(c,
424 FALSE,
425 mode[options.search_mode].table,
426 pattern);
427 sync_highlights(c, browser.filelist);
428 }
429 search_repaint();
430 return true;
432 case CMD_SCREEN_SEARCH:
433 search_new(c);
434 search_repaint();
435 return true;
437 case CMD_CLEAR:
438 search_clear(c, TRUE);
439 list_window_reset(browser.lw);
440 search_repaint();
441 return true;
443 default:
444 break;
445 }
447 if (browser.filelist != NULL &&
448 browser_cmd(&browser, c, cmd)) {
449 if (screen_is_visible(&screen_search))
450 search_repaint();
451 return true;
452 }
454 return false;
455 }
457 const struct screen_functions screen_search = {
458 .init = init,
459 .exit = quit,
460 .open = open,
461 .resize = resize,
462 .paint = paint,
463 .cmd = search_cmd,
464 .get_title = get_title,
465 };