Code

screen_search: fix crash when disconnected
[ncmpc.git] / src / screen_search.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2010 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
4  *
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.
9  *
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.
14  *
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_status.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         SEARCH_ARTIST_TITLE
40 };
42 static const struct {
43         const char *name;
44         const char *localname;
45 } search_tag[MPD_TAG_COUNT] = {
46         [MPD_TAG_ARTIST] = { "artist", N_("artist") },
47         [MPD_TAG_ALBUM] = { "album", N_("album") },
48         [MPD_TAG_TITLE] = { "title", N_("title") },
49         [MPD_TAG_TRACK] = { "track", N_("track") },
50         [MPD_TAG_NAME] = { "name", N_("name") },
51         [MPD_TAG_GENRE] = { "genre", N_("genre") },
52         [MPD_TAG_DATE] = { "date", N_("date") },
53         [MPD_TAG_COMPOSER] = { "composer", N_("composer") },
54         [MPD_TAG_PERFORMER] = { "performer", N_("performer") },
55         [MPD_TAG_COMMENT] = { "comment", N_("comment") },
56 };
58 static int
59 search_get_tag_id(const char *name)
60 {
61         unsigned i;
63         if (g_ascii_strcasecmp(name, "file") == 0 ||
64             strcasecmp(name, _("file")) == 0)
65                 return SEARCH_URI;
67         for (i = 0; i < MPD_TAG_COUNT; ++i)
68                 if (search_tag[i].name != NULL &&
69                     (strcasecmp(search_tag[i].name, name) == 0 ||
70                      strcasecmp(search_tag[i].localname, name) == 0))
71                         return i;
73         return -1;
74 }
76 typedef struct {
77         enum mpd_tag_type table;
78         const char *label;
79 } search_type_t;
81 static search_type_t mode[] = {
82         { MPD_TAG_TITLE, N_("Title") },
83         { MPD_TAG_ARTIST, N_("Artist") },
84         { MPD_TAG_ALBUM, N_("Album") },
85         { SEARCH_URI, N_("Filename") },
86         { SEARCH_ARTIST_TITLE, N_("Artist + Title") },
87         { 0, NULL }
88 };
90 static GList *search_history = NULL;
91 static gchar *pattern = NULL;
92 static gboolean advanced_search_mode = FALSE;
94 static struct screen_browser browser;
96 static const char *const help_text[] = {
97         "Quick     -  Enter a string and ncmpc will search according",
98         "             to the current search mode (displayed above).",
99         "",
100         "Advanced  -  <tag>:<search term> [<tag>:<search term>...]",
101         "               Example: artist:radiohead album:pablo honey",
102         "",
103         "               Available tags: artist, album, title, track,",
104         "               name, genre, date composer, performer, comment, file",
105         "",
106 };
108 /* search info */
109 static const char *
110 lw_search_help_callback(unsigned idx, G_GNUC_UNUSED void *data)
112         assert(idx < G_N_ELEMENTS(help_text));
114         return help_text[idx];
117 static void
118 screen_search_paint(void);
120 static void
121 search_repaint(void)
123         screen_search_paint();
124         wrefresh(browser.lw->w);
127 /* sanity check search mode value */
128 static void
129 search_check_mode(void)
131         int max = 0;
133         while (mode[max].label != NULL)
134                 max++;
135         if (options.search_mode < 0)
136                 options.search_mode = 0;
137         else if (options.search_mode >= max)
138                 options.search_mode = max-1;
141 static void
142 search_clear(bool clear_pattern)
144         if (browser.filelist) {
145                 filelist_free(browser.filelist);
146                 browser.filelist = filelist_new();
147                 list_window_set_length(browser.lw, 0);
148         }
149         if (clear_pattern && pattern) {
150                 g_free(pattern);
151                 pattern = NULL;
152         }
155 static struct filelist *
156 search_simple_query(struct mpd_connection *connection, bool exact_match,
157                     int table, gchar *local_pattern)
159         struct filelist *list;
160         gchar *filter_utf8 = locale_to_utf8(local_pattern);
162         if (table == SEARCH_ARTIST_TITLE) {
163                 mpd_command_list_begin(connection, false);
165                 mpd_search_db_songs(connection, exact_match);
166                 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
167                                               MPD_TAG_ARTIST, filter_utf8);
168                 mpd_search_commit(connection);
170                 mpd_search_db_songs(connection, exact_match);
171                 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
172                                               MPD_TAG_TITLE, filter_utf8);
173                 mpd_search_commit(connection);
175                 mpd_command_list_end(connection);
177                 list = filelist_new_recv(connection);
178                 filelist_no_duplicates(list);
179         } else if (table == SEARCH_URI) {
180                 mpd_search_db_songs(connection, exact_match);
181                 mpd_search_add_uri_constraint(connection, MPD_OPERATOR_DEFAULT,
182                                               filter_utf8);
183                 mpd_search_commit(connection);
185                 list = filelist_new_recv(connection);
186         } else {
187                 mpd_search_db_songs(connection, exact_match);
188                 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
189                                               table, filter_utf8);
190                 mpd_search_commit(connection);
192                 list = filelist_new_recv(connection);
193         }
195         g_free(filter_utf8);
196         return list;
199 /*-----------------------------------------------------------------------
200  * NOTE: This code exists to test a new search ui,
201  *       Its ugly and MUST be redesigned before the next release!
202  *-----------------------------------------------------------------------
203  */
204 static struct filelist *
205 search_advanced_query(struct mpd_connection *connection, char *query)
207         int i,j;
208         char **strv;
209         int table[10];
210         char *arg[10];
211         struct filelist *fl = NULL;
213         advanced_search_mode = FALSE;
214         if (strchr(query, ':') == NULL)
215                 return NULL;
217         strv = g_strsplit_set(query, ": ", 0);
219         memset(table, 0, 10*sizeof(int));
220         memset(arg, 0, 10*sizeof(char *));
222         i=0;
223         j=0;
224         while (strv[i] && strlen(strv[i]) > 0 && i < 9) {
225                 int id = search_get_tag_id(strv[i]);
226                 if (id == -1) {
227                         if (table[j]) {
228                                 char *tmp = arg[j];
229                                 arg[j] = g_strdup_printf("%s %s", arg[j], strv[i]);
230                                 g_free(tmp);
231                         } else {
232                                 screen_status_printf(_("Bad search tag %s"), strv[i]);
233                         }
234                         i++;
235                 } else if (strv[i+1] == NULL || strlen(strv[i+1]) == 0) {
236                         screen_status_printf(_("No argument for search tag %s"), strv[i]);
237                         i++;
238                         //        j--;
239                         //table[j] = -1;
240                 } else {
241                         table[j] = id;
242                         arg[j] = locale_to_utf8(strv[i+1]); // FREE ME
243                         j++;
244                         table[j] = -1;
245                         arg[j] = NULL;
246                         i = i + 2;
247                         advanced_search_mode = TRUE;
248                 }
249         }
251         g_strfreev(strv);
253         if (!advanced_search_mode || j == 0) {
254                 for (i = 0; arg[i] != NULL; ++i)
255                         g_free(arg[i]);
256                 return NULL;
257         }
259         /*-----------------------------------------------------------------------
260          * NOTE (again): This code exists to test a new search ui,
261          *               Its ugly and MUST be redesigned before the next release!
262          *             + the code below should live in mpdclient.c
263          *-----------------------------------------------------------------------
264          */
265         /** stupid - but this is just a test...... (fulhack)  */
266         mpd_search_db_songs(connection, false);
268         for (i = 0; i < 10 && arg[i] != NULL; i++) {
269                 if (table[i] == SEARCH_URI)
270                         mpd_search_add_uri_constraint(connection,
271                                                       MPD_OPERATOR_DEFAULT,
272                                                       arg[i]);
273                 else
274                         mpd_search_add_tag_constraint(connection,
275                                                       MPD_OPERATOR_DEFAULT,
276                                                       table[i], arg[i]);
277         }
279         mpd_search_commit(connection);
280         fl = filelist_new_recv(connection);
281         if (!mpd_response_finish(connection)) {
282                 filelist_free(fl);
283                 fl = NULL;
284         }
286         for (i = 0; arg[i] != NULL; ++i)
287                 g_free(arg[i]);
289         return fl;
292 static struct filelist *
293 do_search(struct mpdclient *c, char *query)
295         struct mpd_connection *connection = mpdclient_get_connection(c);
296         if (connection == NULL)
297                 return NULL;
299         struct filelist *fl = search_advanced_query(connection, query);
300         if (fl != NULL)
301                 return fl;
303         if (mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS) {
304                 mpdclient_handle_error(c);
305                 return NULL;
306         }
308         fl = search_simple_query(connection, FALSE,
309                                  mode[options.search_mode].table,
310                                  query);
311         if (fl == NULL)
312                 mpdclient_handle_error(c);
313         return fl;
316 static void
317 screen_search_reload(struct mpdclient *c)
319         if (pattern == NULL)
320                 return;
322         if (browser.filelist != NULL) {
323                 filelist_free(browser.filelist);
324                 browser.filelist = NULL;
325         }
327         browser.filelist = do_search(c, pattern);
328         if (browser.filelist == NULL)
329                 browser.filelist = filelist_new();
330         list_window_set_length(browser.lw, filelist_length(browser.filelist));
332         screen_browser_sync_highlights(browser.filelist, &c->playlist);
335 static void
336 search_new(struct mpdclient *c)
338         if (!mpdclient_is_connected(c))
339                 return;
341         search_clear(true);
343         g_free(pattern);
344         pattern = screen_readln(_("Search"),
345                                 NULL,
346                                 &search_history,
347                                 NULL);
349         if (pattern == NULL) {
350                 list_window_reset(browser.lw);
351                 return;
352         }
354         screen_search_reload(c);
357 static void
358 screen_search_init(WINDOW *w, int cols, int rows)
360         browser.lw = list_window_init(w, cols, rows);
361         list_window_set_length(browser.lw, G_N_ELEMENTS(help_text));
364 static void
365 screen_search_quit(void)
367         if (search_history)
368                 string_list_free(search_history);
369         if (browser.filelist)
370                 filelist_free(browser.filelist);
371         list_window_free(browser.lw);
373         if (pattern) {
374                 g_free(pattern);
375                 pattern = NULL;
376         }
379 static void
380 screen_search_open(G_GNUC_UNUSED struct mpdclient *c)
382         //  if( pattern==NULL )
383         //    search_new(screen, c);
384         // else
385         screen_status_printf(_("Press %s for a new search"),
386                              get_key_names(CMD_SCREEN_SEARCH, false));
387         search_check_mode();
390 static void
391 screen_search_resize(int cols, int rows)
393         list_window_resize(browser.lw, cols, rows);
396 static void
397 screen_search_paint(void)
399         if (browser.filelist) {
400                 browser.lw->hide_cursor = false;
401                 screen_browser_paint(&browser);
402         } else {
403                 browser.lw->hide_cursor = true;
404                 list_window_paint(browser.lw, lw_search_help_callback, NULL);
405         }
408 static const char *
409 screen_search_get_title(char *str, size_t size)
411         if (advanced_search_mode && pattern)
412                 g_snprintf(str, size, _("Search: %s"), pattern);
413         else if (pattern)
414                 g_snprintf(str, size,
415                            _("Search: Results for %s [%s]"),
416                            pattern,
417                            _(mode[options.search_mode].label));
418         else
419                 g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
420                            get_key_names(CMD_SCREEN_SEARCH, false),
421                            _(mode[options.search_mode].label));
423         return str;
426 static void
427 screen_search_update(struct mpdclient *c)
429         if (browser.filelist != NULL && c->events & MPD_IDLE_QUEUE) {
430                 screen_browser_sync_highlights(browser.filelist, &c->playlist);
431                 search_repaint();
432         }
435 static bool
436 screen_search_cmd(struct mpdclient *c, command_t cmd)
438         switch (cmd) {
439         case CMD_SEARCH_MODE:
440                 options.search_mode++;
441                 if (mode[options.search_mode].label == NULL)
442                         options.search_mode = 0;
443                 screen_status_printf(_("Search mode: %s"),
444                                      _(mode[options.search_mode].label));
445                 /* continue and update... */
446         case CMD_SCREEN_UPDATE:
447                 screen_search_reload(c);
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(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 = screen_search_init,
478         .exit = screen_search_quit,
479         .open = screen_search_open,
480         .resize = screen_search_resize,
481         .paint = screen_search_paint,
482         .update = screen_search_update,
483         .cmd = screen_search_cmd,
484         .get_title = screen_search_get_title,
485 };