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