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