Statistics
| Revision:

root / src / textview.c @ 3253

History | View | Annotate | Download (73.7 KB)

1
/*
2
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3
 * Copyright (C) 1999-2013 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/gdk.h>
29
#include <gdk/gdkkeysyms.h>
30
#include <gdk-pixbuf/gdk-pixbuf.h>
31
#include <gtk/gtkvbox.h>
32
#include <gtk/gtkscrolledwindow.h>
33
#include <gtk/gtksignal.h>
34
#include <gtk/gtkmenu.h>
35
#include <gtk/gtkmenuitem.h>
36
#include <gtk/gtkseparatormenuitem.h>
37
#include <gtk/gtkstock.h>
38
#include <stdio.h>
39
#include <ctype.h>
40
#include <string.h>
41
#include <stdlib.h>
42

    
43
#include "main.h"
44
#include "summaryview.h"
45
#include "imageview.h"
46
#include "procheader.h"
47
#include "prefs_common.h"
48
#include "codeconv.h"
49
#include "statusbar.h"
50
#include "utils.h"
51
#include "gtkutils.h"
52
#include "procmime.h"
53
#include "account.h"
54
#include "html.h"
55
#include "compose.h"
56
#include "displayheader.h"
57
#include "filesel.h"
58
#include "alertpanel.h"
59
#include "menu.h"
60
#include "plugin.h"
61

    
62
typedef struct _RemoteURI        RemoteURI;
63

    
64
struct _RemoteURI
65
{
66
        gchar *uri;
67

    
68
        gchar *filename;
69

    
70
        guint start;
71
        guint end;
72
};
73

    
74
static GdkColor quote_colors[3] = {
75
        {(gulong)0, (gushort)0, (gushort)0, (gushort)0},
76
        {(gulong)0, (gushort)0, (gushort)0, (gushort)0},
77
        {(gulong)0, (gushort)0, (gushort)0, (gushort)0}
78
};
79

    
80
static GdkColor uri_color = {
81
        (gulong)0,
82
        (gushort)0,
83
        (gushort)0,
84
        (gushort)0
85
};
86

    
87
static GdkColor emphasis_color = {
88
        (gulong)0,
89
        (gushort)0,
90
        (gushort)0,
91
        (gushort)0xcfff
92
};
93

    
94
static GdkColor error_color = {
95
        (gulong)0,
96
        (gushort)0xefff,
97
        (gushort)0,
98
        (gushort)0
99
};
100

    
101
#if USE_GPGME
102
static GdkColor good_sig_color = {
103
        (gulong)0,
104
        (gushort)0,
105
        (gushort)0xbfff,
106
        (gushort)0
107
};
108

    
109
static GdkColor untrusted_sig_color = {
110
        (gulong)0,
111
        (gushort)0xefff,
112
        (gushort)0,
113
        (gushort)0
114
};
115

    
116
static GdkColor nocheck_sig_color = {
117
        (gulong)0,
118
        (gushort)0,
119
        (gushort)0,
120
        (gushort)0xcfff
121
};
122

    
123
static GdkColor bad_sig_color = {
124
        (gulong)0,
125
        (gushort)0xefff,
126
        (gushort)0,
127
        (gushort)0
128
};
129
#endif
130

    
131
#define STATUSBAR_PUSH(textview, str)                                            \
132
{                                                                            \
133
        gtk_statusbar_push(GTK_STATUSBAR(textview->messageview->statusbar), \
134
                           textview->messageview->statusbar_cid, str);            \
135
}
136

    
137
#define STATUSBAR_POP(textview)                                                   \
138
{                                                                           \
139
        gtk_statusbar_pop(GTK_STATUSBAR(textview->messageview->statusbar), \
140
                          textview->messageview->statusbar_cid);           \
141
}
142

    
143
static GdkCursor *hand_cursor = NULL;
144
static GdkCursor *regular_cursor = NULL;
145

    
146

    
147
static void textview_part_menu_create        (TextView        *textview);
148

    
149
static void textview_add_part                (TextView        *textview,
150
                                         MimeInfo        *mimeinfo,
151
                                         FILE                *fp);
152
#if USE_GPGME
153
static void textview_add_sig_part        (TextView        *textview,
154
                                         MimeInfo        *mimeinfo);
155
#endif
156
static void textview_add_parts                (TextView        *textview,
157
                                         MimeInfo        *mimeinfo,
158
                                         FILE                *fp);
159
static void textview_write_body                (TextView        *textview,
160
                                         MimeInfo        *mimeinfo,
161
                                         FILE                *fp,
162
                                         const gchar        *charset);
163
static void textview_show_html                (TextView        *textview,
164
                                         FILE                *fp,
165
                                         CodeConverter        *conv);
166

    
167
static void textview_write_line                (TextView        *textview,
168
                                         const gchar        *str,
169
                                         CodeConverter        *conv);
170
static void textview_write_link                (TextView        *textview,
171
                                         const gchar        *str,
172
                                         const gchar        *uri,
173
                                         CodeConverter        *conv);
174

    
175
static GPtrArray *textview_scan_header        (TextView        *textview,
176
                                         FILE                *fp,
177
                                         const gchar        *encoding);
178
static void textview_show_header        (TextView        *textview,
179
                                         GPtrArray        *headers);
180

    
181
static void textview_insert_border        (TextView        *textview,
182
                                         GtkTextIter        *iter,
183
                                         gint                 padding);
184
static void textview_insert_pad                (TextView        *textview,
185
                                         GtkTextIter        *iter,
186
                                         gint                 padding);
187

    
188
static gboolean textview_key_pressed                (GtkWidget        *widget,
189
                                                 GdkEventKey        *event,
190
                                                 TextView        *textview);
191
static gboolean textview_event_after                (GtkWidget        *widget,
192
                                                 GdkEvent        *event,
193
                                                 TextView        *textview);
194
static gboolean textview_motion_notify                (GtkWidget        *widget,
195
                                                 GdkEventMotion        *event,
196
                                                 TextView        *textview);
197
static gboolean textview_leave_notify                (GtkWidget          *widget,
198
                                                 GdkEventCrossing *event,
199
                                                 TextView          *textview);
200
static gboolean textview_visibility_notify        (GtkWidget        *widget,
201
                                                 GdkEventVisibility *event,
202
                                                 TextView        *textview);
203

    
204
static void textview_populate_popup                (GtkWidget        *widget,
205
                                                 GtkMenu        *menu,
206
                                                 TextView        *textview);
207
static void textview_popup_menu_activate_open_uri_cb
208
                                                (GtkMenuItem        *menuitem,
209
                                                 gpointer         data);
210
static void textview_popup_menu_activate_reply_cb
211
                                                (GtkMenuItem        *menuitem,
212
                                                 gpointer         data);
213
static void textview_popup_menu_activate_add_address_cb
214
                                                (GtkMenuItem        *menuitem,
215
                                                 gpointer         data);
216
static void textview_popup_menu_activate_copy_cb(GtkMenuItem        *menuitem,
217
                                                 gpointer         data);
218
static void textview_popup_menu_activate_image_cb
219
                                                (GtkMenuItem        *menuitem,
220
                                                 gpointer         data);
221

    
222
static void textview_adj_value_changed                (GtkAdjustment        *adj,
223
                                                 gpointer         data);
224

    
225
static void textview_smooth_scroll_do                (TextView        *textview,
226
                                                 gfloat                 old_value,
227
                                                 gfloat                 last_value,
228
                                                 gint                 step);
229
static void textview_smooth_scroll_one_line        (TextView        *textview,
230
                                                 gboolean         up);
231
static gboolean textview_smooth_scroll_page        (TextView        *textview,
232
                                                 gboolean         up);
233

    
234
static gboolean textview_get_link_tag_bounds        (TextView        *textview,
235
                                                 GtkTextIter        *iter,
236
                                                 GtkTextIter        *start,
237
                                                 GtkTextIter        *end);
238
static RemoteURI *textview_get_uri                (TextView        *textview,
239
                                                 GtkTextIter        *start,
240
                                                 GtkTextIter        *end);
241
static void textview_show_uri                        (TextView        *textview,
242
                                                 GtkTextIter        *start,
243
                                                 GtkTextIter        *end);
244
static void textview_set_cursor                        (TextView        *textview,
245
                                                 GtkTextView        *text,
246
                                                 gint                 x,
247
                                                 gint                 y);
248

    
249
static gboolean textview_uri_security_check        (TextView        *textview,
250
                                                 RemoteURI        *uri);
251
static void textview_uri_list_remove_all        (GSList                *uri_list);
252
static void textview_uri_list_update_offsets        (TextView        *textview,
253
                                                 gint                 start,
254
                                                 gint                 add);
255

    
256

    
257
TextView *textview_create(void)
258
{
259
        TextView *textview;
260
        GtkWidget *vbox;
261
        GtkWidget *scrolledwin;
262
        GtkWidget *text;
263
        GtkTextBuffer *buffer;
264
        GtkClipboard *clipboard;
265

    
266
        debug_print(_("Creating text view...\n"));
267
        textview = g_new0(TextView, 1);
268

    
269
        scrolledwin = gtk_scrolled_window_new(NULL, NULL);
270
        gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
271
                                       GTK_POLICY_AUTOMATIC,
272
                                       GTK_POLICY_AUTOMATIC);
273
        gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
274
                                            GTK_SHADOW_ETCHED_IN);
275
        gtk_widget_set_size_request
276
                (scrolledwin, prefs_common.mainview_width, -1);
277

    
278
        text = gtk_text_view_new();
279
        gtk_widget_add_events(text, GDK_LEAVE_NOTIFY_MASK);
280
        gtk_widget_show(text);
281
        gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
282
        gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
283
        gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text), 6);
284
        gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text), 6);
285

    
286
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
287
        clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
288
        gtk_text_buffer_add_selection_clipboard(buffer, clipboard);
289

    
290
        gtk_widget_ref(scrolledwin);
291

    
292
        gtk_container_add(GTK_CONTAINER(scrolledwin), text);
293

    
294
        g_signal_connect(G_OBJECT(text), "key-press-event",
295
                         G_CALLBACK(textview_key_pressed), textview);
296
        g_signal_connect(G_OBJECT(text), "event-after",
297
                         G_CALLBACK(textview_event_after), textview);
298
        g_signal_connect(G_OBJECT(text), "motion-notify-event",
299
                         G_CALLBACK(textview_motion_notify), textview);
300
        g_signal_connect(G_OBJECT(text), "leave-notify-event",
301
                         G_CALLBACK(textview_leave_notify), textview);
302
        g_signal_connect(G_OBJECT(text), "visibility-notify-event",
303
                         G_CALLBACK(textview_visibility_notify), textview);
304
        g_signal_connect(G_OBJECT(text), "populate-popup",
305
                         G_CALLBACK(textview_populate_popup), textview);
306

    
307
        g_signal_connect(G_OBJECT(GTK_TEXT_VIEW(text)->vadjustment),
308
                         "value-changed",
309
                         G_CALLBACK(textview_adj_value_changed), textview);
310

    
311
        gtk_widget_show(scrolledwin);
312

    
313
        vbox = gtk_vbox_new(FALSE, 0);
314
        gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
315

    
316
        gtk_widget_show(vbox);
317

    
318
        textview->vbox             = vbox;
319
        textview->scrolledwin      = scrolledwin;
320
        textview->text             = text;
321
        textview->uri_list         = NULL;
322
        textview->body_pos         = 0;
323
        textview->show_all_headers = FALSE;
324

    
325
        textview_part_menu_create(textview);
326

    
327
        return textview;
328
}
329

    
330
static void textview_create_tags(TextView *textview)
331
{
332
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
333
        GtkTextBuffer *buffer;
334
        static PangoFontDescription *font_desc;
335
        GtkTextIter iter;
336
        GtkTextMark *mark;
337

    
338
        if (!font_desc)
339
                font_desc = gtkut_get_default_font_desc();
340

    
341
        buffer = gtk_text_view_get_buffer(text);
342

    
343
        gtk_text_buffer_get_end_iter(buffer, &iter);
344
        mark = gtk_text_buffer_create_mark(buffer, "attach-file-pos",
345
                                           &iter, TRUE);
346

    
347
        gtk_text_buffer_create_tag(buffer, "header",
348
                                   "pixels-above-lines", 1,
349
                                   "pixels-above-lines-set", TRUE,
350
                                   "pixels-below-lines", 0,
351
                                   "pixels-below-lines-set", TRUE,
352
                                   "font-desc", font_desc,
353
                                   NULL);
354
        gtk_text_buffer_create_tag(buffer, "header_title",
355
                                   "weight", PANGO_WEIGHT_BOLD,
356
                                   NULL);
357

    
358
        gtk_text_buffer_create_tag(buffer, "mimepart",
359
                                   "pixels-above-lines", 1,
360
                                   "pixels-above-lines-set", TRUE,
361
                                   "pixels-below-lines", 1,
362
                                   "pixels-below-lines-set", TRUE,
363
                                   "font-desc", font_desc,
364
                                   NULL);
365

    
366
        textview->quote0_tag =
367
                gtk_text_buffer_create_tag(buffer, "quote0",
368
                                           "foreground-gdk", &quote_colors[0],
369
                                           NULL);
370
        textview->quote1_tag =
371
                gtk_text_buffer_create_tag(buffer, "quote1",
372
                                           "foreground-gdk", &quote_colors[1],
373
                                           NULL);
374
        textview->quote2_tag =
375
                gtk_text_buffer_create_tag(buffer, "quote2",
376
                                           "foreground-gdk", &quote_colors[2],
377
                                           NULL);
378
        textview->link_tag =
379
                gtk_text_buffer_create_tag(buffer, "link",
380
                                           "foreground-gdk", &uri_color,
381
                                           NULL);
382
        textview->hover_link_tag =
383
                gtk_text_buffer_create_tag(buffer, "hover-link",
384
                                           "foreground-gdk", &uri_color,
385
                                           "underline", PANGO_UNDERLINE_SINGLE,
386
                                           NULL);
387

    
388
        gtk_text_buffer_create_tag(buffer, "emphasis",
389
                                   "foreground-gdk", &emphasis_color,
390
                                   NULL);
391
        gtk_text_buffer_create_tag(buffer, "error",
392
                                   "foreground-gdk", &error_color,
393
                                   NULL);
394
#if USE_GPGME
395
        gtk_text_buffer_create_tag(buffer, "good-signature",
396
                                   "foreground-gdk", &good_sig_color,
397
                                   NULL);
398
        gtk_text_buffer_create_tag(buffer, "untrusted-signature",
399
                                   "foreground-gdk", &untrusted_sig_color,
400
                                   NULL);
401
        gtk_text_buffer_create_tag(buffer, "bad-signature",
402
                                   "foreground-gdk", &bad_sig_color,
403
                                   NULL);
404
        gtk_text_buffer_create_tag(buffer, "nocheck-signature",
405
                                   "foreground-gdk", &nocheck_sig_color,
406
                                   NULL);
407
#endif /* USE_GPGME */
408
}
409

    
410
void textview_init(TextView *textview)
411
{
412
        if (!hand_cursor)
413
                hand_cursor = gdk_cursor_new(GDK_HAND2);
414
        if (!regular_cursor)
415
                regular_cursor = gdk_cursor_new(GDK_XTERM);
416

    
417
        textview_create_tags(textview);
418
        textview_reflect_prefs(textview);
419
        textview_set_all_headers(textview, FALSE);
420
        textview_set_font(textview, NULL);
421
}
422

    
423
static void textview_update_message_colors(void)
424
{
425
        GdkColor black = {0, 0, 0, 0};
426

    
427
        if (prefs_common.enable_color) {
428
                /* grab the quote colors, converting from an int to a
429
                   GdkColor */
430
                gtkut_convert_int_to_gdk_color(prefs_common.quote_level1_col,
431
                                               &quote_colors[0]);
432
                gtkut_convert_int_to_gdk_color(prefs_common.quote_level2_col,
433
                                               &quote_colors[1]);
434
                gtkut_convert_int_to_gdk_color(prefs_common.quote_level3_col,
435
                                               &quote_colors[2]);
436
                gtkut_convert_int_to_gdk_color(prefs_common.uri_col,
437
                                               &uri_color);
438
        } else {
439
                quote_colors[0] = quote_colors[1] = quote_colors[2] = 
440
                        uri_color = emphasis_color = black;
441
        }
442
}
443

    
444
static void textview_update_tags(TextView *textview)
445
{
446
        g_object_set(textview->quote0_tag, "foreground-gdk", &quote_colors[0],
447
                     NULL);
448
        g_object_set(textview->quote1_tag, "foreground-gdk", &quote_colors[1],
449
                     NULL);
450
        g_object_set(textview->quote2_tag, "foreground-gdk", &quote_colors[2],
451
                     NULL);
452
        g_object_set(textview->link_tag, "foreground-gdk", &uri_color, NULL);
453
        g_object_set(textview->hover_link_tag, "foreground-gdk", &uri_color,
454
                     NULL);
455
}
456

    
457
void textview_reflect_prefs(TextView *textview)
458
{
459
        textview_update_message_colors();
460
        textview_update_tags(textview);
461
        gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(textview->text),
462
                                         prefs_common.textview_cursor_visible);
