Statistics
| Revision:

root / src / textview.c @ 180

History | View | Annotate | Download (51.5 kB)

1 1 hiro
/*
2 1 hiro
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 27 hiro
 * Copyright (C) 1999-2005 Hiroyuki Yamamoto
4 1 hiro
 *
5 1 hiro
 * This program is free software; you can redistribute it and/or modify
6 1 hiro
 * it under the terms of the GNU General Public License as published by
7 1 hiro
 * the Free Software Foundation; either version 2 of the License, or
8 1 hiro
 * (at your option) any later version.
9 1 hiro
 *
10 1 hiro
 * This program is distributed in the hope that it will be useful,
11 1 hiro
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 1 hiro
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 1 hiro
 * GNU General Public License for more details.
14 1 hiro
 *
15 1 hiro
 * You should have received a copy of the GNU General Public License
16 1 hiro
 * along with this program; if not, write to the Free Software
17 1 hiro
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 1 hiro
 */
19 1 hiro
20 1 hiro
#ifdef HAVE_CONFIG_H
21 1 hiro
#  include "config.h"
22 1 hiro
#endif
23 1 hiro
24 1 hiro
#include "defs.h"
25 1 hiro
26 1 hiro
#include <glib.h>
27 92 hiro
#include <glib/gi18n.h>
28 1 hiro
#include <gdk/gdk.h>
29 1 hiro
#include <gdk/gdkkeysyms.h>
30 1 hiro
#include <gtk/gtkvbox.h>
31 1 hiro
#include <gtk/gtkscrolledwindow.h>
32 1 hiro
#include <gtk/gtksignal.h>
33 155 hiro
#include <gdk-pixbuf/gdk-pixbuf.h>
34 1 hiro
#include <stdio.h>
35 1 hiro
#include <ctype.h>
36 1 hiro
#include <string.h>
37 1 hiro
#include <stdlib.h>
38 1 hiro
39 1 hiro
#include "main.h"
40 1 hiro
#include "summaryview.h"
41 155 hiro
#include "imageview.h"
42 1 hiro
#include "procheader.h"
43 1 hiro
#include "prefs_common.h"
44 1 hiro
#include "codeconv.h"
45 1 hiro
#include "statusbar.h"
46 1 hiro
#include "utils.h"
47 1 hiro
#include "gtkutils.h"
48 1 hiro
#include "procmime.h"
49 1 hiro
#include "account.h"
50 1 hiro
#include "html.h"
51 1 hiro
#include "compose.h"
52 1 hiro
#include "displayheader.h"
53 162 hiro
#include "filesel.h"
54 1 hiro
#include "alertpanel.h"
55 1 hiro
56 1 hiro
typedef struct _RemoteURI        RemoteURI;
57 1 hiro
58 1 hiro
struct _RemoteURI
59 1 hiro
{
60 1 hiro
        gchar *uri;
61 1 hiro
62 162 hiro
        gchar *filename;
63 162 hiro
64 1 hiro
        guint start;
65 1 hiro
        guint end;
66 1 hiro
};
67 1 hiro
68 1 hiro
static GdkColor quote_colors[3] = {
69 1 hiro
        {(gulong)0, (gushort)0, (gushort)0, (gushort)0},
70 1 hiro
        {(gulong)0, (gushort)0, (gushort)0, (gushort)0},
71 1 hiro
        {(gulong)0, (gushort)0, (gushort)0, (gushort)0}
72 1 hiro
};
73 1 hiro
74 1 hiro
static GdkColor uri_color = {
75 1 hiro
        (gulong)0,
76 1 hiro
        (gushort)0,
77 1 hiro
        (gushort)0,
78 1 hiro
        (gushort)0
79 1 hiro
};
80 1 hiro
81 1 hiro
static GdkColor emphasis_color = {
82 1 hiro
        (gulong)0,
83 1 hiro
        (gushort)0,
84 1 hiro
        (gushort)0,
85 1 hiro
        (gushort)0xcfff
86 1 hiro
};
87 1 hiro
88 1 hiro
#if 0
89 1 hiro
static GdkColor error_color = {
90 1 hiro
        (gulong)0,
91 1 hiro
        (gushort)0xefff,
92 1 hiro
        (gushort)0,
93 1 hiro
        (gushort)0
94 1 hiro
};
95 1 hiro
#endif
96 1 hiro
97 1 hiro
#if USE_GPGME
98 1 hiro
static GdkColor good_sig_color = {
99 1 hiro
        (gulong)0,
100 1 hiro
        (gushort)0,
101 1 hiro
        (gushort)0xbfff,
102 1 hiro
        (gushort)0
103 1 hiro
};
104 1 hiro
105 56 hiro
static GdkColor untrusted_sig_color = {
106 56 hiro
        (gulong)0,
107 56 hiro
        (gushort)0xefff,
108 56 hiro
        (gushort)0,
109 56 hiro
        (gushort)0
110 56 hiro
};
111 56 hiro
112 1 hiro
static GdkColor nocheck_sig_color = {
113 1 hiro
        (gulong)0,
114 1 hiro
        (gushort)0,
115 1 hiro
        (gushort)0,
116 1 hiro
        (gushort)0xcfff
117 1 hiro
};
118 1 hiro
119 1 hiro
static GdkColor bad_sig_color = {
120 1 hiro
        (gulong)0,
121 1 hiro
        (gushort)0xefff,
122 1 hiro
        (gushort)0,
123 1 hiro
        (gushort)0
124 1 hiro
};
125 1 hiro
#endif
126 1 hiro
127 1 hiro
#define STATUSBAR_PUSH(textview, str)                                            \
128 1 hiro
{                                                                            \
129 1 hiro
        gtk_statusbar_push(GTK_STATUSBAR(textview->messageview->statusbar), \
130 1 hiro
                           textview->messageview->statusbar_cid, str);            \
131 1 hiro
}
132 1 hiro
133 1 hiro
#define STATUSBAR_POP(textview)                                                   \
134 1 hiro
{                                                                           \
135 1 hiro
        gtk_statusbar_pop(GTK_STATUSBAR(textview->messageview->statusbar), \
136 1 hiro
                          textview->messageview->statusbar_cid);           \
137 1 hiro
}
138 1 hiro
139 101 hiro
static GdkCursor *hand_cursor = NULL;
140 101 hiro
static GdkCursor *regular_cursor = NULL;
141 101 hiro
142 101 hiro
143 1 hiro
static void textview_add_part                (TextView        *textview,
144 1 hiro
                                         MimeInfo        *mimeinfo,
145 1 hiro
                                         FILE                *fp);
146 1 hiro
static void textview_add_parts                (TextView        *textview,
147 1 hiro
                                         MimeInfo        *mimeinfo,
148 1 hiro
                                         FILE                *fp);
149 1 hiro
static void textview_write_body                (TextView        *textview,
150 1 hiro
                                         MimeInfo        *mimeinfo,
151 1 hiro
                                         FILE                *fp,
152 1 hiro
                                         const gchar        *charset);
153 1 hiro
static void textview_show_html                (TextView        *textview,
154 1 hiro
                                         FILE                *fp,
155 1 hiro
                                         CodeConverter        *conv);
156 1 hiro
157 1 hiro
static void textview_write_line                (TextView        *textview,
158 1 hiro
                                         const gchar        *str,
159 1 hiro
                                         CodeConverter        *conv);
160 1 hiro
static void textview_write_link                (TextView        *textview,
161 1 hiro
                                         const gchar        *str,
162 1 hiro
                                         const gchar        *uri,
163 1 hiro
                                         CodeConverter        *conv);
164 1 hiro
165 1 hiro
static GPtrArray *textview_scan_header        (TextView        *textview,
166 144 hiro
                                         FILE                *fp,
167 144 hiro
                                         const gchar        *encoding);
168 1 hiro
static void textview_show_header        (TextView        *textview,
169 1 hiro
                                         GPtrArray        *headers);
170 1 hiro
171 1 hiro
static gboolean textview_key_pressed                (GtkWidget        *widget,
172 1 hiro
                                                 GdkEventKey        *event,
173 1 hiro
                                                 TextView        *textview);
174 101 hiro
static gboolean textview_event_after                (GtkWidget        *widget,
175 1 hiro
                                                 GdkEvent        *event,
176 1 hiro
                                                 TextView        *textview);
177 101 hiro
static gboolean textview_motion_notify                (GtkWidget        *widget,
178 101 hiro
                                                 GdkEventMotion        *event,
179 101 hiro
                                                 TextView        *textview);
180 113 hiro
static gboolean textview_leave_notify                (GtkWidget          *widget,
181 113 hiro
                                                 GdkEventCrossing *event,
182 113 hiro
                                                 TextView          *textview);
183 101 hiro
static gboolean textview_visibility_notify        (GtkWidget        *widget,
184 101 hiro
                                                 GdkEventVisibility *event,
185 101 hiro
                                                 TextView        *textview);
186 1 hiro
187 116 hiro
static void textview_populate_popup                (GtkWidget        *widget,
188 116 hiro
                                                 GtkMenu        *menu,
189 116 hiro
                                                 TextView        *textview);
190 116 hiro
static void textview_popup_menu_activate_copy_cb(GtkMenuItem        *menuitem,
191 116 hiro
                                                 gpointer         data);
192 162 hiro
static void textview_popup_menu_activate_image_cb
193 162 hiro
                                                (GtkMenuItem        *menuitem,
194 162 hiro
                                                 gpointer         data);
195 116 hiro
196 1 hiro
static void textview_smooth_scroll_do                (TextView        *textview,
197 1 hiro
                                                 gfloat                 old_value,
198 1 hiro
                                                 gfloat                 last_value,
199 1 hiro
                                                 gint                 step);
200 1 hiro
static void textview_smooth_scroll_one_line        (TextView        *textview,
201 1 hiro
                                                 gboolean         up);
202 1 hiro
static gboolean textview_smooth_scroll_page        (TextView        *textview,
203 1 hiro
                                                 gboolean         up);
204 1 hiro
205 101 hiro
static gboolean textview_get_link_tag_bounds        (TextView        *textview,
206 101 hiro
                                                 GtkTextIter        *iter,
207 101 hiro
                                                 GtkTextIter        *start,
208 101 hiro
                                                 GtkTextIter        *end);
209 101 hiro
static RemoteURI *textview_get_uri                (TextView        *textview,
210 101 hiro
                                                 GtkTextIter        *start,
211 101 hiro
                                                 GtkTextIter        *end);
212 101 hiro
static void textview_show_uri                        (TextView        *textview,
213 101 hiro
                                                 GtkTextIter        *start,
214 101 hiro
                                                 GtkTextIter        *end);
215 101 hiro
static void textview_set_cursor                        (TextView        *textview,
216 101 hiro
                                                 GtkTextView        *text,
217 101 hiro
                                                 gint                 x,
218 101 hiro
                                                 gint                 y);
219 101 hiro
220 1 hiro
static gboolean textview_uri_security_check        (TextView        *textview,
221 1 hiro
                                                 RemoteURI        *uri);
222 1 hiro
static void textview_uri_list_remove_all        (GSList                *uri_list);
223 1 hiro
224 1 hiro
225 1 hiro
TextView *textview_create(void)
226 1 hiro
{
227 1 hiro
        TextView *textview;
228 1 hiro
        GtkWidget *vbox;
229 1 hiro
        GtkWidget *scrolledwin;
230 1 hiro
        GtkWidget *text;
231 1 hiro
        GtkTextBuffer *buffer;
232 1 hiro
        GtkClipboard *clipboard;
233 1 hiro
234 1 hiro
        debug_print(_("Creating text view...\n"));
235 1 hiro
        textview = g_new0(TextView, 1);
236 1 hiro
237 1 hiro
        scrolledwin = gtk_scrolled_window_new(NULL, NULL);
238 1 hiro
        gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
239 97 hiro
                                       GTK_POLICY_AUTOMATIC,
240 97 hiro
                                       GTK_POLICY_AUTOMATIC);
241 4 hiro
        gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
242 4 hiro
                                            GTK_SHADOW_IN);
243 1 hiro
        gtk_widget_set_size_request
244 1 hiro
                (scrolledwin, prefs_common.mainview_width, -1);
245 1 hiro
246 1 hiro
        text = gtk_text_view_new();
247 113 hiro
        gtk_widget_add_events(text, GDK_LEAVE_NOTIFY_MASK);
248 1 hiro
        gtk_widget_show(text);
249 1 hiro
        gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
250 1 hiro
        gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
251 3 hiro
        gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text), 6);
252 3 hiro
        gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text), 6);
253 1 hiro
254 1 hiro
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
255 1 hiro
        clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
256 1 hiro
        gtk_text_buffer_add_selection_clipboard(buffer, clipboard);
257 1 hiro
258 1 hiro
        gtk_widget_ref(scrolledwin);
259 1 hiro
260 1 hiro
        gtk_container_add(GTK_CONTAINER(scrolledwin), text);
261 1 hiro
262 101 hiro
        g_signal_connect(G_OBJECT(text), "key-press-event",
263 1 hiro
                         G_CALLBACK(textview_key_pressed), textview);
