Code

filelist: removed attribute "updated"
[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         } else {
217                 list = mpdclient_filelist_search(c, FALSE, table, local_pattern);
218                 if (list == NULL)
219                         list = filelist_new(NULL);
220         }
222         return list;
225 /*-----------------------------------------------------------------------
226  * NOTE: This code exists to test a new search ui,
227  *       Its ugly and MUST be redesigned before the next release!
228  *-----------------------------------------------------------------------
229  */
230 static mpdclient_filelist_t *
231 search_advanced_query(char *query, mpdclient_t *c)
233         int i,j;
234         char **strv;
235         int table[10];
236         char *arg[10];
237         mpdclient_filelist_t *fl = NULL;
239         advanced_search_mode = FALSE;
240         if( g_strrstr(query, ":") == NULL )
241                 return NULL;
243         strv = g_strsplit_set(query, ": ", 0);
245         i=0;
246         while (strv[i]) {
247                 D("strv[%d] = \"%s\"\n", i, strv[i]);
248                 i++;
249         }
251         memset(table, 0, 10*sizeof(int));
252         memset(arg, 0, 10*sizeof(char *));
254         i=0;
255         j=0;
256         while (strv[i] && strlen(strv[i]) > 0 && i < 9) {
257                 int id = search_get_tag_id(strv[i]);
258                 if (id == -1) {
259                         if (table[j]) {
260                                 char *tmp = arg[j];
261                                 arg[j] = g_strdup_printf("%s %s", arg[j], strv[i]);
262                                 g_free(tmp);
263                         } else {
264                                 D("Bad search tag %s\n", strv[i]);
265                                 screen_status_printf(_("Bad search tag %s"), strv[i]);
266                         }
267                         i++;
268                 } else if (strv[i+1] == NULL || strlen(strv[i+1]) == 0) {
269                         D("No argument for search tag %s\n", strv[i]);
270                         screen_status_printf(_("No argument for search tag %s"), strv[i]);
271                         i++;
272                         //        j--;
273                         //table[j] = -1;
274                 } else {
275                         table[j] = id;
276                         arg[j] = locale_to_utf8(strv[i+1]); // FREE ME
277                         j++;
278                         table[j] = -1;
279                         arg[j] = NULL;
280                         i = i + 2;
281                         advanced_search_mode = TRUE;
282                 }
283         }
285         g_strfreev(strv);
288         if (advanced_search_mode && j > 0) {
289                 int iter;
290                 mpd_InfoEntity *entity;
292                 /*-----------------------------------------------------------------------
293                  * NOTE (again): This code exists to test a new search ui,
294                  *               Its ugly and MUST be redesigned before the next release!
295                  *             + the code below should live in mpdclient.c
296                  *-----------------------------------------------------------------------
297                  */
298                 /** stupid - but this is just a test...... (fulhack)  */
299                 mpd_startSearch(c->connection, FALSE);
301                 for(iter = 0; iter < 10; iter++) {
302                         mpd_addConstraintSearch(c->connection, table[iter], arg[iter]);
303                 }
305                 mpd_commitSearch(c->connection);
307                 fl = g_malloc0(sizeof(mpdclient_filelist_t));
309                 while ((entity=mpd_getNextInfoEntity(c->connection)))
310                         filelist_append(fl, entity);
312                 if (mpdclient_finish_command(c) && fl)
313                         filelist_free(fl);
314         }
316         i=0;
317         while( arg[i] )
318                 g_free(arg[i++]);
320         return fl;
322 #else
323 #define search_advanced_query(pattern,c) (NULL)
324 #endif
326 static void
327 search_new(screen_t *screen, mpdclient_t *c)
329         search_clear(screen, c, TRUE);
331         pattern = screen_readln(screen->status_window.w,
332                                 _("Search: "),
333                                 NULL,
334                                 &search_history,
335                                 NULL);
337         if (pattern && strcmp(pattern,"") == 0) {
338                 g_free(pattern);
339                 pattern=NULL;
340         }
342         if (pattern == NULL) {
343                 list_window_reset(browser.lw);
344                 return;
345         }
347         if (!MPD_VERSION_LT(c, 0, 12, 0))
348                 browser.filelist = search_advanced_query(pattern, c);
350         if (!advanced_search_mode && browser.filelist == NULL)
351                 browser.filelist = filelist_search(c, FALSE,
352                                                   mode[options.search_mode].table,
353                                                   pattern);
355         if (browser.filelist == NULL)
356                 browser.filelist = filelist_new(NULL);
358         sync_highlights(c, browser.filelist);
359         mpdclient_install_playlist_callback(c, playlist_changed_callback);
360         list_window_check_selected(browser.lw, filelist_length(browser.filelist));
365 static void
366 init(WINDOW *w, int cols, int rows)
368         browser.lw = list_window_init(w, cols, rows);
371 static void
372 quit(void)
374         if (search_history)
375                 string_list_free(search_history);
376         if (browser.filelist)
377                 filelist_free(browser.filelist);
378         list_window_free(browser.lw);
380         if (pattern) {
381                 g_free(pattern);
382                 pattern = NULL;
383         }
386 static void
387 open(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
389         //  if( pattern==NULL )
390         //    search_new(screen, c);
391         // else
392         screen_status_printf(_("Press %s for a new search"),
393                              get_key_names(CMD_SCREEN_SEARCH,0));
394         search_check_mode();
397 static void
398 resize(int cols, int rows)
400         browser.lw->cols = cols;
401         browser.lw->rows = rows;
404 static void
405 paint(mpd_unused mpdclient_t *c)
407         if (browser.filelist) {
408                 browser.lw->flags = 0;
409                 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
410         } else {
411                 browser.lw->flags = LW_HIDE_CURSOR;
412                 list_window_paint(browser.lw, lw_search_help_callback, NULL);
413         }
416 static const char *
417 get_title(char *str, size_t size)
419         if (advanced_search_mode && pattern)
420                 g_snprintf(str, size, _("Search: %s"), pattern);
421         else if (pattern)
422                 g_snprintf(str, size,
423                            _("Search: Results for %s [%s]"),
424                            pattern,
425                            _(mode[options.search_mode].label));
426         else
427                 g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
428                            get_key_names(CMD_SCREEN_SEARCH,0),
429                            _(mode[options.search_mode].label));
431         return str;
434 static int
435 search_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
437         switch (cmd) {
438         case CMD_PLAY:
439                 browser_handle_enter(&browser, c);
440                 search_repaint();
441                 return 1;
443         case CMD_SELECT:
444                 if (browser_handle_select(&browser, c) == 0) {
445                         /* continue and select next item... */
446                         cmd = CMD_LIST_NEXT;
447                 }
448                 /* call list_window_cmd to go to the next item */
449                 break;
451         case CMD_SELECT_ALL:
452                 browser_handle_select_all(&browser, c);
453                 search_repaint();
454                 return 0;
456         case CMD_SEARCH_MODE:
457                 options.search_mode++;
458                 if (mode[options.search_mode].label == NULL)
459                         options.search_mode = 0;
460                 screen_status_printf(_("Search mode: %s"),
461                                      _(mode[options.search_mode].label));
462                 /* continue and update... */
463         case CMD_SCREEN_UPDATE:
464                 if (pattern) {
465                         search_clear(screen, c, FALSE);
466                         browser.filelist = filelist_search(c,
467                                                           FALSE,
468                                                           mode[options.search_mode].table,
469                                                           pattern);
470                         sync_highlights(c, browser.filelist);
471                 }
472                 search_repaint();
473                 return 1;
475         case CMD_SCREEN_SEARCH:
476                 search_new(screen, c);
477                 search_repaint();
478                 return 1;
480         case CMD_CLEAR:
481                 search_clear(screen, c, TRUE);
482                 list_window_reset(browser.lw);
483                 search_repaint();
484                 return 1;
486         case CMD_LIST_FIND:
487         case CMD_LIST_RFIND:
488         case CMD_LIST_FIND_NEXT:
489         case CMD_LIST_RFIND_NEXT:
490                 if (browser.filelist) {
491                         screen_find(screen,
492                                     browser.lw, filelist_length(browser.filelist),
493                                     cmd, browser_lw_callback,
494                                     browser.filelist);
495                         search_repaint();
496                 }
498                 return 1;
500         case CMD_MOUSE_EVENT:
501                 browser_handle_mouse_event(&browser, c);
502                 search_repaint();
503                 return 1;
505         default:
506                 break;
507         }
509         if (browser.filelist != NULL &&
510             list_window_cmd(browser.lw,
511                             filelist_length(browser.filelist), cmd)) {
512                 search_repaint();
513                 return 1;
514         }
516         return 0;
519 const struct screen_functions screen_search = {
520         .init = init,
521         .exit = quit,
522         .open = open,
523         .resize = resize,
524         .paint = paint,
525         .cmd = search_cmd,
526         .get_title = get_title,
527 };
529 #endif /* ENABLE_SEARCH_SCREEN */