Code

screen_{file,artist,search}: repaint only if screen is visible
[ncmpc.git] / src / screen_search.c
1 /*
2  * (c) 2004 by Kalle Wallin <kaw@linux.se>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  *
17  */
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"
29 #include "gcc.h"
31 #include <ctype.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <glib.h>
35 #include <ncurses.h>
37 static const struct {
38         const char *name;
39         const char *localname;
40 } search_tag[MPD_TAG_NUM_OF_ITEM_TYPES] = {
41         [MPD_TAG_ITEM_ARTIST] = { "artist", N_("artist") },
42         [MPD_TAG_ITEM_ALBUM] = { "album", N_("album") },
43         [MPD_TAG_ITEM_TITLE] = { "title", N_("title") },
44         [MPD_TAG_ITEM_TRACK] = { "track", N_("track") },
45         [MPD_TAG_ITEM_NAME] = { "name", N_("name") },
46         [MPD_TAG_ITEM_GENRE] = { "genre", N_("genre") },
47         [MPD_TAG_ITEM_DATE] = { "date", N_("date") },
48         [MPD_TAG_ITEM_COMPOSER] = { "composer", N_("composer") },
49         [MPD_TAG_ITEM_PERFORMER] = { "performer", N_("performer") },
50         [MPD_TAG_ITEM_COMMENT] = { "comment", N_("comment") },
51         [MPD_TAG_ITEM_FILENAME] = { "filename", N_("file") },
52 };
54 static int
55 search_get_tag_id(char *name)
56 {
57         unsigned i;
59         for (i = 0; i < MPD_TAG_NUM_OF_ITEM_TYPES; ++i)
60                 if (search_tag[i].name != NULL &&
61                     (strcasecmp(search_tag[i].name, name) == 0 ||
62                      strcasecmp(search_tag[i].localname, name) == 0))
63                         return i;
65         return -1;
66 }
68 #define SEARCH_TITLE    0
69 #define SEARCH_ARTIST   1
70 #define SEARCH_ALBUM    2
71 #define SEARCH_FILE     3
73 #define SEARCH_ARTIST_TITLE 999
75 typedef struct {
76         int table;
77         const char *label;
78 } search_type_t;
80 static search_type_t mode[] = {
81         { MPD_TABLE_TITLE, N_("Title") },
82         { MPD_TABLE_ARTIST, N_("Artist") },
83         { MPD_TABLE_ALBUM, N_("Album") },
84         { MPD_TABLE_FILENAME, N_("Filename") },
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, mpd_unused int *highlight,
99                         mpd_unused void *data)
101         unsigned text_rows;
102         static const char *text[] = {
103                 "Quick  - just 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                 "                  avalible 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 have 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(NULL);
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, mpd_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_TABLE_ARTIST,
187                                                  filter_utf8);
188                 if (list == NULL)
189                         list = filelist_new(NULL);
191                 list2 = mpdclient_filelist_search(c, FALSE, MPD_TABLE_TITLE,
192                                                   filter_utf8);
193                 if (list2 != NULL) {
194                         filelist_move(list, list2);
195                         filelist_free(list2);
196                 }
198                 filelist_sort(list, compare_filelistentry_format);
199         } else {
200                 list = mpdclient_filelist_search(c, FALSE, table, filter_utf8);
201                 if (list == NULL)
202                         list = filelist_new(NULL);
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                 mpd_InfoEntity *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_startSearch(c->connection, FALSE);
282                 for(iter = 0; iter < 10; iter++) {
283                         mpd_addConstraintSearch(c->connection, table[iter], arg[iter]);
284                 }
286                 mpd_commitSearch(c->connection);
288                 fl = g_malloc0(sizeof(mpdclient_filelist_t));
290                 while ((entity=mpd_getNextInfoEntity(c->connection)))
291                         filelist_append(fl, entity);
293                 if (mpdclient_finish_command(c) && fl)
294                         filelist_free(fl);
295         }
297         i=0;
298         while( arg[i] )
299                 g_free(arg[i++]);
301         return fl;
304 static void
305 search_new(mpdclient_t *c)
307         search_clear(c, TRUE);
309         g_free(pattern);
310         pattern = screen_readln(screen.status_window.w,
311                                 _("Search: "),
312                                 NULL,
313                                 &search_history,
314                                 NULL);
316         if (pattern == NULL) {
317                 list_window_reset(browser.lw);
318                 return;
319         }
321         if (browser.filelist != NULL) {
322                 filelist_free(browser.filelist);
323                 browser.filelist = NULL;
324         }
326         if (!MPD_VERSION_LT(c, 0, 12, 0))
327                 browser.filelist = search_advanced_query(pattern, c);
329         if (!advanced_search_mode && browser.filelist == NULL)
330                 browser.filelist = filelist_search(c, FALSE,
331                                                   mode[options.search_mode].table,
332                                                   pattern);
334         if (browser.filelist == NULL)
335                 browser.filelist = filelist_new(NULL);
337         sync_highlights(c, browser.filelist);
338         mpdclient_install_playlist_callback(c, playlist_changed_callback);
339         list_window_check_selected(browser.lw, filelist_length(browser.filelist));
344 static void
345 init(WINDOW *w, int cols, int rows)
347         browser.lw = list_window_init(w, cols, rows);
350 static void
351 quit(void)
353         if (search_history)
354                 string_list_free(search_history);
355         if (browser.filelist)
356                 filelist_free(browser.filelist);
357         list_window_free(browser.lw);
359         if (pattern) {
360                 g_free(pattern);
361                 pattern = NULL;
362         }
365 static void
366 open(mpd_unused mpdclient_t *c)
368         //  if( pattern==NULL )
369         //    search_new(screen, c);
370         // else
371         screen_status_printf(_("Press %s for a new search"),
372                              get_key_names(CMD_SCREEN_SEARCH,0));
373         search_check_mode();
376 static void
377 resize(int cols, int rows)
379         browser.lw->cols = cols;
380         browser.lw->rows = rows;
383 static void
384 paint(void)
386         if (browser.filelist) {
387                 browser.lw->flags = 0;
388                 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
389         } else {
390                 browser.lw->flags = LW_HIDE_CURSOR;
391                 list_window_paint(browser.lw, lw_search_help_callback, NULL);
392         }
395 static const char *
396 get_title(char *str, size_t size)
398         if (advanced_search_mode && pattern)
399                 g_snprintf(str, size, _("Search: %s"), pattern);
400         else if (pattern)
401                 g_snprintf(str, size,
402                            _("Search: Results for %s [%s]"),
403                            pattern,
404                            _(mode[options.search_mode].label));
405         else
406                 g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
407                            get_key_names(CMD_SCREEN_SEARCH,0),
408                            _(mode[options.search_mode].label));
410         return str;
413 static int
414 search_cmd(mpdclient_t *c, command_t cmd)
416         switch (cmd) {
417         case CMD_SEARCH_MODE:
418                 options.search_mode++;
419                 if (mode[options.search_mode].label == NULL)
420                         options.search_mode = 0;
421                 screen_status_printf(_("Search mode: %s"),
422                                      _(mode[options.search_mode].label));
423                 /* continue and update... */
424         case CMD_SCREEN_UPDATE:
425                 if (pattern) {
426                         search_clear(c, FALSE);
427                         browser.filelist = filelist_search(c,
428                                                           FALSE,
429                                                           mode[options.search_mode].table,
430                                                           pattern);
431                         sync_highlights(c, browser.filelist);
432                 }
433                 search_repaint();
434                 return 1;
436         case CMD_SCREEN_SEARCH:
437                 search_new(c);
438                 search_repaint();
439                 return 1;
441         case CMD_CLEAR:
442                 search_clear(c, TRUE);
443                 list_window_reset(browser.lw);
444                 search_repaint();
445                 return 1;
447         default:
448                 break;
449         }
451         if (browser.filelist != NULL &&
452             browser_cmd(&browser, c, cmd)) {
453                 if (screen_is_visible(&screen_search))
454                         search_repaint();
455                 return 1;
456         }
458         return 0;
461 const struct screen_functions screen_search = {
462         .init = init,
463         .exit = quit,
464         .open = open,
465         .resize = resize,
466         .paint = paint,
467         .cmd = search_cmd,
468         .get_title = get_title,
469 };