264 101 hiro
        g_signal_connect(G_OBJECT(text), "event-after",
265 101 hiro
                         G_CALLBACK(textview_event_after), textview);
266 101 hiro
        g_signal_connect(G_OBJECT(text), "motion-notify-event",
267 101 hiro
                         G_CALLBACK(textview_motion_notify), textview);
268 113 hiro
        g_signal_connect(G_OBJECT(text), "leave-notify-event",
269 113 hiro
                         G_CALLBACK(textview_leave_notify), textview);
270 101 hiro
        g_signal_connect(G_OBJECT(text), "visibility-notify-event",
271 101 hiro
                         G_CALLBACK(textview_visibility_notify), textview);
272 116 hiro
        g_signal_connect(G_OBJECT(text), "populate-popup",
273 116 hiro
                         G_CALLBACK(textview_populate_popup), textview);
274 1 hiro
275 1 hiro
        gtk_widget_show(scrolledwin);
276 1 hiro
277 1 hiro
        vbox = gtk_vbox_new(FALSE, 0);
278 1 hiro
        gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
279 1 hiro
280 1 hiro
        gtk_widget_show(vbox);
281 1 hiro
282 1 hiro
        textview->vbox             = vbox;
283 1 hiro
        textview->scrolledwin      = scrolledwin;
284 1 hiro
        textview->text             = text;
285 1 hiro
        textview->uri_list         = NULL;
286 1 hiro
        textview->body_pos         = 0;
287 1 hiro
        textview->show_all_headers = FALSE;
288 1 hiro
289 1 hiro
        return textview;
290 1 hiro
}
291 1 hiro
292 1 hiro
static void textview_create_tags(GtkTextView *text, TextView *textview)
293 1 hiro
{
294 1 hiro
        GtkTextBuffer *buffer;
295 101 hiro
        static PangoFontDescription *font_desc;
296 1 hiro
297 101 hiro
        if (!font_desc)
298 39 hiro
                font_desc = gtkut_get_default_font_desc();
299 39 hiro
300 1 hiro
        buffer = gtk_text_view_get_buffer(text);
301 1 hiro
302 1 hiro
        gtk_text_buffer_create_tag(buffer, "header",
303 1 hiro
                                   "pixels-above-lines", 0,
304 1 hiro
                                   "pixels-above-lines-set", TRUE,
305 1 hiro
                                   "pixels-below-lines", 0,
306 1 hiro
                                   "pixels-below-lines-set", TRUE,
307 39 hiro
                                   "font-desc", font_desc,
308 1 hiro
                                   NULL);
309 1 hiro
        gtk_text_buffer_create_tag(buffer, "header_title",
310 101 hiro
                                   "weight", PANGO_WEIGHT_BOLD,
311 1 hiro
                                   NULL);
312 1 hiro
        gtk_text_buffer_create_tag(buffer, "quote0",
313 1 hiro
                                   "foreground-gdk", &quote_colors[0],
314 1 hiro
                                   NULL);
315 1 hiro
        gtk_text_buffer_create_tag(buffer, "quote1",
316 1 hiro
                                   "foreground-gdk", &quote_colors[1],
317 1 hiro
                                   NULL);
318 1 hiro
        gtk_text_buffer_create_tag(buffer, "quote2",
319 1 hiro
                                   "foreground-gdk", &quote_colors[2],
320 1 hiro
                                   NULL);
321 1 hiro
        gtk_text_buffer_create_tag(buffer, "emphasis",
322 1 hiro
                                   "foreground-gdk", &emphasis_color,
323 1 hiro
                                   NULL);
324 101 hiro
        gtk_text_buffer_create_tag(buffer, "link",
325 101 hiro
                                   "foreground-gdk", &uri_color,
326 101 hiro
                                   NULL);
327 101 hiro
        gtk_text_buffer_create_tag(buffer, "hover-link",
328 101 hiro
                                   "foreground-gdk", &uri_color,
329 101 hiro
                                   "underline", PANGO_UNDERLINE_SINGLE,
330 101 hiro
                                   NULL);
331 1 hiro
#if USE_GPGME
332 1 hiro
        gtk_text_buffer_create_tag(buffer, "good-signature",
333 1 hiro
                                   "foreground-gdk", &good_sig_color,
334 1 hiro
                                   NULL);
335 56 hiro
        gtk_text_buffer_create_tag(buffer, "untrusted-signature",
336 56 hiro
                                   "foreground-gdk", &untrusted_sig_color,
337 56 hiro
                                   NULL);
338 1 hiro
        gtk_text_buffer_create_tag(buffer, "bad-signature",
339 1 hiro
                                   "foreground-gdk", &bad_sig_color,
340 1 hiro
                                   NULL);
341 1 hiro
        gtk_text_buffer_create_tag(buffer, "nocheck-signature",
342 1 hiro
                                   "foreground-gdk", &nocheck_sig_color,
343 1 hiro
                                   NULL);
344 1 hiro
#endif /* USE_GPGME */
345 1 hiro
}
346 1 hiro
347 1 hiro
void textview_init(TextView *textview)
348 1 hiro
{
349 101 hiro
        if (!hand_cursor)
350 101 hiro
                hand_cursor = gdk_cursor_new(GDK_HAND2);
351 101 hiro
        if (!regular_cursor)
352 101 hiro
                regular_cursor = gdk_cursor_new(GDK_XTERM);
353 101 hiro
354 1 hiro
        textview_update_message_colors();
355 1 hiro
        textview_set_all_headers(textview, FALSE);
356 1 hiro
        textview_set_font(textview, NULL);
357 1 hiro
        textview_create_tags(GTK_TEXT_VIEW(textview->text), textview);
358 1 hiro
}
359 1 hiro
360 1 hiro
void textview_update_message_colors(void)
361 1 hiro
{
362 1 hiro
        GdkColor black = {0, 0, 0, 0};
363 1 hiro
364 1 hiro
        if (prefs_common.enable_color) {
365 1 hiro
                /* grab the quote colors, converting from an int to a GdkColor */
366 1 hiro
                gtkut_convert_int_to_gdk_color(prefs_common.quote_level1_col,
367 1 hiro
                                               &quote_colors[0]);
368 1 hiro
                gtkut_convert_int_to_gdk_color(prefs_common.quote_level2_col,
369 1 hiro
                                               &quote_colors[1]);
370 1 hiro
                gtkut_convert_int_to_gdk_color(prefs_common.quote_level3_col,
371 1 hiro
                                               &quote_colors[2]);
372 1 hiro
                gtkut_convert_int_to_gdk_color(prefs_common.uri_col,
373 1 hiro
                                               &uri_color);
374 1 hiro
        } else {
375 1 hiro
                quote_colors[0] = quote_colors[1] = quote_colors[2] =
376 1 hiro
                        uri_color = emphasis_color = black;
377 1 hiro
        }
378 1 hiro
}
379 1 hiro
380 1 hiro
void textview_show_message(TextView *textview, MimeInfo *mimeinfo,
381 1 hiro
                           const gchar *file)
382 1 hiro
{
383 1 hiro
        FILE *fp;
384 1 hiro
        const gchar *charset = NULL;
385 1 hiro
        GPtrArray *headers = NULL;
386 1 hiro
387 1 hiro
        if ((fp = fopen(file, "rb")) == NULL) {
388 1 hiro
                FILE_OP_ERROR(file, "fopen");
389 1 hiro
                return;
390 1 hiro
        }
391 1 hiro
392 1 hiro
        if (textview->messageview->forced_charset)
393 1 hiro
                charset = textview->messageview->forced_charset;
394 1 hiro
        else if (prefs_common.force_charset)
395 1 hiro
                charset = prefs_common.force_charset;
396 1 hiro
        else if (mimeinfo->charset)
397 1 hiro
                charset = mimeinfo->charset;
398 1 hiro
399 1 hiro
        textview_set_font(textview, charset);
400 1 hiro
        textview_clear(textview);
401 1 hiro
402 1 hiro
        if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0) perror("fseek");
403 144 hiro
        headers = textview_scan_header(textview, fp, charset);
404 1 hiro
        if (headers) {
405 1 hiro
                GtkTextView *text = GTK_TEXT_VIEW(textview->text);
406 1 hiro
                GtkTextBuffer *buffer;
407 1 hiro
                GtkTextIter iter;
408 1 hiro
409 1 hiro
                textview_show_header(textview, headers);
410 1 hiro
                procheader_header_array_destroy(headers);
411 1 hiro
412 1 hiro
                buffer = gtk_text_view_get_buffer(text);
413 1 hiro
                gtk_text_buffer_get_end_iter(buffer, &iter);
414 1 hiro
                textview->body_pos = gtk_text_iter_get_offset(&iter);
415 1 hiro
        }
416 1 hiro
417 1 hiro
        textview_add_parts(textview, mimeinfo, fp);
418 1 hiro
419 1 hiro
        fclose(fp);
420 1 hiro
421 1 hiro
        textview_set_position(textview, 0);
422 1 hiro
}
423 1 hiro
424 1 hiro
void textview_show_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
425 1 hiro
{
426 1 hiro
        gchar buf[BUFFSIZE];
427 1 hiro
        const gchar *boundary = NULL;
428 1 hiro
        gint boundary_len = 0;
429 1 hiro
        const gchar *charset = NULL;
430 1 hiro
        GPtrArray *headers = NULL;
431 1 hiro
        gboolean is_rfc822_part = FALSE;
432 1 hiro
433 1 hiro
        g_return_if_fail(mimeinfo != NULL);
434 1 hiro
        g_return_if_fail(fp != NULL);
435 1 hiro
436 1 hiro
        if (mimeinfo->mime_type == MIME_MULTIPART) {
437 1 hiro
                textview_clear(textview);
438 1 hiro
                textview_add_parts(textview, mimeinfo, fp);
439 1 hiro
                return;
440 1 hiro
        }
441 1 hiro
442 1 hiro
        if (mimeinfo->parent && mimeinfo->parent->boundary) {
443 1 hiro
                boundary = mimeinfo->parent->boundary;
444 1 hiro
                boundary_len = strlen(boundary);
445 1 hiro
        }
446 1 hiro
447 144 hiro
        if (textview->messageview->forced_charset)
448 144 hiro
                charset = textview->messageview->forced_charset;
449 144 hiro
        else if (prefs_common.force_charset)
450 144 hiro
                charset = prefs_common.force_charset;
451 144 hiro
        else if (mimeinfo->charset)
452 144 hiro
                charset = mimeinfo->charset;
453 144 hiro
454 1 hiro
        if (!boundary && mimeinfo->mime_type == MIME_TEXT) {
455 1 hiro
                if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
456 1 hiro
                        perror("fseek");
457 144 hiro
                headers = textview_scan_header(textview, fp, charset);
458 1 hiro
        } else {
459 1 hiro
                if (mimeinfo->mime_type == MIME_TEXT && mimeinfo->parent) {
460 1 hiro
                        glong fpos;
461 1 hiro
                        MimeInfo *parent = mimeinfo->parent;
462 1 hiro
463 1 hiro
                        while (parent->parent) {
464 1 hiro
                                if (parent->main &&
465 1 hiro
                                    parent->main->mime_type ==
466 1 hiro
                                        MIME_MESSAGE_RFC822)
467 1 hiro
                                        break;
468 1 hiro
                                parent = parent->parent;
469 1 hiro
                        }
470 1 hiro
471 1 hiro
                        if ((fpos = ftell(fp)) < 0)
472 1 hiro
                                perror("ftell");
473 1 hiro
                        else if (fseek(fp, parent->fpos, SEEK_SET) < 0)
474 1 hiro
                                perror("fseek");
475 1 hiro
                        else {
476 144 hiro
                                headers = textview_scan_header
477 144 hiro
                                        (textview, fp, charset);
478 1 hiro
                                if (fseek(fp, fpos, SEEK_SET) < 0)
479 1 hiro
                                        perror("fseek");
480 1 hiro
                        }
481 1 hiro
                }
482 1 hiro
                /* skip MIME part headers */
483 1 hiro
                while (fgets(buf, sizeof(buf), fp) != NULL)
484 1 hiro
                        if (buf[0] == '\r' || buf[0] == '\n') break;
485 1 hiro
        }
486 1 hiro
487 1 hiro
        /* display attached RFC822 single text message */
488 1 hiro
        if (mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
489 1 hiro
                if (headers) procheader_header_array_destroy(headers);
490 1 hiro
                if (!mimeinfo->sub) {
491 1 hiro
                        textview_clear(textview);
492 1 hiro
                        return;
493 1 hiro
                }
494 144 hiro
                headers = textview_scan_header(textview, fp, charset);
495 1 hiro
                mimeinfo = mimeinfo->sub;
496 1 hiro
                is_rfc822_part = TRUE;
497 1 hiro
        }
498 1 hiro
499 1 hiro
        textview_set_font(textview, charset);
500 1 hiro
501 1 hiro
        textview_clear(textview);
502 1 hiro
503 1 hiro
        if (headers) {
504 1 hiro
                GtkTextView *text = GTK_TEXT_VIEW(textview->text);
505 1 hiro
                GtkTextBuffer *buffer;
506 1 hiro
                GtkTextIter iter;
507 1 hiro
508 1 hiro
                textview_show_header(textview, headers);
509 1 hiro
                procheader_header_array_destroy(headers);
510 1 hiro
511 1 hiro
                buffer = gtk_text_view_get_buffer(text);
512 1 hiro
                gtk_text_buffer_get_end_iter(buffer, &iter);
513 1 hiro
                textview->body_pos = gtk_text_iter_get_offset(&iter);
514 1 hiro
                if (!mimeinfo->main)
515 1 hiro
                        gtk_text_buffer_insert(buffer, &iter, "\n", 1);
516 1 hiro
        }
517 1 hiro
518 1 hiro
        if (mimeinfo->mime_type == MIME_MULTIPART || is_rfc822_part)
519 1 hiro
                textview_add_parts(textview, mimeinfo, fp);
520 1 hiro
        else
521 1 hiro
                textview_write_body(textview, mimeinfo, fp, charset);
522 1 hiro
}
523 1 hiro
524 1 hiro
static void textview_add_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
525 1 hiro
{
526 1 hiro
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
527 1 hiro
        GtkTextBuffer *buffer;
528 1 hiro
        GtkTextIter iter;
529 1 hiro
        gchar buf[BUFFSIZE];
530 1 hiro
        const gchar *boundary = NULL;
531 1 hiro
        gint boundary_len = 0;
532 1 hiro
        const gchar *charset = NULL;
533 1 hiro
        GPtrArray *headers = NULL;
534 1 hiro
535 1 hiro
        g_return_if_fail(mimeinfo != NULL);
536 1 hiro
        g_return_if_fail(fp != NULL);
537 1 hiro
538 1 hiro
        buffer = gtk_text_view_get_buffer(text);
539 1 hiro
        gtk_text_buffer_get_end_iter(buffer, &iter);
540 1 hiro
541 1 hiro
        if (mimeinfo->mime_type == MIME_MULTIPART) return;
542 1 hiro
543 1 hiro
        if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0) {
544 1 hiro
                perror("fseek");
545 1 hiro
                return;
546 1 hiro
        }
