Code

the updated sources
[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 typedef struct
47 {
48   int id;
49   char *name;
50   char *localname;
51 } search_tag_t;
53 static search_tag_t search_tag[] = {
54   { MPD_TAG_ITEM_ARTIST,   "artist",    N_("artist") },
55   { MPD_TAG_ITEM_ALBUM,    "album",     N_("album") },
56   { MPD_TAG_ITEM_TITLE,    "title",     N_("title") },
57   { MPD_TAG_ITEM_TRACK,    "track",     N_("track") },
58   { MPD_TAG_ITEM_NAME,     "name",      N_("name") },
59   { MPD_TAG_ITEM_GENRE,    "genre",     N_("genre") },
60   { MPD_TAG_ITEM_DATE,     "date",      N_("date") },
61   { MPD_TAG_ITEM_COMPOSER, "composer",  N_("composer") },
62   { MPD_TAG_ITEM_PERFORMER,"performer", N_("performer") },
63   { MPD_TAG_ITEM_COMMENT,  "comment",   N_("comment") },
64   { MPD_TAG_ITEM_FILENAME, "filename",  N_("file") },
65   { -1,                    NULL,        NULL }
66 };
68 static int
69 search_get_tag_id(char *name)
70 {
71   int i;
73   i=0;
74   while( search_tag[i].name )
75     {
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     }
81   return -1;
82 }
84 #endif
87 #define SEARCH_TITLE    0
88 #define SEARCH_ARTIST   1
89 #define SEARCH_ALBUM    2
90 #define SEARCH_FILE     3
92 typedef struct {
93   int table;
94   char *label;
95 } search_type_t;
97 static search_type_t mode[] = {
98   { MPD_TABLE_TITLE,    N_("Title") },
99   { MPD_TABLE_ARTIST,   N_("Artist") },
100   { MPD_TABLE_ALBUM,    N_("Album") },
101   { MPD_TABLE_FILENAME, N_("Filename") },
102   { 0, NULL }
103 };
105 static list_window_t *lw = NULL;
106 static mpdclient_filelist_t *filelist = NULL;
107 static GList *search_history = NULL;
108 static gchar *pattern = NULL;
109 static gboolean advanced_search_mode = FALSE;
112 /* search info */
113 static char *
114 lw_search_help_callback(int index, int *highlight, void *data)
116   int text_rows;
117   static char *text[] = {
118     "Welcome to ncmpc's search screen - SVN version.",
119     "",
120     "Quick search - just enter a string and ncmcp will search according",
121     "               to the current search mode (displayed above).",
122     "",
123     "Advanced - bla bla bla.... syntax below",
124     ""
125     " <tag>:<search term> [<tag>:<search term>...]",
126     "",
127     "Example: artist:radiohead album:pablo honey",
128     "",
129     "##### SOMEONE - Write a proper help text, please! #####",
130     "",
131     "avalible tags: artist, album, title, track, name, genre, date",
132     "               composer, performer, comment, file",
133     "",
134     NULL
135   };
137   text_rows=0;
138   while( text[text_rows] )
139     text_rows++;
140   
141   if( index < text_rows )
142     return text[index];
143   return NULL;
146 /* the playlist have been updated -> fix highlights */
147 static void 
148 playlist_changed_callback(mpdclient_t *c, int event, 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(screen_t *screen, mpdclient_t *c, gboolean clear_pattern)
181   if( filelist )
182     {
183       mpdclient_remove_playlist_callback(c, playlist_changed_callback);
184       filelist = mpdclient_filelist_free(filelist);
185     }
186   if( clear_pattern && pattern )
187     {
188       g_free(pattern);
189       pattern = NULL;
190     }
193 #ifdef FUTURE
194 /*-----------------------------------------------------------------------
195  * NOTE: This code exists to test a new search ui,
196  *       Its ugly and MUST be redesigned before the next release!
197  *-----------------------------------------------------------------------
198  */
199 static mpdclient_filelist_t *
200 search_advanced_query(char *query, mpdclient_t *c)
202   int i,j;
203   char **strv;
204   int table[10];
205   char *arg[10];
206   mpdclient_filelist_t *filelist = NULL;
208   advanced_search_mode = FALSE;
209   if( g_strrstr(query, ":") == NULL )
210     return NULL;
211   
212   strv = g_strsplit_set(query, ": ", 0);
214   i=0;
215   while( strv[i] )
216     {
217       D("strv[%d] = \"%s\"\n", i, strv[i]);
218       i++;
219     }
221   memset(table, 0, 10*sizeof(int));
222   memset(arg,   0, 10*sizeof(char *));
224   i=0;
225   j=0;
226   while( strv[i] && strlen(strv[i])>0 && i<9 )
227     {
228       D("strv[%d] = \"%s\"\n", i, strv[i]);
230       int id = search_get_tag_id(strv[i]);
231       if( id==-1 )
232         {
233           if( table[j] )
234             {
235               char *tmp = arg[j];
236               arg[j] = g_strdup_printf("%s %s", arg[j], strv[i]);
237               g_free(tmp);
238             }
239           else
240             {
241               D("Bad search tag %s\n", strv[i]);
242               screen_status_printf(_("Bad search tag %s"), strv[i]);
243             }
244           i++;
245         }
246       else if( strv[i+1] == NULL || strlen(strv[i+1])==0 )
247         {
248           D("No argument for search tag %s\n", strv[i]);
249           screen_status_printf(_("No argument for search tag %s"), strv[i]);
250           i++;
251           //      j--;
252           //table[j] = -1;
253         }
254       else
255         {
256           table[j] = id;
257           arg[j] = locale_to_utf8(strv[i+1]); // FREE ME
258           j++;
259           table[j] = -1;
260           arg[j] = NULL;
261           i = i + 2;
262           advanced_search_mode = TRUE;
263         }     
264     }
266   g_strfreev(strv);
269   if( advanced_search_mode && j>0 )
270     {
271       /*-----------------------------------------------------------------------
272        * NOTE (again): This code exists to test a new search ui,
273        *               Its ugly and MUST be redesigned before the next release!
274        *             + the code below should live in mpdclient.c
275        *-----------------------------------------------------------------------
276        */
277       mpd_InfoEntity *entity;
279       /** stupid - but this is just a test...... (fulhack)  */
280       mpd_sendSearchTagCommand(c->connection, 
281                                table[0], arg[0],
282                                table[1], arg[1],
283                                table[2], arg[2],
284                                table[3], arg[3],
285                                table[4], arg[4],
286                                table[5], arg[5],
287                                table[6], arg[6],
288                                table[7], arg[7],
289                                table[8], arg[8],
290                                table[9], arg[9]);
292       filelist = g_malloc0(sizeof(mpdclient_filelist_t));
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 */