463
}
464

    
465
static const gchar *textview_get_src_encoding(TextView *textview,
466
                                              MimeInfo *mimeinfo)
467
{
468
        const gchar *charset;
469

    
470
        if (textview->messageview->forced_charset)
471
                charset = textview->messageview->forced_charset;
472
        else if (!textview->messageview->new_window &&
473
                 prefs_common.force_charset)
474
                charset = prefs_common.force_charset;
475
        else if (mimeinfo->charset)
476
                charset = mimeinfo->charset;
477
        else if (prefs_common.default_encoding)
478
                charset = prefs_common.default_encoding;
479
        else
480
                charset = NULL;
481

    
482
        return charset;
483
}
484

    
485
void textview_show_message(TextView *textview, MimeInfo *mimeinfo,
486
                           const gchar *file)
487
{
488
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
489
        GtkTextBuffer *buffer;
490
        GtkTextMark *mark;
491
        GtkTextIter iter;
492
        FILE *fp;
493
        const gchar *charset;
494
        GPtrArray *headers = NULL;
495

    
496
        buffer = gtk_text_view_get_buffer(text);
497

    
498
        if ((fp = g_fopen(file, "rb")) == NULL) {
499
                FILE_OP_ERROR(file, "fopen");
500
                return;
501
        }
502

    
503
        charset = textview_get_src_encoding(textview, mimeinfo);
504
        textview_set_font(textview, charset);
505
        textview_clear(textview);
506

    
507
        if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0) perror("fseek");
508
        headers = textview_scan_header(textview, fp, charset);
509
        if (headers) {
510
                textview_show_header(textview, headers);
511
                procheader_header_array_destroy(headers);
512

    
513
                gtk_text_buffer_get_end_iter(buffer, &iter);
514
                textview->body_pos = gtk_text_iter_get_offset(&iter);
515
                debug_print("textview_show_message: body_pos: %d\n",
516
                            textview->body_pos);
517
        } else {
518
                gtk_text_buffer_get_end_iter(buffer, &iter);
519
                mark = gtk_text_buffer_get_mark(buffer, "attach-file-pos");
520
                gtk_text_buffer_move_mark(buffer, mark, &iter);
521
                g_object_set_data(G_OBJECT(mark), "attach-file-count", GINT_TO_POINTER(0));
522
        }
523

    
524
#if USE_GPGME
525
        if (textview->messageview->msginfo->encinfo &&
526
            textview->messageview->msginfo->encinfo->decryption_failed) {
527
                gtk_text_buffer_get_end_iter(buffer, &iter);
528
                gtk_text_buffer_insert(buffer, &iter, "\n", 1);
529
                gtk_text_buffer_insert_with_tags_by_name
530
                        (buffer, &iter, _("This message is encrypted, but its decryption failed.\n"),
531
                         -1, "error", "mimepart", NULL);
532
        }
533
#endif
534

    
535
        textview_add_parts(textview, mimeinfo, fp);
536

    
537
#if USE_GPGME
538
        if (textview->messageview->msginfo->encinfo &&
539
            textview->messageview->msginfo->encinfo->sigstatus)
540
                textview_add_sig_part(textview, NULL);
541
#endif
542

    
543
        fclose(fp);
544

    
545
        textview_set_position(textview, 0);
546
        mark = gtk_text_buffer_get_insert(buffer);
547
        gtk_text_view_scroll_mark_onscreen(text, mark);
548
}
549

    
550
void textview_show_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
551
{
552
        gchar buf[BUFFSIZE];
553
        const gchar *boundary = NULL;
554
        gint boundary_len = 0;
555
        const gchar *charset;
556
        GPtrArray *headers = NULL;
557
        gboolean is_rfc822_part = FALSE;
558
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
559
        GtkTextBuffer *buffer;
560
        GtkTextIter iter;
561
        GtkTextMark *mark;
562

    
563
        g_return_if_fail(mimeinfo != NULL);
564
        g_return_if_fail(fp != NULL);
565

    
566
        if (mimeinfo->mime_type == MIME_MULTIPART) {
567
                textview_clear(textview);
568
                textview_add_parts(textview, mimeinfo, fp);
569
                return;
570
        }
571

    
572
        if (mimeinfo->parent && mimeinfo->parent->boundary) {
573
                boundary = mimeinfo->parent->boundary;
574
                boundary_len = strlen(boundary);
575
        }
576

    
577
        charset = textview_get_src_encoding(textview, mimeinfo);
578

    
579
        if (!boundary && mimeinfo->mime_type == MIME_TEXT) {
580
                if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
581
                        perror("fseek");
582
                headers = textview_scan_header(textview, fp, charset);
583
        } else {
584
                if (mimeinfo->mime_type == MIME_TEXT && mimeinfo->parent) {
585
                        glong fpos;
586
                        MimeInfo *parent = mimeinfo->parent;
587

    
588
                        while (parent->parent) {
589
                                if (parent->main &&
590
                                    parent->main->mime_type ==
591
                                        MIME_MESSAGE_RFC822)
592
                                        break;
593
                                parent = parent->parent;
594
                        }
595

    
596
                        if ((fpos = ftell(fp)) < 0)
597
                                perror("ftell");
598
                        else if (fseek(fp, parent->fpos, SEEK_SET) < 0)
599
                                perror("fseek");
600
                        else {
601
                                headers = textview_scan_header
602
                                        (textview, fp, charset);
603
                                if (fseek(fp, fpos, SEEK_SET) < 0)
604
                                        perror("fseek");
605
                        }
606
                }
607
                /* skip MIME part headers */
608
                while (fgets(buf, sizeof(buf), fp) != NULL)
609
                        if (buf[0] == '\r' || buf[0] == '\n') break;
610
        }
611

    
612
        /* display attached RFC822 single text message */
613
        if (mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
614
                if (headers) procheader_header_array_destroy(headers);
615
                if (!mimeinfo->sub) {
616
                        textview_clear(textview);
617
                        return;
618
                }
619
                headers = textview_scan_header(textview, fp, charset);
620
                mimeinfo = mimeinfo->sub;
621
                is_rfc822_part = TRUE;
622
        }
623

    
624
        textview_set_font(textview, charset);
625
        textview_clear(textview);
626

    
627
        buffer = gtk_text_view_get_buffer(text);
628

    
629
        if (headers) {
630
                textview_show_header(textview, headers);
631
                procheader_header_array_destroy(headers);
632

    
633
                gtk_text_buffer_get_end_iter(buffer, &iter);
634
                textview->body_pos = gtk_text_iter_get_offset(&iter);
635
                debug_print("textview_show_part: body_pos: %d\n",
636
                            textview->body_pos);
637
                if (!mimeinfo->main)
638
                        gtk_text_buffer_insert(buffer, &iter, "\n", 1);
639
        } else {
640
                gtk_text_buffer_get_end_iter(buffer, &iter);
641
                mark = gtk_text_buffer_get_mark(buffer, "attach-file-pos");
642
                gtk_text_buffer_move_mark(buffer, mark, &iter);
643
                g_object_set_data(G_OBJECT(mark), "attach-file-count", GINT_TO_POINTER(0));
644
        }
645

    
646
        if (mimeinfo->mime_type == MIME_MULTIPART || is_rfc822_part)
647
                textview_add_parts(textview, mimeinfo, fp);
648
        else
649
                textview_write_body(textview, mimeinfo, fp, charset);
650

    
651
        textview_set_position(textview, 0);
652
        mark = gtk_text_buffer_get_insert(buffer);
653
        gtk_text_view_scroll_mark_onscreen(text, mark);
654
}
655

    
656
enum {
657
        PART_MENU_NONE,
658
        PART_MENU_OPEN,
659
        PART_MENU_OPEN_WITH,
660
        PART_MENU_SAVE_AS,
661
        PART_MENU_PRINT,
662
        PART_MENU_COPY_FILENAME
663
};
664

    
665
static void part_menu_button_position(GtkMenu *menu, gint *x, gint *y,
666
                                      gboolean *push_in,
667
                                      gpointer user_data)
668
{
669
        GtkWidget *widget;
670
        GtkRequisition requisition;
671
        gint button_xpos, button_ypos;
672
        gint xpos, ypos;
673
        gint width, height;
674
        gint scr_width, scr_height;
675

    
676
        g_return_if_fail(x != NULL && y != NULL);
677

    
678
        widget = GTK_WIDGET(user_data);
679
        gtk_widget_get_child_requisition(GTK_WIDGET(menu), &requisition);
680
        width = requisition.width;
681
        height = requisition.height;
682
        gdk_window_get_origin(widget->window, &button_xpos, &button_ypos);
683

    
684
        xpos = button_xpos;
685
        ypos = button_ypos + widget->requisition.height;
686

    
687
        scr_width = gdk_screen_width();
688
        scr_height = gdk_screen_height();
689

    
690
        if (xpos + width > scr_width)
691
                xpos -= (xpos + width) - scr_width;
692
        if (ypos + height > scr_height)
693
                ypos -= widget->requisition.height + height;
694
        if (xpos < 0)
695
                xpos = 0;
696
        if (ypos < 0)
697
                ypos = 0;
698

    
699
        *x = xpos;
700
        *y = ypos;
701
}
702

    
703
static gboolean textview_part_widget_entered(GtkWidget *widget,
704
                                             GdkEventCrossing *event,
705
                                             gpointer data)
706
{
707
        gtk_widget_set_state(widget, GTK_STATE_PRELIGHT);
708
        return FALSE;
709
}
710

    
711
static gboolean textview_part_widget_left(GtkWidget *widget,
712
                                          GdkEventCrossing *event,
713
                                          gpointer data)
714
{
715
        gtk_widget_set_state(widget, GTK_STATE_NORMAL);
716
        return FALSE;
717
}
718

    
719
static gboolean textview_part_widget_button_pressed(GtkWidget *widget,
720
                                                    GdkEventButton *event,
721
                                                    gpointer data)
722
{
723
        TextView *textview = (TextView *)data;
724
        GtkWidget *menu;
725
        MimeInfo *mimeinfo;
726
        GList *cur;
727

    
728
        if (!event)
729
                return FALSE;
730

    
731
        menu = textview->popup_menu;
732
        mimeinfo = g_object_get_data(G_OBJECT(widget), "mimeinfo");
733

    
734
        for (cur = GTK_MENU_SHELL(menu)->children; cur != NULL; cur = cur->next) {
735
                GtkWidget *menuitem = GTK_WIDGET(cur->data);
736
                gint type = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID));
