Code

cf919218aca4e1b811e506cef958d5b9736ca0ad
[ncmpc.git] / src / screen_search.c
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_browser.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 GList *search_history = NULL;
109 static gchar *pattern = NULL;
110 static gboolean advanced_search_mode = FALSE;
112 static struct screen_browser browser;
115 /* search info */
116 static const char *
117 lw_search_help_callback(unsigned idx, mpd_unused int *highlight,
118                         mpd_unused void *data)
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;
143 /* the playlist have been updated -> fix highlights */
144 static void
145 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
147         browser_playlist_changed(&browser, c, event, data);
150 /* sanity check search mode value */
151 static void
152 search_check_mode(void)
154         int max = 0;
156         while (mode[max].label != NULL)
157                 max++;
158         if (options.search_mode < 0)
159                 options.search_mode = 0;
160         else if (options.search_mode >= max)
161                 options.search_mode = max-1;
164 static void
165 search_clear(mpd_unused screen_t *screen, mpdclient_t *c,
166              gboolean clear_pattern)
168         if (browser.filelist) {
169                 mpdclient_remove_playlist_callback(c, playlist_changed_callback);
170                 filelist_free(browser.filelist);
171                 browser.filelist = NULL;
172         }
173         if (clear_pattern && pattern) {
174                 g_free(pattern);
175                 pattern = NULL;
176         }
179 #ifdef FUTURE
180 static mpdclient_filelist_t *
181 filelist_search(mpdclient_t *c, mpd_unused int exact_match, int table,
182                 gchar *local_pattern)
184         mpdclient_filelist_t *list, *list2;
186         if (table == SEARCH_ARTIST_TITLE) {
187                 list = mpdclient_filelist_search(c, FALSE, MPD_TABLE_ARTIST,
188                                                  local_pattern);
189                 list2 = mpdclient_filelist_search(c, FALSE, MPD_TABLE_TITLE,
190                                                   local_pattern);
192                 filelist_move(list, list2);
193                 filelist_sort(list, compare_filelistentry_format);
194                 list->updated = TRUE;
195         } else
196                 list = mpdclient_filelist_search(c, FALSE, table, local_pattern);
198         return list;
201 /*-----------------------------------------------------------------------
202  * NOTE: This code exists to test a new search ui,
203  *       Its ugly and MUST be redesigned before the next release!
204  *-----------------------------------------------------------------------
205  */
206 static mpdclient_filelist_t *
207 search_advanced_query(char *query, mpdclient_t *c)
209         int i,j;
210         char **strv;
211         int table[10];
212         char *arg[10];
213         mpdclient_filelist_t *fl = NULL;
215         advanced_search_mode = FALSE;
216         if( g_strrstr(query, ":") == NULL )
217                 return NULL;
219         strv = g_strsplit_set(query, ": ", 0);
221         i=0;
222         while (strv[i]) {
223                 D("strv[%d] = \"%s\"\n", i, strv[i]);
224                 i++;
225         }
227         memset(table, 0, 10*sizeof(int));
228         memset(arg, 0, 10*sizeof(char *));
230         i=0;
231         j=0;
232         while (strv[i] && strlen(strv[i]) > 0 && i < 9) {
233                 int id = search_get_tag_id(strv[i]);
234                 if (id == -1) {
235                         if (table[j]) {
236                                 char *tmp = arg[j];
237                                 arg[j] = g_strdup_printf("%s %s", arg[j], strv[i]);
238                                 g_free(tmp);
239                         } else {
240                                 D("Bad search tag %s\n", strv[i]);
241                                 screen_status_printf(_("Bad search tag %s"), strv[i]);
242                         }
243                         i++;
244                 } else if (strv[i+1] == NULL || strlen(strv[i+1]) == 0) {
245                         D("No argument for search tag %s\n", strv[i]);
246                         screen_status_printf(_("No argument for search tag %s"), strv[i]);
247                         i++;
248                         //        j--;
249                         //table[j] = -1;
250                 } else {
251                         table[j] = id;
252                         arg[j] = locale_to_utf8(strv[i+1]); // FREE ME
253                         j++;
254                         table[j] = -1;
255                         arg[j] = NULL;
256                         i = i + 2;
257                         advanced_search_mode = TRUE;
258                 }
259         }
261         g_strfreev(strv);
264         if (advanced_search_mode && j > 0) {
265                 int iter;
266                 mpd_InfoEntity *entity;
268                 /*-----------------------------------------------------------------------
269                  * NOTE (again): This code exists to test a new search ui,
270                  *               Its ugly and MUST be redesigned before the next release!
271                  *             + the code below should live in mpdclient.c
272                  *-----------------------------------------------------------------------
273                  */
274                 /** stupid - but this is just a test...... (fulhack)  */
275                 mpd_startSearch(c->connection, FALSE);
277                 for(iter = 0; iter < 10; iter++) {
278                         mpd_addConstraintSearch(c->connection, table[iter], arg[iter]);
279                 }
281                 mpd_commitSearch(c->connection);
283                 fl = g_malloc0(sizeof(mpdclient_filelist_t));
285                 while ((entity=mpd_getNextInfoEntity(c->connection)))
286                         filelist_append(fl, entity);
288                 if (mpdclient_finish_command(c) && fl)
289                         filelist_free(fl);
291                 fl->updated = TRUE;
292         }
294         i=0;
295         while( arg[i] )
296                 g_free(arg[i++]);
298         return fl;
300 #else
301 #define search_advanced_query(pattern,c) (NULL)
302 #endif
304 static void
305 search_new(screen_t *screen, mpdclient_t *c)
307         search_clear(screen, c, TRUE);
309         pattern = screen_readln(screen->status_window.w,
310                                 _("Search: "),
311                                 NULL,
312                                 &search_history,
313                                 NULL);
315         if (pattern && strcmp(pattern,"") == 0) {
316                 g_free(pattern);
317                 pattern=NULL;
318         }
320         if (pattern == NULL) {
321                 list_window_reset(browser.lw);
322                 return;
323         }
325         if (!MPD_VERSION_LT(c, 0, 12, 0))
326                 browser.filelist = search_advanced_query(pattern, c);
328         if (!advanced_search_mode && browser.filelist == NULL)
329                 browser.filelist = filelist_search(c, FALSE,
330                                                   mode[options.search_mode].table,
331                                                   pattern);
333         sync_highlights(c, browser.filelist);
334         mpdclient_install_playlist_callback(c, playlist_changed_callback);
335         list_window_check_selected(browser.lw, filelist_length(browser.filelist));
340 static void
341 init(WINDOW *w, int cols, int rows)
343         browser.lw = list_window_init(w, cols, rows);
346 static void
347 quit(void)
349         if (search_history)
350                 string_list_free(search_history);
351         if (browser.filelist)
352                 filelist_free(browser.filelist);
353         list_window_free(browser.lw);
355         if (pattern) {
356                 g_free(pattern);
357                 pattern = NULL;
358         }
361 static void
362 open(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
364         //  if( pattern==NULL )
365         //    search_new(screen, c);
366         // else
367         screen_status_printf(_("Press %s for a new search"),
368                              get_key_names(CMD_SCREEN_SEARCH,0));
369         search_check_mode();
372 static void
373 resize(int cols, int rows)
375         browser.lw->cols = cols;
376         browser.lw->rows = rows;
379 static void
380 paint(mpd_unused screen_t *screen, mpdclient_t *c)
382         browser.lw->clear = 1;
384         if (browser.filelist) {
385                 browser.lw->flags = 0;
386                 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
387                 browser.filelist->updated = FALSE;
388         } else {
389                 browser.lw->flags = LW_HIDE_CURSOR;
390                 list_window_paint(browser.lw, lw_search_help_callback, NULL);
391                 if( !MPD_VERSION_LT(c, 0, 12, 0) )
392                         g_strdup_printf("Advanced search disabled (MPD version < 0.12.0");
393                 //      wmove(lw->w, 0, 0);
394                 //wclrtobot(lw->w);
395         }
397         wnoutrefresh(browser.lw->w);
400 static void
401 update(screen_t *screen, mpdclient_t *c)
403         if (browser.filelist == NULL || browser.filelist->updated) {
404                 paint(screen, c);
405                 return;
406         }
408         list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
409         wnoutrefresh(browser.lw->w);
412 static const char *
413 get_title(char *str, size_t size)
415         if (advanced_search_mode && pattern)
416                 g_snprintf(str, size, _("Search: %s"), pattern);
417         else if (pattern)
418                 g_snprintf(str, size,
419                            _("Search: Results for %s [%s]"),
420                            pattern,
421                            _(mode[options.search_mode].label));
422         else
423                 g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
424                            get_key_names(CMD_SCREEN_SEARCH,0),
425                            _(mode[options.search_mode].label));
427         return str;
430 static int
431 search_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
433         switch (cmd) {
434         case CMD_PLAY:
435                 browser_handle_enter(&browser, c);
436                 return 1;
438         case CMD_SELECT:
439                 if (browser_handle_select(&browser, c) == 0) {
440                         /* continue and select next item... */
441                         cmd = CMD_LIST_NEXT;
442                 }
443                 /* call list_window_cmd to go to the next item */
444                 return list_window_cmd(browser.lw, filelist_length(browser.filelist), cmd);
446         case CMD_SELECT_ALL:
447                 browser_handle_select_all(&browser, c);
448                 paint (screen, c);
449                 return 0;
451         case CMD_SEARCH_MODE:
452                 options.search_mode++;
453                 if (mode[options.search_mode].label == NULL)
454                         options.search_mode = 0;
455                 screen_status_printf(_("Search mode: %s"),
456                                      _(mode[options.search_mode].label));
457                 /* continue and update... */
458         case CMD_SCREEN_UPDATE:
459                 if (pattern) {
460                         search_clear(screen, c, FALSE);
461                         browser.filelist = filelist_search(c,
462                                                           FALSE,
463                                                           mode[options.search_mode].table,
464                                                           pattern);
465                         sync_highlights(c, browser.filelist);
466                 }
467                 return 1;
469         case CMD_SCREEN_SEARCH:
470                 search_new(screen, c);
471                 return 1;
473         case CMD_CLEAR:
474                 search_clear(screen, c, TRUE);
475                 list_window_reset(browser.lw);
476                 return 1;
478         case CMD_LIST_FIND:
479         case CMD_LIST_RFIND:
480         case CMD_LIST_FIND_NEXT:
481         case CMD_LIST_RFIND_NEXT:
482                 if (browser.filelist)
483                         return screen_find(screen,
484                                            browser.lw, filelist_length(browser.filelist),
485                                            cmd, browser_lw_callback,
486                                            browser.filelist);
487                 else
488                         return 1;
490         case CMD_MOUSE_EVENT:
491                 return browser_handle_mouse_event(&browser, c);
493         default:
494                 if (browser.filelist)
495                         return list_window_cmd(browser.lw,
496                                                filelist_length(browser.filelist), cmd);
497         }
499         return 0;
502 const struct screen_functions screen_search = {
503         .init = init,
504         .exit = quit,
505         .open = open,
506         .resize = resize,
507         .paint = paint,
508         .update = update,
509         .cmd = search_cmd,
510         .get_title = get_title,
511 };
513 #endif /* ENABLE_SEARCH_SCREEN */