Code

screen: replaced get_cur_mode_id() with 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 "ncmpc.h"
20 #include "options.h"
21 #include "support.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 /* new search stuff with qball's libmpdclient */
38 #define FUTURE
40 #ifdef FUTURE
42 extern gint mpdclient_finish_command(mpdclient_t *c);
44 typedef struct {
45         int id;
46         const char *name;
47         const char *localname;
48 } search_tag_t;
50 static search_tag_t search_tag[] = {
51         { MPD_TAG_ITEM_ARTIST, "artist", N_("artist") },
52         { MPD_TAG_ITEM_ALBUM, "album", N_("album") },
53         { MPD_TAG_ITEM_TITLE, "title", N_("title") },
54         { MPD_TAG_ITEM_TRACK, "track", N_("track") },
55         { MPD_TAG_ITEM_NAME, "name", N_("name") },
56         { MPD_TAG_ITEM_GENRE, "genre", N_("genre") },
57         { MPD_TAG_ITEM_DATE, "date", N_("date") },
58         { MPD_TAG_ITEM_COMPOSER, "composer", N_("composer") },
59         { MPD_TAG_ITEM_PERFORMER, "performer", N_("performer") },
60         { MPD_TAG_ITEM_COMMENT, "comment", N_("comment") },
61         { MPD_TAG_ITEM_FILENAME, "filename", N_("file") },
62         { -1, NULL, NULL }
63 };
65 static int
66 search_get_tag_id(char *name)
67 {
68         int i = 0;
70         while (search_tag[i].name) {
71                 if (strcasecmp(search_tag[i].name, name) == 0 ||
72                     strcasecmp(search_tag[i].localname, name) == 0)
73                         return search_tag[i].id;
74                 i++;
75         }
77         return -1;
78 }
80 #endif
82 #define SEARCH_TITLE    0
83 #define SEARCH_ARTIST   1
84 #define SEARCH_ALBUM    2
85 #define SEARCH_FILE     3
87 #define SEARCH_ARTIST_TITLE 999
89 typedef struct {
90         int table;
91         const char *label;
92 } search_type_t;
94 static search_type_t mode[] = {
95         { MPD_TABLE_TITLE, N_("Title") },
96         { MPD_TABLE_ARTIST, N_("Artist") },
97         { MPD_TABLE_ALBUM, N_("Album") },
98         { MPD_TABLE_FILENAME, N_("Filename") },
99         { SEARCH_ARTIST_TITLE, N_("Artist + Title") },
100         { 0, NULL }
101 };
103 static GList *search_history = NULL;
104 static gchar *pattern = NULL;
105 static gboolean advanced_search_mode = FALSE;
107 static struct screen_browser browser;
110 /* search info */
111 static const char *
112 lw_search_help_callback(unsigned idx, mpd_unused int *highlight,
113                         mpd_unused void *data)
115         unsigned text_rows;
116         static const char *text[] = {
117                 "Quick  - just enter a string and ncmpc will search according",
118                 "               to the current search mode (displayed above).",
119                 "",
120                 "Advanced  -  <tag>:<search term> [<tag>:<search term>...]",
121                 "                       Example: artist:radiohead album:pablo honey",
122                 "",
123                 "                  avalible tags: artist, album, title, track,",
124                 "                  name, genre, date composer, performer, comment, file",
125                 "",
126                 NULL
127         };
129         text_rows=0;
130         while (text[text_rows])
131                 text_rows++;
133         if (idx < text_rows)
134                 return text[idx];
135         return NULL;
138 static void
139 paint(mpdclient_t *c);
141 static void
142 search_repaint(void)
144         paint(NULL);
145         wrefresh(browser.lw->w);
148 static void
149 search_repaint_if_active(void)
151         if (screen_is_visible(&screen_search))
152                 search_repaint();
155 /* the playlist have been updated -> fix highlights */
156 static void
157 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
159         browser_playlist_changed(&browser, c, event, data);
160         search_repaint_if_active();
163 /* sanity check search mode value */
164 static void
165 search_check_mode(void)
167         int max = 0;
169         while (mode[max].label != NULL)
170                 max++;
171         if (options.search_mode < 0)
172                 options.search_mode = 0;
173         else if (options.search_mode >= max)
174                 options.search_mode = max-1;
177 static void
178 search_clear(mpd_unused screen_t *screen, mpdclient_t *c,
179              gboolean clear_pattern)
181         if (browser.filelist) {
182                 mpdclient_remove_playlist_callback(c, playlist_changed_callback);
183                 filelist_free(browser.filelist);
184                 browser.filelist = filelist_new(NULL);
185         }
186         if (clear_pattern && pattern) {
187                 g_free(pattern);
188                 pattern = NULL;
189         }
192 #ifdef FUTURE
193 static mpdclient_filelist_t *
194 filelist_search(mpdclient_t *c, mpd_unused int exact_match, int table,
195                 gchar *local_pattern)
197         mpdclient_filelist_t *list, *list2;
199         if (table == SEARCH_ARTIST_TITLE) {
200                 list = mpdclient_filelist_search(c, FALSE, MPD_TABLE_ARTIST,
201                                                  local_pattern);
202                 if (list == NULL)
203                         list = filelist_new(NULL);
205                 list2 = mpdclient_filelist_search(c, FALSE, MPD_TABLE_TITLE,
206                                                   local_pattern);
207                 if (list2 != NULL) {
208                         filelist_move(list, list2);
209                         filelist_free(list2);
210                 }
212                 filelist_sort(list, compare_filelistentry_format);
213         } else {
214                 list = mpdclient_filelist_search(c, FALSE, table, local_pattern);
215                 if (list == NULL)
216                         list = filelist_new(NULL);
217         }
219         return list;
222 /*-----------------------------------------------------------------------
223  * NOTE: This code exists to test a new search ui,
224  *       Its ugly and MUST be redesigned before the next release!
225  *-----------------------------------------------------------------------
226  */
227 static mpdclient_filelist_t *
228 search_advanced_query(char *query, mpdclient_t *c)
230         int i,j;
231         char **strv;
232         int table[10];
233         char *arg[10];
234         mpdclient_filelist_t *fl = NULL;
236         advanced_search_mode = FALSE;
237         if( g_strrstr(query, ":") == NULL )
238                 return NULL;
240         strv = g_strsplit_set(query, ": ", 0);
242         i=0;
243         while (strv[i]) {
244                 D("strv[%d] = \"%s\"\n", i, strv[i]);
245                 i++;
246         }
248         memset(table, 0, 10*sizeof(int));
249         memset(arg, 0, 10*sizeof(char *));
251         i=0;
252         j=0;
253         while (strv[i] && strlen(strv[i]) > 0 && i < 9) {
254                 int id = search_get_tag_id(strv[i]);
255                 if (id == -1) {
256                         if (table[j]) {
257                                 char *tmp = arg[j];
258                                 arg[j] = g_strdup_printf("%s %s", arg[j], strv[i]);
259                                 g_free(tmp);
260                         } else {
261                                 D("Bad search tag %s\n", strv[i]);
262                                 screen_status_printf(_("Bad search tag %s"), strv[i]);
263                         }
264                         i++;
265                 } else if (strv[i+1] == NULL || strlen(strv[i+1]) == 0) {
266                         D("No argument for search tag %s\n", strv[i]);
267                         screen_status_printf(_("No argument for search tag %s"), strv[i]);
268                         i++;
269                         //        j--;
270                         //table[j] = -1;
271                 } else {
272                         table[j] = id;
273                         arg[j] = locale_to_utf8(strv[i+1]); // FREE ME
274                         j++;
275                         table[j] = -1;
276                         arg[j] = NULL;
277                         i = i + 2;
278                         advanced_search_mode = TRUE;
279                 }
280         }
282         g_strfreev(strv);
285         if (advanced_search_mode && j > 0) {
286                 int iter;
287                 mpd_InfoEntity *entity;
289                 /*-----------------------------------------------------------------------
290                  * NOTE (again): This code exists to test a new search ui,
291                  *               Its ugly and MUST be redesigned before the next release!
292                  *             + the code below should live in mpdclient.c
293                  *-----------------------------------------------------------------------
294                  */
295                 /** stupid - but this is just a test...... (fulhack)  */
296                 mpd_startSearch(c->connection, FALSE);
298                 for(iter = 0; iter < 10; iter++) {
299                         mpd_addConstraintSearch(c->connection, table[iter], arg[iter]);
300                 }
302                 mpd_commitSearch(c->connection);
304                 fl = g_malloc0(sizeof(mpdclient_filelist_t));
306                 while ((entity=mpd_getNextInfoEntity(c->connection)))
307                         filelist_append(fl, entity);
309                 if (mpdclient_finish_command(c) && fl)
310                         filelist_free(fl);
311         }
313         i=0;
314         while( arg[i] )
315                 g_free(arg[i++]);
317         return fl;
319 #else
320 #define search_advanced_query(pattern,c) (NULL)
321 #endif
323 static void
324 search_new(screen_t *screen, mpdclient_t *c)
326         search_clear(screen, c, TRUE);
328         pattern = screen_readln(screen->status_window.w,
329                                 _("Search: "),
330                                 NULL,
331                                 &search_history,
332                                 NULL);
334         if (pattern && strcmp(pattern,"") == 0) {
335                 g_free(pattern);
336                 pattern=NULL;
337         }
339         if (pattern == NULL) {
340                 list_window_reset(browser.lw);
341                 return;
342         }
344         if (!MPD_VERSION_LT(c, 0, 12, 0))
345                 browser.filelist = search_advanced_query(pattern, c);
347         if (!advanced_search_mode && browser.filelist == NULL)
348                 browser.filelist = filelist_search(c, FALSE,
349                                                   mode[options.search_mode].table,
350                                                   pattern);
352         if (browser.filelist == NULL)
353                 browser.filelist = filelist_new(NULL);
355         sync_highlights(c, browser.filelist);
356         mpdclient_install_playlist_callback(c, playlist_changed_callback);
357         list_window_check_selected(browser.lw, filelist_length(browser.filelist));
362 static void
363 init(WINDOW *w, int cols, int rows)
365         browser.lw = list_window_init(w, cols, rows);
368 static void
369 quit(void)
371         if (search_history)
372                 string_list_free(search_history);
373         if (browser.filelist)
374                 filelist_free(browser.filelist);
375         list_window_free(browser.lw);
377         if (pattern) {
378                 g_free(pattern);
379                 pattern = NULL;
380         }
383 static void
384 open(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
386         //  if( pattern==NULL )
387         //    search_new(screen, c);
388         // else
389         screen_status_printf(_("Press %s for a new search"),
390                              get_key_names(CMD_SCREEN_SEARCH,0));
391         search_check_mode();
394 static void
395 resize(int cols, int rows)
397         browser.lw->cols = cols;
398         browser.lw->rows = rows;
401 static void
402 paint(mpd_unused mpdclient_t *c)
404         if (browser.filelist) {
405                 browser.lw->flags = 0;
406                 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
407         } else {
408                 browser.lw->flags = LW_HIDE_CURSOR;
409                 list_window_paint(browser.lw, lw_search_help_callback, NULL);
410         }
413 static const char *
414 get_title(char *str, size_t size)
416         if (advanced_search_mode && pattern)
417                 g_snprintf(str, size, _("Search: %s"), pattern);
418         else if (pattern)
419                 g_snprintf(str, size,
420                            _("Search: Results for %s [%s]"),
421                            pattern,
422                            _(mode[options.search_mode].label));
423         else
424                 g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
425                            get_key_names(CMD_SCREEN_SEARCH,0),
426                            _(mode[options.search_mode].label));
428         return str;
431 static int
432 search_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
434         switch (cmd) {
435         case CMD_PLAY:
436                 browser_handle_enter(&browser, c);
437                 search_repaint();
438                 return 1;
440         case CMD_SELECT:
441                 if (browser_handle_select(&browser, c) == 0) {
442                         /* continue and select next item... */
443                         cmd = CMD_LIST_NEXT;
444                 }
445                 /* call list_window_cmd to go to the next item */
446                 break;
448         case CMD_SELECT_ALL:
449                 browser_handle_select_all(&browser, c);
450                 search_repaint();
451                 return 0;
453         case CMD_SEARCH_MODE:
454                 options.search_mode++;
455                 if (mode[options.search_mode].label == NULL)
456                         options.search_mode = 0;
457                 screen_status_printf(_("Search mode: %s"),
458                                      _(mode[options.search_mode].label));
459                 /* continue and update... */
460         case CMD_SCREEN_UPDATE:
461                 if (pattern) {
462                         search_clear(screen, c, FALSE);
463                         browser.filelist = filelist_search(c,
464                                                           FALSE,
465                                                           mode[options.search_mode].table,
466                                                           pattern);
467                         sync_highlights(c, browser.filelist);
468                 }
469                 search_repaint();
470                 return 1;
472         case CMD_SCREEN_SEARCH:
473                 search_new(screen, c);
474                 search_repaint();
475                 return 1;
477         case CMD_CLEAR:
478                 search_clear(screen, c, TRUE);
479                 list_window_reset(browser.lw);
480                 search_repaint();
481                 return 1;
483         case CMD_LIST_FIND:
484         case CMD_LIST_RFIND:
485         case CMD_LIST_FIND_NEXT:
486         case CMD_LIST_RFIND_NEXT:
487                 if (browser.filelist) {
488                         screen_find(screen,
489                                     browser.lw, filelist_length(browser.filelist),
490                                     cmd, browser_lw_callback,
491                                     browser.filelist);
492                         search_repaint();
493                 }
495                 return 1;
497         case CMD_MOUSE_EVENT:
498                 browser_handle_mouse_event(&browser, c);
499                 search_repaint();
500                 return 1;
502         default:
503                 break;
504         }
506         if (browser.filelist != NULL &&
507             list_window_cmd(browser.lw,
508                             filelist_length(browser.filelist), cmd)) {
509                 search_repaint();
510                 return 1;
511         }
513         return 0;
516 const struct screen_functions screen_search = {
517         .init = init,
518         .exit = quit,
519         .open = open,
520         .resize = resize,
521         .paint = paint,
522         .cmd = search_cmd,
523         .get_title = get_title,
524 };