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