d29c7bf9594045288602093e1a697601722802d2
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 "config.h"
21 #ifndef DISABLE_SEARCH_SCREEN
22 #include "ncmpc.h"
23 #include "options.h"
24 #include "support.h"
25 #include "mpdclient.h"
26 #include "strfsong.h"
27 #include "command.h"
28 #include "screen.h"
29 #include "utils.h"
30 #include "screen_utils.h"
31 #include "screen_browser.h"
32 #include "gcc.h"
34 #include <ctype.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <glib.h>
38 #include <ncurses.h>
40 /* new search stuff with qball's libmpdclient */
41 #define FUTURE
43 #ifdef FUTURE
45 extern gint mpdclient_finish_command(mpdclient_t *c);
47 typedef struct {
48 int id;
49 const char *name;
50 const char *localname;
51 } search_tag_t;
53 static search_tag_t search_tag[] = {
54 { MPD_TAG_ITEM_ARTIST, "artist", N_("artist") },
55 { MPD_TAG_ITEM_ALBUM, "album", N_("album") },
56 { MPD_TAG_ITEM_TITLE, "title", N_("title") },
57 { MPD_TAG_ITEM_TRACK, "track", N_("track") },
58 { MPD_TAG_ITEM_NAME, "name", N_("name") },
59 { MPD_TAG_ITEM_GENRE, "genre", N_("genre") },
60 { MPD_TAG_ITEM_DATE, "date", N_("date") },
61 { MPD_TAG_ITEM_COMPOSER, "composer", N_("composer") },
62 { MPD_TAG_ITEM_PERFORMER, "performer", N_("performer") },
63 { MPD_TAG_ITEM_COMMENT, "comment", N_("comment") },
64 { MPD_TAG_ITEM_FILENAME, "filename", N_("file") },
65 { -1, NULL, NULL }
66 };
68 static int
69 search_get_tag_id(char *name)
70 {
71 int i = 0;
73 while (search_tag[i].name) {
74 if (strcasecmp(search_tag[i].name, name) == 0 ||
75 strcasecmp(search_tag[i].localname, name) == 0)
76 return search_tag[i].id;
77 i++;
78 }
80 return -1;
81 }
83 #endif
85 #define SEARCH_TITLE 0
86 #define SEARCH_ARTIST 1
87 #define SEARCH_ALBUM 2
88 #define SEARCH_FILE 3
90 #define SEARCH_ARTIST_TITLE 999
92 typedef struct {
93 int table;
94 const char *label;
95 } search_type_t;
97 static search_type_t mode[] = {
98 { MPD_TABLE_TITLE, N_("Title") },
99 { MPD_TABLE_ARTIST, N_("Artist") },
100 { MPD_TABLE_ALBUM, N_("Album") },
101 { MPD_TABLE_FILENAME, N_("Filename") },
102 { SEARCH_ARTIST_TITLE, N_("Artist + Title") },
103 { 0, NULL }
104 };
106 static GList *search_history = NULL;
107 static gchar *pattern = NULL;
108 static gboolean advanced_search_mode = FALSE;
110 static struct screen_browser browser;
113 /* search info */
114 static const char *
115 lw_search_help_callback(unsigned idx, mpd_unused int *highlight,
116 mpd_unused void *data)
117 {
118 unsigned text_rows;
119 static const char *text[] = {
120 "Quick - just enter a string and ncmpc will search according",
121 " to the current search mode (displayed above).",
122 "",
123 "Advanced - <tag>:<search term> [<tag>:<search term>...]",
124 " Example: artist:radiohead album:pablo honey",
125 "",
126 " avalible tags: artist, album, title, track,",
127 " name, genre, date composer, performer, comment, file",
128 "",
129 NULL
130 };
132 text_rows=0;
133 while (text[text_rows])
134 text_rows++;
136 if (idx < text_rows)
137 return text[idx];
138 return NULL;
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 }
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(mpd_unused screen_t *screen, 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 #ifdef FUTURE
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 list->updated = TRUE;
199 } else {
200 list = mpdclient_filelist_search(c, FALSE, table, local_pattern);
201 if (list == NULL)
202 list = filelist_new(NULL);
203 }
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 D("strv[%d] = \"%s\"\n", i, strv[i]);
231 i++;
232 }
234 memset(table, 0, 10*sizeof(int));
235 memset(arg, 0, 10*sizeof(char *));
237 i=0;
238 j=0;
239 while (strv[i] && strlen(strv[i]) > 0 && i < 9) {
240 int id = search_get_tag_id(strv[i]);
241 if (id == -1) {
242 if (table[j]) {
243 char *tmp = arg[j];
244 arg[j] = g_strdup_printf("%s %s", arg[j], strv[i]);
245 g_free(tmp);
246 } else {
247 D("Bad search tag %s\n", strv[i]);
248 screen_status_printf(_("Bad search tag %s"), strv[i]);
249 }
250 i++;
251 } else if (strv[i+1] == NULL || strlen(strv[i+1]) == 0) {
252 D("No argument for search tag %s\n", strv[i]);
253 screen_status_printf(_("No argument for search tag %s"), strv[i]);
254 i++;
255 // j--;
256 //table[j] = -1;
257 } else {
258 table[j] = id;
259 arg[j] = locale_to_utf8(strv[i+1]); // FREE ME
260 j++;
261 table[j] = -1;
262 arg[j] = NULL;
263 i = i + 2;
264 advanced_search_mode = TRUE;
265 }
266 }
268 g_strfreev(strv);
271 if (advanced_search_mode && j > 0) {
272 int iter;
273 mpd_InfoEntity *entity;
275 /*-----------------------------------------------------------------------
276 * NOTE (again): This code exists to test a new search ui,
277 * Its ugly and MUST be redesigned before the next release!
278 * + the code below should live in mpdclient.c
279 *-----------------------------------------------------------------------
280 */
281 /** stupid - but this is just a test...... (fulhack) */
282 mpd_startSearch(c->connection, FALSE);
284 for(iter = 0; iter < 10; iter++) {
285 mpd_addConstraintSearch(c->connection, table[iter], arg[iter]);
286 }
288 mpd_commitSearch(c->connection);
290 fl = g_malloc0(sizeof(mpdclient_filelist_t));
292 while ((entity=mpd_getNextInfoEntity(c->connection)))
293 filelist_append(fl, entity);
295 if (mpdclient_finish_command(c) && fl)
296 filelist_free(fl);
298 fl->updated = TRUE;
299 }
301 i=0;
302 while( arg[i] )
303 g_free(arg[i++]);
305 return fl;
306 }
307 #else
308 #define search_advanced_query(pattern,c) (NULL)
309 #endif
311 static void
312 search_new(screen_t *screen, mpdclient_t *c)
313 {
314 search_clear(screen, c, TRUE);
316 pattern = screen_readln(screen->status_window.w,
317 _("Search: "),
318 NULL,
319 &search_history,
320 NULL);
322 if (pattern && strcmp(pattern,"") == 0) {
323 g_free(pattern);
324 pattern=NULL;
325 }
327 if (pattern == NULL) {
328 list_window_reset(browser.lw);
329 return;
330 }
332 if (!MPD_VERSION_LT(c, 0, 12, 0))
333 browser.filelist = search_advanced_query(pattern, c);
335 if (!advanced_search_mode && browser.filelist == NULL)
336 browser.filelist = filelist_search(c, FALSE,
337 mode[options.search_mode].table,
338 pattern);
340 if (browser.filelist == NULL)
341 browser.filelist = filelist_new(NULL);
343 sync_highlights(c, browser.filelist);
344 mpdclient_install_playlist_callback(c, playlist_changed_callback);
345 list_window_check_selected(browser.lw, filelist_length(browser.filelist));
346 }
350 static void
351 init(WINDOW *w, int cols, int rows)
352 {
353 browser.lw = list_window_init(w, cols, rows);
354 }
356 static void
357 quit(void)
358 {
359 if (search_history)
360 string_list_free(search_history);
361 if (browser.filelist)
362 filelist_free(browser.filelist);
363 list_window_free(browser.lw);
365 if (pattern) {
366 g_free(pattern);
367 pattern = NULL;
368 }
369 }
371 static void
372 open(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
373 {
374 // if( pattern==NULL )
375 // search_new(screen, c);
376 // else
377 screen_status_printf(_("Press %s for a new search"),
378 get_key_names(CMD_SCREEN_SEARCH,0));
379 search_check_mode();
380 }
382 static void
383 resize(int cols, int rows)
384 {
385 browser.lw->cols = cols;
386 browser.lw->rows = rows;
387 }
389 static void
390 paint(mpdclient_t *c)
391 {
392 if (browser.filelist) {
393 browser.lw->flags = 0;
394 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
395 browser.filelist->updated = FALSE;
396 } else {
397 browser.lw->flags = LW_HIDE_CURSOR;
398 list_window_paint(browser.lw, lw_search_help_callback, NULL);
399 if( !MPD_VERSION_LT(c, 0, 12, 0) )
400 g_strdup_printf("Advanced search disabled (MPD version < 0.12.0");
401 // wmove(lw->w, 0, 0);
402 //wclrtobot(lw->w);
403 }
404 }
406 static void
407 update(mpd_unused screen_t *screen, mpdclient_t *c)
408 {
409 if (browser.filelist == NULL || browser.filelist->updated) {
410 paint(c);
411 return;
412 }
414 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
415 }
417 static const char *
418 get_title(char *str, size_t size)
419 {
420 if (advanced_search_mode && pattern)
421 g_snprintf(str, size, _("Search: %s"), pattern);
422 else if (pattern)
423 g_snprintf(str, size,
424 _("Search: Results for %s [%s]"),
425 pattern,
426 _(mode[options.search_mode].label));
427 else
428 g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
429 get_key_names(CMD_SCREEN_SEARCH,0),
430 _(mode[options.search_mode].label));
432 return str;
433 }
435 static int
436 search_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
437 {
438 switch (cmd) {
439 case CMD_PLAY:
440 browser_handle_enter(&browser, c);
441 return 1;
443 case CMD_SELECT:
444 if (browser_handle_select(&browser, c) == 0) {
445 /* continue and select next item... */
446 cmd = CMD_LIST_NEXT;
447 }
448 /* call list_window_cmd to go to the next item */
449 return list_window_cmd(browser.lw, filelist_length(browser.filelist), cmd);
451 case CMD_SELECT_ALL:
452 browser_handle_select_all(&browser, c);
453 paint(c);
454 return 0;
456 case CMD_SEARCH_MODE:
457 options.search_mode++;
458 if (mode[options.search_mode].label == NULL)
459 options.search_mode = 0;
460 screen_status_printf(_("Search mode: %s"),
461 _(mode[options.search_mode].label));
462 /* continue and update... */
463 case CMD_SCREEN_UPDATE:
464 if (pattern) {
465 search_clear(screen, c, FALSE);
466 browser.filelist = filelist_search(c,
467 FALSE,
468 mode[options.search_mode].table,
469 pattern);
470 sync_highlights(c, browser.filelist);
471 }
472 return 1;
474 case CMD_SCREEN_SEARCH:
475 search_new(screen, c);
476 return 1;
478 case CMD_CLEAR:
479 search_clear(screen, c, TRUE);
480 list_window_reset(browser.lw);
481 return 1;
483 case CMD_LIST_FIND:
484 case CMD_LIST_RFIND:
485 case CMD_LIST_FIND_NEXT:
486 case CMD_LIST_RFIND_NEXT:
487 if (browser.filelist)
488 return screen_find(screen,
489 browser.lw, filelist_length(browser.filelist),
490 cmd, browser_lw_callback,
491 browser.filelist);
492 else
493 return 1;
495 case CMD_MOUSE_EVENT:
496 return browser_handle_mouse_event(&browser, c);
498 default:
499 if (browser.filelist)
500 return list_window_cmd(browser.lw,
501 filelist_length(browser.filelist), cmd);
502 }
504 return 0;
505 }
507 const struct screen_functions screen_search = {
508 .init = init,
509 .exit = quit,
510 .open = open,
511 .resize = resize,
512 .paint = paint,
513 .update = update,
514 .cmd = search_cmd,
515 .get_title = get_title,
516 };
518 #endif /* ENABLE_SEARCH_SCREEN */