Statistics
| Revision:

root / src / textview.c @ 3177

History | View | Annotate | Download (73.2 KB)

1
/*
2
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3
 * Copyright (C) 1999-2012 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->main &&
1080
                    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
                textview_add_part(textview, mimeinfo, fp);
1146
                if (mimeinfo->parent && mimeinfo->parent->content_type &&
1147
                    !g_ascii_strcasecmp(mimeinfo->parent->content_type,
1148
                                        "multipart/alternative"))
1149
                        mimeinfo = mimeinfo->parent->next;
1150
                else
1151
                        mimeinfo = procmime_mimeinfo_next(mimeinfo);
1152
                if (!mimeinfo || mimeinfo->level <= level)
1153
                        break;
1154
        }
1155
}
1156

    
1157
static void textview_write_error(TextView *textview, const gchar *msg)
1158
{
1159
        GtkTextBuffer *buffer;
1160
        GtkTextIter iter;
1161

    
1162
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
1163
        gtk_text_buffer_get_end_iter(buffer, &iter);
1164
        gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, msg, -1,
1165
                                                 "error", NULL);
1166
}
1167

    
1168
void textview_show_error(TextView *textview)
1169
{
1170
        textview_set_font(textview, NULL);
1171
        textview_clear(textview);
1172
        textview_write_error(textview, _("This message can't be displayed.\n"));
1173
}
1174

    
1175
static void textview_write_body(TextView *textview, MimeInfo *mimeinfo,
1176
                                FILE *fp, const gchar *charset)
1177
{
1178
        FILE *tmpfp;
1179
        gchar buf[BUFFSIZE];
1180
        CodeConverter *conv;
1181

    
1182
        conv = conv_code_converter_new(charset, NULL);
1183

    
1184
        tmpfp = procmime_decode_content(NULL, fp, mimeinfo);
1185
        if (tmpfp) {
1186
                if (mimeinfo->mime_type == MIME_TEXT_HTML &&
1187
                    prefs_common.render_html)
1188
                        textview_show_html(textview, tmpfp, conv);
1189
                else
1190
                        while (fgets(buf, sizeof(buf), tmpfp) != NULL)
1191
                                textview_write_line(textview, buf, conv);
1192
                fclose(tmpfp);
1193
        } else {
1194
                textview_write_error
1195
                        (textview,
1196
                         _("The body text couldn't be displayed because "
1197
                           "writing to temporary file failed.\n"));
1198
        }
1199

    
1200
        conv_code_converter_destroy(conv);
1201
}
1202

    
1203
static void textview_show_html(TextView *textview, FILE *fp,
1204
                               CodeConverter *conv)
1205
{
1206
        HTMLParser *parser;
1207
        const gchar *str;
1208

    
1209
        parser = html_parser_new(fp, conv);
1210
        g_return_if_fail(parser != NULL);
1211

    
1212
        while ((str = html_parse(parser)) != NULL) {
1213
                if (parser->href != NULL)
1214
                        textview_write_link(textview, str, parser->href, NULL);
1215
                else
1216
                        textview_write_line(textview, str, NULL);
1217
        }
1218
        textview_write_line(textview, "\n", NULL);
1219

    
1220
        html_parser_destroy(parser);
1221
}
1222

    
1223
/* get_uri_part() - retrieves a URI starting from scanpos.
1224
                    Returns TRUE if succesful */
1225
static gboolean get_uri_part(const gchar *start, const gchar *scanpos,
1226
                             const gchar **bp, const gchar **ep)
1227
{
1228
        const gchar *ep_;
1229

    
1230
        g_return_val_if_fail(start != NULL, FALSE);
1231
        g_return_val_if_fail(scanpos != NULL, FALSE);
1232
        g_return_val_if_fail(bp != NULL, FALSE);
1233
        g_return_val_if_fail(ep != NULL, FALSE);
1234

    
1235
        *bp = scanpos;
1236

    
1237
        /* find end point of URI */
1238
        for (ep_ = scanpos; *ep_ != '\0'; ep_++) {
1239
                if (!g_ascii_isgraph(*ep_) ||
1240
                    !isascii(*(const guchar *)ep_) ||
1241
                    strchr("()<>{}[]\"", *ep_))
1242
                        break;
1243
        }
1244

    
1245
        /* no punctuation at end of string */
1246

    
1247
        /* FIXME: this stripping of trailing punctuations may bite with other URIs.
1248
         * should pass some URI type to this function and decide on that whether
1249
         * to perform punctuation stripping */
1250

    
1251
#define IS_REAL_PUNCT(ch)        (g_ascii_ispunct(ch) && !strchr("/?=", ch)) 
1252

    
1253
        for (; ep_ - 1 > scanpos + 1 && IS_REAL_PUNCT(*(ep_ - 1)); ep_--)
1254
                ;
1255

    
1256
#undef IS_REAL_PUNCT
1257

    
1258
        *ep = ep_;
1259

    
1260
        return TRUE;                
1261
}
1262

    
1263
static gchar *make_uri_string(const gchar *bp, const gchar *ep)
1264
{
1265
        return g_strndup(bp, ep - bp);
1266
}
1267

    
1268
static gchar *make_http_uri_string(const gchar *bp, const gchar *ep)
1269
{
1270
        gchar *tmp;
1271
        gchar *result;
1272

    
1273
        tmp = g_strndup(bp, ep - bp);
1274
        result = g_strconcat("http://", tmp, NULL);
1275
        g_free(tmp);
1276

    
1277
        return result;
1278
}
1279

    
1280
/* valid mail address characters */
1281
#define IS_RFC822_CHAR(ch) \
1282
        (isascii(ch) && \
1283
         (ch) > 32   && \
1284
         (ch) != 127 && \
1285
         !g_ascii_isspace(ch) && \
1286
         !strchr("(),;<>\"", (ch)))
1287

    
1288
/* alphabet and number within 7bit ASCII */
1289
#define IS_ASCII_ALNUM(ch)        (isascii(ch) && g_ascii_isalnum(ch))
1290

    
1291
/* get_email_part() - retrieves an email address. Returns TRUE if succesful */
1292
static gboolean get_email_part(const gchar *start, const gchar *scanpos,
1293
                               const gchar **bp, const gchar **ep)
1294
{
1295
        /* more complex than the uri part because we need to scan back and forward starting from
1296
         * the scan position. */
1297
        gboolean result = FALSE;
1298
        const gchar *bp_;
1299
        const gchar *ep_;
1300

    
1301
        g_return_val_if_fail(start != NULL, FALSE);
1302
        g_return_val_if_fail(scanpos != NULL, FALSE);
1303
        g_return_val_if_fail(bp != NULL, FALSE);
1304
        g_return_val_if_fail(ep != NULL, FALSE);
1305

    
1306
        /* scan start of address */
1307
        for (bp_ = scanpos - 1;
1308
             bp_ >= start && IS_RFC822_CHAR(*(const guchar *)bp_); bp_--)
1309
                ;
1310

    
1311
        /* TODO: should start with an alnum? */
1312
        bp_++;
1313
        for (; bp_ < scanpos && !IS_ASCII_ALNUM(*(const guchar *)bp_); bp_++)
1314
                ;
1315

    
1316
        if (bp_ != scanpos) {
1317
                /* scan end of address */
1318
                for (ep_ = scanpos + 1;
1319
                     *ep_ && IS_RFC822_CHAR(*(const guchar *)ep_); ep_++)
1320
                        ;
1321

    
1322
                /* TODO: really should terminate with an alnum? */
1323
                for (; ep_ > scanpos && !IS_ASCII_ALNUM(*(const guchar *)ep_);
1324
                     --ep_)
1325
                        ;
1326
                ep_++;
1327

    
1328
                if (ep_ > scanpos + 1) {
1329
                        *ep = ep_;
1330
                        *bp = bp_;
1331
                        result = TRUE;
1332
                }
1333
        }
1334

    
1335
        return result;
1336
}
1337

    
1338
#undef IS_ASCII_ALNUM
1339
#undef IS_RFC822_CHAR
1340

    
1341
static gchar *make_email_string(const gchar *bp, const gchar *ep)
1342
{
1343
        /* returns a mailto: URI; mailto: is also used to detect the
1344
         * uri type later on in the button_pressed signal handler */
1345
        gchar *tmp, *enc;
1346
        gchar *result;
1347

    
1348
        tmp = g_strndup(bp, ep - bp);
1349
        enc = uriencode_for_mailto(tmp);
1350
        result = g_strconcat("mailto:", enc, NULL);
1351
        g_free(enc);
1352
        g_free(tmp);
1353

    
1354
        return result;
1355
}
1356

    
1357
#define ADD_TXT_POS(bp_, ep_, pti_) \
1358
{ \
1359
        struct txtpos *last; \
1360
 \
1361
        last = g_new(struct txtpos, 1); \
1362
        last->bp = (bp_); \
1363
        last->ep = (ep_); \
1364
        last->pti = (pti_); \
1365
        txtpos_list = g_slist_append(txtpos_list, last); \
1366
}
1367

    
1368
/* textview_make_clickable_parts() - colorizes clickable parts */
1369
static void textview_make_clickable_parts(TextView *textview,
1370
                                          const gchar *fg_tag,
1371
                                          const gchar *uri_tag,
1372
                                          const gchar *linebuf)
