2004-11-19 [colin] 0.9.12cvs158.3
[claws.git] / src / textview.c
index a6c3381946893d80a517f3934ec22e45a0a3cfef..1476bab0a7b9b075b014e9aecc75b810ccfbcab1 100644 (file)
@@ -51,6 +51,7 @@
 #include "account.h"
 #include "mimeview.h"
 #include "alertpanel.h"
+#include "menu.h"
 
 struct _RemoteURI
 {
@@ -185,12 +186,19 @@ static gboolean textview_uri_security_check       (TextView       *textview,
                                                 RemoteURI      *uri);
 static void textview_uri_list_remove_all       (GSList         *uri_list);
 
+static void open_uri_cb                                (TextView       *textview,
+                                                guint           action,
+                                                void           *data);
+static void copy_uri_cb                                (TextView       *textview,
+                                                guint           action,
+                                                void           *data);
 
-static void populate_popup(GtkTextView *textview, GtkMenu *menu,
-                          gpointer *dummy)
+static GtkItemFactoryEntry textview_popup_entries[] = 
 {
-       gtk_menu_detach(menu);
-}
+       {N_("/_Open link"),             NULL, open_uri_cb, 0, NULL},
+       {N_("/_Copy link location"),    NULL, copy_uri_cb, 0, NULL},
+};
+
 
 TextView *textview_create(void)
 {
@@ -200,6 +208,9 @@ TextView *textview_create(void)
        GtkWidget *text;
        GtkTextBuffer *buffer;
        GtkClipboard *clipboard;
+       GtkItemFactory *popupfactory;
+       GtkWidget *popupmenu;
+       gint n_entries;
        PangoFontDescription *font_desc = NULL;
 
        debug_print("Creating text view...\n");
@@ -219,9 +230,6 @@ TextView *textview_create(void)
        gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
        gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD_CHAR);
        gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(text), FALSE);
-       g_signal_connect(G_OBJECT(text), "populate-popup",
-                G_CALLBACK(populate_popup), NULL);
-
 
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
        clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
@@ -266,6 +274,12 @@ TextView *textview_create(void)
 
        gtk_widget_show(vbox);
 
+       n_entries = sizeof(textview_popup_entries) /
+               sizeof(textview_popup_entries[0]);
+       popupmenu = menu_create_items(textview_popup_entries, n_entries,
+                                     "<UriPopupMenu>", &popupfactory,
+                                     textview);
+
        textview->vbox             = vbox;
        textview->scrolledwin      = scrolledwin;
        textview->text             = text;
@@ -273,6 +287,8 @@ TextView *textview_create(void)
        textview->body_pos         = 0;
        textview->show_all_headers = FALSE;
        textview->last_buttonpress = GDK_NOTHING;
+       textview->popup_menu       = popupmenu;
+       textview->popup_factory    = popupfactory;
 
        return textview;
 }
@@ -480,8 +496,8 @@ static void recursive_add_parts(TextView *textview, GNode *node)
                 prefered_body = NULL;
                 prefered_score = 0;
                 
