Code

7fb559fabc7eeaac01e33905630f51b7b41924f6
[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 <ctype.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <glib.h>
25 #include <ncurses.h>
27 #include "config.h"
28 #ifndef DISABLE_SEARCH_SCREEN
29 #include "ncmpc.h"
30 #include "options.h"
31 #include "support.h"
32 #include "mpdclient.h"
33 #include "strfsong.h"
34 #include "command.h"
35 #include "screen.h"
36 #include "utils.h"
37 #include "screen_utils.h"
38 #include "screen_browse.h"
40 /* new search stuff with qball's libmpdclient */
41 #define FUTURE
44 #ifdef FUTURE
46 extern gint mpdclient_finish_command(mpdclient_t *c);
48 typedef struct
49 {
50   int id;
51   char *name;
52   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;
75   i=0;
76   while( search_tag[i].name )
77     {
78       if( strcasecmp(search_tag[i].name, name)==0 || 
79           strcasecmp(search_tag[i].localname, name)==0 )
80         return search_tag[i].id;
81       i++;
82     }
83   return -1;
84 }
86 #endif
89 #define SEARCH_TITLE    0
90 #define SEARCH_ARTIST   1
91 #define SEARCH_ALBUM    2
92 #define SEARCH_FILE     3
94 typedef struct {
95   int table;
96   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   { 0, NULL }
105 };
107 static list_window_t *lw = NULL;
108 static mpdclient_filelist_t *filelist = NULL;
109 static GList *search_history = NULL;
110 static gchar *pattern = NULL;
111 static gboolean advanced_search_mode = FALSE;
114 /* search info */
115 static char *
116 lw_search_help_callback(int index, int *highlight, void *data)
118   int text_rows;
119   static 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++;
135   
136   if( index < text_rows )
137     return text[index];
138   return NULL;
141 /* the playlist have been updated -> fix highlights */
142 static void 
143 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
145   if( filelist==NULL )
146     return;
147   D("screen_search.c> playlist_callback() [%d]\n", event);
148   switch(event)
149     {
150     case PLAYLIST_EVENT_CLEAR:
151       clear_highlights(filelist);
152       break;
153     default:
154       sync_highlights(c, filelist);
155       break;
156     }
159 /* sanity check search mode value */
160 static void
161 search_check_mode(void)
163   int max = 0;
165   while( mode[max].label != NULL )
166     max++;
167   if( options.search_mode<0 )
168     options.search_mode = 0;
169   else if( options.search_mode>=max )
170     options.search_mode = max-1;
173 static void
174 search_clear(screen_t *screen, mpdclient_t *c, gboolean clear_pattern)
176   if( filelist )
177     {
178       mpdclient_remove_playlist_callback(c, playlist_changed_callback);
179       filelist = mpdclient_filelist_free(filelist);
180     }
181   if( clear_pattern && pattern )
182     {
183       g_free(pattern);
184       pattern = NULL;
185     }
188 #ifdef FUTURE
189 /*-----------------------------------------------------------------------
190  * NOTE: This code exists to test a new search ui,
191  *       Its ugly and MUST be redesigned before the next release!
192  *-----------------------------------------------------------------------
193  */
194 static mpdclient_filelist_t *
195 search_advanced_query(char *query, mpdclient_t *c)
197   int i,j;
198   char **strv;
199   int table[10];
200   char *arg[10];
201   mpdclient_filelist_t *filelist = NULL;
203   advanced_search_mode = FALSE;
204   if( g_strrstr(query, ":") == NULL )
205     return NULL;
206   
207   strv = g_strsplit_set(query, ": ", 0);
209   i=0;
210   while( strv[i] )
211     {
212       D("strv[%d] = \"%s\"\n", i, strv[i]);
213       i++;
214     }
216   memset(table, 0, 10*sizeof(int));
217   memset(arg,   0, 10*sizeof(char *));
219   i=0;
220   j=0;
221   while( strv[i] && strlen(strv[i])>0 && i<9 )
222     {
223       D("strv[%d] = \"%s\"\n", i, strv[i]);
225       int id = search_get_tag_id(strv[i]);
226       if( id==-1 )
227         {
228           if( table[j] )
229             {
230               char *tmp = arg[j];
231               arg[j] = g_strdup_printf("%s %s", arg[j], strv[i]);
232               g_free(tmp);
233             }
234           else
235             {
236               D("Bad search tag %s\n", strv[i]);
237               screen_status_printf(_("Bad search tag %s"), strv[i]);
238             }
239           i++;
240         }
241       else if( strv[i+1] == NULL || strlen(strv[i+1])==0 )
242         {
243           D("No argument for search tag %s\n", strv[i]);
244           screen_status_printf(_("No argument for search tag %s"), strv[i]);
245           i++;
246           //      j--;
247           //table[j] = -1;
248         }
249       else
250         {
251           table[j] = id;
252           arg[j] = locale_to_utf8(strv[i+1]); // FREE ME
253           j++;
254           table[j] = -1;
255           arg[j] = NULL;
256           i = i + 2;
257           advanced_search_mode = TRUE;
258         }     
259     }
261   g_strfreev(strv);
264   if( advanced_search_mode && j>0 )
265     {
266       /*-----------------------------------------------------------------------
267        * NOTE (again): This code exists to test a new search ui,
268        *               Its ugly and MUST be redesigned before the next release!
269        *             + the code below should live in mpdclient.c
270        *-----------------------------------------------------------------------
271        */
272       /** stupid - but this is just a test...... (fulhack)  */
273           mpd_startSearch(c->connection, FALSE);
275           int iter;
276           for(iter = 0; iter < 10; iter++)
277           {
278                   mpd_addConstraintSearch(c->connection, table[iter], arg[iter]);
279           }               
280                         
281           mpd_commitSearch(c->connection);
282           
283       filelist = g_malloc0(sizeof(mpdclient_filelist_t));
285       mpd_InfoEntity *entity;
287       while( (entity=mpd_getNextInfoEntity(c->connection)) ) 
288         {
289           filelist_entry_t *entry = g_malloc0(sizeof(filelist_entry_t));
290       
291           entry->entity = entity;
292           filelist->list = g_list_append(filelist->list, (gpointer) entry);
293           filelist->length++;
294         }
295   
296       if( mpdclient_finish_command(c) && filelist )
297         filelist = mpdclient_filelist_free(filelist);
299       filelist->updated = TRUE;
300     } 
301   
302   i=0;
303   while( arg[i] )
304     g_free(arg[i++]);
306   return filelist;
308 #else
309 #define search_advanced_query(pattern,c) (NULL)
310 #endif
312 static void
313 search_new(screen_t *screen, mpdclient_t *c)
315   search_clear(screen, c, TRUE);
316   
317   pattern = screen_readln(screen->status_window.w, 
318                           _("Search: "),
319                           NULL,
320                           &search_history,
321                           NULL);
323   if( pattern && strcmp(pattern,"")==0 )
324     {
325       g_free(pattern);
326       pattern=NULL;
327     }
328   
329   if( pattern==NULL )
330     {
331       list_window_reset(lw);
332       return;
333     }
335   if( !MPD_VERSION_LT(c, 0, 12, 0) )
336     filelist = search_advanced_query(pattern, c);
337   if( !advanced_search_mode && filelist==NULL )
338     filelist = mpdclient_filelist_search(c, 
339                                          FALSE,
340                                          mode[options.search_mode].table,
341                                          pattern);
342   sync_highlights(c, filelist);
343   mpdclient_install_playlist_callback(c, playlist_changed_callback);
344   list_window_check_selected(lw, filelist->length);
349 static void
350 init(WINDOW *w, int cols, int rows)
352   lw = list_window_init(w, cols, rows);
355 static void
356 quit(void)
358   if( search_history )
359     string_list_free(search_history);
360   if( filelist )
361     filelist = mpdclient_filelist_free(filelist);
362   list_window_free(lw);
363   if( pattern )
364     g_free(pattern);
365   pattern = NULL;
368 static void
369 open(screen_t *screen, mpdclient_t *c)
371   //  if( pattern==NULL )
372   //    search_new(screen, c);
373   // else
374   screen_status_printf(_("Press %s for a new search"),
375                          get_key_names(CMD_SCREEN_SEARCH,0));
376   search_check_mode();
379 static void
380 resize(int cols, int rows)
382   lw->cols = cols;
383   lw->rows = rows;
386 static void
387 close(void)
391 static void 
392 paint(screen_t *screen, mpdclient_t *c)
394   lw->clear = 1;
395   
396   if( filelist )
397     {
398       lw->flags = 0;
399       list_window_paint(lw, browse_lw_callback, (void *) filelist);
400       filelist->updated = FALSE;
401     }
402   else
403     {
404       lw->flags = LW_HIDE_CURSOR;
405       list_window_paint(lw, lw_search_help_callback, NULL);
406       if( !MPD_VERSION_LT(c, 0, 12, 0) )
407         g_strdup_printf("Advanced search disabled (MPD version < 0.12.0"); 
408       //      wmove(lw->w, 0, 0);
409       //wclrtobot(lw->w);
410     }
411   wnoutrefresh(lw->w);
414 static void 
415 update(screen_t *screen, mpdclient_t *c)
417   if( filelist==NULL || filelist->updated )
418     {
419       paint(screen, c);
420       return;
421     }
422   list_window_paint(lw, browse_lw_callback, (void *) filelist);
423   wnoutrefresh(lw->w);
426 static char *
427 get_title(char *str, size_t size)
429   if( advanced_search_mode && pattern )
430     g_snprintf(str, size, _("Search: %s"), pattern);
431   else if( pattern )
432     g_snprintf(str, size, 
433                _("Search: Results for %s [%s]"), 
434                pattern,
435                _(mode[options.search_mode].label));
436   else
437     g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
438                get_key_names(CMD_SCREEN_SEARCH,0),
439                _(mode[options.search_mode].label));
440                
441   return str;
444 static list_window_t *
445 get_filelist_window()
447   return lw;
450 static int 
451 search_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
453   switch(cmd)
454     {
455     case CMD_PLAY:
456        browse_handle_enter(screen, c, lw, filelist);
457       return 1;
459     case CMD_SELECT:
460       if( browse_handle_select(screen, c, lw, filelist) == 0 )
461         {
462           /* continue and select next item... */
463           cmd = CMD_LIST_NEXT;
464         }
465       return 1;
467     case CMD_SEARCH_MODE:
468       options.search_mode++;
469       if( mode[options.search_mode].label == NULL )
470         options.search_mode = 0;
471       screen_status_printf(_("Search mode: %s"), 
472                            _(mode[options.search_mode].label));
473       /* continue and update... */
474     case CMD_SCREEN_UPDATE:
475       if( pattern )
476         {
477           search_clear(screen, c, FALSE);
478           filelist = mpdclient_filelist_search(c, 
479                                                FALSE,
480                                                mode[options.search_mode].table,
481                                                pattern);
482           sync_highlights(c, filelist);
483         }
484       return 1;
486     case CMD_SCREEN_SEARCH:
487       search_new(screen, c);
488       return 1;
490     case CMD_CLEAR:
491       search_clear(screen, c, TRUE);
492       list_window_reset(lw);
493       return 1;
495     case CMD_LIST_FIND:
496     case CMD_LIST_RFIND:
497     case CMD_LIST_FIND_NEXT:
498     case CMD_LIST_RFIND_NEXT:
499       if( filelist )
500         return screen_find(screen, c, 
501                            lw, filelist->length,
502                            cmd, browse_lw_callback, (void *) filelist);
503       else
504         return 1;
506     case CMD_MOUSE_EVENT:
507       return browse_handle_mouse_event(screen,c,lw,filelist);
509     default:
510       if( filelist )
511         return list_window_cmd(lw, filelist->length, cmd);
512     }
513   
514   return 0;
517 screen_functions_t *
518 get_screen_search(void)
520   static screen_functions_t functions;
522   memset(&functions, 0, sizeof(screen_functions_t));
523   functions.init   = init;
524   functions.exit   = quit;
525   functions.open   = open;
526   functions.close  = close;
527   functions.resize = resize;
528   functions.paint  = paint;
529   functions.update = update;
530   functions.cmd    = search_cmd;
531   functions.get_lw = get_filelist_window;
532   functions.get_title = get_title;
534   return &functions;
538 #endif /* ENABLE_SEARCH_SCREEN */