737
                if (type == PART_MENU_PRINT) {
738
                        if (mimeinfo->mime_type == MIME_TEXT ||
739
                            mimeinfo->mime_type == MIME_TEXT_HTML ||
740
                            mimeinfo->mime_type == MIME_MESSAGE_RFC822)
741
                                gtk_widget_set_sensitive(menuitem, TRUE);
742
                        else
743
                                gtk_widget_set_sensitive(menuitem, FALSE);
744
                } else if (type == PART_MENU_COPY_FILENAME) {
745
                        if (mimeinfo->filename || mimeinfo->name)
746
                                gtk_widget_set_sensitive(menuitem, TRUE);
747
                        else
748
                                gtk_widget_set_sensitive(menuitem, FALSE);
749
                }
750
        }
751

    
752
        g_object_set_data(G_OBJECT(menu), "part-widget", widget);
753
        gtk_menu_popup(GTK_MENU(menu), NULL, NULL, part_menu_button_position, widget, event->button, event->time);
754

    
755
        return TRUE;
756
}
757

    
758
static gboolean textview_part_widget_exposed(GtkWidget *widget,
759
                                             GdkEventExpose *event,
760
                                             gpointer data)
761
{
762
        GdkDrawable *drawable;
763
        GdkGC *gc;
764
        gint w, h;
765

    
766
        w = widget->allocation.width - 1;
767
        h = widget->allocation.height - 1;
768
        if (w <= 0 || h <= 0)
769
                return FALSE;
770

    
771
        drawable = GDK_DRAWABLE(widget->window);
772
        gc = widget->style->dark_gc[GTK_WIDGET_STATE(widget)];
773
        gdk_gc_set_clip_rectangle(gc, &event->area);
774
        gdk_gc_set_line_attributes(gc, 1, GDK_LINE_SOLID, GDK_CAP_NOT_LAST,
775
                                   GDK_JOIN_MITER);
776
        gdk_draw_line(drawable, gc, 1, 0, w, 0);
777
        gdk_draw_line(drawable, gc, w, 1, w, h);
778
        gdk_draw_line(drawable, gc, w, h, 1, h);
779
        gdk_draw_line(drawable, gc, 0, h, 0, 1);
780

    
781
        return FALSE;
782
}
783

    
784
static void textview_part_menu_activated(GtkWidget *widget, gpointer data)
785
{
786
        TextView *textview = (TextView *)data;
787
        GtkWidget *menu;
788
        GtkWidget *part_widget;
789
        gint type;
790
        MimeInfo *mimeinfo;
791
        MimeView *mimeview = textview->messageview->mimeview;
792
        const gchar *filename;
793
        GtkClipboard *clipboard;
794

    
795
        menu = gtk_widget_get_parent(widget);
796
        part_widget = g_object_get_data(G_OBJECT(menu), "part-widget");
797
        mimeinfo = g_object_get_data(G_OBJECT(part_widget), "mimeinfo");
798
        if (!mimeinfo)
799
                return;
800
        type = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), MENU_VAL_ID));
801

    
802
        switch (type) {
803
        case PART_MENU_OPEN:
804
                mimeview_launch_part(mimeview, mimeinfo);
805
                break;
806
        case PART_MENU_OPEN_WITH:
807
                mimeview_open_part_with(mimeview, mimeinfo);
808
                break;
809
        case PART_MENU_SAVE_AS:
810
                mimeview_save_part_as(mimeview, mimeinfo);
811
                break;
812
        case PART_MENU_PRINT:
813
                mimeview_print_part(mimeview, mimeinfo);
814
                break;
815
        case PART_MENU_COPY_FILENAME:
816
                filename = mimeinfo->filename ? mimeinfo->filename :
817
                           mimeinfo->name ? mimeinfo->name : NULL;
818
                if (filename) {
819
                        clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
820
                        gtk_clipboard_set_text(clipboard, filename, -1);
821
                        clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
822
                        gtk_clipboard_set_text(clipboard, filename, -1);
823
                }
824
                break;
825
        default:
826
                break;
827
        }
828
}
829

    
830
static void textview_part_menu_closed(GtkMenuShell *menu_shell, gpointer data)
831
{
832
        g_object_set_data(G_OBJECT(menu_shell), "part-widget", NULL);
833
}
834

    
835
static void textview_part_menu_create(TextView *textview)
836
{
837
        GtkWidget *menu;
838
        GtkWidget *menuitem;
839

    
840
        if (textview->popup_menu)
841
                return;
842

    
843
        menu = gtk_menu_new();
844
        textview->popup_menu = menu;
845

    
846
        MENUITEM_ADD_FROM_STOCK(menu, menuitem, GTK_STOCK_OPEN, PART_MENU_OPEN);
847
        g_signal_connect(G_OBJECT(menuitem), "activate",
848
                         G_CALLBACK(textview_part_menu_activated), textview);
849
        MENUITEM_ADD_WITH_MNEMONIC(menu, menuitem, _("Open _with..."), PART_MENU_OPEN_WITH);
850
        g_signal_connect(G_OBJECT(menuitem), "activate",
851
                         G_CALLBACK(textview_part_menu_activated), textview);
852
        MENUITEM_ADD_WITH_MNEMONIC(menu, menuitem, _("_Save as..."), PART_MENU_SAVE_AS);
853
        g_signal_connect(G_OBJECT(menuitem), "activate",
854
                         G_CALLBACK(textview_part_menu_activated), textview);
855
        MENUITEM_ADD_FROM_STOCK(menu, menuitem, GTK_STOCK_PRINT, PART_MENU_PRINT);
856
        g_signal_connect(G_OBJECT(menuitem), "activate",
857
                         G_CALLBACK(textview_part_menu_activated), textview);
858

    
859
        MENUITEM_ADD(menu, menuitem, NULL, 0);
860

    
861
        MENUITEM_ADD_WITH_MNEMONIC(menu, menuitem, _("_Copy file name"), PART_MENU_COPY_FILENAME);
862
        g_signal_connect(G_OBJECT(menuitem), "activate",
863
                         G_CALLBACK(textview_part_menu_activated), textview);
864

    
865
        g_signal_connect(G_OBJECT(menu), "selection_done",
866
                         G_CALLBACK(textview_part_menu_closed), textview);
867

    
868
        gtk_widget_show_all(menu);
869
}
870

    
871
static void textview_add_part_widget(TextView *textview, GtkTextIter *iter,
872
                                     MimeInfo *mimeinfo, const gchar *str)
873
{
874
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
875
        GtkTextBuffer *buffer;
876
        GtkTextChildAnchor *anchor;
877
        GtkWidget *hbox;
878
        GtkWidget *ebox;
879
        GtkWidget *label;
880
        GtkWidget *arrow;
881
        GtkStyle *style;
882
        GdkColor bg = {0, 0xe000, 0xe500, 0xffff};
883
        GdkColor fg = {0, 0x8000, 0x9800, 0xffff};
884
        GdkColor bg2 = {0, 0xc000, 0xc800, 0xffff};
885
        GdkColor fg2 = {0, 0x6000, 0x8000, 0xffff};
886

    
887
        buffer = gtk_text_view_get_buffer(text);
888
        anchor = gtk_text_buffer_create_child_anchor(buffer, iter);
889
        ebox = gtk_event_box_new();
890
        hbox = gtk_hbox_new(FALSE, 4);
891
        gtk_container_set_border_width(GTK_CONTAINER(hbox), 3);
892
        label = gtk_label_new(str);
893
        gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0);
894
        arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
895
        gtk_box_pack_start(GTK_BOX(hbox), arrow, FALSE, FALSE, 0);
896
        gtk_container_add(GTK_CONTAINER(ebox), hbox);
897
        gtk_widget_show_all(ebox);
898
        g_signal_connect(G_OBJECT(ebox), "button_press_event",
899
                         G_CALLBACK(textview_part_widget_button_pressed),
900
                         textview);
901
        g_signal_connect(G_OBJECT(ebox), "enter_notify_event",
902
                         G_CALLBACK(textview_part_widget_entered), textview);
903
        g_signal_connect(G_OBJECT(ebox), "leave_notify_event",
904
                         G_CALLBACK(textview_part_widget_left), textview);
905
        g_signal_connect_after(G_OBJECT(ebox), "expose_event",
906
                         G_CALLBACK(textview_part_widget_exposed), textview);
907

    
908
        style = gtk_widget_get_style(label);
909
        bg = style->bg[GTK_STATE_NORMAL];
910
        fg = style->fg[GTK_STATE_NORMAL];
911
        bg2 = style->bg[GTK_STATE_PRELIGHT];
912
        fg2 = style->fg[GTK_STATE_PRELIGHT];
913

    
914
        gtk_widget_modify_bg(ebox, GTK_STATE_NORMAL, &bg);
915
        gtk_widget_modify_fg(ebox, GTK_STATE_NORMAL, &fg);
916
        gtk_widget_modify_bg(ebox, GTK_STATE_PRELIGHT, &bg2);
917
        gtk_widget_modify_fg(ebox, GTK_STATE_PRELIGHT, &fg2);
918

    
919
        g_object_set_data(G_OBJECT(ebox), "mimeinfo", mimeinfo);
920

    
921
        gtk_text_view_add_child_at_anchor(text, ebox, anchor);
922
        gtk_text_buffer_insert(buffer, iter, "\n", 1);
923
}
924

    
925
static void textview_add_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
926
{
927
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
928
        GtkTextBuffer *buffer;
929
        GtkTextIter iter;
930
        gchar buf[BUFFSIZE];
931
        const gchar *boundary = NULL;
932
        gint boundary_len = 0;
933
        const gchar *charset;
934
        GPtrArray *headers = NULL;
935
        GtkTextMark *mark;
936

    
937
        g_return_if_fail(mimeinfo != NULL);
938
        g_return_if_fail(fp != NULL);
939

    
940
        buffer = gtk_text_view_get_buffer(text);
941
        gtk_text_buffer_get_end_iter(buffer, &iter);
942

    
943
        if (mimeinfo->mime_type == MIME_MULTIPART) return;
944

    
945
        if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0) {
946
                perror("fseek");
947
                return;
948
        }
949

    
950
        if (mimeinfo->parent && mimeinfo->parent->boundary) {
951
                boundary = mimeinfo->parent->boundary;
952
                boundary_len = strlen(boundary);
953
        }
954

    
955
        while (fgets(buf, sizeof(buf), fp) != NULL)
956
                if (buf[0] == '\r' || buf[0] == '\n') break;
957

    
958
        charset = textview_get_src_encoding(textview, mimeinfo);
959

    
960
        if (mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
961
                g_snprintf(buf, sizeof(buf), "%s (%s)",
962
                           mimeinfo->content_type,
963
                           to_human_readable(mimeinfo->content_size));
964
                gtk_text_buffer_insert(buffer, &iter, "\n", 1);
965
                textview_add_part_widget(textview, &iter, mimeinfo, buf);
966
                gtk_text_buffer_get_end_iter(buffer, &iter);
967
                headers = textview_scan_header(textview, fp, charset);
968
                if (headers) {
969
                        textview_show_header(textview, headers);
970
                        procheader_header_array_destroy(headers);
971
                } else {
972
                        mark = gtk_text_buffer_get_mark(buffer, "attach-file-pos");
973
                        gtk_text_buffer_move_mark(buffer, mark, &iter);
974
                        g_object_set_data(G_OBJECT(mark), "attach-file-count", GINT_TO_POINTER(0));
975
                }
976
                return;
977
        }
978

    
979
#if USE_GPGME
980
        if (mimeinfo->parent && mimeinfo->sigstatus) {
981
                textview_add_sig_part(textview, mimeinfo);
982
                return;
983
        }
984
#endif
985

    
986
        if (mimeinfo->filename || mimeinfo->name)
987
                g_snprintf(buf, sizeof(buf), "%s  %s (%s)",
988
                           mimeinfo->filename ? mimeinfo->filename :
989
                           mimeinfo->name,
990
                           mimeinfo->content_type,
991
                           to_human_readable(mimeinfo->content_size));
992
        else
993
                g_snprintf(buf, sizeof(buf), "%s (%s)",
994
                           mimeinfo->content_type,
995
                           to_human_readable(mimeinfo->content_size));
996

    
997
        if (mimeinfo->mime_type != MIME_TEXT &&
998
            mimeinfo->mime_type != MIME_TEXT_HTML) {
999
                if (mimeinfo->mime_type == MIME_IMAGE &&
1000
                    prefs_common.inline_image) {
1001
                        GdkPixbuf *pixbuf;
1002
                        GError *error = NULL;
1003
                        gchar *filename;
1004
                        RemoteURI *uri;
1005
                        gchar *uri_str;
1006

    
1007
                        gtk_text_buffer_insert(buffer, &iter, "\n", 1);
1008
                        textview_add_part_widget(textview, &iter, mimeinfo, buf);
1009

    
1010
                        filename = procmime_get_tmp_file_name(mimeinfo);
1011
                        if (procmime_get_part_fp(filename, fp, mimeinfo) < 0) {
1012
                                g_warning("Can't get the image file.");
1013
                                g_free(filename);
1014
                                return;
1015
                        }
1016

    
1017
                        pixbuf = gdk_pixbuf_new_from_file(filename, &error);
1018
                        if (error != NULL) {
1019
                                g_warning("%s\n", error->message);
1020
                                g_error_free(error);
1021
                        }
1022
                        if (!pixbuf) {
1023
                                g_warning("Can't load the image.");
1024
                                g_free(filename);
1025
                                return;
1026
                        }
1027

    
1028
                        if (prefs_common.resize_image) {
1029
                                GdkPixbuf *scaled;
1030

    
1031
                                scaled = imageview_get_resized_pixbuf
1032
                                        (pixbuf, textview->text, 8);
1033
                                g_object_unref(pixbuf);
1034
                                pixbuf = scaled;
1035
                        }
1036

    
1037
                        uri_str = g_filename_to_uri(filename, NULL, NULL);
1038
                        if (uri_str) {
1039
                                uri = g_new(RemoteURI, 1);
1040
                                uri->uri = uri_str;
1041
                                uri->filename =
1042
                                        procmime_get_part_file_name(mimeinfo);
1043
                                uri->start = gtk_text_iter_get_offset(&iter);
1044
                                uri->end = uri->start + 1;
1045
                                textview->uri_list =
1046
                                        g_slist_append(textview->uri_list, uri);
1047
                        }
1048
                        gtk_text_buffer_insert_pixbuf(buffer, &iter, pixbuf);
1049
                        gtk_text_buffer_insert(buffer, &iter, "\n", 1);
1050

    
1051
                        g_object_unref(pixbuf);
1052
                        g_free(filename);
1053
                } else if (prefs_common.show_attached_files_first) {
1054
                        gint count;
1055
                        gint prev_pos, new_pos;
1056

    
1057
                        mark = gtk_text_buffer_get_mark(buffer, "attach-file-pos");
1058

    
1059
                        gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
1060
                        prev_pos = gtk_text_iter_get_offset(&iter);
1061
                        count = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(mark), "attach-file-count"));
