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 <ctype.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <glib.h>
25 #include <ncurses.h>
27 #include "config.h"
28 #ifndef DISABLE_SEARCH_SCREEN
29 #include "ncmpc.h"
30 #include "options.h"
31 #include "support.h"
32 #include "mpdclient.h"
33 #include "strfsong.h"
34 #include "command.h"
35 #include "screen.h"
36 #include "utils.h"
37 #include "screen_utils.h"
38 #include "screen_browse.h"
40 /* new search stuff with qball's libmpdclient */
41 #define FUTURE
44 #ifdef FUTURE
46 typedef struct
47 {
48 int id;
49 char *name;
50 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;
73 i=0;
74 while( search_tag[i].name )
75 {
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 }
81 return -1;
82 }
84 #endif
87 #define SEARCH_TITLE 0
88 #define SEARCH_ARTIST 1
89 #define SEARCH_ALBUM 2
90 #define SEARCH_FILE 3
92 typedef struct {
93 int table;
94 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 { 0, NULL }
103 };
105 static list_window_t *lw = NULL;
106 static mpdclient_filelist_t *filelist = NULL;
107 static GList *search_history = NULL;
108 static gchar *pattern = NULL;
109 static gboolean advanced_search_mode = FALSE;
112 /* search info */
113 static char *
114 lw_search_help_callback(int index, int *highlight, void *data)
115 {
116 int text_rows;
117 static char *text[] = {
118 "Welcome to ncmpc's search screen - SVN version.",
119 "",
120 "Quick search - just enter a string and ncmcp will search according",
121 " to the current search mode (displayed above).",
122 "",
123 "Advanced - bla bla bla.... syntax below",
124 ""
125 " <tag>:<search term> [<tag>:<search term>...]",
126 "",
127 "Example: artist:radiohead album:pablo honey",
128 "",
129 "##### SOMEONE - Write a proper help text, please! #####",
130 "",
131 "avalible tags: artist, album, title, track, name, genre, date",
132 " composer, performer, comment, file",
133 "",
134 NULL
135 };
137 text_rows=0;
138 while( text[text_rows] )
139 text_rows++;
141 if( index < text_rows )
142 return text[index];
143 return NULL;
144 }
146 /* the playlist have been updated -> fix highlights */
147 static void
148 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
149 {
150 if( filelist==NULL )
151 return;
152 D("screen_search.c> playlist_callback() [%d]\n", event);
153 switch(event)
154 {
155 case PLAYLIST_EVENT_CLEAR:
156 clear_highlights(filelist);
157 break;
158 default:
159 sync_highlights(c, filelist);
160 break;
161 }
162 }
164 /* sanity check search mode value */
165 static void
166 search_check_mode(void)
167 {
168 int max = 0;
170 while( mode[max].label != NULL )
171 max++;
172 if( options.search_mode<0 )
173 options.search_mode = 0;
174 else if( options.search_mode>=max )
175 options.search_mode = max-1;
176 }
178 static void
179 search_clear(screen_t *screen, mpdclient_t *c, gboolean clear_pattern)
180 {
181 if( filelist )
182 {
183 mpdclient_remove_playlist_callback(c, playlist_changed_callback);
184 filelist = mpdclient_filelist_free(filelist);
185 }
186 if( clear_pattern && pattern )
187 {
188 g_free(pattern);
189 pattern = NULL;
190 }
191 }
193 #ifdef FUTURE
194 /*-----------------------------------------------------------------------
195 * NOTE: This code exists to test a new search ui,
196 * Its ugly and MUST be redesigned before the next release!
197 *-----------------------------------------------------------------------
198 */
199 static mpdclient_filelist_t *
200 search_advanced_query(char *query, mpdclient_t *c)
201 {
202 int i,j;
203 char **strv;
204 int table[10];
205 char *arg[10];
206 mpdclient_filelist_t *filelist = NULL;
208 advanced_search_mode = FALSE;
209 if( g_strrstr(query, ":") == NULL )
210 return NULL;
212 strv = g_strsplit_set(query, ": ", 0);
214 i=0;
215 while( strv[i] )
216 {
217 D("strv[%d] = \"%s\"\n", i, strv[i]);
218 i++;
219 }
221 memset(table, 0, 10*sizeof(int));
222 memset(arg, 0, 10*sizeof(char *));
224 i=0;
225 j=0;
226 while( strv[i] && i<9 )
227 {
228 D("strv[%d] = \"%s\"\n", i, strv[i]);
230 int id = search_get_tag_id(strv[i]);
231 if( id==-1 )
232 {
233 if( table[j] )
234 {
235 char *tmp = arg[j];
236 arg[j] = g_strdup_printf("%s %s", arg[j], strv[i]);
237 g_free(tmp);
238 }
239 else
240 {
241 D("Bad search tag %s\n", strv[i]);
242 screen_status_printf(_("Bad search tag %s"), strv[i]);
243 }
244 i++;
245 }
246 else if( strv[i+1] == NULL )
247 {
248 D("No argument for search tag %s\n", strv[i]);
249 screen_status_printf(_("No argument for search tag %s"), strv[i]);
250 i++;
251 }
252 else
253 {
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 )
268 {
269 /*-----------------------------------------------------------------------
270 * NOTE (again): This code exists to test a new search ui,
271 * Its ugly and MUST be redesigned before the next release!
272 * + the code below should live in mpdclient.c
273 *-----------------------------------------------------------------------
274 */
275 mpd_InfoEntity *entity;
277 /** stupid - but this is just a test...... (fulhack) */
278 mpd_sendSearchTagCommand(c->connection,
279 table[0], arg[0],
280 table[1], arg[1],
281 table[2], arg[2],
282 table[3], arg[3],
283 table[4], arg[4],
284 table[5], arg[5],
285 table[6], arg[6],
286 table[7], arg[7],
287 table[8], arg[8],
288 table[9], arg[9]);
290 filelist = g_malloc0(sizeof(mpdclient_filelist_t));
292 while( (entity=mpd_getNextInfoEntity(c->connection)) )
293 {
294 filelist_entry_t *entry = g_malloc0(sizeof(filelist_entry_t));
296 entry->entity = entity;
297 filelist->list = g_list_append(filelist->list, (gpointer) entry);
298 filelist->length++;
299 }
301 if( mpdclient_finish_command(c) )
302 filelist = mpdclient_filelist_free(filelist);
304 filelist->updated = TRUE;
305 }
307 i=0;
308 while( arg[i] )
309 g_free(arg[i++]);
311 return filelist;
312 }
313 #else
314 #define search_advanced_query(pattern,c) (NULL)
315 #endif
317 static void
318 search_new(screen_t *screen, mpdclient_t *c)
319 {
320 search_clear(screen, c, TRUE);
322 pattern = screen_readln(screen->status_window.w,
323 _("Search: "),
324 NULL,
325 &search_history,
326 NULL);
328 if( pattern && strcmp(pattern,"")==0 )
329 {
330 g_free(pattern);
331 pattern=NULL;
332 }
334 if( pattern==NULL )
335 {
336 list_window_reset(lw);
337 return;
338 }
340 if( !MPD_VERSION_LT(c, 0, 12, 0) )
341 filelist = search_advanced_query(pattern, c);
342 if( !advanced_search_mode && filelist==NULL )
343 filelist = mpdclient_filelist_search(c,
344 FALSE,
345 mode[options.search_mode].table,
346 pattern);
347 sync_highlights(c, filelist);
348 mpdclient_install_playlist_callback(c, playlist_changed_callback);
349 list_window_check_selected(lw, filelist->length);
350 }
354 static void
355 init(WINDOW *w, int cols, int rows)
356 {
357 lw = list_window_init(w, cols, rows);
358 }
360 static void
361 quit(void)
362 {
363 if( search_history )
364 string_list_free(search_history);
365 if( filelist )
366 filelist = mpdclient_filelist_free(filelist);
367 list_window_free(lw);
368 if( pattern )
369 g_free(pattern);
370 pattern = NULL;
371 }
373 static void
374 open(screen_t *screen, mpdclient_t *c)
375 {
376 // if( pattern==NULL )
377 // search_new(screen, c);
378 // else
379 screen_status_printf(_("Press %s for a new search"),
380 get_key_names(CMD_SCREEN_SEARCH,0));
381 search_check_mode();
382 }
384 static void
385 resize(int cols, int rows)
386 {
387 lw->cols = cols;
388 lw->rows = rows;
389 }
391 static void
392 close(void)
393 {
394 }
396 static void
397 paint(screen_t *screen, mpdclient_t *c)
398 {
399 lw->clear = 1;
401 if( filelist )
402 {
403 lw->flags = 0;
404 list_window_paint(lw, browse_lw_callback, (void *) filelist);
405 filelist->updated = FALSE;
406 }
407 else
408 {
409 lw->flags = LW_HIDE_CURSOR;
410 list_window_paint(lw, lw_search_help_callback, NULL);
411 if( !MPD_VERSION_LT(c, 0, 12, 0) )
412 g_strdup_printf("Advanced search disabled (MPD version < 0.12.0");
413 // wmove(lw->w, 0, 0);
414 //wclrtobot(lw->w);
415 }
416 wnoutrefresh(lw->w);
417 }
419 static void
420 update(screen_t *screen, mpdclient_t *c)
421 {
422 if( filelist==NULL || filelist->updated )
423 {
424 paint(screen, c);
425 return;
426 }
427 list_window_paint(lw, browse_lw_callback, (void *) filelist);
428 wnoutrefresh(lw->w);
429 }
431 static char *
432 get_title(char *str, size_t size)
433 {
434 if( advanced_search_mode && pattern )
435 g_snprintf(str, size, _("Search: %s"), pattern);
436 else if( pattern )
437 g_snprintf(str, size,
438 _("Search: Results for %s [%s]"),
439 pattern,
440 _(mode[options.search_mode].label));
441 else
442 g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
443 get_key_names(CMD_SCREEN_SEARCH,0),
444 _(mode[options.search_mode].label));
446 return str;
447 }
449 static list_window_t *
450 get_filelist_window()
451 {
452 return lw;
453 }
455 static int
456 search_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
457 {
458 switch(cmd)
459 {
460 case CMD_PLAY:
461 browse_handle_enter(screen, c, lw, filelist);
462 return 1;
464 case CMD_SELECT:
465 if( browse_handle_select(screen, c, lw, filelist) == 0 )
466 {
467 /* continue and select next item... */
468 cmd = CMD_LIST_NEXT;
469 }
470 return 1;
472 case CMD_SEARCH_MODE:
473 options.search_mode++;
474 if( mode[options.search_mode].label == NULL )
475 options.search_mode = 0;
476 screen_status_printf(_("Search mode: %s"),
477 _(mode[options.search_mode].label));
478 /* continue and update... */
479 case CMD_SCREEN_UPDATE:
480 if( pattern )
481 {
482 search_clear(screen, c, FALSE);
483 filelist = mpdclient_filelist_search(c,
484 FALSE,
485 mode[options.search_mode].table,
486 pattern);
487 sync_highlights(c, filelist);
488 }
489 return 1;
491 case CMD_SCREEN_SEARCH:
492 search_new(screen, c);
493 return 1;
495 case CMD_CLEAR:
496 search_clear(screen, c, TRUE);
497 list_window_reset(lw);
498 return 1;
500 case CMD_LIST_FIND:
501 case CMD_LIST_RFIND:
502 case CMD_LIST_FIND_NEXT:
503 case CMD_LIST_RFIND_NEXT:
504 if( filelist )
505 return screen_find(screen, c,
506 lw, filelist->length,
507 cmd, browse_lw_callback, (void *) filelist);
508 else
509 return 1;
511 case CMD_MOUSE_EVENT:
512 return browse_handle_mouse_event(screen,c,lw,filelist);
514 default:
515 if( filelist )
516 return list_window_cmd(lw, filelist->length, cmd);
517 }
519 return 0;
520 }
522 screen_functions_t *
523 get_screen_search(void)
524 {
525 static screen_functions_t functions;
527 memset(&functions, 0, sizeof(screen_functions_t));
528 functions.init = init;
529 functions.exit = quit;
530 functions.open = open;
531 functions.close = close;
532 functions.resize = resize;
533 functions.paint = paint;
534 functions.update = update;
535 functions.cmd = search_cmd;
536 functions.get_lw = get_filelist_window;
537 functions.get_title = get_title;
539 return &functions;
540 }
543 #endif /* ENABLE_SEARCH_SCREEN */