Code

spelling fixes by avuton
[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       mpd_InfoEntity *entity;
281       /** stupid - but this is just a test...... (fulhack)  */
282           int iter;
283           for(iter = 0; i < 10; i++)
284           {
285                   mpd_addConstraintSearch(c->connection, table[i], arg[i]);
286           }               
287                         
288           mpd_commitSearch(c->connection);
289           
290       filelist = g_malloc0(sizeof(mpdclient_filelist_t));
292       while( (entity=mpd_getNextInfoEntity(c->connection)) ) 
293         {
294           filelist_entry_t *entry = g_malloc0(sizeof(filelist_entry_t));
295       
296           entry->entity = entity;
297           filelist->list = g_list_append(filelist->list, (gpointer) entry);
298           filelist->length++;
299         }
300   
301       if( mpdclient_finish_command(c) && filelist )
302         filelist = mpdclient_filelist_free(filelist);
304       filelist->updated = TRUE;
305     } 
306   
307   i=0;
308   while( arg[i] )
309     g_free(arg[i++]);
311   return filelist;
313 #else
314 #define search_advanced_query(pattern,c) (NULL)
315 #endif
317 static void
318 search_new(screen_t *screen, mpdclient_t *c)
320   search_clear(screen, c, TRUE);
321   
322   pattern = screen_readln(screen->status_window.w, 
323                           _("Search: "),
324                           NULL,
325                           &search_history,
326                           NULL);
328   if( pattern && strcmp(pattern,"")==0 )
329     {
330       g_free(pattern);
331       pattern=NULL;
332     }
333   
334   if( pattern==NULL )
335     {
336       list_window_reset(lw);
337       return;
338     }
340   if( !MPD_VERSION_LT(c, 0, 12, 0) )
341     filelist = search_advanced_query(pattern, c);
342   if( !advanced_search_mode && filelist==NULL )
343     filelist = mpdclient_filelist_search(c, 
344                                          FALSE,
345                                          mode[options.search_mode].table,
346                                          pattern);
347   sync_highlights(c, filelist);
348   mpdclient_install_playlist_callback(c, playlist_changed_callback);
349   list_window_check_selected(lw, filelist->length);
354 static void
355 init(WINDOW *w, int cols, int rows)
357   lw = list_window_init(w, cols, rows);
360 static void
361 quit(void)
363   if( search_history )
364     string_list_free(search_history);
365   if( filelist )
366     filelist = mpdclient_filelist_free(filelist);
367   list_window_free(lw);
368   if( pattern )
369     g_free(pattern);
370   pattern = NULL;
373 static void
374 open(screen_t *screen, 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   lw->cols = cols;
388   lw->rows = rows;
391 static void
392 close(void)
396 static void 
397 paint(screen_t *screen, mpdclient_t *c)
399   lw->clear = 1;
400   
401   if( filelist )
402     {
403       lw->flags = 0;
404       list_window_paint(lw, browse_lw_callback, (void *) filelist);
405       filelist->updated = FALSE;
406     }
407   else
408     {
409       lw->flags = LW_HIDE_CURSOR;
410       list_window_paint(lw, lw_search_help_callback, NULL);
411       if( !MPD_VERSION_LT(c, 0, 12, 0) )
412         g_strdup_printf("Advanced search disabled (MPD version < 0.12.0"); 
413       //      wmove(lw->w, 0, 0);
414       //wclrtobot(lw->w);
415     }
416   wnoutrefresh(lw->w);
419 static void 
420 update(screen_t *screen, mpdclient_t *c)
422   if( filelist==NULL || filelist->updated )
423     {
424       paint(screen, c);
425       return;
426     }
427   list_window_paint(lw, browse_lw_callback, (void *) filelist);
428   wnoutrefresh(lw->w);
431 static char *
432 get_title(char *str, size_t size)
434   if( advanced_search_mode && pattern )
435     g_snprintf(str, size, _("Search: %s"), pattern);
436   else if( pattern )
437     g_snprintf(str, size, 
438                _("Search: Results for %s [%s]"), 
439                pattern,
440                _(mode[options.search_mode].label));
441   else
442     g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
443                get_key_names(CMD_SCREEN_SEARCH,0),
444                _(mode[options.search_mode].label));
445                
446   return str;
449 static list_window_t *
450 get_filelist_window()
452   return lw;
455 static int 
456 search_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
458   switch(cmd)
459     {
460     case CMD_PLAY:
461        browse_handle_enter(screen, c, lw, filelist);
462       return 1;
464     case CMD_SELECT:
465       if( browse_handle_select(screen, c, lw, filelist) == 0 )
466         {
467           /* continue and select next item... */
468           cmd = CMD_LIST_NEXT;
469         }
470       return 1;
472     case CMD_SEARCH_MODE:
473       options.search_mode++;
474       if( mode[options.search_mode].label == NULL )
475         options.search_mode = 0;
476       screen_status_printf(_("Search mode: %s"), 
477                            _(mode[options.search_mode].label));
478       /* continue and update... */
479     case CMD_SCREEN_UPDATE:
480       if( pattern )
481         {
482           search_clear(screen, c, FALSE);
483           filelist = mpdclient_filelist_search(c, 
484                                                FALSE,
485                                                mode[options.search_mode].table,
486                                                pattern);
487           sync_highlights(c, filelist);
488         }
489       return 1;
491     case CMD_SCREEN_SEARCH:
492       search_new(screen, c);
493       return 1;
495     case CMD_CLEAR:
496       search_clear(screen, c, TRUE);
497       list_window_reset(lw);
498       return 1;
500     case CMD_LIST_FIND:
501     case CMD_LIST_RFIND:
502     case CMD_LIST_FIND_NEXT:
503     case CMD_LIST_RFIND_NEXT:
504       if( filelist )
505         return screen_find(screen, c, 
506                            lw, filelist->length,
507                            cmd, browse_lw_callback, (void *) filelist);
508       else
509         return 1;
511     case CMD_MOUSE_EVENT:
512       return browse_handle_mouse_event(screen,c,lw,filelist);
514     default:
515       if( filelist )
516         return list_window_cmd(lw, filelist->length, cmd);
517     }
518   
519   return 0;
522 screen_functions_t *
523 get_screen_search(void)
525   static screen_functions_t functions;
527   memset(&functions, 0, sizeof(screen_functions_t));
528   functions.init   = init;
529   functions.exit   = quit;
530   functions.open   = open;
531   functions.close  = close;
532   functions.resize = resize;
533   functions.paint  = paint;
534   functions.update = update;
535   functions.cmd    = search_cmd;
536   functions.get_lw = get_filelist_window;
537   functions.get_title = get_title;
539   return &functions;
543 #endif /* ENABLE_SEARCH_SCREEN */