Statistics
| Revision:

root / src / summary_search.c @ 1

History | View | Annotate | Download (13.5 KB)

1
/*
2
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3
 * Copyright (C) 1999-2004 Hiroyuki Yamamoto
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
 */
19

    
20
#ifdef HAVE_CONFIG_H
21
#  include "config.h"
22
#endif
23

    
24
#include "defs.h"
25

    
26
#include <glib.h>
27
#include <gdk/gdkkeysyms.h>
28
#include <gtk/gtkwidget.h>
29
#include <gtk/gtkwindow.h>
30
#include <gtk/gtkvbox.h>
31
#include <gtk/gtktable.h>
32
#include <gtk/gtkoptionmenu.h>
33
#include <gtk/gtklabel.h>
34
#include <gtk/gtkentry.h>
35
#include <gtk/gtkhbox.h>
36
#include <gtk/gtkcheckbutton.h>
37
#include <gtk/gtkhbbox.h>
38
#include <gtk/gtkbutton.h>
39
#include <gtk/gtkctree.h>
40
#include <stdio.h>
41
#include <stdlib.h>
42
#include <string.h>
43

    
44
#include "intl.h"
45
#include "main.h"
46
#include "summary_search.h"
47
#include "summaryview.h"
48
#include "messageview.h"
49
#include "mainwindow.h"
50
#include "menu.h"
51
#include "utils.h"
52
#include "gtkutils.h"
53
#include "manage_window.h"
54
#include "alertpanel.h"
55

    
56
static GtkWidget *window;
57
static GtkWidget *bool_optmenu;
58
static GtkWidget *from_entry;
59
static GtkWidget *to_entry;
60
static GtkWidget *subject_entry;
61
static GtkWidget *body_entry;
62
static GtkWidget *case_checkbtn;
63
static GtkWidget *backward_checkbtn;
64
static GtkWidget *all_checkbtn;
65
static GtkWidget *search_btn;
66
static GtkWidget *clear_btn;
67
static GtkWidget *close_btn;
68

    
69
static void summary_search_create(SummaryView *summaryview);
70
static void summary_search_execute(GtkButton *button, gpointer data);
71
static void summary_search_clear(GtkButton *button, gpointer data);
72
static void from_activated(void);
73
static void to_activated(void);
74
static void subject_activated(void);
75
static void body_activated(void);
76
static void all_clicked(GtkButton *button);
77
static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
78
                            gpointer data);
79

    
80
void summary_search(SummaryView *summaryview)
81
{
82
        if (!window)
83
                summary_search_create(summaryview);
84
        else
85
                gtk_widget_hide(window);
86

    
87
        gtk_widget_grab_focus(search_btn);
88
        gtk_widget_grab_focus(subject_entry);
89
        gtk_widget_show(window);
90
}
91

    
92
static void summary_search_create(SummaryView *summaryview)
93
{
94
        GtkWidget *vbox1;
95
        GtkWidget *bool_hbox;
96
        GtkWidget *bool_menu;
97
        GtkWidget *menuitem;
98
        GtkWidget *table1;
99
        GtkWidget *from_label;
100
        GtkWidget *to_label;
101
        GtkWidget *subject_label;
102
        GtkWidget *body_label;
103
        GtkWidget *checkbtn_hbox;
104
        GtkWidget *confirm_area;
105

    
106
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
107
        gtk_window_set_title(GTK_WINDOW (window), _("Search messages"));
108
        gtk_widget_set_size_request(window, 450, -1);
109
        gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, TRUE);
110
        gtk_container_set_border_width(GTK_CONTAINER (window), 8);
111
        g_signal_connect(G_OBJECT(window), "delete_event",
112
                         G_CALLBACK(gtk_widget_hide_on_delete), NULL);
113
        g_signal_connect(G_OBJECT(window), "key_press_event",
114
                         G_CALLBACK(key_pressed), NULL);
115
        MANAGE_WINDOW_SIGNALS_CONNECT(window);
