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