* moved most of the quote code from modest-ui to (new) modest-tny-msg-actions
authorArne Zellentin <arne@kernelconcepts.de>
Mon, 29 May 2006 15:10:38 +0000 (15:10 +0000)
committerArne Zellentin <arne@kernelconcepts.de>
Mon, 29 May 2006 15:10:38 +0000 (15:10 +0000)
* general cleanup (still some left to do)

pmo-trunk-r120

src/Makefile.am
src/gtk-glade/modest-ui.c
src/modest-tny-msg-actions.c [new file with mode: 0644]
src/modest-tny-msg-actions.h [new file with mode: 0644]
src/modest-tny-msg-view.c
src/modest-tny-msg-view.h

index 1ac803d..5a3b731 100644 (file)
@@ -33,6 +33,8 @@ modest_SOURCES=\
        modest-tny-header-tree-view.c\
        modest-tny-msg-view.h\
        modest-tny-msg-view.c\
+       modest-tny-msg-actions.h\
+       modest-tny-msg-actions.c\
        modest-proto.h\
        modest-proto.c\
        modest-ui.h\
index 42539f6..f50eae3 100644 (file)
@@ -25,6 +25,7 @@
 #include "../modest-tny-transport-actions.h"
 
 #include "../modest-text-utils.h"
+#include "../modest-tny-msg-actions.h"
 
 #define MODEST_GLADE          PREFIX "/share/modest/glade/modest.glade"
 #define MODEST_GLADE_MAIN_WIN "main"
@@ -65,6 +66,8 @@ static void on_send_button_clicked (GtkWidget *widget, ModestUI *modest_ui);
 
 static void register_toolbar_callbacks (ModestUI *modest_ui);
 
+static void reply_to_msg (ModestUI *modest_ui, TnyMsgHeaderIface *header,
+                                               ModestTnyMsgView *msg_view);
 
 /* list my signals */
 enum {
@@ -645,54 +648,8 @@ on_new_mail_clicked (GtkWidget *widget, ModestUI *modest_ui)
        modest_ui_show_edit_window (modest_ui, "", "", "", "", "", NULL);
 }
 
-
-static gchar *
-modest_ui_quote_msg(const TnyMsgIface *src, const gchar *from, time_t sent_date)
-{
-       GList *parts;
-       TnyMsgMimePartIface *part;
-       TnyStreamIface* stream;
-       TnyTextBufferStream *dest;
-       TnyMsgMimePartIface *body = NULL;
-       GtkTextBuffer *buf;
-       gint limit = 76; /* TODO: make configurable */
-       gchar *quoted;
-       
-       buf = gtk_text_buffer_new(NULL);
-       dest = tny_text_buffer_stream_new(buf);
-       
-       /* is the warning in this line due to a bug in tinymail? */
-       parts  = (GList*) tny_msg_iface_get_parts (src);
-
-       while (parts) {
-               /* TODO: maybe we'd like to quote more than one part? */
-               TnyMsgMimePartIface *part =
-                       TNY_MSG_MIME_PART_IFACE(parts->data);
-               if (tny_msg_mime_part_iface_content_type_is (part, "text/plain")) {
-                       body = part;
-                       break;
-               }
-               parts = parts->next;
-       }
-       if (!body) {
-               return NULL;
-       }
-       buf    = gtk_text_buffer_new (NULL);
-       stream = TNY_STREAM_IFACE(tny_text_buffer_stream_new (buf));
-
-       tny_stream_iface_reset (stream);
-       tny_msg_mime_part_iface_decode_to_stream (body, stream);
-       tny_stream_iface_reset (stream);
-
-       quoted = modest_text_utils_quote (buf, from, sent_date, limit);
-               
-       g_object_unref(stream);
-       g_object_unref(buf);
-       return quoted;
-}
-
 static void