116

    
117
        vbox1 = gtk_vbox_new (FALSE, 0);
118
        gtk_widget_show (vbox1);
119
        gtk_container_add (GTK_CONTAINER (window), vbox1);
120

    
121
        bool_hbox = gtk_hbox_new(FALSE, 4);
122
        gtk_widget_show(bool_hbox);
123
        gtk_box_pack_start(GTK_BOX(vbox1), bool_hbox, FALSE, FALSE, 0);
124

    
125
        bool_optmenu = gtk_option_menu_new();
126
        gtk_widget_show(bool_optmenu);
127
        gtk_box_pack_start(GTK_BOX(bool_hbox), bool_optmenu, FALSE, FALSE, 0);
128

    
129
        bool_menu = gtk_menu_new();
130
        MENUITEM_ADD(bool_menu, menuitem, _("Match any of the following"), 0);
131
        MENUITEM_ADD(bool_menu, menuitem, _("Match all of the following"), 1);
132
        gtk_option_menu_set_menu(GTK_OPTION_MENU(bool_optmenu), bool_menu);
133

    
134
        table1 = gtk_table_new (4, 3, FALSE);
135
        gtk_widget_show (table1);
136
        gtk_box_pack_start (GTK_BOX (vbox1), table1, TRUE, TRUE, 0);
137
        gtk_container_set_border_width (GTK_CONTAINER (table1), 4);
138
        gtk_table_set_row_spacings (GTK_TABLE (table1), 8);
139
        gtk_table_set_col_spacings (GTK_TABLE (table1), 8);
140

    
141
        from_entry = gtk_entry_new ();
142
        gtk_widget_show (from_entry);
143
        gtk_table_attach (GTK_TABLE (table1), from_entry, 1, 3, 0, 1,
144
                          GTK_EXPAND|GTK_FILL, 0, 0, 0);
145
        g_signal_connect(G_OBJECT(from_entry), "activate",
146
                         G_CALLBACK(from_activated), summaryview);
147

    
148
        to_entry = gtk_entry_new ();
149
        gtk_widget_show (to_entry);
150
        gtk_table_attach (GTK_TABLE (table1), to_entry, 1, 3, 1, 2,
151
                          GTK_EXPAND|GTK_FILL, 0, 0, 0);
152
        g_signal_connect(G_OBJECT(to_entry), "activate",
153
                         G_CALLBACK(to_activated), summaryview);
154

    
155
        subject_entry = gtk_entry_new ();
156
        gtk_widget_show (subject_entry);
157
        gtk_table_attach (GTK_TABLE (table1), subject_entry, 1, 3, 2, 3,
158
                          GTK_EXPAND|GTK_FILL, 0, 0, 0);
159
        g_signal_connect(G_OBJECT(subject_entry), "activate",
160
                         G_CALLBACK(subject_activated), summaryview);
161

    
162
        body_entry = gtk_entry_new ();
163
        gtk_widget_show (body_entry);
164
        gtk_table_attach (GTK_TABLE (table1), body_entry, 1, 3, 3, 4,
165
                          GTK_EXPAND|GTK_FILL, 0, 0, 0);
166
        g_signal_connect(G_OBJECT(body_entry), "activate",
167
                         G_CALLBACK(body_activated), summaryview);
168

    
169
        from_label = gtk_label_new (_("From:"));
170
        gtk_widget_show (from_label);
171
        gtk_table_attach (GTK_TABLE (table1), from_label, 0, 1, 0, 1,
172
                          GTK_FILL, 0, 0, 0);
173
        gtk_label_set_justify (GTK_LABEL (from_label), GTK_JUSTIFY_RIGHT);
174
        gtk_misc_set_alignment (GTK_MISC (from_label), 1, 0.5);
175

    
176
        to_label = gtk_label_new (_("To:"));
177
        gtk_widget_show (to_label);
178
        gtk_table_attach (GTK_TABLE (table1), to_label, 0, 1, 1, 2,
179
                          GTK_FILL, 0, 0, 0);