1373
{
1374
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1375
        GtkTextBuffer *buffer;
1376
        GtkTextIter iter;
1377

    
1378
        /* parse table - in order of priority */
1379
        struct table {
1380
                const gchar *needle; /* token */
1381

    
1382
                /* token search function */
1383
                gchar    *(*search)        (const gchar *haystack,
1384
                                         const gchar *needle);
1385
                /* part parsing function */
1386
                gboolean  (*parse)        (const gchar *start,
1387
                                         const gchar *scanpos,
1388
                                         const gchar **bp_,
1389
                                         const gchar **ep_);
1390
                /* part to URI function */
1391
                gchar    *(*build_uri)        (const gchar *bp,
1392
                                         const gchar *ep);
1393
        };
1394

    
1395
        static struct table parser[] = {
1396
                {"http://",  strcasestr, get_uri_part,   make_uri_string},
1397
                {"https://", strcasestr, get_uri_part,   make_uri_string},
1398
                {"ftp://",   strcasestr, get_uri_part,   make_uri_string},
1399
                {"www.",     strcasestr, get_uri_part,   make_http_uri_string},
1400
                {"mailto:",  strcasestr, get_uri_part,   make_uri_string},
1401
                {"@",        strcasestr, get_email_part, make_email_string}
1402
        };
1403
        const gint PARSE_ELEMS = sizeof parser / sizeof parser[0];
1404

    
1405
        /* flags for search optimization */
1406
        gboolean do_search[] = {TRUE, TRUE, TRUE, TRUE, TRUE, TRUE};
1407

    
1408
        gint  n;
1409
        const gchar *walk, *bp, *ep;
1410

    
1411
        struct txtpos {
1412
                const gchar        *bp, *ep;        /* text position */
1413
                gint                 pti;                /* index in parse table */
1414
        };
1415
        GSList *txtpos_list = NULL;
1416

    
1417
        buffer = gtk_text_view_get_buffer(text);
1418
        gtk_text_buffer_get_end_iter(buffer, &iter);
1419

    
1420
        /* parse for clickable parts, and build a list of begin and
1421
           end positions  */
1422
        for (walk = linebuf, n = 0;;) {
1423
                gint last_index = PARSE_ELEMS;
1424
                const gchar *scanpos = NULL;
1425

    
1426
                /* FIXME: this looks phony. scanning for anything in the
1427
                   parse table */
1428
                for (n = 0; n < PARSE_ELEMS; n++) {
1429
                        const gchar *tmp;
1430

    
1431
                        if (do_search[n]) {
1432
                                tmp = parser[n].search(walk, parser[n].needle);
1433
                                if (tmp) {
1434
                                        if (scanpos == NULL || tmp < scanpos) {
1435
                                                scanpos = tmp;
1436
                                                last_index = n;
1437
                                        }
1438
                                } else
1439
                                        do_search[n] = FALSE;
1440
                        }
1441
                }
1442

    
1443
                if (scanpos) {
1444
                        /* check if URI can be parsed */
1445
                        if (parser[last_index].parse(walk, scanpos, &bp, &ep)
1446
                            && (ep - bp - 1) > strlen(parser[last_index].needle)) {
1447
                                        ADD_TXT_POS(bp, ep, last_index);
1448
                                        walk = ep;
1449
                        } else
1450
                                walk = scanpos +
1451
                                        strlen(parser[last_index].needle);
1452
                } else
1453
                        break;
1454
        }
1455

    
1456
        /* colorize this line */
1457
        if (txtpos_list) {
1458
                const gchar *normal_text = linebuf;
1459
                GSList *cur;
1460

    
1461
                /* insert URIs */
1462
                for (cur = txtpos_list; cur != NULL; cur = cur->next) {
1463
                        struct txtpos *pos = (struct txtpos *)cur->data;
1464
                        RemoteURI *uri;
1465

    
1466
                        uri = g_new(RemoteURI, 1);
1467
                        if (pos->bp - normal_text > 0)
1468
                                gtk_text_buffer_insert_with_tags_by_name
1469
                                        (buffer, &iter,
1470
                                         normal_text,
1471
                                         pos->bp - normal_text,
1472
                                         fg_tag, NULL);
1473
                        uri->uri = parser[pos->pti].build_uri(pos->bp, pos->ep);
1474
                        uri->filename = NULL;
1475
                        uri->start = gtk_text_iter_get_offset(&iter);
1476
                        gtk_text_buffer_insert_with_tags_by_name
1477
                                (buffer, &iter, pos->bp, pos->ep - pos->bp,
1478
                                 uri_tag, fg_tag, NULL);
1479
                        uri->end = gtk_text_iter_get_offset(&iter);
1480
                        textview->uri_list =
1481
                                g_slist_append(textview->uri_list, uri);
1482
                        normal_text = pos->ep;
1483

    
1484
                        g_free(pos);
1485
                }
1486

    
1487
                if (*normal_text)
1488
                        gtkut_text_buffer_insert_with_tag_by_name
1489
                                (buffer, &iter, normal_text, -1, fg_tag);
1490

    
1491
                g_slist_free(txtpos_list);
1492
        } else {
1493
                gtkut_text_buffer_insert_with_tag_by_name
1494
                        (buffer, &iter, linebuf, -1, fg_tag);
1495
        }
1496
}
1497

    
1498
#undef ADD_TXT_POS
1499

    
1500
static void textview_write_line(TextView *textview, const gchar *str,
1501
                                CodeConverter *conv)
1502
{
1503
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1504
        GtkTextBuffer *buffer;
1505
        GtkTextIter iter;
1506
        gchar *buf;
1507
        gchar *fg_color = NULL;
1508
        gint quotelevel = -1;
1509
        gchar quote_tag_str[10];
1510

    
1511
        buffer = gtk_text_view_get_buffer(text);
1512
        gtk_text_buffer_get_end_iter(buffer, &iter);
1513

    
1514
        if (conv) {
1515
                buf = conv_convert(conv, str);
1516
                if (!buf)
1517
                        buf = conv_utf8todisp(str, NULL);
1518
        } else
1519
                buf = g_strdup(str);
1520

    
1521
        strcrchomp(buf);
1522
        //if (prefs_common.conv_mb_alnum) conv_mb_alnum(buf);
1523

    
1524
        /* change color of quotation
1525
           >, foo>, _> ... ok, <foo>, foo bar>, foo-> ... ng
1526
           Up to 3 levels of quotations are detected, and each
1527
           level is colored using a different color. */
1528
        if (prefs_common.enable_color && strchr(buf, '>')) {
1529
                quotelevel = get_quote_level(buf);
1530

    
1531
                /* set up the correct foreground color */
1532
                if (quotelevel > 2) {
1533
                        /* recycle colors */
1534
                        if (prefs_common.recycle_quote_colors)
1535
                                quotelevel %= 3;
1536
                        else
1537
                                quotelevel = 2;
1538
                }
1539
        }
1540

    
1541
        if (quotelevel != -1) {
1542
                g_snprintf(quote_tag_str, sizeof(quote_tag_str),
1543
                           "quote%d", quotelevel);
1544
                fg_color = quote_tag_str;
1545
        }
1546

    
1547
        if (prefs_common.enable_color)
1548
                textview_make_clickable_parts(textview, fg_color, "link", buf);
1549
        else
1550
                textview_make_clickable_parts(textview, fg_color, NULL, buf);
1551

    
1552
        g_free(buf);
1553
}
1554

    
1555
static void textview_write_link(TextView *textview, const gchar *str,
1556
                                const gchar *uri, CodeConverter *conv)
