c3485c5c36c1cb0c0f6774264e02b2bb160be832
[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                                                                 search_string);
143                                         prefs_common.summary_quicksearch_history =
144                                                 add_history(prefs_common.summary_quicksearch_history,
145                                                                 search_string);
146                                 }
147                                 break;
148                         default:
149                                 quicksearch->normal_search_strings =
150                                         add_history(quicksearch->normal_search_strings,
151                                                         search_string);
152                                 prefs_common.summary_quicksearch_history =
153                                         add_history(prefs_common.summary_quicksearch_history,
154                                                         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 != NULL && (event->keyval == GDK_KEY_ISO_Left_Tab)) {
336                 /* Shift+Tab moves focus "back" */
337                 gtk_widget_grab_focus(quicksearch->search_type_opt);
338                 return TRUE;
339         }
340         if (event != NULL && (event->keyval == GDK_KEY_Tab)) {
341                 /* Just Tab moves focus "forwards" */
342                 gtk_widget_grab_focus(quicksearch->clear_search);
343                 return TRUE;
344         }
345
346         if (event != NULL && (event->keyval == GDK_KEY_Escape)) {
347                 gchar *str;
348
349                 quicksearch->in_typing = FALSE;
350
351                 str = quicksearch_get_text(quicksearch);
352                 cm_return_val_if_fail(str != NULL, TRUE);
353
354                 /* If the string entry is empty -> hide quicksearch bar. If not -> empty it */
355                 if (!*str) {
356                         summaryview_activate_quicksearch(
357                                 mainwindow_get_mainwindow()->summaryview, 
358                                 FALSE);
359                 } else {
360                         quicksearch_set(quicksearch, prefs_common.summary_quicksearch_type, "");
361                         gtk_widget_grab_focus(
362                                         mainwindow_get_mainwindow()->summaryview->ctree);
363                 }
364                 g_free(str);
365                 return TRUE;
366         }
367
368         if (event != NULL && (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_KP_Enter)) {
369                 if (quicksearch->press_timeout_id != 0) {
370                         g_source_remove(quicksearch->press_timeout_id);
371                         quicksearch->press_timeout_id = 0;
372                 }
373                 quicksearch->in_typing = FALSE;
374                 /* add expression to history list and exec quicksearch */
375                 searchbar_run(quicksearch, FALSE);
376
377                 g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event");
378                 return TRUE;
379         }
380
381         if (event && (event->keyval == GDK_KEY_Down || event->keyval == GDK_KEY_Up)) {
382                 combobox_set_value_from_arrow_key(
383                                 GTK_COMBO_BOX(quicksearch->search_string_entry),
384                                 event->keyval);
385                 return TRUE;
386         }
387
388         return FALSE;
389 }
390
391 static gboolean searchtype_changed(GtkMenuItem *widget, gpointer data)
392 {
393         QuickSearch *quicksearch = (QuickSearch *)data;
394
395         prefs_common.summary_quicksearch_type = GPOINTER_TO_INT(g_object_get_data(
396                                    G_OBJECT(GTK_MENU_ITEM(gtk_menu_get_active(
397                                    GTK_MENU(quicksearch->search_type)))), MENU_VAL_ID));
398         quicksearch->request.type = prefs_common.summary_quicksearch_type;
399
400         /* Show extended search description button, only when Extended is selected */
401         update_extended_buttons(quicksearch);
402         quicksearch_set_popdown_strings(quicksearch);
403
404         quicksearch_invoke_execute(quicksearch, FALSE);
405         gtk_widget_grab_focus(quicksearch->search_string_entry);
406
407         return TRUE;
408 }
409
410 static gboolean searchtype_recursive_changed(GtkMenuItem *widget, gpointer data)
411 {
412         QuickSearch *quicksearch = (QuickSearch *)data;
413         gboolean checked = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));
414
415         prefs_common.summary_quicksearch_recurse = checked;
416
417         /* reselect the search type */
418         quicksearch_set_type(quicksearch, prefs_common.summary_quicksearch_type);
419
420         quicksearch_invoke_execute(quicksearch, FALSE);
421
422         return TRUE;
423 }
424
425 static gboolean searchtype_sticky_changed(GtkMenuItem *widget, gpointer data)
426 {
427         QuickSearch *quicksearch = (QuickSearch *)data;
428         gboolean checked = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));
429
430         prefs_common.summary_quicksearch_sticky = checked;
431
432         /* reselect the search type */
433         quicksearch_set_type(quicksearch, prefs_common.summary_quicksearch_type);
434
435         return TRUE;
436 }
437
438 static gboolean searchtype_dynamic_changed(GtkMenuItem *widget, gpointer data)
439 {
440         QuickSearch *quicksearch = (QuickSearch *)data;
441         gboolean checked = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));
442
443         prefs_common.summary_quicksearch_dynamic = checked;
444         if (checked)
445                 gtk_check_menu_item_set_active(
446                                 GTK_CHECK_MENU_ITEM(quicksearch->autorun_menuitem),
447                                 FALSE);
448
449         /* reselect the search type */
450         quicksearch_set_type(quicksearch, prefs_common.summary_quicksearch_type);
451
452         return TRUE;
453 }
454
455 static gboolean searchtype_autorun_changed(GtkMenuItem *widget, gpointer data)
456 {
457         QuickSearch *quicksearch = (QuickSearch *)data;
458         gboolean checked = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));
459
460         prefs_common.summary_quicksearch_autorun = checked;
461         if (checked)
462                 gtk_check_menu_item_set_active(
463                                 GTK_CHECK_MENU_ITEM(quicksearch->dynamic_menuitem),
464                                 FALSE);
465
466         /* reselect the search type */
467         quicksearch_set_type(quicksearch, prefs_common.summary_quicksearch_type);
468
469         return TRUE;
470 }
471
472 /*
473  * Strings describing how to use Extended Search
474  *
475  * When adding new lines, remember to put 2 strings for each line
476  */
477 static gchar *search_descr_strings[] = {
478         "a",     N_("all messages"),
479         "ag #",  N_("messages whose age is greater than # days"),
480         "al #",  N_("messages whose age is less than # days"),
481         "agh #",  N_("messages whose age is greater than # hours"),
482         "alh #",  N_("messages whose age is less than # hours"),
483         "b S",   N_("messages which contain S in the message body"),
484         "B S",   N_("messages which contain S in the whole message"),
485         "c S",   N_("messages carbon-copied to S"),
486         "C S",   N_("message is either To: or Cc: to S"),
487         "D",     N_("deleted messages"), /** how I can filter deleted messages **/
488         "da \"YYYY-MM-dd HH:mm:ss\"",  N_("messages whose date is after requested date "
489                                           "(time is optional)"),
490         "db \"YYYY-MM-dd HH:mm:ss\"",  N_("messages whose date is before requested date "
491                                           "(time is optional)"),
492         "e S",   N_("messages which contain S in the Sender field"),
493         "E S",   N_("true if execute \"S\" succeeds"),
494         "f S",   N_("messages originating from user S"),
495         "F",     N_("forwarded messages"),
496         "ha",    N_("messages which have attachments"),
497         "h S",   N_("messages which contain S in any header name or value"),
498         "H S",   N_("messages which contain S in the value of any header"),
499         "i S",   N_("messages which contain S in Message-ID header"),
500         "I S",   N_("messages which contain S in In-Reply-To header"),
501         "k #",   N_("messages which are marked with color #"),
502         "L",     N_("locked messages"),
503         "n S",   N_("messages which are in newsgroup S"),
504         "N",     N_("new messages"),
505         "O",     N_("old messages"),
506         "p",     N_("incomplete messages (not entirely downloaded)"),
507         "r",     N_("messages which you have replied to"),
508         "R",     N_("read messages"),
509         "s S",   N_("messages which contain S in subject"),
510         "se #",  N_("messages whose score is equal to # points"),
511         "sg #",  N_("messages whose score is greater than # points"),
512         "sl #",  N_("messages whose score is lower than # points"),
513         "Se #",  N_("messages whose size is equal to # bytes"),
514         "Sg #",  N_("messages whose size is greater than # bytes"),
515         "Ss #",  N_("messages whose size is smaller than # bytes"),
516         "t S",   N_("messages which have been sent to S"),
517         "tg S",  N_("messages which tags contain S"),
518         "tagged",N_("messages which have tag(s)"),
519         "T",     N_("marked messages"),
520         "U",     N_("unread messages"),
521         "x S",   N_("messages which contain S in References header"),
522         "X \"cmd args\"", N_("messages returning 0 when passed to command - %F is message file"),
523         "y S",   N_("messages which contain S in X-Label header"),
524         "",      "" ,
525         "&amp;",         N_("logical AND operator"),
526         "|",     N_("logical OR operator"),
527         "! or ~",       N_("logical NOT operator"),
528         "%",     N_("case sensitive search"),
529         "&#x00023;", N_("match using regular expressions instead of substring search"),
530         "",      "" ,
531         " ",     N_("all filtering expressions are allowed, but cannot be mixed "
532                     "through logical operators with the expressions above"),
533         NULL,    NULL
534 };
535
536 static DescriptionWindow search_descr = {
537         NULL,
538         NULL,
539         FALSE,
540         2,
541         N_("Extended Search"),
542         N_("Extended Search allows the user to define criteria that messages must "
543            "have in order to match and be displayed in the message list.\n"
544            "The following symbols can be used:"),
545         search_descr_strings
546 };
547
548 static void search_description_cb(GtkWidget *widget)
549 {
550         search_descr.parent = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "description_window");
551         description_window_create(&search_descr);
552 };
553
554 static gboolean clear_search_cb(GtkMenuItem *widget, gpointer data)
555 {
556         QuickSearch *quicksearch = (QuickSearch *)data;
557
558         if (!quicksearch->active)
559                 return TRUE;
560
561         quicksearch_set(quicksearch, prefs_common.summary_quicksearch_type, "");
562
563         return TRUE;
564 };
565
566 static void search_condition_expr_done(MatcherList * matchers)
567 {
568         gchar *str;
569
570         cm_return_if_fail(
571                         mainwindow_get_mainwindow()->summaryview->quicksearch != NULL);
572
573         if (matchers == NULL)
574                 return;
575
576         str = matcherlist_to_string(matchers);
577
578         if (str != NULL) {
579                 quicksearch_set(mainwindow_get_mainwindow()->summaryview->quicksearch,
580                                 prefs_common.summary_quicksearch_type, str);
581                 g_free(str);
582
583                 /* add expression to history list and exec quicksearch */
584                 searchbar_run(mainwindow_get_mainwindow()->summaryview->quicksearch, FALSE);
585         }
586 }
587
588 static gboolean search_condition_expr(GtkMenuItem *widget, gpointer data)
589 {
590         gchar * cond_str;
591         MatcherList * matchers = NULL;
592         
593         cm_return_val_if_fail(
594                         mainwindow_get_mainwindow()->summaryview->quicksearch != NULL,
595                         FALSE);
596
597         /* re-use the current quicksearch value, expanding it so it also works
598          * with extended symbols */
599         cond_str = quicksearch_get_text(mainwindow_get_mainwindow()->summaryview->quicksearch);
600
601         if (*cond_str != '\0') {
602                 gchar *newstr = advsearch_expand_search_string(cond_str);
603
604                 if (newstr && newstr[0] != '\0')
605                         matchers = matcher_parser_get_cond(newstr, FALSE);
606                 g_free(newstr);
607         }
608
609         prefs_matcher_open(matchers, search_condition_expr_done);
610
611         if (matchers != NULL)
612                 matcherlist_free(matchers);
613
614         g_free(cond_str);
615
616         return TRUE;
617 };
618
619 static void quicksearch_set_button(GtkButton *button, const gchar *icon, const gchar *text)
620 {
621         GList *children = gtk_container_get_children(GTK_CONTAINER(button));
622         GList *cur;
623         GtkWidget *box;
624         gboolean icon_visible;
625
626         g_object_get(gtk_settings_get_default(), 
627                                          "gtk-button-images", &icon_visible, 
628                                          NULL);
629
630         for (cur = children; cur; cur = cur->next)
631                 gtk_container_remove(GTK_CONTAINER(button), GTK_WIDGET(cur->data));
632         
633         g_list_free(children);
634         box = gtk_hbox_new(FALSE, 0);
635         
636         gtk_container_add(GTK_CONTAINER(button), box);
637         if (icon_visible || !text || !*text)
638                 gtk_box_pack_start(GTK_BOX(box), gtk_image_new_from_stock(icon, 
639                         GTK_ICON_SIZE_BUTTON), FALSE, FALSE, 0);
640         gtk_box_pack_start(GTK_BOX(box), gtk_label_new_with_mnemonic(text), FALSE, FALSE, 0);
641         gtk_widget_show_all(box);
642 }
643
644 static void quicksearch_error(gpointer data)
645 {
646         alertpanel_error(_("Something went wrong during search. Please check your logs."));
647 }
648
649 QuickSearch *quicksearch_new()
650 {
651         QuickSearch *quicksearch;
652
653         GtkWidget *hbox_search;
654 #if !GTK_CHECK_VERSION(3, 0, 0)
655         GtkWidget *search_type_opt;
656 #endif
657         GtkWidget *search_type;
658         GtkWidget *search_string_entry;
659         GtkWidget *search_hbox;
660         GtkWidget *search_description;
661         GtkWidget *clear_search;
662         GtkWidget *search_condition_expression;
663         GtkWidget *menuitem;
664         GtkWidget *vbox;
665
666         quicksearch = g_new0(QuickSearch, 1);
667
668         quicksearch->asearch = advsearch_new();
669         advsearch_set_on_error_cb(quicksearch->asearch, quicksearch_error, NULL);
670
671         /* init. values initally found in quicksearch_new().
672            There's no need to init. all pointers to NULL since we use g_new0
673          */
674         quicksearch->active = FALSE;
675         quicksearch->running = FALSE;
676         quicksearch->in_typing = FALSE;
677         quicksearch->press_timeout_id = 0;
678         quicksearch->normal_search_strings = NULL;
679         quicksearch->extended_search_strings = NULL;
680
681         /* quick search */
682         hbox_search = gtk_hbox_new(FALSE, 0);
683
684 #if !GTK_CHECK_VERSION(3, 0, 0)
685         search_type_opt = gtk_cmoption_menu_new();
686         gtk_widget_show(search_type_opt);
687         gtk_box_pack_start(GTK_BOX(hbox_search), search_type_opt, FALSE, FALSE, 0);
688 #endif
689
690         search_type = gtk_menu_new();
691         MENUITEM_ADD (search_type, menuitem,
692                         prefs_common_translated_header_name("Subject"), ADVANCED_SEARCH_SUBJECT);
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("From"), ADVANCED_SEARCH_FROM);
698         g_signal_connect(G_OBJECT(menuitem), "activate",
699                          G_CALLBACK(searchtype_changed),
700                          quicksearch);
701         MENUITEM_ADD (search_type, menuitem,
702                         prefs_common_translated_header_name("To"), ADVANCED_SEARCH_TO);
703         g_signal_connect(G_OBJECT(menuitem), "activate",
704                          G_CALLBACK(searchtype_changed),
705                          quicksearch);
706         MENUITEM_ADD (search_type, menuitem,
707                         prefs_common_translated_header_name("Tag"), ADVANCED_SEARCH_TAG);
708         g_signal_connect(G_OBJECT(menuitem), "activate",
709                          G_CALLBACK(searchtype_changed),
710                          quicksearch);
711         MENUITEM_ADD (search_type, menuitem,
712                         _("From/To/Cc/Subject/Tag"), ADVANCED_SEARCH_MIXED);
713         g_signal_connect(G_OBJECT(menuitem), "activate",
714                          G_CALLBACK(searchtype_changed),
715                          quicksearch);
716         MENUITEM_ADD (search_type, menuitem, _("Extended"), ADVANCED_SEARCH_EXTENDED);
717         g_signal_connect(G_OBJECT(menuitem), "activate",
718                          G_CALLBACK(searchtype_changed),
719                          quicksearch);
720
721         gtk_menu_shell_append(GTK_MENU_SHELL(search_type), gtk_separator_menu_item_new());
722
723         menuitem = gtk_check_menu_item_new_with_label(_("Recursive"));
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_recurse);
728         g_signal_connect(G_OBJECT(menuitem), "activate",
729                          G_CALLBACK(searchtype_recursive_changed),
730                          quicksearch);
731
732         menuitem = gtk_check_menu_item_new_with_label(_("Sticky"));
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_sticky);
737
738         g_signal_connect(G_OBJECT(menuitem), "activate",
739                          G_CALLBACK(searchtype_sticky_changed),
740                          quicksearch);
741
742         menuitem = gtk_check_menu_item_new_with_label(_("Type-ahead"));
743         gtk_menu_shell_append(GTK_MENU_SHELL(search_type), menuitem);
744
745         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
746                                         prefs_common.summary_quicksearch_dynamic);
747
748         quicksearch->dynamic_menuitem = menuitem;
749
750         g_signal_connect(G_OBJECT(menuitem), "activate",
751                          G_CALLBACK(searchtype_dynamic_changed),
752                          quicksearch);
753
754         menuitem = gtk_check_menu_item_new_with_label(_("Run on select"));
755         gtk_menu_shell_append(GTK_MENU_SHELL(search_type), menuitem);
756
757         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
758                                         prefs_common.summary_quicksearch_autorun);
759
760         quicksearch->autorun_menuitem = menuitem;
761
762         g_signal_connect(G_OBJECT(menuitem), "activate",
763                          G_CALLBACK(searchtype_autorun_changed),
764                          quicksearch);
765
766 #if !GTK_CHECK_VERSION(3, 0, 0)
767         gtk_cmoption_menu_set_menu(GTK_CMOPTION_MENU(search_type_opt), search_type);
768
769         quicksearch->search_type_opt = search_type_opt;
770 #endif
771         quicksearch_set_type(quicksearch, prefs_common.summary_quicksearch_type);
772
773         gtk_widget_show(search_type);
774
775         search_string_entry = gtk_combo_box_text_new_with_entry ();
776         gtk_combo_box_set_active(GTK_COMBO_BOX(search_string_entry), -1);
777
778         vbox = gtk_vbox_new(TRUE, 0);
779         gtk_box_pack_start(GTK_BOX(vbox), search_string_entry, FALSE, FALSE, 0);
780         gtk_box_pack_start(GTK_BOX(hbox_search), vbox, TRUE, TRUE, 4);
781
782         gtk_widget_show(vbox);
783         gtk_widget_show(search_string_entry);
784
785         search_hbox = gtk_hbox_new(FALSE, 5);
786         clear_search = gtk_button_new_from_stock(GTK_STOCK_CLEAR);
787         gtk_box_pack_start(GTK_BOX(search_hbox), clear_search,
788                            FALSE, FALSE, 0);
789         g_signal_connect(G_OBJECT(clear_search), "clicked",
790                          G_CALLBACK(clear_search_cb), quicksearch);
791         CLAWS_SET_TIP(clear_search,
792                              _("Clear the current search"));
793         gtk_widget_show(clear_search);
794
795         search_condition_expression = gtk_button_new_from_stock(GTK_STOCK_EDIT);
796         gtk_box_pack_start(GTK_BOX(search_hbox), search_condition_expression,
797                            FALSE, FALSE, 0);
798         g_signal_connect(G_OBJECT (search_condition_expression), "clicked",
799                          G_CALLBACK(search_condition_expr),
800                          quicksearch);
801         CLAWS_SET_TIP(search_condition_expression,
802                              _("Edit search criteria"));
803         gtk_widget_show(search_condition_expression);
804
805         search_description = gtk_button_new_from_stock(GTK_STOCK_INFO);
806         gtk_box_pack_start(GTK_BOX(search_hbox), search_description,
807                            FALSE, FALSE, 0);
808         g_signal_connect(G_OBJECT(search_description), "clicked",
809                          G_CALLBACK(search_description_cb), NULL);
810         CLAWS_SET_TIP(search_description,
811                              _("Information about extended symbols"));
812         gtk_widget_show(search_description);
813
814         gtk_box_pack_start(GTK_BOX(hbox_search), search_hbox, FALSE, FALSE, 2);
815         gtk_widget_show(search_hbox);
816
817         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((search_string_entry)))),
818                            "key_press_event",
819                            G_CALLBACK(searchbar_pressed),
820                            quicksearch);
821
822         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((search_string_entry)))),
823                          "changed",
824                          G_CALLBACK(searchbar_changed_cb),
825                          quicksearch);
826
827         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((search_string_entry)))),
828                          "focus_in_event",
829                          G_CALLBACK(searchbar_focus_evt_in),
830                          quicksearch);
831         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((search_string_entry)))),
832                          "focus_out_event",
833                          G_CALLBACK(searchbar_focus_evt_out),
834                          quicksearch);
835
836         quicksearch->hbox_search = hbox_search;
837         quicksearch->search_type = search_type;
838         quicksearch->search_string_entry = search_string_entry;
839         quicksearch->search_condition_expression = search_condition_expression;
840         quicksearch->search_description = search_description;
841         quicksearch->active = FALSE;
842         quicksearch->running = FALSE;
843         quicksearch->clear_search = clear_search;
844         quicksearch->in_typing = FALSE;
845         quicksearch->press_timeout_id = 0;
846         quicksearch->normal_search_strings = NULL;
847         quicksearch->extended_search_strings = NULL;
848
849         quicksearch_set_button(GTK_BUTTON(quicksearch->search_description), GTK_STOCK_INFO, _("_Information"));
850         quicksearch_set_button(GTK_BUTTON(quicksearch->search_condition_expression), GTK_STOCK_EDIT, _("E_dit"));
851         quicksearch_set_button(GTK_BUTTON(quicksearch->clear_search), GTK_STOCK_CLEAR, _("C_lear"));
852         
853         update_extended_buttons(quicksearch);
854
855         gtkut_convert_int_to_gdk_color(prefs_common.color[COL_QS_ACTIVE_BG],
856                                            &qs_active_bgcolor);
857         gtkut_convert_int_to_gdk_color(prefs_common.color[COL_QS_ACTIVE],
858                                            &qs_active_color);
859         gtkut_convert_int_to_gdk_color(prefs_common.color[COL_QS_ERROR_BG],
860                                            &qs_error_bgcolor);
861         gtkut_convert_int_to_gdk_color(prefs_common.color[COL_QS_ERROR],
862                                            &qs_error_color);
863
864         return quicksearch;
865 }
866
867 void quicksearch_relayout(QuickSearch *quicksearch)
868 {
869         switch (prefs_common.layout_mode) {
870         case NORMAL_LAYOUT:
871         case WIDE_LAYOUT:
872         case WIDE_MSGLIST_LAYOUT:
873                 quicksearch_set_button(GTK_BUTTON(quicksearch->search_description), GTK_STOCK_INFO, _("_Information"));
874                 quicksearch_set_button(GTK_BUTTON(quicksearch->search_condition_expression), GTK_STOCK_EDIT, _("E_dit"));
875                 quicksearch_set_button(GTK_BUTTON(quicksearch->clear_search), GTK_STOCK_CLEAR, _("C_lear"));
876                 break;
877         case SMALL_LAYOUT:
878         case VERTICAL_LAYOUT:
879                 quicksearch_set_button(GTK_BUTTON(quicksearch->search_description), GTK_STOCK_INFO, "");
880                 quicksearch_set_button(GTK_BUTTON(quicksearch->search_condition_expression), GTK_STOCK_EDIT, "");
881                 quicksearch_set_button(GTK_BUTTON(quicksearch->clear_search), GTK_STOCK_CLEAR, "");
882                 break;
883         }
884 }
885
886 GtkWidget *quicksearch_get_widget(QuickSearch *quicksearch)
887 {
888         return quicksearch->hbox_search;
889 }
890
891 GtkWidget *quicksearch_get_entry(QuickSearch *quicksearch)
892 {
893         return gtk_bin_get_child(GTK_BIN(quicksearch->search_string_entry));
894 }
895
896 void quicksearch_show(QuickSearch *quicksearch)
897 {
898         MainWindow *mainwin = mainwindow_get_mainwindow();
899         GtkWidget *ctree = NULL;
900         gtk_widget_show(quicksearch->hbox_search);
901         update_extended_buttons(quicksearch);
902         gtk_widget_grab_focus(quicksearch->search_string_entry);
903
904         if (!mainwin || !mainwin->summaryview) {
905                 return;
906         }
907         
908         ctree = summary_get_main_widget(mainwin->summaryview);
909         
910         if (ctree && mainwin->summaryview->selected)
911                 gtk_cmctree_node_moveto(GTK_CMCTREE(ctree), 
912                                 mainwin->summaryview->selected, 
913                                 0, 0.5, 0);
914 }
915
916 void quicksearch_hide(QuickSearch *quicksearch)
917 {
918         if (quicksearch_has_sat_predicate(quicksearch)) {
919                 quicksearch_set(quicksearch, prefs_common.summary_quicksearch_type, "");
920                 quicksearch_set_active(quicksearch, FALSE);
921         }
922         gtk_widget_hide(quicksearch->hbox_search);
923 }
924
925 /*
926  *\brief        Sets the matchstring.
927  *
928  *\param        quicksearch quicksearch to set
929  *\param        matchstring the match string; it is duplicated, not stored
930  */
931 static void quicksearch_set_matchstring(QuickSearch *quicksearch,
932                                         const gchar *matchstring)
933 {
934         g_free(quicksearch->request.matchstring);
935         quicksearch->request.matchstring = g_strdup(matchstring);
936 }
937
938 void quicksearch_set(QuickSearch *quicksearch, AdvancedSearchType type, const gchar *matchstring)
939 {
940         quicksearch_set_type(quicksearch, type);
941
942         if (!matchstring || !(*matchstring))
943                 quicksearch->in_typing = FALSE;
944
945         quicksearch_set_matchstring(quicksearch, matchstring);
946                 
947         g_signal_handlers_block_by_func(G_OBJECT(gtk_bin_get_child(GTK_BIN((quicksearch->search_string_entry)))),
948                                         G_CALLBACK(searchbar_changed_cb), quicksearch);
949         gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((quicksearch->search_string_entry)))),
950                            matchstring);
951         g_signal_handlers_unblock_by_func(G_OBJECT(gtk_bin_get_child(GTK_BIN((quicksearch->search_string_entry)))),
952                                           G_CALLBACK(searchbar_changed_cb), quicksearch);
953
954         prefs_common.summary_quicksearch_type = type;
955
956         quicksearch_invoke_execute(quicksearch, FALSE);
957 }
958
959 gboolean quicksearch_has_sat_predicate(QuickSearch *quicksearch)
960 {
961         return quicksearch->active && advsearch_has_proper_predicate(quicksearch->asearch);
962 }
963
964 static void quicksearch_set_active(QuickSearch *quicksearch, gboolean active)
965 {
966         gboolean error = FALSE;
967
968         quicksearch->active = active;
969
970         if (active && 
971                 (prefs_common.summary_quicksearch_type == ADVANCED_SEARCH_EXTENDED
972                  && !advsearch_has_proper_predicate(quicksearch->asearch)))
973                 error = TRUE;
974
975         if (active) {
976                 gtk_widget_set_sensitive(quicksearch->clear_search, TRUE);
977                         gtk_widget_modify_base(
978                                 gtk_bin_get_child(GTK_BIN((quicksearch->search_string_entry))),
979                                 GTK_STATE_NORMAL, error ? &qs_error_bgcolor : &qs_active_bgcolor);
980                         gtk_widget_modify_text(
981                                 gtk_bin_get_child(GTK_BIN((quicksearch->search_string_entry))),
982                                 GTK_STATE_NORMAL, error ? &qs_error_color : &qs_active_color);
983         } else {
984                 gtk_widget_set_sensitive(quicksearch->clear_search, FALSE);
985                         gtk_widget_modify_base(
986                                 gtk_bin_get_child(GTK_BIN((quicksearch->search_string_entry))),
987                                 GTK_STATE_NORMAL, NULL);
988                         gtk_widget_modify_text(
989                                 gtk_bin_get_child(GTK_BIN((quicksearch->search_string_entry))),
990                                 GTK_STATE_NORMAL, NULL);
991         }
992
993         if (!active) {
994                 advsearch_abort(quicksearch->asearch);
995         }
996 }
997
998 void quicksearch_set_execute_callback(QuickSearch *quicksearch,
999                                       QuickSearchExecuteCallback callback,
1000                                       gpointer data)
1001 {
1002         quicksearch->callback = callback;
1003         quicksearch->callback_data = data;
1004 }
1005
1006 static void quicksearch_set_running(QuickSearch *quicksearch, gboolean run)
1007 {
1008         quicksearch->running = run;
1009 }
1010
1011 gboolean quicksearch_is_running(QuickSearch *quicksearch)
1012 {
1013         return quicksearch->running;
1014 }
1015
1016 gboolean quicksearch_is_in_typing(QuickSearch *quicksearch)
1017 {
1018         return quicksearch->in_typing;
1019 }
1020
1021 void quicksearch_set_search_strings(QuickSearch *quicksearch)
1022 {
1023         GList *strings = prefs_common.summary_quicksearch_history;
1024         gchar *newstr = NULL;
1025         MatcherList *matcher_list = NULL;
1026
1027         if (!strings)
1028                 return;
1029
1030         matcher_parser_disable_warnings(TRUE);
1031         
1032         do {
1033                 newstr = advsearch_expand_search_string((gchar *) strings->data);
1034                 if (newstr && newstr[0] != '\0') {
1035                         if (!strchr(newstr, ' ')) {
1036                                 quicksearch->normal_search_strings =
1037                                         g_list_append(
1038                                                 quicksearch->normal_search_strings,
1039                                                 g_strdup(strings->data));
1040                         } else {
1041                                 matcher_list = matcher_parser_get_cond(newstr, FALSE);
1042                         
1043                                 if (matcher_list) {
1044                                         quicksearch->extended_search_strings =
1045                                                 g_list_prepend(
1046                                                         quicksearch->extended_search_strings,
1047                                                         g_strdup(strings->data));
1048                                         matcherlist_free(matcher_list);
1049                                 } else
1050                                         quicksearch->normal_search_strings =
1051                                                 g_list_prepend(
1052                                                         quicksearch->normal_search_strings,
1053                                                         g_strdup(strings->data));
1054                         }
1055                 }
1056                 g_free(newstr);
1057         
1058         } while ((strings = g_list_next(strings)) != NULL);
1059
1060         matcher_parser_disable_warnings(FALSE); 
1061
1062         quicksearch->normal_search_strings = g_list_reverse(quicksearch->normal_search_strings);
1063         quicksearch->extended_search_strings = g_list_reverse(quicksearch->extended_search_strings);
1064
1065         quicksearch_set_popdown_strings(quicksearch);
1066 }
1067