547 1 hiro
548 1 hiro
        if (mimeinfo->parent && mimeinfo->parent->boundary) {
549 1 hiro
                boundary = mimeinfo->parent->boundary;
550 1 hiro
                boundary_len = strlen(boundary);
551 1 hiro
        }
552 1 hiro
553 1 hiro
        while (fgets(buf, sizeof(buf), fp) != NULL)
554 1 hiro
                if (buf[0] == '\r' || buf[0] == '\n') break;
555 1 hiro
556 144 hiro
        if (textview->messageview->forced_charset)
557 144 hiro
                charset = textview->messageview->forced_charset;
558 144 hiro
        else if (prefs_common.force_charset)
559 144 hiro
                charset = prefs_common.force_charset;
560 144 hiro
        else if (mimeinfo->charset)
561 144 hiro
                charset = mimeinfo->charset;
562 144 hiro
563 1 hiro
        if (mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
564 144 hiro
                headers = textview_scan_header(textview, fp, charset);
565 1 hiro
                if (headers) {
566 1 hiro
                        gtk_text_buffer_insert(buffer, &iter, "\n", 1);
567 1 hiro
                        textview_show_header(textview, headers);
568 1 hiro
                        procheader_header_array_destroy(headers);
569 1 hiro
                }
570 1 hiro
                return;
571 1 hiro
        }
572 1 hiro
573 1 hiro
#if USE_GPGME
574 1 hiro
        if (mimeinfo->sigstatus)
575 1 hiro
                g_snprintf(buf, sizeof(buf), "\n[%s (%s)]\n",
576 1 hiro
                           mimeinfo->content_type, mimeinfo->sigstatus);
577 1 hiro
        else
578 1 hiro
#endif
579 1 hiro
        if (mimeinfo->filename || mimeinfo->name)
580 1 hiro
                g_snprintf(buf, sizeof(buf), "\n[%s  %s (%d bytes)]\n",
581 1 hiro
                           mimeinfo->filename ? mimeinfo->filename :
582 1 hiro
                           mimeinfo->name,
583 1 hiro
                           mimeinfo->content_type, mimeinfo->size);
584 1 hiro
        else
585 1 hiro
                g_snprintf(buf, sizeof(buf), "\n[%s (%d bytes)]\n",
586 1 hiro
                           mimeinfo->content_type, mimeinfo->size);
587 1 hiro
588 1 hiro
#if USE_GPGME
589 1 hiro
        if (mimeinfo->sigstatus) {
590 1 hiro
                const gchar *color;
591 1 hiro
                if (!strcmp(mimeinfo->sigstatus, _("Good signature")))
592 1 hiro
                        color = "good-signature";
593 56 hiro
                else if (!strcmp(mimeinfo->sigstatus, _("Valid signature (untrusted key)")))
594 56 hiro
                        color = "untrusted-signature";
595 1 hiro
                else if (!strcmp(mimeinfo->sigstatus, _("BAD signature")))
596 1 hiro
                        color = "bad-signature";
597 1 hiro
                else
598 1 hiro
                        color = "nocheck-signature";
599 1 hiro
                gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, buf, -1,
600 1 hiro
                                                         color, NULL);
601 1 hiro
        } else
602 1 hiro
#endif
603 1 hiro
        if (mimeinfo->mime_type != MIME_TEXT &&
604 1 hiro
            mimeinfo->mime_type != MIME_TEXT_HTML) {
605 1 hiro
                gtk_text_buffer_insert(buffer, &iter, buf, -1);
606 97 hiro
                if (mimeinfo->mime_type == MIME_IMAGE &&
607 97 hiro
                    prefs_common.inline_image) {
608 97 hiro
                        GdkPixbuf *pixbuf;
609 97 hiro
                        GError *error = NULL;
610 97 hiro
                        gchar *filename;
611 162 hiro
                        RemoteURI *uri;
612 162 hiro
                        gchar *uri_str;
613 97 hiro
614 97 hiro
                        filename = procmime_get_tmp_file_name(mimeinfo);
615 97 hiro
                        if (procmime_get_part_fp(filename, fp, mimeinfo) < 0) {
616 97 hiro
                                g_warning("Can't get the image file.");
617 97 hiro
                                g_free(filename);
618 97 hiro
                                return;
619 97 hiro
                        }
620 97 hiro
621 97 hiro
                        pixbuf = gdk_pixbuf_new_from_file(filename, &error);
622 97 hiro
                        if (error != NULL) {
623 97 hiro
                                g_warning("%s\n", error->message);
624 97 hiro
                                g_error_free(error);
625 97 hiro
                        }
626 97 hiro
                        if (!pixbuf) {
627 97 hiro
                                g_warning("Can't load the image.");
628 162 hiro
                                g_free(filename);
629 97 hiro
                                return;
630 97 hiro
                        }
631 97 hiro
632 155 hiro
                        if (prefs_common.resize_image) {
633 155 hiro
                                GdkPixbuf *scaled;
634 155 hiro
635 155 hiro
                                scaled = imageview_get_resized_pixbuf
636 155 hiro
                                        (pixbuf, textview->text, 8);
637 155 hiro
                                g_object_unref(pixbuf);
638 155 hiro
                                pixbuf = scaled;
639 155 hiro
                        }
640 155 hiro
641 162 hiro
                        uri_str = g_filename_to_uri(filename, NULL, NULL);
642 162 hiro
                        if (uri_str) {
643 162 hiro
                                uri = g_new(RemoteURI, 1);
644 162 hiro
                                uri->uri = uri_str;
645 162 hiro
                                uri->filename =
646 162 hiro
                                        procmime_get_part_file_name(mimeinfo);
647 162 hiro
                                uri->start = gtk_text_iter_get_offset(&iter);
648 162 hiro
                                uri->end = uri->start + 1;
649 162 hiro
                                textview->uri_list =
650 162 hiro
                                        g_slist_append(textview->uri_list, uri);
651 162 hiro
                        }
652 97 hiro
                        gtk_text_buffer_insert_pixbuf(buffer, &iter, pixbuf);
653 97 hiro
                        gtk_text_buffer_insert(buffer, &iter, "\n", 1);
654 97 hiro
655 155 hiro
                        g_object_unref(pixbuf);
656 162 hiro
                        g_free(filename);
657 97 hiro
                }
658 1 hiro
        } else {
659 1 hiro
                if (!mimeinfo->main &&
660 1 hiro
                    mimeinfo->parent &&
661 1 hiro
                    mimeinfo->parent->children != mimeinfo)
662 1 hiro
                        gtk_text_buffer_insert(buffer, &iter, buf, -1);
663 1 hiro
                else
664 1 hiro
                        gtk_text_buffer_insert(buffer, &iter, "\n", 1);
665 1 hiro
                textview_write_body(textview, mimeinfo, fp, charset);
666 1 hiro
        }
