Code

screen_search: removed the FUTURE macro
[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 extern gint mpdclient_finish_command(mpdclient_t *c);
39 typedef struct {
40         int id;
41         const char *name;
42         const char *localname;
43 } search_tag_t;
45 static search_tag_t search_tag[] = {
46         { MPD_TAG_ITEM_ARTIST, "artist", N_("artist") },
47         { MPD_TAG_ITEM_ALBUM, "album", N_("album") },
48         { MPD_TAG_ITEM_TITLE, "title", N_("title") },
49         { MPD_TAG_ITEM_TRACK, "track", N_("track") },
50         { MPD_TAG_ITEM_NAME, "name", N_("name") },
51         { MPD_TAG_ITEM_GENRE, "genre", N_("genre") },
52         { MPD_TAG_ITEM_DATE, "date", N_("date") },
53         { MPD_TAG_ITEM_COMPOSER, "composer", N_("composer") },
54         { MPD_TAG_ITEM_PERFORMER, "performer", N_("performer") },
55         { MPD_TAG_ITEM_COMMENT, "comment", N_("comment") },
56         { MPD_TAG_ITEM_FILENAME, "filename", N_("file") },
57         { -1, NULL, NULL }
58 };
60 static int
61 search_get_tag_id(char *name)
62 {
63         int i = 0;
65         while (search_tag[i].name) {
66                 if (strcasecmp(search_tag[i].name, name) == 0 ||
67                     strcasecmp(search_tag[i].localname, name) == 0)
68                         return search_tag[i].id;
69                 i++;
70         }
72         return -1;
73 }
75 #define SEARCH_TITLE    0
76 #define SEARCH_ARTIST   1
77 #define SEARCH_ALBUM    2
78 #define SEARCH_FILE     3
80 #define SEARCH_ARTIST_TITLE 999
82 typedef struct {
83         int table;
84         const char *label;
85 } search_type_t;
87 static search_type_t mode[] = {
88         { MPD_TABLE_TITLE, N_("Title") },
89         { MPD_TABLE_ARTIST, N_("Artist") },
90         { MPD_TABLE_ALBUM, N_("Album") },
91         { MPD_TABLE_FILENAME, N_("Filename") },
92         { SEARCH_ARTIST_TITLE, N_("Artist + Title") },
93         { 0, NULL }
94 };
96 static GList *search_history = NULL;
97 static gchar *pattern = NULL;
98 static gboolean advanced_search_mode = FALSE;
100 static struct screen_browser browser;
103 /* search info */
104 static const char *
105 lw_search_help_callback(unsigned idx, mpd_unused int *highlight,
106                         mpd_unused void *data)
108         unsigned text_rows;
109         static const char *text[] = {
110                 "Quick  - just enter a string and ncmpc will search according",
111                 "               to the current search mode (displayed above).",
112                 "",
113                 "Advanced  -  <tag>:<search term> [<tag>:<search term>...]",
114                 "                       Example: artist:radiohead album:pablo honey",
115                 "",
116                 "                  avalible tags: artist, album, title, track,",
117                 "                  name, genre, date composer, performer, comment, file",
118                 "",
119                 NULL
120         };
122         text_rows=0;
123         while (text[text_rows])
124                 text_rows++;
126         if (idx < text_rows)
127                 return text[idx];
128         return NULL;
131 static void
132 paint(mpdclient_t *c);
134 static void
135 search_repaint(void)
137         paint(NULL);
138         wrefresh(browser.lw->w);
141 static void
142 search_repaint_if_active(void)
144         if (screen_is_visible(&screen_search))
145                 search_repaint();
148 /* the playlist have been updated -> fix highlights */
149 static void
150 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
152         browser_playlist_changed(&browser, c, event, data);
153         search_repaint_if_active();
156 /* sanity check search mode value */
157 static void
158 search_check_mode(void)
160         int max = 0;
162         while (mode[max].label != NULL)
163                 max++;
164         if (options.search_mode < 0)
165                 options.search_mode = 0;
166         else if (options.search_mode >= max)
167                 options.search_mode = max-1;
170 static void
171 search_clear(mpd_unused screen_t *screen, mpdclient_t *c,
172              gboolean clear_pattern)
174         if (browser.filelist) {
175                 mpdclient_remove_playlist_callback(c, playlist_changed_callback);
176                 filelist_free(browser.filelist);
177                 browser.filelist = filelist_new(NULL);
178         }
179         if (clear_pattern && pattern) {
180                 g_free(pattern);
181                 pattern = NULL;
182         }
185 static mpdclient_filelist_t *
186 filelist_search(mpdclient_t *c, mpd_unused int exact_match, int table,
187                 gchar *local_pattern)
189         mpdclient_filelist_t *list, *list2;
191         if (table == SEARCH_ARTIST_TITLE) {
192                 list = mpdclient_filelist_search(c, FALSE, MPD_TABLE_ARTIST,
193                                                  local_pattern);
194                 if (list == NULL)
195                         list = filelist_new(NULL);
197                 list2 = mpdclient_filelist_search(c, FALSE, MPD_TABLE_TITLE,
198                                                   local_pattern);
199                 if (list2 != NULL) {
200                         filelist_move(list, list2);
201                         filelist_free(list2);
202                 }
204                 filelist_sort(list, compare_filelistentry_format);
205         } else {
206                 list = mpdclient_filelist_search(c, FALSE, table, local_pattern);
207                 if (list == NULL)
208                         list = filelist_new(NULL);
209         }
211         return list;
214 /*-----------------------------------------------------------------------
215  * NOTE: This code exists to test a new search ui,
216  *       Its ugly and MUST be redesigned before the next release!
217  *-----------------------------------------------------------------------
218  */
219 static mpdclient_filelist_t *
220 search_advanced_query(char *query, mpdclient_t *c)
222         int i,j;
223         char **strv;
224         int table[10];
225         char *arg[10];
226         mpdclient_filelist_t *fl = NULL;
228         advanced_search_mode = FALSE;
229         if( g_strrstr(query, ":") == NULL )
230                 return NULL;
232         strv = g_strsplit_set(query, ": ", 0);
234         i=0;
235         while (strv[i]) {
236                 i++;
237         }
239         memset(table, 0, 10*sizeof(int));
240         memset(arg, 0, 10*sizeof(char *));
242         i=0;
243         j=0;
244         while (strv[i] && strlen(strv[i]) > 0 && i < 9) {
245                 int id = search_get_tag_id(strv[i]);
246                 if (id == -1) {
247                         if (table[j]) {
248                                 char *tmp = arg[j];
249                                 arg[j] = g_strdup_printf("%s %s", arg[j], strv[i]);
250                                 g_free(tmp);
251                         } else {
252                                 screen_status_printf(_("Bad search tag %s"), strv[i]);
253                         }
254                         i++;
255                 } else if (strv[i+1] == NULL || strlen(strv[i+1]) == 0) {
256                         screen_status_printf(_("No argument for search tag %s"), strv[i]);
257                         i++;
258                         //        j--;
259                         //table[j] = -1;
260                 } else {
261                         table[j] = id;
262                         arg[j] = locale_to_utf8(strv[i+1]); // FREE ME
263                         j++;
264                         table[j] = -1;
265                         arg[j] = NULL;
266                         i = i + 2;
267                         advanced_search_mode = TRUE;
268                 }
269         }
271         g_strfreev(strv);
274         if (advanced_search_mode && j > 0) {
275                 int iter;
276                 mpd_InfoEntity *entity;
278                 /*-----------------------------------------------------------------------
279                  * NOTE (again): This code exists to test a new search ui,
280                  *               Its ugly and MUST be redesigned before the next release!
281                  *             + the code below should live in mpdclient.c
282                  *-----------------------------------------------------------------------
283                  */
284                 /** stupid - but this is just a test...... (fulhack)  */
285                 mpd_startSearch(c->connection, FALSE);
287                 for(iter = 0; iter < 10; iter++) {
288                         mpd_addConstraintSearch(c->connection, table[iter], arg[iter]);
289                 }
291                 mpd_commitSearch(c->connection);
293                 fl = g_malloc0(sizeof(mpdclient_filelist_t));
295                 while ((entity=mpd_getNextInfoEntity(c->connection)))
296                         filelist_append(fl, entity);
298                 if (mpdclient_finish_command(c) && fl)
299                         filelist_free(fl);
300         }
302         i=0;
303         while( arg[i] )
304                 g_free(arg[i++]);
306         return fl;
309 static void
310 search_new(screen_t *screen, mpdclient_t *c)
312         search_clear(screen, c, TRUE);
314         pattern = screen_readln(screen->status_window.w,
315                                 _("Search: "),
316                                 NULL,
317                                 &search_history,
318                                 NULL);
320         if (pattern && strcmp(pattern,"") == 0) {
321                 g_free(pattern);
322                 pattern=NULL;
323         }
325         if (pattern == NULL) {
326                 list_window_reset(browser.lw);
327                 return;
328         }
330         if (!MPD_VERSION_LT(c, 0, 12, 0))
331                 browser.filelist = search_advanced_query(pattern, c);
333         if (!advanced_search_mode && browser.filelist == NULL)
334                 browser.filelist = filelist_search(c, FALSE,
335                                                   mode[options.search_mode].table,
336                                                   pattern);
338         if (browser.filelist == NULL)
339                 browser.filelist = filelist_new(NULL);
341         sync_highlights(c, browser.filelist);
342         mpdclient_install_playlist_callback(c, playlist_changed_callback);
343         list_window_check_selected(browser.lw, filelist_length(browser.filelist));
348 static void
349 init(WINDOW *w, int cols, int rows)
351         browser.lw = list_window_init(w, cols, rows);
354 static void
355 quit(void)
357         if (search_history)
358                 string_list_free(search_history);
359         if (browser.filelist)
360                 filelist_free(browser.filelist);
361         list_window_free(browser.lw);
363         if (pattern) {
364                 g_free(pattern);
365                 pattern = NULL;
366         }
369 static void
370 open(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
372         //  if( pattern==NULL )
373         //    search_new(screen, c);
374         // else
375         screen_status_printf(_("Press %s for a new search"),
376                              get_key_names(CMD_SCREEN_SEARCH,0));
377         search_check_mode();
380 static void
381 resize(int cols, int rows)
383         browser.lw->cols = cols;
384         browser.lw->rows = rows;
387 static void
388 paint(mpd_unused mpdclient_t *c)
390         if (browser.filelist) {
391                 browser.lw->flags = 0;
392                 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
393         } else {
394                 browser.lw->flags = LW_HIDE_CURSOR;
395                 list_window_paint(browser.lw, lw_search_help_callback, NULL);
396         }
399 static const char *
400 get_title(char *str, size_t size)
402         if (advanced_search_mode && pattern)
403                 g_snprintf(str, size, _("Search: %s"), pattern);
404         else if (pattern)
405                 g_snprintf(str, size,
406                            _("Search: Results for %s [%s]"),
407                            pattern,
408                            _(mode[options.search_mode].label));
409         else
410                 g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
411                            get_key_names(CMD_SCREEN_SEARCH,0),
412                            _(mode[options.search_mode].label));
414         return str;
417 static int
418 search_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
420         switch (cmd) {
421         case CMD_SEARCH_MODE:
422                 options.search_mode++;
423                 if (mode[options.search_mode].label == NULL)
424                         options.search_mode = 0;
425                 screen_status_printf(_("Search mode: %s"),
426                                      _(mode[options.search_mode].label));
427                 /* continue and update... */
428         case CMD_SCREEN_UPDATE:
429                 if (pattern) {
430                         search_clear(screen, c, FALSE);
431                         browser.filelist = filelist_search(c,
432                                                           FALSE,
433                                                           mode[options.search_mode].table,
434                                                           pattern);
435                         sync_highlights(c, browser.filelist);
436                 }
437                 search_repaint();
438                 return 1;
440         case CMD_SCREEN_SEARCH:
441                 search_new(screen, c);
442                 search_repaint();
443                 return 1;
445         case CMD_CLEAR:
446                 search_clear(screen, c, TRUE);
447                 list_window_reset(browser.lw);
448                 search_repaint();
449                 return 1;
451         default:
452                 break;
453         }
455         if (browser.filelist != NULL &&
456             browser_cmd(&browser, screen, c, cmd)) {
457                 search_repaint();
458                 return 1;
459         }
461         return 0;
464 const struct screen_functions screen_search = {
465         .init = init,
466         .exit = quit,
467         .open = open,
468         .resize = resize,
469         .paint = paint,
470         .cmd = search_cmd,
471         .get_title = get_title,
472 };