Code

include cleanup
[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 "config.h"
23 #ifndef DISABLE_SEARCH_SCREEN
24 #include "ncmpc.h"
25 #include "options.h"
26 #include "support.h"
27 #include "mpdclient.h"
28 #include "strfsong.h"
29 #include "command.h"
30 #include "screen.h"
31 #include "utils.h"
32 #include "screen_utils.h"
33 #include "screen_browse.h"
35 #include <ctype.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <glib.h>
39 #include <ncurses.h>
41 /* new search stuff with qball's libmpdclient */
42 #define FUTURE
45 #ifdef FUTURE
47 extern gint mpdclient_finish_command(mpdclient_t *c);
49 typedef struct {
50         int id;
51         const char *name;
52         const 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 #define SEARCH_ARTIST_TITLE 999
96 typedef struct {
97         int table;
98         const char *label;
99 } search_type_t;
101 static search_type_t mode[] = {
102   { MPD_TABLE_TITLE,     N_("Title") },
103   { MPD_TABLE_ARTIST,    N_("Artist") },
104   { MPD_TABLE_ALBUM,     N_("Album") },
105   { MPD_TABLE_FILENAME,  N_("Filename") },
106   { SEARCH_ARTIST_TITLE, N_("Artist + Title") },
107   { 0, NULL }
108 };
110 static list_window_t *lw = NULL;
111 static mpdclient_filelist_t *filelist = NULL;
112 static GList *search_history = NULL;
113 static gchar *pattern = NULL;
114 static gboolean advanced_search_mode = FALSE;
117 /* search info */
118 static const char *
119 lw_search_help_callback(int index, int *highlight, void *data)
121         int text_rows;
122         static const char *text[] = {
123                 "Quick  - just enter a string and ncmpc will search according",
124                 "               to the current search mode (displayed above).",
125                 "",
126                 "Advanced  -  <tag>:<search term> [<tag>:<search term>...]",
127                 "                       Example: artist:radiohead album:pablo honey",
128                 "",
129                 "                  avalible tags: artist, album, title, track,",
130                 "                  name, genre, date composer, performer, comment, file",
131                 "",
132                 NULL
133         };
135         text_rows=0;
136         while (text[text_rows])
137                 text_rows++;
139         if (index < text_rows)
140                 return text[index];
141         return NULL;
144 /* the playlist have been updated -> fix highlights */
145 static void 
146 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
148   if( filelist==NULL )
149     return;
150   D("screen_search.c> playlist_callback() [%d]\n", event);
151   switch(event)
152     {
153     case PLAYLIST_EVENT_CLEAR:
154       clear_highlights(filelist);
155       break;
156     default:
157       sync_highlights(c, filelist);
158       break;
159     }
162 /* sanity check search mode value */
163 static void
164 search_check_mode(void)
166   int max = 0;
168   while( mode[max].label != NULL )
169     max++;
170   if( options.search_mode<0 )
171     options.search_mode = 0;
172   else if( options.search_mode>=max )
173     options.search_mode = max-1;
176 static void
177 search_clear(screen_t *screen, mpdclient_t *c, gboolean clear_pattern)
179   if( filelist )
180     {
181       mpdclient_remove_playlist_callback(c, playlist_changed_callback);
182       filelist = mpdclient_filelist_free(filelist);
183     }
184   if( clear_pattern && pattern )
185     {
186       g_free(pattern);
187       pattern = NULL;
188     }
191 #ifdef FUTURE
192 static mpdclient_filelist_t *
193 filelist_search(mpdclient_t *c, int exact_match, int table, gchar *pattern)
195   mpdclient_filelist_t *list, *list2;
197   if( table == SEARCH_ARTIST_TITLE )
198     {
199       list = mpdclient_filelist_search(c, FALSE, MPD_TABLE_ARTIST, pattern);
200       list2 = mpdclient_filelist_search(c, FALSE, MPD_TABLE_TITLE, pattern);
202       list->length += list2->length;
203       list->list = g_list_concat(list->list, list2->list);
204       list->list = g_list_sort(list->list, compare_filelistentry_format);
205       list->updated = TRUE;
206     }
207   else
208     {
209       list = mpdclient_filelist_search(c, FALSE, table, pattern);
210     }
212   return list;
215 /*-----------------------------------------------------------------------
216  * NOTE: This code exists to test a new search ui,
217  *       Its ugly and MUST be redesigned before the next release!
218  *-----------------------------------------------------------------------
219  */
220 static mpdclient_filelist_t *
221 search_advanced_query(char *query, mpdclient_t *c)
223         int i,j;
224         char **strv;
225         int table[10];
226         char *arg[10];
227         mpdclient_filelist_t *filelist = NULL;
229         advanced_search_mode = FALSE;
230         if( g_strrstr(query, ":") == NULL )
231                 return NULL;
233         strv = g_strsplit_set(query, ": ", 0);
235         i=0;
236         while (strv[i]) {
237                 D("strv[%d] = \"%s\"\n", i, strv[i]);
238                 i++;
239         }
241         memset(table, 0, 10*sizeof(int));
242         memset(arg, 0, 10*sizeof(char *));
244         i=0;
245         j=0;
246         while (strv[i] && strlen(strv[i]) > 0 && i < 9) {
247                 D("strv[%d] = \"%s\"\n", i, strv[i]);
249                 int id = search_get_tag_id(strv[i]);
250                 if (id == -1) {
251                         if (table[j]) {
252                                 char *tmp = arg[j];
253                                 arg[j] = g_strdup_printf("%s %s", arg[j], strv[i]);
254                                 g_free(tmp);
255                         } else {
256                                 D("Bad search tag %s\n", strv[i]);
257                                 screen_status_printf(_("Bad search tag %s"), strv[i]);
258                         }
259                         i++;
260                 } else if (strv[i+1] == NULL || strlen(strv[i+1]) == 0) {
261                         D("No argument for search tag %s\n", strv[i]);
262                         screen_status_printf(_("No argument for search tag %s"), strv[i]);
263                         i++;
264                         //        j--;
265                         //table[j] = -1;
266                 } else {
267                         table[j] = id;
268                         arg[j] = locale_to_utf8(strv[i+1]); // FREE ME
269                         j++;
270                         table[j] = -1;
271                         arg[j] = NULL;
272                         i = i + 2;
273                         advanced_search_mode = TRUE;
274                 }
275         }
277         g_strfreev(strv);
280         if (advanced_search_mode && j > 0) {
281                 /*-----------------------------------------------------------------------
282                  * NOTE (again): This code exists to test a new search ui,
283                  *               Its ugly and MUST be redesigned before the next release!
284                  *             + the code below should live in mpdclient.c
285                  *-----------------------------------------------------------------------
286                  */
287                 /** stupid - but this is just a test...... (fulhack)  */
288                 mpd_startSearch(c->connection, FALSE);
290                 int iter;
291                 for(iter = 0; iter < 10; iter++) {
292                         mpd_addConstraintSearch(c->connection, table[iter], arg[iter]);
293                 }
295                 mpd_commitSearch(c->connection);
297                 filelist = g_malloc0(sizeof(mpdclient_filelist_t));
299                 mpd_InfoEntity *entity;
301                 while ((entity=mpd_getNextInfoEntity(c->connection)))  {
302                         filelist_entry_t *entry = g_malloc0(sizeof(filelist_entry_t));
304                         entry->entity = entity;
305                         filelist->list = g_list_append(filelist->list, (gpointer) entry);
306                         filelist->length++;
307                 }
309                 if (mpdclient_finish_command(c) && filelist)
310                         filelist = mpdclient_filelist_free(filelist);
312                 filelist->updated = TRUE;
313         }
315         i=0;
316         while( arg[i] )
317                 g_free(arg[i++]);
319         return filelist;
321 #else
322 #define search_advanced_query(pattern,c) (NULL)
323 #endif
325 static void
326 search_new(screen_t *screen, mpdclient_t *c)
328   search_clear(screen, c, TRUE);
329   
330   pattern = screen_readln(screen->status_window.w, 
331                           _("Search: "),
332                           NULL,
333                           &search_history,
334                           NULL);
336   if( pattern && strcmp(pattern,"")==0 )
337     {
338       g_free(pattern);
339       pattern=NULL;
340     }
341   
342   if( pattern==NULL )
343     {
344       list_window_reset(lw);
345       return;
346     }
348   if( !MPD_VERSION_LT(c, 0, 12, 0) )
349     filelist = search_advanced_query(pattern, c);
350   if( !advanced_search_mode && filelist==NULL )
351     filelist = filelist_search(c, 
352                                FALSE,
353                                mode[options.search_mode].table,
354                                pattern);
355   sync_highlights(c, filelist);
356   mpdclient_install_playlist_callback(c, playlist_changed_callback);
357   list_window_check_selected(lw, filelist->length);
362 static void
363 init(WINDOW *w, int cols, int rows)
365   lw = list_window_init(w, cols, rows);
368 static void
369 quit(void)
371   if( search_history )
372     string_list_free(search_history);
373   if( filelist )
374     filelist = mpdclient_filelist_free(filelist);
375   list_window_free(lw);
376   if( pattern )
377     g_free(pattern);
378   pattern = NULL;
381 static void
382 open(screen_t *screen, mpdclient_t *c)
384   //  if( pattern==NULL )
385   //    search_new(screen, c);
386   // else
387   screen_status_printf(_("Press %s for a new search"),
388                          get_key_names(CMD_SCREEN_SEARCH,0));
389   search_check_mode();
392 static void
393 resize(int cols, int rows)
395   lw->cols = cols;
396   lw->rows = rows;
399 static void
400 close(void)
404 static void 
405 paint(screen_t *screen, mpdclient_t *c)
407   lw->clear = 1;
408   
409   if( filelist )
410     {
411       lw->flags = 0;
412       list_window_paint(lw, browse_lw_callback, (void *) filelist);
413       filelist->updated = FALSE;
414     }
415   else
416     {
417       lw->flags = LW_HIDE_CURSOR;
418       list_window_paint(lw, lw_search_help_callback, NULL);
419       if( !MPD_VERSION_LT(c, 0, 12, 0) )
420         g_strdup_printf("Advanced search disabled (MPD version < 0.12.0"); 
421       //      wmove(lw->w, 0, 0);
422       //wclrtobot(lw->w);
423     }
424   wnoutrefresh(lw->w);
427 static void 
428 update(screen_t *screen, mpdclient_t *c)
430   if( filelist==NULL || filelist->updated )
431     {
432       paint(screen, c);
433       return;
434     }
435   list_window_paint(lw, browse_lw_callback, (void *) filelist);
436   wnoutrefresh(lw->w);
439 static const char *
440 get_title(char *str, size_t size)
442   if( advanced_search_mode && pattern )
443     g_snprintf(str, size, _("Search: %s"), pattern);
444   else if( pattern )
445     g_snprintf(str, size, 
446                _("Search: Results for %s [%s]"), 
447                pattern,
448                _(mode[options.search_mode].label));
449   else
450     g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
451                get_key_names(CMD_SCREEN_SEARCH,0),
452                _(mode[options.search_mode].label));
453                
454   return str;
457 static list_window_t *
458 get_filelist_window(void)
460         return lw;
463 static int 
464 search_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
466   switch(cmd)
467     {
468     case CMD_PLAY:
469        browse_handle_enter(screen, c, lw, filelist);
470       return 1;
472     case CMD_SELECT:
473       if( browse_handle_select(screen, c, lw, filelist) == 0 )
474         {
475           /* continue and select next item... */
476           cmd = CMD_LIST_NEXT;
477         }
478       /* call list_window_cmd to go to the next item */
479       return list_window_cmd(lw, filelist->length, cmd);
481     case CMD_SELECT_ALL:
482       browse_handle_select_all (screen, c, lw, filelist);
483       paint (screen, c);
484       return 0;
486     case CMD_SEARCH_MODE:
487       options.search_mode++;
488       if( mode[options.search_mode].label == NULL )
489         options.search_mode = 0;
490       screen_status_printf(_("Search mode: %s"), 
491                            _(mode[options.search_mode].label));
492       /* continue and update... */
493     case CMD_SCREEN_UPDATE:
494       if( pattern )
495         {
496           search_clear(screen, c, FALSE);
497           filelist = filelist_search(c, 
498                                      FALSE,
499                                      mode[options.search_mode].table,
500                                      pattern);
501           sync_highlights(c, filelist);
502         }
503       return 1;
505     case CMD_SCREEN_SEARCH:
506       search_new(screen, c);
507       return 1;
509     case CMD_CLEAR:
510       search_clear(screen, c, TRUE);
511       list_window_reset(lw);
512       return 1;
514     case CMD_LIST_FIND:
515     case CMD_LIST_RFIND:
516     case CMD_LIST_FIND_NEXT:
517     case CMD_LIST_RFIND_NEXT:
518       if( filelist )
519         return screen_find(screen,
520                            lw, filelist->length,
521                            cmd, browse_lw_callback, (void *) filelist);
522       else
523         return 1;
525     case CMD_MOUSE_EVENT:
526       return browse_handle_mouse_event(screen,c,lw,filelist);
528     default:
529       if( filelist )
530         return list_window_cmd(lw, filelist->length, cmd);
531     }
532   
533   return 0;
536 screen_functions_t *
537 get_screen_search(void)
539   static screen_functions_t functions;
541   memset(&functions, 0, sizeof(screen_functions_t));
542   functions.init   = init;
543   functions.exit   = quit;
544   functions.open   = open;
545   functions.close  = close;
546   functions.resize = resize;
547   functions.paint  = paint;
548   functions.update = update;
549   functions.cmd    = search_cmd;
550   functions.get_lw = get_filelist_window;
551   functions.get_title = get_title;
553   return &functions;
557 #endif /* ENABLE_SEARCH_SCREEN */