667 1 hiro
}
668 1 hiro
669 1 hiro
static void textview_add_parts(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
670 1 hiro
{
671 1 hiro
        gint level;
672 1 hiro
673 1 hiro
        g_return_if_fail(mimeinfo != NULL);
674 1 hiro
        g_return_if_fail(fp != NULL);
675 1 hiro
676 1 hiro
        level = mimeinfo->level;
677 1 hiro
678 1 hiro
        for (;;) {
679 1 hiro
                textview_add_part(textview, mimeinfo, fp);
680 1 hiro
                if (mimeinfo->parent && mimeinfo->parent->content_type &&
681 1 hiro
                    !strcasecmp(mimeinfo->parent->content_type,
682 1 hiro
                                "multipart/alternative"))
683 1 hiro
                        mimeinfo = mimeinfo->parent->next;
684 1 hiro
                else
685 1 hiro
                        mimeinfo = procmime_mimeinfo_next(mimeinfo);
686 1 hiro
                if (!mimeinfo || mimeinfo->level <= level)
687 1 hiro
                        break;
688 1 hiro
        }
689 1 hiro
}
690 1 hiro
691 1 hiro
void textview_show_error(TextView *textview)
692 1 hiro
{
693 1 hiro
        GtkTextBuffer *buffer;
694 1 hiro
        GtkTextIter iter;
695 1 hiro
696 1 hiro
        textview_set_font(textview, NULL);
697 1 hiro
        textview_clear(textview);
698 1 hiro
699 1 hiro
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
700 1 hiro
        gtk_text_buffer_get_start_iter(buffer, &iter);
701 135 hiro
        gtk_text_buffer_insert(buffer, &iter,
702 135 hiro
                               _("This message can't be displayed.\n"), -1);
703 1 hiro
}
704 1 hiro
705 1 hiro
static void textview_write_body(TextView *textview, MimeInfo *mimeinfo,
706 1 hiro
                                FILE *fp, const gchar *charset)
707 1 hiro
{
708 1 hiro
        FILE *tmpfp;
709 1 hiro
        gchar buf[BUFFSIZE];
710 1 hiro
        CodeConverter *conv;
711 1 hiro
712 129 hiro
        conv = conv_code_converter_new(charset, NULL);
713 1 hiro
714 1 hiro
        tmpfp = procmime_decode_content(NULL, fp, mimeinfo);
715 1 hiro
        if (tmpfp) {
716 177 hiro
                if (mimeinfo->mime_type == MIME_TEXT_HTML &&
717 177 hiro
                    prefs_common.render_html)
718 1 hiro
                        textview_show_html(textview, tmpfp, conv);
719 1 hiro
                else
720 1 hiro
                        while (fgets(buf, sizeof(buf), tmpfp) != NULL)
721 1 hiro
                                textview_write_line(textview, buf, conv);
722 1 hiro
                fclose(tmpfp);
723 1 hiro
        }
724 1 hiro
725 1 hiro
        conv_code_converter_destroy(conv);
726 1 hiro
}
727 1 hiro
728 1 hiro
static void textview_show_html(TextView *textview, FILE *fp,
729 1 hiro
                               CodeConverter *conv)
730 1 hiro
{
731 1 hiro
        HTMLParser *parser;
732 1 hiro
        gchar *str;
733 1 hiro
734 1 hiro
        parser = html_parser_new(fp, conv);
735 1 hiro
        g_return_if_fail(parser != NULL);
736 1 hiro
737 1 hiro
        while ((str = html_parse(parser)) != NULL) {
738 1 hiro
                if (parser->href != NULL)
739 1 hiro
                        textview_write_link(textview, str, parser->href, NULL);
740 1 hiro
                else
741 1 hiro
                        textview_write_line(textview, str, NULL);
742 1 hiro
        }
743 1 hiro
        html_parser_destroy(parser);
744 1 hiro
}
745 1 hiro
746 1 hiro
/* get_uri_part() - retrieves a URI starting from scanpos.
747 1 hiro
                    Returns TRUE if succesful */
748 1 hiro
static gboolean get_uri_part(const gchar *start, const gchar *scanpos,
749 1 hiro
                             const gchar **bp, const gchar **ep)
750 1 hiro
{
751 1 hiro
        const gchar *ep_;
752 1 hiro
753 1 hiro
        g_return_val_if_fail(start != NULL, FALSE);
754 1 hiro
        g_return_val_if_fail(scanpos != NULL, FALSE);
755 1 hiro
        g_return_val_if_fail(bp != NULL, FALSE);
756 1 hiro
        g_return_val_if_fail(ep != NULL, FALSE);
757 1 hiro
758 1 hiro
        *bp = scanpos;
759 1 hiro
760 1 hiro
        /* find end point of URI */
761 1 hiro
        for (ep_ = scanpos; *ep_ != '\0'; ep_++) {
762 1 hiro
                if (!isgraph(*(const guchar *)ep_) ||
763 1 hiro
                    !isascii(*(const guchar *)ep_) ||
764 1 hiro
                    strchr("()<>\"", *ep_))
765 1 hiro
                        break;
766 1 hiro
        }
767 1 hiro
768 1 hiro
        /* no punctuation at end of string */
769 1 hiro
770 1 hiro
        /* FIXME: this stripping of trailing punctuations may bite with other URIs.
771 1 hiro
         * should pass some URI type to this function and decide on that whether
772 1 hiro
         * to perform punctuation stripping */
773 1 hiro
774 1 hiro
#define IS_REAL_PUNCT(ch)        (ispunct(ch) && ((ch) != '/'))
775 1 hiro
776 1 hiro
        for (; ep_ - 1 > scanpos + 1 &&
777 1 hiro
               IS_REAL_PUNCT(*(const guchar *)(ep_ - 1));
778 1 hiro
             ep_--)
779 1 hiro
                ;
780 1 hiro
781 1 hiro
#undef IS_REAL_PUNCT
782 1 hiro
783 1 hiro
        *ep = ep_;
784 1 hiro
785 1 hiro
        return TRUE;
786 1 hiro
}
787 1 hiro
788 1 hiro
static gchar *make_uri_string(const gchar *bp, const gchar *ep)
789 1 hiro
{
790 1 hiro
        return g_strndup(bp, ep - bp);
791 1 hiro
}
792 1 hiro
793 1 hiro
/* valid mail address characters */
794 1 hiro
#define IS_RFC822_CHAR(ch) \
795 1 hiro
        (isascii(ch) && \
796 1 hiro
         (ch) > 32   && \
797 1 hiro
         (ch) != 127 && \
798 1 hiro
         !isspace(ch) && \
799 1 hiro
         !strchr("(),;<>\"", (ch)))
800 1 hiro
801 1 hiro
/* alphabet and number within 7bit ASCII */
802 1 hiro
#define IS_ASCII_ALNUM(ch)        (isascii(ch) && isalnum(ch))
803 1 hiro
804 1 hiro
/* get_email_part() - retrieves an email address. Returns TRUE if succesful */
805 1 hiro
static gboolean get_email_part(const gchar *start, const gchar *scanpos,
806 1 hiro
                               const gchar **bp, const gchar **ep)
807 1 hiro
{
808 1 hiro
        /* more complex than the uri part because we need to scan back and forward starting from
809 1 hiro
         * the scan position. */
810 1 hiro
        gboolean result = FALSE;
811 1 hiro
        const gchar *bp_;
812 1 hiro
        const gchar *ep_;
813 1 hiro
814 1 hiro
        g_return_val_if_fail(start != NULL, FALSE);
815 1 hiro
        g_return_val_if_fail(scanpos != NULL, FALSE);
816 1 hiro
        g_return_val_if_fail(bp != NULL, FALSE);
817 1 hiro
        g_return_val_if_fail(ep != NULL, FALSE);
818 1 hiro
819 1 hiro
        /* scan start of address */
820 1 hiro
        for (bp_ = scanpos - 1;
821 1 hiro
             bp_ >= start && IS_RFC822_CHAR(*(const guchar *)bp_); bp_--)
822 1 hiro
                ;
823 1 hiro
824 1 hiro
        /* TODO: should start with an alnum? */
825 1 hiro
        bp_++;
826 1 hiro
        for (; bp_ < scanpos && !IS_ASCII_ALNUM(*(const guchar *)bp_); bp_++)
827 1 hiro
                ;
828 1 hiro
829 1 hiro
        if (bp_ != scanpos) {
830 1 hiro
                /* scan end of address */
831 1 hiro
                for (ep_ = scanpos + 1;
832 1 hiro
                     *ep_ && IS_RFC822_CHAR(*(const guchar *)ep_); ep_++)
833 1 hiro
                        ;
834 1 hiro
835 1 hiro
                /* TODO: really should terminate with an alnum? */
836 1 hiro
                for (; ep_ > scanpos && !IS_ASCII_ALNUM(*(const guchar *)ep_);
837 1 hiro
                     --ep_)
838 1 hiro
                        ;
839 1 hiro
                ep_++;
840 1 hiro
841 1 hiro
                if (ep_ > scanpos + 1) {
842 1 hiro
                        *ep = ep_;
843 1 hiro
                        *bp = bp_;
844 1 hiro
                        result = TRUE;
845 1 hiro
                }
846 1 hiro
        }
847 1 hiro
848 1 hiro
        return result;
849 1 hiro
}
850 1 hiro
851 1 hiro
#undef IS_ASCII_ALNUM
852 1 hiro
#undef IS_RFC822_CHAR
853 1 hiro
854 1 hiro
static gchar *make_email_string(const gchar *bp, const gchar *ep)
855 1 hiro
{
856 1 hiro
        /* returns a mailto: URI; mailto: is also used to detect the
857 1 hiro
         * uri type later on in the button_pressed signal handler */
858 1 hiro
        gchar *tmp;
859 1 hiro
        gchar *result;
860 1 hiro
861 1 hiro
        tmp = g_strndup(bp, ep - bp);
862 1 hiro
        result = g_strconcat("mailto:", tmp, NULL);
863 1 hiro
        g_free(tmp);
864 1 hiro
865 1 hiro
        return result;
866 1 hiro
}
867 1 hiro
868 1 hiro
#define ADD_TXT_POS(bp_, ep_, pti_) \
869 1 hiro
        if ((last->next = alloca(sizeof(struct txtpos))) != NULL) { \
870 1 hiro
                last = last->next; \
871 1 hiro
                last->bp = (bp_); last->ep = (ep_); last->pti = (pti_); \
872 1 hiro
                last->next = NULL; \
873 1 hiro
        } else { \
874 1 hiro
                g_warning("alloc error scanning URIs\n"); \
875 1 hiro
                gtk_text_buffer_insert_with_tags_by_name \
876 1 hiro
                        (buffer, &iter, linebuf, -1, fg_tag, NULL); \
877 1 hiro
                return; \
878 1 hiro
        }
879 1 hiro
880 1 hiro
/* textview_make_clickable_parts() - colorizes clickable parts */
881 1 hiro
static void textview_make_clickable_parts(TextView *textview,
882 1 hiro
                                          const gchar *fg_tag,
883 1 hiro
                                          const gchar *uri_tag,
884 1 hiro
                                          const gchar *linebuf)
885 1 hiro
{
886 1 hiro
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
887 1 hiro
        GtkTextBuffer *buffer;
888 1 hiro
        GtkTextIter iter;
889 1 hiro
890 1 hiro
        /* parse table - in order of priority */
891 1 hiro
        struct table {
892 1 hiro
                const gchar *needle; /* token */
893 1 hiro
894 1 hiro
                /* token search function */
895 1 hiro
                gchar    *(*search)        (const gchar *haystack,
896 1 hiro
                                         const gchar *needle);
897 1 hiro
                /* part parsing function */
898 1 hiro
                gboolean  (*parse)        (const gchar *start,
899 1 hiro
                                         const gchar *scanpos,
900 1 hiro
                                         const gchar **bp_,
901 1 hiro
                                         const gchar **ep_);
902 1 hiro
                /* part to URI function */
903 1 hiro
                gchar    *(*build_uri)        (const gchar *bp,
904 1 hiro
                                         const gchar *ep);
905 1 hiro
        };
906 1 hiro
907 1 hiro
        static struct table parser[] = {
908 1 hiro
                {"http://",  strcasestr, get_uri_part,   make_uri_string},
909 1 hiro
                {"https://", strcasestr, get_uri_part,   make_uri_string},
910 1 hiro
                {"ftp://",   strcasestr, get_uri_part,   make_uri_string},
911 1 hiro
                {"www.",     strcasestr, get_uri_part,   make_uri_string},
912 1 hiro
                {"mailto:",  strcasestr, get_uri_part,   make_uri_string},
913 1 hiro
                {"@",        strcasestr, get_email_part, make_email_string}
914 1 hiro
        };
915 1 hiro
        const gint PARSE_ELEMS = sizeof parser / sizeof parser[0];
916 1 hiro
917 153 hiro
        /* flags for search optimization */
918 153 hiro
        gboolean do_search[] = {TRUE, TRUE, TRUE, TRUE, TRUE, TRUE};
919 153 hiro
920 1 hiro
        gint  n;
921 1 hiro
        const gchar *walk, *bp, *ep;
922 1 hiro
923 1 hiro
        struct txtpos {
924 1 hiro
                const gchar        *bp, *ep;        /* text position */
925 1 hiro
                gint                 pti;                /* index in parse table */
926 1 hiro
                struct txtpos        *next;                /* next */
927 1 hiro
        } head = {NULL, NULL, 0,  NULL}, *last = &head;
928 1 hiro
929 1 hiro
        buffer = gtk_text_view_get_buffer(text);
930 1 hiro
        gtk_text_buffer_get_end_iter(buffer, &iter);
931 1 hiro
932 1 hiro
        /* parse for clickable parts, and build a list of begin and
933 1 hiro
           end positions  */
934 1 hiro
        for (walk = linebuf, n = 0;;) {
935 1 hiro
                gint last_index = PARSE_ELEMS;
936 153 hiro
                const gchar *scanpos = NULL;
937 1 hiro
938 1 hiro
                /* FIXME: this looks phony. scanning for anything in the
939 1 hiro
                   parse table */
940 1 hiro
                for (n = 0; n < PARSE_ELEMS; n++) {
941 153 hiro
                        const gchar *tmp;
942 1 hiro
943 153 hiro
                        if (do_search[n]) {
944 153 hiro
                                tmp = parser[n].search(walk, parser[n].needle);
945 153 hiro
                                if (tmp) {
946 153 hiro
                                        if (scanpos == NULL || tmp < scanpos) {
947 153 hiro
                                                scanpos = tmp;
948 153 hiro
                                                last_index = n;
949 153 hiro
                                        }
950 153 hiro
                                } else
951 153 hiro
                                        do_search[n] = FALSE;
952 153 hiro
                        }
953 1 hiro
                }
954 1 hiro
955 1 hiro
                if (scanpos) {
956 1 hiro
                        /* check if URI can be parsed */
957 1 hiro
                        if (parser[last_index].parse(walk, scanpos, &bp, &ep)
958 1 hiro
                            && (ep - bp - 1) > strlen(parser[last_index].needle)) {
959 1 hiro
                                        ADD_TXT_POS(bp, ep, last_index);
960 1 hiro
                                        walk = ep;
961 1 hiro
                        } else
962 1 hiro
                                walk = scanpos +
963 1 hiro
                                        strlen(parser[last_index].needle);
964 1 hiro
                } else
965 1 hiro
                        break;
966 1 hiro
        }
967 1 hiro
968 1 hiro
        /* colorize this line */
969 1 hiro
        if (head.next) {
970 1 hiro
                const gchar *normal_text = linebuf;
971 1 hiro
972 1 hiro
                /* insert URIs */
973 1 hiro
                for (last = head.next; last != NULL;
974 1 hiro
                     normal_text = last->ep, last = last->next) {
975 1 hiro
                        RemoteURI *uri;
976 1 hiro
977 1 hiro
                        uri = g_new(RemoteURI, 1);
978 1 hiro
                        if (last->bp - normal_text > 0)
979 1 hiro
                                gtk_text_buffer_insert_with_tags_by_name
980 1 hiro
                                        (buffer, &iter,
981 1 hiro
                                         normal_text,
982 1 hiro
                                         last->bp - normal_text,
983 1 hiro
                                         fg_tag, NULL);
984 1 hiro
                        uri->uri = parser[last->pti].build_uri(last->bp,
985 1 hiro
                                                               last->ep);
986 162 hiro
                        uri->filename = NULL;
987 1 hiro
                        uri->start = gtk_text_iter_get_offset(&iter);
988 1 hiro
                        gtk_text_buffer_insert_with_tags_by_name
989 1 hiro
                                (buffer, &iter, last->bp, last->ep - last->bp,
990 39 hiro
                                 uri_tag, fg_tag, NULL);
991 1 hiro
                        uri->end = gtk_text_iter_get_offset(&iter);
992 1 hiro
                        textview->uri_list =
993 1 hiro
                                g_slist_append(textview->uri_list, uri);
994 1 hiro
                }
995 1 hiro
996 1 hiro
                if (*normal_text)
997 153 hiro
                        gtkut_text_buffer_insert_with_tag_by_name
998 153 hiro
                                (buffer, &iter, normal_text, -1, fg_tag);
999 1 hiro
        } else {
1000 153 hiro
                gtkut_text_buffer_insert_with_tag_by_name
1001 153 hiro
                        (buffer, &iter, linebuf, -1, fg_tag);
1002 1 hiro
        }
1003 1 hiro
}
1004 1 hiro
1005 1 hiro
#undef ADD_TXT_POS
1006 1 hiro
1007 1 hiro
static void textview_write_line(TextView *textview, const gchar *str,
1008 1 hiro
                                CodeConverter *conv)
1009 1 hiro
{
1010 1 hiro
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1011 1 hiro
        GtkTextBuffer *buffer;
1012 1 hiro
        GtkTextIter iter;
1013 180 hiro
        gchar *buf;
1014 153 hiro
        gchar *fg_color = NULL;
1015 1 hiro
        gint quotelevel = -1;
1016 1 hiro
        gchar quote_tag_str[10];
1017 1 hiro
1018 1 hiro
        buffer = gtk_text_view_get_buffer(text);
1019 1 hiro
        gtk_text_buffer_get_end_iter(buffer, &iter);
1020 1 hiro
1021 180 hiro
        if (conv) {
1022 180 hiro
                buf = conv_convert(conv, str);
1023 180 hiro
                if (!buf)
1024 180 hiro
                        buf = conv_utf8todisp(str);
1025 180 hiro
        } else
1026 180 hiro
                buf = g_strdup(str);
1027 1 hiro
1028 1 hiro
        strcrchomp(buf);
1029 1 hiro
        //if (prefs_common.conv_mb_alnum) conv_mb_alnum(buf);
1030 1 hiro
1031 1 hiro
        /* change color of quotation
1032 1 hiro
           >, foo>, _> ... ok, <foo>, foo bar>, foo-> ... ng
1033 1 hiro
           Up to 3 levels of quotations are detected, and each
1034 1 hiro
           level is colored using a different color. */
1035 1 hiro
        if (prefs_common.enable_color && strchr(buf, '>')) {
1036 1 hiro
                quotelevel = get_quote_level(buf);
1037 1 hiro
1038 1 hiro
                /* set up the correct foreground color */
1039 1 hiro
                if (quotelevel > 2) {
1040 1 hiro
                        /* recycle colors */
1041 1 hiro
                        if (prefs_common.recycle_quote_colors)
1042 1 hiro
                                quotelevel %= 3;
1043 1 hiro
                        else
1044 1 hiro
                                quotelevel = 2;
1045 1 hiro
                }
1046 1 hiro
        }
1047 1 hiro
1048 153 hiro
        if (quotelevel != -1) {
1049 1 hiro
                g_snprintf(quote_tag_str, sizeof(quote_tag_str),
1050 1 hiro
                           "quote%d", quotelevel);
1051 1 hiro
                fg_color = quote_tag_str;
1052 1 hiro
        }
1053 1 hiro
1054 1 hiro
        if (prefs_common.enable_color)
1055 1 hiro
                textview_make_clickable_parts(textview, fg_color, "link", buf);
1056 1 hiro
        else
1057 1 hiro
                textview_make_clickable_parts(textview, fg_color, NULL, buf);
1058 180 hiro
1059 180 hiro
        g_free(buf);
1060 1 hiro
}
1061 1 hiro
1062 180 hiro
static void textview_write_link(TextView *textview, const gchar *str,
1063 180 hiro
                                const gchar *uri, CodeConverter *conv)
1064 1 hiro
{
1065 1 hiro
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1066 1 hiro
        GtkTextBuffer *buffer;
1067 1 hiro
        GtkTextIter iter;
1068 180 hiro
        gchar *buf;
1069 1 hiro
        gchar *bufp;
1070 1 hiro
        RemoteURI *r_uri;
1071 1 hiro
1072 1 hiro
        if (*str == '\0')
1073 1 hiro
                return;
1074 1 hiro
1075 1 hiro
        buffer = gtk_text_view_get_buffer(text);
1076 1 hiro
        gtk_text_buffer_get_end_iter(buffer, &iter);
1077 1 hiro
1078 180 hiro
        if (conv) {
1079 180 hiro
                buf = conv_convert(conv, str);
1080 180 hiro
                if (!buf)
1081 180 hiro
                        buf = conv_utf8todisp(str);
1082 180 hiro
        } else
1083 180 hiro
                buf = g_strdup(str);
1084 1 hiro
1085 1 hiro
        strcrchomp(buf);
1086 1 hiro
1087 180 hiro
        for (bufp = buf; g_ascii_isspace(*bufp); bufp++)
1088 180 hiro
                ;
1089 180 hiro
        if (bufp > buf)
1090 180 hiro
                gtk_text_buffer_insert(buffer, &iter, buf, bufp - buf);
1091 1 hiro
1092 1 hiro
        r_uri = g_new(RemoteURI, 1);
1093 1 hiro
        r_uri->uri = g_strdup(uri);
1094 162 hiro
        r_uri->filename = NULL;
1095 1 hiro
        r_uri->start = gtk_text_iter_get_offset(&iter);
1096 1 hiro
        gtk_text_buffer_insert_with_tags_by_name
1097 1 hiro
                (buffer, &iter, bufp, -1, "link", NULL);
1098 1 hiro
        r_uri->end = gtk_text_iter_get_offset(&iter);
1099 1 hiro
        textview->uri_list = g_slist_append(textview->uri_list, r_uri);
1100 180 hiro
1101 180 hiro
        g_free(buf);
1102 1 hiro
}
1103 1 hiro
1104 1 hiro
void textview_clear(TextView *textview)
1105 1 hiro
{
1106 1 hiro
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1107 1 hiro
        GtkTextBuffer *buffer;
1108 1 hiro
1109 1 hiro
        buffer = gtk_text_view_get_buffer(text);
1110 1 hiro
        gtk_text_buffer_set_text(buffer, "", -1);
1111 1 hiro
1112 1 hiro
        STATUSBAR_POP(textview);
1113 1 hiro
        textview_uri_list_remove_all(textview->uri_list);
1114 1 hiro
        textview->uri_list = NULL;
1115 1 hiro
1116 1 hiro
        textview->body_pos = 0;
1117 1 hiro
}
1118 1 hiro
1119 1 hiro
void textview_destroy(TextView *textview)
1120 1 hiro
{
1121 1 hiro
        textview_uri_list_remove_all(textview->uri_list);
1122 1 hiro
        textview->uri_list = NULL;
1123 1 hiro
        g_free(textview);
1124 1 hiro
}
1125 1 hiro
1126 1 hiro
void textview_set_all_headers(TextView *textview, gboolean all_headers)
1127 1 hiro
{
1128 1 hiro
        textview->show_all_headers = all_headers;
1129 1 hiro
}
1130 1 hiro
1131 1 hiro
void textview_set_font(TextView *textview, const gchar *codeset)
1132 1 hiro
{
1133 1 hiro
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1134 1 hiro
1135 1 hiro
        if (prefs_common.textfont) {
1136 1 hiro
                PangoFontDescription *font_desc;
1137 1 hiro
                font_desc = pango_font_description_from_string
1138 1 hiro
                        (prefs_common.textfont);
1139 1 hiro
                if (font_desc) {
1140 1 hiro
                        gtk_widget_modify_font(textview->text, font_desc);
1141 1 hiro
                        pango_font_description_free(font_desc);
1142 1 hiro
                }
1143 1 hiro
        }
1144 1 hiro
1145 1 hiro
        gtk_text_view_set_pixels_above_lines(text, prefs_common.line_space / 2);
1146 1 hiro
        gtk_text_view_set_pixels_below_lines(text, prefs_common.line_space / 2);
1147 1 hiro
}
1148 1 hiro
1149 1 hiro
void textview_set_position(TextView *textview, gint pos)
1150 1 hiro
{
1151 1 hiro
        GtkTextBuffer *buffer;
1152 1 hiro
        GtkTextIter iter;
1153 1 hiro
1154 1 hiro
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
1155 1 hiro
        gtk_text_buffer_get_iter_at_offset(buffer, &iter, pos);
1156 1 hiro
        gtk_text_buffer_place_cursor(buffer, &iter);
1157 1 hiro
}
1158 1 hiro
1159 144 hiro
static GPtrArray *textview_scan_header(TextView *textview, FILE *fp,
1160 144 hiro
                                       const gchar *encoding)
1161 1 hiro
{
1162 1 hiro
        gchar buf[BUFFSIZE];
1163 1 hiro
        GPtrArray *headers, *sorted_headers;
1164 1 hiro
        GSList *disphdr_list;
1165 1 hiro
        Header *header;
1166 1 hiro
        gint i;
1167 1 hiro
1168 1 hiro
        g_return_val_if_fail(fp != NULL, NULL);
1169 1 hiro
1170 1 hiro
        if (textview->show_all_headers)
1171 144 hiro
                return procheader_get_header_array_asis(fp, encoding);
1172 1 hiro
1173 1 hiro
        if (!prefs_common.display_header) {
1174 1 hiro
                while (fgets(buf, sizeof(buf), fp) != NULL)
1175 1 hiro
                        if (buf[0] == '\r' || buf[0] == '\n') break;
1176 1 hiro
                return NULL;
1177 1 hiro
        }
1178 1 hiro
1179 144 hiro
        headers = procheader_get_header_array_asis(fp, encoding);
1180 1 hiro
1181 1 hiro
        sorted_headers = g_ptr_array_new();
1182 1 hiro
1183 1 hiro
        for (disphdr_list = prefs_common.disphdr_list; disphdr_list != NULL;
1184 1 hiro
             disphdr_list = disphdr_list->next) {
1185 1 hiro
                DisplayHeaderProp *dp =
1186 1 hiro
                        (DisplayHeaderProp *)disphdr_list->data;
1187 1 hiro
1188 1 hiro
                for (i = 0; i < headers->len; i++) {
1189 1 hiro
                        header = g_ptr_array_index(headers, i);
1190 1 hiro
1191 1 hiro
                        if (!g_strcasecmp(header->name, dp->name)) {
1192 1 hiro
                                if (dp->hidden)
1193 1 hiro
                                        procheader_header_free(header);
1194 1 hiro
                                else
1195 1 hiro
                                        g_ptr_array_add(sorted_headers, header);
1196 1 hiro
1197 1 hiro
                                g_ptr_array_remove_index(headers, i);
1198 1 hiro
                                i--;
1199 1 hiro
                        }
1200 1 hiro
                }
1201 1 hiro
        }
1202 1 hiro
1203 1 hiro
        if (prefs_common.show_other_header) {
1204 1 hiro
                for (i = 0; i < headers->len; i++) {
1205 1 hiro
                        header = g_ptr_array_index(headers, i);
1206 1 hiro
                        g_ptr_array_add(sorted_headers, header);
1207 1 hiro
                }
1208 1 hiro
                g_ptr_array_free(headers, TRUE);
1209 1 hiro
        } else
1210 1 hiro
                procheader_header_array_destroy(headers);
1211 1 hiro
1212 1 hiro
1213 1 hiro
        return sorted_headers;
1214 1 hiro
}
1215 1 hiro
1216 1 hiro
static void textview_show_header(TextView *textview, GPtrArray *headers)
1217 1 hiro
{
1218 1 hiro
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1219 1 hiro
        GtkTextBuffer *buffer;
1220 1 hiro
        GtkTextIter iter;
1221 1 hiro
        Header *header;
1222 1 hiro
        gint i;
1223 1 hiro
1224 1 hiro
        g_return_if_fail(headers != NULL);
1225 1 hiro
1226 1 hiro
        buffer = gtk_text_view_get_buffer(text);
1227 1 hiro
1228 1 hiro
        for (i = 0; i < headers->len; i++) {
1229 1 hiro
                header = g_ptr_array_index(headers, i);
1230 1 hiro
                g_return_if_fail(header->name != NULL);
1231 1 hiro
1232 1 hiro
                gtk_text_buffer_get_end_iter(buffer, &iter);
1233 1 hiro
                gtk_text_buffer_insert_with_tags_by_name
1234 1 hiro
                        (buffer, &iter, header->name, -1,
1235 1 hiro
                         "header_title", "header", NULL);
1236 1 hiro
                gtk_text_buffer_insert_with_tags_by_name
1237 1 hiro
                        (buffer, &iter, ":", 1,
1238 1 hiro
                         "header_title", "header", NULL);
1239 1 hiro
1240 1 hiro
                if (!g_strcasecmp(header->name, "Subject") ||
1241 1 hiro
                    !g_strcasecmp(header->name, "From")    ||
1242 1 hiro
                    !g_strcasecmp(header->name, "To")      ||
1243 1 hiro
                    !g_strcasecmp(header->name, "Cc"))
1244 1 hiro
                        unfold_line(header->body);
1245 1 hiro
1246 1 hiro
                if (prefs_common.enable_color &&
1247 1 hiro
                    (!strncmp(header->name, "X-Mailer", 8) ||
1248 1 hiro
                     !strncmp(header->name, "X-Newsreader", 12)) &&
1249 1 hiro
                    strstr(header->body, "Sylpheed") != NULL) {
1250 1 hiro
                        gtk_text_buffer_get_end_iter(buffer, &iter);
1251 1 hiro
                        gtk_text_buffer_insert_with_tags_by_name
1252 1 hiro
                                (buffer, &iter, header->body, -1,
1253 1 hiro
                                 "header", "emphasis", NULL);
1254 1 hiro
                } else if (prefs_common.enable_color) {
1255 1 hiro
                        textview_make_clickable_parts
1256 1 hiro
                                (textview, "header", "link", header->body);
1257 1 hiro
                } else {
1258 1 hiro
                        textview_make_clickable_parts
1259 1 hiro
                                (textview, "header", NULL, header->body);
1260 1 hiro
                }
1261 1 hiro
                gtk_text_buffer_get_end_iter(buffer, &iter); //
1262 1 hiro
                gtk_text_buffer_insert_with_tags_by_name
1263 1 hiro
                        (buffer, &iter, "\n", 1, "header", NULL);
1264 1 hiro
        }
1265 1 hiro
}
1266 1 hiro
1267 1 hiro
gboolean textview_search_string(TextView *textview, const gchar *str,
1268 1 hiro
                                gboolean case_sens)
1269 1 hiro
{
1270 90 hiro
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1271 90 hiro
        GtkTextBuffer *buffer;
1272 123 hiro
        GtkTextIter iter, match_pos;
1273 90 hiro
        GtkTextMark *mark;
1274 1 hiro
        gint len;
1275 1 hiro
1276 1 hiro
        g_return_val_if_fail(str != NULL, FALSE);
1277 1 hiro
1278 90 hiro
        buffer = gtk_text_view_get_buffer(text);
1279 90 hiro
1280 90 hiro
        len = g_utf8_strlen(str, -1);
1281 1 hiro
        g_return_val_if_fail(len >= 0, FALSE);
1282 1 hiro
1283 90 hiro
        mark = gtk_text_buffer_get_insert(buffer);
1284 90 hiro
        gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
1285 1 hiro
1286 123 hiro
        if (gtkut_text_buffer_find(buffer, &iter, str, case_sens,
1287 123 hiro
                                   &match_pos)) {
1288 123 hiro
                GtkTextIter end = match_pos;
1289 123 hiro
1290 123 hiro
                gtk_text_iter_forward_chars(&end, len);
1291 123 hiro
                /* place "insert" at the last character */
1292 123 hiro
                gtk_text_buffer_select_range(buffer, &end, &match_pos);
1293 90 hiro
                gtk_text_view_scroll_to_mark(text, mark, 0.0, FALSE, 0.0, 0.0);
1294 1 hiro
                return TRUE;
1295 1 hiro
        }
1296 1 hiro
1297 1 hiro
        return FALSE;
1298 1 hiro
}
1299 1 hiro
1300 1 hiro
gboolean textview_search_string_backward(TextView *textview, const gchar *str,
1301 1 hiro
                                         gboolean case_sens)
1302 1 hiro
{
1303 90 hiro
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1304 90 hiro
        GtkTextBuffer *buffer;
1305 123 hiro
        GtkTextIter iter, match_pos;
1306 90 hiro
        GtkTextMark *mark;
1307 1 hiro
        gint len;
1308 1 hiro
1309 1 hiro
        g_return_val_if_fail(str != NULL, FALSE);
1310 1 hiro
1311 90 hiro
        buffer = gtk_text_view_get_buffer(text);
1312 90 hiro
1313 123 hiro
        len = g_utf8_strlen(str, -1);
1314 123 hiro
        g_return_val_if_fail(len >= 0, FALSE);
1315 1 hiro
1316 90 hiro
        mark = gtk_text_buffer_get_insert(buffer);
1317 90 hiro
        gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
1318 90 hiro
1319 123 hiro
        if (gtkut_text_buffer_find_backward(buffer, &iter, str, case_sens,
1320 123 hiro
                                            &match_pos)) {
1321 123 hiro
                GtkTextIter end = match_pos;
1322 123 hiro
1323 123 hiro
                gtk_text_iter_forward_chars(&end, len);
1324 123 hiro
                gtk_text_buffer_select_range(buffer, &match_pos, &end);
1325 123 hiro
                gtk_text_view_scroll_to_mark(text, mark, 0.0, FALSE, 0.0, 0.0);
1326 123 hiro
                return TRUE;
1327 1 hiro
        }
1328 1 hiro
1329 123 hiro
        return FALSE;
1330 1 hiro
}
1331 1 hiro
1332 1 hiro
void textview_scroll_one_line(TextView *textview, gboolean up)
1333 1 hiro
{
1334 1 hiro
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1335 1 hiro
        GtkAdjustment *vadj = text->vadjustment;
1336 1 hiro
        gfloat upper;
1337 1 hiro
1338 1 hiro
        if (prefs_common.enable_smooth_scroll) {
1339 1 hiro
                textview_smooth_scroll_one_line(textview, up);
1340 1 hiro
                return;
1341 1 hiro
        }
1342 1 hiro
1343 1 hiro
        if (!up) {
1344 1 hiro
                upper = vadj->upper - vadj->page_size;
1345 1 hiro
                if (vadj->value < upper) {
1346 7 hiro
                        vadj->value += vadj->step_increment;
1347 1 hiro
                        vadj->value = MIN(vadj->value, upper);
1348 1 hiro
                        g_signal_emit_by_name(G_OBJECT(vadj),
1349 1 hiro
                                              "value_changed", 0);
1350 1 hiro
                }
1351 1 hiro
        } else {
1352 1 hiro
                if (vadj->value > 0.0) {
1353 7 hiro
                        vadj->value -= vadj->step_increment;
1354 1 hiro
                        vadj->value = MAX(vadj->value, 0.0);
1355 1 hiro
                        g_signal_emit_by_name(G_OBJECT(vadj),
1356 1 hiro
                                              "value_changed", 0);
1357 1 hiro
                }
1358 1 hiro
        }
1359 1 hiro
}
1360 1 hiro
1361 1 hiro
gboolean textview_scroll_page(TextView *textview, gboolean up)
1362 1 hiro
{
1363 1 hiro
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1364 1 hiro
        GtkAdjustment *vadj = text->vadjustment;
1365 1 hiro
        gfloat upper;
1366 1 hiro
        gfloat page_incr;
1367 1 hiro
1368 1 hiro
        if (prefs_common.enable_smooth_scroll)
1369 1 hiro
                return textview_smooth_scroll_page(textview, up);
1370 1 hiro
1371 1 hiro
        if (prefs_common.scroll_halfpage)
1372 1 hiro
                page_incr = vadj->page_increment / 2;
1373 1 hiro
        else
1374 1 hiro
                page_incr = vadj->page_increment;
1375 1 hiro
1376 1 hiro
        if (!up) {
1377 1 hiro
                upper = vadj->upper - vadj->page_size;
1378 1 hiro
                if (vadj->value < upper) {
1379 1 hiro
                        vadj->value += page_incr;
1380 1 hiro
                        vadj->value = MIN(vadj->value, upper);
1381 1 hiro
                        g_signal_emit_by_name(G_OBJECT(vadj),
1382 1 hiro
                                              "value_changed", 0);
1383 1 hiro
                } else
1384 1 hiro
                        return FALSE;
1385 1 hiro
        } else {
1386 1 hiro
                if (vadj->value > 0.0) {
1387 1 hiro
                        vadj->value -= page_incr;
1388 1 hiro
                        vadj->value = MAX(vadj->value, 0.0);
1389 1 hiro
                        g_signal_emit_by_name(G_OBJECT(vadj),
1390 1 hiro
                                              "value_changed", 0);
1391 1 hiro
                } else
1392 1 hiro
                        return FALSE;
1393 1 hiro
        }
1394 1 hiro
1395 1 hiro
        return TRUE;
1396 1 hiro
}
1397 1 hiro
1398 1 hiro
static void textview_smooth_scroll_do(TextView *textview,
1399 1 hiro
                                      gfloat old_value, gfloat last_value,
1400 1 hiro
                                      gint step)
1401 1 hiro
{
1402 1 hiro
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1403 1 hiro
        GtkAdjustment *vadj = text->vadjustment;
1404 1 hiro
        gint change_value;
1405 1 hiro
        gboolean up;
1406 1 hiro
        gint i;
1407 1 hiro
1408 1 hiro
        if (old_value < last_value) {
1409 1 hiro
                change_value = last_value - old_value;
1410 1 hiro
                up = FALSE;
1411 1 hiro
        } else {
1412 1 hiro
                change_value = old_value - last_value;
1413 1 hiro
                up = TRUE;
1414 1 hiro
        }
1415 1 hiro
1416 1 hiro
        /* gdk_key_repeat_disable(); */
1417 1 hiro
1418 1 hiro
        for (i = step; i <= change_value; i += step) {
1419 1 hiro
                vadj->value = old_value + (up ? -i : i);
1420 1 hiro
                g_signal_emit_by_name(G_OBJECT(vadj), "value_changed", 0);
1421 1 hiro
        }
1422 1 hiro
1423 1 hiro
        vadj->value = last_value;
1424 1 hiro
        g_signal_emit_by_name(G_OBJECT(vadj), "value_changed", 0);
1425 1 hiro
1426 1 hiro
        /* gdk_key_repeat_restore(); */
1427 1 hiro
}
1428 1 hiro
1429 1 hiro
static void textview_smooth_scroll_one_line(TextView *textview, gboolean up)
1430 1 hiro
{
1431 1 hiro
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1432 1 hiro
        GtkAdjustment *vadj = text->vadjustment;
1433 1 hiro
        gfloat upper;
1434 1 hiro
        gfloat old_value;
1435 1 hiro
        gfloat last_value;
1436 1 hiro
1437 1 hiro
        if (!up) {
1438 1 hiro
                upper = vadj->upper - vadj->page_size;
1439 1 hiro
                if (vadj->value < upper) {
1440 1 hiro
                        old_value = vadj->value;
1441 7 hiro
                        last_value = vadj->value + vadj->step_increment;
1442 1 hiro
                        last_value = MIN(last_value, upper);
1443 1 hiro
1444 1 hiro
                        textview_smooth_scroll_do(textview, old_value,
1445 1 hiro
                                                  last_value,
1446 1 hiro
                                                  prefs_common.scroll_step);
1447 1 hiro
                }
1448 1 hiro
        } else {
1449 1 hiro
                if (vadj->value > 0.0) {
1450 1 hiro
                        old_value = vadj->value;
1451 7 hiro
                        last_value = vadj->value - vadj->step_increment;
1452 1 hiro
                        last_value = MAX(last_value, 0.0);
1453 1 hiro
1454 1 hiro
                        textview_smooth_scroll_do(textview, old_value,
1455 1 hiro
                                                  last_value,
1456 1 hiro
                                                  prefs_common.scroll_step);
1457 1 hiro
                }
1458 1 hiro
        }
1459 1 hiro
}
1460 1 hiro
1461 1 hiro
static gboolean textview_smooth_scroll_page(TextView *textview, gboolean up)
1462 1 hiro
{
1463 1 hiro
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1464 1 hiro
        GtkAdjustment *vadj = text->vadjustment;
1465 1 hiro
        gfloat upper;
1466 1 hiro
        gfloat page_incr;
1467 1 hiro
        gfloat old_value;
1468 1 hiro
        gfloat last_value;
1469 1 hiro
1470 1 hiro
        if (prefs_common.scroll_halfpage)
1471 1 hiro
                page_incr = vadj->page_increment / 2;
1472 1 hiro
        else
1473 1 hiro
                page_incr = vadj->page_increment;
1474 1 hiro
1475 1 hiro
        if (!up) {
1476 1 hiro
                upper = vadj->upper - vadj->page_size;
1477 1 hiro
                if (vadj->value < upper) {
1478 1 hiro
                        old_value = vadj->value;
1479 1 hiro
                        last_value = vadj->value + page_incr;
1480 1 hiro
                        last_value = MIN(last_value, upper);
1481 1 hiro
1482 1 hiro
                        textview_smooth_scroll_do(textview, old_value,
1483 1 hiro
                                                  last_value,
1484 1 hiro
                                                  prefs_common.scroll_step);
1485 1 hiro
                } else
1486 1 hiro
                        return FALSE;
1487 1 hiro
        } else {
1488 1 hiro
                if (vadj->value > 0.0) {
1489 1 hiro
                        old_value = vadj->value;
1490 1 hiro
                        last_value = vadj->value - page_incr;
1491 1 hiro
                        last_value = MAX(last_value, 0.0);
1492 1 hiro
1493 1 hiro
                        textview_smooth_scroll_do(textview, old_value,
1494 1 hiro
                                                  last_value,
1495 1 hiro
                                                  prefs_common.scroll_step);
1496 1 hiro
                } else
1497 1 hiro
                        return FALSE;
1498 1 hiro
        }
1499 1 hiro
1500 1 hiro
        return TRUE;
1501 1 hiro
}
1502 1 hiro
1503 1 hiro
#warning FIXME_GTK2
1504 1 hiro
#if 0
1505 1 hiro
#define KEY_PRESS_EVENT_STOP() \
1506 1 hiro
        if (gtk_signal_n_emissions_by_name \
1507 101 hiro
                (GTK_OBJECT(widget), "key-press-event") > 0) { \
1508 1 hiro
                gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), \
1509 101 hiro
                                             "key-press-event"); \
