Code

screen_search: free second file list
[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 = 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                 list2 = mpdclient_filelist_search(c, FALSE, MPD_TABLE_TITLE,
190                                                   local_pattern);
192                 filelist_move(list, list2);
193                 filelist_free(list2);
194                 filelist_sort(list, compare_filelistentry_format);
195                 list->updated = TRUE;
196         } else
197                 list = mpdclient_filelist_search(c, FALSE, table, local_pattern);
199         return list;
202 /*-----------------------------------------------------------------------
203  * NOTE: This code exists to test a new search ui,
204  *       Its ugly and MUST be redesigned before the next release!
205  *-----------------------------------------------------------------------
206  */
207 static mpdclient_filelist_t *
208 search_advanced_query(char *query, mpdclient_t *c)
210         int i,j;
211         char **strv;
212         int table[10];
213         char *arg[10];
214         mpdclient_filelist_t *fl = NULL;
216         advanced_search_mode = FALSE;
217         if( g_strrstr(query, ":") == NULL )
218                 return NULL;
220         strv = g_strsplit_set(query, ": ", 0);
222         i=0;
223         while (strv[i]) {
224                 D("strv[%d] = \"%s\"\n", i, strv[i]);
225                 i++;
226         }
228         memset(table, 0, 10*sizeof(int));
229         memset(arg, 0, 10*sizeof(char *));
231         i=0;
232         j=0;
233         while (strv[i] && strlen(strv[i]) > 0 && i < 9) {
234                 int id = search_get_tag_id(strv[i]);
235                 if (id == -1) {
236                         if (table[j]) {
237                                 char *tmp = arg[j];
238                                 arg[j] = g_strdup_printf("%s %s", arg[j], strv[i]);
239                                 g_free(tmp);
240                         } else {
241                                 D("Bad search tag %s\n", strv[i]);
242                                 screen_status_printf(_("Bad search tag %s"), strv[i]);
243                         }
244                         i++;
245                 } else if (strv[i+1] == NULL || strlen(strv[i+1]) == 0) {
246                         D("No argument for search tag %s\n", strv[i]);
247                         screen_status_printf(_("No argument for search tag %s"), strv[i]);
248                         i++;
249                         //        j--;
250                         //table[j] = -1;
251                 } else {
252                         table[j] = id;
253                         arg[j] = locale_to_utf8(strv[i+1]); // FREE ME
254                         j++;
255                         table[j] = -1;
256                         arg[j] = NULL;
257                         i = i + 2;
258                         advanced_search_mode = TRUE;
259                 }
260         }
262         g_strfreev(strv);
265         if (advanced_search_mode && j > 0) {
266                 int iter;
267                 mpd_InfoEntity *entity;
269                 /*-----------------------------------------------------------------------
270                  * NOTE (again): This code exists to test a new search ui,
271                  *               Its ugly and MUST be redesigned before the next release!
272                  *             + the code below should live in mpdclient.c
273                  *-----------------------------------------------------------------------
274                  */
275                 /** stupid - but this is just a test...... (fulhack)  */
276                 mpd_startSearch(c->connection, FALSE);
278                 for(iter = 0; iter < 10; iter++) {
279                         mpd_addConstraintSearch(c->connection, table[iter], arg[iter]);
280                 }
282                 mpd_commitSearch(c->connection);
284                 fl = g_malloc0(sizeof(mpdclient_filelist_t));
286                 while ((entity=mpd_getNextInfoEntity(c->connection)))
287                         filelist_append(fl, entity);
289                 if (mpdclient_finish_command(c) && fl)
290                         filelist_free(fl);
292                 fl->updated = TRUE;
293         }
295         i=0;
296         while( arg[i] )
297                 g_free(arg[i++]);
299         return fl;
301 #else
302 #define search_advanced_query(pattern,c) (NULL)
303 #endif
305 static void
306 search_new(screen_t *screen, mpdclient_t *c)
308         search_clear(screen, c, TRUE);
310         pattern = screen_readln(screen->status_window.w,
311                                 _("Search: "),
312                                 NULL,
313                                 &search_history,
314                                 NULL);
316         if (pattern && strcmp(pattern,"") == 0) {
317                 g_free(pattern);
318                 pattern=NULL;
319         }
321         if (pattern == NULL) {
322                 list_window_reset(browser.lw);
323                 return;
324         }
326         if (!MPD_VERSION_LT(c, 0, 12, 0))
327                 browser.filelist = search_advanced_query(pattern, c);
329         if (!advanced_search_mode && browser.filelist == NULL)
330                 browser.filelist = filelist_search(c, FALSE,
331                                                   mode[options.search_mode].table,
332                                                   pattern);
334         sync_highlights(c, browser.filelist);
335         mpdclient_install_playlist_callback(c, playlist_changed_callback);
336         list_window_check_selected(browser.lw, filelist_length(browser.filelist));
341 static void
342 init(WINDOW *w, int cols, int rows)
344         browser.lw = list_window_init(w, cols, rows);
347 static void
348 quit(void)
350         if (search_history)
351                 string_list_free(search_history);
352         if (browser.filelist)
353                 filelist_free(browser.filelist);
354         list_window_free(browser.lw);
356         if (pattern) {
357                 g_free(pattern);
358                 pattern = NULL;
359         }
362 static void
363 open(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
365         //  if( pattern==NULL )
366         //    search_new(screen, c);
367         // else
368         screen_status_printf(_("Press %s for a new search"),
369                              get_key_names(CMD_SCREEN_SEARCH,0));
370         search_check_mode();
373 static void
374 resize(int cols, int rows)
376         browser.lw->cols = cols;
377         browser.lw->rows = rows;
380 static void
381 paint(mpd_unused screen_t *screen, mpdclient_t *c)
383         browser.lw->clear = 1;
385         if (browser.filelist) {
386                 browser.lw->flags = 0;
387                 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
388                 browser.filelist->updated = FALSE;
389         } else {
390                 browser.lw->flags = LW_HIDE_CURSOR;
391                 list_window_paint(browser.lw, lw_search_help_callback, NULL);
392                 if( !MPD_VERSION_LT(c, 0, 12, 0) )
393                         g_strdup_printf("Advanced search disabled (MPD version < 0.12.0");
394                 //      wmove(lw->w, 0, 0);
395                 //wclrtobot(lw->w);
396         }
398         wnoutrefresh(browser.lw->w);
401 static void
402 update(screen_t *screen, mpdclient_t *c)
404         if (browser.filelist == NULL || browser.filelist->updated) {
405                 paint(screen, c);
406                 return;
407         }
409         list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
410         wnoutrefresh(browser.lw->w);
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                 return 1;
439         case CMD_SELECT:
440                 if (browser_handle_select(&browser, c) == 0) {
441                         /* continue and select next item... */
442                         cmd = CMD_LIST_NEXT;
443                 }
444                 /* call list_window_cmd to go to the next item */
445                 return list_window_cmd(browser.lw, filelist_length(browser.filelist), cmd);
447         case CMD_SELECT_ALL:
448                 browser_handle_select_all(&browser, c);
449                 paint (screen, c);
450                 return 0;
452         case CMD_SEARCH_MODE:
453                 options.search_mode++;
454                 if (mode[options.search_mode].label == NULL)
455                         options.search_mode = 0;
456                 screen_status_printf(_("Search mode: %s"),
457                                      _(mode[options.search_mode].label));
458                 /* continue and update... */
459         case CMD_SCREEN_UPDATE:
460                 if (pattern) {
461                         search_clear(screen, c, FALSE);
462                         browser.filelist = filelist_search(c,
463                                                           FALSE,
464                                                           mode[options.search_mode].table,
465                                                           pattern);
466                         sync_highlights(c, browser.filelist);
467                 }
468                 return 1;
470         case CMD_SCREEN_SEARCH:
471                 search_new(screen, c);
472                 return 1;
474         case CMD_CLEAR:
475                 search_clear(screen, c, TRUE);
476                 list_window_reset(browser.lw);
477                 return 1;
479         case CMD_LIST_FIND:
480         case CMD_LIST_RFIND:
481         case CMD_LIST_FIND_NEXT:
482         case CMD_LIST_RFIND_NEXT:
483                 if (browser.filelist)
484                         return screen_find(screen,
485                                            browser.lw, filelist_length(browser.filelist),
486                                            cmd, browser_lw_callback,
487                                            browser.filelist);
488                 else
489                         return 1;
491         case CMD_MOUSE_EVENT:
492                 return browser_handle_mouse_event(&browser, c);
494         default:
495                 if (browser.filelist)
496                         return list_window_cmd(browser.lw,
497                                                filelist_length(browser.filelist), cmd);
498         }
500         return 0;
503 const struct screen_functions screen_search = {
504         .init = init,
505         .exit = quit,
506         .open = open,
507         .resize = resize,
508         .paint = paint,
509         .update = update,
510         .cmd = search_cmd,
511         .get_title = get_title,
512 };
514 #endif /* ENABLE_SEARCH_SCREEN */