Code

list_window: added attribute "length"
[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 "screen_search.h"
21 #include "screen_interface.h"
22 #include "screen_message.h"
23 #include "screen.h"
24 #include "i18n.h"
25 #include "options.h"
26 #include "charset.h"
27 #include "mpdclient.h"
28 #include "strfsong.h"
29 #include "utils.h"
30 #include "screen_utils.h"
31 #include "screen_browser.h"
32 #include "filelist.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;
97 static const char *const help_text[] = {
98         "Quick     -  Enter a string and ncmpc will search according",
99         "             to the current search mode (displayed above).",
100         "",
101         "Advanced  -  <tag>:<search term> [<tag>:<search term>...]",
102         "               Example: artist:radiohead album:pablo honey",
103         "",
104         "               Available tags: artist, album, title, track,",
105         "               name, genre, date composer, performer, comment, file",
106         "",
107 };
109 /* search info */
110 static const char *
111 lw_search_help_callback(unsigned idx, G_GNUC_UNUSED bool *highlight,
112                         G_GNUC_UNUSED char** sc, G_GNUC_UNUSED void *data)
114         if (idx >= G_N_ELEMENTS(help_text))
115                 return NULL;
117         return help_text[idx];
120 static void
121 screen_search_paint(void);
123 static void
124 search_repaint(void)
126         screen_search_paint();
127         wrefresh(browser.lw->w);
130 /* sanity check search mode value */
131 static void
132 search_check_mode(void)
134         int max = 0;
136         while (mode[max].label != NULL)
137                 max++;
138         if (options.search_mode < 0)
139                 options.search_mode = 0;
140         else if (options.search_mode >= max)
141                 options.search_mode = max-1;
144 static void
145 search_clear(bool clear_pattern)
147         if (browser.filelist) {
148                 filelist_free(browser.filelist);
149                 browser.filelist = filelist_new();
150                 list_window_set_length(browser.lw, 0);
151         }
152         if (clear_pattern && pattern) {
153                 g_free(pattern);
154                 pattern = NULL;
155         }
158 static struct filelist *
159 search_simple_query(struct mpd_connection *connection, bool exact_match,
160                     int table, gchar *local_pattern)
162         struct filelist *list;
163         gchar *filter_utf8 = locale_to_utf8(local_pattern);
165         if (table == SEARCH_ARTIST_TITLE) {
166                 mpd_command_list_begin(connection, false);
168                 mpd_search_db_songs(connection, exact_match);
169                 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
170                                               MPD_TAG_ARTIST, filter_utf8);
171                 mpd_search_commit(connection);
173                 mpd_search_db_songs(connection, exact_match);
174                 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
175                                               MPD_TAG_TITLE, filter_utf8);
176                 mpd_search_commit(connection);
178                 mpd_command_list_end(connection);
180                 list = filelist_new_recv(connection);
181                 filelist_no_duplicates(list);
182         } else if (table == SEARCH_URI) {
183                 mpd_search_db_songs(connection, exact_match);
184                 mpd_search_add_uri_constraint(connection, MPD_OPERATOR_DEFAULT,
185                                               filter_utf8);
186                 mpd_search_commit(connection);
188                 list = filelist_new_recv(connection);
189         } else {
190                 mpd_search_db_songs(connection, exact_match);
191                 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
192                                               table, filter_utf8);
193                 mpd_search_commit(connection);
195                 list = filelist_new_recv(connection);
196         }
198         g_free(filter_utf8);
199         return list;
202 /*-----------------------------------------------------------------------
203  * NOTE: This code exists to test a new search ui,
204  *       Its ugly and MUST be redesigned before the next release!
205  *-----------------------------------------------------------------------
206  */
207 static struct filelist *
208 search_advanced_query(struct mpd_connection *connection, char *query)
210         int i,j;
211         char **strv;
212         int table[10];
213         char *arg[10];
214         struct filelist *fl = NULL;
216         advanced_search_mode = FALSE;
217         if (strchr(query, ':') == NULL)
218                 return NULL;
220         strv = g_strsplit_set(query, ": ", 0);
222         memset(table, 0, 10*sizeof(int));
223         memset(arg, 0, 10*sizeof(char *));
225         i=0;
226         j=0;
227         while (strv[i] && strlen(strv[i]) > 0 && i < 9) {
228                 int id = search_get_tag_id(strv[i]);
229                 if (id == -1) {
230                         if (table[j]) {
231                                 char *tmp = arg[j];
232                                 arg[j] = g_strdup_printf("%s %s", arg[j], strv[i]);
233                                 g_free(tmp);
234                         } else {
235                                 screen_status_printf(_("Bad search tag %s"), strv[i]);
236                         }
237                         i++;
238                 } else if (strv[i+1] == NULL || strlen(strv[i+1]) == 0) {
239                         screen_status_printf(_("No argument for search tag %s"), strv[i]);
240                         i++;
241                         //        j--;
242                         //table[j] = -1;
243                 } else {
244                         table[j] = id;
245                         arg[j] = locale_to_utf8(strv[i+1]); // FREE ME
246                         j++;
247                         table[j] = -1;
248                         arg[j] = NULL;
249                         i = i + 2;
250                         advanced_search_mode = TRUE;
251                 }
252         }
254         g_strfreev(strv);
256         if (!advanced_search_mode || j == 0) {
257                 for (i = 0; arg[i] != NULL; ++i)
258                         g_free(arg[i]);
259                 return NULL;
260         }
262         /*-----------------------------------------------------------------------
263          * NOTE (again): This code exists to test a new search ui,
264          *               Its ugly and MUST be redesigned before the next release!
265          *             + the code below should live in mpdclient.c
266          *-----------------------------------------------------------------------
267          */
268         /** stupid - but this is just a test...... (fulhack)  */
269         mpd_search_db_songs(connection, false);
271         for (i = 0; i < 10 && arg[i] != NULL; i++) {
272                 if (table[i] == SEARCH_URI)
273                         mpd_search_add_uri_constraint(connection,
274                                                       MPD_OPERATOR_DEFAULT,
275                                                       arg[i]);
276                 else
277                         mpd_search_add_tag_constraint(connection,
278                                                       MPD_OPERATOR_DEFAULT,
279                                                       table[i], arg[i]);
280         }
282         mpd_search_commit(connection);
283         fl = filelist_new_recv(connection);
284         if (!mpd_response_finish(connection)) {
285                 filelist_free(fl);
286                 fl = NULL;
287         }
289         for (i = 0; arg[i] != NULL; ++i)
290                 g_free(arg[i]);
292         return fl;
295 static struct filelist *
296 do_search(struct mpdclient *c, char *query)
298         struct mpd_connection *connection = mpdclient_get_connection(c);
299         struct filelist *fl;
301         fl = search_advanced_query(connection, query);
302         if (fl != NULL)
303                 return fl;
305         if (mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS) {
306                 mpdclient_handle_error(c);
307                 return NULL;
308         }
310         fl = search_simple_query(connection, FALSE,
311                                  mode[options.search_mode].table,
312                                  query);
313         if (fl == NULL)
314                 mpdclient_handle_error(c);
315         return fl;
318 static void
319 screen_search_reload(struct mpdclient *c)
321         if (pattern == NULL)
322                 return;
324         if (browser.filelist != NULL) {
325                 filelist_free(browser.filelist);
326                 browser.filelist = NULL;
327         }
329         browser.filelist = do_search(c, pattern);
330         if (browser.filelist == NULL)
331                 browser.filelist = filelist_new();
332         list_window_set_length(browser.lw, filelist_length(browser.filelist));
334         screen_browser_sync_highlights(browser.filelist, &c->playlist);
337 static void
338 search_new(struct mpdclient *c)
340         if (!mpdclient_is_connected(c))
341                 return;
343         search_clear(true);
345         g_free(pattern);
346         pattern = screen_readln(_("Search"),
347                                 NULL,
348                                 &search_history,
349                                 NULL);
351         if (pattern == NULL) {
352                 list_window_reset(browser.lw);
353                 return;
354         }
356         screen_search_reload(c);
359 static void
360 screen_search_init(WINDOW *w, int cols, int rows)
362         browser.lw = list_window_init(w, cols, rows);
363         list_window_set_length(browser.lw, G_N_ELEMENTS(help_text));
366 static void
367 screen_search_quit(void)
369         if (search_history)
370                 string_list_free(search_history);
371         if (browser.filelist)
372                 filelist_free(browser.filelist);
373         list_window_free(browser.lw);
375         if (pattern) {
376                 g_free(pattern);
377                 pattern = NULL;
378         }
381 static void
382 screen_search_open(G_GNUC_UNUSED struct mpdclient *c)
384         //  if( pattern==NULL )
385         //    search_new(screen, c);
386         // else
387         screen_status_printf(_("Press %s for a new search"),
388                              get_key_names(CMD_SCREEN_SEARCH,0));
389         search_check_mode();
392 static void
393 screen_search_resize(int cols, int rows)
395         browser.lw->cols = cols;
396         browser.lw->rows = rows;
399 static void
400 screen_search_paint(void)
402         if (browser.filelist) {
403                 browser.lw->hide_cursor = false;
404                 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
405         } else {
406                 browser.lw->hide_cursor = true;
407                 list_window_paint(browser.lw, lw_search_help_callback, NULL);
408         }
411 static const char *
412 screen_search_get_title(char *str, size_t size)
414         if (advanced_search_mode && pattern)
415                 g_snprintf(str, size, _("Search: %s"), pattern);
416         else if (pattern)
417                 g_snprintf(str, size,
418                            _("Search: Results for %s [%s]"),
419                            pattern,
420                            _(mode[options.search_mode].label));
421         else
422                 g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
423                            get_key_names(CMD_SCREEN_SEARCH,0),
424                            _(mode[options.search_mode].label));
426         return str;
429 static void
430 screen_search_update(struct mpdclient *c)
432         if (browser.filelist != NULL && c->events & MPD_IDLE_PLAYLIST) {
433                 screen_browser_sync_highlights(browser.filelist, &c->playlist);
434                 search_repaint();
435         }
438 static bool
439 screen_search_cmd(struct mpdclient *c, command_t cmd)
441         switch (cmd) {
442         case CMD_SEARCH_MODE:
443                 options.search_mode++;
444                 if (mode[options.search_mode].label == NULL)
445                         options.search_mode = 0;
446                 screen_status_printf(_("Search mode: %s"),
447                                      _(mode[options.search_mode].label));
448                 /* continue and update... */
449         case CMD_SCREEN_UPDATE:
450                 screen_search_reload(c);
451                 search_repaint();
452                 return true;
454         case CMD_SCREEN_SEARCH:
455                 search_new(c);
456                 search_repaint();
457                 return true;
459         case CMD_CLEAR:
460                 search_clear(true);
461                 list_window_reset(browser.lw);
462                 search_repaint();
463                 return true;
465         default:
466                 break;
467         }
469         if (browser.filelist != NULL &&
470             browser_cmd(&browser, c, cmd)) {
471                 if (screen_is_visible(&screen_search))
472                         search_repaint();
473                 return true;
474         }
476         return false;
479 const struct screen_functions screen_search = {
480         .init = screen_search_init,
481         .exit = screen_search_quit,
482         .open = screen_search_open,
483         .resize = screen_search_resize,
484         .paint = screen_search_paint,
485         .update = screen_search_update,
486         .cmd = screen_search_cmd,
487         .get_title = screen_search_get_title,
488 };