1510 1 hiro
        }
1511 1 hiro
#else
1512 1 hiro
#define KEY_PRESS_EVENT_STOP() \
1513 101 hiro
        g_signal_stop_emission_by_name(G_OBJECT(widget), "key-press-event");
1514 1 hiro
#endif
1515 1 hiro
1516 1 hiro
static gboolean textview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1517 1 hiro
                                     TextView *textview)
1518 1 hiro
{
1519 1 hiro
        SummaryView *summaryview = NULL;
1520 1 hiro
        MessageView *messageview = textview->messageview;
1521 1 hiro
1522 1 hiro
        if (!event) return FALSE;
1523 1 hiro
        if (messageview->mainwin)
1524 1 hiro
                summaryview = messageview->mainwin->summaryview;
1525 1 hiro
1526 1 hiro
        switch (event->keyval) {
1527 1 hiro
        case GDK_Tab:
1528 1 hiro
        case GDK_Home:
1529 1 hiro
        case GDK_Left:
1530 1 hiro
        case GDK_Up:
1531 1 hiro
        case GDK_Right:
1532 1 hiro
        case GDK_Down:
1533 1 hiro
        case GDK_Page_Up:
1534 1 hiro
        case GDK_Page_Down:
1535 1 hiro
        case GDK_End:
1536 1 hiro
        case GDK_Control_L:
1537 1 hiro
        case GDK_Control_R:
1538 1 hiro
                break;
1539 1 hiro
        case GDK_space:
1540 1 hiro
                if (summaryview)
1541 1 hiro
                        summary_pass_key_press_event(summaryview, event);
1542 1 hiro
                else
1543 1 hiro
                        textview_scroll_page
1544 1 hiro
                                (textview,
1545 1 hiro
                                 (event->state &
1546 1 hiro
                                  (GDK_SHIFT_MASK|GDK_MOD1_MASK)) != 0);
1547 1 hiro
                break;
1548 1 hiro
        case GDK_BackSpace:
1549 1 hiro
                textview_scroll_page(textview, TRUE);
1550 1 hiro
                break;
1551 1 hiro
        case GDK_Return:
1552 1 hiro
                textview_scroll_one_line
1553 1 hiro
                        (textview, (event->state &
1554 1 hiro
                                    (GDK_SHIFT_MASK|GDK_MOD1_MASK)) != 0);
1555 1 hiro
                break;
1556 1 hiro
        case GDK_Delete:
1557 1 hiro
                if (summaryview)
1558 1 hiro
                        summary_pass_key_press_event(summaryview, event);
1559 1 hiro
                break;
1560 165 hiro
        case GDK_Escape:
1561 165 hiro
                if (summaryview && textview == messageview->textview)
1562 165 hiro
                        gtk_widget_grab_focus(summaryview->ctree);
1563 165 hiro
                else if (messageview->type == MVIEW_MIME &&
1564 165 hiro
                         textview == messageview->mimeview->textview)
1565 165 hiro
                        gtk_widget_grab_focus(messageview->mimeview->ctree);
1566 165 hiro
                break;
1567 1 hiro
        case GDK_n:
1568 1 hiro
        case GDK_N:
1569 1 hiro
        case GDK_p:
1570 1 hiro
        case GDK_P:
1571 1 hiro
        case GDK_y:
1572 1 hiro
        case GDK_t:
1573 1 hiro
        case GDK_l:
1574 1 hiro
                if (messageview->type == MVIEW_MIME &&
1575 1 hiro
                    textview == messageview->mimeview->textview) {
1576 1 hiro
                        KEY_PRESS_EVENT_STOP();
1577 1 hiro
                        mimeview_pass_key_press_event(messageview->mimeview,
1578 1 hiro
                                                      event);
1579 1 hiro
                        break;
1580 1 hiro
                }
1581 1 hiro
                /* fall through */
1582 1 hiro
        default:
1583 1 hiro
                if (summaryview &&
1584 1 hiro
                    event->window != messageview->mainwin->window->window) {
1585 1 hiro
                        GdkEventKey tmpev = *event;
1586 1 hiro
1587 1 hiro
                        tmpev.window = messageview->mainwin->window->window;
1588 1 hiro
                        KEY_PRESS_EVENT_STOP();
1589 1 hiro
                        gtk_widget_event(messageview->mainwin->window,
1590 1 hiro
                                         (GdkEvent *)&tmpev);
1591 1 hiro
                }
1592 1 hiro
                break;
1593 1 hiro
        }
1594 1 hiro
1595 1 hiro
        return FALSE;
1596 1 hiro
}
1597 1 hiro
1598 101 hiro
static gboolean textview_get_link_tag_bounds(TextView *textview,
1599 101 hiro
                                             GtkTextIter *iter,
1600 101 hiro
                                             GtkTextIter *start,
1601 101 hiro
                                             GtkTextIter *end)
