Code

mpdclient: removed the mpdclient_t typedef
[ncmpc.git] / src / screen_search.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2009 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
20 #include "i18n.h"
21 #include "options.h"
22 #include "charset.h"
23 #include "mpdclient.h"
24 #include "strfsong.h"
25 #include "command.h"
26 #include "screen.h"
27 #include "utils.h"
28 #include "screen_utils.h"
29 #include "screen_browser.h"
30 #include "filelist.h"
32 #include <ctype.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <glib.h>
37 enum {
38         SEARCH_URI = MPD_TAG_COUNT + 100,
39 };
41 static const struct {
42         const char *name;
43         const char *localname;
44 } search_tag[MPD_TAG_COUNT] = {
45         [MPD_TAG_ARTIST] = { "artist", N_("artist") },
46         [MPD_TAG_ALBUM] = { "album", N_("album") },
47         [MPD_TAG_TITLE] = { "title", N_("title") },
48         [MPD_TAG_TRACK] = { "track", N_("track") },
49         [MPD_TAG_NAME] = { "name", N_("name") },
50         [MPD_TAG_GENRE] = { "genre", N_("genre") },
51         [MPD_TAG_DATE] = { "date", N_("date") },
52         [MPD_TAG_COMPOSER] = { "composer", N_("composer") },
53         [MPD_TAG_PERFORMER] = { "performer", N_("performer") },
54         [MPD_TAG_COMMENT] = { "comment", N_("comment") },
55 };
57 static int
58 search_get_tag_id(const char *name)
59 {
60         unsigned i;
62         if (g_ascii_strcasecmp(name, "file") == 0 ||
63             strcasecmp(name, _("file")) == 0)
64                 return SEARCH_URI;
66         for (i = 0; i < MPD_TAG_COUNT; ++i)
67                 if (search_tag[i].name != NULL &&
68                     (strcasecmp(search_tag[i].name, name) == 0 ||
69                      strcasecmp(search_tag[i].localname, name) == 0))
70                         return i;
72         return -1;
73 }
75 #define SEARCH_ARTIST_TITLE 999
77 typedef struct {
78         enum mpd_tag_type table;
79         const char *label;
80 } search_type_t;
82 static search_type_t mode[] = {
83         { MPD_TAG_TITLE, N_("Title") },
84         { MPD_TAG_ARTIST, N_("Artist") },
85         { MPD_TAG_ALBUM, N_("Album") },
86         { SEARCH_URI, N_("file") },
87         { SEARCH_ARTIST_TITLE, N_("Artist + Title") },
88         { 0, NULL }
89 };
91 static GList *search_history = NULL;
92 static gchar *pattern = NULL;
93 static gboolean advanced_search_mode = FALSE;
95 static struct screen_browser browser;
98 /* search info */
99 static const char *
100 lw_search_help_callback(unsigned idx, G_GNUC_UNUSED bool *highlight,
101                         G_GNUC_UNUSED char** sc, G_GNUC_UNUSED void *data)
103         unsigned text_rows;
104         static const char *text[] = {
105                 "Quick     -  Enter a string and ncmpc will search according",
106                 "             to the current search mode (displayed above).",
107                 "",
108                 "Advanced  -  <tag>:<search term> [<tag>:<search term>...]",
109                 "               Example: artist:radiohead album:pablo honey",
110                 "",
111                 "               Available tags: artist, album, title, track,",
112                 "               name, genre, date composer, performer, comment, file",
113                 "",
114                 NULL
115         };
117         text_rows=0;
118         while (text[text_rows])
119                 text_rows++;
121         if (idx < text_rows)
122                 return text[idx];
123         return NULL;
126 static void
127 paint(void);
129 static void
130 search_repaint(void)
132         paint();
133         wrefresh(browser.lw->w);
136 static void
137 search_repaint_if_active(void)
139         if (screen_is_visible(&screen_search))
140                 search_repaint();
143 /* the playlist has been updated -> fix highlights */
144 static void
145 playlist_changed_callback(struct mpdclient *c, int event, gpointer data)
147         browser_playlist_changed(&browser, c, event, data);
148         search_repaint_if_active();
151 /* sanity check search mode value */
152 static void
153 search_check_mode(void)
155         int max = 0;
157         while (mode[max].label != NULL)
158                 max++;
159         if (options.search_mode < 0)
160                 options.search_mode = 0;
161         else if (options.search_mode >= max)
162                 options.search_mode = max-1;
165 static void
166 search_clear(struct mpdclient *c,
167              gboolean clear_pattern)
169         if (browser.filelist) {
170                 mpdclient_remove_playlist_callback(c, playlist_changed_callback);
171                 filelist_free(browser.filelist);
172                 browser.filelist = filelist_new();
173         }
174         if (clear_pattern && pattern) {
175                 g_free(pattern);
176                 pattern = NULL;
177         }
180 static struct filelist *
181 filelist_search(struct mpdclient *c, G_GNUC_UNUSED int exact_match, int table,
182                 gchar *local_pattern)
184         struct filelist *list, *list2;
185         gchar *filter_utf8 = locale_to_utf8(local_pattern);
187         if (table == SEARCH_ARTIST_TITLE) {
188                 list = mpdclient_filelist_search(c, FALSE, MPD_TAG_ARTIST,
189                                                  filter_utf8);
190                 if (list == NULL)
191                         list = filelist_new();
193                 list2 = mpdclient_filelist_search(c, FALSE, MPD_TAG_TITLE,
194                                                   filter_utf8);
195                 if (list2 != NULL) {
196                         filelist_move(list, list2);
197                         filelist_free(list2);
198                 }
200                 filelist_sort_all(list, compare_filelistentry_format);
201         } else {
202                 list = mpdclient_filelist_search(c, FALSE, table, filter_utf8);
203                 if (list == NULL)
204                         list = filelist_new();
205         }
207         g_free(filter_utf8);
208         return list;
211 /*-----------------------------------------------------------------------
212  * NOTE: This code exists to test a new search ui,
213  *       Its ugly and MUST be redesigned before the next release!
214  *-----------------------------------------------------------------------
215  */
216 static struct filelist *
217 search_advanced_query(char *query, struct mpdclient *c)
219         int i,j;
220         char **strv;
221         int table[10];
222         char *arg[10];
223         struct filelist *fl = NULL;
225         advanced_search_mode = FALSE;
226         if( g_strrstr(query, ":") == NULL )
227                 return NULL;
229         strv = g_strsplit_set(query, ": ", 0);
231         i=0;
232         while (strv[i]) {
233                 i++;
234         }
236         memset(table, 0, 10*sizeof(int));
237         memset(arg, 0, 10*sizeof(char *));
239         i=0;
240         j=0;
241         while (strv[i] && strlen(strv[i]) > 0 && i < 9) {
242                 int id = search_get_tag_id(strv[i]);
243                 if (id == -1) {
244                         if (table[j]) {
245                                 char *tmp = arg[j];
246                                 arg[j] = g_strdup_printf("%s %s", arg[j], strv[i]);
247                                 g_free(tmp);
248                         } else {
249                                 screen_status_printf(_("Bad search tag %s"), strv[i]);
250                         }
251                         i++;
252                 } else if (strv[i+1] == NULL || strlen(strv[i+1]) == 0) {
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                 struct mpd_entity *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_search_db_songs(c->connection, false);
284                 for(iter = 0; iter < 10 && arg[iter] != NULL; iter++) {
285                         if (table[iter] == SEARCH_URI)
286                                 mpd_search_add_uri_constraint(c->connection,
287                                                               MPD_OPERATOR_DEFAULT,
288                                                               arg[iter]);
289                         else
290                                 mpd_search_add_tag_constraint(c->connection,
291                                                               MPD_OPERATOR_DEFAULT,
292                                                               table[iter], arg[iter]);
293                 }
295                 mpd_search_commit(c->connection);
297                 fl = filelist_new();
299                 while ((entity = mpd_recv_entity(c->connection)) != NULL)
300                         filelist_append(fl, entity);
302                 if (mpdclient_finish_command(c) && fl)
303                         filelist_free(fl);
304         }
306         i=0;
307         while( arg[i] )
308                 g_free(arg[i++]);
310         return fl;
313 static void
314 search_new(struct mpdclient *c)
316         if (c->connection == NULL)
317                 return;
319         search_clear(c, TRUE);
321         g_free(pattern);
322         pattern = screen_readln(screen.status_window.w,
323                                 _("Search"),
324                                 NULL,
325                                 &search_history,
326                                 NULL);
328         if (pattern == NULL) {
329                 list_window_reset(browser.lw);
330                 return;
331         }
333         if (browser.filelist != NULL) {
334                 filelist_free(browser.filelist);
335                 browser.filelist = NULL;
336         }
338         if (mpd_connection_cmp_server_version(c->connection, 0, 12, 0) >= 0)
339                 browser.filelist = search_advanced_query(pattern, c);
341         if (!advanced_search_mode && browser.filelist == NULL)
342                 browser.filelist = filelist_search(c, FALSE,
343                                                   mode[options.search_mode].table,
344                                                   pattern);
346         if (browser.filelist == NULL)
347                 browser.filelist = filelist_new();
349         sync_highlights(c, browser.filelist);
350         mpdclient_install_playlist_callback(c, playlist_changed_callback);
351         list_window_check_selected(browser.lw, filelist_length(browser.filelist));
356 static void
357 init(WINDOW *w, int cols, int rows)
359         browser.lw = list_window_init(w, cols, rows);
362 static void
363 quit(void)
365         if (search_history)
366                 string_list_free(search_history);
367         if (browser.filelist)
368                 filelist_free(browser.filelist);
369         list_window_free(browser.lw);
371         if (pattern) {
372                 g_free(pattern);
373                 pattern = NULL;
374         }
377 static void
378 open(G_GNUC_UNUSED struct mpdclient *c)
380         //  if( pattern==NULL )
381         //    search_new(screen, c);
382         // else
383         screen_status_printf(_("Press %s for a new search"),
384                              get_key_names(CMD_SCREEN_SEARCH,0));
385         search_check_mode();
388 static void
389 resize(int cols, int rows)
391         browser.lw->cols = cols;
392         browser.lw->rows = rows;
395 static void
396 paint(void)
398         if (browser.filelist) {
399                 browser.lw->hide_cursor = false;
400                 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
401         } else {
402                 browser.lw->hide_cursor = true;
403                 list_window_paint(browser.lw, lw_search_help_callback, NULL);
404         }
407 static const char *
408 get_title(char *str, size_t size)
410         if (advanced_search_mode && pattern)
411                 g_snprintf(str, size, _("Search: %s"), pattern);
412         else if (pattern)
413                 g_snprintf(str, size,
414                            _("Search: Results for %s [%s]"),
415                            pattern,
416                            _(mode[options.search_mode].label));
417         else
418                 g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
419                            get_key_names(CMD_SCREEN_SEARCH,0),
420                            _(mode[options.search_mode].label));
422         return str;
425 static bool
426 search_cmd(struct mpdclient *c, command_t cmd)
428         switch (cmd) {
429         case CMD_SEARCH_MODE:
430                 options.search_mode++;
431                 if (mode[options.search_mode].label == NULL)
432                         options.search_mode = 0;
433                 screen_status_printf(_("Search mode: %s"),
434                                      _(mode[options.search_mode].label));
435                 /* continue and update... */
436         case CMD_SCREEN_UPDATE:
437                 if (pattern) {
438                         search_clear(c, FALSE);
439                         browser.filelist = filelist_search(c,
440                                                           FALSE,
441                                                           mode[options.search_mode].table,
442                                                           pattern);
443                         sync_highlights(c, browser.filelist);
444                 }
445                 search_repaint();
446                 return true;
448         case CMD_SCREEN_SEARCH:
449                 search_new(c);
450                 search_repaint();
451                 return true;
453         case CMD_CLEAR:
454                 search_clear(c, TRUE);
455                 list_window_reset(browser.lw);
456                 search_repaint();
457                 return true;
459         default:
460                 break;
461         }
463         if (browser.filelist != NULL &&
464             browser_cmd(&browser, c, cmd)) {
465                 if (screen_is_visible(&screen_search))
466                         search_repaint();
467                 return true;
468         }
470         return false;
473 const struct screen_functions screen_search = {
474         .init = init,
475         .exit = quit,
476         .open = open,
477         .resize = resize,
478         .paint = paint,
479         .cmd = search_cmd,
480         .get_title = get_title,
481 };