Code

screen_browser: added wrapper function screen_browser_paint()
[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         assert(idx < G_N_ELEMENTS(help_text));
116         return help_text[idx];
119 static void
120 screen_search_paint(void);
122 static void
123 search_repaint(void)
125         screen_search_paint();
126         wrefresh(browser.lw->w);
129 /* sanity check search mode value */
130 static void
131 search_check_mode(void)
133         int max = 0;
135         while (mode[max].label != NULL)
136                 max++;
137         if (options.search_mode < 0)
138                 options.search_mode = 0;
139         else if (options.search_mode >= max)
140                 options.search_mode = max-1;
143 static void
144 search_clear(bool clear_pattern)
146         if (browser.filelist) {
147                 filelist_free(browser.filelist);
148                 browser.filelist = filelist_new();
149                 list_window_set_length(browser.lw, 0);
150         }
151         if (clear_pattern && pattern) {
152                 g_free(pattern);
153                 pattern = NULL;
154         }
157 static struct filelist *
158 search_simple_query(struct mpd_connection *connection, bool exact_match,
159                     int table, gchar *local_pattern)
161         struct filelist *list;
162         gchar *filter_utf8 = locale_to_utf8(local_pattern);
164         if (table == SEARCH_ARTIST_TITLE) {
165                 mpd_command_list_begin(connection, false);
167                 mpd_search_db_songs(connection, exact_match);
168                 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
169                                               MPD_TAG_ARTIST, filter_utf8);
170                 mpd_search_commit(connection);
172                 mpd_search_db_songs(connection, exact_match);
173                 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
174                                               MPD_TAG_TITLE, filter_utf8);
175                 mpd_search_commit(connection);
177                 mpd_command_list_end(connection);
179                 list = filelist_new_recv(connection);
180                 filelist_no_duplicates(list);
181         } else if (table == SEARCH_URI) {
182                 mpd_search_db_songs(connection, exact_match);
183                 mpd_search_add_uri_constraint(connection, MPD_OPERATOR_DEFAULT,
184                                               filter_utf8);
185                 mpd_search_commit(connection);
187                 list = filelist_new_recv(connection);
188         } else {
189                 mpd_search_db_songs(connection, exact_match);
190                 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
191                                               table, filter_utf8);
192                 mpd_search_commit(connection);
194                 list = filelist_new_recv(connection);
195         }
197         g_free(filter_utf8);
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 struct filelist *
207 search_advanced_query(struct mpd_connection *connection, char *query)
209         int i,j;
210         char **strv;
211         int table[10];
212         char *arg[10];
213         struct filelist *fl = NULL;
215         advanced_search_mode = FALSE;
216         if (strchr(query, ':') == NULL)
217                 return NULL;
219         strv = g_strsplit_set(query, ": ", 0);
221         memset(table, 0, 10*sizeof(int));
222         memset(arg, 0, 10*sizeof(char *));
224         i=0;
225         j=0;
226         while (strv[i] && strlen(strv[i]) > 0 && i < 9) {
227                 int id = search_get_tag_id(strv[i]);
228                 if (id == -1) {
229                         if (table[j]) {
230                                 char *tmp = arg[j];
231                                 arg[j] = g_strdup_printf("%s %s", arg[j], strv[i]);
232                                 g_free(tmp);
233                         } else {
234                                 screen_status_printf(_("Bad search tag %s"), strv[i]);
235                         }
236                         i++;
237                 } else if (strv[i+1] == NULL || strlen(strv[i+1]) == 0) {
238                         screen_status_printf(_("No argument for search tag %s"), strv[i]);
239                         i++;
240                         //        j--;
241                         //table[j] = -1;
242                 } else {
243                         table[j] = id;
244                         arg[j] = locale_to_utf8(strv[i+1]); // FREE ME
245                         j++;
246                         table[j] = -1;
247                         arg[j] = NULL;
248                         i = i + 2;
249                         advanced_search_mode = TRUE;
250                 }
251         }
253         g_strfreev(strv);
255         if (!advanced_search_mode || j == 0) {
256                 for (i = 0; arg[i] != NULL; ++i)
257                         g_free(arg[i]);
258                 return NULL;
259         }
261         /*-----------------------------------------------------------------------
262          * NOTE (again): This code exists to test a new search ui,
263          *               Its ugly and MUST be redesigned before the next release!
264          *             + the code below should live in mpdclient.c
265          *-----------------------------------------------------------------------
266          */
267         /** stupid - but this is just a test...... (fulhack)  */
268         mpd_search_db_songs(connection, false);
270         for (i = 0; i < 10 && arg[i] != NULL; i++) {
271                 if (table[i] == SEARCH_URI)
272                         mpd_search_add_uri_constraint(connection,
273                                                       MPD_OPERATOR_DEFAULT,
274                                                       arg[i]);
275                 else
276                         mpd_search_add_tag_constraint(connection,
277                                                       MPD_OPERATOR_DEFAULT,
278                                                       table[i], arg[i]);
279         }
281         mpd_search_commit(connection);
282         fl = filelist_new_recv(connection);
283         if (!mpd_response_finish(connection)) {
284                 filelist_free(fl);
285                 fl = NULL;
286         }
288         for (i = 0; arg[i] != NULL; ++i)
289                 g_free(arg[i]);
291         return fl;
294 static struct filelist *
295 do_search(struct mpdclient *c, char *query)
297         struct mpd_connection *connection = mpdclient_get_connection(c);
298         struct filelist *fl;
300         fl = search_advanced_query(connection, query);
301         if (fl != NULL)
302                 return fl;
304         if (mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS) {
305                 mpdclient_handle_error(c);
306                 return NULL;
307         }
309         fl = search_simple_query(connection, FALSE,
310                                  mode[options.search_mode].table,
311                                  query);
312         if (fl == NULL)
313                 mpdclient_handle_error(c);
314         return fl;
317 static void
318 screen_search_reload(struct mpdclient *c)
320         if (pattern == NULL)
321                 return;
323         if (browser.filelist != NULL) {
324                 filelist_free(browser.filelist);
325                 browser.filelist = NULL;
326         }
328         browser.filelist = do_search(c, pattern);
329         if (browser.filelist == NULL)
330                 browser.filelist = filelist_new();
331         list_window_set_length(browser.lw, filelist_length(browser.filelist));
333         screen_browser_sync_highlights(browser.filelist, &c->playlist);
336 static void
337 search_new(struct mpdclient *c)
339         if (!mpdclient_is_connected(c))
340                 return;
342         search_clear(true);
344         g_free(pattern);
345         pattern = screen_readln(_("Search"),
346                                 NULL,
347                                 &search_history,
348                                 NULL);
350         if (pattern == NULL) {
351                 list_window_reset(browser.lw);
352                 return;
353         }
355         screen_search_reload(c);
358 static void
359 screen_search_init(WINDOW *w, int cols, int rows)
361         browser.lw = list_window_init(w, cols, rows);
362         list_window_set_length(browser.lw, G_N_ELEMENTS(help_text));
365 static void
366 screen_search_quit(void)
368         if (search_history)
369                 string_list_free(search_history);
370         if (browser.filelist)
371                 filelist_free(browser.filelist);
372         list_window_free(browser.lw);
374         if (pattern) {
375                 g_free(pattern);
376                 pattern = NULL;
377         }
380 static void
381 screen_search_open(G_GNUC_UNUSED struct mpdclient *c)
383         //  if( pattern==NULL )
384         //    search_new(screen, c);
385         // else
386         screen_status_printf(_("Press %s for a new search"),
387                              get_key_names(CMD_SCREEN_SEARCH,0));
388         search_check_mode();
391 static void
392 screen_search_resize(int cols, int rows)
394         list_window_resize(browser.lw, cols, rows);
397 static void
398 screen_search_paint(void)
400         if (browser.filelist) {
401                 browser.lw->hide_cursor = false;
402                 screen_browser_paint(&browser);
403         } else {
404                 browser.lw->hide_cursor = true;
405                 list_window_paint(browser.lw, lw_search_help_callback, NULL);
406         }
409 static const char *
410 screen_search_get_title(char *str, size_t size)
412         if (advanced_search_mode && pattern)
413                 g_snprintf(str, size, _("Search: %s"), pattern);
414         else if (pattern)
415                 g_snprintf(str, size,
416                            _("Search: Results for %s [%s]"),
417                            pattern,
418                            _(mode[options.search_mode].label));
419         else
420                 g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
421                            get_key_names(CMD_SCREEN_SEARCH,0),
422                            _(mode[options.search_mode].label));
424         return str;
427 static void
428 screen_search_update(struct mpdclient *c)
430         if (browser.filelist != NULL && c->events & MPD_IDLE_PLAYLIST) {
431                 screen_browser_sync_highlights(browser.filelist, &c->playlist);
432                 search_repaint();
433         }
436 static bool
437 screen_search_cmd(struct mpdclient *c, command_t cmd)
439         switch (cmd) {
440         case CMD_SEARCH_MODE:
441                 options.search_mode++;
442                 if (mode[options.search_mode].label == NULL)
443                         options.search_mode = 0;
444                 screen_status_printf(_("Search mode: %s"),
445                                      _(mode[options.search_mode].label));
446                 /* continue and update... */
447         case CMD_SCREEN_UPDATE:
448                 screen_search_reload(c);
449                 search_repaint();
450                 return true;
452         case CMD_SCREEN_SEARCH:
453                 search_new(c);
454                 search_repaint();
455                 return true;
457         case CMD_CLEAR:
458                 search_clear(true);
459                 list_window_reset(browser.lw);
460                 search_repaint();
461                 return true;
463         default:
464                 break;
465         }
467         if (browser.filelist != NULL &&
468             browser_cmd(&browser, c, cmd)) {
469                 if (screen_is_visible(&screen_search))
470                         search_repaint();
471                 return true;
472         }
474         return false;
477 const struct screen_functions screen_search = {
478         .init = screen_search_init,
479         .exit = screen_search_quit,
480         .open = screen_search_open,
481         .resize = screen_search_resize,
482         .paint = screen_search_paint,
483         .update = screen_search_update,
484         .cmd = screen_search_cmd,
485         .get_title = screen_search_get_title,
486 };