2007-07-25 [iwkse] 2.10.0cvs60
[claws.git] / src / textview.c
index 8fe69e1df0b69f8c593ece70c03a0f54641a0f25..99d16f78e5437fa562e420c1fd0106680018e971 100644 (file)
@@ -4,7 +4,7 @@
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
@@ -13,8 +13,8 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
 
 #ifdef HAVE_CONFIG_H
@@ -68,8 +68,7 @@
 #include "base64.h"
 #include "inputdialog.h"
 #include "timing.h"
-
-gint previousquotelevel = -1;
+#include "tags.h"
 
 static GdkColor quote_colors[3] = {
        {(gulong)0, (gushort)0, (gushort)0, (gushort)0},
@@ -180,8 +179,6 @@ static gboolean textview_uri_button_pressed (GtkTextTag     *tag,
                                                 GtkTextIter    *iter,
                                                 TextView       *textview);
 
-static gboolean textview_uri_security_check    (TextView       *textview,
-                                                ClickableText  *uri);
 static void textview_uri_list_remove_all       (GSList         *uri_list);
 
 static void textview_toggle_quote              (TextView       *textview, 
@@ -362,8 +359,19 @@ static void textview_create_tags(GtkTextView *text, TextView *textview)
 {
        GtkTextBuffer *buffer;
        GtkTextTag *tag, *qtag;
+       static GdkColor yellow, black;
+       static gboolean color_init = FALSE;
        static PangoFontDescription *font_desc, *bold_font_desc;
        
+       if (!color_init) {
+               gdk_color_parse("#f5f6be", &yellow);
+               gdk_color_parse("#000000", &black);
+               color_init = gdk_colormap_alloc_color(
+                       gdk_colormap_get_system(), &yellow, FALSE, TRUE);
+               color_init &= gdk_colormap_alloc_color(
+                       gdk_colormap_get_system(), &black, FALSE, TRUE);
+       }
+
        if (!font_desc)
                font_desc = pango_font_description_from_string
                        (NORMAL_FONT);
@@ -425,6 +433,16 @@ static void textview_create_tags(GtkTextView *text, TextView *textview)
                                "foreground-gdk", &quote_colors[2],
                                NULL);
        }
+#if GTK_CHECK_VERSION(2, 8, 0)
+       gtk_text_buffer_create_tag(buffer, "tags",
+                       "foreground-gdk", &black,
+                       "paragraph-background-gdk", &yellow,
+                       NULL);
+#else
+       gtk_text_buffer_create_tag(buffer, "tags",
+                       "foreground-gdk", &emphasis_color,
+                       NULL);
+#endif
        gtk_text_buffer_create_tag(buffer, "emphasis",
                        "foreground-gdk", &emphasis_color,
                        NULL);
@@ -611,7 +629,7 @@ static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
                return;
        }
 
-       previousquotelevel = -1;
+       textview->prev_quote_level = -1;
 
        if ((mimeinfo->type == MIMETYPE_MESSAGE) && !g_ascii_strcasecmp(mimeinfo->subtype, "rfc822")) {
                FILE *fp;
@@ -826,6 +844,8 @@ void textview_show_mime_part(TextView *textview, MimeInfo *partinfo)
        GtkTextView *text;
        GtkTextBuffer *buffer;
        GtkTextIter iter;
+       const gchar *name;
+       gchar *content_type;
 
        if (!partinfo) return;
 
@@ -837,23 +857,48 @@ void textview_show_mime_part(TextView *textview, MimeInfo *partinfo)
        gtk_text_buffer_get_start_iter(buffer, &iter);
 
        TEXTVIEW_INSERT("\n");
-       TEXTVIEW_INSERT(_("  The following can be performed on this part by\n"));
-       TEXTVIEW_INSERT(_("  right-clicking the icon or list item:\n"));
+
+       name = procmime_mimeinfo_get_parameter(partinfo, "filename");
+       if (name == NULL)
+               name = procmime_mimeinfo_get_parameter(partinfo, "name");
+       if (name != NULL) {
+               content_type = procmime_get_content_type_str(partinfo->type,
+                                                    partinfo->subtype);
+               TEXTVIEW_INSERT("  ");
+               TEXTVIEW_INSERT_BOLD(name);
+               TEXTVIEW_INSERT(" (");
+               TEXTVIEW_INSERT(content_type);
+               TEXTVIEW_INSERT(", ");
+               TEXTVIEW_INSERT(to_human_readable(partinfo->length));
+               TEXTVIEW_INSERT("):\n\n");
+               
+               g_free(content_type);
+       }
+       TEXTVIEW_INSERT(_("  The following can be performed on this part\n"));
+#ifndef MAEMO
+       TEXTVIEW_INSERT(_("  by right-clicking the icon or list item:\n"));
+#endif
 
        TEXTVIEW_INSERT(_("     - To save, select "));
        TEXTVIEW_INSERT_LINK(_("'Save as...'"), "sc://save_as", NULL);
+#ifndef MAEMO
        TEXTVIEW_INSERT(_(" (Shortcut key: 'y')\n"));
+#endif
        TEXTVIEW_INSERT(_("     - To display as text, select "));
        TEXTVIEW_INSERT_LINK(_("'Display as text'"), "sc://display_as_text", NULL);
+#ifndef MAEMO
        TEXTVIEW_INSERT(_(" (Shortcut key: 't')\n"));
+#endif
        TEXTVIEW_INSERT(_("     - To open with an external program, select "));
        TEXTVIEW_INSERT_LINK(_("'Open'"), "sc://open", NULL);
+#ifndef MAEMO
        TEXTVIEW_INSERT(_(" (Shortcut key: 'l')\n"));
        TEXTVIEW_INSERT(_("       (alternately double-click, or click the middle "));
        TEXTVIEW_INSERT(_("mouse button)\n"));
        TEXTVIEW_INSERT(_("     - Or use "));
        TEXTVIEW_INSERT_LINK(_("'Open with...'"), "sc://open_with", NULL);
        TEXTVIEW_INSERT(_(" (Shortcut key: 'o')\n"));
+#endif
        textview_show_icon(textview, GTK_STOCK_DIALOG_INFO);
 }
 
