Code

screen_utils: don't pass WINDOW to screen_readln()
[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 (!mpd_response_finish(c->connection)) {
303                         filelist_free(fl);
304                         fl = NULL;
306                         mpdclient_handle_error(c);
307                 }
308         }
310         i=0;
311         while( arg[i] )
312                 g_free(arg[i++]);
314         return fl;
317 static void
318 search_new(struct mpdclient *c)
320         if (c->connection == NULL)
321                 return;
323         search_clear(c, TRUE);
325         g_free(pattern);
326         pattern = screen_readln(_("Search"),
327                                 NULL,
328                                 &search_history,
329                                 NULL);
331         if (pattern == NULL) {
332                 list_window_reset(browser.lw);
333                 return;
334         }
336         if (browser.filelist != NULL) {
337                 filelist_free(browser.filelist);
338                 browser.filelist = NULL;
339         }
341         if (mpd_connection_cmp_server_version(c->connection, 0, 12, 0) >= 0)
342                 browser.filelist = search_advanced_query(pattern, c);
344         if (!advanced_search_mode && browser.filelist == NULL)
345                 browser.filelist = filelist_search(c, FALSE,
346                                                   mode[options.search_mode].table,
347                                                   pattern);
349         if (browser.filelist == NULL)
350                 browser.filelist = filelist_new();
352         sync_highlights(c, browser.filelist);
353         mpdclient_install_playlist_callback(c, playlist_changed_callback);
354         list_window_check_selected(browser.lw, filelist_length(browser.filelist));
359 static void
360 init(WINDOW *w, int cols, int rows)
362         browser.lw = list_window_init(w, cols, rows);
365 static void
366 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 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 resize(int cols, int rows)
394         browser.lw->cols = cols;
395         browser.lw->rows = rows;
398 static void
399 paint(void)
401         if (browser.filelist) {
402                 browser.lw->hide_cursor = false;
403                 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
404         } else {
405                 browser.lw->hide_cursor = true;
406                 list_window_paint(browser.lw, lw_search_help_callback, NULL);
407         }
410 static const char *
411 get_title(char *str, size_t size)
413         if (advanced_search_mode && pattern)
414                 g_snprintf(str, size, _("Search: %s"), pattern);
415         else if (pattern)
416                 g_snprintf(str, size,
417                            _("Search: Results for %s [%s]"),
418                            pattern,
419                            _(mode[options.search_mode].label));
420         else
421                 g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
422                            get_key_names(CMD_SCREEN_SEARCH,0),
423                            _(mode[options.search_mode].label));
425         return str;
428 static bool
429 search_cmd(struct mpdclient *c, command_t cmd)
431         switch (cmd) {
432         case CMD_SEARCH_MODE:
433                 options.search_mode++;
434                 if (mode[options.search_mode].label == NULL)
435                         options.search_mode = 0;
436                 screen_status_printf(_("Search mode: %s"),
437                                      _(mode[options.search_mode].label));
438                 /* continue and update... */
439         case CMD_SCREEN_UPDATE:
440                 if (pattern) {
441                         search_clear(c, FALSE);
442                         browser.filelist = filelist_search(c,
443                                                           FALSE,
444                                                           mode[options.search_mode].table,
445                                                           pattern);
446                         sync_highlights(c, browser.filelist);
447                 }
448                 search_repaint();
449                 return true;
451         case CMD_SCREEN_SEARCH:
452                 search_new(c);
453                 search_repaint();
454                 return true;
456         case CMD_CLEAR:
457                 search_clear(c, TRUE);
458                 list_window_reset(browser.lw);
459                 search_repaint();
460                 return true;
462         default:
463                 break;
464         }
466         if (browser.filelist != NULL &&
467             browser_cmd(&browser, c, cmd)) {
468                 if (screen_is_visible(&screen_search))
469                         search_repaint();
470                 return true;
471         }
473         return false;
476 const struct screen_functions screen_search = {
477         .init = init,
478         .exit = quit,
479         .open = open,
480         .resize = resize,
481         .paint = paint,
482         .cmd = search_cmd,
483         .get_title = get_title,
484 };