cf405980c22404d588bf11ecd61e8c4ed2628ba8
[modest] / src / modest-tny-msg-actions.c
1 /* modest-ui.c */
2
3 /* insert (c)/licensing information) */
4
5 #include <gtk/gtk.h>
6 #include <gtkhtml/gtkhtml.h>
7
8 /* TODO: put in auto* */
9 #include <tny-text-buffer-stream.h>
10 #include <tny-msg-mime-part-iface.h>
11
12 #ifdef HAVE_CONFIG_H
13 #include <config.h>
14 #endif /*HAVE_CONFIG_H*/
15
16 #include "modest-text-utils.h"
17
18
19 /* private */
20 static gchar *quote_msg(const TnyMsgIface *src, const gchar *from, time_t sent_date, gint limit, gboolean textorhtml);
21 static GtkTextBuffer *htmltotext(TnyMsgMimePartIface * body);
22
23
24 static GtkTextBuffer *
25 htmltotext(TnyMsgMimePartIface * body)
26 {
27         GtkTextBuffer *buf;
28 #ifdef ACTIVATE_HACKS
29         GtkWidget *html, *win;
30         TnyStreamIface *stream; 
31         GtkClipboard *clip;
32         gchar *text;
33         
34         win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
35         html = gtk_html_new();
36         gtk_container_add(GTK_CONTAINER(win), html);
37         
38         gtk_html_set_editable        (GTK_HTML(html), FALSE);
39         gtk_html_allow_selection     (GTK_HTML(html), TRUE);
40         gtk_html_set_caret_mode      (GTK_HTML(html), FALSE);
41         gtk_html_set_blocking        (GTK_HTML(html), FALSE);
42         gtk_html_set_images_blocking (GTK_HTML(html), FALSE);
43         
44         stream = TNY_STREAM_IFACE(modest_tny_stream_gtkhtml_new(
45                 gtk_html_begin(GTK_HTML(html))));
46         
47         tny_stream_iface_reset (stream);
48         tny_msg_mime_part_iface_decode_to_stream (body, stream);
49         tny_stream_iface_reset (stream);
50         g_object_unref (G_OBJECT(stream));
51         
52         gtk_widget_show_all(win);
53         gtk_html_select_all(GTK_HTML(html));
54         clip = gtk_widget_get_clipboard(html, GDK_SELECTION_PRIMARY);
55         //clip = gtk_widget_get_clipboard(html, GDK_SELECTION_CLIPBOARD);
56         text = gtk_clipboard_wait_for_text(clip);
57         
58         buf = gtk_text_buffer_new(NULL);
59         gtk_text_buffer_set_text(buf, text, -1);
60         g_free(text);
61         /* destroy win & html */
62 #else
63         buf = gtk_text_buffer_new(NULL);
64 #endif
65         return buf;
66 }
67
68 gchar *
69 modest_tny_msg_actions_quote(const TnyMsgIface *self, const gchar *from, time_t sent_date, gint limit, GtkTextBuffer *to_quote)
70 {
71         gchar *quoted;
72
73         /* 3 cases: */
74         
75         /* a) quote text from selection */
76         if (to_quote != NULL) {
77                 return modest_text_utils_quote(to_quote, from, sent_date, limit);
78         }
79                 
80         /* b) try to find a text/plain part in the msg and quote it */
81         quoted = quote_msg(self, from, sent_date, limit, FALSE);
82         if (quoted != NULL)
83                 return quoted;
84         
85         /* c) if that fails, try text/html */
86         return quote_msg(self, from, sent_date, limit, TRUE);
87 }
88         
89         
90 static gchar *
91 quote_msg(const TnyMsgIface *src, const gchar *from, time_t sent_date, gint limit, gboolean textorhtml)
92 {
93
94         GList *parts; /* LEAK? */
95         TnyStreamIface* stream;
96         TnyTextBufferStream *dest;
97         TnyMsgMimePartIface *body = NULL;
98         TnyMsgMimePartIface *part;
99         GtkTextBuffer *buf;
100         gchar *quoted;
101         
102         /* is the warning in this line due to a bug in tinymail? */
103         parts  = (GList*) tny_msg_iface_get_parts (src);
104
105         while (parts) {
106                 part = TNY_MSG_MIME_PART_IFACE(parts->data);
107                 if (tny_msg_mime_part_iface_content_type_is (part, textorhtml ? "text/html" : "text/plain")) {
108                         body = part;
109                         break;
110                 }
111                 parts = parts->next;
112         }
113         
114         if (!body) {
115                 return NULL;
116         }
117         
118         if (textorhtml == TRUE) {
119                 buf = htmltotext(body);
120         } else {
121                 buf    = gtk_text_buffer_new (NULL);
122                 stream = TNY_STREAM_IFACE(tny_text_buffer_stream_new (buf));
123                 tny_stream_iface_reset (stream);
124                 tny_msg_mime_part_iface_decode_to_stream (body, stream);
125                 tny_stream_iface_reset (stream);
126         }
127         
128         quoted = modest_text_utils_quote (buf, from, sent_date, limit);
129                 
130         g_object_unref(stream);
131         g_object_unref(buf);
132         return quoted;
133 }