Code

fix function prototypes
[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         int id;
50         const char *name;
51         const char *localname;
52 } search_tag_t;
54 static search_tag_t search_tag[] = {
55   { MPD_TAG_ITEM_ARTIST,   "artist",    N_("artist") },
56   { MPD_TAG_ITEM_ALBUM,    "album",     N_("album") },
57   { MPD_TAG_ITEM_TITLE,    "title",     N_("title") },
58   { MPD_TAG_ITEM_TRACK,    "track",     N_("track") },
59   { MPD_TAG_ITEM_NAME,     "name",      N_("name") },
60   { MPD_TAG_ITEM_GENRE,    "genre",     N_("genre") },
61   { MPD_TAG_ITEM_DATE,     "date",      N_("date") },
62   { MPD_TAG_ITEM_COMPOSER, "composer",  N_("composer") },
63   { MPD_TAG_ITEM_PERFORMER,"performer", N_("performer") },
64   { MPD_TAG_ITEM_COMMENT,  "comment",   N_("comment") },
65   { MPD_TAG_ITEM_FILENAME, "filename",  N_("file") },
66   { -1,                    NULL,        NULL }
67 };
69 static int
70 search_get_tag_id(char *name)
71 {
72   int i;
74   i=0;
75   while( search_tag[i].name )
76     {
77       if( strcasecmp(search_tag[i].name, name)==0 || 
78           strcasecmp(search_tag[i].localname, name)==0 )
79         return search_tag[i].id;
80       i++;
81     }
82   return -1;
83 }
85 #endif
88 #define SEARCH_TITLE    0
89 #define SEARCH_ARTIST   1
90 #define SEARCH_ALBUM    2
91 #define SEARCH_FILE     3
93 #define SEARCH_ARTIST_TITLE 999
95 typedef struct {
96         int table;
97         const char *label;
98 } search_type_t;
100 static search_type_t mode[] = {
101   { MPD_TABLE_TITLE,     N_("Title") },
102   { MPD_TABLE_ARTIST,    N_("Artist") },
103   { MPD_TABLE_ALBUM,     N_("Album") },
104   { MPD_TABLE_FILENAME,  N_("Filename") },
105   { SEARCH_ARTIST_TITLE, N_("Artist + Title") },
106   { 0, NULL }
107 };
109 static list_window_t *lw = NULL;
110 static mpdclient_filelist_t *filelist = NULL;
111 static GList *search_history = NULL;
112 static gchar *pattern = NULL;
113 static gboolean advanced_search_mode = FALSE;
116 /* search info */
117 static const char *
118 lw_search_help_callback(int index, int *highlight, void *data)
120         int text_rows;
121         static const char *text[] = {
122                 "Quick  - just enter a string and ncmpc will search according",
123                 "               to the current search mode (displayed above).",
124                 "",
125                 "Advanced  -  <tag>:<search term> [<tag>:<search term>...]",
126                 "                       Example: artist:radiohead album:pablo honey",
127                 "",
128                 "                  avalible tags: artist, album, title, track,",
129                 "                  name, genre, date composer, performer, comment, file",
130                 "",
131                 NULL
132         };
134         text_rows=0;
135         while (text[text_rows])
136                 text_rows++;
138         if (index < text_rows)
139                 return text[index];
140         return NULL;
143 /* the playlist have been updated -> fix highlights */
144 static void 
145 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
147   if( filelist==NULL )
148     return;
149   D("screen_search.c> playlist_callback() [%d]\n", event);
150   switch(event)
151     {
152     case PLAYLIST_EVENT_CLEAR:
153       clear_highlights(filelist);
154       break;
155     default:
156       sync_highlights(c, filelist);
157       break;
158     }
161 /* sanity check search mode value */
162 static void
163 search_check_mode(void)
165   int max = 0;
167   while( mode[max].label != NULL )
168     max++;
169   if( options.search_mode<0 )
170     options.search_mode = 0;
171   else if( options.search_mode>=max )
172     options.search_mode = max-1;
175 static void
176 search_clear(screen_t *screen, mpdclient_t *c, gboolean clear_pattern)
178   if( filelist )
179     {
180       mpdclient_remove_playlist_callback(c, playlist_changed_callback);
181       filelist = mpdclient_filelist_free(filelist);
182     }
183   if( clear_pattern && pattern )
184     {
185       g_free(pattern);
186       pattern = NULL;
187     }
190 #ifdef FUTURE
191 static mpdclient_filelist_t *
192 filelist_search(mpdclient_t *c, int exact_match, int table, gchar *pattern)
194   mpdclient_filelist_t *list, *list2;
196   if( table == SEARCH_ARTIST_TITLE )
197     {
198       list = mpdclient_filelist_search(c, FALSE, MPD_TABLE_ARTIST, pattern);
199       list2 = mpdclient_filelist_search(c, FALSE, MPD_TABLE_TITLE, pattern);
201       list->length += list2->length;
202       list->list = g_list_concat(list->list, list2->list);
203       list->list = g_list_sort(list->list, compare_filelistentry_format);
204       list->updated = TRUE;
205     }
206   else
207     {
208       list = mpdclient_filelist_search(c, FALSE, table, pattern);
209     }
211   return list;
214 /*-----------------------------------------------------------------------
215  * NOTE: This code exists to test a new search ui,
216  *       Its ugly and MUST be redesigned before the next release!
217  *-----------------------------------------------------------------------
218  */
219 static mpdclient_filelist_t *
220 search_advanced_query(char *query, mpdclient_t *c)
222         int i,j;
223         char **strv;
224         int table[10];
225         char *arg[10];
226         mpdclient_filelist_t *filelist = NULL;
228         advanced_search_mode = FALSE;
229         if( g_strrstr(query, ":") == NULL )
230                 return NULL;
232         strv = g_strsplit_set(query, ": ", 0);
234         i=0;
235         while (strv[i]) {
236                 D("strv[%d] = \"%s\"\n", i, strv[i]);
237                 i++;
238         }
240         memset(table, 0, 10*sizeof(int));
241         memset(arg, 0, 10*sizeof(char *));
243         i=0;
244         j=0;
245         while (strv[i] && strlen(strv[i]) > 0 && i < 9) {
246                 D("strv[%d] = \"%s\"\n", i, strv[i]);
248                 int id = search_get_tag_id(strv[i]);
249                 if (id == -1) {
250                         if (table[j]) {
251                                 char *tmp = arg[j];
252                                 arg[j] = g_strdup_printf("%s %s", arg[j], strv[i]);
253                                 g_free(tmp);
254                         } else {
255                                 D("Bad search tag %s\n", strv[i]);
256                                 screen_status_printf(_("Bad search tag %s"), strv[i]);
257                         }
258                         i++;
259                 } else if (strv[i+1] == NULL || strlen(strv[i+1]) == 0) {
260                         D("No argument for search tag %s\n", strv[i]);
261                         screen_status_printf(_("No argument for search tag %s"), strv[i]);
262                         i++;
263                         //        j--;
264                         //table[j] = -1;
265                 } else {
266                         table[j] = id;
267                         arg[j] = locale_to_utf8(strv[i+1]); // FREE ME
268                         j++;
269                         table[j] = -1;
270                         arg[j] = NULL;
271                         i = i + 2;
272                         advanced_search_mode = TRUE;
273                 }
274         }
276         g_strfreev(strv);
279         if (advanced_search_mode && j > 0) {
280                 /*-----------------------------------------------------------------------
281                  * NOTE (again): This code exists to test a new search ui,
282                  *               Its ugly and MUST be redesigned before the next release!
283                  *             + the code below should live in mpdclient.c
284                  *-----------------------------------------------------------------------
285                  */
286                 /** stupid - but this is just a test...... (fulhack)  */
287                 mpd_startSearch(c->connection, FALSE);
289                 int iter;
290                 for(iter = 0; iter < 10; iter++) {
291                         mpd_addConstraintSearch(c->connection, table[iter], arg[iter]);
292                 }
294                 mpd_commitSearch(c->connection);
296                 filelist = g_malloc0(sizeof(mpdclient_filelist_t));
298                 mpd_InfoEntity *entity;
300                 while ((entity=mpd_getNextInfoEntity(c->connection)))  {
301                         filelist_entry_t *entry = g_malloc0(sizeof(filelist_entry_t));
303                         entry->entity = entity;
304                         filelist->list = g_list_append(filelist->list, (gpointer) entry);
305                         filelist->length++;
306                 }
308                 if (mpdclient_finish_command(c) && filelist)
309                         filelist = mpdclient_filelist_free(filelist);
311                 filelist->updated = TRUE;
312         }
314         i=0;
315         while( arg[i] )
316                 g_free(arg[i++]);
318         return filelist;
320 #else
321 #define search_advanced_query(pattern,c) (NULL)
322 #endif
324 static void
325 search_new(screen_t *screen, mpdclient_t *c)
327   search_clear(screen, c, TRUE);
328   
329   pattern = screen_readln(screen->status_window.w, 
330                           _("Search: "),
331                           NULL,
332                           &search_history,
333                           NULL);
335   if( pattern && strcmp(pattern,"")==0 )
336     {
337       g_free(pattern);
338       pattern=NULL;
339     }
340   
341   if( pattern==NULL )
342     {
343       list_window_reset(lw);
344       return;
345     }
347   if( !MPD_VERSION_LT(c, 0, 12, 0) )
348     filelist = search_advanced_query(pattern, c);
349   if( !advanced_search_mode && filelist==NULL )
350     filelist = filelist_search(c, 
351                                FALSE,
352                                mode[options.search_mode].table,
353                                pattern);
354   sync_highlights(c, filelist);
355   mpdclient_install_playlist_callback(c, playlist_changed_callback);
356   list_window_check_selected(lw, filelist->length);
361 static void
362 init(WINDOW *w, int cols, int rows)
364   lw = list_window_init(w, cols, rows);
367 static void
368 quit(void)
370   if( search_history )
371     string_list_free(search_history);
372   if( filelist )
373     filelist = mpdclient_filelist_free(filelist);
374   list_window_free(lw);
375   if( pattern )
376     g_free(pattern);
377   pattern = NULL;
380 static void
381 open(screen_t *screen, mpdclient_t *c)
383   //  if( pattern==NULL )
384   //    search_new(screen, c);
385   // else
386   screen_status_printf(_("Press %s for a new search"),
387                          get_key_names(CMD_SCREEN_SEARCH,0));
388   search_check_mode();
391 static void
392 resize(int cols, int rows)
394   lw->cols = cols;
395   lw->rows = rows;
398 static void
399 close(void)
403 static void 
404 paint(screen_t *screen, mpdclient_t *c)
406   lw->clear = 1;
407   
408   if( filelist )
409     {
410       lw->flags = 0;
411       list_window_paint(lw, browse_lw_callback, (void *) filelist);
412       filelist->updated = FALSE;
413     }
414   else
415     {
416       lw->flags = LW_HIDE_CURSOR;
417       list_window_paint(lw, lw_search_help_callback, NULL);
418       if( !MPD_VERSION_LT(c, 0, 12, 0) )
419         g_strdup_printf("Advanced search disabled (MPD version < 0.12.0"); 
420       //      wmove(lw->w, 0, 0);
421       //wclrtobot(lw->w);
422     }
423   wnoutrefresh(lw->w);
426 static void 
427 update(screen_t *screen, mpdclient_t *c)
429   if( filelist==NULL || filelist->updated )
430     {
431       paint(screen, c);
432       return;
433     }
434   list_window_paint(lw, browse_lw_callback, (void *) filelist);
435   wnoutrefresh(lw->w);
438 static const char *
439 get_title(char *str, size_t size)
441   if( advanced_search_mode && pattern )
442     g_snprintf(str, size, _("Search: %s"), pattern);
443   else if( pattern )
444     g_snprintf(str, size, 
445                _("Search: Results for %s [%s]"), 
446                pattern,
447                _(mode[options.search_mode].label));
448   else
449     g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
450                get_key_names(CMD_SCREEN_SEARCH,0),
451                _(mode[options.search_mode].label));
452                
453   return str;
456 static list_window_t *
457 get_filelist_window(void)
459         return lw;
462 static int 
463 search_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
465   switch(cmd)
466     {
467     case CMD_PLAY:
468        browse_handle_enter(screen, c, lw, filelist);
469       return 1;
471     case CMD_SELECT:
472       if( browse_handle_select(screen, c, lw, filelist) == 0 )
473         {
474           /* continue and select next item... */
475           cmd = CMD_LIST_NEXT;
476         }
477       /* call list_window_cmd to go to the next item */
478       return list_window_cmd(lw, filelist->length, cmd);
480     case CMD_SELECT_ALL:
481       browse_handle_select_all (screen, c, lw, filelist);
482       paint (screen, c);
483       return 0;
485     case CMD_SEARCH_MODE:
486       options.search_mode++;
487       if( mode[options.search_mode].label == NULL )
488         options.search_mode = 0;
489       screen_status_printf(_("Search mode: %s"), 
490                            _(mode[options.search_mode].label));
491       /* continue and update... */
492     case CMD_SCREEN_UPDATE:
493       if( pattern )
494         {
495           search_clear(screen, c, FALSE);
496           filelist = filelist_search(c, 
497                                      FALSE,
498                                      mode[options.search_mode].table,
499                                      pattern);
500           sync_highlights(c, filelist);
501         }
502       return 1;
504     case CMD_SCREEN_SEARCH:
505       search_new(screen, c);
506       return 1;
508     case CMD_CLEAR:
509       search_clear(screen, c, TRUE);
510       list_window_reset(lw);
511       return 1;
513     case CMD_LIST_FIND:
514     case CMD_LIST_RFIND:
515     case CMD_LIST_FIND_NEXT:
516     case CMD_LIST_RFIND_NEXT:
517       if( filelist )
518         return screen_find(screen,
519                            lw, filelist->length,
520                            cmd, browse_lw_callback, (void *) filelist);
521       else
522         return 1;
524     case CMD_MOUSE_EVENT:
525       return browse_handle_mouse_event(screen,c,lw,filelist);
527     default:
528       if( filelist )
529         return list_window_cmd(lw, filelist->length, cmd);
530     }
531   
532   return 0;
535 screen_functions_t *
536 get_screen_search(void)
538   static screen_functions_t functions;
540   memset(&functions, 0, sizeof(screen_functions_t));
541   functions.init   = init;
542   functions.exit   = quit;
543   functions.open   = open;
544   functions.close  = close;
545   functions.resize = resize;
546   functions.paint  = paint;
547   functions.update = update;
548   functions.cmd    = search_cmd;
549   functions.get_lw = get_filelist_window;
550   functions.get_title = get_title;
552   return &functions;
556 #endif /* ENABLE_SEARCH_SCREEN */