fixes for custom headers and displaying of headers
[claws.git] / src / textview.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999,2000 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <gdk/gdk.h>
28 #include <gdk/gdkkeysyms.h>
29 #include <gtk/gtkvbox.h>
30 #include <gtk/gtkscrolledwindow.h>
31 #include <gtk/gtktext.h>
32 #include <gtk/gtksignal.h>
33 #include <stdio.h>
34 #include <ctype.h>
35 #include <string.h>
36 #include <stdlib.h>
37
38 #include "intl.h"
39 #include "main.h"
40 #include "summaryview.h"
41 #include "procheader.h"
42 #include "prefs_common.h"
43 #include "codeconv.h"
44 #include "utils.h"
45 #include "gtkutils.h"
46 #include "procmime.h"
47 #include "html.h"
48 #include "compose.h"
49 #include "addressbook.h"
50 #include "headers_display.h"
51 #include "prefs_display_headers.h"
52
53 #define FONT_LOAD(font, s) \
54 { \
55         gchar *fontstr, *p; \
56  \
57         Xstrdup_a(fontstr, s, ); \
58         if ((p = strchr(fontstr, ',')) != NULL) *p = '\0'; \
59         font = gdk_font_load(fontstr); \
60 }
61
62 typedef struct _RemoteURI       RemoteURI;
63
64 struct _RemoteURI
65 {
66         gchar *uri;
67
68         guint start;
69         guint end;
70 };
71
72 static GdkColor quote_colors[3] = {
73         {(gulong)0, (gushort)0, (gushort)0, (gushort)0},
74         {(gulong)0, (gushort)0, (gushort)0, (gushort)0},
75         {(gulong)0, (gushort)0, (gushort)0, (gushort)0}
76 };
77
78 static GdkColor uri_color = {
79         (gulong)0,
80         (gushort)0,
81         (gushort)0,
82         (gushort)0
83 };
84
85 static GdkColor emphasis_color = {
86         (gulong)0,
87         (gushort)0,
88         (gushort)0,
89         (gushort)0xcfff
90 };
91
92 static GdkColor error_color = {
93         (gulong)0,
94         (gushort)0xefff,
95         (gushort)0,
96         (gushort)0
97 };
98
99 static GdkFont *spacingfont;
100
101 static void textview_show_html          (TextView       *textview,
102                                          FILE           *fp,
103                                          CodeConverter  *conv);
104 static void textview_write_line         (TextView       *textview,
105                                          const gchar    *str,
106                                          CodeConverter  *conv);
107 static GPtrArray *textview_scan_header  (TextView       *textview,
108                                          FILE           *fp);
109 static void textview_show_header        (TextView       *textview,
110                                          GPtrArray      *headers);
111
112 static void textview_key_pressed        (GtkWidget      *widget,
113                                          GdkEventKey    *event,
114                                          TextView       *textview);
115 static void textview_button_pressed     (GtkWidget      *widget,
116                                          GdkEventButton *event,
117                                          TextView       *textview);
118
119 static void textview_uri_list_remove_all(GSList         *uri_list);
120
121 static void textview_smooth_scroll_do           (TextView       *textview,
122                                                  gfloat          old_value,
123                                                  gfloat          last_value,
124                                                  gint            step);
125 static void textview_smooth_scroll_one_line     (TextView       *textview,
126                                                  gboolean        up);
127 static gboolean textview_smooth_scroll_page     (TextView       *textview,
128                                                  gboolean        up);
129
130
131 TextView *textview_create(void)
132 {
133         TextView *textview;
134         GtkWidget *vbox;
135         GtkWidget *scrolledwin_sb;
136         GtkWidget *scrolledwin_mb;
137         GtkWidget *text_sb;
138         GtkWidget *text_mb;
139
140         debug_print(_("Creating text view...\n"));
141         textview = g_new0(TextView, 1);
142
143         scrolledwin_sb = gtk_scrolled_window_new(NULL, NULL);
144         scrolledwin_mb = gtk_scrolled_window_new(NULL, NULL);
145         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin_sb),
146                                        GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
147         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin_mb),
148                                        GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
149         gtk_widget_set_usize(scrolledwin_sb, prefs_common.mainview_width, -1);
150         gtk_widget_set_usize(scrolledwin_mb, prefs_common.mainview_width, -1);
151
152         /* create GtkText widgets for single-byte and multi-byte character */
153         text_sb = gtk_text_new(NULL, NULL);
154         text_mb = gtk_text_new(NULL, NULL);
155         gtk_widget_show(text_sb);
156         gtk_widget_show(text_mb);
157         gtk_text_set_word_wrap(GTK_TEXT(text_sb), TRUE);
158         gtk_text_set_word_wrap(GTK_TEXT(text_mb), TRUE);
159         gtk_widget_ensure_style(text_sb);
160         gtk_widget_ensure_style(text_mb);
161         if (text_sb->style && text_sb->style->font->type == GDK_FONT_FONTSET) {
162                 GtkStyle *style;
163
164                 style = gtk_style_copy(text_sb->style);
165                 gdk_font_unref(style->font);
166                 FONT_LOAD(style->font, NORMAL_FONT);
167                 gtk_widget_set_style(text_sb, style);
168         }
169         if (text_mb->style && text_mb->style->font->type == GDK_FONT_FONT) {
170                 GtkStyle *style;
171
172                 style = gtk_style_copy(text_mb->style);
173                 gdk_font_unref(style->font);
174                 style->font = gdk_fontset_load(NORMAL_FONT);
175                 gtk_widget_set_style(text_mb, style);
176         }
177         gtk_widget_ref(scrolledwin_sb);
178         gtk_widget_ref(scrolledwin_mb);
179
180         gtk_container_add(GTK_CONTAINER(scrolledwin_sb), text_sb);
181         gtk_container_add(GTK_CONTAINER(scrolledwin_mb), text_mb);
182         gtk_signal_connect(GTK_OBJECT(text_sb), "key_press_event",
183                            GTK_SIGNAL_FUNC(textview_key_pressed),
184                            textview);
185         gtk_signal_connect(GTK_OBJECT(text_sb), "button_press_event",
186                            GTK_SIGNAL_FUNC(textview_button_pressed),
187                            textview);
188         gtk_signal_connect(GTK_OBJECT(text_mb), "key_press_event",
189                            GTK_SIGNAL_FUNC(textview_key_pressed),
190                            textview);
191         gtk_signal_connect(GTK_OBJECT(text_mb), "button_press_event",
192                            GTK_SIGNAL_FUNC(textview_button_pressed),
193                            textview);
194
195         gtk_widget_show(scrolledwin_sb);
196         gtk_widget_show(scrolledwin_mb);
197
198         vbox = gtk_vbox_new(FALSE, 0);
199         gtk_box_pack_start(GTK_BOX(vbox), scrolledwin_sb, TRUE, TRUE, 0);
200
201         textview->vbox           = vbox;
202         textview->scrolledwin    = scrolledwin_sb;
203         textview->scrolledwin_sb = scrolledwin_sb;
204         textview->scrolledwin_mb = scrolledwin_mb;
205         textview->text           = text_sb;
206         textview->text_sb        = text_sb;
207         textview->text_mb        = text_mb;
208         textview->text_is_mb     = FALSE;
209         textview->uri_list       = NULL;
210
211         return textview;
212 }
213
214 void textview_init(TextView *textview)
215 {
216         gtkut_widget_disable_theme_engine(textview->text_sb);
217         gtkut_widget_disable_theme_engine(textview->text_mb);
218         textview_update_message_colors();
219         textview_set_font(textview, NULL);
220 }
221
222 void textview_update_message_colors(void)
223 {
224         GdkColor black = {0, 0, 0, 0};
225
226         if (prefs_common.enable_color) {
227                 /* grab the quote colors, converting from an int to a GdkColor */
228                 gtkut_convert_int_to_gdk_color(prefs_common.quote_level1_col,
229                                                &quote_colors[0]);
230                 gtkut_convert_int_to_gdk_color(prefs_common.quote_level2_col,
231                                                &quote_colors[1]);
232                 gtkut_convert_int_to_gdk_color(prefs_common.quote_level3_col,
233                                                &quote_colors[2]);
234                 gtkut_convert_int_to_gdk_color(prefs_common.uri_col,
235                                                &uri_color);
236         } else {
237                 quote_colors[0] = quote_colors[1] = quote_colors[2] = 
238                         uri_color = emphasis_color = black;
239         }
240 }
241
242 void textview_show_message(TextView *textview, MimeInfo *mimeinfo,
243                            const gchar *file)
244 {
245         FILE *fp;
246
247         if ((fp = fopen(file, "r")) == NULL) {
248                 FILE_OP_ERROR(file, "fopen");
249                 return;
250         }
251
252         textview_show_part(textview, mimeinfo, fp);
253
254         fclose(fp);
255 }
256
257 void textview_show_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
258 {
259         GtkText *text;
260         gchar buf[BUFFSIZE];
261         const gchar *boundary = NULL;
262         gint boundary_len = 0;
263         const gchar *charset = NULL;
264         FILE *tmpfp;
265         GPtrArray *headers = NULL;
266         CodeConverter *conv;
267
268         g_return_if_fail(mimeinfo != NULL);
269         g_return_if_fail(fp != NULL);
270
271         if (mimeinfo->mime_type == MIME_MULTIPART) {
272                 if (mimeinfo->sub) {
273                         mimeinfo = mimeinfo->sub;
274                         if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0) {
275                                 perror("fseek");
276                                 return;
277                         }
278                 } else
279                         return;
280         }
281         if (mimeinfo->parent && mimeinfo->parent->boundary) {
282                 boundary = mimeinfo->parent->boundary;
283                 boundary_len = strlen(boundary);
284         }
285
286         if (!boundary && mimeinfo->mime_type == MIME_TEXT) {
287                 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
288                         perror("fseek");
289                 headers = textview_scan_header(textview, fp);
290         } else {
291                 if (mimeinfo->mime_type == MIME_TEXT && mimeinfo->parent) {
292                         glong fpos;
293
294                         if ((fpos = ftell(fp)) < 0)
295                                 perror("ftell");
296                         else if (fseek(fp, mimeinfo->parent->fpos, SEEK_SET)
297                                  < 0)
298                                 perror("fseek");
299                         else {
300                                 headers = textview_scan_header(textview, fp);
301                                 if (fseek(fp, fpos, SEEK_SET) < 0)
302                                         perror("fseek");
303                         }
304                 }
305                 while (fgets(buf, sizeof(buf), fp) != NULL)
306                         if (buf[0] == '\r' || buf[0] == '\n') break;
307         }
308
309         /* display attached RFC822 single text message */
310         if (mimeinfo->parent && mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
311                 if (!mimeinfo->sub || mimeinfo->sub->children) return;
312                 headers = textview_scan_header(textview, fp);
313                 mimeinfo = mimeinfo->sub;
314         } else if (!mimeinfo->parent &&
315                    mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
316                 if (!mimeinfo->sub) return;
317                 headers = textview_scan_header(textview, fp);
318                 mimeinfo = mimeinfo->sub;
319         }
320
321         if (prefs_common.force_charset)
322                 charset = prefs_common.force_charset;
323         else if (mimeinfo->charset)
324                 charset = mimeinfo->charset;
325         textview_set_font(textview, charset);
326
327         conv = conv_code_converter_new(charset);
328
329         textview_clear(textview);
330         text = GTK_TEXT(textview->text);
331         gtk_text_freeze(text);
332
333         if (headers) {
334                 gint i;
335
336                 textview_show_header(textview, headers);
337                 for (i = 0; i < headers->len; i++) {
338                         Header *header = g_ptr_array_index(headers, i);
339                         g_free(header->body);
340                         g_free(header->name);
341                         g_free(header);
342                 }
343                 g_ptr_array_free(headers, TRUE);
344         }
345
346         tmpfp = procmime_decode_content(NULL, fp, mimeinfo);
347         if (tmpfp) {
348                 if (mimeinfo->mime_type == MIME_TEXT_HTML)
349                         textview_show_html(textview, tmpfp, conv);
350                 else
351                         while (fgets(buf, sizeof(buf), tmpfp) != NULL)
352                                 textview_write_line(textview, buf, conv);
353                 fclose(tmpfp);
354         }
355
356         conv_code_converter_destroy(conv);
357
358         gtk_text_thaw(text);
359 }
360
361 void textview_show_mime_part(TextView *textview, MimeInfo *partinfo)
362 {
363         GtkText *text;
364
365         if (!partinfo) return;
366
367         textview_set_font(textview, NULL);
368         text = GTK_TEXT(textview->text);
369         textview_clear(textview);
370
371         gtk_text_freeze(text);
372
373         gtk_text_insert
374                 (text, textview->msgfont, NULL, NULL,
375                  _("To save this part, pop up the context menu with\n"), -1);
376         gtk_text_insert
377                 (text, textview->msgfont, NULL, NULL,
378                  _("right click and select `Save as...', or press `y' key.\n\n"), -1);
379
380         gtk_text_insert
381                 (text, textview->msgfont, NULL, NULL,
382                  _("To display this part as a text message, select\n"), -1);
383         gtk_text_insert
384                 (text, textview->msgfont, NULL, NULL,
385                  _("`Display as text', or press `t' key.\n\n"), -1);
386
387         gtk_text_insert
388                 (text, textview->msgfont, NULL, NULL,
389                  _("To open this part with external program, select `Open',\n"), -1);
390         gtk_text_insert
391                 (text, textview->msgfont, NULL, NULL,
392                  _("or double-click, or click the center button, or press `l' key."), -1);
393
394         gtk_text_thaw(text);
395 }
396
397 #if USE_GPGME
398 void textview_show_signature_part(TextView *textview, MimeInfo *partinfo)
399 {
400         GtkText *text;
401
402         if (!partinfo) return;
403
404         textview_set_font(textview, NULL);
405         text = GTK_TEXT(textview->text);
406         textview_clear(textview);
407
408         gtk_text_freeze(text);
409
410         if (partinfo->sigstatus_full == NULL) {
411                 gtk_text_insert
412                         (text, textview->msgfont, NULL, NULL,
413                          _("This signature has not been checked yet.\n"), -1);
414                 gtk_text_insert
415                         (text, textview->msgfont, NULL, NULL,
416                          _("To check it, pop up the context menu with\n"), -1);
417                 gtk_text_insert
418                         (text, textview->msgfont, NULL, NULL,
419                          _("right click and select `Check signature'.\n"), -1);
420         } else {
421                 gtk_text_insert(text, textview->msgfont, NULL, NULL,
422                                 partinfo->sigstatus_full, -1);
423         }
424
425         gtk_text_thaw(text);
426 }
427 #endif /* USE_GPGME */
428
429 static void textview_show_html(TextView *textview, FILE *fp,
430                                CodeConverter *conv)
431 {
432         HTMLParser *parser;
433         gchar *str;
434
435         parser = html_parser_new(fp, conv);
436         g_return_if_fail(parser != NULL);
437
438         while ((str = html_parse(parser)) != NULL) {
439                 textview_write_line(textview, str, NULL);
440         }
441         html_parser_destroy(parser);
442 }
443
444 /* get_uri_part() - retrieves a URI starting from scanpos.
445                     Returns TRUE if succesful */
446 static gboolean get_uri_part(const gchar *start, const gchar *scanpos,
447                              const gchar **bp, const gchar **ep)
448 {
449         const gchar *ep_;
450
451         g_return_val_if_fail(start != NULL, FALSE);
452         g_return_val_if_fail(scanpos != NULL, FALSE);
453         g_return_val_if_fail(bp != NULL, FALSE);
454         g_return_val_if_fail(ep != NULL, FALSE);
455
456         *bp = scanpos;
457
458         /* find end point of URI */
459         for (ep_ = scanpos; *ep_ != '\0'; ep_++) {
460                 if (!isgraph(*ep_) || !isascii(*ep_) || strchr("()<>\"", *ep_))
461                         break;
462         }
463
464         /* no punctuation at end of string */
465
466         /* FIXME: this stripping of trailing punctuations may bite with other URIs.
467          * should pass some URI type to this function and decide on that whether
468          * to perform punctuation stripping */
469
470 #define IS_REAL_PUNCT(ch) \
471         (ispunct(ch) && ((ch) != '/')) 
472
473         for (; ep_ - 1 > scanpos + 1 && IS_REAL_PUNCT(*(ep_ - 1)); ep_--)
474                 ;
475
476 #undef IS_REAL_PUNCT
477
478         *ep = ep_;
479
480         return TRUE;            
481 }
482
483 static gchar *make_uri_string(const gchar *bp, const gchar *ep)
484 {
485         return g_strndup(bp, ep - bp);
486 }
487
488 /* valid mail address characters */
489 #define IS_RFC822_CHAR(ch) \
490         (isascii(ch) && \
491          (ch) > 32   && \
492          (ch) != 127 && \
493          !isspace(ch) && \
494          !strchr("()<>\"", (ch)))
495
496 /* get_email_part() - retrieves an email address. Returns TRUE if succesful */
497 static gboolean get_email_part(const gchar *start, const gchar *scanpos,
498                                const gchar **bp, const gchar **ep)
499 {
500         /* more complex than the uri part because we need to scan back and forward starting from
501          * the scan position. */
502         gboolean result = FALSE;
503         const gchar *bp_;
504         const gchar *ep_;
505
506         g_return_val_if_fail(start != NULL, FALSE);
507         g_return_val_if_fail(scanpos != NULL, FALSE);
508         g_return_val_if_fail(bp != NULL, FALSE);
509         g_return_val_if_fail(ep != NULL, FALSE);
510
511         /* scan start of address */
512         for (bp_ = scanpos - 1; bp_ >= start && IS_RFC822_CHAR(*bp_); bp_--)
513                 ;
514
515         /* TODO: should start with an alnum? */
516         bp_++;
517         for (; bp_ < scanpos && !isalnum(*bp_); bp_++)
518                 ;
519
520         if (bp_ != scanpos) {
521                 /* scan end of address */
522                 for (ep_ = scanpos + 1; *ep_ && IS_RFC822_CHAR(*ep_); ep_++)
523                         ;
524
525                 /* TODO: really should terminate with an alnum? */
526                 for (; ep_ > scanpos  && !isalnum(*ep_); --ep_)
527                         ;
528                 ep_++;
529
530                 if (ep_ > scanpos + 1) {
531                         *ep = ep_;
532                         *bp = bp_;
533                         result = TRUE;
534                 }
535         }
536         
537         if (result) {
538                 /* we now have at least the address. now see if this is <bracketed>; in this
539                  * case we also scan for the informative part. we could make this a useful
540                  * function because it tries to parse out an email address backwards. :) */
541                 if ( ((bp_ - 1 > start) && *(bp_ - 1) == '<') &&  (*ep_ == '>')) {
542                         char    closure_stack[128];
543                         char   *ptr = closure_stack;
544 #define FULL_STACK()    ((ptr - closure_stack) >= sizeof closure_stack) 
545 #define IN_STACK()              (ptr > closure_stack)
546 /* has underrun check */
547 #define POP_STACK()             if(IN_STACK()) --ptr            
548 /* has overrun check */
549 #define PUSH_STACK(c)   if(!FULL_STACK()) *ptr++ = (c); else return TRUE 
550 /* has underrun check */
551 #define PEEK_STACK()    (IN_STACK() ? *(ptr - 1) : 0)
552
553                         ep_++;
554
555                         /* scan for the informative part */
556                         for (bp_ -= 2; bp_ >= start; bp_--) {
557                                 /* if closure on the stack keep scanning */
558                                 if (PEEK_STACK() == *bp_) {
559                                         POP_STACK();
560                                 }                                       
561                                 else {
562                                         if (*bp_ == '\'' || *bp_ == '"')  {
563                                                 PUSH_STACK(*bp_);
564                                         }                                               
565                                         else {
566                                                 if (!PEEK_STACK()) {
567                                                         /* if nothing in the closure stack, do the special conditions
568                                                          * the following if..else expression simply checks whether 
569                                                          * a token is acceptable. if not acceptable, the clause
570                                                          * should terminate the loop with a 'break' */
571                                                         if ( *bp_ == '-' 
572                                                         &&   (((bp_ - 1) >= start) && isalnum(*(bp_ - 1))) 
573                                                         &&   (((bp_ + 1) < ep_)    && isalnum(*(bp_ + 1))) ) {
574                                                                 /* hyphens are allowed, but only in between alnums */
575                                                         }
576                                                         else if ( !ispunct(*bp_) ) {
577                                                                 /* but anything not being a punctiation is ok */
578                                                         }
579                                                         else {
580                                                                 break; /* anything else is rejected */
581                                                         }
582                                                 }
583                                         }
584                                 }
585                         }
586                         
587                         bp_++;
588
589 #undef PEEK_STACK
590 #undef PUSH_STACK
591 #undef POP_STACK
592 #undef IN_STACK
593 #undef FULL_STACK
594
595                         /* scan forward (should start with an alnum) */
596                         for (; *bp_ != '<' && isspace(*bp_) && *bp_ != '"'; bp_++)
597                                 ;
598
599                         *ep = ep_;
600                         *bp = bp_;
601                 }
602         }
603         return result;
604 }
605
606 #undef IS_RFC822_CHAR
607
608 static gchar *make_email_string(const gchar *bp, const gchar *ep)
609 {
610         /* returns a mailto: URI; mailto: is also used to detect the
611          * uri type later on in the button_pressed signal handler */
612         gchar *tmp;
613         gchar *result;
614
615         tmp = g_strndup(bp, ep - bp);
616         result = g_strconcat("mailto:", tmp, NULL);
617         g_free(tmp);
618
619         return result;
620 }
621
622 #define ADD_TXT_POS(bp_, ep_, pti_) \
623         if ((last->next = alloca(sizeof(struct txtpos))) != NULL) { \
624                 last = last->next; \
625                 last->bp = (bp_); last->ep = (ep_); last->pti = (pti_); \
626                 last->next = NULL; \
627         } else { \
628                 g_warning("alloc error scanning URIs\n"); \
629                 gtk_text_insert(text, textview->msgfont, fg_color, NULL, \
630                                 linebuf, -1); \
631                 return; \
632         }
633
634 /* textview_make_clickable_parts() - colorizes clickable parts */
635 static void textview_make_clickable_parts(TextView *textview, GtkText *text,
636                                           GdkFont  *textfont,
637                                           GdkColor *fg_color,
638                                           GdkColor *uri_color,
639                                           const gchar *linebuf)
640 {
641         /* parse table - in order of priority */
642         struct table {
643                 const gchar *needle; /* token */
644
645                 /* token search function */
646                 gchar    *(*search)     (const gchar *haystack,
647                                          const gchar *needle);
648                 /* part parsing function */
649                 gboolean  (*parse)      (const gchar *start,
650                                          const gchar *scanpos,
651                                          const gchar **bp_,
652                                          const gchar **ep_);
653                 /* part to URI function */
654                 gchar    *(*build_uri)  (const gchar *bp,
655                                          const gchar *ep);
656         };
657
658         static struct table parser[] = {
659                 {"http://",  strcasestr, get_uri_part,   make_uri_string},
660                 {"https://", strcasestr, get_uri_part,   make_uri_string},
661                 {"ftp://",   strcasestr, get_uri_part,   make_uri_string},
662                 {"mailto:",  strcasestr, get_uri_part,   make_uri_string},
663                 {"@",        strcasestr, get_email_part, make_email_string}
664         };
665         const gint PARSE_ELEMS = sizeof parser / sizeof parser[0];
666
667         gint  n;
668         const gchar *walk, *bp, *ep;
669
670         struct txtpos {
671                 const gchar     *bp, *ep;       /* text position */
672                 gint             pti;           /* index in parse table */
673                 struct txtpos   *next;          /* next */
674         } head = {NULL, NULL, 0,  NULL}, *last = &head;
675
676         /* parse for clickable parts, and build a list of begin and end positions  */
677         for (walk = linebuf, n = 0;;) {
678                 gint last_index = PARSE_ELEMS;
679                 gchar *scanpos = NULL;
680
681                 /* FIXME: this looks phony. scanning for anything in the parse table */
682                 for (n = 0; n < PARSE_ELEMS; n++) {
683                         gchar *tmp;
684
685                         tmp = parser[n].search(walk, parser[n].needle);
686                         if (tmp) {
687                                 if (scanpos == NULL || tmp < scanpos) {
688                                         scanpos = tmp;
689                                         last_index = n;
690                                 }
691                         }                                       
692                 }
693
694                 if (scanpos) {
695                         /* check if URI can be parsed */
696                         if (parser[last_index].parse(linebuf, scanpos, &bp, &ep)
697                             && (ep - bp - 1) > strlen(parser[last_index].needle)) {
698                                         ADD_TXT_POS(bp, ep, last_index);
699                                         walk = ep;
700                         } else
701                                 walk = scanpos +
702                                         strlen(parser[last_index].needle);
703                 } else
704                         break;
705         }
706
707         /* colorize this line */
708         if (head.next) {
709                 const gchar *normal_text = linebuf;
710
711                 /* insert URIs */
712                 for (last = head.next; last != NULL;
713                      normal_text = last->ep, last = last->next) {
714                         RemoteURI *uri;
715
716                         uri = g_new(RemoteURI, 1);
717                         if (last->bp - normal_text > 0)
718                                 gtk_text_insert(text, textfont,
719                                                 fg_color, NULL,
720                                                 normal_text,
721                                                 last->bp - normal_text);
722                         uri->uri = parser[last->pti].build_uri(last->bp, last->ep);
723                         uri->start = gtk_text_get_point(text);
724                         gtk_text_insert(text, textfont, uri_color,
725                                         NULL, last->bp, last->ep - last->bp);
726                         uri->end = gtk_text_get_point(text);
727                         textview->uri_list =
728                                 g_slist_append(textview->uri_list, uri);
729                 }
730
731                 if (*normal_text)
732                         gtk_text_insert(text, textfont, fg_color,
733                                         NULL, normal_text, -1);
734         } else
735                 gtk_text_insert(text, textfont, fg_color, NULL,
736                                 linebuf, -1);
737 }
738
739 #undef ADD_TXT_POS
740
741 static void textview_write_line(TextView *textview, const gchar *str,
742                                 CodeConverter *conv)
743 {
744         GtkText *text = GTK_TEXT(textview->text);
745         gchar buf[BUFFSIZE];
746         size_t len;
747         GdkColor *fg_color;
748         gint quotelevel = -1;
749
750         if (!conv)
751                 strncpy2(buf, str, sizeof(buf));
752         else if (conv_convert(conv, buf, sizeof(buf), str) < 0) {
753                 gtk_text_insert(text, textview->msgfont,
754                                 prefs_common.enable_color
755                                 ? &error_color : NULL, NULL,
756                                 "*** Warning: code conversion failed ***\n",
757                                 -1);
758                 return;
759         }
760
761         len = strlen(buf);
762         if (len > 1 && buf[len - 1] == '\n' && buf[len - 2] == '\r') {
763                 buf[len - 2] = '\n';
764                 buf[len - 1] = '\0';
765         }
766         if (prefs_common.conv_mb_alnum) conv_mb_alnum(buf);
767         fg_color = NULL;
768
769         /* change color of quotation
770            >, foo>, _> ... ok, <foo>, foo bar>, foo-> ... ng
771            Up to 3 levels of quotations are detected, and each
772            level is colored using a different color. */
773         if (prefs_common.enable_color && strchr(buf, '>')) {
774                 quotelevel = get_quote_level(buf);
775
776                 /* set up the correct foreground color */
777                 if (quotelevel > 2) {
778                         /* recycle colors */
779                         if (prefs_common.recycle_quote_colors)
780                                 quotelevel %= 3;
781                         else
782                                 quotelevel = 2;
783                 }
784         }
785
786         if (quotelevel == -1)
787                 fg_color = NULL;
788         else
789                 fg_color = &quote_colors[quotelevel];
790
791         if (prefs_common.head_space && spacingfont && buf[0] != '\n')
792                 gtk_text_insert(text, spacingfont, NULL, NULL, " ", 1);
793
794         if (prefs_common.enable_color)
795                 textview_make_clickable_parts(textview, text,
796                                                   textview->msgfont,
797                                               fg_color, &uri_color, buf);
798         else
799                 gtk_text_insert(text, textview->msgfont, fg_color, NULL,
800                                 buf, -1);
801 }
802
803 void textview_clear(TextView *textview)
804 {
805         GtkText *text = GTK_TEXT(textview->text);
806
807         gtk_text_freeze(text);
808         gtk_text_backward_delete(text, gtk_text_get_length(text));
809         gtk_text_thaw(text);
810
811         textview_uri_list_remove_all(textview->uri_list);
812         textview->uri_list = NULL;
813 }
814
815 void textview_destroy(TextView *textview)
816 {
817         textview_uri_list_remove_all(textview->uri_list);
818         textview->uri_list = NULL;
819
820         if (!textview->scrolledwin_sb->parent)
821                 gtk_widget_destroy(textview->scrolledwin_sb);
822         if (!textview->scrolledwin_mb->parent)
823                 gtk_widget_destroy(textview->scrolledwin_mb);
824
825         if (textview->msgfont) {
826                 textview->msgfont->ascent = textview->prev_ascent;
827                 textview->msgfont->descent = textview->prev_descent;
828                 gdk_font_unref(textview->msgfont);
829         }
830         if (textview->boldfont)
831                 gdk_font_unref(textview->boldfont);
832
833         g_free(textview);
834 }
835
836 void textview_set_font(TextView *textview, const gchar *codeset)
837 {
838         gboolean use_fontset = TRUE;
839
840         /* In multi-byte mode, GtkText can't display 8bit characters
841            correctly, so it must be single-byte mode. */
842         if (MB_CUR_MAX > 1) {
843                 if (codeset) {
844                         if (!strncasecmp(codeset, "ISO-8859-", 9) ||
845                             !strncasecmp(codeset, "KOI8-", 5)     ||
846                             !strncasecmp(codeset, "CP", 2)        ||
847                             !strncasecmp(codeset, "WINDOWS-", 8)  ||
848                             !strcasecmp(codeset, "BALTIC"))
849                                 use_fontset = FALSE;
850                 }
851         } else
852                 use_fontset = FALSE;
853
854         if (textview->text_is_mb && !use_fontset) {
855                 GtkWidget *parent;
856
857                 parent = textview->scrolledwin_mb->parent;
858                 gtk_editable_select_region
859                         (GTK_EDITABLE(textview->text_mb), 0, 0);
860                 gtk_container_remove(GTK_CONTAINER(parent),
861                                      textview->scrolledwin_mb);
862                 gtk_container_add(GTK_CONTAINER(parent),
863                                   textview->scrolledwin_sb);
864
865                 textview->text = textview->text_sb;
866                 textview->text_is_mb = FALSE;
867         } else if (!textview->text_is_mb && use_fontset) {
868                 GtkWidget *parent;
869
870                 parent = textview->scrolledwin_sb->parent;
871                 gtk_editable_select_region
872                         (GTK_EDITABLE(textview->text_sb), 0, 0);
873                 gtk_container_remove(GTK_CONTAINER(parent),
874                                      textview->scrolledwin_sb);
875                 gtk_container_add(GTK_CONTAINER(parent),
876                                   textview->scrolledwin_mb);
877
878                 textview->text = textview->text_mb;
879                 textview->text_is_mb = TRUE;
880         }
881
882         if (prefs_common.textfont) {
883                 if (textview->msgfont) {
884                         textview->msgfont->ascent = textview->prev_ascent;
885                         textview->msgfont->descent = textview->prev_descent;
886                         gdk_font_unref(textview->msgfont);
887                         textview->msgfont = NULL;
888                 }
889                 if (use_fontset)
890                         textview->msgfont =
891                                 gdk_fontset_load(prefs_common.textfont);
892                 else {
893                         if (MB_CUR_MAX > 1) {
894                                 FONT_LOAD(textview->msgfont,
895                                           "-*-courier-medium-r-normal--14-*-*-*-*-*-iso8859-1");
896                         } else {
897                                 FONT_LOAD(textview->msgfont,
898                                           prefs_common.textfont);
899                         }
900                 }
901
902                 if (textview->msgfont) {
903                         gint ascent, descent;
904
905                         textview->prev_ascent = textview->msgfont->ascent;
906                         textview->prev_descent = textview->msgfont->descent;
907                         descent = prefs_common.line_space / 2;
908                         ascent  = prefs_common.line_space - descent;
909                         textview->msgfont->ascent  += ascent;
910                         textview->msgfont->descent += descent;
911                 }
912         }
913
914         if (!textview->boldfont)
915                 FONT_LOAD(textview->boldfont, BOLD_FONT);
916         if (!spacingfont)
917                 spacingfont = gdk_font_load("-*-*-medium-r-normal--6-*");
918 }
919
920 enum
921 {
922         H_DATE          = 0,
923         H_FROM          = 1,
924         H_TO            = 2,
925         H_NEWSGROUPS    = 3,
926         H_SUBJECT       = 4,
927         H_CC            = 5,
928         H_REPLY_TO      = 6,
929         H_FOLLOWUP_TO   = 7,
930         H_X_MAILER      = 8,
931         H_X_NEWSREADER  = 9,
932         H_USER_AGENT    = 10,
933         H_ORGANIZATION  = 11,
934 };
935
936 static gboolean hdrequal(char * hdr1, char * hdr2)
937 {
938         int len1;
939         int len2;
940
941         len1 = strlen(hdr1);
942         len2 = strlen(hdr2);
943         if (hdr1[len1 - 1] == ':')
944                 len1--;
945         if (hdr2[len2 - 1] == ':')
946                 len2--;
947         if (len1 != len2)
948                 return 0;
949         return (strncasecmp(hdr1, hdr2, len1) == 0);
950 }
951
952 static GPtrArray *textview_scan_header(TextView *textview, FILE *fp)
953 {
954         /*
955         static HeaderEntry hentry[] = {{"Date:",         NULL, FALSE},
956                                        {"From:",         NULL, TRUE},
957                                        {"To:",           NULL, FALSE},
958                                        {"Newsgroups:",   NULL, FALSE},
959                                        {"Subject:",      NULL, TRUE},
960                                        {"Cc:",           NULL, FALSE},
961                                        {"Reply-To:",     NULL, FALSE},
962                                        {"Followup-To:",  NULL, FALSE},
963                                        {"X-Mailer:",     NULL, TRUE},
964                                        {"X-Newsreader:", NULL, TRUE},
965                                        {"User-Agent:",   NULL, TRUE},
966                                        {"Organization:", NULL, TRUE},
967                                        {NULL,            NULL, FALSE}};
968         */
969         gchar buf[BUFFSIZE], tmp[BUFFSIZE];
970         gint hnum;
971         HeaderEntry *hp;
972         GPtrArray *headers;
973         GSList * l;
974
975         int i;
976         GPtrArray *sorted_headers;
977
978         g_return_val_if_fail(fp != NULL, NULL);
979
980         if (!prefs_common.display_header) {
981                 while (fgets(buf, sizeof(buf), fp) != NULL)
982                         if (buf[0] == '\r' || buf[0] == '\n') break;
983                 return NULL;
984         }
985
986         headers = g_ptr_array_new();
987
988         /*
989         while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
990                != -1) {
991                 Header *header;
992
993                 hp = hentry + hnum;
994
995                 header = g_new(Header, 1);
996                 header->name = g_strndup(buf, strlen(hp->name));
997                 conv_unmime_header(tmp, sizeof(tmp), buf + strlen(hp->name),
998                                    NULL);
999                 header->body = g_strdup(tmp);
1000
1001                 g_ptr_array_add(headers, header);
1002         }
1003         */
1004         //      while (procheader_get_unfolded_line(buf, sizeof(buf), fp) != NULL) {
1005         while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1) {
1006                 gchar * p;
1007                 Header *header;
1008
1009                 if (*buf == ':') continue;
1010                 for (p = buf; *p && *p != ' '; p++) {
1011                         if (*p == ':') {
1012                                 header = g_new(Header, 1);
1013                                 header->name = g_strndup(buf, p - buf + 1);
1014                                 p++;
1015                                 /* while (*p == ' ' || *p == '\t') p++; */
1016                                 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
1017                                 header->body = g_strdup(tmp);
1018
1019                                 g_ptr_array_add(headers, header);
1020                                 break;
1021                         }
1022                 }
1023         }
1024
1025         sorted_headers = g_ptr_array_new();
1026         for(l = prefs_display_headers ; l != NULL ; l = g_slist_next(l)) {
1027                 HeaderDisplayProp * dp = (HeaderDisplayProp *) l->data;
1028                 for(i = 0 ; i < headers->len ; i++) {
1029                         Header * header = g_ptr_array_index(headers, i);
1030                         if (hdrequal(header->name, dp->name)) {
1031                                 if (dp->hidden) {
1032                                         g_ptr_array_remove_index(headers, i);
1033                                         g_free(header->body);
1034                                         g_free(header->name);
1035                                         g_free(header);
1036                                         i--;
1037                                 }
1038                                 else {
1039                                         g_ptr_array_add(sorted_headers,
1040                                                         header);
1041                                         g_ptr_array_remove_index(headers, i);
1042                                         i--;
1043                                 }
1044                         }
1045                 }
1046         }
1047
1048         for(i = 0 ; i < headers->len ; i++) {
1049                 Header * header = g_ptr_array_index(headers, i);
1050                 g_ptr_array_add(sorted_headers, header);
1051         }
1052
1053         g_ptr_array_free(headers, TRUE);
1054         
1055         return sorted_headers;
1056 }
1057
1058 static void textview_show_header(TextView *textview, GPtrArray *headers)
1059 {
1060         GtkText *text = GTK_TEXT(textview->text);
1061         Header *header;
1062         gint i;
1063
1064         g_return_if_fail(headers != NULL);
1065
1066         gtk_text_freeze(text);
1067
1068         for (i = 0; i < headers->len; i++) {
1069                 header = g_ptr_array_index(headers, i);
1070                 g_return_if_fail(header->name != NULL);
1071
1072                 gtk_text_insert(text, textview->boldfont, NULL, NULL,
1073                                 header->name, -1);
1074                 if (prefs_common.enable_color &&
1075                     (strncmp(header->name, "X-Mailer", 8) == 0 ||
1076                      strncmp(header->name, "X-Newsreader", 12) == 0) &&
1077                     strstr(header->body, "Sylpheed") != NULL)
1078                         gtk_text_insert(text, NULL, &emphasis_color, NULL,
1079                                         header->body, -1);
1080                 else {
1081                         if (prefs_common.enable_color) {
1082                                 textview_make_clickable_parts(textview, text,
1083                                                                                           NULL,
1084                                                                                           NULL, 
1085                                                                                           &uri_color, 
1086                                                                                           header->body);
1087                         }                                                                                         
1088                         else {                                                                                    
1089                                 gtk_text_insert(text, NULL, NULL, NULL,
1090                                                 header->body, -1);
1091                         }                                               
1092                 }                                       
1093                 gtk_text_insert(text, textview->msgfont, NULL, NULL, "\n", 1);
1094         }
1095
1096         gtk_text_insert(text, textview->msgfont, NULL, NULL, "\n", 1);
1097         gtk_text_thaw(text);
1098 }
1099
1100 void textview_scroll_one_line(TextView *textview, gboolean up)
1101 {
1102         GtkText *text = GTK_TEXT(textview->text);
1103         gfloat upper;
1104
1105         if (prefs_common.enable_smooth_scroll) {
1106                 textview_smooth_scroll_one_line(textview, up);
1107                 return;
1108         }
1109
1110         if (!up) {
1111                 upper = text->vadj->upper - text->vadj->page_size;
1112                 if (text->vadj->value < upper) {
1113                         text->vadj->value +=
1114                                 text->vadj->step_increment * 4;
1115                         text->vadj->value =
1116                                 MIN(text->vadj->value, upper);
1117                         gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1118                                                 "value_changed");
1119                 }
1120         } else {
1121                 if (text->vadj->value > 0.0) {
1122                         text->vadj->value -=
1123                                 text->vadj->step_increment * 4;
1124                         text->vadj->value =
1125                                 MAX(text->vadj->value, 0.0);
1126                         gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1127                                                 "value_changed");
1128                 }
1129         }
1130 }
1131
1132 gboolean textview_scroll_page(TextView *textview, gboolean up)
1133 {
1134         GtkText *text = GTK_TEXT(textview->text);
1135         gfloat upper;
1136         gfloat page_incr;
1137
1138         if (prefs_common.enable_smooth_scroll)
1139                 return textview_smooth_scroll_page(textview, up);
1140
1141         if (prefs_common.scroll_halfpage)
1142                 page_incr = text->vadj->page_increment / 2;
1143         else
1144                 page_incr = text->vadj->page_increment;
1145
1146         if (!up) {
1147                 upper = text->vadj->upper - text->vadj->page_size;
1148                 if (text->vadj->value < upper) {
1149                         text->vadj->value += page_incr;
1150                         text->vadj->value = MIN(text->vadj->value, upper);
1151                         gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1152                                                 "value_changed");
1153                 } else
1154                         return FALSE;
1155         } else {
1156                 if (text->vadj->value > 0.0) {
1157                         text->vadj->value -= page_incr;
1158                         text->vadj->value = MAX(text->vadj->value, 0.0);
1159                         gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1160                                                 "value_changed");
1161                 } else
1162                         return FALSE;
1163         }
1164
1165         return TRUE;
1166 }
1167
1168 static void textview_smooth_scroll_do(TextView *textview,
1169                                       gfloat old_value, gfloat last_value,
1170                                       gint step)
1171 {
1172         GtkText *text = GTK_TEXT(textview->text);
1173         gint change_value;
1174         gboolean up;
1175         gint i;
1176
1177         if (old_value < last_value) {
1178                 change_value = last_value - old_value;
1179                 up = FALSE;
1180         } else {
1181                 change_value = old_value - last_value;
1182                 up = TRUE;
1183         }
1184
1185         gdk_key_repeat_disable();
1186
1187         for (i = step; i <= change_value; i += step) {
1188                 text->vadj->value = old_value + (up ? -i : i);
1189                 gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
1190                                         "value_changed");
1191         }
1192
1193         text->vadj->value = last_value;
1194         gtk_signal_emit_by_name(GTK_OBJECT(text->vadj), "value_changed");
1195
1196         gdk_key_repeat_restore();
1197 }
1198
1199 static void textview_smooth_scroll_one_line(TextView *textview, gboolean up)
1200 {
1201         GtkText *text = GTK_TEXT(textview->text);
1202         gfloat upper;
1203         gfloat old_value;
1204         gfloat last_value;
1205
1206         if (!up) {
1207                 upper = text->vadj->upper - text->vadj->page_size;
1208                 if (text->vadj->value < upper) {
1209                         old_value = text->vadj->value;
1210                         last_value = text->vadj->value +
1211                                 text->vadj->step_increment * 4;
1212                         last_value = MIN(last_value, upper);
1213
1214                         textview_smooth_scroll_do(textview, old_value,
1215                                                   last_value,
1216                                                   prefs_common.scroll_step);
1217                 }
1218         } else {
1219                 if (text->vadj->value > 0.0) {
1220                         old_value = text->vadj->value;
1221                         last_value = text->vadj->value -
1222                                 text->vadj->step_increment * 4;
1223                         last_value = MAX(last_value, 0.0);
1224
1225                         textview_smooth_scroll_do(textview, old_value,
1226                                                   last_value,
1227                                                   prefs_common.scroll_step);
1228                 }
1229         }
1230 }
1231
1232 static gboolean textview_smooth_scroll_page(TextView *textview, gboolean up)
1233 {
1234         GtkText *text = GTK_TEXT(textview->text);
1235         gfloat upper;
1236         gfloat page_incr;
1237         gfloat old_value;
1238         gfloat last_value;
1239
1240         if (prefs_common.scroll_halfpage)
1241                 page_incr = text->vadj->page_increment / 2;
1242         else
1243                 page_incr = text->vadj->page_increment;
1244
1245         if (!up) {
1246                 upper = text->vadj->upper - text->vadj->page_size;
1247                 if (text->vadj->value < upper) {
1248                         old_value = text->vadj->value;
1249                         last_value = text->vadj->value + page_incr;
1250                         last_value = MIN(last_value, upper);
1251
1252                         textview_smooth_scroll_do(textview, old_value,
1253                                                   last_value,
1254                                                   prefs_common.scroll_step);
1255                 } else
1256                         return FALSE;
1257         } else {
1258                 if (text->vadj->value > 0.0) {
1259                         old_value = text->vadj->value;
1260                         last_value = text->vadj->value - page_incr;
1261                         last_value = MAX(last_value, 0.0);
1262
1263                         textview_smooth_scroll_do(textview, old_value,
1264                                                   last_value,
1265                                                   prefs_common.scroll_step);
1266                 } else
1267                         return FALSE;
1268         }
1269
1270         return TRUE;
1271 }
1272
1273 static void textview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1274                                  TextView *textview)
1275 {
1276         SummaryView *summaryview = NULL;
1277
1278         if (!event) return;
1279         if (textview->messageview->mainwin)
1280                 summaryview = textview->messageview->mainwin->summaryview;
1281
1282         switch (event->keyval) {
1283         case GDK_Tab:
1284         case GDK_Home:
1285         case GDK_Left:
1286         case GDK_Up:
1287         case GDK_Right:
1288         case GDK_Down:
1289         case GDK_Page_Up:
1290         case GDK_Page_Down:
1291         case GDK_End:
1292         case GDK_Control_L:
1293         case GDK_Control_R:
1294                 break;
1295         case GDK_space:
1296                 if (summaryview)
1297                         summary_pass_key_press_event(summaryview, event);
1298                 else
1299                         textview_scroll_page(textview, FALSE);
1300                 break;
1301         case GDK_BackSpace:
1302         case GDK_Delete:
1303                 textview_scroll_page(textview, TRUE);
1304                 break;
1305         case GDK_Return:
1306                 textview_scroll_one_line(textview,
1307                                          (event->state & GDK_MOD1_MASK) != 0);
1308                 break;
1309         default:
1310                 if (summaryview)
1311                         summary_pass_key_press_event(summaryview, event);
1312                 break;
1313         }
1314 }
1315
1316 static void textview_button_pressed(GtkWidget *widget, GdkEventButton *event,
1317                                     TextView *textview)
1318 {
1319         if (event &&
1320             ((event->button == 1 && event->type == GDK_2BUTTON_PRESS) 
1321                 || event->button == 2
1322                 || event->button == 3)) {
1323                 GSList *cur;
1324                 guint current_pos;
1325
1326                 current_pos = GTK_EDITABLE(textview->text)->current_pos;
1327
1328                 for (cur = textview->uri_list; cur != NULL; cur = cur->next) {
1329                         RemoteURI *uri = (RemoteURI *)cur->data;
1330
1331                         if (current_pos >= uri->start &&
1332                             current_pos <  uri->end) {
1333                                 if (!strncasecmp(uri->uri, "mailto:", 7)) {
1334                                         compose_new_with_recipient
1335                                                 (NULL, uri->uri + 7);
1336                                 }                                               
1337                                 else
1338                                         open_uri(uri->uri,
1339                                                  prefs_common.uri_cmd);
1340                         }                               
1341                 }
1342         }
1343 }
1344
1345 static void textview_uri_list_remove_all(GSList *uri_list)
1346 {
1347         GSList *cur;
1348
1349         for (cur = uri_list; cur != NULL; cur = cur->next) {
1350                 if (cur->data) {
1351                         g_free(((RemoteURI *)cur->data)->uri);
1352                         g_free(cur->data);
1353                 }
1354         }
1355
1356         g_slist_free(uri_list);
1357 }