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