aboutsummaryrefslogtreecommitdiffstats
path: root/mail/mail-search.c
blob: a9bb47150795d0d7ff5c1764025afa991ccfe9fa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  Authors: Jon Trowbridge <trow@ximian.com>
 *
 *  Copyright 2001-2003 Ximian, Inc. (www.ximian.com)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
 *
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>

#include "mail-search.h"
#include "e-searching-tokenizer.h"
#include <gtkhtml/gtkhtml-search.h>
#include <gtkhtml/htmlengine.h>
#include <libgnomeui/gnome-window-icon.h>


static ESearchingTokenizer *mail_search_tokenizer (MailSearch *ms);
static void mail_search_redisplay_message (MailSearch *ms);


static GtkObjectClass *parent_class = NULL;


static void
mail_search_finalise (GObject *obj)
{
    MailSearch *ms = MAIL_SEARCH (obj);

    g_free (ms->last_search);
    g_object_unref (ms->mail);
    
    G_OBJECT_CLASS (parent_class)->finalize (obj);
}

static void
mail_search_destroy (GtkObject *obj)
{
    MailSearch *ms = (MailSearch *) obj;
    ESearchingTokenizer *st = mail_search_tokenizer (ms);

    if (ms->begin_handler) {
        g_signal_handler_disconnect(ms->mail->html->engine->ht, ms->match_handler);
        g_signal_handler_disconnect(ms->mail->html->engine->ht, ms->begin_handler);
        ms->begin_handler = 0;
        e_searching_tokenizer_set_primary_search_string (st, NULL);
        mail_search_redisplay_message (ms);
    }
    
    GTK_OBJECT_CLASS (parent_class)->destroy (obj);
}

static void
mail_search_class_init (MailSearchClass *klass)
{
    GObjectClass *object_class = (GObjectClass *) klass;
    GtkObjectClass *gtk_object_class = (GtkObjectClass *) klass;
    
    parent_class = (GtkObjectClass *) g_type_class_ref (GTK_TYPE_DIALOG);
    
    object_class->finalize = mail_search_finalise;
    
    gtk_object_class->destroy = mail_search_destroy;
}

static void
mail_search_init (MailSearch *ms)
{
    
}

GtkType
mail_search_get_type (void)
{
    static GType type = 0;

    if (!type) {
        static const GTypeInfo info = {
            sizeof (MailSearchClass),
            NULL, NULL,
            (GClassInitFunc) mail_search_class_init,
            NULL, NULL,
            sizeof (MailSearch),
            0,
            (GInstanceInitFunc) mail_search_init,
        };
        
        type = g_type_register_static (GTK_TYPE_DIALOG, "MailSearch", &info, 0);
    }
    
    return type;
}

/*
 *  Convenience
 */

static ESearchingTokenizer *
mail_search_tokenizer (MailSearch *ms)
{
    return E_SEARCHING_TOKENIZER (ms->mail->html->engine->ht);
}

static void
mail_search_redisplay_message (MailSearch *ms)
{
    mail_display_redisplay (ms->mail, FALSE);
}

static void
mail_search_set_subject (MailSearch *ms, const char *subject)
{
    char *utf8_subject = NULL;
    
    if (subject && *subject) {
        utf8_subject = g_strdup (subject);
        
        if (g_utf8_validate (utf8_subject, -1, NULL)) {
#define ARBITRARY_CUTOFF 40
            if (g_utf8_strlen (utf8_subject, -1) > ARBITRARY_CUTOFF + 3) {
                char *p = g_utf8_offset_to_pointer (utf8_subject, ARBITRARY_CUTOFF);
                
                strcpy (p, "...");
            }
        } else {
            /* If the subject contains bad utf8, don't show anything in the frame label. */
            g_free (utf8_subject);
            utf8_subject = NULL;
        }
    } else {
        utf8_subject = g_strdup (_("(Untitled Message)"));
    }
    
    gtk_frame_set_label (GTK_FRAME (ms->msg_frame), utf8_subject);
    
    g_free (utf8_subject);
}

/*
 *  Construct Objects
 */

static void
toggled_case_cb (GtkToggleButton *b, MailSearch *ms)
{
    ms->case_sensitive = gtk_toggle_button_get_active (b);
    
    e_searching_tokenizer_set_primary_case_sensitivity (mail_search_tokenizer (ms),
                                ms->case_sensitive);
    mail_search_redisplay_message (ms);
    
}

