2012-11-16 [colin] 3.9.0cvs10
[claws.git] / src / gtk / quicksearch.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 Colin Leroy <colin@colino.net> 
4  * and the Claws Mail team
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 3 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  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  * 
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #include "claws-features.h"
24 #endif
25
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <ctype.h>
29
30 #include <gtk/gtk.h>
31 #include <gdk/gdkkeysyms.h>
32
33 #if !GTK_CHECK_VERSION(3, 0, 0)
34 #include "gtkcmoptionmenu.h"
35 #endif
36 #include "utils.h"
37 #include "combobox.h"
38 #include "menu.h"
39 #include "prefs_common.h"
40 #include "description_window.h"
41 #include "matcher.h"
42 #include "matcher_parser.h"
43 #include "quicksearch.h"
44 #include "folderview.h"
45 #include "folder.h"
46 #include "prefs_matcher.h"
47 #include "claws.h"
48 #include "statusbar.h"
49 #include "advsearch.h"
50 #include "alertpanel.h"
51
52 struct _QuickSearchRequest
53 {
54         AdvancedSearchType               type;
55         gchar                           *matchstring;
56 };
57 typedef struct _QuickSearchRequest QuickSearchRequest;
58
59 struct _QuickSearch
60 {
61         GtkWidget                       *hbox_search;
62         GtkWidget                       *search_type;
63 #if !GTK_CHECK_VERSION(3, 0, 0)
64         GtkWidget                       *search_type_opt;
65 #endif
66         GtkWidget                       *search_string_entry;
67         GtkWidget                       *search_condition_expression;
68         GtkWidget                       *search_description;
69         GtkWidget                       *clear_search;
70
71         gboolean                         active;
72         gchar                           *search_string;
73
74         QuickSearchRequest               request;
75         QuickSearchExecuteCallback       callback;
76         gpointer                         callback_data;
77         gboolean                         running;
78         gboolean                         has_focus;
79         gboolean                         in_typing;
80         guint                            press_timeout_id;
81
82         GList                           *normal_search_strings;
83         GList                           *extended_search_strings;
84         
85         /* dynamic and autorun qs settings are exclusive*/
86         GtkWidget                        *dynamic_menuitem;
87         GtkWidget                        *autorun_menuitem;
88
89         AdvancedSearch                  *asearch;
90         gboolean                         want_reexec;
91         gboolean                         want_history;
92 };
93
94 void quicksearch_set_on_progress_cb(QuickSearch* search,
95                 gboolean (*cb)(gpointer data, guint at, guint matched, guint total), gpointer data)
96 {
97         advsearch_set_on_progress_cb(search->asearch, cb, data);
98 }
99
100 static void quicksearch_set_running(QuickSearch *quicksearch, gboolean run);
101 static void quicksearch_set_matchstring(QuickSearch *quicksearch, const gchar *matchstring);
102 static void quicksearch_set_active(QuickSearch *quicksearch, gboolean active);
103 static void quicksearch_set_popdown_strings(QuickSearch *quicksearch);
104
105 static void quicksearch_add_to_history(QuickSearch* quicksearch)
106 {
107         gchar* search_string = quicksearch->request.matchstring;
108
109         /* add to history, for extended search add only correct matching rules */
110         if (quicksearch->want_history && !quicksearch->in_typing && search_string && strlen(search_string) != 0) {
111                 switch (prefs_common.summary_quicksearch_type) {
112                         case ADVANCED_SEARCH_EXTENDED:
113                                 if (advsearch_has_proper_predicate(quicksearch->asearch)) {
114                                         quicksearch->extended_search_strings =
115                                                 add_history(quicksearch->extended_search_strings,
116                                                                 g_strdup(search_string));
117                                         prefs_common.summary_quicksearch_history =
118                                                 add_history(prefs_common.summary_quicksearch_history,
119                                                                 g_strdup(search_string));
120                                 }
121                                 break;
122                         default:
123                                 quicksearch->normal_search_strings =
124                                         add_history(quicksearch->normal_search_strings,
125                                                         g_strdup(search_string));               
126                                 prefs_common.summary_quicksearch_history =
127                                         add_history(prefs_common.summary_quicksearch_history,
128                                                         g_strdup(search_string));
129                                 break;
130                 }
131
132                 quicksearch_set_popdown_strings(quicksearch);
133         }
134
135         quicksearch->want_history = FALSE;
136 }
137
138 static void quicksearch_invoke_execute(QuickSearch *quicksearch, gboolean run_only_if_fast)
139 {
140         if (quicksearch->running) {
141                 quicksearch->want_reexec = TRUE;
142                 advsearch_abort(quicksearch->asearch);
143                 return;
144         }
145
146         do {
147                 gboolean active = quicksearch->request.matchstring != NULL 
148                                    && g_strcmp0(quicksearch->request.matchstring, "");
149                 advsearch_set(quicksearch->asearch, quicksearch->request.type,
150                                 quicksearch->request.matchstring);
151
152                 if (run_only_if_fast && !advsearch_is_fast(quicksearch->asearch))
153                         return;
154
155                 quicksearch_add_to_history(quicksearch);
156
157                 quicksearch_set_active(quicksearch, active);
158
159                 quicksearch->want_reexec = FALSE;
160                 quicksearch_set_running(quicksearch, TRUE);
161                 if (quicksearch->callback != NULL)
162                         quicksearch->callback(quicksearch, quicksearch->callback_data);
163                 quicksearch_set_running(quicksearch, FALSE);
164         } while (quicksearch->want_reexec);
165 }
166
167 gboolean quicksearch_run_on_folder(QuickSearch* quicksearch, FolderItem *folderItem, MsgInfoList **result)
168 {
169         if (quicksearch_has_sat_predicate(quicksearch)) {
170                 gboolean searchres;
171                 main_window_cursor_wait(mainwindow_get_mainwindow());
172                 searchres = advsearch_search_msgs_in_folders(quicksearch->asearch, result, folderItem, FALSE);
173                 main_window_cursor_normal(mainwindow_get_mainwindow());
174                 if (quicksearch->want_reexec) {
175                         advsearch_set(quicksearch->asearch, quicksearch->request.type, "");
176                 }
177                 return searchres;
178         } else
179                 return FALSE;
180 }
181
182 gboolean quicksearch_is_fast(QuickSearch *quicksearch)
183 {
184         return advsearch_is_fast(quicksearch->asearch);
185 }
186
187 static void quicksearch_set_type(QuickSearch *quicksearch, gint type)
188 {
189 #if !GTK_CHECK_VERSION(3, 0, 0)
190         gint index;
191         quicksearch->request.type = type;
192         index = menu_find_option_menu_index(GTK_CMOPTION_MENU(quicksearch->search_type_opt), 
193                                         GINT_TO_POINTER(type),
194                                         NULL);
195         gtk_cmoption_menu_set_history(GTK_CMOPTION_MENU(quicksearch->search_type_opt), index);  
196 #endif
197 }
198
199 static gchar *quicksearch_get_text(QuickSearch * quicksearch)
200 {
201         gchar *search_string = gtk_editable_get_chars(GTK_EDITABLE(gtk_bin_get_child(GTK_BIN((quicksearch->search_string_entry)))), 0, -1);
202
203         g_strstrip(search_string);
204         return search_string;
205 }
206
207 static void quicksearch_set_popdown_strings(QuickSearch *quicksearch)
208 {
209         GtkWidget *search_string_entry = quicksearch->search_string_entry;
210
211         combobox_unset_popdown_strings(GTK_COMBO_BOX(search_string_entry));
212
213         if (prefs_common.summary_quicksearch_type == ADVANCED_SEARCH_EXTENDED)
214                 combobox_set_popdown_strings(GTK_COMBO_BOX(search_string_entry),
215                         quicksearch->extended_search_strings);  
216         else
217                 combobox_set_popdown_strings(GTK_COMBO_BOX(search_string_entry),
218                         quicksearch->normal_search_strings);
219 }
220
221 static void update_extended_buttons (QuickSearch *quicksearch)
222 {
223         GtkWidget *expr_btn = quicksearch->search_condition_expression;
224         GtkWidget *ext_btn = quicksearch->search_description;
225
226         cm_return_if_fail(expr_btn != NULL);
227         cm_return_if_fail(ext_btn != NULL);
228
229         if (prefs_common.summary_quicksearch_type == ADVANCED_SEARCH_EXTENDED) {
230                 gtk_widget_show(expr_btn);
231                 gtk_widget_show(ext_btn);
232         } else {
233                 gtk_widget_hide(expr_btn);
234                 gtk_widget_hide(ext_btn);
235         }
236 }
237
238 static gboolean searchbar_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
239                                   QuickSearch *qs)
240 {
241         qs->has_focus = TRUE;
242         return FALSE;
243 }
244
245 static gboolean searchbar_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
246                                   QuickSearch *qs)
247 {
248         qs->has_focus = FALSE;
249         qs->in_typing = FALSE;
250         return FALSE;
251 }
252
253 gboolean quicksearch_has_focus(QuickSearch *quicksearch)
254 {
255         return quicksearch->has_focus;
256 }
257
258 static void searchbar_run(QuickSearch *quicksearch, gboolean run_only_if_fast)
259 {
260         gchar *search_string = quicksearch_get_text(quicksearch);
261         quicksearch_set_matchstring(quicksearch, search_string);
262         g_free(search_string);
263
264         quicksearch->want_history = TRUE;
265
266         quicksearch_invoke_execute(quicksearch, run_only_if_fast);
267 }
268
269 static int searchbar_changed_timeout(void *data)
270 {
271         QuickSearch *qs = (QuickSearch *)data;
272         if (qs && prefs_common.summary_quicksearch_dynamic) {
273                 qs->in_typing = TRUE;
274                 searchbar_run(qs, TRUE);
275         }
276         return FALSE;
277 }
278
279 static void searchbar_changed_cb(GtkWidget *widget, QuickSearch *qs)
280 {
281         if (!qs->has_focus && prefs_common.summary_quicksearch_autorun) {
282                 gtk_widget_grab_focus(qs->search_string_entry);
283                 searchbar_run(qs, TRUE);
284                 return;
285         }
286
287         if (prefs_common.summary_quicksearch_dynamic) {
288                 if (qs->press_timeout_id != -1) {
289                         g_source_remove(qs->press_timeout_id);
290                 }
291                 qs->press_timeout_id = g_timeout_add(500,
292                                 searchbar_changed_timeout, qs);
293         }
294
295         if (!qs->has_focus)
296                 gtk_widget_grab_focus(qs->search_string_entry);
297 }
298
299 static gboolean searchbar_pressed(GtkWidget *widget, GdkEventKey *event,
300                                   QuickSearch *quicksearch)
301 {
302         if (event && (event->keyval == GDK_KEY_Escape)) {
303                 gchar *str;
304
305                 quicksearch->in_typing = FALSE;
306
307                 str = quicksearch_get_text(quicksearch);
308                 cm_return_val_if_fail(str != NULL, TRUE);
309
310                 /* If the string entry is empty -> hide quicksearch bar. If not -> empty it */
311                 if (!*str) {
312                         summaryview_activate_quicksearch(
313                                 mainwindow_get_mainwindow()->summaryview, 
314                                 FALSE);
315                 } else {
316                         quicksearch_set(quicksearch, prefs_common.summary_quicksearch_type, "");
317                         gtk_widget_grab_focus(
318                                         mainwindow_get_mainwindow()->summaryview->ctree);
319                 }
320                 g_free(str);
321                 return TRUE;
322         }
323
324         if (event != NULL && (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_KP_Enter)) {
325                 if (quicksearch->press_timeout_id != -1) {
326                         g_source_remove(quicksearch->press_timeout_id);
327                         quicksearch->press_timeout_id = -1;
328                 }
329                 quicksearch->in_typing = FALSE;
330                 /* add expression to history list and exec quicksearch */
331                 searchbar_run(quicksearch, FALSE);
332
333                 g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event");
334                 return TRUE;
335         }
336
337         if (event && (event->keyval == GDK_KEY_Down || event->keyval == GDK_KEY_Up)) {
338                 combobox_set_value_from_arrow_key(
339                                 GTK_COMBO_BOX(quicksearch->search_string_entry),
340                                 event->keyval);
341                 return TRUE;
342         }
343
344         return FALSE;
345 }
346
347 static gboolean searchtype_changed(GtkMenuItem *widget, gpointer data)
348 {
349         QuickSearch *quicksearch = (QuickSearch *)data;
350
351         prefs_common.summary_quicksearch_type = GPOINTER_TO_INT(g_object_get_data(
352                                    G_OBJECT(GTK_MENU_ITEM(gtk_menu_get_active(
353                                    GTK_MENU(quicksearch->search_type)))), MENU_VAL_ID));
354         quicksearch->request.type = prefs_common.summary_quicksearch_type;
355
356         /* Show extended search description button, only when Extended is selected */
357         update_extended_buttons(quicksearch);
358         quicksearch_set_popdown_strings(quicksearch);
359
360         quicksearch_invoke_execute(quicksearch, FALSE);
361
362         return TRUE;
363 }
364
365 static gboolean searchtype_recursive_changed(GtkMenuItem *widget, gpointer data)
366 {
367         QuickSearch *quicksearch = (QuickSearch *)data;
368         gboolean checked = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));
369
370         prefs_common.summary_quicksearch_recurse = checked;
371
372         /* reselect the search type */
373         quicksearch_set_type(quicksearch, prefs_common.summary_quicksearch_type);
374
375         quicksearch_invoke_execute(quicksearch, FALSE);
376
377         return TRUE;
378 }
379
380 static gboolean searchtype_sticky_changed(GtkMenuItem *widget, gpointer data)
381 {
382         QuickSearch *quicksearch = (QuickSearch *)data;
383         gboolean checked = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));
384
385         prefs_common.summary_quicksearch_sticky = checked;
386
387         /* reselect the search type */
388         quicksearch_set_type(quicksearch, prefs_common.summary_quicksearch_type);
389
390         return TRUE;
391 }
392
393 static gboolean searchtype_dynamic_changed(GtkMenuItem *widget, gpointer data)
394 {
395         QuickSearch *quicksearch = (QuickSearch *)data;
396         gboolean checked = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));
397
398         prefs_common.summary_quicksearch_dynamic = checked;
399         if (checked)
400                 gtk_check_menu_item_set_active(
401                                 GTK_CHECK_MENU_ITEM(quicksearch->autorun_menuitem),
402                                 FALSE);
403
404         /* reselect the search type */
405         quicksearch_set_type(quicksearch, prefs_common.summary_quicksearch_type);
406
407         return TRUE;
408 }
409
410 static gboolean searchtype_autorun_changed(GtkMenuItem *widget, gpointer data)
411 {
412         QuickSearch *quicksearch = (QuickSearch *)data;
413         gboolean checked = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));
414
415         prefs_common.summary_quicksearch_autorun = checked;
416         if (checked)
417                 gtk_check_menu_item_set_active(
418                                 GTK_CHECK_MENU_ITEM(quicksearch->dynamic_menuitem),
419                                 FALSE);
420
421         /* reselect the search type */
422         quicksearch_set_type(quicksearch, prefs_common.summary_quicksearch_type);
423
424         return TRUE;
425 }
426
427 /*
428  * Strings describing how to use Extended Search
429  *
430  * When adding new lines, remember to put 2 strings for each line
431  */
432 static gchar *search_descr_strings[] = {
433         "a",     N_("all messages"),
434         "ag #",  N_("messages whose age is greater than # days"),
435         "al #",  N_("messages whose age is less than # days"),
436         "b S",   N_("messages which contain S in the message body"),
437         "B S",   N_("messages which contain S in the whole message"),
438         "c S",   N_("messages carbon-copied to S"),
439         "C S",   N_("message is either to: or cc: to S"),
440         "D",     N_("deleted messages"), /** how I can filter deleted messages **/
441         "e S",   N_("messages which contain S in the Sender field"),
442         "E S",   N_("true if execute \"S\" succeeds"),
443         "f S",   N_("messages originating from user S"),
444         "F",     N_("forwarded messages"),
445         "ha",    N_("messages which have attachments"),
446         "h S",   N_("messages which contain header S"),
447         "i S",   N_("messages which contain S in Message-ID header"),
448         "I S",   N_("messages which contain S in In-Reply-To header"),
449         "k #",   N_("messages which are marked with color #"),
450         "L",     N_("locked messages"),
451         "n S",   N_("messages which are in newsgroup S"),
452         "N",     N_("new messages"),
453         "O",     N_("old messages"),
454         "p",     N_("incomplete messages (not entirely downloaded)"),
455         "r",     N_("messages which have been replied to"),
456         "R",     N_("read messages"),
457         "s S",   N_("messages which contain S in subject"),
458         "se #",  N_("messages whose score is equal to # points"),
459         "sg #",  N_("messages whose score is greater than # points"),
460         "sl #",  N_("messages whose score is lower than # points"),
461         "Se #",  N_("messages whose size is equal to # bytes"),
462         "Sg #",  N_("messages whose size is greater than # bytes"),
463         "Ss #",  N_("messages whose size is smaller than # bytes"),
464         "t S",   N_("messages which have been sent to S"),
465         "tg S",  N_("messages which tags contain S"),
466         "tagged",N_("messages which have tag(s)"),
467         "T",     N_("marked messages"),
468         "U",     N_("unread messages"),
469         "x S",   N_("messages which contain S in References header"),
470         "X \"cmd args\"", N_("messages returning 0 when passed to command - %F is message file"),
471         "y S",   N_("messages which contain S in X-Label header"),
472         "",      "" ,
473         "&amp;",         N_("logical AND operator"),
474         "|",     N_("logical OR operator"),
475         "! or ~",       N_("logical NOT operator"),
476         "%",     N_("case sensitive search"),
477         "#",     N_("match using regular expressions instead of substring search"),
478         "",      "" ,
479         " ",     N_("all filtering expressions are allowed"),
480         NULL,    NULL
481 };
482
483 static DescriptionWindow search_descr = {
484         NULL,
485         NULL,
486         FALSE,
487         2,
488         N_("Extended Search"),
489         N_("Extended Search allows the user to define criteria that messages must "
490            "have in order to match and be displayed in the message list.\n"
491            "The following symbols can be used:"),
492         search_descr_strings
493 };
494
495 static void search_description_cb(GtkWidget *widget)
496 {
497         search_descr.parent = mainwindow_get_mainwindow()->window;
498         description_window_create(&search_descr);
499 };
500
501 static gboolean clear_search_cb(GtkMenuItem *widget, gpointer data)
502 {
503         QuickSearch *quicksearch = (QuickSearch *)data;
504
505         if (!quicksearch->active)
506                 return TRUE;
507
508         quicksearch_set(quicksearch, prefs_common.summary_quicksearch_type, "");
509
510         return TRUE;
511 };
512
513 static void search_condition_expr_done(MatcherList * matchers)
514 {
515         gchar *str;
516
517         cm_return_if_fail(
518                         mainwindow_get_mainwindow()->summaryview->quicksearch != NULL);
519
520         if (matchers == NULL)
521                 return;
522
523         str = matcherlist_to_string(matchers);
524
525         if (str != NULL) {
526                 quicksearch_set(mainwindow_get_mainwindow()->summaryview->quicksearch,
527                                 prefs_common.summary_quicksearch_type, str);
528                 g_free(str);
529
530                 /* add expression to history list and exec quicksearch */
531                 searchbar_run(mainwindow_get_mainwindow()->summaryview->quicksearch, FALSE);
532         }
533 }
534
535 static gboolean search_condition_expr(GtkMenuItem *widget, gpointer data)
536 {
537         gchar * cond_str;
538         MatcherList * matchers = NULL;
539         
540         cm_return_val_if_fail(
541                         mainwindow_get_mainwindow()->summaryview->quicksearch != NULL,
542                         FALSE);
543
544         /* re-use the current quicksearch value if it's a condition expression,
545            otherwise ignore it silently */
546         cond_str = quicksearch_get_text(mainwindow_get_mainwindow()->summaryview->quicksearch);
547
548         if (*cond_str != '\0') {
549                 matchers = matcher_parser_get_cond((gchar*)cond_str, NULL);
550         }
551
552         prefs_matcher_open(matchers, search_condition_expr_done);
553
554         if (matchers != NULL)
555                 matcherlist_free(matchers);
556
557         g_free(cond_str);
558
559         return TRUE;
560 };
561
562 static void quicksearch_set_button(GtkButton *button, const gchar *icon, const gchar *text)
563 {
564         GList *children = gtk_container_get_children(GTK_CONTAINER(button));
565         GList *cur;
566         GtkWidget *box;
567         gboolean icon_visible;
568
569         g_object_get(gtk_settings_get_default(), 
570                                          "gtk-button-images", &icon_visible, 
571                                          NULL);
572
573         for (cur = children; cur; cur = cur->next)
574                 gtk_container_remove(GTK_CONTAINER(button), GTK_WIDGET(cur->data));
575         
576         g_list_free(children);
577         box = gtk_hbox_new(FALSE, 0);
578         
579         gtk_container_add(GTK_CONTAINER(button), box);
580         if (icon_visible || !text || !*text)
581                 gtk_box_pack_start(GTK_BOX(box), gtk_image_new_from_stock(icon, 
582                         GTK_ICON_SIZE_BUTTON), FALSE, FALSE, 0);
583         gtk_box_pack_start(GTK_BOX(box), gtk_label_new_with_mnemonic(text), FALSE, FALSE, 0);
584         gtk_widget_show_all(box);
585 }
586
587 static void quicksearch_error(gpointer data)
588 {
589         alertpanel_error(_("Something went wrong during search. Please check your logs."));
590 }
591
592 QuickSearch *quicksearch_new()
593 {
594         QuickSearch *quicksearch;
595
596         GtkWidget *hbox_search;
597 #if !GTK_CHECK_VERSION(3, 0, 0)
598         GtkWidget *search_type_opt;
599 #endif
600         GtkWidget *search_type;
601         GtkWidget *search_string_entry;
602         GtkWidget *search_hbox;
603         GtkWidget *search_description;
604         GtkWidget *clear_search;
605         GtkWidget *search_condition_expression;
606         GtkWidget *menuitem;
607         GtkWidget *vbox;
608
609         quicksearch = g_new0(QuickSearch, 1);
610
611         quicksearch->asearch = advsearch_new();
612         advsearch_set_on_error_cb(quicksearch->asearch, quicksearch_error, NULL);
613
614         /* init. values initally found in quicksearch_new().
615            There's no need to init. all pointers to NULL since we use g_new0
616          */
617         quicksearch->active = FALSE;
618         quicksearch->running = FALSE;
619         quicksearch->in_typing = FALSE;
620         quicksearch->press_timeout_id = -1;
621         quicksearch->normal_search_strings = NULL;
622         quicksearch->extended_search_strings = NULL;
623
624         /* quick search */
625         hbox_search = gtk_hbox_new(FALSE, 0);
626
627 #if !GTK_CHECK_VERSION(3, 0, 0)
628         search_type_opt = gtk_cmoption_menu_new();
629         gtk_widget_show(search_type_opt);
630         gtk_box_pack_start(GTK_BOX(hbox_search), search_type_opt, FALSE, FALSE, 0);
631 #endif
632
633         search_type = gtk_menu_new();
634         MENUITEM_ADD (search_type, menuitem,
635                         prefs_common_translated_header_name("Subject"), ADVANCED_SEARCH_SUBJECT);
636         g_signal_connect(G_OBJECT(menuitem), "activate",
637                          G_CALLBACK(searchtype_changed),
638                          quicksearch);
639         MENUITEM_ADD (search_type, menuitem,
640                         prefs_common_translated_header_name("From"), ADVANCED_SEARCH_FROM);
641         g_signal_connect(G_OBJECT(menuitem), "activate",
642                          G_CALLBACK(searchtype_changed),
643                          quicksearch);
644         MENUITEM_ADD (search_type, menuitem,
645                         prefs_common_translated_header_name("To"), ADVANCED_SEARCH_TO);
646         g_signal_connect(G_OBJECT(menuitem), "activate",
647                          G_CALLBACK(searchtype_changed),
648                          quicksearch);
649         MENUITEM_ADD (search_type, menuitem,
650                         prefs_common_translated_header_name("Tag"), ADVANCED_SEARCH_TAG);
651         g_signal_connect(G_OBJECT(menuitem), "activate",
652                          G_CALLBACK(searchtype_changed),
653                          quicksearch);
654         MENUITEM_ADD (search_type, menuitem,
655                         _("From/To/Subject/Tag"), ADVANCED_SEARCH_MIXED);
656         g_signal_connect(G_OBJECT(menuitem), "activate",
657                          G_CALLBACK(searchtype_changed),
658                          quicksearch);
659         MENUITEM_ADD (search_type, menuitem, _("Extended"), ADVANCED_SEARCH_EXTENDED);
660         g_signal_connect(G_OBJECT(menuitem), "activate",
661                          G_CALLBACK(searchtype_changed),
662                          quicksearch);
663
664         gtk_menu_shell_append(GTK_MENU_SHELL(search_type), gtk_separator_menu_item_new());
665
666         menuitem = gtk_check_menu_item_new_with_label(_("Recursive"));
667         gtk_menu_shell_append(GTK_MENU_SHELL(search_type), menuitem);
668
669         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
670                                         prefs_common.summary_quicksearch_recurse);
671         g_signal_connect(G_OBJECT(menuitem), "activate",
672                          G_CALLBACK(searchtype_recursive_changed),
673                          quicksearch);
674
675         menuitem = gtk_check_menu_item_new_with_label(_("Sticky"));
676         gtk_menu_shell_append(GTK_MENU_SHELL(search_type), menuitem);
677
678         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
679                                         prefs_common.summary_quicksearch_sticky);
680
681         g_signal_connect(G_OBJECT(menuitem), "activate",
682                          G_CALLBACK(searchtype_sticky_changed),
683                          quicksearch);
684
685         menuitem = gtk_check_menu_item_new_with_label(_("Type-ahead"));
686         gtk_menu_shell_append(GTK_MENU_SHELL(search_type), menuitem);
687
688         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
689                                         prefs_common.summary_quicksearch_dynamic);
690
691         quicksearch->dynamic_menuitem = menuitem;
692
693         g_signal_connect(G_OBJECT(menuitem), "activate",
694                          G_CALLBACK(searchtype_dynamic_changed),
695                          quicksearch);
696
697         menuitem = gtk_check_menu_item_new_with_label(_("Run on select"));
698         gtk_menu_shell_append(GTK_MENU_SHELL(search_type), menuitem);
699
700         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
701                                         prefs_common.summary_quicksearch_autorun);
702
703         quicksearch->autorun_menuitem = menuitem;
704
705         g_signal_connect(G_OBJECT(menuitem), "activate",
706                          G_CALLBACK(searchtype_autorun_changed),
707                          quicksearch);
708
709 #if !GTK_CHECK_VERSION(3, 0, 0)
710         gtk_cmoption_menu_set_menu(GTK_CMOPTION_MENU(search_type_opt), search_type);
711
712         quicksearch->search_type_opt = search_type_opt;
713 #endif
714         quicksearch_set_type(quicksearch, prefs_common.summary_quicksearch_type);
715
716         gtk_widget_show(search_type);
717
718 #if !GTK_CHECK_VERSION(2, 24, 0)
719         search_string_entry = gtk_combo_box_entry_new_text ();
720 #else
721         search_string_entry = gtk_combo_box_text_new_with_entry ();
722 #endif
723         gtk_combo_box_set_active(GTK_COMBO_BOX(search_string_entry), -1);
724
725         vbox = gtk_vbox_new(TRUE, 0);
726         gtk_box_pack_start(GTK_BOX(vbox), search_string_entry, FALSE, FALSE, 0);
727         gtk_box_pack_start(GTK_BOX(hbox_search), vbox, TRUE, TRUE, 4);
728
729         gtk_widget_show(vbox);
730         gtk_widget_show(search_string_entry);
731
732         search_hbox = gtk_hbox_new(FALSE, 5);
733         clear_search = gtk_button_new_from_stock(GTK_STOCK_CLEAR);
734         gtk_box_pack_start(GTK_BOX(search_hbox), clear_search,
735                            FALSE, FALSE, 0);
736         g_signal_connect(G_OBJECT(clear_search), "clicked",
737                          G_CALLBACK(clear_search_cb), quicksearch);
738         CLAWS_SET_TIP(clear_search,
739                              _("Clear the current search"));
740         gtk_widget_show(clear_search);
741
742         search_condition_expression = gtk_button_new_from_stock(GTK_STOCK_EDIT);
743         gtk_box_pack_start(GTK_BOX(search_hbox), search_condition_expression,
744                            FALSE, FALSE, 0);
745         g_signal_connect(G_OBJECT (search_condition_expression), "clicked",
746                          G_CALLBACK(search_condition_expr),
747                          quicksearch);
748         CLAWS_SET_TIP(search_condition_expression,
749                              _("Edit search criteria"));
750         gtk_widget_show(search_condition_expression);
751
752         search_description = gtk_button_new_from_stock(GTK_STOCK_INFO);
753         gtk_box_pack_start(GTK_BOX(search_hbox), search_description,
754                            FALSE, FALSE, 0);
755         g_signal_connect(G_OBJECT(search_description), "clicked",
756                          G_CALLBACK(search_description_cb), NULL);
757         CLAWS_SET_TIP(search_description,
758                              _("Information about extended symbols"));
759         gtk_widget_show(search_description);
760
761         gtk_box_pack_start(GTK_BOX(hbox_search), search_hbox, FALSE, FALSE, 2);
762         gtk_widget_show(search_hbox);
763
764         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((search_string_entry)))),
765                            "key_press_event",
766                            G_CALLBACK(searchbar_pressed),
767                            quicksearch);
768
769         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((search_string_entry)))),
770                          "changed",
771                          G_CALLBACK(searchbar_changed_cb),
772                          quicksearch);
773
774         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((search_string_entry)))),
775                          "focus_in_event",
776                          G_CALLBACK(searchbar_focus_evt_in),
777                          quicksearch);
778         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((search_string_entry)))),
779                          "focus_out_event",
780                          G_CALLBACK(searchbar_focus_evt_out),
781                          quicksearch);
782
783         quicksearch->hbox_search = hbox_search;
784         quicksearch->search_type = search_type;
785         quicksearch->search_string_entry = search_string_entry;
786         quicksearch->search_condition_expression = search_condition_expression;
787         quicksearch->search_description = search_description;
788         quicksearch->active = FALSE;
789         quicksearch->running = FALSE;
790         quicksearch->clear_search = clear_search;
791         quicksearch->in_typing = FALSE;
792         quicksearch->press_timeout_id = -1;
793         quicksearch->normal_search_strings = NULL;
794         quicksearch->extended_search_strings = NULL;
795
796         quicksearch_set_button(GTK_BUTTON(quicksearch->search_description), GTK_STOCK_INFO, _("_Information"));
797         quicksearch_set_button(GTK_BUTTON(quicksearch->search_condition_expression), GTK_STOCK_EDIT, _("_Edit"));
798         quicksearch_set_button(GTK_BUTTON(quicksearch->clear_search), GTK_STOCK_CLEAR, _("C_lear"));
799         
800         update_extended_buttons(quicksearch);
801
802         return quicksearch;
803 }
804
805 void quicksearch_relayout(QuickSearch *quicksearch)
806 {
807         switch (prefs_common.layout_mode) {
808         case NORMAL_LAYOUT:
809         case WIDE_LAYOUT:
810         case WIDE_MSGLIST_LAYOUT:
811                 quicksearch_set_button(GTK_BUTTON(quicksearch->search_description), GTK_STOCK_INFO, _("_Information"));
812                 quicksearch_set_button(GTK_BUTTON(quicksearch->search_condition_expression), GTK_STOCK_EDIT, _("_Edit"));
813                 quicksearch_set_button(GTK_BUTTON(quicksearch->clear_search), GTK_STOCK_CLEAR, _("C_lear"));
814                 break;
815         case SMALL_LAYOUT:
816         case VERTICAL_LAYOUT:
817                 quicksearch_set_button(GTK_BUTTON(quicksearch->search_description), GTK_STOCK_INFO, "");
818                 quicksearch_set_button(GTK_BUTTON(quicksearch->search_condition_expression), GTK_STOCK_EDIT, "");
819                 quicksearch_set_button(GTK_BUTTON(quicksearch->clear_search), GTK_STOCK_CLEAR, "");
820                 break;
821         }
822 }
823
824 GtkWidget *quicksearch_get_widget(QuickSearch *quicksearch)
825 {
826         return quicksearch->hbox_search;
827 }
828
829 void quicksearch_show(QuickSearch *quicksearch)
830 {
831         MainWindow *mainwin = mainwindow_get_mainwindow();
832         GtkWidget *ctree = NULL;
833         gtk_widget_show(quicksearch->hbox_search);
834         update_extended_buttons(quicksearch);
835         gtk_widget_grab_focus(
836                 GTK_WIDGET(gtk_bin_get_child(GTK_BIN((quicksearch->search_string_entry)))));
837
838         if (!mainwin || !mainwin->summaryview) {
839                 return;
840         }
841         
842         ctree = summary_get_main_widget(mainwin->summaryview);
843         
844         if (ctree && mainwin->summaryview->selected)
845                 gtk_cmctree_node_moveto(GTK_CMCTREE(ctree), 
846                                 mainwin->summaryview->selected, 
847                                 0, 0.5, 0);
848 }
849
850 void quicksearch_hide(QuickSearch *quicksearch)
851 {
852         if (quicksearch_has_sat_predicate(quicksearch)) {
853                 quicksearch_set(quicksearch, prefs_common.summary_quicksearch_type, "");
854                 quicksearch_set_active(quicksearch, FALSE);
855         }
856         gtk_widget_hide(quicksearch->hbox_search);
857 }
858
859 /*
860  *\brief        Sets the matchstring.
861  *
862  *\param        quicksearch quicksearch to set
863  *\param        matchstring the match string; it is duplicated, not stored
864  */
865 static void quicksearch_set_matchstring(QuickSearch *quicksearch,
866                                         const gchar *matchstring)
867 {
868         g_free(quicksearch->request.matchstring);
869         quicksearch->request.matchstring = g_strdup(matchstring);
870 }
871
872 void quicksearch_set(QuickSearch *quicksearch, AdvancedSearchType type, const gchar *matchstring)
873 {
874         quicksearch_set_type(quicksearch, type);
875
876         if (!matchstring || !(*matchstring))
877                 quicksearch->in_typing = FALSE;
878
879         quicksearch_set_matchstring(quicksearch, matchstring);
880                 
881         g_signal_handlers_block_by_func(G_OBJECT(gtk_bin_get_child(GTK_BIN((quicksearch->search_string_entry)))),
882                                         G_CALLBACK(searchbar_changed_cb), quicksearch);
883         gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((quicksearch->search_string_entry)))),
884                            matchstring);
885         g_signal_handlers_unblock_by_func(G_OBJECT(gtk_bin_get_child(GTK_BIN((quicksearch->search_string_entry)))),
886                                           G_CALLBACK(searchbar_changed_cb), quicksearch);
887
888         prefs_common.summary_quicksearch_type = type;
889
890         quicksearch_invoke_execute(quicksearch, FALSE);
891 }
892
893 gboolean quicksearch_has_sat_predicate(QuickSearch *quicksearch)
894 {
895         return quicksearch->active && advsearch_has_proper_predicate(quicksearch->asearch);
896 }
897
898 static void quicksearch_set_active(QuickSearch *quicksearch, gboolean active)
899 {
900 #if !GTK_CHECK_VERSION(3, 0, 0)
901         static GdkColor yellow;
902         static GdkColor red;
903         static GdkColor black;
904         static gboolean colors_initialised = FALSE;
905 #else
906         static GdkColor yellow = { (guint32)0, (guint16)0xf5, (guint16)0xf6, (guint16)0xbe };
907         static GdkColor red = { (guint32)0, (guint16)0xff, (guint16)0x70, (guint16)0x70 };
908         static GdkColor black = { (guint32)0, (guint16)0x0, (guint16)0x0, (guint16)0x0 };
909 #endif
910         gboolean error = FALSE;
911
912         
913         quicksearch->active = active;
914
915 #if !GTK_CHECK_VERSION(3, 0, 0)
916         if (!colors_initialised) {
917                 gdk_color_parse("#f5f6be", &yellow);
918                 gdk_color_parse("#000000", &black);
919                 gdk_color_parse("#ff7070", &red);
920                 colors_initialised = gdk_colormap_alloc_color(
921                         gdk_colormap_get_system(), &yellow, FALSE, TRUE);
922                 colors_initialised &= gdk_colormap_alloc_color(
923                         gdk_colormap_get_system(), &black, FALSE, TRUE);
924                 colors_initialised &= gdk_colormap_alloc_color(
925                         gdk_colormap_get_system(), &red, FALSE, TRUE);
926         }
927 #endif
928
929         if (active && 
930                 (prefs_common.summary_quicksearch_type == ADVANCED_SEARCH_EXTENDED
931                  && !advsearch_has_proper_predicate(quicksearch->asearch)))
932                 error = TRUE;
933
934         if (active) {
935                 gtk_widget_set_sensitive(quicksearch->clear_search, TRUE);
936 #if !GTK_CHECK_VERSION(3, 0, 0)
937                 if (colors_initialised) {
938 #endif
939                         gtk_widget_modify_base(
940                                 gtk_bin_get_child(GTK_BIN((quicksearch->search_string_entry))),
941                                 GTK_STATE_NORMAL, error ? &red : &yellow);
942                         gtk_widget_modify_text(
943                                 gtk_bin_get_child(GTK_BIN((quicksearch->search_string_entry))),
944                                 GTK_STATE_NORMAL, &black);
945 #if !GTK_CHECK_VERSION(3, 0, 0)
946                 }
947 #endif
948         } else {
949                 gtk_widget_set_sensitive(quicksearch->clear_search, FALSE);
950 #if !GTK_CHECK_VERSION(3, 0, 0)
951                 if (colors_initialised) {
952 #endif
953                         gtk_widget_modify_base(
954                                 gtk_bin_get_child(GTK_BIN((quicksearch->search_string_entry))),
955                                 GTK_STATE_NORMAL, NULL);
956                         gtk_widget_modify_text(
957                                 gtk_bin_get_child(GTK_BIN((quicksearch->search_string_entry))),
958                                 GTK_STATE_NORMAL, NULL);
959 #if !GTK_CHECK_VERSION(3, 0, 0)
960                 }
961 #endif
962         }
963
964         if (!active) {
965                 advsearch_abort(quicksearch->asearch);
966         }
967 }
968
969 void quicksearch_set_execute_callback(QuickSearch *quicksearch,
970                                       QuickSearchExecuteCallback callback,
971                                       gpointer data)
972 {
973         quicksearch->callback = callback;
974         quicksearch->callback_data = data;
975 }
976
977 static void quicksearch_set_running(QuickSearch *quicksearch, gboolean run)
978 {
979         quicksearch->running = run;
980 }
981
982 gboolean quicksearch_is_running(QuickSearch *quicksearch)
983 {
984         return quicksearch->running;
985 }
986
987 void quicksearch_pass_key(QuickSearch *quicksearch, guint val, GdkModifierType mod)
988 {
989         GtkEntry *entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN((quicksearch->search_string_entry))));
990         glong curpos = gtk_editable_get_position(GTK_EDITABLE(entry));
991         guint32 c;
992         char *str = g_strdup(gtk_entry_get_text(entry));
993         char *begin = str;
994         char *end = NULL;
995         char *new = NULL;
996         char key[7] = "";
997         guint char_len = 0;
998
999         if (gtk_editable_get_selection_bounds(GTK_EDITABLE(entry), NULL, NULL)) {
1000                 /* remove selection */
1001                 gtk_editable_delete_selection(GTK_EDITABLE(entry));
1002                 curpos = gtk_editable_get_position(GTK_EDITABLE(entry));
1003                 /* refresh string */
1004                 g_free(str);
1005                 str = g_strdup(gtk_entry_get_text(entry));
1006                 begin = str;
1007         }
1008
1009         if (!(c = gdk_keyval_to_unicode(val))) {
1010                 g_free(str);
1011                 return;
1012         }
1013         char_len = g_unichar_to_utf8(c, key);
1014         if (char_len < 0)
1015                 return;
1016         key[char_len] = '\0';
1017         if (curpos < g_utf8_strlen(str, -1)) {
1018                 gchar *stop = g_utf8_offset_to_pointer(begin, curpos);
1019                 end = g_strdup(g_utf8_offset_to_pointer(str, curpos));
1020                 *stop = '\0';
1021                 new = g_strdup_printf("%s%s%s", begin, key, end);
1022                 gtk_entry_set_text(entry, new);
1023                 g_free(end);
1024         } else {
1025                 new = g_strdup_printf("%s%s", begin, key);
1026                 gtk_entry_set_text(entry, new);
1027         }
1028         g_free(str);
1029         g_free(new);
1030         gtk_editable_set_position(GTK_EDITABLE(entry), curpos+1);
1031
1032 }
1033
1034 gboolean quicksearch_is_in_typing(QuickSearch *quicksearch)
1035 {
1036         return quicksearch->in_typing;
1037 }
1038
1039 void quicksearch_set_search_strings(QuickSearch *quicksearch)
1040 {
1041         GList *strings = prefs_common.summary_quicksearch_history;
1042         gchar *newstr = NULL;
1043         MatcherList *matcher_list = NULL;
1044
1045         if (!strings)
1046                 return;
1047
1048         matcher_parser_disable_warnings(TRUE);
1049         
1050         do {
1051                 newstr = advsearch_expand_search_string((gchar *) strings->data);
1052                 if (newstr && newstr[0] != '\0') {
1053                         if (!strchr(newstr, ' ')) {
1054                                 quicksearch->normal_search_strings =
1055                                         g_list_append(
1056                                                 quicksearch->normal_search_strings,
1057                                                 g_strdup(strings->data));
1058                                 g_free(newstr);
1059                                 continue;
1060                         }
1061                         
1062                         matcher_list = matcher_parser_get_cond(newstr, FALSE);
1063                         g_free(newstr);
1064                         
1065                         if (matcher_list) {
1066                                 quicksearch->extended_search_strings =
1067                                         g_list_prepend(
1068                                                 quicksearch->extended_search_strings,
1069                                                 g_strdup(strings->data));
1070                                 matcherlist_free(matcher_list);
1071                         } else
1072                                 quicksearch->normal_search_strings =
1073                                         g_list_prepend(
1074                                                 quicksearch->normal_search_strings,
1075                                                 g_strdup(strings->data));
1076                 }
1077         
1078         } while ((strings = g_list_next(strings)) != NULL);
1079
1080         matcher_parser_disable_warnings(FALSE); 
1081
1082         quicksearch->normal_search_strings = g_list_reverse(quicksearch->normal_search_strings);
1083         quicksearch->extended_search_strings = g_list_reverse(quicksearch->extended_search_strings);
1084
1085         quicksearch_set_popdown_strings(quicksearch);
1086 }
1087