-modest_ui_reply_to_msg (ModestUI *modest_ui, TnyMsgHeaderIface *header,
+reply_to_msg (ModestUI *modest_ui, TnyMsgHeaderIface *header,
                                                ModestTnyMsgView *msg_view) {
        const TnyMsgIface *msg;
        const TnyMsgFolderIface *folder;
@@ -701,41 +658,37 @@ modest_ui_reply_to_msg (ModestUI *modest_ui, TnyMsgHeaderIface *header,
        GtkTextBuffer *unquoted;
        gchar *quoted;
        time_t sent_date;
+       gint line_limit = 76;
        
-       quoted = "";
-       if (header) {
-               folder = tny_msg_header_iface_get_folder (TNY_MSG_HEADER_IFACE(header));
-               if (!folder) {
-                       g_warning ("cannot find folder");
-                       return;
-               }
-
-               msg = tny_msg_folder_iface_get_message (TNY_MSG_FOLDER_IFACE(folder), header);
-               if (!msg) {
-                       g_warning ("cannot find msg");
-                       return;
-               }
-               subject = tny_msg_header_iface_get_subject(header);
-               re_sub = g_string_new(subject);
-               g_string_prepend(re_sub, "Re: ");
-               /* FIXME: honor replyto, cc */
-               from = tny_msg_header_iface_get_from(header);
-               sent_date = tny_msg_header_iface_get_date_sent(header);
-               
-               unquoted = modest_tny_msg_view_get_selected(msg_view);
-               
-               if (unquoted != NULL) {
-                       quoted = modest_text_utils_quote (unquoted, from, sent_date, 66);
-               } else {
-                       quoted = modest_ui_quote_msg(msg, from, sent_date);
-               }
-       } else {
+       if (!header) {
                g_warning("no header");
                return;
        }
+       folder = tny_msg_header_iface_get_folder (TNY_MSG_HEADER_IFACE(header));
+       if (!folder) {
+               g_warning ("cannot find folder");
+               return;
+       }
 
+       msg = tny_msg_folder_iface_get_message (TNY_MSG_FOLDER_IFACE(folder), header);
+       if (!msg) {
+               g_warning ("cannot find msg");
+               return;
+       }
+       subject = tny_msg_header_iface_get_subject(header);
+       re_sub = g_string_new(subject);
+       g_string_prepend(re_sub, "Re: ");
+       /* FIXME: honor replyto, cc */
+       from = tny_msg_header_iface_get_from(header);
+       sent_date = tny_msg_header_iface_get_date_sent(header);
+       
+       unquoted = modest_tny_msg_view_get_selected_text(msg_view);
+       quoted = modest_tny_msg_actions_quote(msg, from, sent_date, line_limit, unquoted);
+       
        modest_ui_show_edit_window (modest_ui, from, /* cc */ "", /* bcc */ "", re_sub->str, quoted, NULL);
        g_free(quoted);
+       if (unquoted != NULL)
+               g_object_unref(unquoted);
        g_string_free(re_sub, TRUE);
 }
 
@@ -784,7 +737,7 @@ on_reply_clicked (GtkWidget *widget, ModestUI *modest_ui)
                            TNY_MSG_HEADER_LIST_MODEL_INSTANCE_COLUMN,
                            &header, -1);
 
-       modest_ui_reply_to_msg (modest_ui, header, msg_view);
+       reply_to_msg (modest_ui, header, msg_view);
 }
 
 
diff --git a/src/modest-tny-msg-actions.c b/src/modest-tny-msg-actions.c
new file mode 100644 (file)
index 0000000..cf40598
--- /dev/null
@@ -0,0 +1,133 @@
+/* modest-ui.c */
+
+/* insert (c)/licensing information) */
+
+#include <gtk/gtk.h>
+#include <gtkhtml/gtkhtml.h>
+
+/* TODO: put in auto* */
+#include <tny-text-buffer-stream.h>
+#include <tny-msg-mime-part-iface.h>
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif /*HAVE_CONFIG_H*/
+
+#include "modest-text-utils.h"
+
+
+/* private */
+static gchar *quote_msg(const TnyMsgIface *src, const gchar *from, time_t sent_date, gint limit, gboolean textorhtml);
+static GtkTextBuffer *htmltotext(TnyMsgMimePartIface * body);
+
+
+static GtkTextBuffer *
+htmltotext(TnyMsgMimePartIface * body)
+{
+       GtkTextBuffer *buf;
+#ifdef ACTIVATE_HACKS
+       GtkWidget *html, *win;
+       TnyStreamIface *stream; 
+       GtkClipboard *clip;
+       gchar *text;
+       
+       win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
+       html = gtk_html_new();
+       gtk_container_add(GTK_CONTAINER(win), html);
+       
+       gtk_html_set_editable        (GTK_HTML(html), FALSE);
+       gtk_html_allow_selection     (GTK_HTML(html), TRUE);
+       gtk_html_set_caret_mode      (GTK_HTML(html), FALSE);
+       gtk_html_set_blocking        (GTK_HTML(html), FALSE);
+       gtk_html_set_images_blocking (GTK_HTML(html), FALSE);
+       
+       stream = TNY_STREAM_IFACE(modest_tny_stream_gtkhtml_new(
+               gtk_html_begin(GTK_HTML(html))));
+       
+       tny_stream_iface_reset (stream);
+       tny_msg_mime_part_iface_decode_to_stream (body, stream);
+       tny_stream_iface_reset (stream);
+       g_object_unref (G_OBJECT(stream));
+       
+       gtk_widget_show_all(win);
+       gtk_html_select_all(GTK_HTML(html));
+       clip = gtk_widget_get_clipboard(html, GDK_SELECTION_PRIMARY);
+       //clip = gtk_widget_get_clipboard(html, GDK_SELECTION_CLIPBOARD);
+       text = gtk_clipboard_wait_for_text(clip);
+       
+       buf = gtk_text_buffer_new(NULL);
+       gtk_text_buffer_set_text(buf, text, -1);
+       g_free(text);
+       /* destroy win & html */
+#else
+       buf = gtk_text_buffer_new(NULL);
+#endif
+       return buf;
+}
+
+gchar *
+modest_tny_msg_actions_quote(const TnyMsgIface *self, const gchar *from, time_t sent_date, gint limit, GtkTextBuffer *to_quote)
+{
+       gchar *quoted;
+
+       /* 3 cases: */
+       
+       /* a) quote text from selection */
+       if (to_quote != NULL) {
+               return modest_text_utils_quote(to_quote, from, sent_date, limit);
+       }
+               
+       /* b) try to find a text/plain part in the msg and quote it */
+       quoted = quote_msg(self, from, sent_date, limit, FALSE);
+       if (quoted != NULL)
+               return quoted;
+       
+       /* c) if that fails, try text/html */
+       return quote_msg(self, from, sent_date, limit, TRUE);
+}
+       
+       
+static gchar *
+quote_msg(const TnyMsgIface *src, const gchar *from, time_t sent_date, gint limit, gboolean textorhtml)
+{
+
+       GList *parts; /* LEAK? */
+       TnyStreamIface* stream;
+       TnyTextBufferStream *dest;
+       TnyMsgMimePartIface *body = NULL;
+       TnyMsgMimePartIface *part;
+       GtkTextBuffer *buf;
+       gchar *quoted;
+       
+       /* is the warning in this line due to a bug in tinymail? */
+       parts  = (GList*) tny_msg_iface_get_parts (src);
+
+       while (parts) {
+               part = TNY_MSG_MIME_PART_IFACE(parts->data);
+               if (tny_msg_mime_part_iface_content_type_is (part, textorhtml ? "text/html" : "text/plain")) {
+                       body = part;
+                       break;
+               }
+               parts = parts->next;
+       }
+       
+       if (!body) {
+               return NULL;
+       }
+       
+       if (textorhtml == TRUE) {
+               buf = htmltotext(body);
+       } else {
+               buf    = gtk_text_buffer_new (NULL);
+               stream = TNY_STREAM_IFACE(tny_text_buffer_stream_new (buf));
+               tny_stream_iface_reset (stream);
+               tny_msg_mime_part_iface_decode_to_stream (body, stream);
+               tny_stream_iface_reset (stream);
+       }
+       
+       quoted = modest_text_utils_quote (buf, from, sent_date, limit);
+               
+       g_object_unref(stream);
+       g_object_unref(buf);
+       return quoted;
+}
diff --git a/src/modest-tny-msg-actions.h b/src/modest-tny-msg-actions.h
new file mode 100644 (file)
index 0000000..9253a67
--- /dev/null
@@ -0,0 +1,22 @@
+/* modest-tny-msg-actions.h */
+/* insert (c)/licensing information) */
+
+#ifndef __MODEST_TNY_MSG_ACTIONS_H__
+#define __MODEST_TNY_MSG_ACTIONS_H__
+
+/* public */
+
+/**
+ * modest_tny_msg_actions_quote:
+ * @self: the message to quote
+ * 
+ * Returns: a string containing the quoted message
+ */
+
+gchar *modest_tny_msg_actions_quote(const TnyMsgIface *self,
+                                    const gchar *from,
+                                    time_t sent_date,
+                                    gint limit,
+                                                                       GtkTextBuffer *to_quote);
+
+#endif /* __MODEST_TNY_MSG_ACTIONS_H__*/
index 5cc8f63..d1f66af 100644 (file)
@@ -509,52 +509,8 @@ set_text_message (ModestTnyMsgView *self, TnyMsgMimePartIface *tny_body)
        return TRUE;
 }
 