180
        gtk_label_set_justify (GTK_LABEL (to_label), GTK_JUSTIFY_RIGHT);
181
        gtk_misc_set_alignment (GTK_MISC (to_label), 1, 0.5);
182

    
183
        subject_label = gtk_label_new (_("Subject:"));
184
        gtk_widget_show (subject_label);
185
        gtk_table_attach (GTK_TABLE (table1), subject_label, 0, 1, 2, 3,
186
                          GTK_FILL, 0, 0, 0);
187
        gtk_label_set_justify (GTK_LABEL (subject_label), GTK_JUSTIFY_RIGHT);
188
        gtk_misc_set_alignment (GTK_MISC (subject_label), 1, 0.5);
189

    
190
        body_label = gtk_label_new (_("Body:"));
191
        gtk_widget_show (body_label);
192
        gtk_table_attach (GTK_TABLE (table1), body_label, 0, 1, 3, 4,
193
                          GTK_FILL, 0, 0, 0);
194
        gtk_label_set_justify (GTK_LABEL (body_label), GTK_JUSTIFY_RIGHT);
195
        gtk_misc_set_alignment (GTK_MISC (body_label), 1, 0.5);
196

    
197
        checkbtn_hbox = gtk_hbox_new (FALSE, 8);
198
        gtk_widget_show (checkbtn_hbox);
199
        gtk_box_pack_start (GTK_BOX (vbox1), checkbtn_hbox, TRUE, TRUE, 0);
200
        gtk_container_set_border_width (GTK_CONTAINER (checkbtn_hbox), 8);
201

    
202
        case_checkbtn = gtk_check_button_new_with_label (_("Case sensitive"));
203
        gtk_widget_show (case_checkbtn);
204
        gtk_box_pack_start (GTK_BOX (checkbtn_hbox), case_checkbtn,
205
                            FALSE, FALSE, 0);
206

    
207
        backward_checkbtn =
208
                gtk_check_button_new_with_label (_("Backward search"));
209
        gtk_widget_show (backward_checkbtn);
210
        gtk_box_pack_start (GTK_BOX (checkbtn_hbox), backward_checkbtn,
211
                            FALSE, FALSE, 0);
212

    
213
        all_checkbtn =
214
                gtk_check_button_new_with_label (_("Select all matched"));
215
        gtk_widget_show (all_checkbtn);
216
        gtk_box_pack_start (GTK_BOX (checkbtn_hbox), all_checkbtn,
217
                            FALSE, FALSE, 0);
218
        g_signal_connect(G_OBJECT(all_checkbtn), "clicked",
219
                         G_CALLBACK(all_clicked), summaryview);
220

    
221
        gtkut_button_set_create(&confirm_area,
222
                                &search_btn, _("Search"),
223
                                &clear_btn,  _("Clear"),
224
                                &close_btn,  _("Close"));
225
        gtk_widget_show (confirm_area);
226
        gtk_box_pack_start (GTK_BOX (vbox1), confirm_area, FALSE, FALSE, 0);
227
        gtk_widget_grab_default(search_btn);
228

    
229
        g_signal_connect(G_OBJECT(search_btn), "clicked",
230
                         G_CALLBACK(summary_search_execute), summaryview);
231
        g_signal_connect(G_OBJECT(clear_btn), "clicked",
232
                         G_CALLBACK(summary_search_clear), summaryview);
233
        g_signal_connect_closure
234
                (G_OBJECT(close_btn), "clicked",
235
                 g_cclosure_new_swap(G_CALLBACK(gtk_widget_hide),
236
                                     window, NULL),
237
                 FALSE);