@@ -1396,7 +1441,8 @@ static void textview_write_line(TextView *textview, const gchar *str,
                fg_color = quote_tag_str;
        }
 
-       if (prefs_common.enable_color && (strcmp(buf,"-- \n") == 0 || textview->is_in_signature)) {
+       if (prefs_common.enable_color && 
+           (strcmp(buf,"-- \n") == 0 || strcmp(buf, "- -- \n") == 0 || textview->is_in_signature)) {
                fg_color = "signature";
                textview->is_in_signature = TRUE;
        }
@@ -1410,7 +1456,7 @@ static void textview_write_line(TextView *textview, const gchar *str,
                        g_free(utf8buf);
                }
 do_quote:
-               if ( previousquotelevel != real_quotelevel ) {
+               if ( textview->prev_quote_level != real_quotelevel ) {
                        ClickableText *uri;
                        uri = g_new0(ClickableText, 1);
                        uri->uri = g_strdup("");
@@ -1430,14 +1476,20 @@ do_quote:
                        textview->uri_list =
                                g_slist_prepend(textview->uri_list, uri);
                
-                       previousquotelevel = real_quotelevel;
+                       textview->prev_quote_level = real_quotelevel;
                } else {
                        GSList *last = textview->uri_list;
-                       ClickableText *lasturi = (ClickableText *)last->data;
+                       ClickableText *lasturi = NULL;
                        gint e_len = 0, n_len = 0;
                        
+                       if (textview->uri_list) {
+                               lasturi = (ClickableText *)last->data;
+                       } else {
+                               printf("oops (%d %d)\n",
+                                       real_quotelevel, textview->prev_quote_level);
+                       }               
                        if (lasturi->is_quote == FALSE) {
-                               previousquotelevel = -1;
+                               textview->prev_quote_level = -1;
                                goto do_quote;
                        }
                        e_len = lasturi->data ? strlen(lasturi->data):0;
@@ -1448,7 +1500,7 @@ do_quote:
                }
        } else {
                textview_make_clickable_parts(textview, fg_color, "link", buf, FALSE);
-               previousquotelevel = -1;
+               textview->prev_quote_level = -1;
        }
 }
 
@@ -1528,6 +1580,7 @@ void textview_clear(TextView *textview)
        textview_uri_list_remove_all(textview->uri_list);
        textview->uri_list = NULL;
        textview->uri_hover = NULL;
+       textview->prev_quote_level = -1;
 
        textview->body_pos = 0;
        if (textview->image) 
@@ -1552,6 +1605,7 @@ void textview_destroy(TextView *textview)
 
        textview_uri_list_remove_all(textview->uri_list);
        textview->uri_list = NULL;
+       textview->prev_quote_level = -1;
 
        g_free(textview);
 }
@@ -1851,6 +1905,57 @@ bail:
 }
 #endif
 