1557
{
1558
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1559
        GtkTextBuffer *buffer;
1560
        GtkTextIter iter;
1561
        gchar *buf;
1562
        gchar *bufp;
1563
        RemoteURI *r_uri;
1564

    
1565
        if (!str || *str == '\0')
1566
                return;
1567
        if (!uri)
1568
                return;
1569

    
1570
        buffer = gtk_text_view_get_buffer(text);
1571
        gtk_text_buffer_get_end_iter(buffer, &iter);
1572

    
1573
        if (conv) {
1574
                buf = conv_convert(conv, str);
1575
                if (!buf)
1576
                        buf = conv_utf8todisp(str, NULL);
1577
        } else
1578
                buf = g_strdup(str);
1579

    
1580
        if (g_utf8_validate(buf, -1, NULL) == FALSE) {
1581
                g_free(buf);
1582
                return;
1583
        }
1584

    
1585
        strcrchomp(buf);
1586

    
1587
        for (bufp = buf; *bufp != '\0'; bufp = g_utf8_next_char(bufp)) {
1588
                gunichar ch;
1589

    
1590
                ch = g_utf8_get_char(bufp);
1591
                if (!g_unichar_isspace(ch))
1592
                        break;
1593
        }
1594
        if (bufp > buf)
1595
                gtk_text_buffer_insert(buffer, &iter, buf, bufp - buf);
1596

    
1597
        r_uri = g_new(RemoteURI, 1);
1598
        r_uri->uri = g_strstrip(g_strdup(uri));
1599
        r_uri->filename = NULL;
1600
        r_uri->start = gtk_text_iter_get_offset(&iter);
1601
        gtk_text_buffer_insert_with_tags_by_name
1602
                (buffer, &iter, bufp, -1, "link", NULL);
1603
        r_uri->end = gtk_text_iter_get_offset(&iter);
1604
        textview->uri_list = g_slist_append(textview->uri_list, r_uri);
1605

    
1606
        g_free(buf);
1607
}
1608

    
1609
void textview_clear(TextView *textview)
1610
{
1611
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1612
        GtkTextBuffer *buffer;
1613

    
1614
        buffer = gtk_text_view_get_buffer(text);
1615
        gtk_text_buffer_set_text(buffer, "", -1);
1616

    
1617
        /* workaround for the assertion failure in
1618
           gtk_text_view_validate_onscreen() */
1619
        text->vadjustment->value = 0.0;
1620

    
1621
        STATUSBAR_POP(textview);
1622
        textview_uri_list_remove_all(textview->uri_list);
1623
        textview->uri_list = NULL;
1624

    
1625
        textview->body_pos = 0;
1626
}
1627

    
1628
void textview_destroy(TextView *textview)
1629
{
1630
        GtkTextBuffer *buffer;
1631
        GtkClipboard *clipboard;
1632

    
1633
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
1634
        clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1635
        gtk_text_buffer_remove_selection_clipboard(buffer, clipboard);
1636

    
1637
        gtk_widget_destroy(textview->popup_menu);
1638

    
1639
        textview_uri_list_remove_all(textview->uri_list);
1640
        textview->uri_list = NULL;
1641

    
1642
        g_free(textview);
1643
}
1644

    
1645
void textview_set_all_headers(TextView *textview, gboolean all_headers)
1646
{
1647
        textview->show_all_headers = all_headers;
1648
}
1649

    
1650
void textview_set_font(TextView *textview, const gchar *codeset)
1651
{
1652
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1653

    
1654
        if (prefs_common.textfont) {
1655
                PangoFontDescription *font_desc;
1656
                font_desc = pango_font_description_from_string
1657
                        (prefs_common.textfont);
1658
                if (font_desc) {
1659
                        gtk_widget_modify_font(textview->text, font_desc);
1660
                        pango_font_description_free(font_desc);
1661
                }
1662
        }
1663

    
1664
        gtk_text_view_set_pixels_above_lines(text, prefs_common.line_space - prefs_common.line_space / 2);
1665
        gtk_text_view_set_pixels_below_lines(text, prefs_common.line_space / 2);
1666
        gtk_text_view_set_pixels_inside_wrap(text, prefs_common.line_space);
1667
}
1668

    
1669
void textview_set_position(TextView *textview, gint pos)
1670
{
1671
        GtkTextBuffer *buffer;
1672
        GtkTextIter iter;
1673

    
1674
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
1675
        gtk_text_buffer_get_iter_at_offset(buffer, &iter, pos);
1676
        gtk_text_buffer_place_cursor(buffer, &iter);
1677
}
1678

    
1679
static GPtrArray *textview_scan_header(TextView *textview, FILE *fp,
1680
                                       const gchar *encoding)
1681
{
1682
        g_return_val_if_fail(fp != NULL, NULL);
1683

    
1684
        if (textview->show_all_headers)
1685
                return procheader_get_header_array_asis(fp, encoding);
1686

    
1687
        if (!prefs_common.display_header) {
1688
                gchar buf[BUFFSIZE];
1689

    
1690
                while (fgets(buf, sizeof(buf), fp) != NULL)
1691
                        if (buf[0] == '\r' || buf[0] == '\n') break;
1692
                return NULL;
1693
        }
1694

    
1695
        return procheader_get_header_array_for_display(fp, encoding);
1696
}
1697
 
1698
static void textview_size_allocate_cb(GtkWidget *widget,
1699
                                      GtkAllocation *allocation, gpointer data)
1700
{
1701
        GtkWidget *child = GTK_WIDGET(data);
1702
        gint new_width;
1703

    
1704
        /* g_print("textview_size_allocate_cb: (%d, %d)\n", allocation->width, allocation->height); */
1705
        new_width = allocation->width - 14;
1706
        if (new_width < -1)
1707
                new_width = -1;
1708
        gtk_widget_set_size_request(child, new_width, 16);
1709
}
1710

    
1711
void textview_hline_destroy_cb(GtkObject *object, gpointer data)
1712
{
1713
        GtkWidget *text = GTK_WIDGET(data);
1714

    
1715
        g_signal_handlers_disconnect_by_func(text, textview_size_allocate_cb,
1716
                                             object);
1717
}
1718

    
1719
static void textview_show_header(TextView *textview, GPtrArray *headers)
1720
{
1721
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1722
        GtkTextBuffer *buffer;
1723
        GtkTextIter iter;
1724
        Header *header;
1725
        gint i;
1726
        GtkTextMark *mark;
1727

    
1728
        g_return_if_fail(headers != NULL);
1729

    
1730
        buffer = gtk_text_view_get_buffer(text);
1731
        gtk_text_buffer_get_end_iter(buffer, &iter);
1732

    
1733
        for (i = 0; i < headers->len; i++) {
1734
                header = g_ptr_array_index(headers, i);
1735
                g_return_if_fail(header->name != NULL);
1736

    
1737
                gtk_text_buffer_insert_with_tags_by_name
1738
                        (buffer, &iter, header->name, -1,
1739
                         "header_title", "header", NULL);
1740
                gtk_text_buffer_insert_with_tags_by_name
1741
                        (buffer, &iter, ":", 1,
1742
                         "header_title", "header", NULL);
1743

    
1744
                if (!g_ascii_strcasecmp(header->name, "Subject") ||
1745
                    !g_ascii_strcasecmp(header->name, "From")    ||
1746
                    !g_ascii_strcasecmp(header->name, "To")      ||
1747
                    !g_ascii_strcasecmp(header->name, "Cc"))
1748
                        unfold_line(header->body);
1749

    
1750
                if (prefs_common.enable_color &&
1751
                    (!strncmp(header->name, "X-Mailer", 8) ||
1752
                     !strncmp(header->name, "X-Newsreader", 12)) &&
1753
                    strstr(header->body, "Sylpheed") != NULL) {
1754
                        gtk_text_buffer_insert_with_tags_by_name
1755
                                (buffer, &iter, header->body, -1,
1756
                                 "header", "emphasis", NULL);
1757
                } else if (prefs_common.enable_color) {
1758
                        textview_make_clickable_parts
1759
                                (textview, "header", "link", header->body);
1760
                } else {
1761
                        textview_make_clickable_parts
1762
                                (textview, "header", NULL, header->body);
1763
                }
1764
                gtk_text_buffer_get_end_iter(buffer, &iter);
1765
                gtk_text_buffer_insert_with_tags_by_name
1766
                        (buffer, &iter, "\n", 1, "header", NULL);
1767
        }
1768

    
1769
        mark = gtk_text_buffer_get_mark(buffer, "attach-file-pos");
1770
        gtk_text_buffer_move_mark(buffer, mark, &iter);
1771
        g_object_set_data(G_OBJECT(mark), "attach-file-count", GINT_TO_POINTER(0));
1772

    
1773
        textview_insert_border(textview, &iter, 0);
1774
}
1775

    
1776
static void textview_insert_border(TextView *textview, GtkTextIter *iter,
1777
                                   gint padding)
