Statistics
| Revision:

root / src / summary_search.c @ 237

History | View | Annotate | Download (13.6 kB)

1
/*
2
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3
 * Copyright (C) 1999-2005 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 <glib/gi18n.h>
28
#include <gdk/gdkkeysyms.h>
29
#include <gtk/gtkwidget.h>
30
#include <gtk/gtkwindow.h>
31
#include <gtk/gtkvbox.h>
32
#include <gtk/gtktable.h>
33
#include <gtk/gtkoptionmenu.h>
34
#include <gtk/gtklabel.h>
35
#include <gtk/gtkentry.h>
36
#include <gtk/gtkhbox.h>
37
#include <gtk/gtkcheckbutton.h>
38
#include <gtk/gtkhbbox.h>
39
#include <gtk/gtkbutton.h>
40
#include <gtk/gtkmenuitem.h>
41
#include <gtk/gtkstock.h>
42
#include <stdio.h>
43
#include <stdlib.h>
44
#include <string.h>
45
46
#include "main.h"
47
#include "summary_search.h"
48
#include "summaryview.h"
49
#include "messageview.h"
50
#include "mainwindow.h"
51
#include "menu.h"
52
#include "utils.h"
53
#include "gtkutils.h"
54
#include "manage_window.h"
55
#include "alertpanel.h"
56
57
static GtkWidget *window;
58
static GtkWidget *bool_optmenu;
59
static GtkWidget *from_entry;
60
static GtkWidget *to_entry;
61
static GtkWidget *subject_entry;
62
static GtkWidget *body_entry;
63
static GtkWidget *case_checkbtn;
64
static GtkWidget *backward_checkbtn;
65
static GtkWidget *all_checkbtn;
66
static GtkWidget *search_btn;
67
static GtkWidget *clear_btn;
68
static GtkWidget *close_btn;
69
70
static void summary_search_create(SummaryView *summaryview);
71
static void summary_search_execute(GtkButton *button, gpointer data);
72
static void summary_search_clear(GtkButton *button, gpointer data);
73
static void from_activated(void);
74
static void to_activated(void);
75
static void subject_activated(void);
76
static void body_activated(void);
77
static void all_clicked(GtkButton *button);
78
static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
79
                            gpointer data);
80
81
void summary_search(SummaryView *summaryview)
82
{
83
        if (!window)
84
                summary_search_create(summaryview);
85
        else
86
                gtk_widget_hide(window);
87
88
        gtk_widget_grab_focus(search_btn);
89
        gtk_widget_grab_focus(subject_entry);
90
        gtk_widget_show(window);
91
}
92
93
static void summary_search_create(SummaryView *summaryview)
94
{
95
        GtkWidget *vbox1;
96
        GtkWidget *bool_hbox;
97
        GtkWidget *bool_menu;
98
        GtkWidget *menuitem;
99
        GtkWidget *table1;
100
        GtkWidget *from_label;
101
        GtkWidget *to_label;
102
        GtkWidget *subject_label;
103
        GtkWidget *body_label;
104
        GtkWidget *checkbtn_hbox;
105
        GtkWidget *confirm_area;
106
107
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
108
        gtk_window_set_title(GTK_WINDOW (window), _("Search messages"));
109
        gtk_widget_set_size_request(window, 450, -1);
110
        gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, TRUE);
111
        gtk_container_set_border_width(GTK_CONTAINER (window), 8);
112
        g_signal_connect(G_OBJECT(window), "delete_event",
113
                         G_CALLBACK(gtk_widget_hide_on_delete), NULL);
114
        g_signal_connect(G_OBJECT(window), "key_press_event",
115
                         G_CALLBACK(key_pressed), NULL);
116
        MANAGE_WINDOW_SIGNALS_CONNECT(window);
117
118
        vbox1 = gtk_vbox_new (FALSE, 0);
119
        gtk_widget_show (vbox1);
120
        gtk_container_add (GTK_CONTAINER (window), vbox1);
121
122
        bool_hbox = gtk_hbox_new(FALSE, 4);
123
        gtk_widget_show(bool_hbox);
124
        gtk_box_pack_start(GTK_BOX(vbox1), bool_hbox, FALSE, FALSE, 0);
125
126
        bool_optmenu = gtk_option_menu_new();
127
        gtk_widget_show(bool_optmenu);
128
        gtk_box_pack_start(GTK_BOX(bool_hbox), bool_optmenu, FALSE, FALSE, 0);
129
130
        bool_menu = gtk_menu_new();
131
        MENUITEM_ADD(bool_menu, menuitem, _("Match any of the following"), 0);
132
        MENUITEM_ADD(bool_menu, menuitem, _("Match all of the following"), 1);
133
        gtk_option_menu_set_menu(GTK_OPTION_MENU(bool_optmenu), bool_menu);
134
135
        table1 = gtk_table_new (4, 3, FALSE);
136
        gtk_widget_show (table1);
137
        gtk_box_pack_start (GTK_BOX (vbox1), table1, TRUE, TRUE, 0);
138
        gtk_container_set_border_width (GTK_CONTAINER (table1), 4);
139
        gtk_table_set_row_spacings (GTK_TABLE (table1), 8);
140
        gtk_table_set_col_spacings (GTK_TABLE (table1), 8);
141
142
        from_entry = gtk_entry_new ();
143
        gtk_widget_show (from_entry);
144
        gtk_table_attach (GTK_TABLE (table1), from_entry, 1, 3, 0, 1,
145
                          GTK_EXPAND|GTK_FILL, 0, 0, 0);
146
        g_signal_connect(G_OBJECT(from_entry), "activate",
147
                         G_CALLBACK(from_activated), summaryview);
148
149
        to_entry = gtk_entry_new ();
150
        gtk_widget_show (to_entry);
151
        gtk_table_attach (GTK_TABLE (table1), to_entry, 1, 3, 1, 2,
152
                          GTK_EXPAND|GTK_FILL, 0, 0, 0);
153
        g_signal_connect(G_OBJECT(to_entry), "activate",
154
                         G_CALLBACK(to_activated), summaryview);
155
156
        subject_entry = gtk_entry_new ();
157
        gtk_widget_show (subject_entry);
158
        gtk_table_attach (GTK_TABLE (table1), subject_entry, 1, 3, 2, 3,
159
                          GTK_EXPAND|GTK_FILL, 0, 0, 0);
160
        g_signal_connect(G_OBJECT(subject_entry), "activate",
161
                         G_CALLBACK(subject_activated), summaryview);
162
163
        body_entry = gtk_entry_new ();
164
        gtk_widget_show (body_entry);
165
        gtk_table_attach (GTK_TABLE (table1), body_entry, 1, 3, 3, 4,
166
                          GTK_EXPAND|GTK_FILL, 0, 0, 0);
167
        g_signal_connect(G_OBJECT(body_entry), "activate",
168
                         G_CALLBACK(body_activated), summaryview);
169
170
        from_label = gtk_label_new (_("From:"));
171
        gtk_widget_show (from_label);
172
        gtk_table_attach (GTK_TABLE (table1), from_label, 0, 1, 0, 1,
173
                          GTK_FILL, 0, 0, 0);
174
        gtk_label_set_justify (GTK_LABEL (from_label), GTK_JUSTIFY_RIGHT);
175
        gtk_misc_set_alignment (GTK_MISC (from_label), 1, 0.5);
176
177
        to_label = gtk_label_new (_("To:"));
178
        gtk_widget_show (to_label);
179
        gtk_table_attach (GTK_TABLE (table1), to_label, 0, 1, 1, 2,
180
                          GTK_FILL, 0, 0, 0);
181
        gtk_label_set_justify (GTK_LABEL (to_label), GTK_JUSTIFY_RIGHT);
182
        gtk_misc_set_alignment (GTK_MISC (to_label), 1, 0.5);
183
184
        subject_label = gtk_label_new (_("Subject:"));
185
        gtk_widget_show (subject_label);
186
        gtk_table_attach (GTK_TABLE (table1), subject_label, 0, 1, 2, 3,
187
                          GTK_FILL, 0, 0, 0);
188
        gtk_label_set_justify (GTK_LABEL (subject_label), GTK_JUSTIFY_RIGHT);
189
        gtk_misc_set_alignment (GTK_MISC (subject_label), 1, 0.5);
190
191
        body_label = gtk_label_new (_("Body:"));
192
        gtk_widget_show (body_label);
193
        gtk_table_attach (GTK_TABLE (table1), body_label, 0, 1, 3, 4,
194
                          GTK_FILL, 0, 0, 0);
195
        gtk_label_set_justify (GTK_LABEL (body_label), GTK_JUSTIFY_RIGHT);
196
        gtk_misc_set_alignment (GTK_MISC (body_label), 1, 0.5);
197
198
        checkbtn_hbox = gtk_hbox_new (FALSE, 8);
199
        gtk_widget_show (checkbtn_hbox);
200
        gtk_box_pack_start (GTK_BOX (vbox1), checkbtn_hbox, TRUE, TRUE, 0);
201
        gtk_container_set_border_width (GTK_CONTAINER (checkbtn_hbox), 8);
202
203
        case_checkbtn = gtk_check_button_new_with_label (_("Case sensitive"));
204
        gtk_widget_show (case_checkbtn);
205
        gtk_box_pack_start (GTK_BOX (checkbtn_hbox), case_checkbtn,
206
                            FALSE, FALSE, 0);
207
208
        backward_checkbtn =
209
                gtk_check_button_new_with_label (_("Backward search"));
210
        gtk_widget_show (backward_checkbtn);
211
        gtk_box_pack_start (GTK_BOX (checkbtn_hbox), backward_checkbtn,
212
                            FALSE, FALSE, 0);
213
214
        all_checkbtn =
215
                gtk_check_button_new_with_label (_("Select all matched"));
216
        gtk_widget_show (all_checkbtn);
217
        gtk_box_pack_start (GTK_BOX (checkbtn_hbox), all_checkbtn,
218
                            FALSE, FALSE, 0);
219
        g_signal_connect(G_OBJECT(all_checkbtn), "clicked",
220
                         G_CALLBACK(all_clicked), summaryview);
221
222
        gtkut_stock_button_set_create(&confirm_area,
223
                                      &search_btn, GTK_STOCK_FIND,
224
                                      &clear_btn, GTK_STOCK_CLEAR,
225
                                      &close_btn, GTK_STOCK_CLOSE);
226
        gtk_widget_show (confirm_area);
227
        gtk_box_pack_start (GTK_BOX (vbox1), confirm_area, FALSE, FALSE, 0);
228
        gtk_widget_grab_default(search_btn);
229
230
        g_signal_connect(G_OBJECT(search_btn), "clicked",
231
                         G_CALLBACK(summary_search_execute), summaryview);
232
        g_signal_connect(G_OBJECT(clear_btn), "clicked",
233
                         G_CALLBACK(summary_search_clear), summaryview);
234
        g_signal_connect_closure
235
                (G_OBJECT(close_btn), "clicked",
236
                 g_cclosure_new_swap(G_CALLBACK(gtk_widget_hide),
237
                                     window, NULL),
238
                 FALSE);
239
}
240
241
static void summary_search_execute(GtkButton *button, gpointer data)
242
{
243
#if 0
244
        SummaryView *summaryview = data;
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
                                         GTK_STOCK_YES, GTK_STOCK_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_row
407
                                                (summaryview, &iter, 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
#endif
431
}
432
433
static void summary_search_clear(GtkButton *button, gpointer data)
434
{
435
        gtk_editable_delete_text(GTK_EDITABLE(from_entry),    0, -1);
436
        gtk_editable_delete_text(GTK_EDITABLE(to_entry),      0, -1);
437
        gtk_editable_delete_text(GTK_EDITABLE(subject_entry), 0, -1);
438
        gtk_editable_delete_text(GTK_EDITABLE(body_entry),    0, -1);
439
}
440
441
static void from_activated(void)
442
{
443
        gtk_widget_grab_focus(to_entry);
444
}
445
446
static void to_activated(void)
447
{
448
        gtk_widget_grab_focus(subject_entry);
449
}
450
451
static void subject_activated(void)
452
{
453
        gtk_button_clicked(GTK_BUTTON(search_btn));
454
}
455
456
static void body_activated(void)
457
{
458
        gtk_button_clicked(GTK_BUTTON(search_btn));
459
}
460
461
static void all_clicked(GtkButton *button)
462
{
463
        if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)))
464
                gtk_widget_set_sensitive(backward_checkbtn, FALSE);
465
        else
466
                gtk_widget_set_sensitive(backward_checkbtn, TRUE);
467
}
468
469
static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
470
                            gpointer data)
471
{
472
        if (event && event->keyval == GDK_Escape)
473
                gtk_widget_hide(window);
474
        return FALSE;
475
}