Code

51a8a2966c064f29f873ef2d799f6d9f1ff4468f
[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 "i18n.h"
20 #include "options.h"
21 #include "charset.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 static const struct {
38         const char *name;
39         const char *localname;
40 } search_tag[MPD_TAG_NUM_OF_ITEM_TYPES] = {
41         [MPD_TAG_ITEM_ARTIST] = { "artist", N_("artist") },
42         [MPD_TAG_ITEM_ALBUM] = { "album", N_("album") },
43         [MPD_TAG_ITEM_TITLE] = { "title", N_("title") },
44         [MPD_TAG_ITEM_TRACK] = { "track", N_("track") },
45         [MPD_TAG_ITEM_NAME] = { "name", N_("name") },
46         [MPD_TAG_ITEM_GENRE] = { "genre", N_("genre") },
47         [MPD_TAG_ITEM_DATE] = { "date", N_("date") },
48         [MPD_TAG_ITEM_COMPOSER] = { "composer", N_("composer") },
49         [MPD_TAG_ITEM_PERFORMER] = { "performer", N_("performer") },
50         [MPD_TAG_ITEM_COMMENT] = { "comment", N_("comment") },
51         [MPD_TAG_ITEM_FILENAME] = { "filename", N_("file") },
52 };
54 static int
55 search_get_tag_id(char *name)
56 {
57         unsigned i;
59         for (i = 0; i < MPD_TAG_NUM_OF_ITEM_TYPES; ++i)
60                 if (search_tag[i].name != NULL &&
61                     (strcasecmp(search_tag[i].name, name) == 0 ||
62                      strcasecmp(search_tag[i].localname, name) == 0))
63                         return i;
65         return -1;
66 }
68 #define SEARCH_TITLE    0
69 #define SEARCH_ARTIST   1
70 #define SEARCH_ALBUM    2
71 #define SEARCH_FILE     3
73 #define SEARCH_ARTIST_TITLE 999
75 typedef struct {
76         int table;
77         const char *label;
78 } search_type_t;
80 static search_type_t mode[] = {
81         { MPD_TABLE_TITLE, N_("Title") },
82         { MPD_TABLE_ARTIST, N_("Artist") },
83         { MPD_TABLE_ALBUM, N_("Album") },
84         { MPD_TABLE_FILENAME, N_("Filename") },
85         { SEARCH_ARTIST_TITLE, N_("Artist + Title") },
86         { 0, NULL }
87 };
89 static GList *search_history = NULL;
90 static gchar *pattern = NULL;
91 static gboolean advanced_search_mode = FALSE;
93 static struct screen_browser browser;
96 /* search info */
97 static const char *
98 lw_search_help_callback(unsigned idx, mpd_unused int *highlight,
99                         mpd_unused void *data)
101         unsigned text_rows;
102         static const char *text[] = {
103                 "Quick  - just enter a string and ncmpc will search according",
104                 "               to the current search mode (displayed above).",
105                 "",
106                 "Advanced  -  <tag>:<search term> [<tag>:<search term>...]",
107                 "                       Example: artist:radiohead album:pablo honey",
108                 "",
109                 "                  avalible tags: artist, album, title, track,",
110                 "                  name, genre, date composer, performer, comment, file",
111                 "",
112                 NULL
113         };
115         text_rows=0;
116         while (text[text_rows])
117                 text_rows++;
119         if (idx < text_rows)
120                 return text[idx];
121         return NULL;
124 static void
125 paint(void);
127 static void
128 search_repaint(void)
130         paint();
131         wrefresh(browser.lw->w);
134 static void
135 search_repaint_if_active(void)
137         if (screen_is_visible(&screen_search))
138                 search_repaint();
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);
146         search_repaint_if_active();
149 /* sanity check search mode value */
150 static void
151 search_check_mode(void)
153         int max = 0;
155         while (mode[max].label != NULL)
156                 max++;
157         if (options.search_mode < 0)
158                 options.search_mode = 0;
159         else if (options.search_mode >= max)
160                 options.search_mode = max-1;
163 static void
164 search_clear(mpdclient_t *c,
165              gboolean clear_pattern)
167         if (browser.filelist) {
168                 mpdclient_remove_playlist_callback(c, playlist_changed_callback);
169                 filelist_free(browser.filelist);
170                 browser.filelist = filelist_new(NULL);
171         }
172         if (clear_pattern && pattern) {
173                 g_free(pattern);
174                 pattern = NULL;
175         }
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         } else {
199                 list = mpdclient_filelist_search(c, FALSE, table, local_pattern);
200                 if (list == NULL)
201                         list = filelist_new(NULL);
202         }
204         return list;
207 /*-----------------------------------------------------------------------
208  * NOTE: This code exists to test a new search ui,
209  *       Its ugly and MUST be redesigned before the next release!
210  *-----------------------------------------------------------------------
211  */
212 static mpdclient_filelist_t *
213 search_advanced_query(char *query, mpdclient_t *c)
215         int i,j;
216         char **strv;
217         int table[10];
218         char *arg[10];
219         mpdclient_filelist_t *fl = NULL;
221         advanced_search_mode = FALSE;
222         if( g_strrstr(query, ":") == NULL )
223                 return NULL;
225         strv = g_strsplit_set(query, ": ", 0);
227         i=0;
228         while (strv[i]) {
229                 i++;
230         }
232         memset(table, 0, 10*sizeof(int));
233         memset(arg, 0, 10*sizeof(char *));
235         i=0;
236         j=0;
237         while (strv[i] && strlen(strv[i]) > 0 && i < 9) {
238                 int id = search_get_tag_id(strv[i]);
239                 if (id == -1) {
240                         if (table[j]) {
241                                 char *tmp = arg[j];
242                                 arg[j] = g_strdup_printf("%s %s", arg[j], strv[i]);
243                                 g_free(tmp);
244                         } else {
245                                 screen_status_printf(_("Bad search tag %s"), strv[i]);
246                         }
247                         i++;
248                 } else if (strv[i+1] == NULL || strlen(strv[i+1]) == 0) {
249                         screen_status_printf(_("No argument for search tag %s"), strv[i]);
250                         i++;
251                         //        j--;
252                         //table[j] = -1;
253                 } else {
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 && j > 0) {
268                 int iter;
269                 mpd_InfoEntity *entity;
271                 /*-----------------------------------------------------------------------
272                  * NOTE (again): This code exists to test a new search ui,
273                  *               Its ugly and MUST be redesigned before the next release!
274                  *             + the code below should live in mpdclient.c
275                  *-----------------------------------------------------------------------
276                  */
277                 /** stupid - but this is just a test...... (fulhack)  */
278                 mpd_startSearch(c->connection, FALSE);
280                 for(iter = 0; iter < 10; iter++) {
281                         mpd_addConstraintSearch(c->connection, table[iter], arg[iter]);
282                 }
284                 mpd_commitSearch(c->connection);
286                 fl = g_malloc0(sizeof(mpdclient_filelist_t));
288                 while ((entity=mpd_getNextInfoEntity(c->connection)))
289                         filelist_append(fl, entity);
291                 if (mpdclient_finish_command(c) && fl)
292                         filelist_free(fl);
293         }
295         i=0;
296         while( arg[i] )
297                 g_free(arg[i++]);
299         return fl;
302 static void
303 search_new(mpdclient_t *c)
305         search_clear(c, TRUE);
307         g_free(pattern);
308         pattern = screen_readln(screen.status_window.w,
309                                 _("Search: "),
310                                 NULL,
311                                 &search_history,
312                                 NULL);
314         if (pattern && strcmp(pattern,"") == 0) {
315                 g_free(pattern);
316                 pattern=NULL;
317         }
319         if (pattern == NULL) {
320                 list_window_reset(browser.lw);
321                 return;
322         }
324         if (browser.filelist != NULL) {
325                 filelist_free(browser.filelist);
326                 browser.filelist = NULL;
327         }
329         if (!MPD_VERSION_LT(c, 0, 12, 0))
330                 browser.filelist = search_advanced_query(pattern, c);
332         if (!advanced_search_mode && browser.filelist == NULL)
333                 browser.filelist = filelist_search(c, FALSE,
334                                                   mode[options.search_mode].table,
335                                                   pattern);
337         if (browser.filelist == NULL)
338                 browser.filelist = filelist_new(NULL);
340         sync_highlights(c, browser.filelist);
341         mpdclient_install_playlist_callback(c, playlist_changed_callback);
342         list_window_check_selected(browser.lw, filelist_length(browser.filelist));
347 static void
348 init(WINDOW *w, int cols, int rows)
350         browser.lw = list_window_init(w, cols, rows);
353 static void
354 quit(void)
356         if (search_history)
357                 string_list_free(search_history);
358         if (browser.filelist)
359                 filelist_free(browser.filelist);
360         list_window_free(browser.lw);
362         if (pattern) {
363                 g_free(pattern);
364                 pattern = NULL;
365         }
368 static void
369 open(mpd_unused mpdclient_t *c)
371         //  if( pattern==NULL )
372         //    search_new(screen, c);
373         // else
374         screen_status_printf(_("Press %s for a new search"),
375                              get_key_names(CMD_SCREEN_SEARCH,0));
376         search_check_mode();
379 static void
380 resize(int cols, int rows)
382         browser.lw->cols = cols;
383         browser.lw->rows = rows;
386 static void
387 paint(void)
389         if (browser.filelist) {
390                 browser.lw->flags = 0;
391                 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
392         } else {
393                 browser.lw->flags = LW_HIDE_CURSOR;
394                 list_window_paint(browser.lw, lw_search_help_callback, NULL);
395         }
398 static const char *
399 get_title(char *str, size_t size)
401         if (advanced_search_mode && pattern)
402                 g_snprintf(str, size, _("Search: %s"), pattern);
403         else if (pattern)
404                 g_snprintf(str, size,
405                            _("Search: Results for %s [%s]"),
406                            pattern,
407                            _(mode[options.search_mode].label));
408         else
409                 g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
410                            get_key_names(CMD_SCREEN_SEARCH,0),
411                            _(mode[options.search_mode].label));
413         return str;
416 static int
417 search_cmd(mpdclient_t *c, command_t cmd)
419         switch (cmd) {
420         case CMD_SEARCH_MODE:
421                 options.search_mode++;
422                 if (mode[options.search_mode].label == NULL)
423                         options.search_mode = 0;
424                 screen_status_printf(_("Search mode: %s"),
425                                      _(mode[options.search_mode].label));
426                 /* continue and update... */
427         case CMD_SCREEN_UPDATE:
428                 if (pattern) {
429                         search_clear(c, FALSE);
430                         browser.filelist = filelist_search(c,
431                                                           FALSE,
432                                                           mode[options.search_mode].table,
433                                                           pattern);
434                         sync_highlights(c, browser.filelist);
435                 }
436                 search_repaint();
437                 return 1;
439         case CMD_SCREEN_SEARCH:
440                 search_new(c);
441                 search_repaint();
442                 return 1;
444         case CMD_CLEAR:
445                 search_clear(c, TRUE);
446                 list_window_reset(browser.lw);
447                 search_repaint();
448                 return 1;
450         default:
451                 break;
452         }
454         if (browser.filelist != NULL &&
455             browser_cmd(&browser, c, cmd)) {
456                 search_repaint();
457                 return 1;
458         }
460         return 0;
463 const struct screen_functions screen_search = {
464         .init = init,
465         .exit = quit,
466         .open = open,
467         .resize = resize,
468         .paint = paint,
469         .cmd = search_cmd,
470         .get_title = get_title,
471 };