1778
{
1779
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1780
        GtkTextBuffer *buffer;
1781
        GtkTextChildAnchor *anchor;
1782
        GtkWidget *vbox;
1783
        GtkWidget *pad;
1784
        GtkWidget *hline;
1785

    
1786
        buffer = gtk_text_view_get_buffer(text);
1787

    
1788
        anchor = gtk_text_buffer_create_child_anchor(buffer, iter);
1789
        vbox = gtk_vbox_new(FALSE, 0);
1790
        if (padding > 0) {
1791
                pad = gtk_vbox_new(FALSE, 0);
1792
                gtk_box_pack_start(GTK_BOX(vbox), pad, FALSE, FALSE, 0);
1793
                gtk_widget_set_size_request(pad, -1, padding);
1794
        }
1795
        hline = gtk_hseparator_new();
1796
        gtk_box_pack_start(GTK_BOX(vbox), hline, FALSE, FALSE, 0);
1797
        gtk_widget_show_all(vbox);
1798
        gtk_widget_set_size_request(hline, 320, 16);
1799
        gtk_text_view_add_child_at_anchor(text, vbox, anchor);
1800
        g_signal_connect(G_OBJECT(hline), "destroy",
1801
                         G_CALLBACK(textview_hline_destroy_cb), text);
1802
        g_signal_connect(G_OBJECT(text), "size-allocate",
1803
                         G_CALLBACK(textview_size_allocate_cb), hline);
1804
}
1805

    
1806
static void textview_insert_pad(TextView *textview, GtkTextIter *iter,
1807
                                gint padding)
1808
{
1809
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1810
        GtkTextBuffer *buffer;
1811
        GtkTextChildAnchor *anchor;
1812
        GtkWidget *vbox;
1813

    
1814
        buffer = gtk_text_view_get_buffer(text);
1815

    
1816
        anchor = gtk_text_buffer_create_child_anchor(buffer, iter);
1817
        vbox = gtk_vbox_new(FALSE, 0);
1818
        gtk_widget_set_size_request(vbox, -1, padding);
1819
        gtk_widget_show_all(vbox);
1820
        gtk_text_view_add_child_at_anchor(text, vbox, anchor);
1821

    
1822
        gtk_text_buffer_insert(buffer, iter, "\n", 1);
1823
}
1824

    
1825
gboolean textview_search_string(TextView *textview, const gchar *str,
1826
                                gboolean case_sens)
1827
{
1828
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1829
        GtkTextBuffer *buffer;
1830
        GtkTextIter iter, match_pos;
1831
        GtkTextMark *mark;
1832
        gint len;
1833

    
1834
        g_return_val_if_fail(str != NULL, FALSE);
1835

    
1836
        buffer = gtk_text_view_get_buffer(text);
1837

    
1838
        len = g_utf8_strlen(str, -1);
1839
        g_return_val_if_fail(len >= 0, FALSE);
1840

    
1841
        gtk_text_buffer_get_selection_bounds(buffer, NULL, &iter);
1842

    
1843
        if (gtkut_text_buffer_find(buffer, &iter, str, case_sens,
1844
                                   &match_pos)) {
1845
                GtkTextIter end = match_pos;
1846

    
1847
                gtk_text_iter_forward_chars(&end, len);
1848
                /* place "insert" at the last character */
1849
                gtk_text_buffer_select_range(buffer, &end, &match_pos);
1850
                mark = gtk_text_buffer_get_insert(buffer);
1851
                gtk_text_view_scroll_to_mark(text, mark, 0.0, FALSE, 0.0, 0.0);
1852
                return TRUE;
1853
        }
1854

    
1855
        return FALSE;
1856
}
1857

    
1858
gboolean textview_search_string_backward(TextView *textview, const gchar *str,
1859
                                         gboolean case_sens)
1860
{
1861
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1862
        GtkTextBuffer *buffer;
1863
        GtkTextIter iter, match_pos;
1864
        GtkTextMark *mark;
1865
        gint len;
1866

    
1867
        g_return_val_if_fail(str != NULL, FALSE);
1868

    
1869
        buffer = gtk_text_view_get_buffer(text);
1870

    
1871
        len = g_utf8_strlen(str, -1);
1872
        g_return_val_if_fail(len >= 0, FALSE);
1873

    
1874
        gtk_text_buffer_get_selection_bounds(buffer, &iter, NULL);
1875

    
1876
        if (gtkut_text_buffer_find_backward(buffer, &iter, str, case_sens,
1877
                                            &match_pos)) {
1878
                GtkTextIter end = match_pos;
1879

    
1880
                gtk_text_iter_forward_chars(&end, len);
1881
                gtk_text_buffer_select_range(buffer, &match_pos, &end);
1882
                mark = gtk_text_buffer_get_insert(buffer);
1883
                gtk_text_view_scroll_to_mark(text, mark, 0.0, FALSE, 0.0, 0.0);
1884
                return TRUE;
1885
        }
1886

    
1887
        return FALSE;
1888
}
1889

    
1890
void textview_scroll_one_line(TextView *textview, gboolean up)
1891
{
1892
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1893
        GtkAdjustment *vadj = text->vadjustment;
1894
        gfloat upper;
1895

    
1896
        if (prefs_common.enable_smooth_scroll) {
1897
                textview_smooth_scroll_one_line(textview, up);
1898
                return;
1899
        }
1900

    
1901
        if (!up) {
1902
                upper = vadj->upper - vadj->page_size;
1903
                if (vadj->value < upper) {
1904
                        vadj->value += vadj->step_increment;
1905
                        vadj->value = MIN(vadj->value, upper);
1906
                        g_signal_emit_by_name(G_OBJECT(vadj),
1907
                                              "value_changed", 0);
1908
                }
1909
        } else {
1910
                if (vadj->value > 0.0) {
1911
                        vadj->value -= vadj->step_increment;
1912
                        vadj->value = MAX(vadj->value, 0.0);
1913
                        g_signal_emit_by_name(G_OBJECT(vadj),
1914
                                              "value_changed", 0);
1915
                }
1916
        }
1917
}
1918

    
1919
gboolean textview_scroll_page(TextView *textview, gboolean up)
1920
{
1921
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1922
        GtkAdjustment *vadj = text->vadjustment;
1923
        gfloat upper;
1924
        gfloat page_incr;
1925

    
1926
        if (prefs_common.enable_smooth_scroll)
1927
                return textview_smooth_scroll_page(textview, up);
1928

    
1929
        if (prefs_common.scroll_halfpage)
1930
                page_incr = vadj->page_increment / 2;
1931
        else
1932
                page_incr = vadj->page_increment;
1933

    
1934
        if (!up) {
1935
                upper = vadj->upper - vadj->page_size;
1936
                if (vadj->value < upper) {
1937
                        vadj->value += page_incr;
1938
                        vadj->value = MIN(vadj->value, upper);
1939
                        g_signal_emit_by_name(G_OBJECT(vadj),
1940
                                              "value_changed", 0);
1941
                } else
1942
                        return FALSE;
1943
        } else {
1944
                if (vadj->value > 0.0) {
1945
                        vadj->value -= page_incr;
1946
                        vadj->value = MAX(vadj->value, 0.0);
1947
                        g_signal_emit_by_name(G_OBJECT(vadj),
1948
                                              "value_changed", 0);
1949
                } else
1950
                        return FALSE;
1951
        }
1952

    
1953
        return TRUE;
1954
}
1955

    
1956
static void textview_smooth_scroll_do(TextView *textview,
1957
                                      gfloat old_value, gfloat last_value,
1958
                                      gint step)
