Code

code style, indent with tabs X
[ncmpc.git] / src / screen_search.c
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)
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;
141 /* the playlist have been updated -> fix highlights */
142 static void
143 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
145         browser_playlist_changed(&browser, c, event, data);
148 /* sanity check search mode value */
149 static void
150 search_check_mode(void)
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;
162 static void
163 search_clear(mpd_unused screen_t *screen, mpdclient_t *c,
164              gboolean clear_pattern)
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         }
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)
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;
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)
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;
307 #else
308 #define search_advanced_query(pattern,c) (NULL)
309 #endif
311 static void
312 search_new(screen_t *screen, mpdclient_t *c)
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));
350 static void
351 init(WINDOW *w, int cols, int rows)
353         browser.lw = list_window_init(w, cols, rows);
356 static void
357 quit(void)
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         }
371 static void
372 open(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
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();
382 static void
383 resize(int cols, int rows)
385         browser.lw->cols = cols;
386         browser.lw->rows = rows;
389 static void
390 paint(mpd_unused screen_t *screen, mpdclient_t *c)
392         browser.lw->clear = 1;
394         if (browser.filelist) {
395                 browser.lw->flags = 0;
396                 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
397                 browser.filelist->updated = FALSE;
398         } else {
399                 browser.lw->flags = LW_HIDE_CURSOR;
400                 list_window_paint(browser.lw, lw_search_help_callback, NULL);
401                 if( !MPD_VERSION_LT(c, 0, 12, 0) )
402                         g_strdup_printf("Advanced search disabled (MPD version < 0.12.0");
403                 //      wmove(lw->w, 0, 0);
404                 //wclrtobot(lw->w);
405         }
407         wnoutrefresh(browser.lw->w);
410 static void
411 update(screen_t *screen, mpdclient_t *c)
413         if (browser.filelist == NULL || browser.filelist->updated) {
414                 paint(screen, c);
415                 return;
416         }
418         list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
419         wnoutrefresh(browser.lw->w);
422 static const char *
423 get_title(char *str, size_t size)
425         if (advanced_search_mode && pattern)
426                 g_snprintf(str, size, _("Search: %s"), pattern);
427         else if (pattern)
428                 g_snprintf(str, size,
429                            _("Search: Results for %s [%s]"),
430                            pattern,
431                            _(mode[options.search_mode].label));
432         else
433                 g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
434                            get_key_names(CMD_SCREEN_SEARCH,0),
435                            _(mode[options.search_mode].label));
437         return str;
440 static int
441 search_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
443         switch (cmd) {
444         case CMD_PLAY:
445                 browser_handle_enter(&browser, c);
446                 return 1;
448         case CMD_SELECT:
449                 if (browser_handle_select(&browser, c) == 0) {
450                         /* continue and select next item... */
451                         cmd = CMD_LIST_NEXT;
452                 }
453                 /* call list_window_cmd to go to the next item */
454                 return list_window_cmd(browser.lw, filelist_length(browser.filelist), cmd);
456         case CMD_SELECT_ALL:
457                 browser_handle_select_all(&browser, c);
458                 paint (screen, c);
459                 return 0;
461         case CMD_SEARCH_MODE:
462                 options.search_mode++;
463                 if (mode[options.search_mode].label == NULL)
464                         options.search_mode = 0;
465                 screen_status_printf(_("Search mode: %s"),
466                                      _(mode[options.search_mode].label));
467                 /* continue and update... */
468         case CMD_SCREEN_UPDATE:
469                 if (pattern) {
470                         search_clear(screen, c, FALSE);
471                         browser.filelist = filelist_search(c,
472                                                           FALSE,
473                                                           mode[options.search_mode].table,
474                                                           pattern);
475                         sync_highlights(c, browser.filelist);
476                 }
477                 return 1;
479         case CMD_SCREEN_SEARCH:
480                 search_new(screen, c);
481                 return 1;
483         case CMD_CLEAR:
484                 search_clear(screen, c, TRUE);
485                 list_window_reset(browser.lw);
486                 return 1;
488         case CMD_LIST_FIND:
489         case CMD_LIST_RFIND:
490         case CMD_LIST_FIND_NEXT:
491         case CMD_LIST_RFIND_NEXT:
492                 if (browser.filelist)
493                         return screen_find(screen,
494                                            browser.lw, filelist_length(browser.filelist),
495                                            cmd, browser_lw_callback,
496                                            browser.filelist);
497                 else
498                         return 1;
500         case CMD_MOUSE_EVENT:
501                 return browser_handle_mouse_event(&browser, c);
503         default:
504                 if (browser.filelist)
505                         return list_window_cmd(browser.lw,
506                                                filelist_length(browser.filelist), cmd);
507         }
509         return 0;
512 const struct screen_functions screen_search = {
513         .init = init,
514         .exit = quit,
515         .open = open,
516         .resize = resize,
517         .paint = paint,
518         .update = update,
519         .cmd = search_cmd,
520         .get_title = get_title,
521 };
523 #endif /* ENABLE_SEARCH_SCREEN */