+static void textview_show_tags(TextView *textview)
+{
+       MsgInfo *msginfo = textview->messageview->msginfo;
+       GtkTextView *text = GTK_TEXT_VIEW(textview->text);
+       GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
+       GtkTextIter iter;
+       ClickableText *uri;
+       GSList *cur;
+       gboolean found_tag = FALSE;
+       
+       if (!msginfo->tags)
+               return;
+       
+       for (cur = msginfo->tags; cur; cur = cur->next) {
+               if (tags_get_tag(GPOINTER_TO_INT(cur->data)) != NULL) {
+                       found_tag = TRUE;
+                       break;
+               }
+       }
+       if (!found_tag) 
+               return;
+
+       gtk_text_buffer_get_end_iter (buffer, &iter);
+       gtk_text_buffer_insert_with_tags_by_name(buffer,
+               &iter, _("Tags: "), -1,
+               "header_title", "header", "tags", NULL);
+
+       for (cur = msginfo->tags; cur; cur = cur->next) {
+               uri = g_new0(ClickableText, 1);
+               uri->uri = g_strdup("");
+               uri->start = gtk_text_iter_get_offset(&iter);
+               gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, 
+                       tags_get_tag(GPOINTER_TO_INT(cur->data)), -1,
+                       "link", "header", "tags", NULL);
+               uri->end = gtk_text_iter_get_offset(&iter);
+               uri->filename = g_strdup_printf("sc://search_tags:%s", tags_get_tag(GPOINTER_TO_INT(cur->data)));
+               uri->data = NULL;
+               textview->uri_list =
+                       g_slist_prepend(textview->uri_list, uri);
+               if (cur->next)
+                       gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, ", ", 2,
+                               "header", "tags", NULL);
+               else
+                       gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, " ", 1,
+                               "header", "tags", NULL);
+       }
+
+       gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, "\n", 1,
+               "header", "tags", NULL);
+}
+
 static void textview_show_header(TextView *textview, GPtrArray *headers)
 {
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
@@ -1861,6 +1966,8 @@ static void textview_show_header(TextView *textview, GPtrArray *headers)
 
        g_return_if_fail(headers != NULL);
 
+       textview_show_tags(textview);
+
        for (i = 0; i < headers->len; i++) {
                header = g_ptr_array_index(headers, i);
                g_return_if_fail(header->name != NULL);
@@ -1995,6 +2102,7 @@ static gint textview_key_pressed(GtkWidget *widget, GdkEventKey *event,
                mimeview_scroll_page(messageview->mimeview, TRUE);
                break;
        case GDK_Return:
+       case GDK_KP_Enter:
                mimeview_scroll_one_line
                        (messageview->mimeview, (event->state &
                                    (GDK_SHIFT_MASK|GDK_MOD1_MASK)) != 0);
@@ -2006,7 +2114,9 @@ static gint textview_key_pressed(GtkWidget *widget, GdkEventKey *event,
        case GDK_y:
        case GDK_t:
        case GDK_l:
+       case GDK_o:
        case GDK_c:
+       case GDK_a:
                if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) == 0) {
                        KEY_PRESS_EVENT_STOP();
                        mimeview_pass_key_press_event(messageview->mimeview,
@@ -2412,6 +2522,8 @@ static gboolean textview_uri_button_pressed(GtkTextTag *tag, GObject *obj,
                                        folder_item = textview->messageview->msginfo->folder;
                                        if (folder_item->prefs && folder_item->prefs->enable_default_account)
                                                account = account_find_from_id(folder_item->prefs->default_account);
+                                       if (!account)
+                                               account = account_find_from_item(folder_item);
                                }
                                compose_new_with_folderitem(account, folder_item, uri->uri + 7);
                        }
@@ -2446,6 +2558,20 @@ static gboolean textview_uri_button_pressed(GtkTextTag *tag, GObject *obj,
        return FALSE;
 }
 
+gchar *textview_get_visible_uri                (TextView       *textview, 
+                                        ClickableText  *uri)
+{
+       GtkTextBuffer *buffer;
+       GtkTextIter start, 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);
+
+       return gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
+}
+
 /*!
  *\brief    Check to see if a web URL has been disguised as a different
  *          URL (possible with HTML email).
@@ -2457,23 +2583,15 @@ static gboolean textview_uri_button_pressed(GtkTextTag *tag, GObject *obj,
  *\return   gboolean TRUE if the URL is ok, or if the user chose to open
  *          it anyway, otherwise FALSE          
  */
-static gboolean textview_uri_security_check(TextView *textview, ClickableText *uri)
+gboolean textview_uri_security_check(TextView *textview, ClickableText *uri)
 {
        gchar *visible_str;
        gboolean retval = TRUE;
-       GtkTextBuffer *buffer;
-       GtkTextIter start, end;
 
        if (is_uri_string(uri->uri) == FALSE)
                return TRUE;
 
-       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);
-
+       visible_str = textview_get_visible_uri(textview, uri);
        if (visible_str == NULL)
                return TRUE;