-                for(iter = g_node_first_child(node) ; iter != NULL ;
-                    iter = g_node_next_sibling(iter)) {
+                for (iter = g_node_first_child(node) ; iter != NULL ;
+                     iter = g_node_next_sibling(iter)) {
                         int score;
                         MimeInfo * submime;
                         
@@ -506,8 +522,8 @@ static void recursive_add_parts(TextView *textview, GNode *node)
                 }
         }
         else {
-                for(iter = g_node_first_child(node) ; iter != NULL ;
-                    iter = g_node_next_sibling(iter)) {
+                for (iter = g_node_first_child(node) ; iter != NULL ;
+                     iter = g_node_next_sibling(iter)) {
                         recursive_add_parts(textview, iter);
                 }
         }
@@ -805,7 +821,7 @@ static gboolean get_email_part(const gchar *start, const gchar *scanpos,
        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. */
@@ -873,8 +889,30 @@ static gboolean get_email_part(const gchar *start, const gchar *scanpos,
 
        if (!result) return FALSE;
 
+       if (*(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_)) 
+       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. */
@@ -937,7 +975,7 @@ static gboolean get_email_part(const gchar *start, const gchar *scanpos,
 
        *ep = ep_;
        *bp = bp_;
-
+       
        return result;
 }
 
@@ -1029,7 +1067,7 @@ static void textview_make_clickable_parts(TextView *textview,
                const gchar     *bp, *ep;       /* text position */
                gint             pti;           /* index in parse table */
                struct txtpos   *next;          /* next */
-       } head = {NULL, NULL, 0,  NULL}, *last = &head, *next = NULL;
+       } head = {NULL, NULL, 0,  NULL}, *last = &head;
 
        gtk_text_buffer_get_end_iter(buffer, &iter);
 
@@ -1072,15 +1110,6 @@ static void textview_make_clickable_parts(TextView *textview,
                for (last = head.next; last != NULL;
                     normal_text = last->ep, last = last->next) {
                        RemoteURI *uri;
-                       next = last->next;
-                       /* fix "colin@colino.net" <colin@colino.net> types of URIs
-                       /* FIXME would be better to fix it in the email parser */
-                       if (next && next->bp == last->ep) {
-                               next->bp = last->bp;
-                               if (*(next->bp -1 )=='"' && strchr(next->bp, '"'))
-                                       next->bp--;
-                               continue;
-                       }
                        uri = g_new(RemoteURI, 1);
                        if (last->bp - normal_text > 0)
                                gtk_text_buffer_insert_with_tags_by_name
@@ -1768,14 +1797,18 @@ static gint textview_key_pressed(GtkWidget *widget, GdkEventKey *event,
                if (summaryview)
                        summary_pass_key_press_event(summaryview, event);
                else
-                       textview_scroll_page(textview, FALSE);
+                       textview_scroll_page
+                               (textview,
+                                (event->state &
+                                 (GDK_SHIFT_MASK|GDK_MOD1_MASK)) != 0);
                break;
        case GDK_BackSpace:
                textview_scroll_page(textview, TRUE);
                break;
        case GDK_Return:
-               textview_scroll_one_line(textview,
-                                        (event->state & GDK_MOD1_MASK) != 0);
+               textview_scroll_one_line
+                       (textview, (event->state &
+                                   (GDK_SHIFT_MASK|GDK_MOD1_MASK)) != 0);
                break;
        case GDK_Delete:
                if (summaryview)
@@ -1807,70 +1840,6 @@ static gint textview_key_pressed(GtkWidget *widget, GdkEventKey *event,
        return TRUE;
 }
 
-/*!
- *\brief    Check to see if a web URL has been disguised as a different
- *          URL (possible with HTML email).
- *
- *\param    uri The uri to check
- *
- *\param    textview The TextView the URL is contained in
- *
- *\return   gboolean TRUE if the URL is ok, or if the user chose to open
- *          it anyway, otherwise FALSE          
- */
-static gboolean uri_security_check(RemoteURI *uri, TextView *textview) 
-{
-       gchar *clicked_str;
-       gboolean retval = TRUE;
-
-       if (g_ascii_strncasecmp(uri->uri, "http:", 5) &&
-           g_ascii_strncasecmp(uri->uri, "https:", 6) &&
-           g_ascii_strncasecmp(uri->uri, "www.", 4)) 
-               return retval;
-
-       clicked_str = gtk_editable_get_chars(GTK_EDITABLE(textview->text),
-                                            uri->start,
-                                            uri->end);
-       if (clicked_str == NULL)
-               return TRUE;
-
-       if (strcmp(clicked_str, uri->uri) &&
-           (!g_ascii_strncasecmp(clicked_str, "http:",  5) ||
-            !g_ascii_strncasecmp(clicked_str, "https:", 6) ||
-            !g_ascii_strncasecmp(clicked_str, "www.",   4))) {
-               gchar *str;
-               retval = FALSE;
-
-               /* allow uri->uri    == http://somewhere.com
-                  and   clicked_str ==        somewhere.com */
-               str = g_strconcat("http://", clicked_str, NULL);
-
-               if (!g_ascii_strcasecmp(str, uri->uri))
-                       retval = TRUE;
-               g_free(str);
-       }
-
-       if (retval == FALSE) {
-               gchar *msg = NULL;
-               AlertValue resp;
-
-               msg = g_strdup_printf(_("The real URL (%s) is different from\n"
-                                       "the apparent URL (%s).  \n"
-                                       "Open it anyway?"),
-                                       uri->uri, clicked_str);
-               resp = alertpanel_with_type(_("Warning"), 
-                                 msg,
-                                 _("Yes"), 
-                                 _("No"),
-                                 NULL, NULL, ALERT_WARNING);
-               g_free(msg);
-               if (resp == G_ALERTDEFAULT)
-                       retval = TRUE;
-       } 
-       g_free(clicked_str);
-       return retval;
-}
-
 static gboolean textview_motion_notify(GtkWidget *widget,
                                       GdkEventMotion *event,
                                       TextView *textview)
@@ -1895,7 +1864,15 @@ static gboolean textview_visibility_notify(GtkWidget *widget,
                                           TextView *textview)
 {
        gint wx, wy;
-  
+       GdkWindow *window;
+
+       window = gtk_text_view_get_window(GTK_TEXT_VIEW(widget),
+                                         GTK_TEXT_WINDOW_TEXT);
+
+       /* check if occurred for the text window part */
+       if (window != event->window)
+               return FALSE;
+       
        gdk_window_get_pointer(widget->window, &wx, &wy, NULL);
        textview_uri_update(textview, wx, wy);
 
@@ -1987,15 +1964,15 @@ static gboolean textview_get_uri_range(TextView *textview,
 {
        GtkTextIter _start_iter, _end_iter;
 
-       _start_iter = *iter;
-       if(!gtk_text_iter_backward_to_tag_toggle(&_start_iter, tag)) {
-               debug_print("Can't find start.");
+       _end_iter = *iter;
+       if (!gtk_text_iter_forward_to_tag_toggle(&_end_iter, tag)) {
+               debug_print("Can't find end");
                return FALSE;
        }
 
-       _end_iter = *iter;
-       if(!gtk_text_iter_forward_to_tag_toggle(&_end_iter, tag)) {
-               debug_print("Can't find end");
+       _start_iter = _end_iter;
+       if (!gtk_text_iter_backward_to_tag_toggle(&_start_iter, tag)) {
+               debug_print("Can't find start.");
                return FALSE;
        }
 
@@ -2113,9 +2090,18 @@ static gboolean textview_uri_button_pressed(GtkTextTag *tag, GObject *obj,
                        }
                        return TRUE;
                } else {
-                       if (textview_uri_security_check(textview, uri) == TRUE) 
-                               open_uri(uri->uri,
-                                        prefs_common.uri_cmd);
+                       if (bevent->button == 1 &&
+                           textview_uri_security_check(textview, uri) == TRUE) 
+                                       open_uri(uri->uri,
+                                                prefs_common.uri_cmd);
+                       else if (bevent->button == 3) {
+                               g_object_set_data(
+                                       G_OBJECT(textview->popup_menu),
+                                       "menu_button", uri);
+                               gtk_menu_popup(GTK_MENU(textview->popup_menu), 
+                                              NULL, NULL, NULL, NULL, 
+                                              bevent->button, bevent->time);
+                       }
                        return TRUE;
                }
        }
@@ -2138,12 +2124,19 @@ static gboolean textview_uri_security_check(TextView *textview, RemoteURI *uri)
 {
        gchar *visible_str;
        gboolean retval = TRUE;
+       GtkTextBuffer *buffer;
+       GtkTextIter start, end;
 
        if (is_uri_string(uri->uri) == FALSE)
                return TRUE;
 
-       visible_str = gtk_editable_get_chars(GTK_EDITABLE(textview->text),
-                                            uri->start, uri->end);
+       buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
+
+       gtk_text_buffer_get_iter_at_offset(buffer, &start, uri->start);
+       gtk_text_buffer_get_iter_at_offset(buffer, &end,   uri->end);
+
+       visible_str = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
+
        if (visible_str == NULL)
                return TRUE;
 
@@ -2190,3 +2183,29 @@ static void textview_uri_list_remove_all(GSList *uri_list)
 
        g_slist_free(uri_list);
 }
+
+static void open_uri_cb (TextView *textview, guint action, void *data)
+{
+       RemoteURI *uri = g_object_get_data(G_OBJECT(textview->popup_menu),
+                                          "menu_button");
+       if (uri == NULL)
+               return;
+
+       if (textview_uri_security_check(textview, uri) == TRUE) 
+               open_uri(uri->uri,
+                        prefs_common.uri_cmd);
+       g_object_set_data(G_OBJECT(textview->popup_menu), "menu_button",
+                         NULL);
+}
+
+static void copy_uri_cb        (TextView *textview, guint action, void *data)
+{
+       RemoteURI *uri = g_object_get_data(G_OBJECT(textview->popup_menu),
+                                          "menu_button");
+       if (uri == NULL)
+               return;
+
+       gtk_clipboard_set_text(gtk_clipboard_get(GDK_NONE), uri->uri, -1);
+       g_object_set_data(G_OBJECT(textview->popup_menu), "menu_button",
+                         NULL);
+}