Code

fix unused parameter warnings
[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_browse.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
46 #ifdef FUTURE
48 extern gint mpdclient_finish_command(mpdclient_t *c);
50 typedef struct {
51         int id;
52         const char *name;
53         const char *localname;
54 } search_tag_t;
56 static search_tag_t search_tag[] = {
57   { MPD_TAG_ITEM_ARTIST,   "artist",    N_("artist") },
58   { MPD_TAG_ITEM_ALBUM,    "album",     N_("album") },
59   { MPD_TAG_ITEM_TITLE,    "title",     N_("title") },
60   { MPD_TAG_ITEM_TRACK,    "track",     N_("track") },
61   { MPD_TAG_ITEM_NAME,     "name",      N_("name") },
62   { MPD_TAG_ITEM_GENRE,    "genre",     N_("genre") },
63   { MPD_TAG_ITEM_DATE,     "date",      N_("date") },
64   { MPD_TAG_ITEM_COMPOSER, "composer",  N_("composer") },
65   { MPD_TAG_ITEM_PERFORMER,"performer", N_("performer") },
66   { MPD_TAG_ITEM_COMMENT,  "comment",   N_("comment") },
67   { MPD_TAG_ITEM_FILENAME, "filename",  N_("file") },
68   { -1,                    NULL,        NULL }
69 };
71 static int
72 search_get_tag_id(char *name)
73 {
74   int i;
76   i=0;
77   while( search_tag[i].name )
78     {
79       if( strcasecmp(search_tag[i].name, name)==0 || 
80           strcasecmp(search_tag[i].localname, name)==0 )
81         return search_tag[i].id;
82       i++;
83     }
84   return -1;
85 }
87 #endif
90 #define SEARCH_TITLE    0
91 #define SEARCH_ARTIST   1
92 #define SEARCH_ALBUM    2
93 #define SEARCH_FILE     3
95 #define SEARCH_ARTIST_TITLE 999
97 typedef struct {
98         int table;
99         const char *label;
100 } search_type_t;
102 static search_type_t mode[] = {
103   { MPD_TABLE_TITLE,     N_("Title") },
104   { MPD_TABLE_ARTIST,    N_("Artist") },
105   { MPD_TABLE_ALBUM,     N_("Album") },
106   { MPD_TABLE_FILENAME,  N_("Filename") },
107   { SEARCH_ARTIST_TITLE, N_("Artist + Title") },
108   { 0, NULL }
109 };
111 static list_window_t *lw = NULL;
112 static mpdclient_filelist_t *filelist = NULL;
113 static GList *search_history = NULL;
114 static gchar *pattern = NULL;
115 static gboolean advanced_search_mode = FALSE;
118 /* search info */
119 static const char *
120 lw_search_help_callback(unsigned idx, mpd_unused int *highlight,
121                         mpd_unused void *data)
123         unsigned text_rows;
124         static const char *text[] = {
125                 "Quick  - just enter a string and ncmpc will search according",
126                 "               to the current search mode (displayed above).",
127                 "",
128                 "Advanced  -  <tag>:<search term> [<tag>:<search term>...]",
129                 "                       Example: artist:radiohead album:pablo honey",
130                 "",
131                 "                  avalible tags: artist, album, title, track,",
132                 "                  name, genre, date composer, performer, comment, file",
133                 "",
134                 NULL
135         };
137         text_rows=0;
138         while (text[text_rows])
139                 text_rows++;
141         if (idx < text_rows)
142                 return text[idx];
143         return NULL;
146 /* the playlist have been updated -> fix highlights */
147 static void 
148 playlist_changed_callback(mpdclient_t *c, int event, mpd_unused gpointer data)
150   if( filelist==NULL )
151     return;
152   D("screen_search.c> playlist_callback() [%d]\n", event);
153   switch(event)
154     {
155     case PLAYLIST_EVENT_CLEAR:
156       clear_highlights(filelist);
157       break;
158     default:
159       sync_highlights(c, filelist);
160       break;
161     }
164 /* sanity check search mode value */
165 static void
166 search_check_mode(void)
168   int max = 0;
170   while( mode[max].label != NULL )
171     max++;
172   if( options.search_mode<0 )
173     options.search_mode = 0;
174   else if( options.search_mode>=max )
175     options.search_mode = max-1;
178 static void
179 search_clear(mpd_unused screen_t *screen, mpdclient_t *c,
180              gboolean clear_pattern)
182   if( filelist )
183     {
184       mpdclient_remove_playlist_callback(c, playlist_changed_callback);
185       filelist = mpdclient_filelist_free(filelist);
186     }
187   if( clear_pattern && pattern )
188     {
189       g_free(pattern);
190       pattern = NULL;
191     }
194 #ifdef FUTURE
195 static mpdclient_filelist_t *
196 filelist_search(mpdclient_t *c, mpd_unused int exact_match, int table,
197                 gchar *local_pattern)
199   mpdclient_filelist_t *list, *list2;
201   if( table == SEARCH_ARTIST_TITLE )
202     {
203       list = mpdclient_filelist_search(c, FALSE, MPD_TABLE_ARTIST,
204                                        local_pattern);
205       list2 = mpdclient_filelist_search(c, FALSE, MPD_TABLE_TITLE,
206                                         local_pattern);
208       list->length += list2->length;
209       list->list = g_list_concat(list->list, list2->list);
210       list->list = g_list_sort(list->list, compare_filelistentry_format);
211       list->updated = TRUE;
212     }
213   else
214     {
215       list = mpdclient_filelist_search(c, FALSE, table, local_pattern);
216     }
218   return list;
221 /*-----------------------------------------------------------------------
222  * NOTE: This code exists to test a new search ui,
223  *       Its ugly and MUST be redesigned before the next release!
224  *-----------------------------------------------------------------------
225  */
226 static mpdclient_filelist_t *
227 search_advanced_query(char *query, mpdclient_t *c)
229         int i,j;
230         char **strv;
231         int table[10];
232         char *arg[10];
233         mpdclient_filelist_t *fl = NULL;
235         advanced_search_mode = FALSE;
236         if( g_strrstr(query, ":") == NULL )
237                 return NULL;
239         strv = g_strsplit_set(query, ": ", 0);
241         i=0;
242         while (strv[i]) {
243                 D("strv[%d] = \"%s\"\n", i, strv[i]);
244                 i++;
245         }
247         memset(table, 0, 10*sizeof(int));
248         memset(arg, 0, 10*sizeof(char *));
250         i=0;
251         j=0;
252         while (strv[i] && strlen(strv[i]) > 0 && i < 9) {
253                 int id = search_get_tag_id(strv[i]);
254                 if (id == -1) {
255                         if (table[j]) {
256                                 char *tmp = arg[j];
257                                 arg[j] = g_strdup_printf("%s %s", arg[j], strv[i]);
258                                 g_free(tmp);
259                         } else {
260                                 D("Bad search tag %s\n", strv[i]);
261                                 screen_status_printf(_("Bad search tag %s"), strv[i]);
262                         }
263                         i++;
264                 } else if (strv[i+1] == NULL || strlen(strv[i+1]) == 0) {
265                         D("No argument for search tag %s\n", strv[i]);
266                         screen_status_printf(_("No argument for search tag %s"), strv[i]);
267                         i++;
268                         //        j--;
269                         //table[j] = -1;
270                 } else {
271                         table[j] = id;
272                         arg[j] = locale_to_utf8(strv[i+1]); // FREE ME
273                         j++;
274                         table[j] = -1;
275                         arg[j] = NULL;
276                         i = i + 2;
277                         advanced_search_mode = TRUE;
278                 }
279         }
281         g_strfreev(strv);
284         if (advanced_search_mode && j > 0) {
285                 int iter;
286                 mpd_InfoEntity *entity;
288                 /*-----------------------------------------------------------------------
289                  * NOTE (again): This code exists to test a new search ui,
290                  *               Its ugly and MUST be redesigned before the next release!
291                  *             + the code below should live in mpdclient.c
292                  *-----------------------------------------------------------------------
293                  */
294                 /** stupid - but this is just a test...... (fulhack)  */
295                 mpd_startSearch(c->connection, FALSE);
297                 for(iter = 0; iter < 10; iter++) {
298                         mpd_addConstraintSearch(c->connection, table[iter], arg[iter]);
299                 }
301                 mpd_commitSearch(c->connection);
303                 fl = g_malloc0(sizeof(mpdclient_filelist_t));
305                 while ((entity=mpd_getNextInfoEntity(c->connection)))  {
306                         filelist_entry_t *entry = g_malloc0(sizeof(filelist_entry_t));
308                         entry->entity = entity;
309                         fl->list = g_list_append(fl->list, (gpointer) entry);
310                         fl->length++;
311                 }
313                 if (mpdclient_finish_command(c) && fl)
314                         fl = mpdclient_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);
333   
334   pattern = screen_readln(screen->status_window.w, 
335                           _("Search: "),
336                           NULL,
337                           &search_history,
338                           NULL);
340   if( pattern && strcmp(pattern,"")==0 )
341     {
342       g_free(pattern);
343       pattern=NULL;
344     }
345   
346   if( pattern==NULL )
347     {
348       list_window_reset(lw);
349       return;
350     }
352   if( !MPD_VERSION_LT(c, 0, 12, 0) )
353     filelist = search_advanced_query(pattern, c);
354   if( !advanced_search_mode && filelist==NULL )
355     filelist = filelist_search(c, 
356                                FALSE,
357                                mode[options.search_mode].table,
358                                pattern);
359   sync_highlights(c, filelist);
360   mpdclient_install_playlist_callback(c, playlist_changed_callback);
361   list_window_check_selected(lw, filelist->length);
366 static void
367 init(WINDOW *w, int cols, int rows)
369   lw = list_window_init(w, cols, rows);
372 static void
373 quit(void)
375   if( search_history )
376     string_list_free(search_history);
377   if( filelist )
378     filelist = mpdclient_filelist_free(filelist);
379   list_window_free(lw);
380   if( pattern )
381     g_free(pattern);
382   pattern = NULL;
385 static void
386 open(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
388   //  if( pattern==NULL )
389   //    search_new(screen, c);
390   // else
391   screen_status_printf(_("Press %s for a new search"),
392                          get_key_names(CMD_SCREEN_SEARCH,0));
393   search_check_mode();
396 static void
397 resize(int cols, int rows)
399   lw->cols = cols;
400   lw->rows = rows;
403 static void
404 close(void)
408 static void 
409 paint(mpd_unused screen_t *screen, mpdclient_t *c)
411   lw->clear = 1;
412   
413   if( filelist )
414     {
415       lw->flags = 0;
416       list_window_paint(lw, browse_lw_callback, (void *) filelist);
417       filelist->updated = FALSE;
418     }
419   else
420     {
421       lw->flags = LW_HIDE_CURSOR;
422       list_window_paint(lw, lw_search_help_callback, NULL);
423       if( !MPD_VERSION_LT(c, 0, 12, 0) )
424         g_strdup_printf("Advanced search disabled (MPD version < 0.12.0"); 
425       //      wmove(lw->w, 0, 0);
426       //wclrtobot(lw->w);
427     }
428   wnoutrefresh(lw->w);
431 static void 
432 update(screen_t *screen, mpdclient_t *c)
434   if( filelist==NULL || filelist->updated )
435     {
436       paint(screen, c);
437       return;
438     }
439   list_window_paint(lw, browse_lw_callback, (void *) filelist);
440   wnoutrefresh(lw->w);
443 static const char *
444 get_title(char *str, size_t size)
446   if( advanced_search_mode && pattern )
447     g_snprintf(str, size, _("Search: %s"), pattern);
448   else if( pattern )
449     g_snprintf(str, size, 
450                _("Search: Results for %s [%s]"), 
451                pattern,
452                _(mode[options.search_mode].label));
453   else
454     g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
455                get_key_names(CMD_SCREEN_SEARCH,0),
456                _(mode[options.search_mode].label));
457                
458   return str;
461 static list_window_t *
462 get_filelist_window(void)
464         return lw;
467 static int 
468 search_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
470   switch(cmd)
471     {
472     case CMD_PLAY:
473        browse_handle_enter(screen, c, lw, filelist);
474       return 1;
476     case CMD_SELECT:
477       if( browse_handle_select(screen, c, lw, filelist) == 0 )
478         {
479           /* continue and select next item... */
480           cmd = CMD_LIST_NEXT;
481         }
482       /* call list_window_cmd to go to the next item */
483       return list_window_cmd(lw, filelist->length, cmd);
485     case CMD_SELECT_ALL:
486       browse_handle_select_all (screen, c, lw, filelist);
487       paint (screen, c);
488       return 0;
490     case CMD_SEARCH_MODE:
491       options.search_mode++;
492       if( mode[options.search_mode].label == NULL )
493         options.search_mode = 0;
494       screen_status_printf(_("Search mode: %s"), 
495                            _(mode[options.search_mode].label));
496       /* continue and update... */
497     case CMD_SCREEN_UPDATE:
498       if( pattern )
499         {
500           search_clear(screen, c, FALSE);
501           filelist = filelist_search(c, 
502                                      FALSE,
503                                      mode[options.search_mode].table,
504                                      pattern);
505           sync_highlights(c, filelist);
506         }
507       return 1;
509     case CMD_SCREEN_SEARCH:
510       search_new(screen, c);
511       return 1;
513     case CMD_CLEAR:
514       search_clear(screen, c, TRUE);
515       list_window_reset(lw);
516       return 1;
518     case CMD_LIST_FIND:
519     case CMD_LIST_RFIND:
520     case CMD_LIST_FIND_NEXT:
521     case CMD_LIST_RFIND_NEXT:
522       if( filelist )
523         return screen_find(screen,
524                            lw, filelist->length,
525                            cmd, browse_lw_callback, (void *) filelist);
526       else
527         return 1;
529     case CMD_MOUSE_EVENT:
530       return browse_handle_mouse_event(screen,c,lw,filelist);
532     default:
533       if( filelist )
534         return list_window_cmd(lw, filelist->length, cmd);
535     }
536   
537   return 0;
540 screen_functions_t *
541 get_screen_search(void)
543   static screen_functions_t functions;
545   memset(&functions, 0, sizeof(screen_functions_t));
546   functions.init   = init;
547   functions.exit   = quit;
548   functions.open   = open;
549   functions.close  = close;
550   functions.resize = resize;
551   functions.paint  = paint;
552   functions.update = update;
553   functions.cmd    = search_cmd;
554   functions.get_lw = get_filelist_window;
555   functions.get_title = get_title;
557   return &functions;
561 #endif /* ENABLE_SEARCH_SCREEN */