1959
{
1960
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1961
        GtkAdjustment *vadj = text->vadjustment;
1962
        gint change_value;
1963
        gboolean up;
1964
        gint i;
1965

    
1966
        if (old_value < last_value) {
1967
                change_value = last_value - old_value;
1968
                up = FALSE;
1969
        } else {
1970
                change_value = old_value - last_value;
1971
                up = TRUE;
1972
        }
1973

    
1974
        /* gdk_key_repeat_disable(); */
1975

    
1976
        g_signal_handlers_block_by_func(vadj, textview_adj_value_changed,
1977
                                        textview);
1978

    
1979
        for (i = step; i <= change_value; i += step) {
1980
                vadj->value = old_value + (up ? -i : i);
1981
                g_signal_emit_by_name(G_OBJECT(vadj), "value_changed", 0);
1982
        }
1983

    
1984
        g_signal_handlers_unblock_by_func(vadj, textview_adj_value_changed,
1985
                                          textview);
1986

    
1987
        vadj->value = last_value;
1988
        g_signal_emit_by_name(G_OBJECT(vadj), "value_changed", 0);
1989

    
1990
        /* gdk_key_repeat_restore(); */
1991

    
1992
        gtk_widget_queue_draw(GTK_WIDGET(text));
1993
}
1994

    
1995
static void textview_smooth_scroll_one_line(TextView *textview, gboolean up)
1996
{
1997
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1998
        GtkAdjustment *vadj = text->vadjustment;
1999
        gfloat upper;
2000
        gfloat old_value;
2001
        gfloat last_value;
2002

    
2003
        if (!up) {
2004
                upper = vadj->upper - vadj->page_size;
2005
                if (vadj->value < upper) {
2006
                        old_value = vadj->value;
2007
                        last_value = vadj->value + vadj->step_increment;
2008
                        last_value = MIN(last_value, upper);
2009

    
2010
                        textview_smooth_scroll_do(textview, old_value,
2011
                                                  last_value,
2012
                                                  prefs_common.scroll_step);
2013
                }
2014
        } else {
2015
                if (vadj->value > 0.0) {
2016
                        old_value = vadj->value;
2017
                        last_value = vadj->value - vadj->step_increment;
2018
                        last_value = MAX(last_value, 0.0);
2019

    
2020
                        textview_smooth_scroll_do(textview, old_value,
2021
                                                  last_value,
2022
                                                  prefs_common.scroll_step);
2023
                }
2024
        }
2025
}
2026

    
2027
static gboolean textview_smooth_scroll_page(TextView *textview, gboolean up)
2028
{
2029
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
2030
        GtkAdjustment *vadj = text->vadjustment;
2031
        gfloat upper;
2032
        gfloat page_incr;
2033
        gfloat old_value;
2034
        gfloat last_value;
2035

    
2036
        if (prefs_common.scroll_halfpage)
2037
                page_incr = vadj->page_increment / 2;
2038
        else
2039
                page_incr = vadj->page_increment;
2040

    
2041
        if (!up) {
2042
                upper = vadj->upper - vadj->page_size;
2043
                if (vadj->value < upper) {
2044
                        old_value = vadj->value;
2045
                        last_value = vadj->value + page_incr;
2046
                        last_value = MIN(last_value, upper);
2047

    
2048
                        textview_smooth_scroll_do(textview, old_value,
2049
                                                  last_value,
2050
                                                  prefs_common.scroll_step);
2051
                } else
2052
                        return FALSE;
2053
        } else {
2054
                if (vadj->value > 0.0) {
2055
                        old_value = vadj->value;
2056
                        last_value = vadj->value - page_incr;
2057
                        last_value = MAX(last_value, 0.0);
2058

    
2059
                        textview_smooth_scroll_do(textview, old_value,
2060
                                                  last_value,
2061
                                                  prefs_common.scroll_step);
2062
                } else
2063
                        return FALSE;
2064
        }
2065

    
2066
        return TRUE;
2067
}
2068

    
2069
#define KEY_PRESS_EVENT_STOP() \
2070
        g_signal_stop_emission_by_name(G_OBJECT(widget), "key-press-event");
2071

    
2072
static gboolean textview_key_pressed(GtkWidget *widget, GdkEventKey *event,
2073
                                     TextView *textview)
2074
{
2075
        SummaryView *summaryview = NULL;
2076
        MessageView *messageview = textview->messageview;
2077

    
2078
        if (!event) return FALSE;
2079
        if (messageview->mainwin)
2080
                summaryview = messageview->mainwin->summaryview;
2081

    
2082
        switch (event->keyval) {
2083
        case GDK_Tab:
2084
        case GDK_Home:
2085
        case GDK_Left:
2086
        case GDK_Up:
2087
        case GDK_Right:
2088
        case GDK_Down:
2089
        case GDK_Page_Up:
2090
        case GDK_Page_Down:
2091
        case GDK_End:
2092
        case GDK_Control_L:
2093
        case GDK_Control_R:
2094
        case GDK_KP_Tab:
2095
        case GDK_KP_Home:
2096
        case GDK_KP_Left:
2097
        case GDK_KP_Up:
2098
        case GDK_KP_Right:
2099
        case GDK_KP_Down:
2100
        case GDK_KP_Page_Up:
2101
        case GDK_KP_Page_Down:
2102
        case GDK_KP_End:
2103
                break;
2104
        case GDK_space:
2105
        case GDK_KP_Space:
2106
                if (summaryview)
2107
                        summary_pass_key_press_event(summaryview, event);
2108
                else
2109
                        textview_scroll_page
2110
                                (textview,
2111
                                 (event->state &
2112
                                  (GDK_SHIFT_MASK|GDK_MOD1_MASK)) != 0);
2113
                break;
2114
        case GDK_BackSpace:
2115
                textview_scroll_page(textview, TRUE);
2116
                break;
2117
        case GDK_Return:
2118
        case GDK_KP_Enter:
2119
                textview_scroll_one_line
2120
                        (textview, (event->state &
2121
                                    (GDK_SHIFT_MASK|GDK_MOD1_MASK)) != 0);
2122
                break;
2123
        case GDK_Delete:
2124
        case GDK_KP_Delete:
2125
                if (summaryview)
2126
                        summary_pass_key_press_event(summaryview, event);
2127
                break;
2128
        case GDK_Escape:
2129
                if (summaryview && textview == messageview->textview)
2130
                        gtk_widget_grab_focus(summaryview->treeview);
2131
                else if (messageview->type == MVIEW_MIME &&
2132
                         textview == messageview->mimeview->textview)
2133
                        gtk_widget_grab_focus(messageview->mimeview->treeview);
2134
                break;
2135
        case GDK_n:
2136
        case GDK_N:
2137
        case GDK_p:
2138
        case GDK_P:
2139
        case GDK_y:
2140
        case GDK_t:
2141
        case GDK_l:
2142
                if (messageview->type == MVIEW_MIME &&
2143
                    textview == messageview->mimeview->textview) {
2144
                        KEY_PRESS_EVENT_STOP();
2145
                        mimeview_pass_key_press_event(messageview->mimeview,
2146
                                                      event);
2147
                        break;
2148
                }
2149
                /* fall through */
2150
        default:
2151
                if (summaryview &&
2152
                    event->window != messageview->mainwin->window->window) {
2153
                        GdkEventKey tmpev = *event;
2154

    
2155
                        tmpev.window = messageview->mainwin->window->window;
2156
                        KEY_PRESS_EVENT_STOP();
2157
                        gtk_widget_event(messageview->mainwin->window,
2158
                                         (GdkEvent *)&tmpev);
2159
                }
2160
                break;
2161
        }
2162

    
2163
        return FALSE;
2164
}
2165

    
2166
static gboolean textview_get_link_tag_bounds(TextView *textview,
2167
                                             GtkTextIter *iter,
2168
                                             GtkTextIter *start,
2169
                                             GtkTextIter *end)
