55c02694a8e0d8796b89cc9396ac2c2d9d5ea915
[modest] / src / modest-tny-msg-actions.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30
31 #include <gtk/gtk.h>
32 #include <gtkhtml/gtkhtml.h>
33 #include <tny-gtk-text-buffer-stream.h>
34 #include <tny-simple-list.h>
35 #include <tny-folder.h>
36
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif /*HAVE_CONFIG_H */
40
41 #include "modest-tny-msg-actions.h"
42 #include "modest-text-utils.h"
43
44 static void modest_tny_msg_actions_xfer (TnyHeader *header, TnyFolder *folder, 
45                                          gboolean delete_original);
46
47
48 static const gchar *
49 get_body_text (TnyMsg *msg, gboolean want_html)
50 {
51         TnyStream *stream;
52         TnyMimePart *body;
53         GtkTextBuffer *buf;
54         GtkTextIter start, end;
55         const gchar *to_quote;
56         gchar *quoted;
57
58         body = modest_tny_msg_actions_find_body_part(msg, want_html);
59         if (!body)
60                 return NULL;
61
62         buf = gtk_text_buffer_new (NULL);
63         stream = TNY_STREAM (tny_gtk_text_buffer_stream_new (buf));
64         tny_stream_reset (stream);
65         tny_mime_part_decode_to_stream (body, stream);
66         tny_stream_reset (stream);
67
68         g_object_unref (G_OBJECT(stream));
69         g_object_unref (G_OBJECT(body));
70         
71         gtk_text_buffer_get_bounds (buf, &start, &end);
72         to_quote = gtk_text_buffer_get_text (buf, &start, &end, FALSE);
73         g_object_unref (buf);
74
75         return to_quote;
76 }
77
78 gchar*
79 modest_tny_msg_actions_quote (TnyMsg * self, const gchar * from,
80                               time_t sent_date, gint limit,
81                               const gchar * to_quote)
82 {
83         gchar *quoted_msg = NULL;
84         const gchar *body;
85
86         /* 2 cases: */
87
88         /* a) quote text from selection */
89         if (to_quote != NULL) 
90                 return modest_text_utils_quote (to_quote, from, sent_date,
91                                                 limit);
92         
93         /* b) try to find a text/plain part in the msg and quote it */
94         body = get_body_text (self, FALSE);
95         if (body)
96                 quoted_msg = modest_text_utils_quote (body, from, sent_date, limit);
97         
98         return quoted_msg;
99 }
100
101
102
103 TnyMimePart *
104 modest_tny_msg_actions_find_body_part (TnyMsg *msg, gboolean want_html)
105 {
106         const gchar *mime_type = want_html ? "text/html" : "text/plain";
107         TnyMimePart *part = NULL;
108         TnyList *parts;
109         TnyIterator *iter;
110
111         if (!msg)
112                 return NULL;
113
114         parts = TNY_LIST (tny_simple_list_new());
115         tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
116
117         iter  = tny_list_create_iterator(parts);
118
119         while (!tny_iterator_is_done(iter)) {
120
121                 part = TNY_MIME_PART(tny_iterator_get_current (iter));
122                 
123                 if (tny_mime_part_content_type_is (part, mime_type) &&
124                     !tny_mime_part_is_attachment (part)) {
125                         break;
126                 }
127                 part = NULL;
128                 tny_iterator_next (iter);
129         }
130
131         /* did we find a matching part? */
132         if (part)
133                 g_object_ref (G_OBJECT(part));
134
135         g_object_unref (G_OBJECT(iter));
136         g_object_unref (G_OBJECT(parts));
137
138         /* if were trying to find an HTML part and couldn't find it,
139          * try to find a text/plain part instead
140          */
141         if (!part && want_html) 
142                 return modest_tny_msg_actions_find_body_part (msg, FALSE);
143
144         return part ? part : NULL;
145 }
146
147
148
149 TnyMimePart *
150 modest_tny_msg_actions_find_nth_part (TnyMsg *msg, gint index)
151 {
152         TnyMimePart *part;
153         TnyList *parts;
154         TnyIterator *iter;
155
156         g_return_val_if_fail (msg, NULL);
157         g_return_val_if_fail (index > 0, NULL);
158                 
159         parts = TNY_LIST(tny_simple_list_new());
160         tny_mime_part_get_parts (TNY_MIME_PART(msg), parts);
161         iter  = tny_list_create_iterator (parts);
162
163         part = NULL;
164         
165         if (!tny_iterator_is_done(iter)) {
166                 tny_iterator_nth (iter, index);
167                 part = TNY_MIME_PART(tny_iterator_get_current (iter));
168         }
169
170         g_object_unref (G_OBJECT(iter));
171         g_object_unref (G_OBJECT(parts));
172
173         return part;
174 }
175
176 gchar * 
177 modest_tny_msg_actions_find_body (TnyMsg *msg, gboolean want_html)
178 {
179         const gchar *body;
180
181         body = get_body_text (msg, want_html);
182
183         if (body)
184                 return g_strdup (body);
185         else 
186                 return NULL;
187 }
188
189
190 static void
191 modest_tny_msg_actions_xfer (TnyHeader *header, TnyFolder *folder, 
192                              gboolean delete_original)
193 {
194         TnyFolder *src_folder;
195         TnyList *headers;
196
197         src_folder = tny_header_get_folder (header);
198         headers = tny_simple_list_new ();
199
200         /* Move */
201         tny_list_prepend (headers, G_OBJECT (header));
202         tny_folder_transfer_msgs (src_folder, headers, folder, delete_original);
203
204         /* Free */
205         g_object_unref (headers);
206         g_object_unref (folder);
207 }
208
209 void
210 modest_tny_msg_actions_copy (TnyHeader *header, TnyFolder *folder)
211 {
212         g_return_if_fail (TNY_IS_HEADER (header));
213         g_return_if_fail (TNY_IS_FOLDER (folder));
214
215         modest_tny_msg_actions_xfer (header, folder, FALSE);
216 }
217
218 void
219 modest_tny_msg_actions_move (TnyHeader *header, TnyFolder *folder)
220 {
221         g_return_if_fail (TNY_IS_HEADER (header));
222         g_return_if_fail (TNY_IS_FOLDER (folder));
223
224         modest_tny_msg_actions_xfer (header, folder, TRUE);
225 }
226
227 void
228 modest_tny_msg_actions_remove (TnyHeader *header)
229 {
230         TnyFolder *folder;
231
232         g_return_if_fail (TNY_IS_HEADER (header));
233
234         folder = tny_header_get_folder (header);
235
236         /* Remove */
237         tny_folder_remove_msg (folder, header);
238         tny_folder_expunge (folder);
239
240         /* Free */
241         g_object_unref (folder);
242 }