2012-11-08 [colin] 3.8.1cvs114
[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         2,
487         N_("Extended Search"),
488         N_("Extended Search allows the user to define criteria that messages must "
489            "have in order to match and be displayed in the message list.\n"
490            "The following symbols can be used:"),
491         search_descr_strings
492 };
493
494 static void search_description_cb(GtkWidget *widget)
495 {
496         search_descr.parent = mainwindow_get_mainwindow()->window;
497         description_window_create(&search_descr);
498 };
499
500 static gboolean clear_search_cb(GtkMenuItem *widget, gpointer data)
501 {
502         QuickSearch *quicksearch = (QuickSearch *)data;
503
504         if (!quicksearch->active)
505                 return TRUE;
506
507         quicksearch_set(quicksearch, prefs_common.summary_quicksearch_type, "");
508
509         return TRUE;
510 };
511
512 static void search_condition_expr_done(MatcherList * matchers)
513 {
514         gchar *str;
515
516         cm_return_if_fail(
517                         mainwindow_get_mainwindow()->summaryview->quicksearch != NULL);
518
519         if (matchers == NULL)
520                 return;
521
522         str = matcherlist_to_string(matchers);
523
524         if (str != NULL) {
525                 quicksearch_set(mainwindow_get_mainwindow()->summaryview->quicksearch,
526                                 prefs_common.summary_quicksearch_type, str);
527                 g_free(str);
528
529                 /* add expression to history list and exec quicksearch */
530                 searchbar_run(mainwindow_get_mainwindow()->summaryview->quicksearch, FALSE);
531         }
532 }
533
534 static gboolean search_condition_expr(GtkMenuItem *widget, gpointer data)
535 {
536         gchar * cond_str;
537         MatcherList * matchers = NULL;
538         
539         cm_return_val_if_fail(
540                         mainwindow_get_mainwindow()->summaryview->quicksearch != NULL,
541                         FALSE);
542
543         /* re-use the current quicksearch value if it's a condition expression,
544            otherwise ignore it silently */
545         cond_str = quicksearch_get_text(mainwindow_get_mainwindow()->summaryview->quicksearch);
546
547         if (*cond_str != '\0') {
548                 matchers = matcher_parser_get_cond((gchar*)cond_str, NULL);
549         }
550
551         prefs_matcher_open(matchers, search_condition_expr_done);
552
553         if (matchers != NULL)
554                 matcherlist_free(matchers);
555
556         g_free(cond_str);
557
558         return TRUE;
559 };
560
561 static void quicksearch_set_button(GtkButton *button, const gchar *icon, const gchar *text)
562 {
563         GList *children = gtk_container_get_children(GTK_CONTAINER(button));
564         GList *cur;
565         GtkWidget *box;
566         gboolean icon_visible;
567
568         g_object_get(gtk_settings_get_default(), 
569                                          "gtk-button-images", &icon_visible, 
570                                          NULL);
571
572         for (cur = children; cur; cur = cur->next)
573                 gtk_container_remove(GTK_CONTAINER(button), GTK_WIDGET(cur->data));
574         
575         g_list_free(children);
576         box = gtk_hbox_new(FALSE, 0);
577         
578         gtk_container_add(GTK_CONTAINER(button), box);
579         if (icon_visible || !text || !*text)
580                 gtk_box_pack_start(GTK_BOX(box), gtk_image_new_from_stock(icon, 
581                         GTK_ICON_SIZE_BUTTON), FALSE, FALSE, 0);
582         gtk_box_pack_start(GTK_BOX(box), gtk_label_new_with_mnemonic(text), FALSE, FALSE, 0);
583         gtk_widget_show_all(box);
584 }
585
586 static void quicksearch_error(gpointer data)
587 {
588         alertpanel_error(_("Something went wrong during search. Please check your logs."));
589 }
590
591 QuickSearch *quicksearch_new()
592 {
593         QuickSearch *quicksearch;
594
595         GtkWidget *hbox_search;
596 #if !GTK_CHECK_VERSION(3, 0, 0)
597         GtkWidget *search_type_opt;
598 #endif
599         GtkWidget *search_type;
600         GtkWidget *search_string_entry;
601         GtkWidget *search_hbox;
602         GtkWidget *search_description;
603         GtkWidget *clear_search;
604         GtkWidget *search_condition_expression;
605         GtkWidget *menuitem;
606         GtkWidget *vbox;
607
608         quicksearch = g_new0(QuickSearch, 1);
609
610         quicksearch->asearch = advsearch_new();
611         advsearch_set_on_error_cb(quicksearch->asearch, quicksearch_error, NULL);
612
613         /* init. values initally found in quicksearch_new().
614            There's no need to init. all pointers to NULL since we use g_new0
615          */
616         quicksearch->active = FALSE;
617         quicksearch->running = FALSE;
618         quicksearch->in_typing = FALSE;
619         quicksearch->press_timeout_id = -1;
620         quicksearch->normal_search_strings = NULL;
621         quicksearch->extended_search_strings = NULL;
622
623         /* quick search */
624         hbox_search = gtk_hbox_new(FALSE, 0);
625
626 #if !GTK_CHECK_VERSION(3, 0, 0)
627         search_type_opt = gtk_cmoption_menu_new();
628         gtk_widget_show(search_type_opt);
629         gtk_box_pack_start(GTK_BOX(hbox_search), search_type_opt, FALSE, FALSE, 0);
630 #endif
631
632         search_type = gtk_menu_new();
633         MENUITEM_ADD (search_type, menuitem,
634                         prefs_common_translated_header_name("Subject"), ADVANCED_SEARCH_SUBJECT);
635         g_signal_connect(G_OBJECT(menuitem), "activate",
636                          G_CALLBACK(searchtype_changed),
637                          quicksearch);
638         MENUITEM_ADD (search_type, menuitem,
639                         prefs_common_translated_header_name("From"), ADVANCED_SEARCH_FROM);
640         g_signal_connect(G_OBJECT(menuitem), "activate",
641                          G_CALLBACK(searchtype_changed),
642                          quicksearch);
643         MENUITEM_ADD (search_type, menuitem,
644                         prefs_common_translated_header_name("To"), ADVANCED_SEARCH_TO);
645         g_signal_connect(G_OBJECT(menuitem), "activate",
646                          G_CALLBACK(searchtype_changed),
647                          quicksearch);
648         MENUITEM_ADD (search_type, menuitem,
649                         prefs_common_translated_header_name("Tag"), ADVANCED_SEARCH_TAG);
650         g_signal_connect(G_OBJECT(menuitem), "activate",
651                          G_CALLBACK(searchtype_changed),
652                          quicksearch);
653         MENUITEM_ADD (search_type, menuitem,
654                         _("From/To/Subject/Tag"), ADVANCED_SEARCH_MIXED);
655         g_signal_connect(G_OBJECT(menuitem), "activate",
656                          G_CALLBACK(searchtype_changed),
657                          quicksearch);
658         MENUITEM_ADD (search_type, menuitem, _("Extended"), ADVANCED_SEARCH_EXTENDED);
659         g_signal_connect(G_OBJECT(menuitem), "activate",
660                          G_CALLBACK(searchtype_changed),
661                          quicksearch);
662
663         gtk_menu_shell_append(GTK_MENU_SHELL(search_type), gtk_separator_menu_item_new());
664
665         menuitem = gtk_check_menu_item_new_with_label(_("Recursive"));
666         gtk_menu_shell_append(GTK_MENU_SHELL(search_type), menuitem);
667
668         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
669                                         prefs_common.summary_quicksearch_recurse);
670         g_signal_connect(G_OBJECT(menuitem), "activate",
671                          G_CALLBACK(searchtype_recursive_changed),
672                          quicksearch);
673
674         menuitem = gtk_check_menu_item_new_with_label(_("Sticky"));
675         gtk_menu_shell_append(GTK_MENU_SHELL(search_type), menuitem);
676
677         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
678                                         prefs_common.summary_quicksearch_sticky);
679
680         g_signal_connect(G_OBJECT(menuitem), "activate",
681                          G_CALLBACK(searchtype_sticky_changed),
682                          quicksearch);
683
684         menuitem = gtk_check_menu_item_new_with_label(_("Type-ahead"));
685         gtk_menu_shell_append(GTK_MENU_SHELL(search_type), menuitem);
686
687         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
688                                         prefs_common.summary_quicksearch_dynamic);
689
690         quicksearch->dynamic_menuitem = menuitem;
691
692         g_signal_connect(G_OBJECT(menuitem), "activate",
693                          G_CALLBACK(searchtype_dynamic_changed),
694                          quicksearch);
695
696         menuitem = gtk_check_menu_item_new_with_label(_("Run on select"));
697         gtk_menu_shell_append(GTK_MENU_SHELL(search_type), menuitem);
698
699         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
700                                         prefs_common.summary_quicksearch_autorun);
701
702         quicksearch->autorun_menuitem = menuitem;
703
704         g_signal_connect(G_OBJECT(menuitem), "activate",
705                          G_CALLBACK(searchtype_autorun_changed),
706                          quicksearch);
707
708 #if !GTK_CHECK_VERSION(3, 0, 0)
709         gtk_cmoption_menu_set_menu(GTK_CMOPTION_MENU(search_type_opt), search_type);
710
711         quicksearch->search_type_opt = search_type_opt;
712 #endif
713         quicksearch_set_type(quicksearch, prefs_common.summary_quicksearch_type);
714
715         gtk_widget_show(search_type);
716
717         search_string_entry = gtk_combo_box_entry_new_text ();
718         gtk_combo_box_set_active(GTK_COMBO_BOX(search_string_entry), -1);
719
720         vbox = gtk_vbox_new(TRUE, 0);
721         gtk_box_pack_start(GTK_BOX(vbox), search_string_entry, FALSE, FALSE, 0);
722         gtk_box_pack_start(GTK_BOX(hbox_search), vbox, TRUE, TRUE, 4);
723
724         gtk_widget_show(vbox);
725         gtk_widget_show(search_string_entry);
726
727         search_hbox = gtk_hbox_new(FALSE, 5);
728         clear_search = gtk_button_new_from_stock(GTK_STOCK_CLEAR);
729         gtk_box_pack_start(GTK_BOX(search_hbox), clear_search,
730                            FALSE, FALSE, 0);
731         g_signal_connect(G_OBJECT(clear_search), "clicked",
732                          G_CALLBACK(clear_search_cb), quicksearch);
733         CLAWS_SET_TIP(clear_search,
734                              _("Clear the current search"));
735         gtk_widget_show(clear_search);
736
737         search_condition_expression = gtk_button_new_from_stock(GTK_STOCK_EDIT);
738         gtk_box_pack_start(GTK_BOX(search_hbox), search_condition_expression,
739                            FALSE, FALSE, 0);
740         g_signal_connect(G_OBJECT (search_condition_expression), "clicked",
741                          G_CALLBACK(search_condition_expr),
742                          quicksearch);
743         CLAWS_SET_TIP(search_condition_expression,
744                              _("Edit search criteria"));
745         gtk_widget_show(search_condition_expression);
746
747         search_description = gtk_button_new_from_stock(GTK_STOCK_INFO);
748         gtk_box_pack_start(GTK_BOX(search_hbox), search_description,
749                            FALSE, FALSE, 0);
750         g_signal_connect(G_OBJECT(search_description), "clicked",
751                          G_CALLBACK(search_description_cb), NULL);
752         CLAWS_SET_TIP(search_description,
753                              _("Information about extended symbols"));
754         gtk_widget_show(search_description);
755
756         gtk_box_pack_start(GTK_BOX(hbox_search), search_hbox, FALSE, FALSE, 2);
757         gtk_widget_show(search_hbox);
758
759         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((search_string_entry)))),
760                            "key_press_event",
761                            G_CALLBACK(searchbar_pressed),
762                            quicksearch);
763
764         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((search_string_entry)))),
765                          "changed",
766                          G_CALLBACK(searchbar_changed_cb),
767                          quicksearch);
768
769         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((search_string_entry)))),
770                          "focus_in_event",
771                          G_CALLBACK(searchbar_focus_evt_in),
772                          quicksearch);
773         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((search_string_entry)))),
774                          "focus_out_event",
775                          G_CALLBACK(searchbar_focus_evt_out),
776                          quicksearch);
777
778         quicksearch->hbox_search = hbox_search;
779         quicksearch->search_type = search_type;
780         quicksearch->search_string_entry = search_string_entry;
781         quicksearch->search_condition_expression = search_condition_expression;
782         quicksearch->search_description = search_description;
783         quicksearch->active = FALSE;
784         quicksearch->running = FALSE;
785         quicksearch->clear_search = clear_search;
786         quicksearch->in_typing = FALSE;
787         quicksearch->press_timeout_id = -1;
788         quicksearch->normal_search_strings = NULL;
789         quicksearch->extended_search_strings = NULL;
790
791         quicksearch_set_button(GTK_BUTTON(quicksearch->search_description), GTK_STOCK_INFO, _("_Information"));
792         quicksearch_set_button(GTK_BUTTON(quicksearch->search_condition_expression), GTK_STOCK_EDIT, _("_Edit"));
793         quicksearch_set_button(GTK_BUTTON(quicksearch->clear_search), GTK_STOCK_CLEAR, _("C_lear"));
794         
795         update_extended_buttons(quicksearch);
796
797         return quicksearch;
798 }
799
800 void quicksearch_relayout(QuickSearch *quicksearch)
801 {
802         switch (prefs_common.layout_mode) {
803         case NORMAL_LAYOUT:
804         case WIDE_LAYOUT:
805         case WIDE_MSGLIST_LAYOUT:
806                 quicksearch_set_button(GTK_BUTTON(quicksearch->search_description), GTK_STOCK_INFO, _("_Information"));
807                 quicksearch_set_button(GTK_BUTTON(quicksearch->search_condition_expression), GTK_STOCK_EDIT, _("_Edit"));
808                 quicksearch_set_button(GTK_BUTTON(quicksearch->clear_search), GTK_STOCK_CLEAR, _("C_lear"));
809                 break;
810         case SMALL_LAYOUT:
811         case VERTICAL_LAYOUT:
812                 quicksearch_set_button(GTK_BUTTON(quicksearch->search_description), GTK_STOCK_INFO, "");
813                 quicksearch_set_button(GTK_BUTTON(quicksearch->search_condition_expression), GTK_STOCK_EDIT, "");
814                 quicksearch_set_button(GTK_BUTTON(quicksearch->clear_search), GTK_STOCK_CLEAR, "");
815                 break;
816         }
817 }
818
819 GtkWidget *quicksearch_get_widget(QuickSearch *quicksearch)
820 {
821         return quicksearch->hbox_search;
822 }
823
824 void quicksearch_show(QuickSearch *quicksearch)
825 {
826         MainWindow *mainwin = mainwindow_get_mainwindow();
827         GtkWidget *ctree = NULL;
828         gtk_widget_show(quicksearch->hbox_search);
829         update_extended_buttons(quicksearch);
830         gtk_widget_grab_focus(
831                 GTK_WIDGET(gtk_bin_get_child(GTK_BIN((quicksearch->search_string_entry)))));
832
833         if (!mainwin || !mainwin->summaryview) {
834                 return;
835         }
836         
837         ctree = summary_get_main_widget(mainwin->summaryview);
838         
839         if (ctree && mainwin->summaryview->selected)
840                 gtk_cmctree_node_moveto(GTK_CMCTREE(ctree), 
841                                 mainwin->summaryview->selected, 
842                                 0, 0.5, 0);
843 }
844
845 void quicksearch_hide(QuickSearch *quicksearch)
846 {
847         if (quicksearch_has_sat_predicate(quicksearch)) {
848                 quicksearch_set(quicksearch, prefs_common.summary_quicksearch_type, "");
849                 quicksearch_set_active(quicksearch, FALSE);
850         }
851         gtk_widget_hide(quicksearch->hbox_search);
852 }
853
854 /*
855  *\brief        Sets the matchstring.
856  *
857  *\param        quicksearch quicksearch to set
858  *\param        matchstring the match string; it is duplicated, not stored
859  */
860 static void quicksearch_set_matchstring(QuickSearch *quicksearch,
861                                         const gchar *matchstring)
862 {
863         g_free(quicksearch->request.matchstring);
864         quicksearch->request.matchstring = g_strdup(matchstring);
865 }
866
867 void quicksearch_set(QuickSearch *quicksearch, AdvancedSearchType type, const gchar *matchstring)
868 {
869         quicksearch_set_type(quicksearch, type);
870
871         if (!matchstring || !(*matchstring))
872                 quicksearch->in_typing = FALSE;
873
874         quicksearch_set_matchstring(quicksearch, matchstring);
875                 
876         g_signal_handlers_block_by_func(G_OBJECT(gtk_bin_get_child(GTK_BIN((quicksearch->search_string_entry)))),
877                                         G_CALLBACK(searchbar_changed_cb), quicksearch);
878         gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((quicksearch->search_string_entry)))),
879                            matchstring);
880         g_signal_handlers_unblock_by_func(G_OBJECT(gtk_bin_get_child(GTK_BIN((quicksearch->search_string_entry)))),
881                                           G_CALLBACK(searchbar_changed_cb), quicksearch);
882
883         prefs_common.summary_quicksearch_type = type;
884
885         quicksearch_invoke_execute(quicksearch, FALSE);
886 }
887
888 gboolean quicksearch_has_sat_predicate(QuickSearch *quicksearch)
889 {
890         return quicksearch->active && advsearch_has_proper_predicate(quicksearch->asearch);
891 }
892
893 static void quicksearch_set_active(QuickSearch *quicksearch, gboolean active)
894 {
895 #if !GTK_CHECK_VERSION(3, 0, 0)
896         static GdkColor yellow;
897         static GdkColor red;
898         static GdkColor black;
899         static gboolean colors_initialised = FALSE;
900 #else
901         static GdkColor yellow = { (guint32)0, (guint16)0xf5, (guint16)0xf6, (guint16)0xbe };
902         static GdkColor red = { (guint32)0, (guint16)0xff, (guint16)0x70, (guint16)0x70 };
903         static GdkColor black = { (guint32)0, (guint16)0x0, (guint16)0x0, (guint16)0x0 };
904 #endif
905         gboolean error = FALSE;
906
907         
908         quicksearch->active = active;
909
910 #if !GTK_CHECK_VERSION(3, 0, 0)
911         if (!colors_initialised) {
912                 gdk_color_parse("#f5f6be", &yellow);
913                 gdk_color_parse("#000000", &black);
914                 gdk_color_parse("#ff7070", &red);
915                 colors_initialised = gdk_colormap_alloc_color(
916                         gdk_colormap_get_system(), &yellow, FALSE, TRUE);
917                 colors_initialised &= gdk_colormap_alloc_color(
918                         gdk_colormap_get_system(), &black, FALSE, TRUE);
919                 colors_initialised &= gdk_colormap_alloc_color(
920                         gdk_colormap_get_system(), &red, FALSE, TRUE);
921         }
922 #endif
923
924         if (active && 
925                 (prefs_common.summary_quicksearch_type == ADVANCED_SEARCH_EXTENDED
926                  && !advsearch_has_proper_predicate(quicksearch->asearch)))
927                 error = TRUE;
928
929         if (active) {
930                 gtk_widget_set_sensitive(quicksearch->clear_search, TRUE);
931 #if !GTK_CHECK_VERSION(3, 0, 0)
932                 if (colors_initialised) {
933 #endif
934                         gtk_widget_modify_base(
935                                 gtk_bin_get_child(GTK_BIN((quicksearch->search_string_entry))),
936                                 GTK_STATE_NORMAL, error ? &red : &yellow);
937                         gtk_widget_modify_text(
938                                 gtk_bin_get_child(GTK_BIN((quicksearch->search_string_entry))),
939                                 GTK_STATE_NORMAL, &black);
940 #if !GTK_CHECK_VERSION(3, 0, 0)
941                 }
942 #endif
943         } else {
944                 gtk_widget_set_sensitive(quicksearch->clear_search, FALSE);
945                 if (colors_initialised) {
946                         gtk_widget_modify_base(
947                                 gtk_bin_get_child(GTK_BIN((quicksearch->search_string_entry))),
948                                 GTK_STATE_NORMAL, NULL);
949                         gtk_widget_modify_text(
950                                 gtk_bin_get_child(GTK_BIN((quicksearch->search_string_entry))),
951                                 GTK_STATE_NORMAL, NULL);
952 #if !GTK_CHECK_VERSION(3, 0, 0)
953                 }
954 #endif
955         }
956
957         if (!active) {
958                 advsearch_abort(quicksearch->asearch);
959         }
960 }
961
962 void quicksearch_set_execute_callback(QuickSearch *quicksearch,
963                                       QuickSearchExecuteCallback callback,
964                                       gpointer data)
965 {
966         quicksearch->callback = callback;
967         quicksearch->callback_data = data;
968 }
969
970 static void quicksearch_set_running(QuickSearch *quicksearch, gboolean run)
971 {
972         quicksearch->running = run;
973 }
974
975 gboolean quicksearch_is_running(QuickSearch *quicksearch)
976 {
977         return quicksearch->running;
978 }
979
980 void quicksearch_pass_key(QuickSearch *quicksearch, guint val, GdkModifierType mod)
981 {
982         GtkEntry *entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN((quicksearch->search_string_entry))));
983         glong curpos = gtk_editable_get_position(GTK_EDITABLE(entry));
984         guint32 c;
985         char *str = g_strdup(gtk_entry_get_text(entry));
986         char *begin = str;
987         char *end = NULL;
988         char *new = NULL;
989         char key[7] = "";
990         guint char_len = 0;
991
992         if (gtk_editable_get_selection_bounds(GTK_EDITABLE(entry), NULL, NULL)) {
993                 /* remove selection */
994                 gtk_editable_delete_selection(GTK_EDITABLE(entry));
995                 curpos = gtk_editable_get_position(GTK_EDITABLE(entry));
996                 /* refresh string */
997                 g_free(str);
998                 str = g_strdup(gtk_entry_get_text(entry));
999                 begin = str;
1000         }
1001
1002         if (!(c = gdk_keyval_to_unicode(val))) {
1003                 g_free(str);
1004                 return;
1005         }
1006         char_len = g_unichar_to_utf8(c, key);
1007         if (char_len < 0)
1008                 return;
1009         key[char_len] = '\0';
1010         if (curpos < g_utf8_strlen(str, -1)) {
1011                 gchar *stop = g_utf8_offset_to_pointer(begin, curpos);
1012                 end = g_strdup(g_utf8_offset_to_pointer(str, curpos));
1013                 *stop = '\0';
1014                 new = g_strdup_printf("%s%s%s", begin, key, end);
1015                 gtk_entry_set_text(entry, new);
1016                 g_free(end);
1017         } else {
1018                 new = g_strdup_printf("%s%s", begin, key);
1019                 gtk_entry_set_text(entry, new);
1020         }
1021         g_free(str);
1022         g_free(new);
1023         gtk_editable_set_position(GTK_EDITABLE(entry), curpos+1);
1024
1025 }
1026
1027 gboolean quicksearch_is_in_typing(QuickSearch *quicksearch)
1028 {
1029         return quicksearch->in_typing;
1030 }
1031
1032 void quicksearch_set_search_strings(QuickSearch *quicksearch)
1033 {
1034         GList *strings = prefs_common.summary_quicksearch_history;
1035         gchar *newstr = NULL;
1036         MatcherList *matcher_list = NULL;
1037
1038         if (!strings)
1039                 return;
1040
1041         matcher_parser_disable_warnings(TRUE);
1042         
1043         do {
1044                 newstr = advsearch_expand_search_string((gchar *) strings->data);
1045                 if (newstr && newstr[0] != '\0') {
1046                         if (!strchr(newstr, ' ')) {
1047                                 quicksearch->normal_search_strings =
1048                                         g_list_append(
1049                                                 quicksearch->normal_search_strings,
1050                                                 g_strdup(strings->data));
1051                                 g_free(newstr);
1052                                 continue;
1053                         }
1054                         
1055                         matcher_list = matcher_parser_get_cond(newstr, FALSE);
1056                         g_free(newstr);
1057                         
1058                         if (matcher_list) {
1059                                 quicksearch->extended_search_strings =
1060                                         g_list_prepend(
1061                                                 quicksearch->extended_search_strings,
1062                                                 g_strdup(strings->data));
1063                                 matcherlist_free(matcher_list);
1064                         } else
1065                                 quicksearch->normal_search_strings =
1066                                         g_list_prepend(
1067                                                 quicksearch->normal_search_strings,
1068                                                 g_strdup(strings->data));
1069                 }
1070         
1071         } while ((strings = g_list_next(strings)) != NULL);
1072
1073         matcher_parser_disable_warnings(FALSE); 
1074
1075         quicksearch->normal_search_strings = g_list_reverse(quicksearch->normal_search_strings);
1076         quicksearch->extended_search_strings = g_list_reverse(quicksearch->extended_search_strings);
1077
1078         quicksearch_set_popdown_strings(quicksearch);
1079 }
1080