2170
{
2171
        GSList *tags, *cur;
2172
        gboolean on_link = FALSE;
2173

    
2174
        tags = gtk_text_iter_get_tags(iter);
2175
        *start = *end = *iter;
2176

    
2177
        for (cur = tags; cur != NULL; cur = cur->next) {
2178
                GtkTextTag *tag = cur->data;
2179
                gchar *tag_name;
2180

    
2181
                g_object_get(G_OBJECT(tag), "name", &tag_name, NULL);
2182
                if (tag_name && !strcmp(tag_name, "link")) {
2183
                        if (!gtk_text_iter_begins_tag(start, tag))
2184
                                gtk_text_iter_backward_to_tag_toggle
2185
                                        (start, tag);
2186
                        if (!gtk_text_iter_ends_tag(end, tag))
2187
                                gtk_text_iter_forward_to_tag_toggle(end, tag);
2188
                        on_link = TRUE;
2189
                        g_free(tag_name);
2190
                        break;
2191
                }
2192
                if (tag_name)
2193
                        g_free(tag_name);
2194
        }
2195

    
2196
        if (tags)
2197
                g_slist_free(tags);
2198

    
2199
        return on_link;
2200
}
2201

    
2202
static RemoteURI *textview_get_uri(TextView *textview, GtkTextIter *start,
2203
                                   GtkTextIter *end)
2204
{
2205
        gint start_pos, end_pos;
2206
        GSList *cur;
2207
        RemoteURI *uri = NULL;
2208

    
2209
        start_pos = gtk_text_iter_get_offset(start);
2210
        end_pos = gtk_text_iter_get_offset(end);
2211

    
2212
        for (cur = textview->uri_list; cur != NULL; cur = cur->next) {
2213
                RemoteURI *uri_ = (RemoteURI *)cur->data;
2214

    
2215
                if (start_pos == uri_->start && end_pos == uri_->end) {
2216
                        debug_print("uri found: (%d, %d): %s\n",
2217
                                    start_pos, end_pos, uri_->uri);
2218
                        uri = uri_;
2219
                        break;
2220
                }
2221
        }
2222

    
2223
        return uri;
2224
}
2225

    
2226
static gboolean textview_event_after(GtkWidget *widget, GdkEvent *event,
2227
                                     TextView *textview)
2228
{
2229
        GdkEventButton *bevent = (GdkEventButton *)event;
2230
        GtkTextBuffer *buffer;
2231
        GtkTextIter iter, start, end;
2232
        gint x, y;
2233
        gboolean on_link;
2234
        RemoteURI *uri;
2235

    
2236
        if (event->type != GDK_BUTTON_RELEASE)
2237
                return FALSE;
2238
        if (bevent->button != 1 && bevent->button != 2)
2239
                return FALSE;
2240

    
2241
        /* don't process child widget's event */
2242
        if (gtk_text_view_get_window(GTK_TEXT_VIEW(widget), GTK_TEXT_WINDOW_TEXT) != bevent->window)
2243
                return FALSE;
2244

    
2245
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(widget));
2246

    
2247
        /* don't follow a link if the user has selected something */
2248
        gtk_text_buffer_get_selection_bounds(buffer, &start, &end);
2249
        if (!gtk_text_iter_equal(&start, &end))
2250
                return FALSE;
2251

    
2252
        gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(widget),
2253
                                              GTK_TEXT_WINDOW_TEXT,
2254
                                              bevent->x, bevent->y, &x, &y);
2255
        gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(widget), &iter, x, y);
2256
        on_link = textview_get_link_tag_bounds(textview, &iter, &start, &end);
2257
        if (!on_link)
2258
                return FALSE;
2259

    
2260
        uri = textview_get_uri(textview, &start, &end);
2261
        if (!uri || !uri->uri)
2262
                return FALSE;
2263

    
2264
        if (!g_ascii_strncasecmp(uri->uri, "mailto:", 7)) {
2265
                PrefsAccount *ac = NULL;
2266
                MsgInfo *msginfo = textview->messageview->msginfo;
2267

    
2268
                if (msginfo && msginfo->folder)
2269
                        ac = account_find_from_item(msginfo->folder);
2270
                if (ac && ac->protocol == A_NNTP)
2271
                        ac = NULL;
2272
                compose_new(ac, msginfo->folder, uri->uri + 7, NULL);
2273
        } else if (uri->uri[0] == '#') {
2274
                /* don't open in-page link */
2275
        } else if (textview_uri_security_check(textview, uri) == TRUE)
2276
                open_uri(uri->uri, prefs_common.uri_cmd);
2277

    
2278
        return FALSE;
2279
}
2280

    
2281
static void textview_show_uri(TextView *textview, GtkTextIter *start,
2282
                              GtkTextIter *end)
2283
{
2284
        RemoteURI *uri;
2285

    
2286
        STATUSBAR_POP(textview);
2287
        uri = textview_get_uri(textview, start, end);
2288
        if (uri) {
2289
                STATUSBAR_PUSH(textview, uri->uri);
2290
        }
2291
}
2292

    
2293
static void textview_set_cursor(TextView *textview, GtkTextView *text,
2294
                                gint x, gint y)
2295
{
2296
        GtkTextBuffer *buffer;
2297
        GtkTextIter iter;
2298
        GtkTextIter start, end;
2299
        GtkTextMark *start_mark, *end_mark;
2300
        gboolean on_link = FALSE;
2301

    
2302
        buffer = gtk_text_view_get_buffer(text);
2303
        gtk_text_view_get_iter_at_location(text, &iter, x, y);
2304
        on_link = textview_get_link_tag_bounds(textview, &iter, &start, &end);
2305

    
2306
        start_mark = gtk_text_buffer_get_mark(buffer, "hover-link-start");
2307
        end_mark = gtk_text_buffer_get_mark(buffer, "hover-link-end");
2308
        if (start_mark) {
2309
                GtkTextIter prev_start, prev_end;
2310

    
2311
                gtk_text_buffer_get_iter_at_mark(buffer, &prev_start,
2312
                                                 start_mark);
2313
                gtk_text_buffer_get_iter_at_mark(buffer, &prev_end, end_mark);
2314
                if (on_link) {
2315
                        if (gtk_text_iter_equal(&prev_start, &start))
2316
                                return;
2317
                }
2318

    
2319
                gtk_text_buffer_get_iter_at_mark(buffer, &prev_start,
2320
                                                 start_mark);
2321
                gtk_text_buffer_get_iter_at_mark(buffer, &prev_end, end_mark);
2322
                gtk_text_buffer_remove_tag_by_name(buffer, "hover-link",
2323
                                                   &prev_start, &prev_end);
2324
                gtk_text_buffer_delete_mark(buffer, start_mark);
2325
                gtk_text_buffer_delete_mark(buffer, end_mark);
2326
        } else {
2327
                if (!on_link)
2328
                        return;
2329
        }
2330

    
2331
        if (on_link) {
2332
                gtk_text_buffer_create_mark
2333
                        (buffer, "hover-link-start", &start, FALSE);
2334
                gtk_text_buffer_create_mark
2335
                        (buffer, "hover-link-end", &end, FALSE);
2336
                gtk_text_buffer_apply_tag_by_name
2337
                        (buffer, "hover-link", &start, &end);
2338
                gdk_window_set_cursor
2339
                        (gtk_text_view_get_window(text, GTK_TEXT_WINDOW_TEXT),
2340
                         hand_cursor);
2341
                textview_show_uri(textview, &start, &end);
2342
        } else {
2343
                gdk_window_set_cursor
2344
                        (gtk_text_view_get_window(text, GTK_TEXT_WINDOW_TEXT),
2345
                         regular_cursor);
2346
                STATUSBAR_POP(textview);
2347
        }
2348
}
2349

    
2350
static gboolean textview_motion_notify(GtkWidget *widget,
2351
                                       GdkEventMotion *event,
2352
                                       TextView *textview)
