af34baf7b006d2e0b5618d067c03a0687cd39bf4
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 "support.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 /* new search stuff with qball's libmpdclient */
38 #define FUTURE
40 #ifdef FUTURE
42 extern gint mpdclient_finish_command(mpdclient_t *c);
44 typedef struct {
45 int id;
46 const char *name;
47 const char *localname;
48 } search_tag_t;
50 static search_tag_t search_tag[] = {
51 { MPD_TAG_ITEM_ARTIST, "artist", N_("artist") },
52 { MPD_TAG_ITEM_ALBUM, "album", N_("album") },
53 { MPD_TAG_ITEM_TITLE, "title", N_("title") },
54 { MPD_TAG_ITEM_TRACK, "track", N_("track") },
55 { MPD_TAG_ITEM_NAME, "name", N_("name") },
56 { MPD_TAG_ITEM_GENRE, "genre", N_("genre") },
57 { MPD_TAG_ITEM_DATE, "date", N_("date") },
58 { MPD_TAG_ITEM_COMPOSER, "composer", N_("composer") },
59 { MPD_TAG_ITEM_PERFORMER, "performer", N_("performer") },
60 { MPD_TAG_ITEM_COMMENT, "comment", N_("comment") },
61 { MPD_TAG_ITEM_FILENAME, "filename", N_("file") },
62 { -1, NULL, NULL }
63 };
65 static int
66 search_get_tag_id(char *name)
67 {
68 int i = 0;
70 while (search_tag[i].name) {
71 if (strcasecmp(search_tag[i].name, name) == 0 ||
72 strcasecmp(search_tag[i].localname, name) == 0)
73 return search_tag[i].id;
74 i++;
75 }
77 return -1;
78 }
80 #endif
82 #define SEARCH_TITLE 0
83 #define SEARCH_ARTIST 1
84 #define SEARCH_ALBUM 2
85 #define SEARCH_FILE 3
87 #define SEARCH_ARTIST_TITLE 999
89 typedef struct {
90 int table;
91 const char *label;
92 } search_type_t;
94 static search_type_t mode[] = {
95 { MPD_TABLE_TITLE, N_("Title") },
96 { MPD_TABLE_ARTIST, N_("Artist") },
97 { MPD_TABLE_ALBUM, N_("Album") },
98 { MPD_TABLE_FILENAME, N_("Filename") },
99 { SEARCH_ARTIST_TITLE, N_("Artist + Title") },
100 { 0, NULL }
101 };
103 static GList *search_history = NULL;
104 static gchar *pattern = NULL;
105 static gboolean advanced_search_mode = FALSE;
107 static struct screen_browser browser;
110 /* search info */
111 static const char *
112 lw_search_help_callback(unsigned idx, mpd_unused int *highlight,
113 mpd_unused void *data)
114 {
115 unsigned text_rows;
116 static const char *text[] = {
117 "Quick - just enter a string and ncmpc will search according",
118 " to the current search mode (displayed above).",
119 "",
120 "Advanced - <tag>:<search term> [<tag>:<search term>...]",
121 " Example: artist:radiohead album:pablo honey",
122 "",
123 " avalible tags: artist, album, title, track,",
124 " name, genre, date composer, performer, comment, file",
125 "",
126 NULL
127 };
129 text_rows=0;
130 while (text[text_rows])
131 text_rows++;
133 if (idx < text_rows)
134 return text[idx];
135 return NULL;
136 }
138 static void
139 paint(mpdclient_t *c);
141 static void
142 search_repaint(void)
143 {
144 paint(NULL);
145 wrefresh(browser.lw->w);
146 }
148 static void
149 search_repaint_if_active(void)
150 {
151 if (screen_is_visible(&screen_search))
152 search_repaint();
153 }
155 /* the playlist have been updated -> fix highlights */
156 static void
157 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
158 {
159 browser_playlist_changed(&browser, c, event, data);
160 search_repaint_if_active();
161 }
163 /* sanity check search mode value */
164 static void
165 search_check_mode(void)
166 {
167 int max = 0;
169 while (mode[max].label != NULL)
170 max++;
171 if (options.search_mode < 0)
172 options.search_mode = 0;
173 else if (options.search_mode >= max)
174 options.search_mode = max-1;
175 }
177 static void
178 search_clear(mpd_unused screen_t *screen, mpdclient_t *c,
179 gboolean clear_pattern)
180 {
181 if (browser.filelist) {
182 mpdclient_remove_playlist_callback(c, playlist_changed_callback);
183 filelist_free(browser.filelist);
184 browser.filelist = filelist_new(NULL);
185 }
186 if (clear_pattern && pattern) {
187 g_free(pattern);
188 pattern = NULL;
189 }
190 }
192 #ifdef FUTURE
193 static mpdclient_filelist_t *
194 filelist_search(mpdclient_t *c, mpd_unused int exact_match, int table,
195 gchar *local_pattern)
196 {
197 mpdclient_filelist_t *list, *list2;
199 if (table == SEARCH_ARTIST_TITLE) {
200 list = mpdclient_filelist_search(c, FALSE, MPD_TABLE_ARTIST,
201 local_pattern);
202 if (list == NULL)
203 list = filelist_new(NULL);
205 list2 = mpdclient_filelist_search(c, FALSE, MPD_TABLE_TITLE,
206 local_pattern);
207 if (list2 != NULL) {
208 filelist_move(list, list2);
209 filelist_free(list2);
210 }
212 filelist_sort(list, compare_filelistentry_format);
213 } else {
214 list = mpdclient_filelist_search(c, FALSE, table, local_pattern);
215 if (list == NULL)
216 list = filelist_new(NULL);
217 }
219 return list;
220 }
222 /*-----------------------------------------------------------------------
223 * NOTE: This code exists to test a new search ui,
224 * Its ugly and MUST be redesigned before the next release!
225 *-----------------------------------------------------------------------
226 */
227 static mpdclient_filelist_t *
228 search_advanced_query(char *query, mpdclient_t *c)
229 {
230 int i,j;
231 char **strv;
232 int table[10];
233 char *arg[10];
234 mpdclient_filelist_t *fl = NULL;
236 advanced_search_mode = FALSE;
237 if( g_strrstr(query, ":") == NULL )
238 return NULL;
240 strv = g_strsplit_set(query, ": ", 0);
242 i=0;
243 while (strv[i]) {
244 i++;
245 }
247 memset(table, 0, 10*sizeof(int));
248 memset(arg, 0, 10*sizeof(char *));
250 i=0;
251 j=0;
252 while (strv[i] && strlen(strv[i]) > 0 && i < 9) {
253 int id = search_get_tag_id(strv[i]);
254 if (id == -1) {
255 if (table[j]) {
256 char *tmp = arg[j];
257 arg[j] = g_strdup_printf("%s %s", arg[j], strv[i]);
258 g_free(tmp);
259 } else {
260 screen_status_printf(_("Bad search tag %s"), strv[i]);
261 }
262 i++;
263 } else if (strv[i+1] == NULL || strlen(strv[i+1]) == 0) {
264 screen_status_printf(_("No argument for search tag %s"), strv[i]);
265 i++;
266 // j--;
267 //table[j] = -1;
268 } else {
269 table[j] = id;
270 arg[j] = locale_to_utf8(strv[i+1]); // FREE ME
271 j++;
272 table[j] = -1;
273 arg[j] = NULL;
274 i = i + 2;
275 advanced_search_mode = TRUE;
276 }
277 }
279 g_strfreev(strv);
282 if (advanced_search_mode && j > 0) {
283 int iter;
284 mpd_InfoEntity *entity;
286 /*-----------------------------------------------------------------------
287 * NOTE (again): This code exists to test a new search ui,
288 * Its ugly and MUST be redesigned before the next release!
289 * + the code below should live in mpdclient.c
290 *-----------------------------------------------------------------------
291 */
292 /** stupid - but this is just a test...... (fulhack) */
293 mpd_startSearch(c->connection, FALSE);
295 for(iter = 0; iter < 10; iter++) {
296 mpd_addConstraintSearch(c->connection, table[iter], arg[iter]);
297 }
299 mpd_commitSearch(c->connection);
301 fl = g_malloc0(sizeof(mpdclient_filelist_t));
303 while ((entity=mpd_getNextInfoEntity(c->connection)))
304 filelist_append(fl, entity);
306 if (mpdclient_finish_command(c) && fl)
307 filelist_free(fl);
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(browser.lw);
338 return;
339 }
341 if (!MPD_VERSION_LT(c, 0, 12, 0))
342 browser.filelist = search_advanced_query(pattern, c);
344 if (!advanced_search_mode && browser.filelist == NULL)
345 browser.filelist = filelist_search(c, FALSE,
346 mode[options.search_mode].table,
347 pattern);
349 if (browser.filelist == NULL)
350 browser.filelist = filelist_new(NULL);
352 sync_highlights(c, browser.filelist);
353 mpdclient_install_playlist_callback(c, playlist_changed_callback);
354 list_window_check_selected(browser.lw, filelist_length(browser.filelist));
355 }
359 static void
360 init(WINDOW *w, int cols, int rows)
361 {
362 browser.lw = list_window_init(w, cols, rows);
363 }
365 static void
366 quit(void)
367 {
368 if (search_history)
369 string_list_free(search_history);
370 if (browser.filelist)
371 filelist_free(browser.filelist);
372 list_window_free(browser.lw);
374 if (pattern) {
375 g_free(pattern);
376 pattern = NULL;
377 }
378 }
380 static void
381 open(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
382 {
383 // if( pattern==NULL )
384 // search_new(screen, c);
385 // else
386 screen_status_printf(_("Press %s for a new search"),
387 get_key_names(CMD_SCREEN_SEARCH,0));
388 search_check_mode();
389 }
391 static void
392 resize(int cols, int rows)
393 {
394 browser.lw->cols = cols;
395 browser.lw->rows = rows;
396 }
398 static void
399 paint(mpd_unused mpdclient_t *c)
400 {
401 if (browser.filelist) {
402 browser.lw->flags = 0;
403 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
404 } else {
405 browser.lw->flags = LW_HIDE_CURSOR;
406 list_window_paint(browser.lw, lw_search_help_callback, NULL);
407 }
408 }
410 static const char *
411 get_title(char *str, size_t size)
412 {
413 if (advanced_search_mode && pattern)
414 g_snprintf(str, size, _("Search: %s"), pattern);
415 else if (pattern)
416 g_snprintf(str, size,
417 _("Search: Results for %s [%s]"),
418 pattern,
419 _(mode[options.search_mode].label));
420 else
421 g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
422 get_key_names(CMD_SCREEN_SEARCH,0),
423 _(mode[options.search_mode].label));
425 return str;
426 }
428 static int
429 search_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
430 {
431 switch (cmd) {
432 case CMD_SEARCH_MODE:
433 options.search_mode++;
434 if (mode[options.search_mode].label == NULL)
435 options.search_mode = 0;
436 screen_status_printf(_("Search mode: %s"),
437 _(mode[options.search_mode].label));
438 /* continue and update... */
439 case CMD_SCREEN_UPDATE:
440 if (pattern) {
441 search_clear(screen, c, FALSE);
442 browser.filelist = filelist_search(c,
443 FALSE,
444 mode[options.search_mode].table,
445 pattern);
446 sync_highlights(c, browser.filelist);
447 }
448 search_repaint();
449 return 1;
451 case CMD_SCREEN_SEARCH:
452 search_new(screen, c);
453 search_repaint();
454 return 1;
456 case CMD_CLEAR:
457 search_clear(screen, c, TRUE);
458 list_window_reset(browser.lw);
459 search_repaint();
460 return 1;
462 default:
463 break;
464 }
466 if (browser.filelist != NULL &&
467 browser_cmd(&browser, screen, c, cmd)) {
468 search_repaint();
469 return 1;
470 }
472 return 0;
473 }
475 const struct screen_functions screen_search = {
476 .init = init,
477 .exit = quit,
478 .open = open,
479 .resize = resize,
480 .paint = paint,
481 .cmd = search_cmd,
482 .get_title = get_title,
483 };