prevent secondary selection claim after "add to sender" dialog
[claws.git] / src / textview.c
index 130e4fb5ce74e3511553458c740485e326e05144..3af5cc4e3493ed25c295b30d45f678aacf16bf1f 100644 (file)
@@ -45,6 +45,7 @@
 #include "gtkutils.h"
 #include "procmime.h"
 #include "html.h"
+#include "enriched.h"
 #include "compose.h"
 #include "addressbook.h"
 #include "displayheader.h"
@@ -97,12 +98,19 @@ static GdkColor error_color = {
 
 static GdkFont *spacingfont;
 
+static void textview_show_ertf         (TextView       *textview,
+                                        FILE           *fp,
+                                        CodeConverter  *conv);
 static void textview_show_html         (TextView       *textview,
                                         FILE           *fp,
                                         CodeConverter  *conv);
 static void textview_write_line                (TextView       *textview,
                                         const gchar    *str,
                                         CodeConverter  *conv);
+static void textview_write_link         (TextView      *textview,
+                                         const gchar    *url,
+                                        const gchar    *str,
+                                        CodeConverter  *conv);
 static GPtrArray *textview_scan_header (TextView       *textview,
                                         FILE           *fp);
 static void textview_show_header       (TextView       *textview,
@@ -286,18 +294,27 @@ void textview_show_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
                boundary_len = strlen(boundary);
        }
 
-       if (!boundary && mimeinfo->mime_type == MIME_TEXT) {
+       if (!boundary && (mimeinfo->mime_type == MIME_TEXT || mimeinfo->mime_type == MIME_TEXT_HTML || mimeinfo->mime_type == MIME_TEXT_ENRICHED)) {
+       
                if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
                        perror("fseek");
                headers = textview_scan_header(textview, fp);
        } else {
                if (mimeinfo->mime_type == MIME_TEXT && mimeinfo->parent) {
                        glong fpos;
+                       MimeInfo *parent = mimeinfo->parent;
+
+                       while (parent->parent) {
+                               if (parent->main &&
+                                   parent->main->mime_type ==
+                                       MIME_MESSAGE_RFC822)
+                                       break;
+                               parent = parent->parent;
+                       }
 
                        if ((fpos = ftell(fp)) < 0)
                                perror("ftell");
-                       else if (fseek(fp, mimeinfo->parent->fpos, SEEK_SET)
-                                < 0)
+                       else if (fseek(fp, parent->fpos, SEEK_SET) < 0)
                                perror("fseek");
                        else {
                                headers = textview_scan_header(textview, fp);
@@ -305,6 +322,7 @@ void textview_show_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
                                        perror("fseek");
                        }
                }
+               /* skip MIME part headers */
                while (fgets(buf, sizeof(buf), fp) != NULL)
                        if (buf[0] == '\r' || buf[0] == '\n') break;
        }
@@ -347,6 +365,8 @@ void textview_show_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
        if (tmpfp) {
                if (mimeinfo->mime_type == MIME_TEXT_HTML)
                        textview_show_html(textview, tmpfp, conv);
+               else if (mimeinfo->mime_type == MIME_TEXT_ENRICHED)
+                       textview_show_ertf(textview, tmpfp, conv);
                else
                        while (fgets(buf, sizeof(buf), tmpfp) != NULL)
                                textview_write_line(textview, buf, conv);
@@ -420,16 +440,55 @@ 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);
 
        while ((str = html_parse(parser)) != NULL) {
-               textview_write_line(textview, str, NULL);
+               if (parser->state == HTML_HREF) {
+                       /* first time : get and copy the URL */
+                       if (url == 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);
+                                       /* 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;
+                       }
+                       textview_write_line(textview, str, NULL);
+               }
        }
        html_parser_destroy(parser);
 }
 
+static void textview_show_ertf(TextView *textview, FILE *fp,
+                              CodeConverter *conv)
+{
+       ERTFParser *parser;
+       gchar *str;
+       gchar* url = NULL;
+
+       parser = ertf_parser_new(fp, conv);
+       g_return_if_fail(parser != NULL);
+
+       while ((str = ertf_parse(parser)) != NULL) {
+               textview_write_line(textview, str, NULL);
+       }
+       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,
@@ -456,8 +515,7 @@ static gboolean get_uri_part(const gchar *start, const gchar *scanpos,
         * 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) != '/')) 
+#define IS_REAL_PUNCT(ch)      (ispunct(ch) && ((ch) != '/')) 
 
        for (; ep_ - 1 > scanpos + 1 && IS_REAL_PUNCT(*(ep_ - 1)); ep_--)
                ;
@@ -482,6 +540,8 @@ static gchar *make_uri_string(const gchar *bp, const gchar *ep)
         !isspace(ch) && \
         !strchr("()<>\"", (ch)))
 
+/* alphabet and number within 7bit ASCII */
+#define IS_ASCII_ALNUM(ch)     (isascii(ch) && isalnum(ch))
 #define IS_QUOTE(ch) ((ch) == '\'' || (ch) == '"')
 
 /* get_email_part() - retrieves an email address. Returns TRUE if succesful */
@@ -511,7 +571,7 @@ static gboolean get_email_part(const gchar *start, const gchar *scanpos,
 
        /* TODO: should start with an alnum? */
        bp_++;
-       for (; bp_ < scanpos && !isalnum(*bp_); bp_++)
+       for (; bp_ < scanpos && !IS_ASCII_ALNUM(*bp_); bp_++)
                ;
 
        if (bp_ != scanpos) {
@@ -520,7 +580,7 @@ static gboolean get_email_part(const gchar *start, const gchar *scanpos,
                        ;
 
                /* TODO: really should terminate with an alnum? */
-               for (; ep_ > scanpos  && !isalnum(*ep_); --ep_)
+               for (; ep_ > scanpos && !IS_ASCII_ALNUM(*ep_); --ep_)
                        ;
                ep_++;
 
@@ -739,12 +799,46 @@ 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;
+    GtkText *text = GTK_TEXT(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 (!conv)
+           strncpy2(buf, str, sizeof(buf));
+    else if (conv_convert(conv, buf, sizeof(buf), str) < 0) {
+                   gtk_text_insert(text, textview->msgfont,
+                           prefs_common.enable_color
+                           ? &error_color : NULL, NULL,
+                           "*** Warning: code conversion failed ***\n",
+                           -1);
+           return;
+    }
+
+    /* 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_text_get_point(text);
+    gtk_text_insert(text, textview->msgfont, link_color, NULL, buf,
+                   strlen(buf));
+    uri->end = gtk_text_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)
 {
        GtkText *text = GTK_TEXT(textview->text);
        gchar buf[BUFFSIZE];
-       size_t len;
        GdkColor *fg_color;
        gint quotelevel = -1;
 
@@ -759,11 +853,7 @@ static void textview_write_line(TextView *textview, const gchar *str,
                return;
        }
 
-       len = strlen(buf);
-       if (len > 1 && buf[len - 1] == '\n' && buf[len - 2] == '\r') {
-               buf[len - 2] = '\n';
-               buf[len - 1] = '\0';
-       }
+       strcrchomp(buf);
        if (prefs_common.conv_mb_alnum) conv_mb_alnum(buf);
        fg_color = NULL;
 
@@ -858,8 +948,8 @@ void textview_set_font(TextView *textview, const gchar *codeset)
                GtkWidget *parent;
 
                parent = textview->scrolledwin_mb->parent;
-               gtk_editable_select_region
-                       (GTK_EDITABLE(textview->text_mb), 0, 0);
+               gtk_editable_claim_selection(GTK_EDITABLE(textview->text_mb),
+                                            FALSE, GDK_CURRENT_TIME);
                gtk_container_remove(GTK_CONTAINER(parent),
                                     textview->scrolledwin_mb);
                gtk_container_add(GTK_CONTAINER(parent),
@@ -871,8 +961,8 @@ void textview_set_font(TextView *textview, const gchar *codeset)
                GtkWidget *parent;
 
                parent = textview->scrolledwin_sb->parent;
-               gtk_editable_select_region
-                       (GTK_EDITABLE(textview->text_sb), 0, 0);
+               gtk_editable_claim_selection(GTK_EDITABLE(textview->text_sb),
+                                            FALSE, GDK_CURRENT_TIME);
                gtk_container_remove(GTK_CONTAINER(parent),
                                     textview->scrolledwin_sb);
                gtk_container_add(GTK_CONTAINER(parent),
@@ -1335,7 +1425,6 @@ static void textview_key_pressed(GtkWidget *widget, GdkEventKey *event,
                        textview_scroll_page(textview, FALSE);
                break;
        case GDK_BackSpace:
-       case GDK_Delete:
                textview_scroll_page(textview, TRUE);
                break;
        case GDK_Return:
@@ -1360,6 +1449,13 @@ static void textview_button_pressed(GtkWidget *widget, GdkEventButton *event,
             || event->button == 2 || event->button == 3)) {
                GSList *cur;
 
+               /* double click seems to set the cursor after the current
+                * word. The cursor position needs fixing, otherwise the
+                * last word of a clickable zone will not work */
+               if (event->button == 1 && event->type == GDK_2BUTTON_PRESS) {
+                       textview->cur_pos--;
+               }
+
                for (cur = textview->uri_list; cur != NULL; cur = cur->next) {
                        RemoteURI *uri = (RemoteURI *)cur->data;
 
@@ -1368,14 +1464,31 @@ static void textview_button_pressed(GtkWidget *widget, GdkEventButton *event,
                                if (!g_strncasecmp(uri->uri, "mailto:", 7)) {
                                        if (event->button == 3) {
                                                gchar *fromname, *fromaddress;
+                                               GdkEventButton tmpev;   
+                                               
                                                /* extract url */
                                                fromaddress = g_strdup(uri->uri + 7);
                                                /* Hiroyuki: please put this function in utils.c! */
                                                fromname = procheader_get_fromname(fromaddress);
                                                extract_address(fromaddress);
                                                g_message("adding from textview %s <%s>", fromname, fromaddress);
-                                               // Add to address book - Match
+                                               /* Add to address book - Match */
                                                addressbook_add_contact( fromname, fromaddress, NULL );
+                                               
+                                               /* force press and release at (0, 0) to work around secondary 
+                                                * selection claim */
+                                               tmpev         = *event;
+                                               tmpev.x_root -= tmpev.x;
+                                               tmpev.y_root -= tmpev.y;
+                                               tmpev.x       = 0;
+                                               tmpev.y       = 0;
+                                               tmpev.time    = GDK_CURRENT_TIME;
+                                               gtk_widget_event(widget, (GdkEvent *)&tmpev);
+
+                                               tmpev.type    = GDK_BUTTON_RELEASE;
+                                               tmpev.time    = GDK_CURRENT_TIME;
+                                               gtk_widget_event(widget, (GdkEvent *)&tmpev);
+
                                                g_free(fromaddress);
                                                g_free(fromname);
                                        } else {
@@ -1389,6 +1502,7 @@ static void textview_button_pressed(GtkWidget *widget, GdkEventButton *event,
                        }
                }
        }
+       return TRUE;
 }
 
 static void textview_uri_list_remove_all(GSList *uri_list)