-/* WIP (az), unused */
-char *
-get_all_text(ModestTnyMsgView *self)
-{
-       GtkWidget *html;
-       ModestTnyMsgViewPrivate *priv;
-       GSList *list;
-       GString *str = g_string_new ("");
-       gchar *ptr;
-       gint id = 0;
-       GObject *obj;
-       //HtmlBoxText *text;
-       
-       g_return_if_fail (self);
-       priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
-       html = priv->gtkhtml;
-       
-       //list = view->sel_list;
-       
-       //if (view->sel_list == NULL)
-               //      return NULL;
-       
-       while (obj = gtk_html_get_object_by_id(GTK_HTML(html), id)) {
-               //text = HTML_BOX_TEXT (obj->data);
-
-               //list = list->next;
-               /*
-                * Some boxes may not have any text
-                */
-               //if (text->canon_text == NULL)
-               //      continue;
-               //ptr = (gchar *)text->canon_text;
-               //g_string_append_len (str, ptr, text->length);
-               printf("obj#%d\n", id);
-               id++;
-                       
-       }
-       ptr = str->str;
-       g_string_free (str, FALSE);
-       return ptr;
-
-}
-
-
 GtkTextBuffer *
-modest_tny_msg_view_get_selected (ModestTnyMsgView *self)
+modest_tny_msg_view_get_selected_text (ModestTnyMsgView *self)
 {
        ModestTnyMsgViewPrivate *priv;
        gchar *sel;
@@ -571,9 +527,8 @@ modest_tny_msg_view_get_selected (ModestTnyMsgView *self)
        /* I'm sure there is a better way to check for selected text */
        sel = gtk_html_get_selection_html(GTK_HTML(html), &len);
        
-       /* and I hope we can get rid of this hack soon: */
        if (sel == NULL)
-               gtk_html_select_all(GTK_HTML(html));
+               return NULL;
        
        clip = gtk_widget_get_clipboard(html, GDK_SELECTION_PRIMARY);
        text = gtk_clipboard_wait_for_text(clip);
index 6dfa856..ba98311 100644 (file)
@@ -66,7 +66,7 @@ GtkWidget*   modest_tny_msg_view_new          (TnyMsgIface *tny_msg);
 void         modest_tny_msg_view_set_message  (ModestTnyMsgView *self,
                                                TnyMsgIface *tny_msg);
                                                
-GtkTextBuffer *      modest_tny_msg_view_get_selected (ModestTnyMsgView *self);
+GtkTextBuffer *      modest_tny_msg_view_get_selected_text (ModestTnyMsgView *self);
 
 G_END_DECLS