b8e7f43d7008451bf7dd67f9676894aba18110bc
1 /*
2 * (c) 2004 by Kalle Wallin <kaw@linux.se>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 */
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"
29 #include "gcc.h"
31 #include <ctype.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <glib.h>
36 static const struct {
37 const char *name;
38 const char *localname;
39 } search_tag[MPD_TAG_NUM_OF_ITEM_TYPES] = {
40 [MPD_TAG_ITEM_ARTIST] = { "artist", N_("artist") },
41 [MPD_TAG_ITEM_ALBUM] = { "album", N_("album") },
42 [MPD_TAG_ITEM_TITLE] = { "title", N_("title") },
43 [MPD_TAG_ITEM_TRACK] = { "track", N_("track") },
44 [MPD_TAG_ITEM_NAME] = { "name", N_("name") },
45 [MPD_TAG_ITEM_GENRE] = { "genre", N_("genre") },
46 [MPD_TAG_ITEM_DATE] = { "date", N_("date") },
47 [MPD_TAG_ITEM_COMPOSER] = { "composer", N_("composer") },
48 [MPD_TAG_ITEM_PERFORMER] = { "performer", N_("performer") },
49 [MPD_TAG_ITEM_COMMENT] = { "comment", N_("comment") },
50 [MPD_TAG_ITEM_FILENAME] = { "filename", N_("file") },
51 };
53 static int
54 search_get_tag_id(char *name)
55 {
56 unsigned i;
58 for (i = 0; i < MPD_TAG_NUM_OF_ITEM_TYPES; ++i)
59 if (search_tag[i].name != NULL &&
60 (strcasecmp(search_tag[i].name, name) == 0 ||
61 strcasecmp(search_tag[i].localname, name) == 0))
62 return i;
64 return -1;
65 }
67 #define SEARCH_TITLE 0
68 #define SEARCH_ARTIST 1
69 #define SEARCH_ALBUM 2
70 #define SEARCH_FILE 3
72 #define SEARCH_ARTIST_TITLE 999
74 typedef struct {
75 int table;
76 const char *label;
77 } search_type_t;
79 static search_type_t mode[] = {
80 { MPD_TABLE_TITLE, N_("Title") },
81 { MPD_TABLE_ARTIST, N_("Artist") },
82 { MPD_TABLE_ALBUM, N_("Album") },
83 { MPD_TABLE_FILENAME, 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;
95 /* search info */
96 static const char *
97 lw_search_help_callback(unsigned idx, mpd_unused int *highlight,
98 mpd_unused void *data)
99 {
100 unsigned text_rows;
101 static const char *text[] = {
102 "Quick - just enter a string and ncmpc will search according",
103 " to the current search mode (displayed above).",
104 "",
105 "Advanced - <tag>:<search term> [<tag>:<search term>...]",
106 " Example: artist:radiohead album:pablo honey",
107 "",
108 " avalible tags: artist, album, title, track,",
109 " name, genre, date composer, performer, comment, file",
110 "",
111 NULL
112 };
114 text_rows=0;
115 while (text[text_rows])
116 text_rows++;
118 if (idx < text_rows)
119 return text[idx];
120 return NULL;
121 }
123 static void
124 paint(void);
126 static void
127 search_repaint(void)
128 {
129 paint();
130 wrefresh(browser.lw->w);
131 }
133 static void
134 search_repaint_if_active(void)
135 {
136 if (screen_is_visible(&screen_search))
137 search_repaint();
138 }
140 /* the playlist have been updated -> fix highlights */
141 static void
142 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
143 {
144 browser_playlist_changed(&browser, c, event, data);
145 search_repaint_if_active();
146 }
148 /* sanity check search mode value */
149 static void
150 search_check_mode(void)
151 {
152 int max = 0;
154 while (mode[max].label != NULL)
155 max++;
156 if (options.search_mode < 0)
157 options.search_mode = 0;
158 else if (options.search_mode >= max)
159 options.search_mode = max-1;
160 }
162 static void
163 search_clear(mpdclient_t *c,
164 gboolean clear_pattern)
165 {
166 if (browser.filelist) {
167 mpdclient_remove_playlist_callback(c, playlist_changed_callback);
168 filelist_free(browser.filelist);
169 browser.filelist = filelist_new(NULL);
170 }
171 if (clear_pattern && pattern) {
172 g_free(pattern);
173 pattern = NULL;
174 }
175 }
177 static mpdclient_filelist_t *
178 filelist_search(mpdclient_t *c, mpd_unused int exact_match, int table,
179 gchar *local_pattern)
180 {
181 mpdclient_filelist_t *list, *list2;
182 gchar *filter_utf8 = locale_to_utf8(local_pattern);
184 if (table == SEARCH_ARTIST_TITLE) {
185 list = mpdclient_filelist_search(c, FALSE, MPD_TABLE_ARTIST,
186 filter_utf8);
187 if (list == NULL)
188 list = filelist_new(NULL);
190 list2 = mpdclient_filelist_search(c, FALSE, MPD_TABLE_TITLE,
191 filter_utf8);
192 if (list2 != NULL) {
193 filelist_move(list, list2);
194 filelist_free(list2);
195 }
197 filelist_sort(list, compare_filelistentry_format);
198 } else {
199 list = mpdclient_filelist_search(c, FALSE, table, filter_utf8);
200 if (list == NULL)
201 list = filelist_new(NULL);
202 }
204 g_free(filter_utf8);
205 return list;
206 }
208 /*-----------------------------------------------------------------------
209 * NOTE: This code exists to test a new search ui,
210 * Its ugly and MUST be redesigned before the next release!
211 *-----------------------------------------------------------------------
212 */
213 static mpdclient_filelist_t *
214 search_advanced_query(char *query, mpdclient_t *c)
215 {
216 int i,j;
217 char **strv;
218 int table[10];
219 char *arg[10];
220 mpdclient_filelist_t *fl = NULL;
222 advanced_search_mode = FALSE;
223 if( g_strrstr(query, ":") == NULL )
224 return NULL;
226 strv = g_strsplit_set(query, ": ", 0);
228 i=0;
229 while (strv[i]) {
230 i++;
231 }
233 memset(table, 0, 10*sizeof(int));
234 memset(arg, 0, 10*sizeof(char *));
236 i=0;
237 j=0;
238 while (strv[i] && strlen(strv[i]) > 0 && i < 9) {
239 int id = search_get_tag_id(strv[i]);
240 if (id == -1) {
241 if (table[j]) {
242 char *tmp = arg[j];
243 arg[j] = g_strdup_printf("%s %s", arg[j], strv[i]);
244 g_free(tmp);
245 } else {
246 screen_status_printf(_("Bad search tag %s"), strv[i]);
247 }
248 i++;
249 } else if (strv[i+1] == NULL || strlen(strv[i+1]) == 0) {
250 screen_status_printf(_("No argument for search tag %s"), strv[i]);
251 i++;
252 // j--;
253 //table[j] = -1;
254 } else {
255 table[j] = id;
256 arg[j] = locale_to_utf8(strv[i+1]); // FREE ME
257 j++;
258 table[j] = -1;
259 arg[j] = NULL;
260 i = i + 2;
261 advanced_search_mode = TRUE;
262 }
263 }
265 g_strfreev(strv);
268 if (advanced_search_mode && j > 0) {
269 int iter;
270 mpd_InfoEntity *entity;
272 /*-----------------------------------------------------------------------
273 * NOTE (again): This code exists to test a new search ui,
274 * Its ugly and MUST be redesigned before the next release!
275 * + the code below should live in mpdclient.c
276 *-----------------------------------------------------------------------
277 */
278 /** stupid - but this is just a test...... (fulhack) */
279 mpd_startSearch(c->connection, FALSE);
281 for(iter = 0; iter < 10; iter++) {
282 mpd_addConstraintSearch(c->connection, table[iter], arg[iter]);
283 }
285 mpd_commitSearch(c->connection);
287 fl = g_malloc0(sizeof(mpdclient_filelist_t));
289 while ((entity=mpd_getNextInfoEntity(c->connection)))
290 filelist_append(fl, entity);
292 if (mpdclient_finish_command(c) && fl)
293 filelist_free(fl);
294 }
296 i=0;
297 while( arg[i] )
298 g_free(arg[i++]);
300 return fl;
301 }
303 static void
304 search_new(mpdclient_t *c)
305 {
306 search_clear(c, TRUE);
308 g_free(pattern);
309 pattern = screen_readln(screen.status_window.w,
310 _("Search: "),
311 NULL,
312 &search_history,
313 NULL);
315 if (pattern == NULL) {
316 list_window_reset(browser.lw);
317 return;
318 }
320 if (browser.filelist != NULL) {
321 filelist_free(browser.filelist);
322 browser.filelist = NULL;
323 }
325 if (!MPD_VERSION_LT(c, 0, 12, 0))
326 browser.filelist = search_advanced_query(pattern, c);
328 if (!advanced_search_mode && browser.filelist == NULL)
329 browser.filelist = filelist_search(c, FALSE,
330 mode[options.search_mode].table,
331 pattern);
333 if (browser.filelist == NULL)
334 browser.filelist = filelist_new(NULL);
336 sync_highlights(c, browser.filelist);
337 mpdclient_install_playlist_callback(c, playlist_changed_callback);
338 list_window_check_selected(browser.lw, filelist_length(browser.filelist));
339 }
343 static void
344 init(WINDOW *w, int cols, int rows)
345 {
346 browser.lw = list_window_init(w, cols, rows);
347 }
349 static void
350 quit(void)
351 {
352 if (search_history)
353 string_list_free(search_history);
354 if (browser.filelist)
355 filelist_free(browser.filelist);
356 list_window_free(browser.lw);
358 if (pattern) {
359 g_free(pattern);
360 pattern = NULL;
361 }
362 }
364 static void
365 open(mpd_unused mpdclient_t *c)
366 {
367 // if( pattern==NULL )
368 // search_new(screen, c);
369 // else
370 screen_status_printf(_("Press %s for a new search"),
371 get_key_names(CMD_SCREEN_SEARCH,0));
372 search_check_mode();
373 }
375 static void
376 resize(int cols, int rows)
377 {
378 browser.lw->cols = cols;
379 browser.lw->rows = rows;
380 }
382 static void
383 paint(void)
384 {
385 if (browser.filelist) {
386 browser.lw->flags = 0;
387 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
388 } else {
389 browser.lw->flags = LW_HIDE_CURSOR;
390 list_window_paint(browser.lw, lw_search_help_callback, NULL);
391 }
392 }
394 static const char *
395 get_title(char *str, size_t size)
396 {
397 if (advanced_search_mode && pattern)
398 g_snprintf(str, size, _("Search: %s"), pattern);
399 else if (pattern)
400 g_snprintf(str, size,
401 _("Search: Results for %s [%s]"),
402 pattern,
403 _(mode[options.search_mode].label));
404 else
405 g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
406 get_key_names(CMD_SCREEN_SEARCH,0),
407 _(mode[options.search_mode].label));
409 return str;
410 }
412 static bool
413 search_cmd(mpdclient_t *c, command_t cmd)
414 {
415 switch (cmd) {
416 case CMD_SEARCH_MODE:
417 options.search_mode++;
418 if (mode[options.search_mode].label == NULL)
419 options.search_mode = 0;
420 screen_status_printf(_("Search mode: %s"),
421 _(mode[options.search_mode].label));
422 /* continue and update... */
423 case CMD_SCREEN_UPDATE:
424 if (pattern) {
425 search_clear(c, FALSE);
426 browser.filelist = filelist_search(c,
427 FALSE,
428 mode[options.search_mode].table,
429 pattern);
430 sync_highlights(c, browser.filelist);
431 }
432 search_repaint();
433 return true;
435 case CMD_SCREEN_SEARCH:
436 search_new(c);
437 search_repaint();
438 return true;
440 case CMD_CLEAR:
441 search_clear(c, TRUE);
442 list_window_reset(browser.lw);
443 search_repaint();
444 return true;
446 default:
447 break;
448 }
450 if (browser.filelist != NULL &&
451 browser_cmd(&browser, c, cmd)) {
452 if (screen_is_visible(&screen_search))
453 search_repaint();
454 return true;
455 }
457 return false;
458 }
460 const struct screen_functions screen_search = {
461 .init = init,
462 .exit = quit,
463 .open = open,
464 .resize = resize,
465 .paint = paint,
466 .cmd = search_cmd,
467 .get_title = get_title,
468 };