2005-09-14 [paul] 1.9.14cvs26
[claws.git] / src / textview.c
index 09c59dc3545e74cdefba161f0e0bc79f859a5967..c5a512baa57e3fd3170358e81b469667b6347280 100644 (file)
@@ -208,6 +208,9 @@ static void copy_mail_to_uri_cb                     (TextView       *textview,
 static void save_file_cb                       (TextView       *textview,
                                                 guint           action,
                                                 void           *data);
+static void open_image_cb                      (TextView       *textview,
+                                                guint           action,
+                                                void           *data);
 
 static GtkItemFactoryEntry textview_link_popup_entries[] = 
 {
@@ -224,7 +227,8 @@ static GtkItemFactoryEntry textview_mail_popup_entries[] =
 
 static GtkItemFactoryEntry textview_file_popup_entries[] = 
 {
-       {N_("/_Save this image..."),            NULL, save_file_cb, 0, NULL},
+       {N_("/_Open image"),            NULL, open_image_cb, 0, NULL},
+       {N_("/_Save image..."),         NULL, save_file_cb, 0, NULL},
 };
 
 
@@ -328,14 +332,14 @@ static void textview_create_tags(GtkTextView *text, TextView *textview)
        GtkTextBuffer *buffer;
        GtkTextTag *tag;
        static PangoFontDescription *font_desc, *bold_font_desc;
-
+       
        if (!font_desc)
                font_desc = pango_font_description_from_string
                        (NORMAL_FONT);
 
        if (!bold_font_desc) {
                bold_font_desc = pango_font_description_from_string
-                       (BOLD_FONT);
+                       (NORMAL_FONT);
                pango_font_description_set_weight
                        (bold_font_desc, PANGO_WEIGHT_BOLD);
        }
@@ -376,8 +380,7 @@ static void textview_create_tags(GtkTextView *text, TextView *textview)
                                   "foreground-gdk", &uri_color,
                                   "underline", PANGO_UNDERLINE_SINGLE,
                                   NULL);
-
-       g_signal_connect(G_OBJECT(tag), "event",
+       g_signal_connect(G_OBJECT(tag), "event",
                          G_CALLBACK(textview_uri_button_pressed), textview);
  }
 
@@ -394,9 +397,20 @@ void textview_init(TextView *textview)
        textview_create_tags(GTK_TEXT_VIEW(textview->text), textview);
 }
 
-void textview_update_message_colors(void)
+#define CHANGE_TAG_COLOR(tagname, color) { \
+       tag = gtk_text_tag_table_lookup(tags, tagname); \
+       if (tag) \
+               g_object_set(G_OBJECT(tag), "foreground-gdk", color, NULL); \
+}
+
+static void textview_update_message_colors(TextView *textview)
 {
        GdkColor black = {0, 0, 0, 0};
+       GdkColor colored_emphasis = {0, 0, 0, 0xcfff};
+       GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
+
+       GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
+       GtkTextTag *tag = NULL;
 
        if (prefs_common.enable_color) {
                /* grab the quote colors, converting from an int to a GdkColor */
@@ -410,15 +424,26 @@ void textview_update_message_colors(void)
                                               &uri_color);
                gtkut_convert_int_to_gdk_color(prefs_common.signature_col,
                                               &signature_color);
+               emphasis_color = colored_emphasis;
        } else {
                quote_colors[0] = quote_colors[1] = quote_colors[2] = 
                        uri_color = emphasis_color = signature_color = black;
        }
+       CHANGE_TAG_COLOR("quote0", &quote_colors[0]);
+       CHANGE_TAG_COLOR("quote1", &quote_colors[1]);
+       CHANGE_TAG_COLOR("quote2", &quote_colors[2]);
+       CHANGE_TAG_COLOR("emphasis", &emphasis_color);
+       CHANGE_TAG_COLOR("signature", &signature_color);
+       CHANGE_TAG_COLOR("link", &uri_color);
+       CHANGE_TAG_COLOR("link-hover", &uri_color);
+
 }
