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>
35 #include <ncurses.h>
37 static const struct {
38 const char *name;
39 const char *localname;
40 } search_tag[MPD_TAG_NUM_OF_ITEM_TYPES] = {
41 [MPD_TAG_ITEM_ARTIST] = { "artist", N_("artist") },
42 [MPD_TAG_ITEM_ALBUM] = { "album", N_("album") },
43 [MPD_TAG_ITEM_TITLE] = { "title", N_("title") },
44 [MPD_TAG_ITEM_TRACK] = { "track", N_("track") },
45 [MPD_TAG_ITEM_NAME] = { "name", N_("name") },
46 [MPD_TAG_ITEM_GENRE] = { "genre", N_("genre") },
47 [MPD_TAG_ITEM_DATE] = { "date", N_("date") },
48 [MPD_TAG_ITEM_COMPOSER] = { "composer", N_("composer") },
49 [MPD_TAG_ITEM_PERFORMER] = { "performer", N_("performer") },
50 [MPD_TAG_ITEM_COMMENT] = { "comment", N_("comment") },
51 [MPD_TAG_ITEM_FILENAME] = { "filename", N_("file") },
52 };
54 static int
55 search_get_tag_id(char *name)
56 {
57 unsigned i;
59 for (i = 0; i < MPD_TAG_NUM_OF_ITEM_TYPES; ++i)
60 if (search_tag[i].name != NULL &&
61 (strcasecmp(search_tag[i].name, name) == 0 ||
62 strcasecmp(search_tag[i].localname, name) == 0))
63 return i;
65 return -1;
66 }
68 #define SEARCH_TITLE 0
69 #define SEARCH_ARTIST 1
70 #define SEARCH_ALBUM 2
71 #define SEARCH_FILE 3
73 #define SEARCH_ARTIST_TITLE 999
75 typedef struct {
76 int table;
77 const char *label;
78 } search_type_t;
80 static search_type_t mode[] = {
81 { MPD_TABLE_TITLE, N_("Title") },
82 { MPD_TABLE_ARTIST, N_("Artist") },
83 { MPD_TABLE_ALBUM, N_("Album") },
84 { MPD_TABLE_FILENAME, N_("Filename") },
85 { SEARCH_ARTIST_TITLE, N_("Artist + Title") },
86 { 0, NULL }
87 };
89 static GList *search_history = NULL;
90 static gchar *pattern = NULL;
91 static gboolean advanced_search_mode = FALSE;
93 static struct screen_browser browser;
96 /* search info */
97 static const char *
98 lw_search_help_callback(unsigned idx, mpd_unused int *highlight,
99 mpd_unused void *data)
100 {
101 unsigned text_rows;
102 static const char *text[] = {
103 "Quick - just enter a string and ncmpc will search according",
104 " to the current search mode (displayed above).",
105 "",
106 "Advanced - <tag>:<search term> [<tag>:<search term>...]",
107 " Example: artist:radiohead album:pablo honey",
108 "",
109 " avalible tags: artist, album, title, track,",
110 " name, genre, date composer, performer, comment, file",
111 "",
112 NULL
113 };
115 text_rows=0;
116 while (text[text_rows])
117 text_rows++;
119 if (idx < text_rows)
120 return text[idx];
121 return NULL;
122 }
124 static void
125 paint(void);
127 static void
128 search_repaint(void)
129 {
130 paint();
131 wrefresh(browser.lw->w);
132 }
134 static void
135 search_repaint_if_active(void)
136 {
137 if (screen_is_visible(&screen_search))
138 search_repaint();
139 }
141 /* the playlist have been updated -> fix highlights */
142 static void
143 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
144 {
145 browser_playlist_changed(&browser, c, event, data);
146 search_repaint_if_active();
147 }
149 /* sanity check search mode value */
150 static void
151 search_check_mode(void)
152 {
153 int max = 0;
155 while (mode[max].label != NULL)
156 max++;
157 if (options.search_mode < 0)
158 options.search_mode = 0;
159 else if (options.search_mode >= max)
160 options.search_mode = max-1;
161 }
163 static void
164 search_clear(mpdclient_t *c,
165 gboolean clear_pattern)
166 {
167 if (browser.filelist) {
168 mpdclient_remove_playlist_callback(c, playlist_changed_callback);
169 filelist_free(browser.filelist);
170 browser.filelist = filelist_new(NULL);
171 }
172 if (clear_pattern && pattern) {
173 g_free(pattern);
174 pattern = NULL;
175 }
176 }
178 static mpdclient_filelist_t *
179 filelist_search(mpdclient_t *c, mpd_unused int exact_match, int table,
180 gchar *local_pattern)
181 {
182 mpdclient_filelist_t *list, *list2;
184 if (table == SEARCH_ARTIST_TITLE) {
185 list = mpdclient_filelist_search(c, FALSE, MPD_TABLE_ARTIST,
186 local_pattern);
187 if (list == NULL)
188 list = filelist_new(NULL);
190 list2 = mpdclient_filelist_search(c, FALSE, MPD_TABLE_TITLE,
191 local_pattern);
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, local_pattern);
200 if (list == NULL)
201 list = filelist_new(NULL);
202 }
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 = g_malloc0(sizeof(mpdclient_filelist_t));
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 pattern = screen_readln(screen.status_window.w,
308 _("Search: "),
309 NULL,
310 &search_history,
311 NULL);
313 if (pattern && strcmp(pattern,"") == 0) {
314 g_free(pattern);
315 pattern=NULL;
316 }
318 if (pattern == NULL) {
319 list_window_reset(browser.lw);
320 return;
321 }
323 if (!MPD_VERSION_LT(c, 0, 12, 0))
324 browser.filelist = search_advanced_query(pattern, c);
326 if (!advanced_search_mode && browser.filelist == NULL)
327 browser.filelist = filelist_search(c, FALSE,
328 mode[options.search_mode].table,
329 pattern);
331 if (browser.filelist == NULL)
332 browser.filelist = filelist_new(NULL);
334 sync_highlights(c, browser.filelist);
335 mpdclient_install_playlist_callback(c, playlist_changed_callback);
336 list_window_check_selected(browser.lw, filelist_length(browser.filelist));
337 }
341 static void
342 init(WINDOW *w, int cols, int rows)
343 {
344 browser.lw = list_window_init(w, cols, rows);
345 }
347 static void
348 quit(void)
349 {
350 if (search_history)
351 string_list_free(search_history);
352 if (browser.filelist)
353 filelist_free(browser.filelist);
354 list_window_free(browser.lw);
356 if (pattern) {
357 g_free(pattern);
358 pattern = NULL;
359 }
360 }
362 static void
363 open(mpd_unused mpdclient_t *c)
364 {
365 // if( pattern==NULL )
366 // search_new(screen, c);
367 // else
368 screen_status_printf(_("Press %s for a new search"),
369 get_key_names(CMD_SCREEN_SEARCH,0));
370 search_check_mode();
371 }
373 static void
374 resize(int cols, int rows)
375 {
376 browser.lw->cols = cols;
377 browser.lw->rows = rows;
378 }
380 static void
381 paint(void)
382 {
383 if (browser.filelist) {
384 browser.lw->flags = 0;
385 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
386 } else {
387 browser.lw->flags = LW_HIDE_CURSOR;
388 list_window_paint(browser.lw, lw_search_help_callback, NULL);
389 }
390 }
392 static const char *
393 get_title(char *str, size_t size)
394 {
395 if (advanced_search_mode && pattern)
396 g_snprintf(str, size, _("Search: %s"), pattern);
397 else if (pattern)
398 g_snprintf(str, size,
399 _("Search: Results for %s [%s]"),
400 pattern,
401 _(mode[options.search_mode].label));
402 else
403 g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
404 get_key_names(CMD_SCREEN_SEARCH,0),
405 _(mode[options.search_mode].label));
407 return str;
408 }
410 static int
411 search_cmd(mpdclient_t *c, command_t cmd)
412 {
413 switch (cmd) {
414 case CMD_SEARCH_MODE:
415 options.search_mode++;
416 if (mode[options.search_mode].label == NULL)
417 options.search_mode = 0;
418 screen_status_printf(_("Search mode: %s"),
419 _(mode[options.search_mode].label));
420 /* continue and update... */
421 case CMD_SCREEN_UPDATE:
422 if (pattern) {
423 search_clear(c, FALSE);
424 browser.filelist = filelist_search(c,
425 FALSE,
426 mode[options.search_mode].table,
427 pattern);
428 sync_highlights(c, browser.filelist);
429 }
430 search_repaint();
431 return 1;
433 case CMD_SCREEN_SEARCH:
434 search_new(c);
435 search_repaint();
436 return 1;
438 case CMD_CLEAR:
439 search_clear(c, TRUE);
440 list_window_reset(browser.lw);
441 search_repaint();
442 return 1;
444 default:
445 break;
446 }
448 if (browser.filelist != NULL &&
449 browser_cmd(&browser, c, cmd)) {
450 search_repaint();
451 return 1;
452 }
454 return 0;
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 };