Code

Daniel rocked the house
[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     "Welcome to ncmpc's search screen - SVN version.",
121     "",
122     "Quick search - just enter a string and ncmpc will search according",
123     "               to the current search mode (displayed above).",
124     "",
125     "Advanced - bla bla bla.... syntax below",
126     ""
127     " <tag>:<search term> [<tag>:<search term>...]",
128     "",
129     "Example: artist:radiohead album:pablo honey",
130     "",
131     "##### SOMEONE - Write a proper help text, please! #####",
132     "",
133     "avalible tags: artist, album, title, track, name, genre, date",
134     "               composer, performer, comment, file",
135     "",
136     NULL
137   };
139   text_rows=0;
140   while( text[text_rows] )
141     text_rows++;
142   
143   if( index < text_rows )
144     return text[index];
145   return NULL;
148 /* the playlist have been updated -> fix highlights */
149 static void 
150 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
152   if( filelist==NULL )
153     return;
154   D("screen_search.c> playlist_callback() [%d]\n", event);
155   switch(event)
156     {
157     case PLAYLIST_EVENT_CLEAR:
158       clear_highlights(filelist);
159       break;
160     default:
161       sync_highlights(c, filelist);
162       break;
163     }
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(screen_t *screen, mpdclient_t *c, gboolean clear_pattern)
183   if( filelist )
184     {
185       mpdclient_remove_playlist_callback(c, playlist_changed_callback);
186       filelist = mpdclient_filelist_free(filelist);
187     }
188   if( clear_pattern && pattern )
189     {
190       g_free(pattern);
191       pattern = NULL;
192     }
195 #ifdef FUTURE
196 /*-----------------------------------------------------------------------
197  * NOTE: This code exists to test a new search ui,
198  *       Its ugly and MUST be redesigned before the next release!
199  *-----------------------------------------------------------------------
200  */
201 static mpdclient_filelist_t *
202 search_advanced_query(char *query, mpdclient_t *c)
204   int i,j;
205   char **strv;
206   int table[10];
207   char *arg[10];
208   mpdclient_filelist_t *filelist = NULL;
210   advanced_search_mode = FALSE;
211   if( g_strrstr(query, ":") == NULL )
212     return NULL;
213   
214   strv = g_strsplit_set(query, ": ", 0);
216   i=0;
217   while( strv[i] )
218     {
219       D("strv[%d] = \"%s\"\n", i, strv[i]);
220       i++;
221     }
223   memset(table, 0, 10*sizeof(int));
224   memset(arg,   0, 10*sizeof(char *));
226   i=0;
227   j=0;
228   while( strv[i] && strlen(strv[i])>0 && i<9 )
229     {
230       D("strv[%d] = \"%s\"\n", i, strv[i]);
232       int id = search_get_tag_id(strv[i]);
233       if( id==-1 )
234         {
235           if( table[j] )
236             {
237               char *tmp = arg[j];
238               arg[j] = g_strdup_printf("%s %s", arg[j], strv[i]);
239               g_free(tmp);
240             }
241           else
242             {
243               D("Bad search tag %s\n", strv[i]);
244               screen_status_printf(_("Bad search tag %s"), strv[i]);
245             }
246           i++;
247         }
248       else if( strv[i+1] == NULL || strlen(strv[i+1])==0 )
249         {
250           D("No argument for search tag %s\n", strv[i]);
251           screen_status_printf(_("No argument for search tag %s"), strv[i]);
252           i++;
253           //      j--;
254           //table[j] = -1;
255         }
256       else
257         {
258           table[j] = id;
259           arg[j] = locale_to_utf8(strv[i+1]); // FREE ME
260           j++;
261           table[j] = -1;
262           arg[j] = NULL;
263           i = i + 2;
264           advanced_search_mode = TRUE;
265         }     
266     }
268   g_strfreev(strv);
271   if( advanced_search_mode && j>0 )
272     {
273       /*-----------------------------------------------------------------------
274        * NOTE (again): This code exists to test a new search ui,
275        *               Its ugly and MUST be redesigned before the next release!
276        *             + the code below should live in mpdclient.c
277        *-----------------------------------------------------------------------
278        */
279       /** stupid - but this is just a test...... (fulhack)  */
280           mpd_startSearch(c->connection, FALSE);
282           int iter;
283           for(iter = 0; iter < 10; iter++)
284           {
285                   mpd_addConstraintSearch(c->connection, table[iter], arg[iter]);
286           }               
287                         
288           mpd_commitSearch(c->connection);
289           
290       filelist = g_malloc0(sizeof(mpdclient_filelist_t));
292       mpd_InfoEntity *entity;
294       while( (entity=mpd_getNextInfoEntity(c->connection)) ) 
295         {
296           filelist_entry_t *entry = g_malloc0(sizeof(filelist_entry_t));
297       
298           entry->entity = entity;
299           filelist->list = g_list_append(filelist->list, (gpointer) entry);
300           filelist->length++;
301         }
302   
303       if( mpdclient_finish_command(c) && filelist )
304         filelist = mpdclient_filelist_free(filelist);
306       filelist->updated = TRUE;
307     } 
308   
309   i=0;
310   while( arg[i] )
311     g_free(arg[i++]);
313   return filelist;
315 #else
316 #define search_advanced_query(pattern,c) (NULL)
317 #endif
319 static void
320 search_new(screen_t *screen, mpdclient_t *c)
322   search_clear(screen, c, TRUE);
323   
324   pattern = screen_readln(screen->status_window.w, 
325                           _("Search: "),
326                           NULL,
327                           &search_history,
328                           NULL);
330   if( pattern && strcmp(pattern,"")==0 )
331     {
332       g_free(pattern);
333       pattern=NULL;
334     }
335   
336   if( pattern==NULL )
337     {
338       list_window_reset(lw);
339       return;
340     }
342   if( !MPD_VERSION_LT(c, 0, 12, 0) )
343     filelist = search_advanced_query(pattern, c);
344   if( !advanced_search_mode && filelist==NULL )
345     filelist = mpdclient_filelist_search(c, 
346                                          FALSE,
347                                          mode[options.search_mode].table,
348                                          pattern);
349   sync_highlights(c, filelist);
350   mpdclient_install_playlist_callback(c, playlist_changed_callback);
351   list_window_check_selected(lw, filelist->length);
356 static void
357 init(WINDOW *w, int cols, int rows)
359   lw = list_window_init(w, cols, rows);
362 static void
363 quit(void)
365   if( search_history )
366     string_list_free(search_history);
367   if( filelist )
368     filelist = mpdclient_filelist_free(filelist);
369   list_window_free(lw);
370   if( pattern )
371     g_free(pattern);
372   pattern = NULL;
375 static void
376 open(screen_t *screen, mpdclient_t *c)
378   //  if( pattern==NULL )
379   //    search_new(screen, c);
380   // else
381   screen_status_printf(_("Press %s for a new search"),
382                          get_key_names(CMD_SCREEN_SEARCH,0));
383   search_check_mode();
386 static void
387 resize(int cols, int rows)
389   lw->cols = cols;
390   lw->rows = rows;
393 static void
394 close(void)
398 static void 
399 paint(screen_t *screen, mpdclient_t *c)
401   lw->clear = 1;
402   
403   if( filelist )
404     {
405       lw->flags = 0;
406       list_window_paint(lw, browse_lw_callback, (void *) filelist);
407       filelist->updated = FALSE;
408     }
409   else
410     {
411       lw->flags = LW_HIDE_CURSOR;
412       list_window_paint(lw, lw_search_help_callback, NULL);
413       if( !MPD_VERSION_LT(c, 0, 12, 0) )
414         g_strdup_printf("Advanced search disabled (MPD version < 0.12.0"); 
415       //      wmove(lw->w, 0, 0);
416       //wclrtobot(lw->w);
417     }
418   wnoutrefresh(lw->w);
421 static void 
422 update(screen_t *screen, mpdclient_t *c)
424   if( filelist==NULL || filelist->updated )
425     {
426       paint(screen, c);
427       return;
428     }
429   list_window_paint(lw, browse_lw_callback, (void *) filelist);
430   wnoutrefresh(lw->w);
433 static char *
434 get_title(char *str, size_t size)
436   if( advanced_search_mode && pattern )
437     g_snprintf(str, size, _("Search: %s"), pattern);
438   else if( pattern )
439     g_snprintf(str, size, 
440                _("Search: Results for %s [%s]"), 
441                pattern,
442                _(mode[options.search_mode].label));
443   else
444     g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
445                get_key_names(CMD_SCREEN_SEARCH,0),
446                _(mode[options.search_mode].label));
447                
448   return str;
451 static list_window_t *
452 get_filelist_window()
454   return lw;
457 static int 
458 search_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
460   switch(cmd)
461     {
462     case CMD_PLAY:
463        browse_handle_enter(screen, c, lw, filelist);
464       return 1;
466     case CMD_SELECT:
467       if( browse_handle_select(screen, c, lw, filelist) == 0 )
468         {
469           /* continue and select next item... */
470           cmd = CMD_LIST_NEXT;
471         }
472       return 1;
474     case CMD_SEARCH_MODE:
475       options.search_mode++;
476       if( mode[options.search_mode].label == NULL )
477         options.search_mode = 0;
478       screen_status_printf(_("Search mode: %s"), 
479                            _(mode[options.search_mode].label));
480       /* continue and update... */
481     case CMD_SCREEN_UPDATE:
482       if( pattern )
483         {
484           search_clear(screen, c, FALSE);
485           filelist = mpdclient_filelist_search(c, 
486                                                FALSE,
487                                                mode[options.search_mode].table,
488                                                pattern);
489           sync_highlights(c, filelist);
490         }
491       return 1;
493     case CMD_SCREEN_SEARCH:
494       search_new(screen, c);
495       return 1;
497     case CMD_CLEAR:
498       search_clear(screen, c, TRUE);
499       list_window_reset(lw);
500       return 1;
502     case CMD_LIST_FIND:
503     case CMD_LIST_RFIND:
504     case CMD_LIST_FIND_NEXT:
505     case CMD_LIST_RFIND_NEXT:
506       if( filelist )
507         return screen_find(screen, c, 
508                            lw, filelist->length,
509                            cmd, browse_lw_callback, (void *) filelist);
510       else
511         return 1;
513     case CMD_MOUSE_EVENT:
514       return browse_handle_mouse_event(screen,c,lw,filelist);
516     default:
517       if( filelist )
518         return list_window_cmd(lw, filelist->length, cmd);
519     }
520   
521   return 0;
524 screen_functions_t *
525 get_screen_search(void)
527   static screen_functions_t functions;
529   memset(&functions, 0, sizeof(screen_functions_t));
530   functions.init   = init;
531   functions.exit   = quit;
532   functions.open   = open;
533   functions.close  = close;
534   functions.resize = resize;
535   functions.paint  = paint;
536   functions.update = update;
537   functions.cmd    = search_cmd;
538   functions.get_lw = get_filelist_window;
539   functions.get_title = get_title;
541   return &functions;
545 #endif /* ENABLE_SEARCH_SCREEN */