2353
{
2354
        gint x, y;
2355
 
2356
        if (gtk_text_view_get_window(GTK_TEXT_VIEW(widget), GTK_TEXT_WINDOW_TEXT) != event->window)
2357
                return FALSE;
2358
        gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(widget),
2359
                                              GTK_TEXT_WINDOW_WIDGET,
2360
                                              event->x, event->y, &x, &y);
2361
        textview_set_cursor(textview, GTK_TEXT_VIEW(widget), x, y);
2362
        gdk_window_get_pointer(widget->window, NULL, NULL, NULL);
2363

    
2364
        return FALSE;
2365
}
2366

    
2367
static gboolean textview_leave_notify(GtkWidget *widget,
2368
                                      GdkEventCrossing *event,
2369
                                      TextView *textview)
2370
{
2371
        textview_set_cursor(textview, GTK_TEXT_VIEW(widget), 0, 0);
2372

    
2373
        return FALSE;
2374
}
2375

    
2376
static gboolean textview_visibility_notify(GtkWidget *widget,
2377
                                           GdkEventVisibility *event,
2378
                                           TextView *textview)
2379
{
2380
        gint wx, wy, bx, by;
2381
        GdkWindow *window;
2382

    
2383
        window = gtk_text_view_get_window(GTK_TEXT_VIEW(widget),
2384
                                          GTK_TEXT_WINDOW_TEXT);
2385

    
2386
        /* check if the event occurred for the text window part */
2387
        if (window != event->window)
2388
                return FALSE;
2389

    
2390
        gdk_window_get_pointer(widget->window, &wx, &wy, NULL);
2391
        gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(widget),
2392
                                              GTK_TEXT_WINDOW_WIDGET,
2393
                                              wx, wy, &bx, &by);
2394
        textview_set_cursor(textview, GTK_TEXT_VIEW(widget), bx, by);
2395

    
2396
        return FALSE;
2397
}
2398

    
2399
#define ADD_MENU(label, func)                                                \
2400
{                                                                        \
2401
        menuitem = gtk_menu_item_new_with_mnemonic(label);                \
2402
        g_signal_connect(menuitem, "activate", G_CALLBACK(func), uri);        \
2403
        gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);                \
2404
        g_object_set_data(G_OBJECT(menuitem), "textview", textview);        \
2405
        gtk_widget_show(menuitem);                                        \
2406
}
2407

    
2408
#define ADD_MENU_SEPARATOR()                                                \
2409
{                                                                        \
2410
        menuitem = gtk_menu_item_new();                                        \
2411
        gtk_widget_set_sensitive(menuitem, FALSE);                        \
2412
        gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);                \
2413
        gtk_widget_show(menuitem);                                        \
2414
}
2415

    
2416
static void textview_populate_popup(GtkWidget *widget, GtkMenu *menu,
2417
                                    TextView *textview)
2418
{
2419
        gint px, py, x, y;
2420
        GtkWidget *separator, *menuitem;
2421
        GtkTextBuffer *buffer;
2422
        GtkTextIter iter, start, end;
2423
        gboolean on_link;
2424
        RemoteURI *uri;
2425
        GdkPixbuf *pixbuf;
2426
        gchar *selected_text;
2427

    
2428
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(widget));
2429

    
2430
        gdk_window_get_pointer(widget->window, &px, &py, NULL);
2431
        gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(widget),
2432
                                              GTK_TEXT_WINDOW_WIDGET,
2433
                                              px, py, &x, &y);
2434
        gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(widget), &iter, x, y);
2435
        if ((pixbuf = gtk_text_iter_get_pixbuf(&iter)) != NULL) {
2436
                start = end = iter;
2437
                gtk_text_iter_forward_char(&end);
2438
                uri = textview_get_uri(textview, &start, &end);
2439

    
2440
                separator = gtk_separator_menu_item_new();
2441
                gtk_menu_shell_append(GTK_MENU_SHELL(menu), separator);
2442
                gtk_widget_show(separator);
2443

    
2444
                ADD_MENU(_("Sa_ve this image as..."),
2445
                         textview_popup_menu_activate_image_cb);
2446
        }
2447

    
2448
        selected_text = gtkut_text_view_get_selection(GTK_TEXT_VIEW(widget));
2449

    
2450
        uri = NULL;
2451
        on_link = textview_get_link_tag_bounds(textview, &iter, &start, &end);
2452
        if (!on_link)
2453
                goto finish;
2454

    
2455
        uri = textview_get_uri(textview, &start, &end);
2456
        if (!uri || !uri->uri)
2457
                goto finish;
2458

    
2459
        separator = gtk_separator_menu_item_new();
2460
        gtk_menu_shell_append(GTK_MENU_SHELL(menu), separator);
2461
        gtk_widget_show(separator);
2462

    
2463
        if (!g_ascii_strncasecmp(uri->uri, "mailto:", 7)) {
2464
                ADD_MENU(_("Compose _new message"),
2465
                         textview_popup_menu_activate_open_uri_cb);
2466
                ADD_MENU(_("R_eply to this address"),
2467
                         textview_popup_menu_activate_reply_cb);
2468
                ADD_MENU_SEPARATOR();
2469
                ADD_MENU(_("Add to address _book..."),
2470
                         textview_popup_menu_activate_add_address_cb);
2471
                ADD_MENU(_("Copy this add_ress"),
2472
                         textview_popup_menu_activate_copy_cb);
2473
        } else {
2474
                ADD_MENU(_("_Open with Web browser"),
2475
                         textview_popup_menu_activate_open_uri_cb);
2476
                ADD_MENU(_("Copy this _link"),
2477
                         textview_popup_menu_activate_copy_cb);
2478
        }
2479

    
2480
finish:
2481
        syl_plugin_signal_emit("textview-menu-popup", menu,
2482
                               GTK_TEXT_VIEW(widget),
2483
                               uri ? uri->uri : NULL,
2484
                               selected_text,
2485
                               textview->messageview->msginfo);
2486
        g_free(selected_text);
2487
}
2488

    
2489
static void textview_popup_menu_activate_open_uri_cb(GtkMenuItem *menuitem,
2490
                                                     gpointer data)
2491
{
2492
        RemoteURI *uri = (RemoteURI *)data;
2493
        TextView *textview;
2494

    
2495
        g_return_if_fail(uri != NULL);
2496

    
2497
        if (!uri->uri)
2498
                return;
2499

    
2500
        textview = g_object_get_data(G_OBJECT(menuitem), "textview");
2501
        g_return_if_fail(textview != NULL);
2502

    
2503
        if (!g_ascii_strncasecmp(uri->uri, "mailto:", 7)) {
2504
                PrefsAccount *ac = NULL;
2505
                MsgInfo *msginfo = textview->messageview->msginfo;
2506

    
2507
                if (msginfo && msginfo->folder)
2508
                        ac = account_find_from_item(msginfo->folder);
2509
                if (ac && ac->protocol == A_NNTP)
2510
                        ac = NULL;
2511
                compose_new(ac, msginfo->folder, uri->uri + 7, NULL);
2512
        } else if (uri->uri[0] == '#') {
2513
                /* don't open in-page link */
2514
        } else if (textview_uri_security_check(textview, uri) == TRUE)
2515
                open_uri(uri->uri, prefs_common.uri_cmd);
2516
}
2517

    
2518
static void textview_popup_menu_activate_reply_cb(GtkMenuItem *menuitem,
2519
                                                  gpointer data)