1062
                        if (count == 0) {
1063
                                textview_insert_pad(textview, &iter, 4);
1064
                                gtk_text_buffer_move_mark(buffer, mark, &iter);
1065
                        }
1066
                        textview_add_part_widget(textview, &iter, mimeinfo, buf);
1067
                        gtk_text_buffer_move_mark(buffer, mark, &iter);
1068
                        new_pos = gtk_text_iter_get_offset(&iter);
1069
                        textview_uri_list_update_offsets(textview, new_pos, new_pos - prev_pos);
1070
                        g_object_set_data(G_OBJECT(mark), "attach-file-count", GINT_TO_POINTER(count + 1));
1071
                        gtk_text_buffer_get_end_iter(buffer, &iter);
1072
                } else {
1073
                        gtk_text_buffer_insert(buffer, &iter, "\n", 1);
1074
                        textview_add_part_widget(textview, &iter, mimeinfo, buf);
1075
                }
1076
        } else {
1077
                /* text part */
1078
                gtk_text_buffer_insert(buffer, &iter, "\n", 1);
1079
                if (mimeinfo->mime_type == MIME_TEXT_HTML ||
1080
                    (!mimeinfo->main && mimeinfo->parent &&
1081
                     mimeinfo->parent->children != mimeinfo))
1082
                        textview_add_part_widget(textview, &iter, mimeinfo, buf);
1083
                textview_write_body(textview, mimeinfo, fp, charset);
1084
        }
1085
}
1086

    
1087
#if USE_GPGME
1088
static void textview_add_sig_part(TextView *textview, MimeInfo *mimeinfo)
1089
{
1090
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1091
        GtkTextBuffer *buffer;
1092
        GtkTextIter iter;
1093
        gchar buf[BUFFSIZE];
1094
        const gchar *color;
1095
        const gchar *sigstatus;
1096
        const gchar *sigstatus_full;
1097
        const gchar *type;
1098

    
1099
        if (mimeinfo) {
1100
                sigstatus = mimeinfo->sigstatus;
1101
                sigstatus_full = mimeinfo->sigstatus_full;
1102
                type = mimeinfo->content_type;
1103
        } else if (textview->messageview->msginfo->encinfo) {
1104
                sigstatus = textview->messageview->msginfo->encinfo->sigstatus;
1105
                sigstatus_full =
1106
                        textview->messageview->msginfo->encinfo->sigstatus_full;
1107
                type = "signature";
1108
        } else
1109
                return;
1110

    
1111
        if (!sigstatus)
1112
                return;
1113

    
1114
        g_snprintf(buf, sizeof(buf), "\n[%s (%s)]\n", type, sigstatus);
1115

    
1116
        if (!strcmp(sigstatus, _("Good signature")))
1117
                color = "good-signature";
1118
        else if (!strcmp(sigstatus, _("Valid signature (untrusted key)")))
1119
                color = "untrusted-signature";
1120
        else if (!strcmp(sigstatus, _("BAD signature")))
1121
                color = "bad-signature";
1122
        else
1123
                color = "nocheck-signature";
1124

    
1125
        buffer = gtk_text_view_get_buffer(text);
1126
        gtk_text_buffer_get_end_iter(buffer, &iter);
1127
        gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, buf, -1,
1128
                                                 color, "mimepart", NULL);
1129
        if (sigstatus_full)
1130
                gtk_text_buffer_insert_with_tags_by_name
1131
                        (buffer, &iter, sigstatus_full, -1, "mimepart", NULL);
1132
}
1133
#endif
1134

    
1135
static void textview_add_parts(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
1136
{
1137
        gint level;
1138

    
1139
        g_return_if_fail(mimeinfo != NULL);
1140
        g_return_if_fail(fp != NULL);
1141

    
1142
        level = mimeinfo->level;
1143

    
1144
        for (;;) {
1145
                if (mimeinfo->mime_type == MIME_MULTIPART &&
1146
                    mimeinfo->content_type &&
1147
                    !g_ascii_strcasecmp(mimeinfo->content_type,
1148
                                        "multipart/alternative")) {
1149
                        MimeInfo *preferred_part = mimeinfo->children;
1150
                        MimeInfo *child;
1151

    
1152
                        if (prefs_common.alt_prefer_html) {
1153
                                for (child = mimeinfo->children; child != NULL; child = child->next) {
1154
                                        if (child->mime_type == MIME_TEXT_HTML) {
1155
                                                preferred_part = child;
1156
                                                break;
1157
                                        }
1158
                                }
1159
                        }
1160

    
1161
                        if (preferred_part) {
1162
                                textview_add_part(textview, preferred_part, fp);
1163
                                mimeinfo = preferred_part;
1164
                                while (mimeinfo->next)
1165
                                        mimeinfo = mimeinfo->next;
1166
                        }
1167
                } else {
1168
                        textview_add_part(textview, mimeinfo, fp);
1169
                }
1170

    
1171
                mimeinfo = procmime_mimeinfo_next(mimeinfo);
1172

    
1173
                if (!mimeinfo || mimeinfo->level <= level)
1174
                        break;
1175
        }
1176
}
1177

    
1178
static void textview_write_error(TextView *textview, const gchar *msg)
1179
{
1180
        GtkTextBuffer *buffer;
1181
        GtkTextIter iter;
1182

    
1183
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
1184
        gtk_text_buffer_get_end_iter(buffer, &iter);
1185
        gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, msg, -1,
1186
                                                 "error", NULL);
1187
}
1188

    
1189
void textview_show_error(TextView *textview)
1190
{
1191
        textview_set_font(textview, NULL);
1192
        textview_clear(textview);
1193
        textview_write_error(textview, _("This message can't be displayed.\n"));
1194
}
1195

    
1196
static void textview_write_body(TextView *textview, MimeInfo *mimeinfo,
1197
                                FILE *fp, const gchar *charset)
1198
{
1199
        FILE *tmpfp;
1200
        gchar buf[BUFFSIZE];
1201
        CodeConverter *conv;
1202

    
1203
        conv = conv_code_converter_new(charset, NULL);
1204

    
1205
        tmpfp = procmime_decode_content(NULL, fp, mimeinfo);
1206
        if (tmpfp) {
1207
                if (mimeinfo->mime_type == MIME_TEXT_HTML &&
1208
                    prefs_common.render_html)
1209
                        textview_show_html(textview, tmpfp, conv);
1210
                else
1211
                        while (fgets(buf, sizeof(buf), tmpfp) != NULL)
1212
                                textview_write_line(textview, buf, conv);
1213
                fclose(tmpfp);
1214
        } else {
1215
                textview_write_error
1216
                        (textview,
1217
                         _("The body text couldn't be displayed because "
1218
                           "writing to temporary file failed.\n"));
1219
        }
1220

    
1221
        conv_code_converter_destroy(conv);
1222
}
1223

    
1224
static void textview_show_html(TextView *textview, FILE *fp,
1225
                               CodeConverter *conv)
1226
{
1227
        HTMLParser *parser;
1228
        const gchar *str;
1229

    
1230
        parser = html_parser_new(fp, conv);
1231
        g_return_if_fail(parser != NULL);
1232

    
1233
        while ((str = html_parse(parser)) != NULL) {
1234
                if (parser->href != NULL)
1235
                        textview_write_link(textview, str, parser->href, NULL);
1236
                else
1237
                        textview_write_line(textview, str, NULL);
1238
        }
1239
        textview_write_line(textview, "\n", NULL);
1240

    
1241
        html_parser_destroy(parser);
1242
}
1243

    
1244
/* get_uri_part() - retrieves a URI starting from scanpos.
1245
                    Returns TRUE if succesful */
1246
static gboolean get_uri_part(const gchar *start, const gchar *scanpos,
1247
                             const gchar **bp, const gchar **ep)
1248
{
1249
        const gchar *ep_;
1250

    
1251
        g_return_val_if_fail(start != NULL, FALSE);
1252
        g_return_val_if_fail(scanpos != NULL, FALSE);
1253
        g_return_val_if_fail(bp != NULL, FALSE);
1254
        g_return_val_if_fail(ep != NULL, FALSE);
1255

    
1256
        *bp = scanpos;
1257

    
1258
        /* find end point of URI */
1259
        for (ep_ = scanpos; *ep_ != '\0'; ep_++) {
1260
                if (!g_ascii_isgraph(*ep_) ||
1261
                    !isascii(*(const guchar *)ep_) ||
1262
                    strchr("()<>{}[]\"", *ep_))
1263
                        break;
1264
        }
1265

    
1266
        /* no punctuation at end of string */
1267

    
1268
        /* FIXME: this stripping of trailing punctuations may bite with other URIs.
1269
         * should pass some URI type to this function and decide on that whether
1270
         * to perform punctuation stripping */
1271

    
1272
#define IS_REAL_PUNCT(ch)        (g_ascii_ispunct(ch) && !strchr("/?=", ch)) 
1273

    
1274
        for (; ep_ - 1 > scanpos + 1 && IS_REAL_PUNCT(*(ep_ - 1)); ep_--)
1275
                ;
1276

    
1277
#undef IS_REAL_PUNCT
1278

    
1279
        *ep = ep_;
1280

    
1281
        return TRUE;                
1282
}
1283

    
1284
static gchar *make_uri_string(const gchar *bp, const gchar *ep)
1285
{
1286
        return g_strndup(bp, ep - bp);
1287
}
1288

    
1289
static gchar *make_http_uri_string(const gchar *bp, const gchar *ep)
1290
{
1291
        gchar *tmp;
1292
        gchar *result;
1293

    
1294
        tmp = g_strndup(bp, ep - bp);
1295
        result = g_strconcat("http://", tmp, NULL);
1296
        g_free(tmp);
1297

    
1298
        return result;
1299
}
1300

    
1301
/* valid mail address characters */
1302
#define IS_RFC822_CHAR(ch) \
1303
        (isascii(ch) && \
1304
         (ch) > 32   && \
1305
         (ch) != 127 && \
1306
         !g_ascii_isspace(ch) && \
1307
         !strchr("(),;<>\"", (ch)))
1308

    
1309
/* alphabet and number within 7bit ASCII */
1310
#define IS_ASCII_ALNUM(ch)        (isascii(ch) && g_ascii_isalnum(ch))
1311

    
1312
/* get_email_part() - retrieves an email address. Returns TRUE if succesful */
1313
static gboolean get_email_part(const gchar *start, const gchar *scanpos,
1314
                               const gchar **bp, const gchar **ep)
1315
{
1316
        /* more complex than the uri part because we need to scan back and forward starting from
1317
         * the scan position. */
1318
        gboolean result = FALSE;
1319
        const gchar *bp_;
1320
        const gchar *ep_;
1321

    
1322
        g_return_val_if_fail(start != NULL, FALSE);
1323
        g_return_val_if_fail(scanpos != NULL, FALSE);
1324
        g_return_val_if_fail(bp != NULL, FALSE);
1325
        g_return_val_if_fail(ep != NULL, FALSE);
1326

    
1327
        /* scan start of address */
1328
        for (bp_ = scanpos - 1;
1329
             bp_ >= start && IS_RFC822_CHAR(*(const guchar *)bp_); bp_--)
1330
                ;
1331

    
1332
        /* TODO: should start with an alnum? */
1333
        bp_++;
1334
        for (; bp_ < scanpos && !IS_ASCII_ALNUM(*(const guchar *)bp_); bp_++)
1335
                ;
1336

    
1337
        if (bp_ != scanpos) {
1338
                /* scan end of address */
1339
                for (ep_ = scanpos + 1;
1340
                     *ep_ && IS_RFC822_CHAR(*(const guchar *)ep_); ep_++)
1341
                        ;
1342

    
1343
                /* TODO: really should terminate with an alnum? */
1344
                for (; ep_ > scanpos && !IS_ASCII_ALNUM(*(const guchar *)ep_);
1345
                     --ep_)
1346
                        ;
1347
                ep_++;
1348

    
1349
                if (ep_ > scanpos + 1) {
1350
                        *ep = ep_;
1351
                        *bp = bp_;
1352
                        result = TRUE;
1353
                }
1354
        }
1355

    
1356
        return result;
1357
}
1358

    
1359
#undef IS_ASCII_ALNUM
1360
#undef IS_RFC822_CHAR
1361

    
1362
static gchar *make_email_string(const gchar *bp, const gchar *ep)
1363
{
1364
        /* returns a mailto: URI; mailto: is also used to detect the
1365
         * uri type later on in the button_pressed signal handler */
1366
        gchar *tmp, *enc;
1367
        gchar *result;
1368

    
1369
        tmp = g_strndup(bp, ep - bp);
1370
        enc = uriencode_for_mailto(tmp);
1371
        result = g_strconcat("mailto:", enc, NULL);
1372
        g_free(enc);
1373
        g_free(tmp);
1374

    
1375
        return result;
1376
}
1377

    
1378
#define ADD_TXT_POS(bp_, ep_, pti_) \
1379
{ \
1380
        struct txtpos *last; \
1381
 \
1382
        last = g_new(struct txtpos, 1); \
1383
        last->bp = (bp_); \
1384
        last->ep = (ep_); \
1385
        last->pti = (pti_); \
1386
        txtpos_list = g_slist_append(txtpos_list, last); \
1387
}
1388

    
1389
/* textview_make_clickable_parts() - colorizes clickable parts */
1390
static void textview_make_clickable_parts(TextView *textview,
1391
                                          const gchar *fg_tag,
1392
                                          const gchar *uri_tag,
1393
                                          const gchar *linebuf)
