* got rid of most GtkTextBuffers in quote code
[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-tny-msg-actions.h"
17 #include "modest-text-utils.h"
18
19
20 /* private */
21 static gchar *quote_msg (const TnyMsgIface * src, const gchar * from,
22                          time_t sent_date, gint limit, gboolean textorhtml);
23 static GtkTextBuffer *htmltotext (TnyMsgMimePartIface * body);
24
25
26 static GtkTextBuffer *
27 htmltotext (TnyMsgMimePartIface * body)
28 {
29         GtkTextBuffer *buf;
30
31 #ifdef ACTIVATE_HACKS /* it still doesn't work, don't bother! */
32         GtkWidget *html, *win;
33         TnyStreamIface *stream;
34         GtkClipboard *clip;
35         gchar *text;
36
37         win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
38         html = gtk_html_new ();
39         gtk_container_add (GTK_CONTAINER (win), html);
40
41         gtk_html_set_editable (GTK_HTML (html), FALSE);
42         gtk_html_allow_selection (GTK_HTML (html), TRUE);
43         stream = TNY_STREAM_IFACE (modest_tny_stream_gtkhtml_new
44                                    (gtk_html_begin (GTK_HTML (html))));
45
46         tny_stream_iface_reset (stream);
47         tny_msg_mime_part_iface_decode_to_stream (body, stream);
48         tny_stream_iface_reset (stream);
49         g_object_unref (G_OBJECT (stream));
50
51         gtk_widget_show_all (win);
52         gtk_html_select_all (GTK_HTML (html));
53         clip = gtk_widget_get_clipboard (html, GDK_SELECTION_PRIMARY);
54         /*clip = gtk_widget_get_clipboard(html, GDK_SELECTION_CLIPBOARD);*/
55         text = gtk_clipboard_wait_for_text (clip);
56
57         buf = gtk_text_buffer_new (NULL);
58         gtk_text_buffer_set_text (buf, text, -1);
59         g_free (text);
60         /* destroy win & html */
61 #else
62         buf = gtk_text_buffer_new (NULL);
63 #endif
64         return buf;
65 }
66
67 gchar *
68 modest_tny_msg_actions_quote (const TnyMsgIface * self, const gchar * from,
69                               time_t sent_date, gint limit,
70                               gchar * to_quote)
71 {
72         gchar *quoted;
73
74         /* 3 cases: */
75
76         /* a) quote text from selection */
77         if (to_quote != NULL) {
78                 return modest_text_utils_quote (to_quote, from, sent_date,
79                                                 limit);
80         }
81
82         /* b) try to find a text/plain part in the msg and quote it */
83         quoted = quote_msg (self, from, sent_date, limit, FALSE);
84         if (quoted != NULL)
85                 return quoted;
86
87         /* c) if that fails, try text/html */
88         return quote_msg (self, from, sent_date, limit, TRUE);
89 }
90
91
92 static gchar *
93 quote_msg (const TnyMsgIface * src, const gchar * from, time_t sent_date,
94            gint limit, gboolean textorhtml)
95 {
96         TnyStreamIface *stream;
97         TnyMsgMimePartIface *body;
98         GtkTextBuffer *buf;
99         GtkTextIter start, end;
100         const gchar *to_quote;
101         gchar *quoted;
102
103         /* the cast makes me uneasy... */
104         body = modest_tny_msg_actions_find_body_part((TnyMsgIface *) src,
105                                                                                                  textorhtml ? "text/html" : "text/plain");
106         
107         if (!body)
108                 return NULL;
109
110         if (textorhtml == TRUE) {
111                 buf = htmltotext (body);
112         } else {
113                 buf = gtk_text_buffer_new (NULL);
114                 stream = TNY_STREAM_IFACE (tny_text_buffer_stream_new (buf));
115                 tny_stream_iface_reset (stream);
116                 tny_msg_mime_part_iface_decode_to_stream (body, stream);
117                 tny_stream_iface_reset (stream);
118                 g_object_unref (stream);
119         }
120         
121         gtk_text_buffer_get_bounds (buf, &start, &end);
122         to_quote = gtk_text_buffer_get_text (buf, &start, &end, FALSE);
123         quoted = modest_text_utils_quote (to_quote, from, sent_date, limit);
124         g_object_unref (buf);
125         return quoted;
126 }
127
128
129 TnyMsgMimePartIface *
130 modest_tny_msg_actions_find_body_part (TnyMsgIface *self, const gchar *mime_type)
131 {
132         TnyMsgMimePartIface *part = NULL;
133         GList *parts;
134
135         g_return_val_if_fail (self, NULL);
136         g_return_val_if_fail (mime_type, NULL);
137
138         parts  = (GList*) tny_msg_iface_get_parts (self);
139         while (parts && !part) {
140                 part = TNY_MSG_MIME_PART_IFACE(parts->data);
141                 if (!tny_msg_mime_part_iface_content_type_is (part, mime_type))
142                         part = NULL;
143                 parts = parts->next;
144         }
145         
146         return part;
147 }