1602 1 hiro
{
1603 101 hiro
        GSList *tags, *cur;
1604 101 hiro
        gboolean on_link = FALSE;
1605 1 hiro
1606 101 hiro
        tags = gtk_text_iter_get_tags(iter);
1607 101 hiro
        *start = *end = *iter;
1608 1 hiro
1609 101 hiro
        for (cur = tags; cur != NULL; cur = cur->next) {
1610 101 hiro
                GtkTextTag *tag = cur->data;
1611 101 hiro
                gchar *tag_name;
1612 1 hiro
1613 101 hiro
                g_object_get(G_OBJECT(tag), "name", &tag_name, NULL);
1614 101 hiro
                if (tag_name && !strcmp(tag_name, "link")) {
1615 101 hiro
                        if (!gtk_text_iter_begins_tag(start, tag))
1616 101 hiro
                                gtk_text_iter_backward_to_tag_toggle
1617 101 hiro
                                        (start, tag);
1618 101 hiro
                        if (!gtk_text_iter_ends_tag(end, tag))
1619 101 hiro
                                gtk_text_iter_forward_to_tag_toggle(end, tag);
1620 101 hiro
                        on_link = TRUE;
1621 101 hiro
                        g_free(tag_name);
1622 101 hiro
                        break;
1623 101 hiro
                }
1624 102 hiro
                if (tag_name)
1625 102 hiro
                        g_free(tag_name);
1626 1 hiro
        }
1627 1 hiro
1628 101 hiro
        if (tags)
1629 101 hiro
                g_slist_free(tags);
1630 1 hiro
1631 101 hiro
        return on_link;
1632 101 hiro
}
1633 101 hiro
1634 101 hiro
static RemoteURI *textview_get_uri(TextView *textview, GtkTextIter *start,
1635 101 hiro
                                   GtkTextIter *end)
