g_free(trimmed_uri);
[claws.git] / src / textview.c
index fe24af82554a4c27e6a17176de307a7eeda13ce1..9e0070b260ee0cb1819cb1b8be4da955bd9d5917 100644 (file)
@@ -148,9 +148,9 @@ static void textview_show_html              (TextView       *textview,
 static void textview_write_line                (TextView       *textview,
                                         const gchar    *str,
                                         CodeConverter  *conv);
-static void textview_write_link         (TextView      *textview,
-                                         const gchar    *url,
+static void textview_write_link                (TextView       *textview,
                                         const gchar    *str,
+                                        const gchar    *uri,
                                         CodeConverter  *conv);
 static GPtrArray *textview_scan_header (TextView       *textview,
                                         FILE           *fp);
@@ -329,10 +329,13 @@ void textview_show_message(TextView *textview, MimeInfo *mimeinfo,
                return;
        }
 
-       if (prefs_common.force_charset)
+       if (textview->messageview->forced_charset)
+               charset = textview->messageview->forced_charset;
+       else if (prefs_common.force_charset)
                charset = prefs_common.force_charset;
        else if (mimeinfo->charset)
                charset = mimeinfo->charset;
+
        textview_set_font(textview, charset);
        textview_clear(textview);
 
@@ -426,10 +429,13 @@ void textview_show_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
                is_rfc822_part = TRUE;
        }
 
-       if (prefs_common.force_charset)
+       if (textview->messageview->forced_charset)
+               charset = textview->messageview->forced_charset;
+       else if (prefs_common.force_charset)
                charset = prefs_common.force_charset;
        else if (mimeinfo->charset)
                charset = mimeinfo->charset;
+
        textview_set_font(textview, charset);
 
        text = GTK_STEXT(textview->text);
@@ -467,12 +473,6 @@ static void textview_add_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
 
        if (mimeinfo->mime_type == MIME_MULTIPART) return;
 
-       if (!mimeinfo->parent &&
-           mimeinfo->mime_type != MIME_TEXT &&
-           mimeinfo->mime_type != MIME_TEXT_HTML &&
-           mimeinfo->mime_type != MIME_TEXT_ENRICHED)
-               return;
-
        if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0) {
                perror("fseek");
                return;
@@ -523,7 +523,7 @@ static void textview_add_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
                textview->msgfont = NULL;
 
                tmp = g_strconcat("pgp: ", _("Check signature"), NULL);
-               textview_write_link(textview, tmp, buf, NULL);
+               textview_write_link(textview, buf, tmp, NULL);
                
                /* put things back */
                textview->msgfont = (GdkFont *)oldfont;
@@ -551,13 +551,15 @@ static void textview_add_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
                        gtk_stext_insert(text, NULL, NULL, NULL, buf, -1);
                else if (prefs_common.display_header)
                        gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
-               if (prefs_common.force_charset)
+               if (textview->messageview->forced_charset)
+                       charset = textview->messageview->forced_charset;
+               else if (prefs_common.force_charset)
                        charset = prefs_common.force_charset;
                else if (mimeinfo->charset)
                        charset = mimeinfo->charset;
                textview_write_body(textview, mimeinfo, fp, charset);
        }
-       
+
        gtk_stext_thaw(text);
 }
 
@@ -595,7 +597,7 @@ void textview_show_mime_part(TextView *textview, MimeInfo *partinfo)
        textview_set_font(textview, NULL);
        text = GTK_STEXT(textview->text);
        textview_clear(textview);
-       
+
        gtk_stext_freeze(text);
 
        TEXT_INSERT(_("To save this part, pop up the context menu with "));
@@ -605,9 +607,6 @@ void textview_show_mime_part(TextView *textview, MimeInfo *partinfo)
        TEXT_INSERT(_("To display this part as a text message, select "));
        TEXT_INSERT(_("`Display as text', or press `t' key.\n\n"));
 
-       TEXT_INSERT(_("To display this part as an image, select "));
-       TEXT_INSERT(_("`Display image', or press `i' key.\n\n"));
-
        TEXT_INSERT(_("To open this part with external program, select "));
        TEXT_INSERT(_("`Open' or `Open with...', "));
        TEXT_INSERT(_("or double-click, or click the center button, "));
@@ -636,7 +635,7 @@ void textview_show_signature_part(TextView *textview, MimeInfo *partinfo)
        } else {
                TEXT_INSERT(partinfo->sigstatus_full);
        }
-               
+
        gtk_stext_thaw(text);
 }
 #endif /* USE_GPGME */
