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