#if 0
static void
toggled_fwd_cb (GtkToggleButton *b, MailSearch *ms)
{
    ms->search_forward = gtk_toggle_button_get_active (b);
    gtk_html_engine_search_set_forward (ms->mail->html, ms->search_forward);
}
#endif

static void
dialog_response_cb (GtkWidget *widget, int button, MailSearch *ms)
{
    ESearchingTokenizer *st = mail_search_tokenizer (ms);
    
    if (button == GTK_RESPONSE_ACCEPT) {
        char *search_text;
        
        search_text = gtk_editable_get_chars (GTK_EDITABLE (ms->entry), 0, -1);
        g_strstrip (search_text);
        
        if (search_text && *search_text) {
            if (ms->last_search && !strcmp (ms->last_search, search_text)) {
                
                if (!gtk_html_engine_search_next (ms->mail->html)) {
                    g_free (ms->last_search);
                    ms->last_search = NULL;
                }
            } else {
                g_free (ms->last_search);
                ms->last_search = NULL;
                
                e_searching_tokenizer_set_primary_search_string (st, search_text);
                e_searching_tokenizer_set_primary_case_sensitivity (st, ms->case_sensitive);
                
                mail_search_redisplay_message (ms);
                
                if (gtk_html_engine_search (ms->mail->html, search_text,
                                ms->case_sensitive, ms->search_forward,
                                FALSE)) {
                    ms->last_search = g_strdup (search_text);
                }
            }
        }
        
        g_free (search_text);
    } else if (button == GTK_RESPONSE_CANCEL) {
        gtk_widget_destroy (widget);
    }
}

static void
begin_cb (ESearchingTokenizer *st, char *foo, MailSearch *ms)
{
    const char *subject;
    
    if (ms && ms->mail && ms->mail->current_message) {
        subject = ms->mail->current_message->subject;
        
        if (subject == NULL)
            subject = _("Untitled Message");
    } else {
        subject = _("Empty Message");
    }
    
    gtk_label_set_text (GTK_LABEL (ms->count_label), "0");
    mail_search_set_subject (ms, subject);
}

static void
match_cb (ESearchingTokenizer *st, MailSearch *ms)
{
    char buf[16];
    
    g_snprintf (buf, 16, "%d", e_searching_tokenizer_match_count (st));
    gtk_label_set_text (GTK_LABEL (ms->count_label), buf);
}

static void
entry_run_search (GtkWidget *w, MailSearch *ms)
{
    /* run search when enter pressed on widget */
    gtk_dialog_response ((GtkDialog *) ms, GTK_RESPONSE_ACCEPT);
}