@@ -657,7 +656,6 @@ static void textview_write_body(TextView *textview, MimeInfo *mimeinfo,
        textview->is_in_signature = FALSE;
 
        if (tmpfp) {
-               
                if (mimeinfo->mime_type == MIME_TEXT_HTML)
                        textview_show_html(textview, tmpfp, conv);
                else if (mimeinfo->mime_type == MIME_TEXT_ENRICHED)
@@ -676,7 +674,6 @@ static void textview_show_html(TextView *textview, FILE *fp,
 {
        HTMLParser *parser;
        gchar *str;
-       gchar* url = NULL;
 
        parser = html_parser_new(fp, conv);
        g_return_if_fail(parser != NULL);
@@ -684,29 +681,22 @@ static void textview_show_html(TextView *textview, FILE *fp,
        while ((str = html_parse(parser)) != NULL) {
                if (parser->state == HTML_HREF) {
                        /* first time : get and copy the URL */
-                       if (url == NULL) {
+                       if (parser->href == NULL) {
                                /* ALF - the sylpheed html parser returns an empty string,
                                 * if still inside an <a>, but already parsed past HREF */
                                str = strtok(str, " ");
                                if (str) { 
-                                       url = strdup(str);
+                                       parser->href = strdup(str);
                                        /* the URL may (or not) be followed by the
                                         * referenced text */
                                        str = strtok(NULL, "");
                                }       
                        }
-                       if (str != NULL) {
-                               textview_write_link(textview, url, str, NULL);
-                       }
-               } else {
-                       if (url != NULL) {
-                               free(url);
-                               url = NULL;
-                       }
+                       if (str != NULL)
+                               textview_write_link(textview, str, parser->href, NULL);
+               } else
                        textview_write_line(textview, str, NULL);
-               }
        }
-       
        html_parser_destroy(parser);
 }
 
@@ -1123,49 +1113,6 @@ static void textview_make_clickable_parts(TextView *textview,
 
 #undef ADD_TXT_POS
 
-/* This function writes str as a double-clickable link with the given url. */ 
-static void textview_write_link(TextView *textview, const gchar *url,
-                                const gchar *str, CodeConverter *conv)
-{
-    GdkColor *link_color = NULL;
-    RemoteURI* uri;
-    GtkSText *text = GTK_STEXT(textview->text);
-    gchar buf[BUFFSIZE];
-
-    /* this part is taken from textview_write_line. Right now the only place
-     * that calls this function passes NULL for conv, but you never know. */
-#if 0
-    if (!conv)
-           strncpy2(buf, str, sizeof(buf));
-    else if (conv_convert(conv, buf, sizeof(buf), str) < 0) {
-                   gtk_stext_insert(text, textview->msgfont,
-                           prefs_common.enable_color
-                           ? &error_color : NULL, NULL,
-                           "*** Warning: code conversion failed ***\n",
-                           -1);
-           return;
-    }
-#endif
-
-    if (!conv || conv_convert(conv, buf, sizeof(buf), str) < 0)
-       strncpy2(buf, str, sizeof(buf));
-
-    strcrchomp(buf);
-    gtk_stext_insert(text, textview->msgfont, NULL, NULL, " ", 1);
-    /* this part is based on the code in make_clickable_parts */
-    if (prefs_common.enable_color) {
-       link_color = &uri_color;
-    }
-    uri = g_new(RemoteURI, 1);
-    uri->uri = g_strdup(url);
-    uri->start = gtk_stext_get_point(text);
-    gtk_stext_insert(text, textview->msgfont, link_color, NULL, str,
-                   strlen(str));
-    uri->end = gtk_stext_get_point(text);
-    textview->uri_list = g_slist_append(textview->uri_list, uri);
-}
-
 static void textview_write_line(TextView *textview, const gchar *str,
                                CodeConverter *conv)
 {
@@ -1227,6 +1174,44 @@ static void textview_write_line(TextView *textview, const gchar *str,
                                              fg_color, NULL, buf);
 }
 
+void textview_write_link(TextView *textview, const gchar *str,
+                        const gchar *uri, CodeConverter *conv)
+{
+       GdkColor *link_color = NULL;
+       GtkSText *text = GTK_STEXT(textview->text);
+       gchar buf[BUFFSIZE];
+       gchar *bufp;
+       RemoteURI *r_uri;
+
+       if (*str == '\0')
+               return;
+
+       if (!conv) {
+               if (textview->text_is_mb)
+                       conv_localetodisp(buf, sizeof(buf), str);
+               else
+                       strncpy2(buf, str, sizeof(buf));
+       } else if (conv_convert(conv, buf, sizeof(buf), str) < 0)
+               conv_localetodisp(buf, sizeof(buf), str);
+       else if (textview->text_is_mb)
+               conv_unreadable_locale(buf);
+
+       strcrchomp(buf);
+
+       for (bufp = buf; isspace(*bufp); bufp++)
+               gtk_stext_insert(text, textview->msgfont, NULL, NULL, bufp, 1);
+
+       if (prefs_common.enable_color) {
+               link_color = &uri_color;
+       }
+       r_uri = g_new(RemoteURI, 1);
+       r_uri->uri = g_strdup(uri);
+       r_uri->start = gtk_stext_get_point(text);
+       gtk_stext_insert(text, textview->msgfont, link_color, NULL, bufp, -1);
+       r_uri->end = gtk_stext_get_point(text);
+       textview->uri_list = g_slist_append(textview->uri_list, r_uri);
+}
+
 void textview_clear(TextView *textview)
 {
        GtkSText *text = GTK_STEXT(textview->text);
@@ -1445,6 +1430,7 @@ static GPtrArray *textview_scan_header(TextView *textview, FILE *fp)
        } else
                procheader_header_array_destroy(headers);
 
+
        return sorted_headers;
 }
 
@@ -1504,43 +1490,25 @@ gboolean textview_search_string(TextView *textview, const gchar *str,
 {
        GtkSText *text = GTK_STEXT(textview->text);
        gint pos;
-       wchar_t *wcs;
        gint len;
-       gint text_len;
-       gboolean found = FALSE;
 
        g_return_val_if_fail(str != NULL, FALSE);
 
-       wcs = strdup_mbstowcs(str);
-       g_return_val_if_fail(wcs != NULL, FALSE);
-       len = wcslen(wcs);
+       len = get_mbs_len(str);
+       g_return_val_if_fail(len >= 0, FALSE);
+
        pos = textview->cur_pos;
        if (pos < textview->body_pos)
                pos = textview->body_pos;
-       text_len = gtk_stext_get_length(text);
-       if (text_len - pos < len) {
-               g_free(wcs);
-               return FALSE;
-       }
 
-       for (; pos < text_len; pos++) {
-               if (text_len - pos < len) break;
-               if (gtk_stext_match_string(text, pos, wcs, len, case_sens)
-                   == TRUE) {
-                       gtk_widget_hide(GTK_WIDGET(textview->scrolledwin));
-                       gtk_editable_set_position(GTK_EDITABLE(text),
-                                                 pos + len);
-                       gtk_editable_select_region(GTK_EDITABLE(text),
-                                                  pos, pos + len);
-                       gtk_widget_show(GTK_WIDGET(textview->scrolledwin));
-                       textview_set_position(textview, pos + len);
-                       found = TRUE;
-                       break;
-               }
-               if (text_len - pos == len) break;
+       if ((pos = gtkut_stext_find(text, pos, str, case_sens)) != -1) {
+               gtk_editable_set_position(GTK_EDITABLE(text), pos + len);
+               gtk_editable_select_region(GTK_EDITABLE(text), pos, pos + len);
+               textview_set_position(textview, pos + len);
+               return TRUE;
        }
-       g_free(wcs);
-       return found;
+
+       return FALSE;
 }
 
 gboolean textview_search_string_backward(TextView *textview, const gchar *str,
@@ -1878,7 +1846,10 @@ static gint textview_button_released(GtkWidget *widget, GdkEventButton *event,
                        RemoteURI *uri = (RemoteURI *)cur->data;
 
                        if (textview->cur_pos >= uri->start &&
-                           textview->cur_pos <  uri->end) {
+                           textview->cur_pos <= uri->end) {
+                               gchar *trimmed_uri;
+                               
+                               trimmed_uri = trim_string(uri->uri, 60);
                                /* single click: display url in statusbar */
                                if (event->button == 1 && textview->last_buttonpress != GDK_2BUTTON_PRESS) {
                                        if (textview->messageview->mainwin) {
@@ -1893,7 +1864,7 @@ static gint textview_button_released(GtkWidget *widget, GdkEventButton *event,
                                                textview->show_url_msgid = gtk_statusbar_push(
                                                                GTK_STATUSBAR(textview->messageview->mainwin->statusbar),
                                                                textview->messageview->mainwin->folderview_cid,
-                                                               uri->uri);
+                                                               trimmed_uri);
                                                textview->show_url_timeout_tag = gtk_timeout_add( 4000, show_url_timeout_cb, textview );
                                                gtkut_widget_wait_for_draw(textview->messageview->mainwin->hbox_stat);
                                        }
@@ -1941,6 +1912,7 @@ static gint textview_button_released(GtkWidget *widget, GdkEventButton *event,
                                        open_uri(uri->uri,
                                                 prefs_common.uri_cmd);
                                }
+                               g_free(trimmed_uri);
                        }
                }
        }