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