void
mail_search_construct (MailSearch *ms, MailDisplay *mail)
{
    GtkWidget *find_hbox;
    GtkWidget *matches_hbox;
    GtkWidget *toggles_hbox;
    GtkWidget *frame_vbox;
    GtkWidget *entry;
    GtkWidget *count_label;
    GtkWidget *case_check;
#if 0
    GtkWidget *fwd_check;
#endif
    GtkWidget *button;
    GtkWidget *msg_hbox;
    GtkWidget *msg_frame;
    
    g_return_if_fail (ms != NULL && IS_MAIL_SEARCH (ms));
    g_return_if_fail (mail != NULL && IS_MAIL_DISPLAY (mail));
    
    /* Basic set-up */
    
    ms->mail = mail;
    g_object_ref (mail);
    
    gtk_window_set_title ((GtkWindow *) ms, _("Find in Message"));
    
    gtk_dialog_add_button ((GtkDialog *) ms, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
    
    button = gtk_button_new_from_stock (GTK_STOCK_FIND);
    gtk_button_set_label ((GtkButton *) button, _("Search"));
    gtk_dialog_add_action_widget ((GtkDialog*) ms, button, GTK_RESPONSE_ACCEPT);
    gtk_dialog_set_default_response ((GtkDialog *) ms, GTK_RESPONSE_ACCEPT);
    
    ms->search_forward = TRUE;
    ms->case_sensitive = FALSE;
    
    ms->begin_handler = g_signal_connect (ms->mail->html->engine->ht, "begin",
                          G_CALLBACK (begin_cb), ms);
    ms->match_handler = g_signal_connect (ms->mail->html->engine->ht, "match",
                          G_CALLBACK (match_cb), ms);
    
    /* Construct the dialog contents. */
    
    msg_hbox     = gtk_hbox_new (FALSE, 3);
    find_hbox    = gtk_hbox_new (FALSE, 3);
    matches_hbox = gtk_hbox_new (FALSE, 3);
    toggles_hbox = gtk_hbox_new (FALSE, 3);
    frame_vbox   = gtk_vbox_new (FALSE, 3);
    gtk_container_set_border_width ((GtkContainer *) frame_vbox, 3);
    
    entry       = gtk_entry_new ();
    count_label = gtk_label_new ("0");
    
    msg_frame   = gtk_frame_new (NULL);
    gtk_container_set_border_width ((GtkContainer *) msg_frame, 6);
    
    case_check  = gtk_check_button_new_with_label (_("Case Sensitive"));
#if 0
    fwd_check   = gtk_check_button_new_with_label (_("Search Forward"));
#endif
    
    ms->entry       = entry;
    ms->count_label = count_label;
    
    ms->msg_frame   = msg_frame;
    
    if (mail->current_message->subject && *mail->current_message->subject)
        mail_search_set_subject (ms, mail->current_message->subject);
    else
        mail_search_set_subject (ms, NULL);
    
#if 0
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fwd_check),  ms->search_forward);
#endif
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (case_check), ms->case_sensitive);
    
    gtk_box_pack_start (GTK_BOX (msg_hbox), GTK_WIDGET (msg_frame), TRUE, TRUE, 0);
    
    gtk_box_pack_start (GTK_BOX (find_hbox), gtk_label_new (_("Find:")), FALSE, FALSE, 3);
    gtk_box_pack_start (GTK_BOX (find_hbox), entry, TRUE, TRUE, 3);
    
    gtk_box_pack_start (GTK_BOX (matches_hbox), gtk_hbox_new (FALSE, 0), TRUE, TRUE, 0);
    gtk_box_pack_start (GTK_BOX (matches_hbox), gtk_label_new (_("Matches:")), FALSE, FALSE, 3);
    gtk_box_pack_start (GTK_BOX (matches_hbox), count_label, FALSE, FALSE, 3);
    gtk_box_pack_start (GTK_BOX (matches_hbox), gtk_hbox_new (FALSE, 0), TRUE, TRUE, 0);
    
    gtk_box_pack_start (GTK_BOX (toggles_hbox), case_check, FALSE, FALSE, 3);
    
    /*
     * Disabling the forward/backward search button because there are problems with it
     * (related to how gtkhtml handles searches), the GUI freeze is upon us, and I
     * don't know if they'll get resolved for 1.0.  Hopefully getting this fixed can
     * be a 1.1 item.
     */
    
#if 0
    gtk_box_pack_start (GTK_BOX (toggles_hbox), fwd_check,  FALSE, FALSE, 3);
#endif
    
    gtk_box_pack_start (GTK_BOX (frame_vbox), find_hbox, FALSE, FALSE, 3);
    gtk_box_pack_start (GTK_BOX (frame_vbox), matches_hbox, FALSE, FALSE, 3); 
    gtk_box_pack_start (GTK_BOX (frame_vbox), toggles_hbox, FALSE, FALSE, 3);
    
    gtk_container_add (GTK_CONTAINER (msg_frame), GTK_WIDGET (frame_vbox));
    
    gtk_box_pack_start (GTK_BOX (GTK_DIALOG (ms)->vbox), msg_hbox, TRUE, TRUE, 0);  
    
    gtk_widget_grab_focus (entry); /* Give focus to entry by default */ 
    g_signal_connect (entry, "activate", G_CALLBACK (entry_run_search), ms);
    gnome_window_icon_set_from_file (GTK_WINDOW (ms), EVOLUTION_ICONSDIR "/find-message.xpm");
    
    gtk_widget_show_all (msg_hbox);
    gtk_widget_show_all (find_hbox);
    gtk_widget_show_all (matches_hbox);
    gtk_widget_show_all (toggles_hbox);
    
    /* Hook up signals */
    
    g_signal_connect (case_check, "toggled", G_CALLBACK (toggled_case_cb), ms);
#if 0
    g_signal_connect (fwd_check, "toggled", G_CALLBACK (toggled_fwd_cb), ms);
#endif
    g_signal_connect (ms, "response", G_CALLBACK (dialog_response_cb), ms);
    
    g_object_weak_ref ((GObject *) ms->mail, (GWeakNotify) gtk_widget_destroy, ms);
}

GtkWidget *
mail_search_new (MailDisplay *mail)
{
    GtkWidget *widget;
    
    g_return_val_if_fail (IS_MAIL_DISPLAY (mail), NULL);
    
    widget = g_object_new (mail_search_get_type (), NULL);
    mail_search_construct (MAIL_SEARCH (widget), mail);
    
    return widget;
}