1394
{
1395
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1396
        GtkTextBuffer *buffer;
1397
        GtkTextIter iter;
1398

    
1399
        /* parse table - in order of priority */
1400
        struct table {
1401
                const gchar *needle; /* token */
1402

    
1403
                /* token search function */
1404
                gchar    *(*search)        (const gchar *haystack,
1405
                                         const gchar *needle);
1406
                /* part parsing function */
1407
                gboolean  (*parse)        (const gchar *start,
1408
                                         const gchar *scanpos,
1409
                                         const gchar **bp_,
1410
                                         const gchar **ep_);
1411
                /* part to URI function */
1412
                gchar    *(*build_uri)        (const gchar *bp,
1413
                                         const gchar *ep);
1414
        };
1415

    
1416
        static struct table parser[] = {
1417
                {"http://",  strcasestr, get_uri_part,   make_uri_string},
1418
                {"https://", strcasestr, get_uri_part,   make_uri_string},
1419
                {"ftp://",   strcasestr, get_uri_part,   make_uri_string},
1420
                {"www.",     strcasestr, get_uri_part,   make_http_uri_string},
1421
                {"mailto:",  strcasestr, get_uri_part,   make_uri_string},
1422
                {"@",        strcasestr, get_email_part, make_email_string}
1423
        };
1424
        const gint PARSE_ELEMS = sizeof parser / sizeof parser[0];
1425

    
1426
        /* flags for search optimization */
1427
        gboolean do_search[] = {TRUE, TRUE, TRUE, TRUE, TRUE, TRUE};
1428

    
1429
        gint  n;
1430
        const gchar *walk, *bp, *ep;
1431

    
1432
        struct txtpos {
1433
                const gchar        *bp, *ep;        /* text position */
1434
                gint                 pti;                /* index in parse table */
1435
        };
1436
        GSList *txtpos_list = NULL;
1437

    
1438
        buffer = gtk_text_view_get_buffer(text);
1439
        gtk_text_buffer_get_end_iter(buffer, &iter);
1440

    
1441
        /* parse for clickable parts, and build a list of begin and
1442
           end positions  */
1443
        for (walk = linebuf, n = 0;;) {
1444
                gint last_index = PARSE_ELEMS;
1445
                const gchar *scanpos = NULL;
1446

    
1447
                /* FIXME: this looks phony. scanning for anything in the
1448
                   parse table */
1449
                for (n = 0; n < PARSE_ELEMS; n++) {
1450
                        const gchar *tmp;
1451

    
1452
                        if (do_search[n]) {
1453
                                tmp = parser[n].search(walk, parser[n].needle);
1454
                                if (tmp) {
1455
                                        if (scanpos == NULL || tmp < scanpos) {
1456
                                                scanpos = tmp;
1457
                                                last_index = n;
1458
                                        }
1459
                                } else
1460
                                        do_search[n] = FALSE;
1461
                        }
1462
                }
1463

    
1464
                if (scanpos) {
1465
                        /* check if URI can be parsed */
1466
                        if (parser[last_index].parse(walk, scanpos, &bp, &ep)
1467
                            && (ep - bp - 1) > strlen(parser[last_index].needle)) {
1468
                                        ADD_TXT_POS(bp, ep, last_index);
1469
                                        walk = ep;
1470
                        } else
1471
                                walk = scanpos +
1472
                                        strlen(parser[last_index].needle);
1473
                } else
1474
                        break;
1475
        }
1476

    
1477
        /* colorize this line */
1478
        if (txtpos_list) {
1479
                const gchar *normal_text = linebuf;
1480
                GSList *cur;
1481

    
1482
                /* insert URIs */
1483
                for (cur = txtpos_list; cur != NULL; cur = cur->next) {
1484
                        struct txtpos *pos = (struct txtpos *)cur->data;
1485
                        RemoteURI *uri;
1486

    
1487
                        uri = g_new(RemoteURI, 1);
1488
                        if (pos->bp - normal_text > 0)
1489
                                gtk_text_buffer_insert_with_tags_by_name
1490
                                        (buffer, &iter,
1491
                                         normal_text,
1492
                                         pos->bp - normal_text,
1493
                                         fg_tag, NULL);
1494
                        uri->uri = parser[pos->pti].build_uri(pos->bp, pos->ep);
1495
                        uri->filename = NULL;
1496
                        uri->start = gtk_text_iter_get_offset(&iter);
1497
                        gtk_text_buffer_insert_with_tags_by_name
1498
                                (buffer, &iter, pos->bp, pos->ep - pos->bp,
1499
                                 uri_tag, fg_tag, NULL);
1500
                        uri->end = gtk_text_iter_get_offset(&iter);
1501
                        textview->uri_list =
1502
                                g_slist_append(textview->uri_list, uri);
1503
                        normal_text = pos->ep;
1504

    
1505
                        g_free(pos);
1506
                }
1507

    
1508
                if (*normal_text)
1509
                        gtkut_text_buffer_insert_with_tag_by_name
1510
                                (buffer, &iter, normal_text, -1, fg_tag);
1511

    
1512
                g_slist_free(txtpos_list);
1513
        } else {
1514
                gtkut_text_buffer_insert_with_tag_by_name
1515
                        (buffer, &iter, linebuf, -1, fg_tag);
1516
        }
1517
}
1518

    
1519
#undef ADD_TXT_POS
1520

    
1521
static void textview_write_line(TextView *textview, const gchar *str,
1522
                                CodeConverter *conv)
1523
{
1524
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1525
        GtkTextBuffer *buffer;
1526
        GtkTextIter iter;
1527
        gchar *buf;
1528
        gchar *fg_color = NULL;
1529
        gint quotelevel = -1;
1530
        gchar quote_tag_str[10];
1531

    
1532
        buffer = gtk_text_view_get_buffer(text);
1533
        gtk_text_buffer_get_end_iter(buffer, &iter);
1534

    
1535
        if (conv) {
1536
                buf = conv_convert(conv, str);
1537
                if (!buf)
1538
                        buf = conv_utf8todisp(str, NULL);
1539
        } else
1540
                buf = g_strdup(str);
1541

    
1542
        strcrchomp(buf);
1543
        //if (prefs_common.conv_mb_alnum) conv_mb_alnum(buf);
1544

    
1545
        /* change color of quotation
1546
           >, foo>, _> ... ok, <foo>, foo bar>, foo-> ... ng
1547
           Up to 3 levels of quotations are detected, and each
1548
           level is colored using a different color. */
1549
        if (prefs_common.enable_color && strchr(buf, '>')) {
1550
                quotelevel = get_quote_level(buf);
1551

    
1552
                /* set up the correct foreground color */
1553
                if (quotelevel > 2) {
1554
                        /* recycle colors */
1555
                        if (prefs_common.recycle_quote_colors)
1556
                                quotelevel %= 3;
1557
                        else
1558
                                quotelevel = 2;
1559
                }
1560
        }
1561

    
1562
        if (quotelevel != -1) {
1563
                g_snprintf(quote_tag_str, sizeof(quote_tag_str),
1564
                           "quote%d", quotelevel);
1565
                fg_color = quote_tag_str;
1566
        }
1567

    
1568
        if (prefs_common.enable_color)
1569
                textview_make_clickable_parts(textview, fg_color, "link", buf);
1570
        else
1571
                textview_make_clickable_parts(textview, fg_color, NULL, buf);
1572

    
1573
        g_free(buf);
1574
}
1575

    
1576
static void textview_write_link(TextView *textview, const gchar *str,
1577
                                const gchar *uri, CodeConverter *conv)
1578
{
1579
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1580
        GtkTextBuffer *buffer;
1581
        GtkTextIter iter;
1582
        gchar *buf;
1583
        gchar *bufp;
1584
        RemoteURI *r_uri;
1585

    
1586
        if (!str || *str == '\0')
1587
                return;
1588
        if (!uri)
1589
                return;
1590

    
1591
        buffer = gtk_text_view_get_buffer(text);
1592
        gtk_text_buffer_get_end_iter(buffer, &iter);
1593

    
1594
        if (conv) {
1595
                buf = conv_convert(conv, str);
1596
                if (!buf)
1597
                        buf = conv_utf8todisp(str, NULL);
1598
        } else
1599
                buf = g_strdup(str);
1600

    
1601
        if (g_utf8_validate(buf, -1, NULL) == FALSE) {
1602
                g_free(buf);
1603
                return;
1604
        }
1605

    
1606
        strcrchomp(buf);
1607

    
1608
        for (bufp = buf; *bufp != '\0'; bufp = g_utf8_next_char(bufp)) {
1609
                gunichar ch;
1610

    
1611
                ch = g_utf8_get_char(bufp);
1612
                if (!g_unichar_isspace(ch))
1613
                        break;
1614
        }
1615
        if (bufp > buf)
1616
                gtk_text_buffer_insert(buffer, &iter, buf, bufp - buf);
1617

    
1618
        r_uri = g_new(RemoteURI, 1);
1619
        r_uri->uri = g_strstrip(g_strdup(uri));
1620
        r_uri->filename = NULL;
1621
        r_uri->start = gtk_text_iter_get_offset(&iter);
1622
        gtk_text_buffer_insert_with_tags_by_name
1623
                (buffer, &iter, bufp, -1, "link", NULL);
1624
        r_uri->end = gtk_text_iter_get_offset(&iter);
1625
        textview->uri_list = g_slist_append(textview->uri_list, r_uri);
1626

    
1627
        g_free(buf);
1628
}
1629

    
1630
void textview_clear(TextView *textview)
1631
{
1632
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1633
        GtkTextBuffer *buffer;
1634

    
1635
        buffer = gtk_text_view_get_buffer(text);
1636
        gtk_text_buffer_set_text(buffer, "", -1);
1637

    
1638
        /* workaround for the assertion failure in
1639
           gtk_text_view_validate_onscreen() */
1640
        text->vadjustment->value = 0.0;
1641

    
1642
        STATUSBAR_POP(textview);
1643
        textview_uri_list_remove_all(textview->uri_list);
1644
        textview->uri_list = NULL;
1645

    
1646
        textview->body_pos = 0;
1647
}
1648

    
1649
void textview_destroy(TextView *textview)
1650
{
1651
        GtkTextBuffer *buffer;
1652
        GtkClipboard *clipboard;
1653

    
1654
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
1655
        clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1656
        gtk_text_buffer_remove_selection_clipboard(buffer, clipboard);
1657

    
1658
        gtk_widget_destroy(textview->popup_menu);
1659

    
1660
        textview_uri_list_remove_all(textview->uri_list);
1661
        textview->uri_list = NULL;
1662

    
1663
        g_free(textview);
1664
}
1665

    
1666
void textview_set_all_headers(TextView *textview, gboolean all_headers)
1667
{
1668
        textview->show_all_headers = all_headers;
1669
}
1670

    
1671
void textview_set_font(TextView *textview, const gchar *codeset)
1672
{
1673
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1674

    
1675
        if (prefs_common.textfont) {
1676
                PangoFontDescription *font_desc;
1677
                font_desc = pango_font_description_from_string
1678
                        (prefs_common.textfont);
1679
                if (font_desc) {
1680
                        gtk_widget_modify_font(textview->text, font_desc);
1681
                        pango_font_description_free(font_desc);
1682
                }
1683
        }
1684

    
1685
        gtk_text_view_set_pixels_above_lines(text, prefs_common.line_space - prefs_common.line_space / 2);
1686
        gtk_text_view_set_pixels_below_lines(text, prefs_common.line_space / 2);
1687
        gtk_text_view_set_pixels_inside_wrap(text, prefs_common.line_space);
1688
}
1689

    
1690
void textview_set_position(TextView *textview, gint pos)
1691
{
1692
        GtkTextBuffer *buffer;
1693
        GtkTextIter iter;
1694

    
1695
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
1696
        gtk_text_buffer_get_iter_at_offset(buffer, &iter, pos);
1697
        gtk_text_buffer_place_cursor(buffer, &iter);
1698
}
1699

    
1700
static GPtrArray *textview_scan_header(TextView *textview, FILE *fp,
1701
                                       const gchar *encoding)
1702
{
1703
        g_return_val_if_fail(fp != NULL, NULL);
1704

    
1705
        if (textview->show_all_headers)
1706
                return procheader_get_header_array_asis(fp, encoding);
1707

    
1708
        if (!prefs_common.display_header) {
1709
                gchar buf[BUFFSIZE];
1710

    
1711
                while (fgets(buf, sizeof(buf), fp) != NULL)
1712
                        if (buf[0] == '\r' || buf[0] == '\n') break;
1713
                return NULL;
1714
        }
1715

    
1716
        return procheader_get_header_array_for_display(fp, encoding);
1717
}
1718
 
1719
static void textview_size_allocate_cb(GtkWidget *widget,
1720
                                      GtkAllocation *allocation, gpointer data)