238
}
239

    
240
static void summary_search_execute(GtkButton *button, gpointer data)
241
{
242
        SummaryView *summaryview = data;
243
        GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
244
        GtkCTreeNode *node;
245
        MsgInfo *msginfo;
246
        gboolean bool_and;
247
        gboolean case_sens;
248
        gboolean backward;
249
        gboolean search_all;
250
        gboolean all_searched = FALSE;
251
        gboolean matched;
252
        gboolean body_matched;
253
        const gchar *from_str, *to_str, *subject_str, *body_str;
254
        StrFindFunc str_find_func;
255

    
256
        if (summary_is_locked(summaryview)) return;
257
        summary_lock(summaryview);
258

    
259
        bool_and = menu_get_option_menu_active_index
260
                (GTK_OPTION_MENU(bool_optmenu));
261
        case_sens = gtk_toggle_button_get_active
262
                (GTK_TOGGLE_BUTTON(case_checkbtn));
263
        backward = gtk_toggle_button_get_active
264
                (GTK_TOGGLE_BUTTON(backward_checkbtn));
265
        search_all = gtk_toggle_button_get_active
266
                (GTK_TOGGLE_BUTTON(all_checkbtn));
267

    
268
        if (case_sens)
269
                str_find_func = str_find;
270
        else
271
                str_find_func = str_case_find;
272

    
273
        from_str    = gtk_entry_get_text(GTK_ENTRY(from_entry));
274
        to_str      = gtk_entry_get_text(GTK_ENTRY(to_entry));
275
        subject_str = gtk_entry_get_text(GTK_ENTRY(subject_entry));
276
        body_str    = gtk_entry_get_text(GTK_ENTRY(body_entry));
277

    
278
        if (search_all) {
279
                gtk_clist_freeze(GTK_CLIST(ctree));
280
                gtk_clist_unselect_all(GTK_CLIST(ctree));
281
                node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
282
                backward = FALSE;
283
        } else if (!summaryview->selected) {
284
                if (backward)
285
                        node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list_end);
286
                else
287
                        node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
288

    
289
                if (!node) {
290
                        summary_unlock(summaryview);
291
                        return;
292
                }
293
        } else {
294
                if (backward)
295
                        node = gtkut_ctree_node_prev
296
                                (ctree, summaryview->selected);
297
                else
298
                        node = gtkut_ctree_node_next
299
                                (ctree, summaryview->selected);
300
        }
301

    
302
        if (*body_str)
303
                main_window_cursor_wait(summaryview->mainwin);
