Statistics
| Revision:

root / src / textview.c @ 411

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