1721
{
1722
        GtkWidget *child = GTK_WIDGET(data);
1723
        gint new_width;
1724

    
1725
        /* g_print("textview_size_allocate_cb: (%d, %d)\n", allocation->width, allocation->height); */
1726
        new_width = allocation->width - 14;
1727
        if (new_width < -1)
1728
                new_width = -1;
1729
        gtk_widget_set_size_request(child, new_width, 16);
1730
}
1731

    
1732
void textview_hline_destroy_cb(GtkObject *object, gpointer data)
1733
{
1734
        GtkWidget *text = GTK_WIDGET(data);
1735

    
1736
        g_signal_handlers_disconnect_by_func(text, textview_size_allocate_cb,
1737
                                             object);
1738
}
1739

    
1740
static void textview_show_header(TextView *textview, GPtrArray *headers)
1741
{
1742
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1743
        GtkTextBuffer *buffer;
1744
        GtkTextIter iter;
1745
        Header *header;
1746
        gint i;
1747
        GtkTextMark *mark;
1748

    
1749
        g_return_if_fail(headers != NULL);
1750

    
1751
        buffer = gtk_text_view_get_buffer(text);
1752
        gtk_text_buffer_get_end_iter(buffer, &iter);
1753

    
1754
        for (i = 0; i < headers->len; i++) {
1755
                header = g_ptr_array_index(headers, i);
1756
                g_return_if_fail(header->name != NULL);
1757

    
1758
                gtk_text_buffer_insert_with_tags_by_name
1759
                        (buffer, &iter, header->name, -1,
1760
                         "header_title", "header", NULL);
1761
                gtk_text_buffer_insert_with_tags_by_name
1762
                        (buffer, &iter, ":", 1,
1763
                         "header_title", "header", NULL);
1764

    
1765
                if (!g_ascii_strcasecmp(header->name, "Subject") ||
1766
                    !g_ascii_strcasecmp(header->name, "From")    ||
1767
                    !g_ascii_strcasecmp(header->name, "To")      ||
1768
                    !g_ascii_strcasecmp(header->name, "Cc"))
1769
                        unfold_line(header->body);
1770

    
1771
                if (prefs_common.enable_color &&
1772
                    (!strncmp(header->name, "X-Mailer", 8) ||
1773
                     !strncmp(header->name, "X-Newsreader", 12)) &&
1774
                    strstr(header->body, "Sylpheed") != NULL) {
1775
                        gtk_text_buffer_insert_with_tags_by_name
1776
                                (buffer, &iter, header->body, -1,
1777
                                 "header", "emphasis", NULL);
1778
                } else if (prefs_common.enable_color) {
1779
                        textview_make_clickable_parts
1780
                                (textview, "header", "link", header->body);
1781
                } else {
1782
                        textview_make_clickable_parts
1783
                                (textview, "header", NULL, header->body);
1784
                }
1785
                gtk_text_buffer_get_end_iter(buffer, &iter);
1786
                gtk_text_buffer_insert_with_tags_by_name
1787
                        (buffer, &iter, "\n", 1, "header", NULL);
1788
        }
1789

    
1790
        mark = gtk_text_buffer_get_mark(buffer, "attach-file-pos");
1791
        gtk_text_buffer_move_mark(buffer, mark, &iter);
1792
        g_object_set_data(G_OBJECT(mark), "attach-file-count", GINT_TO_POINTER(0));
1793

    
1794
        textview_insert_border(textview, &iter, 0);
1795
}
1796

    
1797
static void textview_insert_border(TextView *textview, GtkTextIter *iter,
1798
                                   gint padding)
1799
{
1800
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1801
        GtkTextBuffer *buffer;
1802
        GtkTextChildAnchor *anchor;
1803
        GtkWidget *vbox;
1804
        GtkWidget *pad;
1805
        GtkWidget *hline;
1806

    
1807
        buffer = gtk_text_view_get_buffer(text);
1808

    
1809
        anchor = gtk_text_buffer_create_child_anchor(buffer, iter);
1810
        vbox = gtk_vbox_new(FALSE, 0);
1811
        if (padding > 0) {
1812
                pad = gtk_vbox_new(FALSE, 0);
1813
                gtk_box_pack_start(GTK_BOX(vbox), pad, FALSE, FALSE, 0);
1814
                gtk_widget_set_size_request(pad, -1, padding);
1815
        }
1816
        hline = gtk_hseparator_new();
1817
        gtk_box_pack_start(GTK_BOX(vbox), hline, FALSE, FALSE, 0);
1818
        gtk_widget_show_all(vbox);
1819
        gtk_widget_set_size_request(hline, 320, 16);
1820
        gtk_text_view_add_child_at_anchor(text, vbox, anchor);
1821
        g_signal_connect(G_OBJECT(hline), "destroy",
1822
                         G_CALLBACK(textview_hline_destroy_cb), text);
1823
        g_signal_connect(G_OBJECT(text), "size-allocate",
1824
                         G_CALLBACK(textview_size_allocate_cb), hline);
1825
}
1826

    
1827
static void textview_insert_pad(TextView *textview, GtkTextIter *iter,
1828
                                gint padding)
1829
{
1830
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1831
        GtkTextBuffer *buffer;
1832
        GtkTextChildAnchor *anchor;
1833
        GtkWidget *vbox;
1834

    
1835
        buffer = gtk_text_view_get_buffer(text);
1836

    
1837
        anchor = gtk_text_buffer_create_child_anchor(buffer, iter);
1838
        vbox = gtk_vbox_new(FALSE, 0);
1839
        gtk_widget_set_size_request(vbox, -1, padding);
1840
        gtk_widget_show_all(vbox);
1841
        gtk_text_view_add_child_at_anchor(text, vbox, anchor);
1842

    
1843
        gtk_text_buffer_insert(buffer, iter, "\n", 1);
1844
}
1845

    
1846
gboolean textview_search_string(TextView *textview, const gchar *str,
1847
                                gboolean case_sens)
1848
{
1849
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1850
        GtkTextBuffer *buffer;
1851
        GtkTextIter iter, match_pos;
1852
        GtkTextMark *mark;
1853
        gint len;
1854

    
1855
        g_return_val_if_fail(str != NULL, FALSE);
1856

    
1857
        buffer = gtk_text_view_get_buffer(text);
1858

    
1859
        len = g_utf8_strlen(str, -1);
1860
        g_return_val_if_fail(len >= 0, FALSE);
1861

    
1862
        gtk_text_buffer_get_selection_bounds(buffer, NULL, &iter);
1863

    
1864
        if (gtkut_text_buffer_find(buffer, &iter, str, case_sens,
1865
                                   &match_pos)) {
1866
                GtkTextIter end = match_pos;
1867

    
1868
                gtk_text_iter_forward_chars(&end, len);
1869
                /* place "insert" at the last character */
1870
                gtk_text_buffer_select_range(buffer, &end, &match_pos);
1871
                mark = gtk_text_buffer_get_insert(buffer);
1872
                gtk_text_view_scroll_to_mark(text, mark, 0.0, FALSE, 0.0, 0.0);
1873
                return TRUE;
1874
        }
1875

    
1876
        return FALSE;
1877
}
1878

    
1879
gboolean textview_search_string_backward(TextView *textview, const gchar *str,
1880
                                         gboolean case_sens)
1881
{
1882
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1883
        GtkTextBuffer *buffer;
1884
        GtkTextIter iter, match_pos;
1885
        GtkTextMark *mark;
1886
        gint len;
1887

    
1888
        g_return_val_if_fail(str != NULL, FALSE);
1889

    
1890
        buffer = gtk_text_view_get_buffer(text);
1891

    
1892
        len = g_utf8_strlen(str, -1);
1893
        g_return_val_if_fail(len >= 0, FALSE);
1894

    
1895
        gtk_text_buffer_get_selection_bounds(buffer, &iter, NULL);
1896

    
1897
        if (gtkut_text_buffer_find_backward(buffer, &iter, str, case_sens,
1898
                                            &match_pos)) {
1899
                GtkTextIter end = match_pos;
1900

    
1901
                gtk_text_iter_forward_chars(&end, len);
1902
                gtk_text_buffer_select_range(buffer, &match_pos, &end);
1903
                mark = gtk_text_buffer_get_insert(buffer);
1904
                gtk_text_view_scroll_to_mark(text, mark, 0.0, FALSE, 0.0, 0.0);
1905
                return TRUE;
1906
        }
1907

    
1908
        return FALSE;
1909
}
1910

    
1911
void textview_scroll_one_line(TextView *textview, gboolean up)
1912
{
1913
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1914
        GtkAdjustment *vadj = text->vadjustment;
1915
        gfloat upper;
1916

    
1917
        if (prefs_common.enable_smooth_scroll) {
1918
                textview_smooth_scroll_one_line(textview, up);
1919
                return;
1920
        }
1921

    
1922
        if (!up) {
1923
                upper = vadj->upper - vadj->page_size;
1924
                if (vadj->value < upper) {
1925
                        vadj->value += vadj->step_increment;
1926
                        vadj->value = MIN(vadj->value, upper);
1927
                        g_signal_emit_by_name(G_OBJECT(vadj),
1928
                                              "value_changed", 0);
1929
                }
1930
        } else {
1931
                if (vadj->value > 0.0) {
1932
                        vadj->value -= vadj->step_increment;
1933
                        vadj->value = MAX(vadj->value, 0.0);
1934
                        g_signal_emit_by_name(G_OBJECT(vadj),
1935
                                              "value_changed", 0);
1936
                }
1937
        }
1938
}
1939

    
1940
gboolean textview_scroll_page(TextView *textview, gboolean up)
1941
{
1942
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1943
        GtkAdjustment *vadj = text->vadjustment;
1944
        gfloat upper;
1945
        gfloat page_incr;
1946

    
1947
        if (prefs_common.enable_smooth_scroll)
1948
                return textview_smooth_scroll_page(textview, up);
1949

    
1950
        if (prefs_common.scroll_halfpage)
1951
                page_incr = vadj->page_increment / 2;
1952
        else
1953
                page_incr = vadj->page_increment;
1954

    
1955
        if (!up) {
1956
                upper = vadj->upper - vadj->page_size;
1957
                if (vadj->value < upper) {
1958
                        vadj->value += page_incr;
1959
                        vadj->value = MIN(vadj->value, upper);
1960
                        g_signal_emit_by_name(G_OBJECT(vadj),
1961
                                              "value_changed", 0);
1962
                } else
1963
                        return FALSE;
1964
        } else {
1965
                if (vadj->value > 0.0) {
1966
                        vadj->value -= page_incr;
1967
                        vadj->value = MAX(vadj->value, 0.0);
1968
                        g_signal_emit_by_name(G_OBJECT(vadj),
1969
                                              "value_changed", 0);
1970
                } else
1971
                        return FALSE;
1972
        }
1973

    
1974
        return TRUE;
1975
}
1976

    
1977
static void textview_smooth_scroll_do(TextView *textview,
1978
                                      gfloat old_value, gfloat last_value,
1979
                                      gint step)
1980
{
1981
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1982
        GtkAdjustment *vadj = text->vadjustment;
1983
        gint change_value;
1984
        gboolean up;
1985
        gint i;
1986

    
1987
        if (old_value < last_value) {
1988
                change_value = last_value - old_value;
1989
                up = FALSE;
1990
        } else {
1991
                change_value = old_value - last_value;
1992
                up = TRUE;
1993
        }
1994

    
1995
        /* gdk_key_repeat_disable(); */
1996

    
1997
        g_signal_handlers_block_by_func(vadj, textview_adj_value_changed,
1998
                                        textview);
1999

    
2000
        for (i = step; i <= change_value; i += step) {
2001
                vadj->value = old_value + (up ? -i : i);
2002
                g_signal_emit_by_name(G_OBJECT(vadj), "value_changed", 0);
2003
        }
2004

    
2005
        g_signal_handlers_unblock_by_func(vadj, textview_adj_value_changed,
2006
                                          textview);
2007

    
2008
        vadj->value = last_value;
2009
        g_signal_emit_by_name(G_OBJECT(vadj), "value_changed", 0);
2010

    
2011
        /* gdk_key_repeat_restore(); */
2012

    
2013
        gtk_widget_queue_draw(GTK_WIDGET(text));
2014
}
2015

    
2016
static void textview_smooth_scroll_one_line(TextView *textview, gboolean up)
2017
{
2018
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
2019
        GtkAdjustment *vadj = text->vadjustment;
2020
        gfloat upper;
2021
        gfloat old_value;
2022
        gfloat last_value;
2023

    
2024
        if (!up) {
2025
                upper = vadj->upper - vadj->page_size;
2026
                if (vadj->value < upper) {
2027
                        old_value = vadj->value;
2028
                        last_value = vadj->value + vadj->step_increment;
2029
                        last_value = MIN(last_value, upper);
2030

    
2031
                        textview_smooth_scroll_do(textview, old_value,
2032
                                                  last_value,
2033
                                                  prefs_common.scroll_step);
2034
                }
2035
        } else {
2036
                if (vadj->value > 0.0) {
2037
                        old_value = vadj->value;
2038
                        last_value = vadj->value - vadj->step_increment;
2039
                        last_value = MAX(last_value, 0.0);
2040

    
2041
                        textview_smooth_scroll_do(textview, old_value,
2042
                                                  last_value,
2043
                                                  prefs_common.scroll_step);
2044
                }
2045
        }
2046
}
2047

    
2048
static gboolean textview_smooth_scroll_page(TextView *textview, gboolean up)
2049
{
2050
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
2051
        GtkAdjustment *vadj = text->vadjustment;
2052
        gfloat upper;
2053
        gfloat page_incr;
2054
        gfloat old_value;
2055
        gfloat last_value;
2056

    
2057
        if (prefs_common.scroll_halfpage)
2058
                page_incr = vadj->page_increment / 2;
2059
        else
2060
                page_incr = vadj->page_increment;
2061

    
2062
        if (!up) {
2063
                upper = vadj->upper - vadj->page_size;
2064
                if (vadj->value < upper) {
2065
                        old_value = vadj->value;
2066
                        last_value = vadj->value + page_incr;
2067
                        last_value = MIN(last_value, upper);
2068

    
2069
                        textview_smooth_scroll_do(textview, old_value,
2070
                                                  last_value,
2071
                                                  prefs_common.scroll_step);
2072
                } else
2073
                        return FALSE;
2074
        } else {
2075
                if (vadj->value > 0.0) {
2076
                        old_value = vadj->value;
2077
                        last_value = vadj->value - page_incr;
2078
                        last_value = MAX(last_value, 0.0);
2079

    
2080
                        textview_smooth_scroll_do(textview, old_value,
2081
                                                  last_value,
2082
                                                  prefs_common.scroll_step);
2083
                } else
2084
                        return FALSE;
2085
        }
2086

    
2087
        return TRUE;
2088
}
2089

    
2090
#define KEY_PRESS_EVENT_STOP() \
2091
        g_signal_stop_emission_by_name(G_OBJECT(widget), "key-press-event");