2520
{
2521
        RemoteURI *uri = (RemoteURI *)data;
2522
        TextView *textview;
2523

    
2524
        g_return_if_fail(uri != NULL);
2525

    
2526
        if (!uri->uri)
2527
                return;
2528

    
2529
        textview = g_object_get_data(G_OBJECT(menuitem), "textview");
2530
        g_return_if_fail(textview != NULL);
2531

    
2532
        if (!g_ascii_strncasecmp(uri->uri, "mailto:", 7)) {
2533
                MsgInfo *msginfo = textview->messageview->msginfo;
2534
                ComposeMode mode = COMPOSE_REPLY;
2535
                gchar *text;
2536
                GList *list;
2537
                Compose *compose;
2538

    
2539
                g_return_if_fail(msginfo != NULL);
2540

    
2541
                if (prefs_common.reply_with_quote)
2542
                        mode |= COMPOSE_WITH_QUOTE;
2543

    
2544
                text = gtkut_text_view_get_selection
2545
                        (GTK_TEXT_VIEW(textview->text));
2546
                if (text && *text == '\0') {
2547
                        g_free(text);
2548
                        text = NULL;
2549
                }
2550

    
2551
                compose_reply(msginfo, msginfo->folder, mode, text);
2552
                list = compose_get_compose_list();
2553
                list = g_list_last(list);
2554
                if (list && list->data) {
2555
                        compose = (Compose *)list->data;
2556
                        compose_block_modified(compose);
2557
                        compose_entry_set(compose, uri->uri + 7,
2558
                                          COMPOSE_ENTRY_TO);
2559
                        compose_unblock_modified(compose);
2560
                }
2561
                g_free(text);
2562
        }
2563
}
2564

    
2565
static void textview_popup_menu_activate_add_address_cb(GtkMenuItem *menuitem,
2566
                                                        gpointer data)
2567
{
2568
        RemoteURI *uri = (RemoteURI *)data;
2569
        gchar *addr;
2570

    
2571
        g_return_if_fail(uri != NULL);
2572

    
2573
        if (!uri->uri)
2574
                return;
2575

    
2576
        if (!g_ascii_strncasecmp(uri->uri, "mailto:", 7)) {
2577
                addr = g_malloc(strlen(uri->uri + 7) + 1);
2578
                decode_uri(addr, uri->uri + 7);
2579
        } else
2580
                addr = g_strdup(uri->uri);
2581

    
2582
        addressbook_add_contact(addr, addr, NULL);
2583

    
2584
        g_free(addr);
2585
}
2586

    
2587
static void textview_popup_menu_activate_copy_cb(GtkMenuItem *menuitem,
2588
                                                 gpointer data)
2589
{
2590
        RemoteURI *uri = (RemoteURI *)data;
2591
        gchar *uri_string;
2592
        GtkClipboard *clipboard;
2593

    
2594
        g_return_if_fail(uri != NULL);
2595

    
2596
        if (!uri->uri)
2597
                return;
2598

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

    
2605
        clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
2606
        gtk_clipboard_set_text(clipboard, uri_string, -1);
2607
        clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
2608
        gtk_clipboard_set_text(clipboard, uri_string, -1);
2609

    
2610
        g_free(uri_string);
2611
}
2612

    
2613
static void textview_popup_menu_activate_image_cb(GtkMenuItem *menuitem,
2614
                                                  gpointer data)
2615
{
2616
        RemoteURI *uri = (RemoteURI *)data;
2617
        gchar *src;
2618
        gchar *dest;
2619
        gchar *filename;
2620

    
2621
        g_return_if_fail(uri != NULL);
2622

    
2623
        if (!uri->uri)
2624
                return;
2625

    
2626
        src = g_filename_from_uri(uri->uri, NULL, NULL);
2627
        g_return_if_fail(src != NULL);
2628

    
2629
        filename = conv_filename_to_utf8(uri->filename);
2630
        dest = filesel_save_as(filename);
2631
        if (dest) {
2632
                copy_file(src, dest, FALSE);
2633
                g_free(dest);
2634
        }
2635
        g_free(filename);
2636
        g_free(src);
2637
}
2638

    
2639
static void textview_adj_value_changed(GtkAdjustment *adj, gpointer data)
2640
{
2641
        TextView *textview = (TextView *)data;
2642
        GtkTextBuffer *buffer;
2643

    
2644
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
2645
        if (gtk_text_buffer_get_selection_bounds(buffer, NULL, NULL))
2646
                return;
2647
        gtk_text_view_place_cursor_onscreen(GTK_TEXT_VIEW(textview->text));
2648
}
2649

    
2650
static gboolean textview_uri_security_check(TextView *textview, RemoteURI *uri)
2651
{
2652
        GtkTextBuffer *buffer;
2653
        GtkTextIter start_iter, end_iter;
2654
        gchar *visible_str;
2655
        gboolean retval = TRUE;
2656

    
2657
        if (is_uri_string(uri->uri) == FALSE)
2658
                return TRUE;
2659

    
2660
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
2661
        gtk_text_buffer_get_iter_at_offset(buffer, &start_iter, uri->start);
2662
        gtk_text_buffer_get_iter_at_offset(buffer, &end_iter, uri->end);
2663
        visible_str = gtk_text_buffer_get_text(buffer, &start_iter, &end_iter,
2664
                                               FALSE);
2665
        if (visible_str == NULL)
2666
                return TRUE;
2667

    
2668
        if (strcmp(visible_str, uri->uri) != 0 && is_uri_string(visible_str)) {
2669
                gchar *uri_path;
2670
                gchar *visible_uri_path;
2671

    
2672
                uri_path = get_uri_path(uri->uri);
2673
                visible_uri_path = get_uri_path(visible_str);
2674
                if (path_cmp(uri_path, visible_uri_path) != 0)
2675
                        retval = FALSE;
2676
        }
2677

    
2678
        if (retval == FALSE) {
2679
                gchar *msg;
2680
                AlertValue aval;
2681

    
2682
                msg = g_strdup_printf(_("The real URL (%s) is different from\n"
2683
                                        "the apparent URL (%s).\n"
2684
                                        "\n"
2685
                                        "Open it anyway?"),
2686
                                      uri->uri, visible_str);
2687
                aval = alertpanel_full(_("Fake URL warning"), msg,
2688
                                       ALERT_WARNING, G_ALERTDEFAULT, FALSE,
2689
                                       GTK_STOCK_YES, GTK_STOCK_NO, NULL);
2690
                g_free(msg);
2691
                if (aval == G_ALERTDEFAULT)
2692
                        retval = TRUE;
2693
        }
2694

    
2695
        g_free(visible_str);
2696

    
2697
        return retval;
2698
}
2699

    
2700
static void textview_uri_list_remove_all(GSList *uri_list)
2701
{
2702
        GSList *cur;
2703

    
2704
        for (cur = uri_list; cur != NULL; cur = cur->next) {
2705
                RemoteURI *uri = (RemoteURI *)cur->data;
2706

    
2707
                if (uri) {
2708
                        g_free(uri->uri);
2709
                        g_free(uri->filename);
2710
                        g_free(uri);
2711
                }
2712
        }
2713

    
2714
        g_slist_free(uri_list);
2715
}
2716

    
2717
static void textview_uri_list_update_offsets(TextView *textview, gint start, gint add)
2718
{
2719
        GSList *cur;
2720

    
2721
        debug_print("textview_uri_list_update_offsets: from %d: add %d\n", start, add);
2722

    
2723
        for (cur = textview->uri_list; cur != NULL; cur = cur->next) {
2724
                RemoteURI *uri = (RemoteURI *)cur->data;
2725

    
2726
                if (uri->start >= start) {
2727
                        debug_print("textview_uri_list_update_offsets: update: (%d, %d) -> (%d, %d)\n", uri->start, uri->end, uri->start + add, uri->end + add);
2728
                        uri->start += add;
2729
                        uri->end += add;
2730
                }
2731
        }
2732
}