Code

use libmpdclient2
[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 */
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"
30 #include <ctype.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <glib.h>
35 enum {
36         SEARCH_URI = MPD_TAG_COUNT + 100,
37 };
39 static const struct {
40         const char *name;
41         const char *localname;
42 } search_tag[MPD_TAG_COUNT] = {
43         [MPD_TAG_ARTIST] = { "artist", N_("artist") },
44         [MPD_TAG_ALBUM] = { "album", N_("album") },
45         [MPD_TAG_TITLE] = { "title", N_("title") },
46         [MPD_TAG_TRACK] = { "track", N_("track") },
47         [MPD_TAG_NAME] = { "name", N_("name") },
48         [MPD_TAG_GENRE] = { "genre", N_("genre") },
49         [MPD_TAG_DATE] = { "date", N_("date") },
50         [MPD_TAG_COMPOSER] = { "composer", N_("composer") },
51         [MPD_TAG_PERFORMER] = { "performer", N_("performer") },
52         [MPD_TAG_COMMENT] = { "comment", N_("comment") },
53 };
55 static int
56 search_get_tag_id(const char *name)
57 {
58         unsigned i;
60         if (g_ascii_strcasecmp(name, "file") == 0 ||
61             strcasecmp(name, _("file")) == 0)
62                 return SEARCH_URI;
64         for (i = 0; i < MPD_TAG_COUNT; ++i)
65                 if (search_tag[i].name != NULL &&
66                     (strcasecmp(search_tag[i].name, name) == 0 ||
67                      strcasecmp(search_tag[i].localname, name) == 0))
68                         return i;
70         return -1;
71 }
73 #define SEARCH_ARTIST_TITLE 999
75 typedef struct {
76         enum mpd_tag_type table;
77         const char *label;
78 } search_type_t;
80 static search_type_t mode[] = {
81         { MPD_TAG_TITLE, N_("Title") },
82         { MPD_TAG_ARTIST, N_("Artist") },
83         { MPD_TAG_ALBUM, N_("Album") },
84         { SEARCH_URI, N_("file") },
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, G_GNUC_UNUSED bool *highlight,
99                         G_GNUC_UNUSED char** sc, G_GNUC_UNUSED void *data)
101         unsigned text_rows;
102         static const char *text[] = {
103                 "Quick     -  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                 "               Available 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 has 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();
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, G_GNUC_UNUSED int exact_match, int table,
180                 gchar *local_pattern)
182         mpdclient_filelist_t *list, *list2;
183         gchar *filter_utf8 = locale_to_utf8(local_pattern);
185         if (table == SEARCH_ARTIST_TITLE) {
186                 list = mpdclient_filelist_search(c, FALSE, MPD_TAG_ARTIST,
187                                                  filter_utf8);
188                 if (list == NULL)
189                         list = filelist_new();
191                 list2 = mpdclient_filelist_search(c, FALSE, MPD_TAG_TITLE,
192                                                   filter_utf8);
193                 if (list2 != NULL) {
194                         filelist_move(list, list2);
195                         filelist_free(list2);
196                 }
198                 filelist_sort_all(list, compare_filelistentry_format);
199         } else {
200                 list = mpdclient_filelist_search(c, FALSE, table, filter_utf8);
201                 if (list == NULL)
202                         list = filelist_new();
203         }
205         g_free(filter_utf8);
206         return list;
209 /*-----------------------------------------------------------------------
210  * NOTE: This code exists to test a new search ui,
211  *       Its ugly and MUST be redesigned before the next release!
212  *-----------------------------------------------------------------------
213  */
214 static mpdclient_filelist_t *
215 search_advanced_query(char *query, mpdclient_t *c)
217         int i,j;
218         char **strv;
219         int table[10];
220         char *arg[10];
221         mpdclient_filelist_t *fl = NULL;
223         advanced_search_mode = FALSE;
224         if( g_strrstr(query, ":") == NULL )
225                 return NULL;
227         strv = g_strsplit_set(query, ": ", 0);
229         i=0;
230         while (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                                 screen_status_printf(_("Bad search tag %s"), strv[i]);
248                         }
249                         i++;
250                 } else if (strv[i+1] == NULL || strlen(strv[i+1]) == 0) {
251                         screen_status_printf(_("No argument for search tag %s"), strv[i]);
252                         i++;
253                         //        j--;
254                         //table[j] = -1;
255                 } else {
256                         table[j] = id;
257                         arg[j] = locale_to_utf8(strv[i+1]); // FREE ME
258                         j++;
259                         table[j] = -1;
260                         arg[j] = NULL;
261                         i = i + 2;
262                         advanced_search_mode = TRUE;
263                 }
264         }
266         g_strfreev(strv);
269         if (advanced_search_mode && j > 0) {
270                 int iter;
271                 struct mpd_entity *entity;
273                 /*-----------------------------------------------------------------------
274                  * NOTE (again): This code exists to test a new search ui,
275                  *               Its ugly and MUST be redesigned before the next release!
276                  *             + the code below should live in mpdclient.c
277                  *-----------------------------------------------------------------------
278                  */
279                 /** stupid - but this is just a test...... (fulhack)  */
280                 mpd_search_db_songs(c->connection, false);
282                 for(iter = 0; iter < 10 && arg[iter] != NULL; iter++) {
283                         if (table[iter] == SEARCH_URI)
284                                 mpd_search_add_uri_constraint(c->connection,
285                                                               MPD_OPERATOR_DEFAULT,
286                                                               arg[iter]);
287                         else
288                                 mpd_search_add_tag_constraint(c->connection,
289                                                               MPD_OPERATOR_DEFAULT,
290                                                               table[iter], arg[iter]);
291                 }
293                 mpd_search_commit(c->connection);
295                 fl = filelist_new();
297                 while ((entity = mpd_recv_entity(c->connection)) != NULL)
298                         filelist_append(fl, entity);
300                 if (mpdclient_finish_command(c) && fl)
301                         filelist_free(fl);
302         }
304         i=0;
305         while( arg[i] )
306                 g_free(arg[i++]);
308         return fl;
311 static void
312 search_new(mpdclient_t *c)
314         if (c->connection == NULL)
315                 return;
317         search_clear(c, TRUE);
319         g_free(pattern);
320         pattern = screen_readln(screen.status_window.w,
321                                 _("Search"),
322                                 NULL,
323                                 &search_history,
324                                 NULL);
326         if (pattern == NULL) {
327                 list_window_reset(browser.lw);
328                 return;
329         }
331         if (browser.filelist != NULL) {
332                 filelist_free(browser.filelist);
333                 browser.filelist = NULL;
334         }
336         if (mpd_connection_cmp_server_version(c->connection, 0, 12, 0) >= 0)
337                 browser.filelist = search_advanced_query(pattern, c);
339         if (!advanced_search_mode && browser.filelist == NULL)
340                 browser.filelist = filelist_search(c, FALSE,
341                                                   mode[options.search_mode].table,
342                                                   pattern);
344         if (browser.filelist == NULL)
345                 browser.filelist = filelist_new();
347         sync_highlights(c, browser.filelist);
348         mpdclient_install_playlist_callback(c, playlist_changed_callback);
349         list_window_check_selected(browser.lw, filelist_length(browser.filelist));
354 static void
355 init(WINDOW *w, int cols, int rows)
357         browser.lw = list_window_init(w, cols, rows);
360 static void
361 quit(void)
363         if (search_history)
364                 string_list_free(search_history);
365         if (browser.filelist)
366                 filelist_free(browser.filelist);
367         list_window_free(browser.lw);
369         if (pattern) {
370                 g_free(pattern);
371                 pattern = NULL;
372         }
375 static void
376 open(G_GNUC_UNUSED mpdclient_t *c)
378         //  if( pattern==NULL )
379         //    search_new(screen, c);
380         // else
381         screen_status_printf(_("Press %s for a new search"),
382                              get_key_names(CMD_SCREEN_SEARCH,0));
383         search_check_mode();
386 static void
387 resize(int cols, int rows)
389         browser.lw->cols = cols;
390         browser.lw->rows = rows;
393 static void
394 paint(void)
396         if (browser.filelist) {
397                 browser.lw->hide_cursor = false;
398                 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
399         } else {
400                 browser.lw->hide_cursor = true;
401                 list_window_paint(browser.lw, lw_search_help_callback, NULL);
402         }
405 static const char *
406 get_title(char *str, size_t size)
408         if (advanced_search_mode && pattern)
409                 g_snprintf(str, size, _("Search: %s"), pattern);
410         else if (pattern)
411                 g_snprintf(str, size,
412                            _("Search: Results for %s [%s]"),
413                            pattern,
414                            _(mode[options.search_mode].label));
415         else
416                 g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
417                            get_key_names(CMD_SCREEN_SEARCH,0),
418                            _(mode[options.search_mode].label));
420         return str;
423 static bool
424 search_cmd(mpdclient_t *c, command_t cmd)
426         switch (cmd) {
427         case CMD_SEARCH_MODE:
428                 options.search_mode++;
429                 if (mode[options.search_mode].label == NULL)
430                         options.search_mode = 0;
431                 screen_status_printf(_("Search mode: %s"),
432                                      _(mode[options.search_mode].label));
433                 /* continue and update... */
434         case CMD_SCREEN_UPDATE:
435                 if (pattern) {
436                         search_clear(c, FALSE);
437                         browser.filelist = filelist_search(c,
438                                                           FALSE,
439                                                           mode[options.search_mode].table,
440                                                           pattern);
441                         sync_highlights(c, browser.filelist);
442                 }
443                 search_repaint();
444                 return true;
446         case CMD_SCREEN_SEARCH:
447                 search_new(c);
448                 search_repaint();
449                 return true;
451         case CMD_CLEAR:
452                 search_clear(c, TRUE);
453                 list_window_reset(browser.lw);
454                 search_repaint();
455                 return true;
457         default:
458                 break;
459         }
461         if (browser.filelist != NULL &&
462             browser_cmd(&browser, c, cmd)) {
463                 if (screen_is_visible(&screen_search))
464                         search_repaint();
465                 return true;
466         }
468         return false;
471 const struct screen_functions screen_search = {
472         .init = init,
473         .exit = quit,
474         .open = open,
475         .resize = resize,
476         .paint = paint,
477         .cmd = search_cmd,
478         .get_title = get_title,
479 };