1636 101 hiro
{
1637 101 hiro
        gint start_pos, end_pos;
1638 101 hiro
        GSList *cur;
1639 101 hiro
        RemoteURI *uri = NULL;
1640 101 hiro
1641 101 hiro
        start_pos = gtk_text_iter_get_offset(start);
1642 101 hiro
        end_pos = gtk_text_iter_get_offset(end);
1643 101 hiro
1644 1 hiro
        for (cur = textview->uri_list; cur != NULL; cur = cur->next) {
1645 1 hiro
                RemoteURI *uri_ = (RemoteURI *)cur->data;
1646 1 hiro
1647 1 hiro
                if (start_pos == uri_->start && end_pos == uri_->end) {
1648 1 hiro
                        uri = uri_;
1649 1 hiro
                        break;
1650 1 hiro
                }
1651 1 hiro
        }
1652 1 hiro
1653 101 hiro
        return uri;
1654 101 hiro
}
1655 1 hiro
1656 101 hiro
static gboolean textview_event_after(GtkWidget *widget, GdkEvent *event,
1657 101 hiro
                                     TextView *textview)
1658 101 hiro
{
1659 101 hiro
        GdkEventButton *bevent = (GdkEventButton *)event;
1660 101 hiro
        GtkTextBuffer *buffer;
1661 101 hiro
        GtkTextIter iter, start, end;
1662 101 hiro
        gint x, y;
1663 101 hiro
        gboolean on_link;
1664 101 hiro
        RemoteURI *uri;
1665 101 hiro
1666 101 hiro
        if (event->type != GDK_BUTTON_RELEASE)
1667 101 hiro
                return FALSE;
1668 101 hiro
        if (bevent->button != 1 && bevent->button != 2)
1669 101 hiro
                return FALSE;
1670 101 hiro
1671 101 hiro
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(widget));
1672 101 hiro
1673 101 hiro
        /* don't follow a link if the user has selected something */
1674 101 hiro
        gtk_text_buffer_get_selection_bounds(buffer, &start, &end);
1675 101 hiro
        if (!gtk_text_iter_equal(&start, &end))
1676 101 hiro
                return FALSE;
1677 101 hiro
1678 101 hiro
        gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(widget),
1679 101 hiro
                                              GTK_TEXT_WINDOW_WIDGET,
1680 101 hiro
                                              bevent->x, bevent->y, &x, &y);
1681 101 hiro
        gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(widget), &iter, x, y);
1682 101 hiro
        on_link = textview_get_link_tag_bounds(textview, &iter, &start, &end);
1683 101 hiro
        if (!on_link)
1684 101 hiro
                return FALSE;
1685 101 hiro
1686 101 hiro
        uri = textview_get_uri(textview, &start, &end);
1687 1 hiro
        if (!uri)
1688 1 hiro
                return FALSE;
1689 1 hiro
1690 101 hiro
        if (!g_strncasecmp(uri->uri, "mailto:", 7)) {
1691 101 hiro
                PrefsAccount *ac = NULL;
1692 101 hiro
                MsgInfo *msginfo = textview->messageview->msginfo;
1693 1 hiro
1694 101 hiro
                if (msginfo && msginfo->folder)
1695 101 hiro
                        ac = account_find_from_item(msginfo->folder);
1696 101 hiro
                if (ac && ac->protocol == A_NNTP)
1697 101 hiro
                        ac = NULL;
1698 101 hiro
                compose_new(ac, msginfo->folder, uri->uri + 7, NULL);
1699 101 hiro
        } else if (textview_uri_security_check(textview, uri) == TRUE)
1700 101 hiro
                open_uri(uri->uri, prefs_common.uri_cmd);
1701 1 hiro
1702 101 hiro
        return FALSE;
1703 101 hiro
}
1704 101 hiro
1705 101 hiro
static void textview_show_uri(TextView *textview, GtkTextIter *start,
1706 101 hiro
                              GtkTextIter *end)
1707 101 hiro
{
1708 101 hiro
        RemoteURI *uri;
1709 101 hiro
1710 101 hiro
        STATUSBAR_POP(textview);
1711 101 hiro
        uri = textview_get_uri(textview, start, end);
1712 101 hiro
        if (uri) {
1713 101 hiro
                gchar *trimmed_uri;
1714 101 hiro
1715 101 hiro
                trimmed_uri = trim_string(uri->uri, 60);
1716 101 hiro
                STATUSBAR_PUSH(textview, trimmed_uri);
1717 101 hiro
                g_free(trimmed_uri);
1718 101 hiro
        }
1719 101 hiro
}
1720 101 hiro
1721 101 hiro
static void textview_set_cursor(TextView *textview, GtkTextView *text,
1722 101 hiro
                                gint x, gint y)
