* moded find_body_part from modest-tny-msg-view to modest-tny-msg-actions
[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 static GtkTextBuffer *
25 htmltotext (TnyMsgMimePartIface * body)
26 {
27         GtkTextBuffer *buf;
28
29 #ifdef ACTIVATE_HACKS
30         GtkWidget *html, *win;
31         TnyStreamIface *stream;
32         GtkClipboard *clip;
33         gchar *text;
34
35         win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
36         html = gtk_html_new ();
37         gtk_container_add (GTK_CONTAINER (win), html);
38
39         gtk_html_set_editable (GTK_HTML (html), FALSE);
40         gtk_html_allow_selection (GTK_HTML (html), TRUE);
41         stream = TNY_STREAM_IFACE (modest_tny_stream_gtkhtml_new
42                                    (gtk_html_begin (GTK_HTML (html))));
43
44         tny_stream_iface_reset (stream);
45         tny_msg_mime_part_iface_decode_to_stream (body, stream);
46         tny_stream_iface_reset (stream);
47         g_object_unref (G_OBJECT (stream));
48
49         gtk_widget_show_all (win);
50         gtk_html_select_all (GTK_HTML (html));
51         clip = gtk_widget_get_clipboard (html, GDK_SELECTION_PRIMARY);
52         //clip = gtk_widget_get_clipboard(html, GDK_SELECTION_CLIPBOARD);
53         text = gtk_clipboard_wait_for_text (clip);
54
55         buf = gtk_text_buffer_new (NULL);
56         gtk_text_buffer_set_text (buf, text, -1);
57         g_free (text);
58         /* destroy win & html */
59 #else
60         buf = gtk_text_buffer_new (NULL);
61 #endif
62         return buf;
63 }
64
65 gchar *
66 modest_tny_msg_actions_quote (const TnyMsgIface * self, const gchar * from,
67                               time_t sent_date, gint limit,
68                               char * to_quote)
69 {
70         gchar *quoted;
71
72         /* 3 cases: */
73
74         /* a) quote text from selection */
75         if (to_quote != NULL) {
76                 return modest_text_utils_quote (to_quote, from, sent_date,
77                                                 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,
92            gint limit, gboolean textorhtml)
93 {
94         TnyStreamIface *stream;
95         TnyMsgMimePartIface *body;
96         GtkTextBuffer *buf;
97         gchar *quoted;
98
99         /* the cast makes me uneasy... */
100         body = modest_tny_msg_actions_find_body_part((TnyMsgIface *) src,
101                                                                                                  textorhtml ? "text/html" : "text/plain");
102         
103         if (!body)
104                 return NULL;
105
106         if (textorhtml == TRUE) {
107                 buf = htmltotext (body);
108         } else {
109                 buf = gtk_text_buffer_new (NULL);
110                 stream = TNY_STREAM_IFACE (tny_text_buffer_stream_new (buf));
111                 tny_stream_iface_reset (stream);
112                 tny_msg_mime_part_iface_decode_to_stream (body, stream);
113                 tny_stream_iface_reset (stream);
114                 g_object_unref (stream);
115         }
116
117         quoted = modest_text_utils_quote_text_buffer (buf, from, sent_date, limit);
118
119         g_object_unref (buf);
120         return quoted;
121 }
122
123
124 TnyMsgMimePartIface *
125 modest_tny_msg_actions_find_body_part (TnyMsgIface *self, const gchar *mime_type)
126 {
127         TnyMsgMimePartIface *part = NULL;
128         GList *parts;
129
130         g_return_val_if_fail (self, NULL);
131         g_return_val_if_fail (mime_type, NULL);
132
133         parts  = (GList*) tny_msg_iface_get_parts (self);
134         while (parts && !part) {
135                 part = TNY_MSG_MIME_PART_IFACE(parts->data);
136                 if (!tny_msg_mime_part_iface_content_type_is (part, mime_type))
137                         part = NULL;
138                 parts = parts->next;
139         }
140         
141         return part;
142 }