Code

screen_search: removed update() method
[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 "config.h"
21 #ifndef DISABLE_SEARCH_SCREEN
22 #include "ncmpc.h"
23 #include "options.h"
24 #include "support.h"
25 #include "mpdclient.h"
26 #include "strfsong.h"
27 #include "command.h"
28 #include "screen.h"
29 #include "utils.h"
30 #include "screen_utils.h"
31 #include "screen_browser.h"
32 #include "gcc.h"
34 #include <ctype.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <glib.h>
38 #include <ncurses.h>
40 /* new search stuff with qball's libmpdclient */
41 #define FUTURE
43 #ifdef FUTURE
45 extern gint mpdclient_finish_command(mpdclient_t *c);
47 typedef struct {
48         int id;
49         const char *name;
50         const char *localname;
51 } search_tag_t;
53 static search_tag_t search_tag[] = {
54         { MPD_TAG_ITEM_ARTIST, "artist", N_("artist") },
55         { MPD_TAG_ITEM_ALBUM, "album", N_("album") },
56         { MPD_TAG_ITEM_TITLE, "title", N_("title") },
57         { MPD_TAG_ITEM_TRACK, "track", N_("track") },
58         { MPD_TAG_ITEM_NAME, "name", N_("name") },
59         { MPD_TAG_ITEM_GENRE, "genre", N_("genre") },
60         { MPD_TAG_ITEM_DATE, "date", N_("date") },
61         { MPD_TAG_ITEM_COMPOSER, "composer", N_("composer") },
62         { MPD_TAG_ITEM_PERFORMER, "performer", N_("performer") },
63         { MPD_TAG_ITEM_COMMENT, "comment", N_("comment") },
64         { MPD_TAG_ITEM_FILENAME, "filename", N_("file") },
65         { -1, NULL, NULL }
66 };
68 static int
69 search_get_tag_id(char *name)
70 {
71         int i = 0;
73         while (search_tag[i].name) {
74                 if (strcasecmp(search_tag[i].name, name) == 0 ||
75                     strcasecmp(search_tag[i].localname, name) == 0)
76                         return search_tag[i].id;
77                 i++;
78         }
80         return -1;
81 }
83 #endif
85 #define SEARCH_TITLE    0
86 #define SEARCH_ARTIST   1
87 #define SEARCH_ALBUM    2
88 #define SEARCH_FILE     3
90 #define SEARCH_ARTIST_TITLE 999
92 typedef struct {
93         int table;
94         const char *label;
95 } search_type_t;
97 static search_type_t mode[] = {
98         { MPD_TABLE_TITLE, N_("Title") },
99         { MPD_TABLE_ARTIST, N_("Artist") },
100         { MPD_TABLE_ALBUM, N_("Album") },
101         { MPD_TABLE_FILENAME, N_("Filename") },
102         { SEARCH_ARTIST_TITLE, N_("Artist + Title") },
103         { 0, NULL }
104 };
106 static GList *search_history = NULL;
107 static gchar *pattern = NULL;
108 static gboolean advanced_search_mode = FALSE;
110 static struct screen_browser browser;
113 /* search info */
114 static const char *
115 lw_search_help_callback(unsigned idx, mpd_unused int *highlight,
116                         mpd_unused void *data)
118         unsigned text_rows;
119         static const char *text[] = {
120                 "Quick  - just enter a string and ncmpc will search according",
121                 "               to the current search mode (displayed above).",
122                 "",
123                 "Advanced  -  <tag>:<search term> [<tag>:<search term>...]",
124                 "                       Example: artist:radiohead album:pablo honey",
125                 "",
126                 "                  avalible tags: artist, album, title, track,",
127                 "                  name, genre, date composer, performer, comment, file",
128                 "",
129                 NULL
130         };
132         text_rows=0;
133         while (text[text_rows])
134                 text_rows++;
136         if (idx < text_rows)
137                 return text[idx];
138         return NULL;
141 static void
142 paint(mpdclient_t *c);
144 static void
145 search_repaint(void)
147         paint(NULL);
148         wrefresh(browser.lw->w);
151 static void
152 search_repaint_if_active(void)
154         if (get_cur_mode_id() == 103) /* XXX don't use the literal number */
155                 search_repaint();
158 /* the playlist have been updated -> fix highlights */
159 static void
160 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
162         browser_playlist_changed(&browser, c, event, data);
163         search_repaint_if_active();
166 /* sanity check search mode value */
167 static void
168 search_check_mode(void)
170         int max = 0;
172         while (mode[max].label != NULL)
173                 max++;
174         if (options.search_mode < 0)
175                 options.search_mode = 0;
176         else if (options.search_mode >= max)
177                 options.search_mode = max-1;
180 static void
181 search_clear(mpd_unused screen_t *screen, mpdclient_t *c,
182              gboolean clear_pattern)
184         if (browser.filelist) {
185                 mpdclient_remove_playlist_callback(c, playlist_changed_callback);
186                 filelist_free(browser.filelist);
187                 browser.filelist = filelist_new(NULL);
188         }
189         if (clear_pattern && pattern) {
190                 g_free(pattern);
191                 pattern = NULL;
192         }
195 #ifdef FUTURE
196 static mpdclient_filelist_t *
197 filelist_search(mpdclient_t *c, mpd_unused int exact_match, int table,
198                 gchar *local_pattern)
200         mpdclient_filelist_t *list, *list2;
202         if (table == SEARCH_ARTIST_TITLE) {
203                 list = mpdclient_filelist_search(c, FALSE, MPD_TABLE_ARTIST,
204                                                  local_pattern);
205                 if (list == NULL)
206                         list = filelist_new(NULL);
208                 list2 = mpdclient_filelist_search(c, FALSE, MPD_TABLE_TITLE,
209                                                   local_pattern);
210                 if (list2 != NULL) {
211                         filelist_move(list, list2);
212                         filelist_free(list2);
213                 }
215                 filelist_sort(list, compare_filelistentry_format);
216                 list->updated = TRUE;
217         } else {
218                 list = mpdclient_filelist_search(c, FALSE, table, local_pattern);
219                 if (list == NULL)
220                         list = filelist_new(NULL);
221         }
223         return list;
226 /*-----------------------------------------------------------------------
227  * NOTE: This code exists to test a new search ui,
228  *       Its ugly and MUST be redesigned before the next release!
229  *-----------------------------------------------------------------------
230  */
231 static mpdclient_filelist_t *
232 search_advanced_query(char *query, mpdclient_t *c)
234         int i,j;
235         char **strv;
236         int table[10];
237         char *arg[10];
238         mpdclient_filelist_t *fl = NULL;
240         advanced_search_mode = FALSE;
241         if( g_strrstr(query, ":") == NULL )
242                 return NULL;
244         strv = g_strsplit_set(query, ": ", 0);
246         i=0;
247         while (strv[i]) {
248                 D("strv[%d] = \"%s\"\n", i, strv[i]);
249                 i++;
250         }
252         memset(table, 0, 10*sizeof(int));
253         memset(arg, 0, 10*sizeof(char *));
255         i=0;
256         j=0;
257         while (strv[i] && strlen(strv[i]) > 0 && i < 9) {
258                 int id = search_get_tag_id(strv[i]);
259                 if (id == -1) {
260                         if (table[j]) {
261                                 char *tmp = arg[j];
262                                 arg[j] = g_strdup_printf("%s %s", arg[j], strv[i]);
263                                 g_free(tmp);
264                         } else {
265                                 D("Bad search tag %s\n", strv[i]);
266                                 screen_status_printf(_("Bad search tag %s"), strv[i]);
267                         }
268                         i++;
269                 } else if (strv[i+1] == NULL || strlen(strv[i+1]) == 0) {
270                         D("No argument for search tag %s\n", strv[i]);
271                         screen_status_printf(_("No argument for search tag %s"), strv[i]);
272                         i++;
273                         //        j--;
274                         //table[j] = -1;
275                 } else {
276                         table[j] = id;
277                         arg[j] = locale_to_utf8(strv[i+1]); // FREE ME
278                         j++;
279                         table[j] = -1;
280                         arg[j] = NULL;
281                         i = i + 2;
282                         advanced_search_mode = TRUE;
283                 }
284         }
286         g_strfreev(strv);
289         if (advanced_search_mode && j > 0) {
290                 int iter;
291                 mpd_InfoEntity *entity;
293                 /*-----------------------------------------------------------------------
294                  * NOTE (again): This code exists to test a new search ui,
295                  *               Its ugly and MUST be redesigned before the next release!
296                  *             + the code below should live in mpdclient.c
297                  *-----------------------------------------------------------------------
298                  */
299                 /** stupid - but this is just a test...... (fulhack)  */
300                 mpd_startSearch(c->connection, FALSE);
302                 for(iter = 0; iter < 10; iter++) {
303                         mpd_addConstraintSearch(c->connection, table[iter], arg[iter]);
304                 }
306                 mpd_commitSearch(c->connection);
308                 fl = g_malloc0(sizeof(mpdclient_filelist_t));
310                 while ((entity=mpd_getNextInfoEntity(c->connection)))
311                         filelist_append(fl, entity);
313                 if (mpdclient_finish_command(c) && fl)
314                         filelist_free(fl);
316                 fl->updated = TRUE;
317         }
319         i=0;
320         while( arg[i] )
321                 g_free(arg[i++]);
323         return fl;
325 #else
326 #define search_advanced_query(pattern,c) (NULL)
327 #endif
329 static void
330 search_new(screen_t *screen, mpdclient_t *c)
332         search_clear(screen, c, TRUE);
334         pattern = screen_readln(screen->status_window.w,
335                                 _("Search: "),
336                                 NULL,
337                                 &search_history,
338                                 NULL);
340         if (pattern && strcmp(pattern,"") == 0) {
341                 g_free(pattern);
342                 pattern=NULL;
343         }
345         if (pattern == NULL) {
346                 list_window_reset(browser.lw);
347                 return;
348         }
350         if (!MPD_VERSION_LT(c, 0, 12, 0))
351                 browser.filelist = search_advanced_query(pattern, c);
353         if (!advanced_search_mode && browser.filelist == NULL)
354                 browser.filelist = filelist_search(c, FALSE,
355                                                   mode[options.search_mode].table,
356                                                   pattern);
358         if (browser.filelist == NULL)
359                 browser.filelist = filelist_new(NULL);
361         sync_highlights(c, browser.filelist);
362         mpdclient_install_playlist_callback(c, playlist_changed_callback);
363         list_window_check_selected(browser.lw, filelist_length(browser.filelist));
368 static void
369 init(WINDOW *w, int cols, int rows)
371         browser.lw = list_window_init(w, cols, rows);
374 static void
375 quit(void)
377         if (search_history)
378                 string_list_free(search_history);
379         if (browser.filelist)
380                 filelist_free(browser.filelist);
381         list_window_free(browser.lw);
383         if (pattern) {
384                 g_free(pattern);
385                 pattern = NULL;
386         }
389 static void
390 open(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
392         //  if( pattern==NULL )
393         //    search_new(screen, c);
394         // else
395         screen_status_printf(_("Press %s for a new search"),
396                              get_key_names(CMD_SCREEN_SEARCH,0));
397         search_check_mode();
400 static void
401 resize(int cols, int rows)
403         browser.lw->cols = cols;
404         browser.lw->rows = rows;
407 static void
408 paint(mpd_unused mpdclient_t *c)
410         if (browser.filelist) {
411                 browser.lw->flags = 0;
412                 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
413                 browser.filelist->updated = FALSE;
414         } else {
415                 browser.lw->flags = LW_HIDE_CURSOR;
416                 list_window_paint(browser.lw, lw_search_help_callback, NULL);
417         }
420 static const char *
421 get_title(char *str, size_t size)
423         if (advanced_search_mode && pattern)
424                 g_snprintf(str, size, _("Search: %s"), pattern);
425         else if (pattern)
426                 g_snprintf(str, size,
427                            _("Search: Results for %s [%s]"),
428                            pattern,
429                            _(mode[options.search_mode].label));
430         else
431                 g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
432                            get_key_names(CMD_SCREEN_SEARCH,0),
433                            _(mode[options.search_mode].label));
435         return str;
438 static int
439 search_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
441         switch (cmd) {
442         case CMD_PLAY:
443                 browser_handle_enter(&browser, c);
444                 search_repaint();
445                 return 1;
447         case CMD_SELECT:
448                 if (browser_handle_select(&browser, c) == 0) {
449                         /* continue and select next item... */
450                         cmd = CMD_LIST_NEXT;
451                 }
452                 /* call list_window_cmd to go to the next item */
453                 break;
455         case CMD_SELECT_ALL:
456                 browser_handle_select_all(&browser, c);
457                 search_repaint();
458                 return 0;
460         case CMD_SEARCH_MODE:
461                 options.search_mode++;
462                 if (mode[options.search_mode].label == NULL)
463                         options.search_mode = 0;
464                 screen_status_printf(_("Search mode: %s"),
465                                      _(mode[options.search_mode].label));
466                 /* continue and update... */
467         case CMD_SCREEN_UPDATE:
468                 if (pattern) {
469                         search_clear(screen, c, FALSE);
470                         browser.filelist = filelist_search(c,
471                                                           FALSE,
472                                                           mode[options.search_mode].table,
473                                                           pattern);
474                         sync_highlights(c, browser.filelist);
475                 }
476                 search_repaint();
477                 return 1;
479         case CMD_SCREEN_SEARCH:
480                 search_new(screen, c);
481                 search_repaint();
482                 return 1;
484         case CMD_CLEAR:
485                 search_clear(screen, c, TRUE);
486                 list_window_reset(browser.lw);
487                 search_repaint();
488                 return 1;
490         case CMD_LIST_FIND:
491         case CMD_LIST_RFIND:
492         case CMD_LIST_FIND_NEXT:
493         case CMD_LIST_RFIND_NEXT:
494                 if (browser.filelist) {
495                         screen_find(screen,
496                                     browser.lw, filelist_length(browser.filelist),
497                                     cmd, browser_lw_callback,
498                                     browser.filelist);
499                         search_repaint();
500                 }
502                 return 1;
504         case CMD_MOUSE_EVENT:
505                 browser_handle_mouse_event(&browser, c);
506                 search_repaint();
507                 return 1;
509         default:
510                 break;
511         }
513         if (browser.filelist != NULL &&
514             list_window_cmd(browser.lw,
515                             filelist_length(browser.filelist), cmd)) {
516                 search_repaint();
517                 return 1;
518         }
520         return 0;
523 const struct screen_functions screen_search = {
524         .init = init,
525         .exit = quit,
526         .open = open,
527         .resize = resize,
528         .paint = paint,
529         .cmd = search_cmd,
530         .get_title = get_title,
531 };
533 #endif /* ENABLE_SEARCH_SCREEN */