304

    
305
        for (;;) {
306
                if (!node) {
307
                        gchar *str;
308
                        AlertValue val;
309

    
310
                        if (search_all) {
311
                                gtk_clist_thaw(GTK_CLIST(ctree));
312
                                break;
313
                        }
314

    
315
                        if (all_searched) {
316
                                alertpanel_message
317
                                        (_("Search failed"),
318
                                         _("Search string not found."),
319
                                         ALERT_WARNING);
320
                                break;
321
                        }
322

    
323
                        if (backward)
324
                                str = _("Beginning of list reached; continue from end?");
325
                        else
326
                                str = _("End of list reached; continue from beginning?");
327

    
328
                        val = alertpanel(_("Search finished"), str,
329
                                         _("Yes"), _("No"), NULL);
330
                        if (G_ALERTDEFAULT == val) {
331
                                if (backward)
332
                                        node = GTK_CTREE_NODE
333
                                                (GTK_CLIST(ctree)->row_list_end);
334
                                else
335
                                        node = GTK_CTREE_NODE
336
                                                (GTK_CLIST(ctree)->row_list);
337

    
338
                                all_searched = TRUE;
339

    
340
                                manage_window_focus_in(window, NULL, NULL);
341
                        } else
342
                                break;
343
                }
344

    
345

    
346
                msginfo = gtk_ctree_node_get_row_data(ctree, node);
347
                body_matched = FALSE;
348

    
349
                if (bool_and) {
350
                        matched = TRUE;
351
                        if (*from_str) {
352
                                if (!msginfo->from ||
353
                                    !str_find_func(msginfo->from, from_str))
354
                                        matched = FALSE;
355
                        }
356
                        if (matched && *to_str) {
357
                                if (!msginfo->to ||
358
                                    !str_find_func(msginfo->to, to_str))
359
                                        matched = FALSE;
360
                        }
361
                        if (matched && *subject_str) {
362
                                if (!msginfo->subject ||
363
                                    !str_find_func(msginfo->subject, subject_str))
364
                                        matched = FALSE;
365
                        }
366
                        if (matched && *body_str) {
367
                                if (procmime_find_string(msginfo, body_str,
368
                                                         str_find_func))
369
                                        body_matched = TRUE;
370
                                else
371
                                        matched = FALSE;
372
                        }
373
                        if (matched && !*from_str && !*to_str &&
374
                            !*subject_str && !*body_str)
375
                                matched = FALSE;
376
                } else {
377
                        matched = FALSE;
378
                        if (*from_str && msginfo->from) {
379
                                if (str_find_func(msginfo->from, from_str))
380
                                        matched = TRUE;
381
                        }
382
                        if (!matched && *to_str && msginfo->to) {
383
                                if (str_find_func(msginfo->to, to_str))
384
                                        matched = TRUE;
385
                        }
386
                        if (!matched && *subject_str && msginfo->subject) {
387
                                if (str_find_func(msginfo->subject, subject_str))
388
                                        matched = TRUE;
389
                        }
390
                        if (!matched && *body_str) {
391
                                if (procmime_find_string(msginfo, body_str,
392
                                                         str_find_func)) {
393
                                        matched = TRUE;
394
                                        body_matched = TRUE;
395
                                }
396
                        }
397
                }
398

    
399
                if (matched) {
400
                        if (search_all)
401
                                gtk_ctree_select(ctree, node);
402
                        else {
403
                                if (messageview_is_visible
404
                                        (summaryview->messageview)) {
405
                                        summary_unlock(summaryview);
406
                                        summary_select_node
407
                                                (summaryview, node, TRUE, TRUE);
408
                                        summary_lock(summaryview);
409
                                        if (body_matched) {
410
                                                messageview_search_string
411
                                                        (summaryview->messageview,
412
                                                         body_str, case_sens);
413
                                        }
414
                                } else {
415
                                        summary_select_node
416
                                                (summaryview, node, FALSE, TRUE);
417
                                }
418
                                break;
419
                        }
420
                }
421

    
422
                node = backward ? gtkut_ctree_node_prev(ctree, node)
423
                                : gtkut_ctree_node_next(ctree, node);
424
        }
425

    
426
        if (*body_str)
427
                main_window_cursor_normal(summaryview->mainwin);
428

    
429
        summary_unlock(summaryview);
430
}
431

    
432
static void summary_search_clear(GtkButton *button, gpointer data)
433
{
434
        gtk_editable_delete_text(GTK_EDITABLE(from_entry),    0, -1);
435
        gtk_editable_delete_text(GTK_EDITABLE(to_entry),      0, -1);
436
        gtk_editable_delete_text(GTK_EDITABLE(subject_entry), 0, -1);
437
        gtk_editable_delete_text(GTK_EDITABLE(body_entry),    0, -1);
438
}
439

    
440
static void from_activated(void)
441
{
442
        gtk_widget_grab_focus(to_entry);
443
}
444

    
445
static void to_activated(void)
446
{
447
        gtk_widget_grab_focus(subject_entry);
448
}
449

    
450
static void subject_activated(void)
451
{
452
        gtk_button_clicked(GTK_BUTTON(search_btn));
453
}
454

    
455
static void body_activated(void)
456
{
457
        gtk_button_clicked(GTK_BUTTON(search_btn));
458
}
459

    
460
static void all_clicked(GtkButton *button)
461
{
462
        if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)))
463
                gtk_widget_set_sensitive(backward_checkbtn, FALSE);
464
        else
465
                gtk_widget_set_sensitive(backward_checkbtn, TRUE);
466
}
467

    
468
static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
469
                            gpointer data)
470
{
471
        if (event && event->keyval == GDK_Escape)
472
                gtk_widget_hide(window);
473
        return FALSE;
474
}