+#undef CHANGE_TAG_COLOR
 
 void textview_reflect_prefs(TextView *textview)
 {
-       textview_update_message_colors();
+       textview_set_font(textview, NULL);
+       textview_update_message_colors(textview);
        gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(textview->text),
                                         prefs_common.textview_cursor_visible);
 }
@@ -426,19 +451,10 @@ void textview_reflect_prefs(TextView *textview)
 void textview_show_message(TextView *textview, MimeInfo *mimeinfo,
                           const gchar *file)
 {
-       FILE *fp;
-
-       if ((fp = fopen(file, "rb")) == NULL) {
-               FILE_OP_ERROR(file, "fopen");
-               return;
-       }
-
        textview_clear(textview);
 
        textview_add_parts(textview, mimeinfo);
 
-       fclose(fp);
-
        textview_set_position(textview, 0);
 }
 
@@ -488,7 +504,7 @@ static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
        if ((mimeinfo->type == MIMETYPE_MESSAGE) && !g_ascii_strcasecmp(mimeinfo->subtype, "rfc822")) {
                FILE *fp;
 
-               fp = fopen(mimeinfo->data.filename, "rb");
+               fp = g_fopen(mimeinfo->data.filename, "rb");
                fseek(fp, mimeinfo->offset, SEEK_SET);
                headers = textview_scan_header(textview, fp);
                if (headers) {
@@ -526,7 +542,7 @@ static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
                        gchar *uri_str;
                        FILE *fp;
 
-                       fp = fopen(mimeinfo->data.filename, "rb");
+                       fp = g_fopen(mimeinfo->data.filename, "rb");
                        fseek(fp, mimeinfo->offset, SEEK_SET);
 
                        filename = procmime_get_tmp_file_name(mimeinfo);
@@ -722,7 +738,7 @@ static void textview_write_body(TextView *textview, MimeInfo *mimeinfo)
                charset = textview->messageview->forced_charset;
        else
                charset = procmime_mimeinfo_get_parameter(mimeinfo, "charset");
-
+printf("charset %s\n", charset);
        textview_set_font(textview, charset);
 
        conv = conv_code_converter_new(charset);
@@ -739,10 +755,10 @@ static void textview_write_body(TextView *textview, MimeInfo *mimeinfo)
                
                filename = procmime_get_tmp_file_name(mimeinfo);
                if (procmime_get_part(filename, mimeinfo) == 0) {
-                       tmpfp = fopen(filename, "rb");
+                       tmpfp = g_fopen(filename, "rb");
                        textview_show_html(textview, tmpfp, conv);
                        fclose(tmpfp);
-                       unlink(filename);
+                       g_unlink(filename);
                }
                g_free(filename);
        } else if (!g_ascii_strcasecmp(mimeinfo->subtype, "enriched")) {
@@ -750,14 +766,14 @@ static void textview_write_body(TextView *textview, MimeInfo *mimeinfo)
                
                filename = procmime_get_tmp_file_name(mimeinfo);
                if (procmime_get_part(filename, mimeinfo) == 0) {
-                       tmpfp = fopen(filename, "rb");
+                       tmpfp = g_fopen(filename, "rb");
                        textview_show_ertf(textview, tmpfp, conv);
                        fclose(tmpfp);
-                       unlink(filename);
+                       g_unlink(filename);
                }
                g_free(filename);
        } else {
-               tmpfp = fopen(mimeinfo->data.filename, "rb");
+               tmpfp = g_fopen(mimeinfo->data.filename, "rb");
                fseek(tmpfp, mimeinfo->offset, SEEK_SET);
                debug_print("Viewing text content of type: %s (length: %d)\n", mimeinfo->subtype, mimeinfo->length);
                while ((fgets(buf, sizeof(buf), tmpfp) != NULL) && 
@@ -818,324 +834,6 @@ static void textview_show_ertf(TextView *textview, FILE *fp,
        ertf_parser_destroy(parser);
 }
 
-/* get_uri_part() - retrieves a URI starting from scanpos.
-                   Returns TRUE if succesful */
-static gboolean get_uri_part(const gchar *start, const gchar *scanpos,
-                            const gchar **bp, const gchar **ep)
-{
-       const gchar *ep_;
-
-       g_return_val_if_fail(start != NULL, FALSE);
-       g_return_val_if_fail(scanpos != NULL, FALSE);
-       g_return_val_if_fail(bp != NULL, FALSE);
-       g_return_val_if_fail(ep != NULL, FALSE);
-
-       *bp = scanpos;
-
-       /* find end point of URI */
-       for (ep_ = scanpos; *ep_ != '\0'; ep_++) {
-               if (!isgraph(*(const guchar *)ep_) ||
-                   !IS_ASCII(*(const guchar *)ep_) ||
-                   strchr("[]{}()<>\"", *ep_))
-                       break;
-       }
-
-       /* no punctuation at end of string */
-
-       /* FIXME: this stripping of trailing punctuations may bite with other URIs.
-        * should pass some URI type to this function and decide on that whether
-        * to perform punctuation stripping */
-
-#define IS_REAL_PUNCT(ch)      (ispunct(ch) && ((ch) != '/')) 
-
-       for (; ep_ - 1 > scanpos + 1 &&
-              IS_REAL_PUNCT(*(const guchar *)(ep_ - 1));
-            ep_--)
-               ;
-
-#undef IS_REAL_PUNCT
-
-       *ep = ep_;
-
-       return TRUE;            
-}
-
-static gchar *make_uri_string(const gchar *bp, const gchar *ep)
-{
-       return g_strndup(bp, ep - bp);
-}
-
-/* valid mail address characters */
-#define IS_RFC822_CHAR(ch) \
-       (IS_ASCII(ch) && \
-        (ch) > 32   && \
-        (ch) != 127 && \
-        !isspace(ch) && \
-        !strchr("(),;<>\"", (ch)))
-
-/* alphabet and number within 7bit ASCII */
-#define IS_ASCII_ALNUM(ch)     (IS_ASCII(ch) && isalnum(ch))
-#define IS_QUOTE(ch) ((ch) == '\'' || (ch) == '"')
-
-static GHashTable *create_domain_tab(void)
-{
-       static const gchar *toplvl_domains [] = {
-           "museum", "aero",
-           "arpa", "coop", "info", "name", "biz", "com", "edu", "gov",
-           "int", "mil", "net", "org", "ac", "ad", "ae", "af", "ag",
-           "ai", "al", "am", "an", "ao", "aq", "ar", "as", "at", "au",
-           "aw", "az", "ba", "bb", "bd", "be", "bf", "bg", "bh", "bi",
-           "bj", "bm", "bn", "bo", "br", "bs", "bt", "bv", "bw", "by",
-           "bz", "ca", "cc", "cd", "cf", "cg", "ch", "ci", "ck", "cl",
-           "cm", "cn", "co", "cr", "cu", "cv", "cx", "cy", "cz", "de",
-           "dj", "dk", "dm", "do", "dz", "ec", "ee", "eg", "eh", "er",
-           "es", "et", "fi", "fj", "fk", "fm", "fo", "fr", "ga", "gd",
-           "ge", "gf", "gg", "gh", "gi", "gl", "gm", "gn", "gp", "gq",
-           "gr", "gs", "gt", "gu", "gw", "gy", "hk", "hm", "hn", "hr",
-           "ht", "hu", "id", "ie", "il", "im", "in", "io", "iq", "ir",
-           "is", "it", "je", "jm", "jo", "jp", "ke", "kg", "kh", "ki",
-           "km", "kn", "kp", "kr", "kw", "ky", "kz", "la", "lb", "lc",
-           "li", "lk", "lr", "ls", "lt", "lu", "lv", "ly", "ma", "mc",
-           "md", "mg", "mh", "mk", "ml", "mm", "mn", "mo", "mp", "mq",
-           "mr", "ms", "mt", "mu", "mv", "mw", "mx", "my", "mz", "na",
-           "nc", "ne", "nf", "ng", "ni", "nl", "no", "np", "nr", "nu",
-           "nz", "om", "pa", "pe", "pf", "pg", "ph", "pk", "pl", "pm",
-           "pn", "pr", "ps", "pt", "pw", "py", "qa", "re", "ro", "ru",
-           "rw", "sa", "sb", "sc", "sd", "se", "sg", "sh", "si", "sj",
-           "sk", "sl", "sm", "sn", "so", "sr", "st", "sv", "sy", "sz",
-           "tc", "td", "tf", "tg", "th", "tj", "tk", "tm", "tn", "to",
-           "tp", "tr", "tt", "tv", "tw", "tz", "ua", "ug", "uk", "um",
-           "us", "uy", "uz", "va", "vc", "ve", "vg", "vi", "vn", "vu",
-            "wf", "ws", "ye", "yt", "yu", "za", "zm", "zw" 
-       };
-       gint n;
-       GHashTable *htab = g_hash_table_new(g_stricase_hash, g_stricase_equal);
-       
-       g_return_val_if_fail(htab, NULL);
-       for (n = 0; n < sizeof toplvl_domains / sizeof toplvl_domains[0]; n++) 
-               g_hash_table_insert(htab, (gpointer) toplvl_domains[n], (gpointer) toplvl_domains[n]);
-       return htab;
-}
-
-static gboolean is_toplvl_domain(GHashTable *tab, const gchar *first, const gchar *last)
-{
-       const gint MAX_LVL_DOM_NAME_LEN = 6;
-       gchar buf[MAX_LVL_DOM_NAME_LEN + 1];
-       const gchar *m = buf + MAX_LVL_DOM_NAME_LEN + 1;
-       register gchar *p;
-       
-       if (last - first > MAX_LVL_DOM_NAME_LEN || first > last)
-               return FALSE;
-
-       for (p = buf; p < m &&  first < last; *p++ = *first++)
-               ;
-       *p = 0;
-
-       return g_hash_table_lookup(tab, buf) != NULL;
-}
-
-/* get_email_part() - retrieves an email address. Returns TRUE if succesful */
-static gboolean get_email_part(const gchar *start, const gchar *scanpos,
-                              const gchar **bp, const gchar **ep)
-{
-       /* more complex than the uri part because we need to scan back and forward starting from
-        * the scan position. */
-       gboolean result = FALSE;
-       const gchar *bp_ = NULL;
-       const gchar *ep_ = NULL;
-       static GHashTable *dom_tab;
-       const gchar *last_dot = NULL;
-       const gchar *prelast_dot = NULL;
-       const gchar *last_tld_char = NULL;
-       
-       /* the informative part of the email address (describing the name
-        * of the email address owner) may contain quoted parts. the
-        * closure stack stores the last encountered quotes. */
-       gchar closure_stack[128];
-       gchar *ptr = closure_stack;
-
-       g_return_val_if_fail(start != NULL, FALSE);
-       g_return_val_if_fail(scanpos != NULL, FALSE);
-       g_return_val_if_fail(bp != NULL, FALSE);
-       g_return_val_if_fail(ep != NULL, FALSE);
-
-       if (!dom_tab)
-               dom_tab = create_domain_tab();
-       g_return_val_if_fail(dom_tab, FALSE);   
-
-       /* scan start of address */
-       for (bp_ = scanpos - 1;
-            bp_ >= start && IS_RFC822_CHAR(*(const guchar *)bp_); bp_--)
-               ;
-
-       /* TODO: should start with an alnum? */
-       bp_++;
-       for (; bp_ < scanpos && !IS_ASCII_ALNUM(*(const guchar *)bp_); bp_++)
-               ;
-
-       if (bp_ != scanpos) {
-               /* scan end of address */
-               for (ep_ = scanpos + 1;
-                    *ep_ && IS_RFC822_CHAR(*(const guchar *)ep_); ep_++)
-                       if (*ep_ == '.') {
-                               prelast_dot = last_dot;
-                               last_dot = ep_;
-                               if (*(last_dot + 1) == '.') {
-                                       if (prelast_dot == NULL)
-                                               return FALSE;
-                                       last_dot = prelast_dot;
-                                       break;
-                               }
-                       }
-
-               /* TODO: really should terminate with an alnum? */
-               for (; ep_ > scanpos && !IS_ASCII_ALNUM(*(const guchar *)ep_);
-                    --ep_)
-                       ;
-               ep_++;
-
-               if (last_dot == NULL)
-                       return FALSE;
-               if (last_dot >= ep_)
-                       last_dot = prelast_dot;
-               if (last_dot == NULL || (scanpos + 1 >= last_dot))
-                       return FALSE;
-               last_dot++;
-
-               for (last_tld_char = last_dot; last_tld_char < ep_; last_tld_char++)
-                       if (*last_tld_char == '?')
-                               break;
-
-               if (is_toplvl_domain(dom_tab, last_dot, last_tld_char))
-                       result = TRUE;
-
-               *ep = ep_;
-               *bp = bp_;
-       }
-
-       if (!result) return FALSE;
-
-       if (*ep_ && *(bp_ - 1) == '"' && *(ep_) == '"' 
-       && *(ep_ + 1) == ' ' && *(ep_ + 2) == '<'
-       && IS_RFC822_CHAR(*(ep_ + 3))) {
-               /* this informative part with an @ in it is 
-                * followed by the email address */
-               ep_ += 3;
-               
-               /* go to matching '>' (or next non-rfc822 char, like \n) */
-               for (; *ep_ != '>' && *ep != '\0' && IS_RFC822_CHAR(*ep_); ep_++)
-                       ;
-                       
-               /* include the bracket */
-               if (*ep_ == '>') ep_++;
-               
-               /* include the leading quote */         
-               bp_--;
-
-               *ep = ep_;
-               *bp = bp_;
-               return TRUE;
-       }
-
-       /* skip if it's between quotes "'alfons@proteus.demon.nl'" <alfons@proteus.demon.nl> */
-       if (bp_ - 1 > start && IS_QUOTE(*(bp_ - 1)) && IS_QUOTE(*ep_))
-               return FALSE;
-
-       /* see if this is <bracketed>; in this case we also scan for the informative part. */
-       if (bp_ - 1 <= start || *(bp_ - 1) != '<' || *ep_ != '>')
-               return TRUE;
-
-#define FULL_STACK()   ((size_t) (ptr - closure_stack) >= sizeof closure_stack)
-#define IN_STACK()     (ptr > closure_stack)
-/* has underrun check */
-#define POP_STACK()    if(IN_STACK()) --ptr
-/* has overrun check */
-#define PUSH_STACK(c)  if(!FULL_STACK()) *ptr++ = (c); else return TRUE
-/* has underrun check */
-#define PEEK_STACK()   (IN_STACK() ? *(ptr - 1) : 0)
-
-       ep_++;
-
-       /* scan for the informative part. */
-       for (bp_ -= 2; bp_ >= start; bp_--) {
-               /* if closure on the stack keep scanning */
-               if (PEEK_STACK() == *bp_) {
-                       POP_STACK();
-                       continue;
-               }
-               if (*bp_ == '\'' || *bp_ == '"') {
-                       PUSH_STACK(*bp_);
-                       continue;
-               }
-
-               /* if nothing in the closure stack, do the special conditions
-                * the following if..else expression simply checks whether 
-                * a token is acceptable. if not acceptable, the clause
-                * should terminate the loop with a 'break' */
-               if (!PEEK_STACK()) {
-                       if (*bp_ == '-'
-                       && (((bp_ - 1) >= start) && isalnum(*(bp_ - 1)))
-                       && (((bp_ + 1) < ep_)    && isalnum(*(bp_ + 1)))) {
-                               /* hyphens are allowed, but only in
-                                  between alnums */
-                       } else if (!strchr(",;:=?./+<>!&\r\n\t", *bp_)) {
-                               /* but anything not being a punctiation
-                                  is ok */
-                       } else {
-                               break; /* anything else is rejected */
-                       }
-               }
-       }
-
-       bp_++;
-
-#undef PEEK_STACK
-#undef PUSH_STACK
-#undef POP_STACK
-#undef IN_STACK
-#undef FULL_STACK
-
-       /* scan forward (should start with an alnum) */
-       for (; *bp_ != '<' && isspace(*bp_) && *bp_ != '"'; bp_++)
-               ;
-
-       *ep = ep_;
-       *bp = bp_;
-       
-       return result;
-}
-
-#undef IS_QUOTE
-#undef IS_ASCII_ALNUM
-#undef IS_RFC822_CHAR
-
-static gchar *make_email_string(const gchar *bp, const gchar *ep)
-{
-       /* returns a mailto: URI; mailto: is also used to detect the
-        * uri type later on in the button_pressed signal handler */
-       gchar *tmp;
-       gchar *result;
-
-       tmp = g_strndup(bp, ep - bp);
-       result = g_strconcat("mailto:", tmp, NULL);
-       g_free(tmp);
-
-       return result;
-}
-
-static gchar *make_http_string(const gchar *bp, const gchar *ep)
-{
-       /* returns an http: URI; */
-       gchar *tmp;
-       gchar *result;
-
-       tmp = g_strndup(bp, ep - bp);
-       result = g_strconcat("http://", tmp, NULL);
-       g_free(tmp);
-
-       return result;
-}
-
 #define ADD_TXT_POS(bp_, ep_, pti_) \
        if ((last->next = alloca(sizeof(struct txtpos))) != NULL) { \
                last = last->next; \
@@ -1328,10 +1026,7 @@ static void textview_write_line(TextView *textview, const gchar *str,
                textview->is_in_signature = TRUE;
        }
 
-       if (prefs_common.enable_color)
-               textview_make_clickable_parts(textview, fg_color, "link", buf);
-       else
-               textview_make_clickable_parts(textview, fg_color, NULL, buf);
+       textview_make_clickable_parts(textview, fg_color, "link", buf);
 }
 
 void textview_write_link(TextView *textview, const gchar *str,
@@ -1416,10 +1111,39 @@ void textview_set_all_headers(TextView *textview, gboolean all_headers)
        textview->show_all_headers = all_headers;
 }
 
+#define CHANGE_TAG_FONT(tagname, font) { \
+       tag = gtk_text_tag_table_lookup(tags, tagname); \
+       if (tag) \
+               g_object_set(G_OBJECT(tag), "font-desc", font, NULL); \
+}
+
 void textview_set_font(TextView *textview, const gchar *codeset)
 {
+       GtkTextTag *tag;
+       GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
+       GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
+       
+       if (NORMAL_FONT) {
+               PangoFontDescription *font_desc, *bold_font_desc;
+               font_desc = pango_font_description_from_string
+                                               (NORMAL_FONT);
+               bold_font_desc = pango_font_description_from_string
+                                               (NORMAL_FONT);
+               if (font_desc) {
+                       gtk_widget_modify_font(textview->text, font_desc);
+                       CHANGE_TAG_FONT("header", font_desc);
+                       pango_font_description_free(font_desc);
+               }
+               if (bold_font_desc) {
+                       pango_font_description_set_weight
+                               (bold_font_desc, PANGO_WEIGHT_BOLD);
+                       CHANGE_TAG_FONT("header_title", bold_font_desc);
+                       pango_font_description_free(bold_font_desc);
+               }
+       }
+
        if (prefs_common.textfont) {
-               PangoFontDescription *font_desc = NULL;
+               PangoFontDescription *font_desc;
 
                font_desc = pango_font_description_from_string
                                                (prefs_common.textfont);
@@ -1562,8 +1286,7 @@ static void textview_show_header(TextView *textview, GPtrArray *headers)
                    procheader_headername_equal(header->name, "Cc"))
                        unfold_line(header->body);
 
-               if (prefs_common.enable_color &&
-                   (procheader_headername_equal(header->name, "X-Mailer") ||
+               if ((procheader_headername_equal(header->name, "X-Mailer") ||
                     procheader_headername_equal(header->name,
                                                 "X-Newsreader")) &&
                    strstr(header->body, "Sylpheed") != NULL) {
@@ -1571,11 +1294,8 @@ static void textview_show_header(TextView *textview, GPtrArray *headers)
                        gtk_text_buffer_insert_with_tags_by_name
                                (buffer, &iter, header->body, -1,
                                 "header", "emphasis", NULL);
-               } else if (prefs_common.enable_color) {
-                       textview_make_clickable_parts(textview, "header", "link",
-                                                     header->body);
                } else {
-                       textview_make_clickable_parts(textview, "header", NULL,
+                       textview_make_clickable_parts(textview, "header", "link",
                                                      header->body);
                }
                gtk_text_buffer_get_end_iter (buffer, &iter);
@@ -2255,6 +1975,62 @@ static void open_uri_cb (TextView *textview, guint action, void *data)
                          NULL);
 }
 
+static void open_image_cb (TextView *textview, guint action, void *data)
+{
+       RemoteURI *uri = g_object_get_data(G_OBJECT(textview->file_popup_menu),
+                                          "menu_button");
+
+       static gchar *default_cmdline = DEFAULT_IMAGE_VIEWER_CMD;
+       gchar buf[1024];
+       const gchar *cmd;
+       const gchar *def_cmd;
+       const gchar *p;
+       gchar *filename = NULL;
+       gchar *tmp_filename = NULL;
+
+       if (uri == NULL)
+               return;
+
+       if (uri->filename == NULL)
+               return;
+       
+       filename = g_strdup(uri->filename);
+       
+       if (!g_utf8_validate(filename, -1, NULL)) {
+               gchar *tmp = conv_filename_to_utf8(filename);
+               g_free(filename);
+               filename = tmp;
+       }
+
+       subst_for_filename(filename);
+
+       tmp_filename = g_filename_from_uri(uri->uri, NULL, NULL);
+       copy_file(tmp_filename, filename, FALSE);
+       g_free(tmp_filename);
+
+       cmd = prefs_common.mime_image_viewer;
+       def_cmd = default_cmdline;
+       
+       if (cmd && (p = strchr(cmd, '%')) && *(p + 1) == 's' &&
+           !strchr(p + 2, '%'))
+               g_snprintf(buf, sizeof(buf), cmd, filename);
+       else {
+               if (cmd)
+                       g_warning("Image viewer command line is invalid: '%s'", cmd);
+               if (def_cmd)
+                       g_snprintf(buf, sizeof(buf), def_cmd, filename);
+               else
+                       return;
+       }
+
+       execute_command_line(buf, TRUE);
+
+       g_free(filename);
+
+       g_object_set_data(G_OBJECT(textview->file_popup_menu), "menu_button",
+                         NULL);
+}
+
 static void save_file_cb (TextView *textview, guint action, void *data)
 {
        RemoteURI *uri = g_object_get_data(G_OBJECT(textview->file_popup_menu),