1723 101 hiro
{
1724 101 hiro
        GtkTextBuffer *buffer;
1725 101 hiro
        GtkTextIter iter;
1726 101 hiro
        GtkTextIter start, end;
1727 101 hiro
        GtkTextMark *start_mark, *end_mark;
1728 101 hiro
        gboolean on_link = FALSE;
1729 101 hiro
1730 101 hiro
        buffer = gtk_text_view_get_buffer(text);
1731 101 hiro
        gtk_text_view_get_iter_at_location(text, &iter, x, y);
1732 101 hiro
        on_link = textview_get_link_tag_bounds(textview, &iter, &start, &end);
1733 101 hiro
1734 101 hiro
        start_mark = gtk_text_buffer_get_mark(buffer, "hover-link-start");
1735 101 hiro
        end_mark = gtk_text_buffer_get_mark(buffer, "hover-link-end");
1736 101 hiro
        if (start_mark) {
1737 101 hiro
                GtkTextIter prev_start, prev_end;
1738 101 hiro
1739 101 hiro
                gtk_text_buffer_get_iter_at_mark(buffer, &prev_start,
1740 101 hiro
                                                 start_mark);
1741 101 hiro
                gtk_text_buffer_get_iter_at_mark(buffer, &prev_end, end_mark);
1742 101 hiro
                if (on_link) {
1743 101 hiro
                        if (gtk_text_iter_equal(&prev_start, &start))
1744 101 hiro
                                return;
1745 1 hiro
                }
1746 101 hiro
1747 101 hiro
                gtk_text_buffer_get_iter_at_mark(buffer, &prev_start,
1748 101 hiro
                                                 start_mark);
1749 101 hiro
                gtk_text_buffer_get_iter_at_mark(buffer, &prev_end, end_mark);
1750 101 hiro
                gtk_text_buffer_remove_tag_by_name(buffer, "hover-link",
1751 101 hiro
                                                   &prev_start, &prev_end);
1752 101 hiro
                gtk_text_buffer_delete_mark(buffer, start_mark);
1753 101 hiro
                gtk_text_buffer_delete_mark(buffer, end_mark);
1754 101 hiro
        } else {
1755 101 hiro
                if (!on_link)
1756 101 hiro
                        return;
1757 1 hiro
        }
1758 1 hiro
1759 101 hiro
        if (on_link) {
1760 101 hiro
                gtk_text_buffer_create_mark
1761 101 hiro
                        (buffer, "hover-link-start", &start, FALSE);
1762 101 hiro
                gtk_text_buffer_create_mark
1763 101 hiro
                        (buffer, "hover-link-end", &end, FALSE);
1764 101 hiro
                gtk_text_buffer_apply_tag_by_name
1765 101 hiro
                        (buffer, "hover-link", &start, &end);
1766 101 hiro
                gdk_window_set_cursor
1767 101 hiro
                        (gtk_text_view_get_window(text, GTK_TEXT_WINDOW_TEXT),
1768 101 hiro
                         hand_cursor);
1769 101 hiro
                textview_show_uri(textview, &start, &end);
1770 101 hiro
        } else {
1771 101 hiro
                gdk_window_set_cursor
1772 101 hiro
                        (gtk_text_view_get_window(text, GTK_TEXT_WINDOW_TEXT),
1773 101 hiro
                         regular_cursor);
1774 101 hiro
                STATUSBAR_POP(textview);
1775 101 hiro
        }
1776 101 hiro
}
1777 101 hiro
1778 101 hiro
static gboolean textview_motion_notify(GtkWidget *widget,
1779 101 hiro
                                       GdkEventMotion *event,
1780 101 hiro
                                       TextView *textview)
1781 101 hiro
{
1782 101 hiro
        gint x, y;
1783 101 hiro
1784 101 hiro
        gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(widget),
1785 101 hiro
                                              GTK_TEXT_WINDOW_WIDGET,
1786 101 hiro
                                              event->x, event->y, &x, &y);
1787 101 hiro
        textview_set_cursor(textview, GTK_TEXT_VIEW(widget), x, y);
1788 101 hiro
        gdk_window_get_pointer(widget->window, NULL, NULL, NULL);
1789 101 hiro
1790 1 hiro
        return FALSE;
1791 1 hiro
}
1792 1 hiro
1793 113 hiro
static gboolean textview_leave_notify(GtkWidget *widget,
1794 113 hiro
                                      GdkEventCrossing *event,
1795 113 hiro
                                      TextView *textview)
1796 113 hiro
{
1797 113 hiro
        textview_set_cursor(textview, GTK_TEXT_VIEW(widget), 0, 0);
1798 113 hiro
1799 113 hiro
        return FALSE;
1800 113 hiro
}
1801 113 hiro
1802 101 hiro
static gboolean textview_visibility_notify(GtkWidget *widget,
1803 101 hiro
                                           GdkEventVisibility *event,
1804 101 hiro
                                           TextView *textview)
1805 101 hiro
{
1806 101 hiro
        gint wx, wy, bx, by;
1807 113 hiro
        GdkWindow *window;
1808 101 hiro
1809 113 hiro
        window = gtk_text_view_get_window(GTK_TEXT_VIEW(widget),
1810 113 hiro
                                          GTK_TEXT_WINDOW_TEXT);
1811 113 hiro
1812 113 hiro
        /* check if the event occurred for the text window part */
1813 113 hiro
        if (window != event->window)
1814 113 hiro
                return FALSE;
1815 113 hiro
1816 101 hiro
        gdk_window_get_pointer(widget->window, &wx, &wy, NULL);
1817 101 hiro
        gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(widget),
1818 101 hiro
                                              GTK_TEXT_WINDOW_WIDGET,
1819 101 hiro
                                              wx, wy, &bx, &by);
1820 101 hiro
        textview_set_cursor(textview, GTK_TEXT_VIEW(widget), bx, by);
1821 101 hiro
1822 101 hiro
        return FALSE;
1823 101 hiro
}
1824 101 hiro
1825 116 hiro
static void textview_populate_popup(GtkWidget *widget, GtkMenu *menu,
1826 116 hiro
                                    TextView *textview)
1827 116 hiro
{
1828 116 hiro
        gint px, py, x, y;
1829 116 hiro
        GtkWidget *separator, *menuitem;
1830 116 hiro
        GtkTextBuffer *buffer;
1831 116 hiro
        GtkTextIter iter, start, end;
1832 116 hiro
        gboolean on_link;
1833 116 hiro
        RemoteURI *uri;
1834 162 hiro
        GdkPixbuf *pixbuf;
1835 116 hiro
1836 116 hiro
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(widget));
1837 116 hiro
1838 116 hiro
        gdk_window_get_pointer(widget->window, &px, &py, NULL);
1839 116 hiro
        gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(widget),
1840 116 hiro
                                              GTK_TEXT_WINDOW_WIDGET,
1841 116 hiro
                                              px, py, &x, &y);
1842 116 hiro
        gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(widget), &iter, x, y);
1843 162 hiro
        if ((pixbuf = gtk_text_iter_get_pixbuf(&iter)) != NULL) {
1844 162 hiro
                start = end = iter;
1845 162 hiro
                gtk_text_iter_forward_char(&end);
1846 162 hiro
                uri = textview_get_uri(textview, &start, &end);
1847 162 hiro
1848 162 hiro
                separator = gtk_separator_menu_item_new();
1849 162 hiro
                gtk_menu_shell_append(GTK_MENU_SHELL(menu), separator);
1850 162 hiro
                gtk_widget_show(separator);
1851 162 hiro
1852 162 hiro
                menuitem = gtk_menu_item_new_with_mnemonic
1853 162 hiro
                        (_("Sa_ve this image as..."));
1854 162 hiro
                g_signal_connect
1855 162 hiro
                        (menuitem, "activate",
1856 162 hiro
                         G_CALLBACK(textview_popup_menu_activate_image_cb),
1857 162 hiro
                         uri);
1858 162 hiro
                gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
1859 162 hiro
                gtk_widget_show(menuitem);
1860 162 hiro
        }
1861 116 hiro
        on_link = textview_get_link_tag_bounds(textview, &iter, &start, &end);
1862 116 hiro
        if (!on_link)
1863 116 hiro
                return;
1864 116 hiro
1865 116 hiro
        uri = textview_get_uri(textview, &start, &end);
1866 116 hiro
        if (!uri)
1867 116 hiro
                return;
1868 116 hiro
1869 116 hiro
        separator = gtk_separator_menu_item_new();
1870 116 hiro
        gtk_menu_shell_append(GTK_MENU_SHELL(menu), separator);
1871 116 hiro
        gtk_widget_show(separator);
1872 116 hiro
1873 116 hiro
        menuitem = gtk_menu_item_new_with_mnemonic(_("Copy this _link"));
1874 116 hiro
        g_signal_connect(menuitem, "activate",
1875 116 hiro
                         G_CALLBACK(textview_popup_menu_activate_copy_cb), uri);
1876 116 hiro
        gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
1877 116 hiro
        gtk_widget_show(menuitem);
1878 116 hiro
}
1879 116 hiro
1880 116 hiro
static void textview_popup_menu_activate_copy_cb(GtkMenuItem *menuitem,
1881 116 hiro
                                                 gpointer data)
1882 116 hiro
{
1883 116 hiro
        RemoteURI *uri = (RemoteURI *)data;
1884 116 hiro
        const gchar *uri_string;
1885 116 hiro
        GtkClipboard *clipboard;
1886 116 hiro
1887 116 hiro
        g_return_if_fail(uri != NULL);
1888 116 hiro
1889 116 hiro
        if (!uri->uri)
1890 116 hiro
                return;
1891 116 hiro
1892 116 hiro
        if (!g_strncasecmp(uri->uri, "mailto:", 7))
1893 116 hiro
                uri_string = uri->uri + 7;
1894 116 hiro
        else
1895 116 hiro
                uri_string = uri->uri;
1896 116 hiro
1897 116 hiro
        clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1898 116 hiro
        gtk_clipboard_set_text(clipboard, uri_string, -1);
1899 116 hiro
        clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
1900 116 hiro
        gtk_clipboard_set_text(clipboard, uri_string, -1);
1901 116 hiro
}
1902 116 hiro
1903 162 hiro
static void textview_popup_menu_activate_image_cb(GtkMenuItem *menuitem,
1904 162 hiro
                                                  gpointer data)
1905 162 hiro
{
1906 162 hiro
        RemoteURI *uri = (RemoteURI *)data;
1907 162 hiro
        gchar *src;
1908 162 hiro
        gchar *dest;
1909 162 hiro
        gchar *filename;
1910 162 hiro
1911 162 hiro
        g_return_if_fail(uri != NULL);
1912 162 hiro
1913 162 hiro
        if (!uri->uri)
1914 162 hiro
                return;
1915 162 hiro
1916 162 hiro
        src = g_filename_from_uri(uri->uri, NULL, NULL);
1917 162 hiro
        g_return_if_fail(src != NULL);
1918 162 hiro
1919 162 hiro
        filename = conv_filename_to_utf8(uri->filename);
1920 162 hiro
        dest = filesel_save_as(filename);
1921 162 hiro
        if (dest) {
1922 162 hiro
                copy_file(src, dest, FALSE);
1923 162 hiro
                g_free(dest);
1924 162 hiro
        }
1925 162 hiro
        g_free(filename);
1926 162 hiro
        g_free(src);
1927 162 hiro
}
1928 162 hiro
1929 1 hiro
static gboolean textview_uri_security_check(TextView *textview, RemoteURI *uri)
1930 1 hiro
{
1931 1 hiro
        GtkTextBuffer *buffer;
1932 1 hiro
        GtkTextIter start_iter, end_iter;
1933 1 hiro
        gchar *visible_str;
1934 1 hiro
        gboolean retval = TRUE;
1935 1 hiro
1936 1 hiro
        if (is_uri_string(uri->uri) == FALSE)
1937 1 hiro
                return TRUE;
1938 1 hiro
1939 1 hiro
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
1940 1 hiro
        gtk_text_buffer_get_iter_at_offset(buffer, &start_iter, uri->start);
1941 1 hiro
        gtk_text_buffer_get_iter_at_offset(buffer, &end_iter, uri->end);
1942 1 hiro
        visible_str = gtk_text_buffer_get_text(buffer, &start_iter, &end_iter,
1943 1 hiro
                                               FALSE);
1944 1 hiro
        if (visible_str == NULL)
1945 1 hiro
                return TRUE;
1946 1 hiro
1947 1 hiro
        if (strcmp(visible_str, uri->uri) != 0 && is_uri_string(visible_str)) {
1948 1 hiro
                gchar *uri_path;
1949 1 hiro
                gchar *visible_uri_path;
1950 1 hiro
1951 1 hiro
                uri_path = get_uri_path(uri->uri);
1952 1 hiro
                visible_uri_path = get_uri_path(visible_str);
1953 1 hiro
                if (strcmp(uri_path, visible_uri_path) != 0)
1954 1 hiro
                        retval = FALSE;
1955 1 hiro
        }
1956 1 hiro
1957 1 hiro
        if (retval == FALSE) {
1958 1 hiro
                gchar *msg;
1959 1 hiro
                AlertValue aval;
1960 1 hiro
1961 1 hiro
                msg = g_strdup_printf(_("The real URL (%s) is different from\n"
1962 1 hiro
                                        "the apparent URL (%s).\n"
1963 1 hiro
                                        "Open it anyway?"),
1964 1 hiro
                                      uri->uri, visible_str);
1965 31 hiro
                aval = alertpanel(_("Warning"), msg,
1966 31 hiro
                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
1967 1 hiro
                g_free(msg);
1968 1 hiro
                if (aval == G_ALERTDEFAULT)
1969 1 hiro
                        retval = TRUE;
1970 1 hiro
        }
1971 1 hiro
1972 1 hiro
        g_free(visible_str);
1973 1 hiro
1974 1 hiro
        return retval;
1975 1 hiro
}
1976 1 hiro
1977 1 hiro
static void textview_uri_list_remove_all(GSList *uri_list)
1978 1 hiro
{
1979 1 hiro
        GSList *cur;
1980 1 hiro
1981 1 hiro
        for (cur = uri_list; cur != NULL; cur = cur->next) {
1982 162 hiro
                RemoteURI *uri = (RemoteURI *)cur->data;
1983 162 hiro
1984 162 hiro
                if (uri) {
1985 162 hiro
                        g_free(uri->uri);
1986 162 hiro
                        g_free(uri->filename);
1987 162 hiro
                        g_free(uri);
1988 1 hiro
                }
1989 1 hiro
        }
1990 1 hiro
1991 1 hiro
        g_slist_free(uri_list);
1992 1 hiro
}