2092

    
2093
static gboolean textview_key_pressed(GtkWidget *widget, GdkEventKey *event,
2094
                                     TextView *textview)
2095
{
2096
        SummaryView *summaryview = NULL;
2097
        MessageView *messageview = textview->messageview;
2098

    
2099
        if (!event) return FALSE;
2100
        if (messageview->mainwin)
2101
                summaryview = messageview->mainwin->summaryview;
2102

    
2103
        switch (event->keyval) {
2104
        case GDK_Tab:
2105
        case GDK_Home:
2106
        case GDK_Left:
2107
        case GDK_Up:
2108
        case GDK_Right:
2109
        case GDK_Down:
2110
        case GDK_Page_Up:
2111
        case GDK_Page_Down:
2112
        case GDK_End:
2113
        case GDK_Control_L:
2114
        case GDK_Control_R:
2115
        case GDK_KP_Tab:
2116
        case GDK_KP_Home:
2117
        case GDK_KP_Left:
2118
        case GDK_KP_Up:
2119
        case GDK_KP_Right:
2120
        case GDK_KP_Down:
2121
        case GDK_KP_Page_Up:
2122
        case GDK_KP_Page_Down:
2123
        case GDK_KP_End:
2124
                break;
2125
        case GDK_space:
2126
        case GDK_KP_Space:
2127
                if (summaryview)
2128
                        summary_pass_key_press_event(summaryview, event);
2129
                else
2130
                        textview_scroll_page
2131
                                (textview,
2132
                                 (event->state &
2133
                                  (GDK_SHIFT_MASK|GDK_MOD1_MASK)) != 0);
2134
                break;
2135
        case GDK_BackSpace:
2136
                textview_scroll_page(textview, TRUE);
2137
                break;
2138
        case GDK_Return:
2139
        case GDK_KP_Enter:
2140
                textview_scroll_one_line
2141
                        (textview, (event->state &
2142
                                    (GDK_SHIFT_MASK|GDK_MOD1_MASK)) != 0);
2143
                break;
2144
        case GDK_Delete:
2145
        case GDK_KP_Delete:
2146
                if (summaryview)
2147
                        summary_pass_key_press_event(summaryview, event);
2148
                break;
2149
        case GDK_Escape:
2150
                if (summaryview && textview == messageview->textview)
2151
                        gtk_widget_grab_focus(summaryview->treeview);
2152
                else if (messageview->type == MVIEW_MIME &&
2153
                         textview == messageview->mimeview->textview)
2154
                        gtk_widget_grab_focus(messageview->mimeview->treeview);
2155
                break;
2156
        case GDK_n:
2157
        case GDK_N:
2158
        case GDK_p:
2159
        case GDK_P:
2160
        case GDK_y:
2161
        case GDK_t:
2162
        case GDK_l:
2163
                if (messageview->type == MVIEW_MIME &&
2164
                    textview == messageview->mimeview->textview) {
2165
                        KEY_PRESS_EVENT_STOP();
2166
                        mimeview_pass_key_press_event(messageview->mimeview,
2167
                                                      event);
2168
                        break;
2169
                }
2170
                /* fall through */
2171
        default:
2172
                if (summaryview &&
2173
                    event->window != messageview->mainwin->window->window) {
2174
                        GdkEventKey tmpev = *event;
2175

    
2176
                        tmpev.window = messageview->mainwin->window->window;
2177
                        KEY_PRESS_EVENT_STOP();
2178
                        gtk_widget_event(messageview->mainwin->window,
2179
                                         (GdkEvent *)&tmpev);
2180
                }
2181
                break;
2182
        }
2183

    
2184
        return FALSE;
2185
}
2186

    
2187
static gboolean textview_get_link_tag_bounds(TextView *textview,
2188
                                             GtkTextIter *iter,
2189
                                             GtkTextIter *start,
2190
                                             GtkTextIter *end)
2191
{
2192
        GSList *tags, *cur;
2193
        gboolean on_link = FALSE;
2194

    
2195
        tags = gtk_text_iter_get_tags(iter);
2196
        *start = *end = *iter;
2197

    
2198
        for (cur = tags; cur != NULL; cur = cur->next) {
2199
                GtkTextTag *tag = cur->data;
2200
                gchar *tag_name;
2201

    
2202
                g_object_get(G_OBJECT(tag), "name", &tag_name, NULL);
2203
                if (tag_name && !strcmp(tag_name, "link")) {
2204
                        if (!gtk_text_iter_begins_tag(start, tag))
2205
                                gtk_text_iter_backward_to_tag_toggle
2206
                                        (start, tag);
2207
                        if (!gtk_text_iter_ends_tag(end, tag))
2208
                                gtk_text_iter_forward_to_tag_toggle(end, tag);
2209
                        on_link = TRUE;
2210
                        g_free(tag_name);
2211
                        break;
2212
                }
2213
                if (tag_name)
2214
                        g_free(tag_name);
2215
        }
2216

    
2217
        if (tags)
2218
                g_slist_free(tags);
2219

    
2220
        return on_link;
2221
}
2222

    
2223
static RemoteURI *textview_get_uri(TextView *textview, GtkTextIter *start,
2224
                                   GtkTextIter *end)
2225
{
2226
        gint start_pos, end_pos;
2227
        GSList *cur;
2228
        RemoteURI *uri = NULL;
2229

    
2230
        start_pos = gtk_text_iter_get_offset(start);
2231
        end_pos = gtk_text_iter_get_offset(end);
2232

    
2233
        for (cur = textview->uri_list; cur != NULL; cur = cur->next) {
2234
                RemoteURI *uri_ = (RemoteURI *)cur->data;
2235

    
2236
                if (start_pos == uri_->start && end_pos == uri_->end) {
2237
                        debug_print("uri found: (%d, %d): %s\n",
2238
                                    start_pos, end_pos, uri_->uri);
2239
                        uri = uri_;
2240
                        break;
2241
                }
2242
        }
2243

    
2244
        return uri;
2245
}
2246

    
2247
static gboolean textview_event_after(GtkWidget *widget, GdkEvent *event,
2248
                                     TextView *textview)
2249
{
2250
        GdkEventButton *bevent = (GdkEventButton *)event;
2251
        GtkTextBuffer *buffer;
2252
        GtkTextIter iter, start, end;
2253
        gint x, y;
2254
        gboolean on_link;
2255
        RemoteURI *uri;
2256

    
2257
        if (event->type != GDK_BUTTON_RELEASE)
2258
                return FALSE;
2259
        if (bevent->button != 1 && bevent->button != 2)
2260
                return FALSE;
2261

    
2262
        /* don't process child widget's event */
2263
        if (gtk_text_view_get_window(GTK_TEXT_VIEW(widget), GTK_TEXT_WINDOW_TEXT) != bevent->window)
2264
                return FALSE;
2265

    
2266
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(widget));
2267

    
2268
        /* don't follow a link if the user has selected something */
2269
        gtk_text_buffer_get_selection_bounds(buffer, &start, &end);
2270
        if (!gtk_text_iter_equal(&start, &end))
2271
                return FALSE;
2272

    
2273
        gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(widget),
2274
                                              GTK_TEXT_WINDOW_TEXT,
2275
                                              bevent->x, bevent->y, &x, &y);
2276
        gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(widget), &iter, x, y);
2277
        on_link = textview_get_link_tag_bounds(textview, &iter, &start, &end);
2278
        if (!on_link)
2279
                return FALSE;
2280

    
2281
        uri = textview_get_uri(textview, &start, &end);
2282
        if (!uri || !uri->uri)
2283
                return FALSE;
2284

    
2285
        if (!g_ascii_strncasecmp(uri->uri, "mailto:", 7)) {
2286
                PrefsAccount *ac = NULL;
2287
                MsgInfo *msginfo = textview->messageview->msginfo;
2288

    
2289
                if (msginfo && msginfo->folder)
2290
                        ac = account_find_from_item(msginfo->folder);
2291
                if (ac && ac->protocol == A_NNTP)
2292
                        ac = NULL;
2293
                compose_new(ac, msginfo->folder, uri->uri + 7, NULL);
2294
        } else if (uri->uri[0] == '#') {
2295
                /* don't open in-page link */
2296
        } else if (textview_uri_security_check(textview, uri) == TRUE)
2297
                open_uri(uri->uri, prefs_common.uri_cmd);
2298

    
2299
        return FALSE;
2300
}
2301

    
2302
static void textview_show_uri(TextView *textview, GtkTextIter *start,
2303
                              GtkTextIter *end)
2304
{
2305
        RemoteURI *uri;
2306

    
2307
        STATUSBAR_POP(textview);
2308
        uri = textview_get_uri(textview, start, end);
2309
        if (uri) {
2310
                STATUSBAR_PUSH(textview, uri->uri);
2311
        }
2312
}
2313

    
2314
static void textview_set_cursor(TextView *textview, GtkTextView *text,
2315
                                gint x, gint y)
2316
{
2317
        GtkTextBuffer *buffer;
2318
        GtkTextIter iter;
2319
        GtkTextIter start, end;
2320
        GtkTextMark *start_mark, *end_mark;
2321
        gboolean on_link = FALSE;
2322

    
2323
        buffer = gtk_text_view_get_buffer(text);
2324
        gtk_text_view_get_iter_at_location(text, &iter, x, y);
2325
        on_link = textview_get_link_tag_bounds(textview, &iter, &start, &end);
2326

    
2327
        start_mark = gtk_text_buffer_get_mark(buffer, "hover-link-start");
2328
        end_mark = gtk_text_buffer_get_mark(buffer, "hover-link-end");
2329
        if (start_mark) {
2330
                GtkTextIter prev_start, prev_end;
2331

    
2332
                gtk_text_buffer_get_iter_at_mark(buffer, &prev_start,
2333
                                                 start_mark);
2334
                gtk_text_buffer_get_iter_at_mark(buffer, &prev_end, end_mark);
2335
                if (on_link) {
2336
                        if (gtk_text_iter_equal(&prev_start, &start))
2337
                                return;
2338
                }
2339

    
2340
                gtk_text_buffer_get_iter_at_mark(buffer, &prev_start,
2341
                                                 start_mark);
2342
                gtk_text_buffer_get_iter_at_mark(buffer, &prev_end, end_mark);
2343
                gtk_text_buffer_remove_tag_by_name(buffer, "hover-link",
2344
                                                   &prev_start, &prev_end);
2345
                gtk_text_buffer_delete_mark(buffer, start_mark);
2346
                gtk_text_buffer_delete_mark(buffer, end_mark);
2347
        } else {
2348
                if (!on_link)
2349
                        return;
2350
        }
2351

    
2352
        if (on_link) {
2353
                gtk_text_buffer_create_mark
2354
                        (buffer, "hover-link-start", &start, FALSE);
2355
                gtk_text_buffer_create_mark
2356
                        (buffer, "hover-link-end", &end, FALSE);
2357
                gtk_text_buffer_apply_tag_by_name
2358
                        (buffer, "hover-link", &start, &end);
2359
                gdk_window_set_cursor
2360
                        (gtk_text_view_get_window(text, GTK_TEXT_WINDOW_TEXT),
2361
                         hand_cursor);
2362
                textview_show_uri(textview, &start, &end);
2363
        } else {
2364
                gdk_window_set_cursor
2365
                        (gtk_text_view_get_window(text, GTK_TEXT_WINDOW_TEXT),
2366
                         regular_cursor);
2367
                STATUSBAR_POP(textview);
2368
        }
2369
}
2370

    
2371
static gboolean textview_motion_notify(GtkWidget *widget,
2372
                                       GdkEventMotion *event,
2373
                                       TextView *textview)
2374
{
2375
        gint x, y;
2376
 
2377
        if (gtk_text_view_get_window(GTK_TEXT_VIEW(widget), GTK_TEXT_WINDOW_TEXT) != event->window)
2378
                return FALSE;
2379
        gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(widget),
2380
                                              GTK_TEXT_WINDOW_WIDGET,
2381
                                              event->x, event->y, &x, &y);
2382
        textview_set_cursor(textview, GTK_TEXT_VIEW(widget), x, y);
2383
        gdk_window_get_pointer(widget->window, NULL, NULL, NULL);
2384

    
2385
        return FALSE;
2386
}
2387

    
2388
static gboolean textview_leave_notify(GtkWidget *widget,
2389
                                      GdkEventCrossing *event,
2390
                                      TextView *textview)
2391
{
2392
        textview_set_cursor(textview, GTK_TEXT_VIEW(widget), 0, 0);
2393

    
2394
        return FALSE;
2395
}
2396

    
2397
static gboolean textview_visibility_notify(GtkWidget *widget,
2398
                                           GdkEventVisibility *event,
2399
                                           TextView *textview)
2400
{
2401
        gint wx, wy, bx, by;
2402
        GdkWindow *window;
2403

    
2404
        window = gtk_text_view_get_window(GTK_TEXT_VIEW(widget),
2405
                                          GTK_TEXT_WINDOW_TEXT);
2406

    
2407
        /* check if the event occurred for the text window part */
2408
        if (window != event->window)
2409
                return FALSE;
2410

    
2411
        gdk_window_get_pointer(widget->window, &wx, &wy, NULL);
2412
        gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(widget),
2413
                                              GTK_TEXT_WINDOW_WIDGET,
2414
                                              wx, wy, &bx, &by);
