7e971f50124269655fb55e4e13a191380efc1aef
1 /*
2 * $Id$
3 *
4 * (c) 2004 by Kalle Wallin <kaw@linux.se>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
21 #include "config.h"
23 #ifndef DISABLE_SEARCH_SCREEN
24 #include "ncmpc.h"
25 #include "options.h"
26 #include "support.h"
27 #include "mpdclient.h"
28 #include "strfsong.h"
29 #include "command.h"
30 #include "screen.h"
31 #include "utils.h"
32 #include "screen_utils.h"
33 #include "screen_browse.h"
34 #include "gcc.h"
36 #include <ctype.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <glib.h>
40 #include <ncurses.h>
42 /* new search stuff with qball's libmpdclient */
43 #define FUTURE
45 #ifdef FUTURE
47 extern gint mpdclient_finish_command(mpdclient_t *c);
49 typedef struct {
50 int id;
51 const char *name;
52 const char *localname;
53 } search_tag_t;
55 static search_tag_t search_tag[] = {
56 { MPD_TAG_ITEM_ARTIST, "artist", N_("artist") },
57 { MPD_TAG_ITEM_ALBUM, "album", N_("album") },
58 { MPD_TAG_ITEM_TITLE, "title", N_("title") },
59 { MPD_TAG_ITEM_TRACK, "track", N_("track") },
60 { MPD_TAG_ITEM_NAME, "name", N_("name") },
61 { MPD_TAG_ITEM_GENRE, "genre", N_("genre") },
62 { MPD_TAG_ITEM_DATE, "date", N_("date") },
63 { MPD_TAG_ITEM_COMPOSER, "composer", N_("composer") },
64 { MPD_TAG_ITEM_PERFORMER, "performer", N_("performer") },
65 { MPD_TAG_ITEM_COMMENT, "comment", N_("comment") },
66 { MPD_TAG_ITEM_FILENAME, "filename", N_("file") },
67 { -1, NULL, NULL }
68 };
70 static int
71 search_get_tag_id(char *name)
72 {
73 int i = 0;
75 while (search_tag[i].name) {
76 if (strcasecmp(search_tag[i].name, name) == 0 ||
77 strcasecmp(search_tag[i].localname, name) == 0)
78 return search_tag[i].id;
79 i++;
80 }
82 return -1;
83 }
85 #endif
87 #define SEARCH_TITLE 0
88 #define SEARCH_ARTIST 1
89 #define SEARCH_ALBUM 2
90 #define SEARCH_FILE 3
92 #define SEARCH_ARTIST_TITLE 999
94 typedef struct {
95 int table;
96 const char *label;
97 } search_type_t;
99 static search_type_t mode[] = {
100 { MPD_TABLE_TITLE, N_("Title") },
101 { MPD_TABLE_ARTIST, N_("Artist") },
102 { MPD_TABLE_ALBUM, N_("Album") },
103 { MPD_TABLE_FILENAME, N_("Filename") },
104 { SEARCH_ARTIST_TITLE, N_("Artist + Title") },
105 { 0, NULL }
106 };
108 static list_window_t *lw = NULL;
109 static mpdclient_filelist_t *filelist = NULL;
110 static GList *search_history = NULL;
111 static gchar *pattern = NULL;
112 static gboolean advanced_search_mode = FALSE;
115 /* search info */
116 static const char *
117 lw_search_help_callback(unsigned idx, mpd_unused int *highlight,
118 mpd_unused void *data)
119 {
120 unsigned text_rows;
121 static const char *text[] = {
122 "Quick - just enter a string and ncmpc will search according",
123 " to the current search mode (displayed above).",
124 "",
125 "Advanced - <tag>:<search term> [<tag>:<search term>...]",
126 " Example: artist:radiohead album:pablo honey",
127 "",
128 " avalible tags: artist, album, title, track,",
129 " name, genre, date composer, performer, comment, file",
130 "",
131 NULL
132 };
134 text_rows=0;
135 while (text[text_rows])
136 text_rows++;
138 if (idx < text_rows)
139 return text[idx];
140 return NULL;
141 }
143 /* the playlist have been updated -> fix highlights */
144 static void
145 playlist_changed_callback(mpdclient_t *c, int event, mpd_unused gpointer data)
146 {
147 if (filelist == NULL)
148 return;
149 D("screen_search.c> playlist_callback() [%d]\n", event);
150 switch(event) {
151 case PLAYLIST_EVENT_CLEAR:
152 clear_highlights(filelist);
153 break;
154 default:
155 sync_highlights(c, filelist);
156 break;
157 }
158 }
160 /* sanity check search mode value */
161 static void
162 search_check_mode(void)
163 {
164 int max = 0;
166 while (mode[max].label != NULL)
167 max++;
168 if (options.search_mode < 0)
169 options.search_mode = 0;
170 else if (options.search_mode >= max)
171 options.search_mode = max-1;
172 }
174 static void
175 search_clear(mpd_unused screen_t *screen, mpdclient_t *c,
176 gboolean clear_pattern)
177 {
178 if (filelist) {
179 mpdclient_remove_playlist_callback(c, playlist_changed_callback);
180 mpdclient_filelist_free(filelist);
181 filelist = NULL;
182 }
183 if (clear_pattern && pattern) {
184 g_free(pattern);
185 pattern = NULL;
186 }
187 }
189 #ifdef FUTURE
190 static mpdclient_filelist_t *
191 filelist_search(mpdclient_t *c, mpd_unused int exact_match, int table,
192 gchar *local_pattern)
193 {
194 mpdclient_filelist_t *list, *list2;
196 if (table == SEARCH_ARTIST_TITLE) {
197 list = mpdclient_filelist_search(c, FALSE, MPD_TABLE_ARTIST,
198 local_pattern);
199 list2 = mpdclient_filelist_search(c, FALSE, MPD_TABLE_TITLE,
200 local_pattern);
202 list->length += list2->length;
203 list->list = g_list_concat(list->list, list2->list);
204 list->list = g_list_sort(list->list, compare_filelistentry_format);
205 list->updated = TRUE;
206 } else
207 list = mpdclient_filelist_search(c, FALSE, table, local_pattern);
209 return list;
210 }
212 /*-----------------------------------------------------------------------
213 * NOTE: This code exists to test a new search ui,
214 * Its ugly and MUST be redesigned before the next release!
215 *-----------------------------------------------------------------------
216 */
217 static mpdclient_filelist_t *
218 search_advanced_query(char *query, mpdclient_t *c)
219 {
220 int i,j;
221 char **strv;
222 int table[10];
223 char *arg[10];
224 mpdclient_filelist_t *fl = NULL;
226 advanced_search_mode = FALSE;
227 if( g_strrstr(query, ":") == NULL )
228 return NULL;
230 strv = g_strsplit_set(query, ": ", 0);
232 i=0;
233 while (strv[i]) {
234 D("strv[%d] = \"%s\"\n", i, strv[i]);
235 i++;
236 }
238 memset(table, 0, 10*sizeof(int));
239 memset(arg, 0, 10*sizeof(char *));
241 i=0;
242 j=0;
243 while (strv[i] && strlen(strv[i]) > 0 && i < 9) {
244 int id = search_get_tag_id(strv[i]);
245 if (id == -1) {
246 if (table[j]) {
247 char *tmp = arg[j];
248 arg[j] = g_strdup_printf("%s %s", arg[j], strv[i]);
249 g_free(tmp);
250 } else {
251 D("Bad search tag %s\n", strv[i]);
252 screen_status_printf(_("Bad search tag %s"), strv[i]);
253 }
254 i++;
255 } else if (strv[i+1] == NULL || strlen(strv[i+1]) == 0) {
256 D("No argument for search tag %s\n", strv[i]);
257 screen_status_printf(_("No argument for search tag %s"), strv[i]);
258 i++;
259 // j--;
260 //table[j] = -1;
261 } else {
262 table[j] = id;
263 arg[j] = locale_to_utf8(strv[i+1]); // FREE ME
264 j++;
265 table[j] = -1;
266 arg[j] = NULL;
267 i = i + 2;
268 advanced_search_mode = TRUE;
269 }
270 }
272 g_strfreev(strv);
275 if (advanced_search_mode && j > 0) {
276 int iter;
277 mpd_InfoEntity *entity;
279 /*-----------------------------------------------------------------------
280 * NOTE (again): This code exists to test a new search ui,
281 * Its ugly and MUST be redesigned before the next release!
282 * + the code below should live in mpdclient.c
283 *-----------------------------------------------------------------------
284 */
285 /** stupid - but this is just a test...... (fulhack) */
286 mpd_startSearch(c->connection, FALSE);
288 for(iter = 0; iter < 10; iter++) {
289 mpd_addConstraintSearch(c->connection, table[iter], arg[iter]);
290 }
292 mpd_commitSearch(c->connection);
294 fl = g_malloc0(sizeof(mpdclient_filelist_t));
296 while ((entity=mpd_getNextInfoEntity(c->connection))) {
297 filelist_entry_t *entry = g_malloc0(sizeof(filelist_entry_t));
299 entry->entity = entity;
300 fl->list = g_list_append(fl->list, (gpointer) entry);
301 fl->length++;
302 }
304 if (mpdclient_finish_command(c) && fl)
305 mpdclient_filelist_free(fl);
307 fl->updated = TRUE;
308 }
310 i=0;
311 while( arg[i] )
312 g_free(arg[i++]);
314 return fl;
315 }
316 #else
317 #define search_advanced_query(pattern,c) (NULL)
318 #endif
320 static void
321 search_new(screen_t *screen, mpdclient_t *c)
322 {
323 search_clear(screen, c, TRUE);
325 pattern = screen_readln(screen->status_window.w,
326 _("Search: "),
327 NULL,
328 &search_history,
329 NULL);
331 if (pattern && strcmp(pattern,"") == 0) {
332 g_free(pattern);
333 pattern=NULL;
334 }
336 if (pattern == NULL) {
337 list_window_reset(lw);
338 return;
339 }
341 if (!MPD_VERSION_LT(c, 0, 12, 0))
342 filelist = search_advanced_query(pattern, c);
344 if (!advanced_search_mode && filelist == NULL)
345 filelist = filelist_search(c, FALSE,
346 mode[options.search_mode].table,
347 pattern);
349 sync_highlights(c, filelist);
350 mpdclient_install_playlist_callback(c, playlist_changed_callback);
351 list_window_check_selected(lw, filelist->length);
352 }
356 static void
357 init(WINDOW *w, int cols, int rows)
358 {
359 lw = list_window_init(w, cols, rows);
360 }
362 static void
363 quit(void)
364 {
365 if (search_history)
366 string_list_free(search_history);
367 if (filelist)
368 mpdclient_filelist_free(filelist);
369 list_window_free(lw);
371 if (pattern) {
372 g_free(pattern);
373 pattern = NULL;
374 }
375 }
377 static void
378 open(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
379 {
380 // if( pattern==NULL )
381 // search_new(screen, c);
382 // else
383 screen_status_printf(_("Press %s for a new search"),
384 get_key_names(CMD_SCREEN_SEARCH,0));
385 search_check_mode();
386 }
388 static void
389 resize(int cols, int rows)
390 {
391 lw->cols = cols;
392 lw->rows = rows;
393 }
395 static void
396 paint(mpd_unused screen_t *screen, mpdclient_t *c)
397 {
398 lw->clear = 1;
400 if (filelist) {
401 lw->flags = 0;
402 list_window_paint(lw, browse_lw_callback, (void *) filelist);
403 filelist->updated = FALSE;
404 } else {
405 lw->flags = LW_HIDE_CURSOR;
406 list_window_paint(lw, lw_search_help_callback, NULL);
407 if( !MPD_VERSION_LT(c, 0, 12, 0) )
408 g_strdup_printf("Advanced search disabled (MPD version < 0.12.0");
409 // wmove(lw->w, 0, 0);
410 //wclrtobot(lw->w);
411 }
413 wnoutrefresh(lw->w);
414 }
416 static void
417 update(screen_t *screen, mpdclient_t *c)
418 {
419 if (filelist==NULL || filelist->updated) {
420 paint(screen, c);
421 return;
422 }
424 list_window_paint(lw, browse_lw_callback, (void *) filelist);
425 wnoutrefresh(lw->w);
426 }
428 static const char *
429 get_title(char *str, size_t size)
430 {
431 if (advanced_search_mode && pattern)
432 g_snprintf(str, size, _("Search: %s"), pattern);
433 else if (pattern)
434 g_snprintf(str, size,
435 _("Search: Results for %s [%s]"),
436 pattern,
437 _(mode[options.search_mode].label));
438 else
439 g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
440 get_key_names(CMD_SCREEN_SEARCH,0),
441 _(mode[options.search_mode].label));
443 return str;
444 }
446 static int
447 search_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
448 {
449 switch (cmd) {
450 case CMD_PLAY:
451 browse_handle_enter(screen, c, lw, filelist);
452 return 1;
454 case CMD_SELECT:
455 if (browse_handle_select(screen, c, lw, filelist) == 0) {
456 /* continue and select next item... */
457 cmd = CMD_LIST_NEXT;
458 }
459 /* call list_window_cmd to go to the next item */
460 return list_window_cmd(lw, filelist->length, cmd);
462 case CMD_SELECT_ALL:
463 browse_handle_select_all (screen, c, lw, filelist);
464 paint (screen, c);
465 return 0;
467 case CMD_SEARCH_MODE:
468 options.search_mode++;
469 if (mode[options.search_mode].label == NULL)
470 options.search_mode = 0;
471 screen_status_printf(_("Search mode: %s"),
472 _(mode[options.search_mode].label));
473 /* continue and update... */
474 case CMD_SCREEN_UPDATE:
475 if (pattern) {
476 search_clear(screen, c, FALSE);
477 filelist = filelist_search(c,
478 FALSE,
479 mode[options.search_mode].table,
480 pattern);
481 sync_highlights(c, filelist);
482 }
483 return 1;
485 case CMD_SCREEN_SEARCH:
486 search_new(screen, c);
487 return 1;
489 case CMD_CLEAR:
490 search_clear(screen, c, TRUE);
491 list_window_reset(lw);
492 return 1;
494 case CMD_LIST_FIND:
495 case CMD_LIST_RFIND:
496 case CMD_LIST_FIND_NEXT:
497 case CMD_LIST_RFIND_NEXT:
498 if (filelist)
499 return screen_find(screen,
500 lw, filelist->length,
501 cmd, browse_lw_callback, (void *) filelist);
502 else
503 return 1;
505 case CMD_MOUSE_EVENT:
506 return browse_handle_mouse_event(screen,c,lw,filelist);
508 default:
509 if (filelist)
510 return list_window_cmd(lw, filelist->length, cmd);
511 }
513 return 0;
514 }
516 const struct screen_functions screen_search = {
517 .init = init,
518 .exit = quit,
519 .open = open,
520 .resize = resize,
521 .paint = paint,
522 .update = update,
523 .cmd = search_cmd,
524 .get_title = get_title,
525 };
527 #endif /* ENABLE_SEARCH_SCREEN */