Code

browse: ensure that filelist!=NULL
[ncmpc.git] / src / screen_search.c
1 /* 
2  * $Id$
3  *
4  * (c) 2004 by Kalle Wallin <kaw@linux.se>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  */
21 #include "config.h"
23 #ifndef DISABLE_SEARCH_SCREEN
24 #include "ncmpc.h"
25 #include "options.h"
26 #include "support.h"
27 #include "mpdclient.h"
28 #include "strfsong.h"
29 #include "command.h"
30 #include "screen.h"
31 #include "utils.h"
32 #include "screen_utils.h"
33 #include "screen_browser.h"
34 #include "gcc.h"
36 #include <ctype.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <glib.h>
40 #include <ncurses.h>
42 /* new search stuff with qball's libmpdclient */
43 #define FUTURE
45 #ifdef FUTURE
47 extern gint mpdclient_finish_command(mpdclient_t *c);
49 typedef struct {
50         int id;
51         const char *name;
52         const char *localname;
53 } search_tag_t;
55 static search_tag_t search_tag[] = {
56         { MPD_TAG_ITEM_ARTIST, "artist", N_("artist") },
57         { MPD_TAG_ITEM_ALBUM, "album", N_("album") },
58         { MPD_TAG_ITEM_TITLE, "title", N_("title") },
59         { MPD_TAG_ITEM_TRACK, "track", N_("track") },
60         { MPD_TAG_ITEM_NAME, "name", N_("name") },
61         { MPD_TAG_ITEM_GENRE, "genre", N_("genre") },
62         { MPD_TAG_ITEM_DATE, "date", N_("date") },
63         { MPD_TAG_ITEM_COMPOSER, "composer", N_("composer") },
64         { MPD_TAG_ITEM_PERFORMER, "performer", N_("performer") },
65         { MPD_TAG_ITEM_COMMENT, "comment", N_("comment") },
66         { MPD_TAG_ITEM_FILENAME, "filename", N_("file") },
67         { -1, NULL, NULL }
68 };
70 static int
71 search_get_tag_id(char *name)
72 {
73         int i = 0;
75         while (search_tag[i].name) {
76                 if (strcasecmp(search_tag[i].name, name) == 0 ||
77                     strcasecmp(search_tag[i].localname, name) == 0)
78                         return search_tag[i].id;
79                 i++;
80         }
82         return -1;
83 }
85 #endif
87 #define SEARCH_TITLE    0
88 #define SEARCH_ARTIST   1
89 #define SEARCH_ALBUM    2
90 #define SEARCH_FILE     3
92 #define SEARCH_ARTIST_TITLE 999
94 typedef struct {
95         int table;
96         const char *label;
97 } search_type_t;
99 static search_type_t mode[] = {
100         { MPD_TABLE_TITLE, N_("Title") },
101         { MPD_TABLE_ARTIST, N_("Artist") },
102         { MPD_TABLE_ALBUM, N_("Album") },
103         { MPD_TABLE_FILENAME, N_("Filename") },
104         { SEARCH_ARTIST_TITLE, N_("Artist + Title") },
105         { 0, NULL }
106 };
108 static GList *search_history = NULL;
109 static gchar *pattern = NULL;
110 static gboolean advanced_search_mode = FALSE;
112 static struct screen_browser browser;
115 /* search info */
116 static const char *
117 lw_search_help_callback(unsigned idx, mpd_unused int *highlight,
118                         mpd_unused void *data)
120         unsigned text_rows;
121         static const char *text[] = {
122                 "Quick  - just enter a string and ncmpc will search according",
123                 "               to the current search mode (displayed above).",
124                 "",
125                 "Advanced  -  <tag>:<search term> [<tag>:<search term>...]",
126                 "                       Example: artist:radiohead album:pablo honey",
127                 "",
128                 "                  avalible tags: artist, album, title, track,",
129                 "                  name, genre, date composer, performer, comment, file",
130                 "",
131                 NULL
132         };
134         text_rows=0;
135         while (text[text_rows])
136                 text_rows++;
138         if (idx < text_rows)
139                 return text[idx];
140         return NULL;
143 /* the playlist have been updated -> fix highlights */
144 static void
145 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
147         browser_playlist_changed(&browser, c, event, data);
150 /* sanity check search mode value */
151 static void
152 search_check_mode(void)
154         int max = 0;
156         while (mode[max].label != NULL)
157                 max++;
158         if (options.search_mode < 0)
159                 options.search_mode = 0;
160         else if (options.search_mode >= max)
161                 options.search_mode = max-1;
164 static void
165 search_clear(mpd_unused screen_t *screen, mpdclient_t *c,
166              gboolean clear_pattern)
168         if (browser.filelist) {
169                 mpdclient_remove_playlist_callback(c, playlist_changed_callback);
170                 filelist_free(browser.filelist);
171                 browser.filelist = filelist_new(NULL);
172         }
173         if (clear_pattern && pattern) {
174                 g_free(pattern);
175                 pattern = NULL;
176         }
179 #ifdef FUTURE
180 static mpdclient_filelist_t *
181 filelist_search(mpdclient_t *c, mpd_unused int exact_match, int table,
182                 gchar *local_pattern)
184         mpdclient_filelist_t *list, *list2;
186         if (table == SEARCH_ARTIST_TITLE) {
187                 list = mpdclient_filelist_search(c, FALSE, MPD_TABLE_ARTIST,
188                                                  local_pattern);
189                 if (list == NULL)
190                         list = filelist_new(NULL);
192                 list2 = mpdclient_filelist_search(c, FALSE, MPD_TABLE_TITLE,
193                                                   local_pattern);
194                 if (list2 != NULL) {
195                         filelist_move(list, list2);
196                         filelist_free(list2);
197                 }
199                 filelist_sort(list, compare_filelistentry_format);
200                 list->updated = TRUE;
201         } else {
202                 list = mpdclient_filelist_search(c, FALSE, table, local_pattern);
203                 if (list == NULL)
204                         list = filelist_new(NULL);
205         }
207         return list;
210 /*-----------------------------------------------------------------------
211  * NOTE: This code exists to test a new search ui,
212  *       Its ugly and MUST be redesigned before the next release!
213  *-----------------------------------------------------------------------
214  */
215 static mpdclient_filelist_t *
216 search_advanced_query(char *query, mpdclient_t *c)
218         int i,j;
219         char **strv;
220         int table[10];
221         char *arg[10];
222         mpdclient_filelist_t *fl = NULL;
224         advanced_search_mode = FALSE;
225         if( g_strrstr(query, ":") == NULL )
226                 return NULL;
228         strv = g_strsplit_set(query, ": ", 0);
230         i=0;
231         while (strv[i]) {
232                 D("strv[%d] = \"%s\"\n", i, strv[i]);
233                 i++;
234         }
236         memset(table, 0, 10*sizeof(int));
237         memset(arg, 0, 10*sizeof(char *));
239         i=0;
240         j=0;
241         while (strv[i] && strlen(strv[i]) > 0 && i < 9) {
242                 int id = search_get_tag_id(strv[i]);
243                 if (id == -1) {
244                         if (table[j]) {
245                                 char *tmp = arg[j];
246                                 arg[j] = g_strdup_printf("%s %s", arg[j], strv[i]);
247                                 g_free(tmp);
248                         } else {
249                                 D("Bad search tag %s\n", strv[i]);
250                                 screen_status_printf(_("Bad search tag %s"), strv[i]);
251                         }
252                         i++;
253                 } else if (strv[i+1] == NULL || strlen(strv[i+1]) == 0) {
254                         D("No argument for search tag %s\n", strv[i]);
255                         screen_status_printf(_("No argument for search tag %s"), strv[i]);
256                         i++;
257                         //        j--;
258                         //table[j] = -1;
259                 } else {
260                         table[j] = id;
261                         arg[j] = locale_to_utf8(strv[i+1]); // FREE ME
262                         j++;
263                         table[j] = -1;
264                         arg[j] = NULL;
265                         i = i + 2;
266                         advanced_search_mode = TRUE;
267                 }
268         }
270         g_strfreev(strv);
273         if (advanced_search_mode && j > 0) {
274                 int iter;
275                 mpd_InfoEntity *entity;
277                 /*-----------------------------------------------------------------------
278                  * NOTE (again): This code exists to test a new search ui,
279                  *               Its ugly and MUST be redesigned before the next release!
280                  *             + the code below should live in mpdclient.c
281                  *-----------------------------------------------------------------------
282                  */
283                 /** stupid - but this is just a test...... (fulhack)  */
284                 mpd_startSearch(c->connection, FALSE);
286                 for(iter = 0; iter < 10; iter++) {
287                         mpd_addConstraintSearch(c->connection, table[iter], arg[iter]);
288                 }
290                 mpd_commitSearch(c->connection);
292                 fl = g_malloc0(sizeof(mpdclient_filelist_t));
294                 while ((entity=mpd_getNextInfoEntity(c->connection)))
295                         filelist_append(fl, entity);
297                 if (mpdclient_finish_command(c) && fl)
298                         filelist_free(fl);
300                 fl->updated = TRUE;
301         }
303         i=0;
304         while( arg[i] )
305                 g_free(arg[i++]);
307         return fl;
309 #else
310 #define search_advanced_query(pattern,c) (NULL)
311 #endif
313 static void
314 search_new(screen_t *screen, mpdclient_t *c)
316         search_clear(screen, c, TRUE);
318         pattern = screen_readln(screen->status_window.w,
319                                 _("Search: "),
320                                 NULL,
321                                 &search_history,
322                                 NULL);
324         if (pattern && strcmp(pattern,"") == 0) {
325                 g_free(pattern);
326                 pattern=NULL;
327         }
329         if (pattern == NULL) {
330                 list_window_reset(browser.lw);
331                 return;
332         }
334         if (!MPD_VERSION_LT(c, 0, 12, 0))
335                 browser.filelist = search_advanced_query(pattern, c);
337         if (!advanced_search_mode && browser.filelist == NULL)
338                 browser.filelist = filelist_search(c, FALSE,
339                                                   mode[options.search_mode].table,
340                                                   pattern);
342         if (browser.filelist == NULL)
343                 browser.filelist = filelist_new(NULL);
345         sync_highlights(c, browser.filelist);
346         mpdclient_install_playlist_callback(c, playlist_changed_callback);
347         list_window_check_selected(browser.lw, filelist_length(browser.filelist));
352 static void
353 init(WINDOW *w, int cols, int rows)
355         browser.lw = list_window_init(w, cols, rows);
358 static void
359 quit(void)
361         if (search_history)
362                 string_list_free(search_history);
363         if (browser.filelist)
364                 filelist_free(browser.filelist);
365         list_window_free(browser.lw);
367         if (pattern) {
368                 g_free(pattern);
369                 pattern = NULL;
370         }
373 static void
374 open(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
376         //  if( pattern==NULL )
377         //    search_new(screen, c);
378         // else
379         screen_status_printf(_("Press %s for a new search"),
380                              get_key_names(CMD_SCREEN_SEARCH,0));
381         search_check_mode();
384 static void
385 resize(int cols, int rows)
387         browser.lw->cols = cols;
388         browser.lw->rows = rows;
391 static void
392 paint(mpd_unused screen_t *screen, mpdclient_t *c)
394         browser.lw->clear = 1;
396         if (browser.filelist) {
397                 browser.lw->flags = 0;
398                 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
399                 browser.filelist->updated = FALSE;
400         } else {
401                 browser.lw->flags = LW_HIDE_CURSOR;
402                 list_window_paint(browser.lw, lw_search_help_callback, NULL);
403                 if( !MPD_VERSION_LT(c, 0, 12, 0) )
404                         g_strdup_printf("Advanced search disabled (MPD version < 0.12.0");
405                 //      wmove(lw->w, 0, 0);
406                 //wclrtobot(lw->w);
407         }
409         wnoutrefresh(browser.lw->w);
412 static void
413 update(screen_t *screen, mpdclient_t *c)
415         if (browser.filelist == NULL || browser.filelist->updated) {
416                 paint(screen, c);
417                 return;
418         }
420         list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
421         wnoutrefresh(browser.lw->w);
424 static const char *
425 get_title(char *str, size_t size)
427         if (advanced_search_mode && pattern)
428                 g_snprintf(str, size, _("Search: %s"), pattern);
429         else if (pattern)
430                 g_snprintf(str, size,
431                            _("Search: Results for %s [%s]"),
432                            pattern,
433                            _(mode[options.search_mode].label));
434         else
435                 g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
436                            get_key_names(CMD_SCREEN_SEARCH,0),
437                            _(mode[options.search_mode].label));
439         return str;
442 static int
443 search_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
445         switch (cmd) {
446         case CMD_PLAY:
447                 browser_handle_enter(&browser, c);
448                 return 1;
450         case CMD_SELECT:
451                 if (browser_handle_select(&browser, c) == 0) {
452                         /* continue and select next item... */
453                         cmd = CMD_LIST_NEXT;
454                 }
455                 /* call list_window_cmd to go to the next item */
456                 return list_window_cmd(browser.lw, filelist_length(browser.filelist), cmd);
458         case CMD_SELECT_ALL:
459                 browser_handle_select_all(&browser, c);
460                 paint (screen, c);
461                 return 0;
463         case CMD_SEARCH_MODE:
464                 options.search_mode++;
465                 if (mode[options.search_mode].label == NULL)
466                         options.search_mode = 0;
467                 screen_status_printf(_("Search mode: %s"),
468                                      _(mode[options.search_mode].label));
469                 /* continue and update... */
470         case CMD_SCREEN_UPDATE:
471                 if (pattern) {
472                         search_clear(screen, c, FALSE);
473                         browser.filelist = filelist_search(c,
474                                                           FALSE,
475                                                           mode[options.search_mode].table,
476                                                           pattern);
477                         sync_highlights(c, browser.filelist);
478                 }
479                 return 1;
481         case CMD_SCREEN_SEARCH:
482                 search_new(screen, c);
483                 return 1;
485         case CMD_CLEAR:
486                 search_clear(screen, c, TRUE);
487                 list_window_reset(browser.lw);
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                         return screen_find(screen,
496                                            browser.lw, filelist_length(browser.filelist),
497                                            cmd, browser_lw_callback,
498                                            browser.filelist);
499                 else
500                         return 1;
502         case CMD_MOUSE_EVENT:
503                 return browser_handle_mouse_event(&browser, c);
505         default:
506                 if (browser.filelist)
507                         return list_window_cmd(browser.lw,
508                                                filelist_length(browser.filelist), cmd);
509         }
511         return 0;
514 const struct screen_functions screen_search = {
515         .init = init,
516         .exit = quit,
517         .open = open,
518         .resize = resize,
519         .paint = paint,
520         .update = update,
521         .cmd = search_cmd,
522         .get_title = get_title,
523 };
525 #endif /* ENABLE_SEARCH_SCREEN */