2415
        textview_set_cursor(textview, GTK_TEXT_VIEW(widget), bx, by);
2416

    
2417
        return FALSE;
2418
}
2419

    
2420
#define ADD_MENU(label, func)                                                \
2421
{                                                                        \
2422
        menuitem = gtk_menu_item_new_with_mnemonic(label);                \
2423
        g_signal_connect(menuitem, "activate", G_CALLBACK(func), uri);        \
2424
        gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);                \
2425
        g_object_set_data(G_OBJECT(menuitem), "textview", textview);        \
2426
        gtk_widget_show(menuitem);                                        \
2427
}
2428

    
2429
#define ADD_MENU_SEPARATOR()                                                \
2430
{                                                                        \
2431
        menuitem = gtk_menu_item_new();                                        \
2432
        gtk_widget_set_sensitive(menuitem, FALSE);                        \
2433
        gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);                \
2434
        gtk_widget_show(menuitem);                                        \
2435
}
2436

    
2437
static void textview_populate_popup(GtkWidget *widget, GtkMenu *menu,
2438
                                    TextView *textview)
2439
{
2440
        gint px, py, x, y;
2441
        GtkWidget *separator, *menuitem;
2442
        GtkTextBuffer *buffer;
2443
        GtkTextIter iter, start, end;
2444
        gboolean on_link;
2445
        RemoteURI *uri;
2446
        GdkPixbuf *pixbuf;
2447
        gchar *selected_text;
2448

    
2449
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(widget));
2450

    
2451
        gdk_window_get_pointer(widget->window, &px, &py, NULL);
2452
        gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(widget),
2453
                                              GTK_TEXT_WINDOW_WIDGET,
2454
                                              px, py, &x, &y);
2455
        gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(widget), &iter, x, y);
2456
        if ((pixbuf = gtk_text_iter_get_pixbuf(&iter)) != NULL) {
2457
                start = end = iter;
2458
                gtk_text_iter_forward_char(&end);
2459
                uri = textview_get_uri(textview, &start, &end);
2460

    
2461
                separator = gtk_separator_menu_item_new();
2462
                gtk_menu_shell_append(GTK_MENU_SHELL(menu), separator);
2463
                gtk_widget_show(separator);
2464

    
2465
                ADD_MENU(_("Sa_ve this image as..."),
2466
                         textview_popup_menu_activate_image_cb);
2467
        }
2468

    
2469
        selected_text = gtkut_text_view_get_selection(GTK_TEXT_VIEW(widget));
2470

    
2471
        uri = NULL;
2472
        on_link = textview_get_link_tag_bounds(textview, &iter, &start, &end);
2473
        if (!on_link)
2474
                goto finish;
2475

    
2476
        uri = textview_get_uri(textview, &start, &end);
2477
        if (!uri || !uri->uri)
2478
                goto finish;
2479

    
2480
        separator = gtk_separator_menu_item_new();
2481
        gtk_menu_shell_append(GTK_MENU_SHELL(menu), separator);
2482
        gtk_widget_show(separator);
2483

    
2484
        if (!g_ascii_strncasecmp(uri->uri, "mailto:", 7)) {
2485
                ADD_MENU(_("Compose _new message"),
2486
                         textview_popup_menu_activate_open_uri_cb);
2487
                ADD_MENU(_("R_eply to this address"),
2488
                         textview_popup_menu_activate_reply_cb);
2489
                ADD_MENU_SEPARATOR();
2490
                ADD_MENU(_("Add to address _book..."),
2491
                         textview_popup_menu_activate_add_address_cb);
2492
                ADD_MENU(_("Copy this add_ress"),
2493
                         textview_popup_menu_activate_copy_cb);
2494
        } else {
2495
                ADD_MENU(_("_Open with Web browser"),
2496
                         textview_popup_menu_activate_open_uri_cb);
2497
                ADD_MENU(_("Copy this _link"),
2498
                         textview_popup_menu_activate_copy_cb);
2499
        }
2500

    
2501
finish:
2502
        syl_plugin_signal_emit("textview-menu-popup", menu,
2503
                               GTK_TEXT_VIEW(widget),
2504
                               uri ? uri->uri : NULL,
2505
                               selected_text,
2506
                               textview->messageview->msginfo);
2507
        g_free(selected_text);
2508
}
2509

    
2510
static void textview_popup_menu_activate_open_uri_cb(GtkMenuItem *menuitem,
2511
                                                     gpointer data)
2512
{
2513
        RemoteURI *uri = (RemoteURI *)data;
2514
        TextView *textview;
2515

    
2516
        g_return_if_fail(uri != NULL);
2517

    
2518
        if (!uri->uri)
2519
                return;
2520

    
2521
        textview = g_object_get_data(G_OBJECT(menuitem), "textview");
2522
        g_return_if_fail(textview != NULL);
2523

    
2524
        if (!g_ascii_strncasecmp(uri->uri, "mailto:", 7)) {
2525
                PrefsAccount *ac = NULL;
2526
                MsgInfo *msginfo = textview->messageview->msginfo;
2527

    
2528
                if (msginfo && msginfo->folder)
2529
                        ac = account_find_from_item(msginfo->folder);
2530
                if (ac && ac->protocol == A_NNTP)
2531
                        ac = NULL;
2532
                compose_new(ac, msginfo->folder, uri->uri + 7, NULL);
2533
        } else if (uri->uri[0] == '#') {
2534
                /* don't open in-page link */
2535
        } else if (textview_uri_security_check(textview, uri) == TRUE)
2536
                open_uri(uri->uri, prefs_common.uri_cmd);
2537
}
2538

    
2539
static void textview_popup_menu_activate_reply_cb(GtkMenuItem *menuitem,
2540
                                                  gpointer data)
2541
{
2542
        RemoteURI *uri = (RemoteURI *)data;
2543
        TextView *textview;
2544

    
2545
        g_return_if_fail(uri != NULL);
2546

    
2547
        if (!uri->uri)
2548
                return;
2549

    
2550
        textview = g_object_get_data(G_OBJECT(menuitem), "textview");
2551
        g_return_if_fail(textview != NULL);
2552

    
2553
        if (!g_ascii_strncasecmp(uri->uri, "mailto:", 7)) {
2554
                MsgInfo *msginfo = textview->messageview->msginfo;
2555
                ComposeMode mode = COMPOSE_REPLY;
2556
                gchar *text;
2557
                GList *list;
2558
                Compose *compose;
2559

    
2560
                g_return_if_fail(msginfo != NULL);
2561

    
2562
                if (prefs_common.reply_with_quote)
2563
                        mode |= COMPOSE_WITH_QUOTE;
2564

    
2565
                text = gtkut_text_view_get_selection
2566
                        (GTK_TEXT_VIEW(textview->text));
2567
                if (text && *text == '\0') {
2568
                        g_free(text);
2569
                        text = NULL;
2570
                }
2571

    
2572
                compose_reply(msginfo, msginfo->folder, mode, text);
2573
                list = compose_get_compose_list();
2574
                list = g_list_last(list);
2575
                if (list && list->data) {
2576
                        compose = (Compose *)list->data;
2577
                        compose_block_modified(compose);
2578
                        compose_entry_set(compose, uri->uri + 7,
2579
                                          COMPOSE_ENTRY_TO);
2580
                        compose_unblock_modified(compose);
2581
                }
2582
                g_free(text);
2583
        }
2584
}
2585

    
2586
static void textview_popup_menu_activate_add_address_cb(GtkMenuItem *menuitem,
2587
                                                        gpointer data)
2588
{
2589
        RemoteURI *uri = (RemoteURI *)data;
2590
        gchar *addr;
2591

    
2592
        g_return_if_fail(uri != NULL);
2593

    
2594
        if (!uri->uri)
2595
                return;
2596

    
2597
        if (!g_ascii_strncasecmp(uri->uri, "mailto:", 7)) {
2598
                addr = g_malloc(strlen(uri->uri + 7) + 1);
2599
                decode_uri(addr, uri->uri + 7);
2600
        } else
2601
                addr = g_strdup(uri->uri);
2602

    
2603
        addressbook_add_contact(addr, addr, NULL);
2604

    
2605
        g_free(addr);
2606
}
2607

    
2608
static void textview_popup_menu_activate_copy_cb(GtkMenuItem *menuitem,
2609
                                                 gpointer data)
2610
{
2611
        RemoteURI *uri = (RemoteURI *)data;
2612
        gchar *uri_string;
2613
        GtkClipboard *clipboard;
2614

    
2615
        g_return_if_fail(uri != NULL);
2616

    
2617
        if (!uri->uri)
2618
                return;
2619

    
2620
        if (!g_ascii_strncasecmp(uri->uri, "mailto:", 7)) {
2621
                uri_string = g_malloc(strlen(uri->uri + 7) + 1);
2622
                decode_uri(uri_string, uri->uri + 7);
2623
        } else
2624
                uri_string = g_strdup(uri->uri);
2625

    
2626
        clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
2627
        gtk_clipboard_set_text(clipboard, uri_string, -1);
2628
        clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
2629
        gtk_clipboard_set_text(clipboard, uri_string, -1);
2630

    
2631
        g_free(uri_string);
2632
}
2633

    
2634
static void textview_popup_menu_activate_image_cb(GtkMenuItem *menuitem,
2635
                                                  gpointer data)
2636
{
2637
        RemoteURI *uri = (RemoteURI *)data;
2638
        gchar *src;
2639
        gchar *dest;
2640
        gchar *filename;
2641

    
2642
        g_return_if_fail(uri != NULL);
2643

    
2644
        if (!uri->uri)
2645
                return;
2646

    
2647
        src = g_filename_from_uri(uri->uri, NULL, NULL);
2648
        g_return_if_fail(src != NULL);
2649

    
2650
        filename = conv_filename_to_utf8(uri->filename);
2651
        dest = filesel_save_as(filename);
2652
        if (dest) {
2653
                copy_file(src, dest, FALSE);
2654
                g_free(dest);
2655
        }
2656
        g_free(filename);
2657
        g_free(src);
2658
}
2659

    
2660
static void textview_adj_value_changed(GtkAdjustment *adj, gpointer data)
2661
{
2662
        TextView *textview = (TextView *)data;
2663
        GtkTextBuffer *buffer;
2664

    
2665
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
2666
        if (gtk_text_buffer_get_selection_bounds(buffer, NULL, NULL))
2667
                return;
2668
        gtk_text_view_place_cursor_onscreen(GTK_TEXT_VIEW(textview->text));
2669
}
2670

    
2671
static gboolean textview_uri_security_check(TextView *textview, RemoteURI *uri)
2672
{
2673
        GtkTextBuffer *buffer;
2674
        GtkTextIter start_iter, end_iter;
2675
        gchar *visible_str;
2676
        gboolean retval = TRUE;
2677

    
2678
        if (is_uri_string(uri->uri) == FALSE)
2679
                return TRUE;
2680

    
2681
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
2682
        gtk_text_buffer_get_iter_at_offset(buffer, &start_iter, uri->start);
2683
        gtk_text_buffer_get_iter_at_offset(buffer, &end_iter, uri->end);
2684
        visible_str = gtk_text_buffer_get_text(buffer, &start_iter, &end_iter,
2685
                                               FALSE);
2686
        if (visible_str == NULL)
2687
                return TRUE;
2688

    
2689
        if (strcmp(visible_str, uri->uri) != 0 && is_uri_string(visible_str)) {
2690
                gchar *uri_path;
2691
                gchar *visible_uri_path;
2692

    
2693
                uri_path = get_uri_path(uri->uri);
2694
                visible_uri_path = get_uri_path(visible_str);
2695
                if (path_cmp(uri_path, visible_uri_path) != 0)
2696
                        retval = FALSE;
2697
        }
2698

    
2699
        if (retval == FALSE) {
2700
                gchar *msg;
2701
                AlertValue aval;
2702

    
2703
                msg = g_strdup_printf(_("The real URL (%s) is different from\n"
2704
                                        "the apparent URL (%s).\n"
2705
                                        "\n"
2706
                                        "Open it anyway?"),
2707
                                      uri->uri, visible_str);
2708
                aval = alertpanel_full(_("Fake URL warning"), msg,
2709
                                       ALERT_WARNING, G_ALERTDEFAULT, FALSE,
2710
                                       GTK_STOCK_YES, GTK_STOCK_NO, NULL);
2711
                g_free(msg);
2712
                if (aval == G_ALERTDEFAULT)
2713
                        retval = TRUE;
2714
        }
2715

    
2716
        g_free(visible_str);
2717

    
2718
        return retval;
2719
}
2720

    
2721
static void textview_uri_list_remove_all(GSList *uri_list)
2722
{
2723
        GSList *cur;
2724

    
2725
        for (cur = uri_list; cur != NULL; cur = cur->next) {
2726
                RemoteURI *uri = (RemoteURI *)cur->data;
2727

    
2728
                if (uri) {
2729
                        g_free(uri->uri);
2730
                        g_free(uri->filename);
2731
                        g_free(uri);
2732
                }
2733
        }
2734

    
2735
        g_slist_free(uri_list);
2736
}
2737

    
2738
static void textview_uri_list_update_offsets(TextView *textview, gint start, gint add)
2739
{
2740
        GSList *cur;
2741

    
2742
        debug_print("textview_uri_list_update_offsets: from %d: add %d\n", start, add);
2743

    
2744
        for (cur = textview->uri_list; cur != NULL; cur = cur->next) {
2745
                RemoteURI *uri = (RemoteURI *)cur->data;
2746

    
2747
                if (uri->start >= start) {
2748
                        debug_print("textview_uri_list_update_offsets: update: (%d, %d) -> (%d, %d)\n", uri->start, uri->end, uri->start + add, uri->end + add);
2749
                        uri->start += add;
2750
                        uri->end += add;
2751
                }
2752
        }
2753
}