2004-12-04 [paul] 0.9.12cvs182.3
[claws.git] / src / gtk / quicksearch.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2004 Hiroyuki Yamamoto & the Sylpheed-Claws team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
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
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <ctype.h>
25
26 #include <gtk/gtk.h>
27 #include <gdk/gdkkeysyms.h>
28
29 #include "intl.h"
30 #include "utils.h"
31 #include "menu.h"
32 #include "prefs_common.h"
33 #include "description_window.h"
34 #include "matcher.h"
35 #include "matcher_parser.h"
36 #include "quicksearch.h"
37
38 struct _QuickSearch
39 {
40         GtkWidget                       *hbox_search;
41         GtkWidget                       *search_type;
42         GtkWidget                       *search_type_opt;
43         GtkWidget                       *search_string_entry;
44         GtkWidget                       *search_description;
45
46         gboolean                         active;
47         gchar                           *search_string;
48         MatcherList                     *matcher_list;
49
50         QuickSearchExecuteCallback       callback;
51         gpointer                         callback_data;
52         gboolean                         running;
53         gboolean                         has_focus;
54 };
55
56 static void quicksearch_set_running(QuickSearch *quicksearch, gboolean run);
57
58 static void prepare_matcher(QuickSearch *quicksearch)
59 {
60         const gchar *search_string = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(quicksearch->search_string_entry)->entry));
61
62         if (quicksearch->matcher_list != NULL) {
63                 matcherlist_free(quicksearch->matcher_list);
64                 quicksearch->matcher_list = NULL;
65         }
66
67         if (search_string == NULL || search_string[0] == '\0') {
68                 quicksearch->active = FALSE;
69                 return;
70         }
71
72         if (prefs_common.summary_quicksearch_type == QUICK_SEARCH_EXTENDED) {
73                 char *newstr = NULL;
74
75                 newstr = expand_search_string(search_string);
76                 if (newstr && newstr[0] != '\0') {
77                         quicksearch->matcher_list = matcher_parser_get_cond(newstr);
78                         g_free(newstr);
79                 } else {
80                         quicksearch->matcher_list = NULL;
81                         quicksearch->active = FALSE;
82
83                         return;
84                 }
85         } else {
86                 if (quicksearch->search_string != NULL)
87                         g_free(quicksearch->search_string);
88                 quicksearch->search_string = g_strdup(search_string);
89         }
90
91         quicksearch->active = TRUE;
92 }
93
94 static void update_extended_button (QuickSearch *quicksearch)
95 {
96         GtkWidget *btn = quicksearch->search_description;
97         
98         g_return_if_fail(btn != NULL);
99                 
100         if (prefs_common.summary_quicksearch_type == QUICK_SEARCH_EXTENDED) {
101                 gtk_button_set_label(GTK_BUTTON(btn), _("Extended symbols"));
102                 gtk_widget_show(btn);
103         } else {
104                 gtk_widget_hide(btn);
105         }
106         
107 }
108
109 static gboolean searchbar_focus_evt(GtkWidget *widget, GdkEventFocus *event,
110                                   QuickSearch *quicksearch)
111 {
112         quicksearch->has_focus = (event && event->in);
113         return FALSE;
114 }
115
116 gboolean quicksearch_has_focus(QuickSearch *quicksearch)
117 {
118         return quicksearch->has_focus;
119 }
120
121 static gboolean searchbar_pressed(GtkWidget *widget, GdkEventKey *event,
122                                   QuickSearch *quicksearch)
123 {
124         if (event != NULL && event->keyval == GDK_Escape) {
125                 quicksearch_set(quicksearch, prefs_common.summary_quicksearch_type, "");
126                 gtk_widget_grab_focus(GTK_WIDGET(GTK_COMBO(quicksearch->search_string_entry)->entry));
127                 return TRUE;
128         }
129
130         if (event != NULL && event->keyval == GDK_Return) {
131                 const gchar *search_string = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(quicksearch->search_string_entry)->entry));
132
133                 if (search_string && strlen(search_string) != 0) {
134                         prefs_common.summary_quicksearch_history =
135                                 add_history(prefs_common.summary_quicksearch_history,
136                                             search_string);
137                         gtk_combo_set_popdown_strings(GTK_COMBO(quicksearch->search_string_entry), 
138                                 prefs_common.summary_quicksearch_history);                      
139                 }
140
141                 prepare_matcher(quicksearch);
142
143                 quicksearch_set_running(quicksearch, TRUE);
144                 if (quicksearch->callback != NULL)
145                         quicksearch->callback(quicksearch, quicksearch->callback_data);
146                 quicksearch_set_running(quicksearch, FALSE);
147                 g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event");
148                 gtk_widget_grab_focus(GTK_WIDGET(GTK_COMBO(
149                         quicksearch->search_string_entry)->entry));
150                 return TRUE;
151         }
152
153         return FALSE;           
154 }
155
156 static gboolean searchtype_changed(GtkMenuItem *widget, gpointer data)
157 {
158         QuickSearch *quicksearch = (QuickSearch *)data;
159
160         prefs_common.summary_quicksearch_type = GPOINTER_TO_INT(g_object_get_data(
161                                    G_OBJECT(GTK_MENU_ITEM(gtk_menu_get_active(
162                                    GTK_MENU(quicksearch->search_type)))), MENU_VAL_ID));
163
164         /* Show extended search description button, only when Extended is selected */
165         update_extended_button(quicksearch);
166
167         prepare_matcher(quicksearch);
168
169         quicksearch_set_running(quicksearch, TRUE);
170         if (quicksearch->callback != NULL)
171                 quicksearch->callback(quicksearch, quicksearch->callback_data);
172         quicksearch_set_running(quicksearch, FALSE);
173 }
174
175 /*
176  * Strings describing how to use Extended Search
177  * 
178  * When adding new lines, remember to put 2 strings for each line
179  */
180 static gchar *search_descr_strings[] = {
181         "a",     N_("all messages"),
182         "ag #",  N_("messages whose age is greater than #"),
183         "al #",  N_("messages whose age is less than #"),
184         "b S",   N_("messages which contain S in the message body"),
185         "B S",   N_("messages which contain S in the whole message"),
186         "c S",   N_("messages carbon-copied to S"),
187         "C S",   N_("message is either to: or cc: to S"),
188         "D",     N_("deleted messages"), /** how I can filter deleted messages **/
189         "e S",   N_("messages which contain S in the Sender field"),
190         "E S",   N_("true if execute \"S\" succeeds"),
191         "f S",   N_("messages originating from user S"),
192         "F",     N_("forwarded messages"),
193         "h S",   N_("messages which contain header S"),
194         "i S",   N_("messages which contain S in Message-ID header"),
195         "I S",   N_("messages which contain S in inreplyto header"),
196         "L",     N_("locked messages"),
197         "n S",   N_("messages which are in newsgroup S"),
198         "N",     N_("new messages"),
199         "O",     N_("old messages"),
200         "p",     N_("incomplete messages (not entirely downloaded)"),
201         "r",     N_("messages which have been replied to"),
202         "R",     N_("read messages"),
203         "s S",   N_("messages which contain S in subject"),
204         "se #",  N_("messages whose score is equal to #"),
205         "sg #",  N_("messages whose score is greater than #"),
206         "sl #",  N_("messages whose score is lower than #"),
207         "Se #",  N_("messages whose size is equal to #"),
208         "Sg #",  N_("messages whose size is greater than #"),
209         "Ss #",  N_("messages whose size is smaller than #"),
210         "t S",   N_("messages which have been sent to S"),
211         "T",     N_("marked messages"),
212         "U",     N_("unread messages"),
213         "x S",   N_("messages which contain S in References header"),
214         "X cmd", N_("messages returning 0 when passed to command"),
215         "y S",   N_("messages which contain S in X-Label header"),
216         "",      "" ,
217         "&",     N_("logical AND operator"),
218         "|",     N_("logical OR operator"),
219         "! or ~",       N_("logical NOT operator"),
220         "%",     N_("case sensitive search"),
221         "",      "" ,
222         " ",     N_("all filtering expressions are allowed"),
223         NULL,    NULL 
224 };
225  
226 static DescriptionWindow search_descr = {
227         NULL, 
228         2,
229         N_("Extended Search symbols"),
230         search_descr_strings
231 };
232         
233 static void search_description_cb(GtkWidget *widget)
234 {
235         description_window_create(&search_descr);
236 };
237
238 static gboolean clear_search_cb(GtkMenuItem *widget, gpointer data)
239 {
240         QuickSearch *quicksearch = (QuickSearch *)data;
241         
242         quicksearch_set(quicksearch, prefs_common.summary_quicksearch_type, "");
243         
244         return TRUE;
245 };
246
247 QuickSearch *quicksearch_new()
248 {
249         QuickSearch *quicksearch;
250
251         GtkWidget *hbox_search;
252         GtkWidget *search_type_opt;
253         GtkWidget *search_type;
254         GtkWidget *search_string_entry;
255         GtkWidget *search_hbbox;
256         GtkWidget *search_description;
257         GtkWidget *clear_search;
258         GtkWidget *menuitem;
259
260         quicksearch = g_new0(QuickSearch, 1);
261
262         /* quick search */
263         hbox_search = gtk_hbox_new(FALSE, 0);
264
265         search_type_opt = gtk_option_menu_new();
266         gtk_widget_show(search_type_opt);
267         gtk_box_pack_start(GTK_BOX(hbox_search), search_type_opt, FALSE, FALSE, 0);
268
269         search_type = gtk_menu_new();
270         MENUITEM_ADD (search_type, menuitem, _("Subject"), QUICK_SEARCH_SUBJECT);
271         g_signal_connect(G_OBJECT(menuitem), "activate",
272                          G_CALLBACK(searchtype_changed),
273                          quicksearch);
274         MENUITEM_ADD (search_type, menuitem, _("From"), QUICK_SEARCH_FROM);
275         g_signal_connect(G_OBJECT(menuitem), "activate",
276                          G_CALLBACK(searchtype_changed),
277                          quicksearch);
278         MENUITEM_ADD (search_type, menuitem, _("To"), QUICK_SEARCH_TO);
279         g_signal_connect(G_OBJECT(menuitem), "activate",
280                          G_CALLBACK(searchtype_changed),
281                          quicksearch);
282         MENUITEM_ADD (search_type, menuitem, _("Extended"), QUICK_SEARCH_EXTENDED);
283         g_signal_connect(G_OBJECT(menuitem), "activate",
284                          G_CALLBACK(searchtype_changed),
285                          quicksearch);
286
287         gtk_option_menu_set_menu(GTK_OPTION_MENU(search_type_opt), search_type);
288         
289         gtk_option_menu_set_history(GTK_OPTION_MENU(search_type_opt), prefs_common.summary_quicksearch_type);
290         
291         gtk_widget_show(search_type);
292         
293         search_string_entry = gtk_combo_new();
294         gtk_box_pack_start(GTK_BOX(hbox_search), search_string_entry, FALSE, FALSE, 2);
295         gtk_combo_set_value_in_list(GTK_COMBO(search_string_entry), FALSE, TRUE);
296         gtk_combo_set_case_sensitive(GTK_COMBO(search_string_entry), TRUE);
297         if (prefs_common.summary_quicksearch_history) 
298                 gtk_combo_set_popdown_strings(GTK_COMBO(search_string_entry), 
299                         prefs_common.summary_quicksearch_history);
300         gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(search_string_entry)->entry), "");
301         gtk_widget_show(search_string_entry);
302
303         search_hbbox = gtk_hbutton_box_new();
304         gtk_button_box_set_layout(GTK_BUTTON_BOX(search_hbbox),         
305                                   GTK_BUTTONBOX_START);
306
307         gtk_box_set_spacing(GTK_BOX(search_hbbox), 5);
308                 
309         if (prefs_common.summary_quicksearch_sticky) {
310                 clear_search = gtk_button_new_with_label(_("Clear"));
311                 gtk_box_pack_start(GTK_BOX(search_hbbox), clear_search,
312                                    FALSE, FALSE, 0);
313                 gtk_widget_set_usize(clear_search, 120, -1);
314                 g_signal_connect(G_OBJECT(clear_search), "clicked",
315                                  G_CALLBACK(clear_search_cb), quicksearch);
316                 gtk_widget_show(clear_search);
317         }
318
319         search_description = gtk_button_new_with_label(_("Extended Symbols"));
320         gtk_box_pack_start(GTK_BOX(search_hbbox), search_description,
321                            TRUE, TRUE, 0);
322         gtk_widget_show(search_description);
323                 
324         g_signal_connect(G_OBJECT(search_description), "clicked",
325                          G_CALLBACK(search_description_cb), NULL);
326
327         gtk_box_pack_start(GTK_BOX(hbox_search), search_hbbox, FALSE, FALSE, 2);                                
328         gtk_widget_show(search_hbbox);
329         if (prefs_common.summary_quicksearch_type == QUICK_SEARCH_EXTENDED)
330                 gtk_widget_show(search_description);
331         else
332                 gtk_widget_hide(search_description);
333         
334         g_signal_connect(G_OBJECT(GTK_COMBO(search_string_entry)->entry), 
335                            "key_press_event",
336                            G_CALLBACK(searchbar_pressed),
337                            quicksearch);
338
339         
340         g_signal_connect(G_OBJECT(GTK_COMBO(search_string_entry)->entry),
341                          "focus_in_event",
342                          G_CALLBACK(searchbar_focus_evt),
343                          quicksearch);
344
345         g_signal_connect(G_OBJECT(GTK_COMBO(search_string_entry)->entry),
346                          "focus_out_event",
347                          G_CALLBACK(searchbar_focus_evt),
348                          quicksearch);
349         
350
351         quicksearch->hbox_search = hbox_search;
352         quicksearch->search_type = search_type;
353         quicksearch->search_type_opt = search_type_opt;
354         quicksearch->search_string_entry = search_string_entry;
355         quicksearch->search_description = search_description;
356         quicksearch->matcher_list = NULL;
357         quicksearch->active = FALSE;
358         quicksearch->running = FALSE;
359         
360         update_extended_button(quicksearch);
361         
362         return quicksearch;
363 }
364
365 GtkWidget *quicksearch_get_widget(QuickSearch *quicksearch)
366 {
367         return quicksearch->hbox_search;
368 }
369
370 void quicksearch_show(QuickSearch *quicksearch)
371 {
372         prepare_matcher(quicksearch);
373         gtk_widget_show(quicksearch->hbox_search);
374         update_extended_button(quicksearch);
375 }
376
377 void quicksearch_hide(QuickSearch *quicksearch)
378 {
379         quicksearch->active = FALSE;
380         gtk_widget_hide(quicksearch->hbox_search);
381 }
382
383 void quicksearch_set(QuickSearch *quicksearch, QuickSearchType type,
384                      const gchar *matchstring)
385 {
386         gtk_option_menu_set_history(GTK_OPTION_MENU(quicksearch->search_type_opt),
387                                     type);
388         gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(quicksearch->search_string_entry)->entry),
389                            matchstring);
390         prefs_common.summary_quicksearch_type = type;
391
392         prepare_matcher(quicksearch);
393
394         quicksearch_set_running(quicksearch, TRUE);
395         if (quicksearch->callback != NULL)
396                 quicksearch->callback(quicksearch, quicksearch->callback_data); 
397         quicksearch_set_running(quicksearch, FALSE);
398 }
399
400 gboolean quicksearch_is_active(QuickSearch *quicksearch)
401 {
402         return quicksearch->active;
403 }
404
405 void quicksearch_set_execute_callback(QuickSearch *quicksearch,
406                                       QuickSearchExecuteCallback callback,
407                                       gpointer data)
408 {
409         quicksearch->callback = callback;
410         quicksearch->callback_data = data;
411 }
412
413 gboolean quicksearch_match(QuickSearch *quicksearch, MsgInfo *msginfo)
414 {
415         gchar *searched_header = NULL;
416
417         if (!quicksearch->active)
418                 return TRUE;
419
420         switch (prefs_common.summary_quicksearch_type) {
421         case QUICK_SEARCH_SUBJECT:
422                 searched_header = msginfo->subject;
423                 break;
424         case QUICK_SEARCH_FROM:
425                 searched_header = msginfo->from;
426                 break;
427         case QUICK_SEARCH_TO:
428                 searched_header = msginfo->to;
429                 break;
430         case QUICK_SEARCH_EXTENDED:
431                 break;
432         default:
433                 debug_print("unknown search type (%d)\n", prefs_common.summary_quicksearch_type);
434                 break;
435         }
436
437         if (prefs_common.summary_quicksearch_type != QUICK_SEARCH_EXTENDED && quicksearch->search_string &&
438             searched_header && strcasestr(searched_header, quicksearch->search_string) != NULL)
439                 return TRUE;
440         else if ((quicksearch->matcher_list != NULL) && matcherlist_match(quicksearch->matcher_list, msginfo))
441                 return TRUE;
442
443         return FALSE;
444 }
445
446 /* allow Mutt-like patterns in quick search */
447 gchar *expand_search_string(const gchar *search_string)
448 {
449         int i = 0;
450         gchar term_char, save_char;
451         gchar *cmd_start, *cmd_end;
452         GString *matcherstr;
453         gchar *returnstr = NULL;
454         gchar *copy_str;
455         gboolean casesens, dontmatch;
456         /* list of allowed pattern abbreviations */
457         struct {
458                 gchar           *abbreviated;   /* abbreviation */
459                 gchar           *command;       /* actual matcher command */ 
460                 gint            numparams;      /* number of params for cmd */
461                 gboolean        qualifier;      /* do we append regexpcase */
462                 gboolean        quotes;         /* do we need quotes */
463         }
464         cmds[] = {
465                 { "a",  "all",                          0,      FALSE,  FALSE },
466                 { "ag", "age_greater",                  1,      FALSE,  FALSE },
467                 { "al", "age_lower",                    1,      FALSE,  FALSE },
468                 { "b",  "body_part",                    1,      TRUE,   TRUE  },
469                 { "B",  "message",                      1,      TRUE,   TRUE  },
470                 { "c",  "cc",                           1,      TRUE,   TRUE  },
471                 { "C",  "to_or_cc",                     1,      TRUE,   TRUE  },
472                 { "D",  "deleted",                      0,      FALSE,  FALSE },
473                 { "e",  "header \"Sender\"",            1,      TRUE,   TRUE  },
474                 { "E",  "execute",                      1,      FALSE,  TRUE  },
475                 { "f",  "from",                         1,      TRUE,   TRUE  },
476                 { "F",  "forwarded",                    0,      FALSE,  FALSE },
477                 { "h",  "headers_part",                 1,      TRUE,   TRUE  },
478                 { "i",  "header \"Message-ID\"",        1,      TRUE,   TRUE  },
479                 { "I",  "inreplyto",                    1,      TRUE,   TRUE  },
480                 { "L",  "locked",                       0,      FALSE,  FALSE },
481                 { "n",  "newsgroups",                   1,      TRUE,   TRUE  },
482                 { "N",  "new",                          0,      FALSE,  FALSE },
483                 { "O",  "~new",                         0,      FALSE,  FALSE },
484                 { "r",  "replied",                      0,      FALSE,  FALSE },
485                 { "R",  "~unread",                      0,      FALSE,  FALSE },
486                 { "s",  "subject",                      1,      TRUE,   TRUE  },
487                 { "se", "score_equal",                  1,      FALSE,  FALSE },
488                 { "sg", "score_greater",                1,      FALSE,  FALSE },
489                 { "sl", "score_lower",                  1,      FALSE,  FALSE },
490                 { "Se", "size_equal",                   1,      FALSE,  FALSE },
491                 { "Sg", "size_greater",                 1,      FALSE,  FALSE },
492                 { "Ss", "size_smaller",                 1,      FALSE,  FALSE },
493                 { "t",  "to",                           1,      TRUE,   TRUE  },
494                 { "T",  "marked",                       0,      FALSE,  FALSE },
495                 { "U",  "unread",                       0,      FALSE,  FALSE },
496                 { "x",  "header \"References\"",        1,      TRUE,   TRUE  },
497                 { "X",  "test",                         1,      FALSE,  FALSE }, 
498                 { "y",  "header \"X-Label\"",           1,      TRUE,   TRUE  },
499                 { "&",  "&",                            0,      FALSE,  FALSE },
500                 { "|",  "|",                            0,      FALSE,  FALSE },
501                 { "p",  "partial",                      0,      FALSE,  FALSE },
502                 { NULL, NULL,                           0,      FALSE,  FALSE }
503         };
504
505         if (search_string == NULL)
506                 return NULL;
507
508         copy_str = g_strdup(search_string);
509
510         matcherstr = g_string_sized_new(16);
511         cmd_start = copy_str;
512         while (cmd_start && *cmd_start) {
513                 /* skip all white spaces */
514                 while (*cmd_start && isspace((guchar)*cmd_start))
515                         cmd_start++;
516                 cmd_end = cmd_start;
517
518                 /* extract a command */
519                 while (*cmd_end && !isspace((guchar)*cmd_end))
520                         cmd_end++;
521
522                 /* save character */
523                 save_char = *cmd_end;
524                 *cmd_end = '\0';
525
526                 dontmatch = FALSE;
527                 casesens = FALSE;
528
529                 /* ~ and ! mean logical NOT */
530                 if (*cmd_start == '~' || *cmd_start == '!')
531                 {
532                         dontmatch = TRUE;
533                         cmd_start++;
534                 }
535                 /* % means case sensitive match */
536                 if (*cmd_start == '%')
537                 {
538                         casesens = TRUE;
539                         cmd_start++;
540                 }
541
542                 /* find matching abbreviation */
543                 for (i = 0; cmds[i].command; i++) {
544                         if (!strcmp(cmd_start, cmds[i].abbreviated)) {
545                                 /* restore character */
546                                 *cmd_end = save_char;
547
548                                 /* copy command */
549                                 if (matcherstr->len > 0) {
550                                         g_string_append(matcherstr, " ");
551                                 }
552                                 if (dontmatch)
553                                         g_string_append(matcherstr, "~");
554                                 g_string_append(matcherstr, cmds[i].command);
555                                 g_string_append(matcherstr, " ");
556
557                                 /* stop if no params required */
558                                 if (cmds[i].numparams == 0)
559                                         break;
560
561                                 /* extract a parameter, allow quotes */
562                                 while (*cmd_end && isspace((guchar)*cmd_end))
563                                         cmd_end++;
564
565                                 cmd_start = cmd_end;
566                                 if (*cmd_start == '"') {
567                                         term_char = '"';
568                                         cmd_end++;
569                                 }
570                                 else
571                                         term_char = ' ';
572
573                                 /* extract actual parameter */
574                                 while ((*cmd_end) && (*cmd_end != term_char))
575                                         cmd_end++;
576
577                                 if (*cmd_end == '"')
578                                         cmd_end++;
579
580                                 save_char = *cmd_end;
581                                 *cmd_end = '\0';
582
583                                 if (cmds[i].qualifier) {
584                                         if (casesens)
585                                                 g_string_append(matcherstr, "regexp ");
586                                         else
587                                                 g_string_append(matcherstr, "regexpcase ");
588                                 }
589
590                                 /* do we need to add quotes ? */
591                                 if (cmds[i].quotes && term_char != '"')
592                                         g_string_append(matcherstr, "\"");
593
594                                 /* copy actual parameter */
595                                 g_string_append(matcherstr, cmd_start);
596
597                                 /* do we need to add quotes ? */
598                                 if (cmds[i].quotes && term_char != '"')
599                                         g_string_append(matcherstr, "\"");
600
601                                 /* restore original character */
602                                 *cmd_end = save_char;
603
604                                 break;
605                         }
606                 }
607
608                 if (*cmd_end)
609                         cmd_end++;
610                 cmd_start = cmd_end;
611         }
612
613         g_free(copy_str);
614
615         /* return search string if no match is found to allow 
616            all available filtering expressions in quicksearch */
617         if (matcherstr->len > 0) returnstr = matcherstr->str;
618         else returnstr = g_strdup(search_string);
619
620         g_string_free(matcherstr, FALSE);
621         return returnstr;
622 }
623
624 static void quicksearch_set_running(QuickSearch *quicksearch, gboolean run)
625 {
626         quicksearch->running = run;
627 }
628
629 gboolean quicksearch_is_running(QuickSearch *quicksearch) 
630 {
631         return quicksearch->running;
632 }
633
634 void quicksearch_pass_key(QuickSearch *quicksearch, guint val, GdkModifierType mod)
635 {
636         GtkEntry *entry = GTK_ENTRY(GTK_COMBO(quicksearch->search_string_entry)->entry);
637         gint curpos = gtk_editable_get_position(GTK_EDITABLE(entry));
638         char *str = g_strdup(gtk_entry_get_text(entry));
639         char *begin = str;
640         char *end = NULL;
641         char *new = NULL;
642         
643         if (mod == GDK_SHIFT_MASK)
644                 val = toupper(val);
645         
646         if (curpos < strlen(str)-1) {
647                 end = g_strdup(str+curpos);
648                 *(str+curpos) = '\0';
649                 new = g_strdup_printf("%s%c%s", begin, val, end);
650                 gtk_entry_set_text(entry, new);
651                 g_free(end);
652         } else {
653                 new = g_strdup_printf("%s%c", begin, val);
654                 gtk_entry_set_text(entry, new);
655         }
656         g_free(str);
657         g_free(new);
658         gtk_editable_set_position(GTK_EDITABLE(entry), curpos+1);
659         
660 }