This commit implements a new handling of image attachments. We follow
[modest] / src / modest-formatter.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 #include <glib/gi18n.h>
31 #include <string.h>
32 #include <tny-header.h>
33 #include <tny-simple-list.h>
34 #include <tny-gtk-text-buffer-stream.h>
35 #include <tny-camel-stream.h>
36 #include <camel/camel-stream-mem.h>
37 #include "modest-formatter.h"
38 #include "modest-text-utils.h"
39 #include "modest-tny-platform-factory.h"
40 #include <modest-runtime.h>
41
42 typedef struct _ModestFormatterPrivate ModestFormatterPrivate;
43 struct _ModestFormatterPrivate {
44         gchar *content_type;
45         gchar *signature;
46 };
47 #define MODEST_FORMATTER_GET_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE((o), \
48                                           MODEST_TYPE_FORMATTER, \
49                                           ModestFormatterPrivate))
50
51 static GObjectClass *parent_class = NULL;
52
53 typedef gchar* FormatterFunc (ModestFormatter *self, const gchar *text, TnyHeader *header, GList *attachments);
54
55 static TnyMsg *modest_formatter_do (ModestFormatter *self, TnyMimePart *body,  TnyHeader *header, 
56                                     FormatterFunc func, GList *attachments);
57
58 static gchar*  modest_formatter_wrapper_cite   (ModestFormatter *self, const gchar *text,
59                                                 TnyHeader *header, GList *attachments);
60 static gchar*  modest_formatter_wrapper_quote  (ModestFormatter *self, const gchar *text,
61                                                 TnyHeader *header, GList *attachments);
62 static gchar*  modest_formatter_wrapper_inline (ModestFormatter *self, const gchar *text,
63                                                 TnyHeader *header, GList *attachments);
64
65 static TnyMimePart *find_body_parent (TnyMimePart *part);
66
67 static gchar *
68 extract_text (ModestFormatter *self, TnyMimePart *body)
69 {
70         TnyStream *stream;
71         GtkTextBuffer *buf;
72         GtkTextIter start, end;
73         gchar *text, *converted_text;
74         ModestFormatterPrivate *priv;
75
76         buf = gtk_text_buffer_new (NULL);
77         stream = TNY_STREAM (tny_gtk_text_buffer_stream_new (buf));
78         tny_stream_reset (stream);
79         tny_mime_part_decode_to_stream (body, stream);
80         tny_stream_reset (stream);
81
82         g_object_unref (G_OBJECT(stream));
83         
84         gtk_text_buffer_get_bounds (buf, &start, &end);
85         text = gtk_text_buffer_get_text (buf, &start, &end, FALSE);
86         g_object_unref (G_OBJECT(buf));
87
88         /* Convert to desired content type if needed */
89         priv = MODEST_FORMATTER_GET_PRIVATE (self);
90
91         if (strcmp (tny_mime_part_get_content_type (body), priv->content_type) == 0) {
92                 if (!strcmp (priv->content_type, "text/html"))
93                         converted_text = modest_text_utils_convert_to_html  (text);
94                 else
95                         converted_text = g_strdup (text);
96
97                 g_free (text);
98                 text = converted_text;
99         }
100         return text;
101 }
102
103 static void
104 construct_from_text (TnyMimePart *part,
105                      const gchar *text,
106                      const gchar *content_type)
107 {
108         TnyStream *text_body_stream;
109
110         /* Create the stream */
111         text_body_stream = TNY_STREAM (tny_camel_stream_new
112                                        (camel_stream_mem_new_with_buffer
113                                         (text, strlen(text))));
114
115         /* Construct MIME part */
116         tny_stream_reset (text_body_stream);
117         tny_mime_part_construct_from_stream (part, text_body_stream, content_type);
118         tny_stream_reset (text_body_stream);
119
120         /* Clean */
121         g_object_unref (G_OBJECT (text_body_stream));
122 }
123
124 static TnyMsg *
125 modest_formatter_do (ModestFormatter *self, TnyMimePart *body, TnyHeader *header, FormatterFunc func,
126                      GList *attachments)
127 {
128         TnyMsg *new_msg = NULL;
129         gchar *body_text = NULL, *txt = NULL;
130         ModestFormatterPrivate *priv;
131         TnyMimePart *body_part = NULL;
132
133         g_return_val_if_fail (self, NULL);
134         g_return_val_if_fail (header, NULL);
135         g_return_val_if_fail (func, NULL);
136
137         /* Build new part */
138         new_msg = modest_formatter_create_message (self, TRUE, attachments != NULL, FALSE);
139         body_part = modest_formatter_create_body_part (self, new_msg);
140
141         if (body)
142                 body_text = extract_text (self, body);
143         else
144                 body_text = g_strdup ("");
145
146         txt = (gchar *) func (self, (const gchar*) body_text, header, attachments);
147         priv = MODEST_FORMATTER_GET_PRIVATE (self);
148         construct_from_text (TNY_MIME_PART (body_part), (const gchar*) txt, priv->content_type);
149         g_object_unref (body_part);
150         
151         /* Clean */
152         g_free (body_text);
153         g_free (txt);
154
155         return new_msg;
156 }
157
158 TnyMsg *
159 modest_formatter_cite (ModestFormatter *self, TnyMimePart *body, TnyHeader *header)
160 {
161         return modest_formatter_do (self, body, header, modest_formatter_wrapper_cite, NULL);
162 }
163
164 TnyMsg *
165 modest_formatter_quote (ModestFormatter *self, TnyMimePart *body, TnyHeader *header, GList *attachments)
166 {
167         return modest_formatter_do (self, body, header, modest_formatter_wrapper_quote, attachments);
168 }
169
170 TnyMsg *
171 modest_formatter_inline (ModestFormatter *self, TnyMimePart *body, TnyHeader *header, GList *attachments)
172 {
173         return modest_formatter_do (self, body, header, modest_formatter_wrapper_inline, attachments);
174 }
175
176 TnyMsg *
177 modest_formatter_attach (ModestFormatter *self, TnyMsg *msg, TnyHeader *header)
178 {
179         TnyMsg *new_msg = NULL;
180         TnyMimePart *body_part = NULL;
181         ModestFormatterPrivate *priv;
182         TnyPlatformFactory *fact;
183
184         fact = modest_runtime_get_platform_factory ();
185         /* Build new part */
186         new_msg     = modest_formatter_create_message (self, TRUE, TRUE, FALSE);
187         body_part = modest_formatter_create_body_part (self, new_msg);
188
189         /* Create the two parts */
190         priv = MODEST_FORMATTER_GET_PRIVATE (self);
191         construct_from_text (body_part, "", priv->content_type);
192         g_object_unref (body_part);
193
194         /* Add parts */
195         tny_mime_part_add_part (TNY_MIME_PART (new_msg), TNY_MIME_PART (msg));
196
197         return new_msg;
198 }
199
200 ModestFormatter*
201 modest_formatter_new (const gchar *content_type, const gchar *signature)
202 {
203         ModestFormatter *formatter;
204         ModestFormatterPrivate *priv;
205
206         formatter = g_object_new (MODEST_TYPE_FORMATTER, NULL);
207         priv = MODEST_FORMATTER_GET_PRIVATE (formatter);
208         priv->content_type = g_strdup (content_type);
209         priv->signature = g_strdup (signature);
210
211         return formatter;
212 }
213
214 static void
215 modest_formatter_instance_init (GTypeInstance *instance, gpointer g_class)
216 {
217         ModestFormatter *self = (ModestFormatter *)instance;
218         ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
219
220         priv->content_type = NULL;
221 }
222
223 static void
224 modest_formatter_finalize (GObject *object)
225 {
226         ModestFormatter *self = (ModestFormatter *)object;
227         ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
228
229         if (priv->content_type)
230                 g_free (priv->content_type);
231
232         if (priv->signature)
233                 g_free (priv->signature);
234
235         (*parent_class->finalize) (object);
236 }
237
238 static void 
239 modest_formatter_class_init (ModestFormatterClass *class)
240 {
241         GObjectClass *object_class;
242
243         parent_class = g_type_class_peek_parent (class);
244         object_class = (GObjectClass*) class;
245         object_class->finalize = modest_formatter_finalize;
246
247         g_type_class_add_private (object_class, sizeof (ModestFormatterPrivate));
248 }
249
250 GType 
251 modest_formatter_get_type (void)
252 {
253         static GType type = 0;
254
255         if (G_UNLIKELY(type == 0))
256         {
257                 static const GTypeInfo info = 
258                 {
259                   sizeof (ModestFormatterClass),
260                   NULL,   /* base_init */
261                   NULL,   /* base_finalize */
262                   (GClassInitFunc) modest_formatter_class_init,   /* class_init */
263                   NULL,   /* class_finalize */
264                   NULL,   /* class_data */
265                   sizeof (ModestFormatter),
266                   0,      /* n_preallocs */
267                   modest_formatter_instance_init    /* instance_init */
268                 };
269                 
270                 type = g_type_register_static (G_TYPE_OBJECT,
271                         "ModestFormatter",
272                         &info, 0);
273         }
274
275         return type;
276 }
277
278 /****************/
279 static gchar *
280 modest_formatter_wrapper_cite (ModestFormatter *self, const gchar *text, TnyHeader *header,
281                                GList *attachments) 
282 {
283         ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
284         
285         return modest_text_utils_cite (text, 
286                                        priv->content_type, 
287                                        priv->signature,
288                                        tny_header_get_from (header), 
289                                        tny_header_get_date_sent (header));
290 }
291
292 static gchar *
293 modest_formatter_wrapper_inline (ModestFormatter *self, const gchar *text, TnyHeader *header,
294                                  GList *attachments) 
295 {
296         ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
297
298         return modest_text_utils_inline (text, 
299                                          priv->content_type, 
300                                          priv->signature,
301                                          tny_header_get_from (header), 
302                                          tny_header_get_date_sent (header),
303                                          tny_header_get_to (header),
304                                          tny_header_get_subject (header));
305 }
306
307 static gchar *
308 modest_formatter_wrapper_quote (ModestFormatter *self, const gchar *text, TnyHeader *header,
309                                 GList *attachments) 
310 {
311         ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
312         GList *filenames = NULL;
313         GList *node = NULL;
314         gchar *result = NULL;
315        
316         /* First we need a GList of attachments filenames */
317         for (node = attachments; node != NULL; node = g_list_next (node)) {
318                 TnyMimePart *part = (TnyMimePart *) node->data;
319                 gchar *filename = NULL;
320                 if (TNY_IS_MSG (part)) {
321                         TnyHeader *header = tny_msg_get_header (TNY_MSG (part));
322                         filename = g_strdup (tny_header_get_subject (header));
323                         if ((filename == NULL)||(filename[0] == '\0')) {
324                                 g_free (filename);
325                                 filename = g_strdup (_("mail_va_no_subject"));
326                         }
327                         g_object_unref (header);
328                 } else {
329                         filename = g_strdup (tny_mime_part_get_filename (part));
330                         if ((filename == NULL)||(filename[0] == '\0'))
331                                 filename = g_strdup ("");
332                 }
333                 filenames = g_list_append (filenames, filename);
334         }
335         filenames = g_list_reverse (filenames);
336
337         /* TODO: get 80 from the configuration */
338         result = modest_text_utils_quote (text, 
339                                           priv->content_type, 
340                                           priv->signature,
341                                           tny_header_get_from (header), 
342                                           tny_header_get_date_sent (header),
343                                           filenames,
344                                           80);
345
346         g_list_foreach (filenames, (GFunc) g_free, NULL);
347         g_list_free (filenames);
348         return result;
349 }
350
351 TnyMsg * 
352 modest_formatter_create_message (ModestFormatter *self, gboolean single_body, 
353                                  gboolean has_attachments, gboolean has_images)
354 {
355         TnyMsg *result = NULL;
356         TnyPlatformFactory *fact = NULL;
357         TnyMimePart *body_mime_part = NULL;
358         TnyMimePart *related_mime_part = NULL;
359
360         fact    = modest_runtime_get_platform_factory ();
361         result = tny_platform_factory_new_msg (fact);
362         if (has_attachments) {
363                 tny_mime_part_set_content_type (TNY_MIME_PART (result), "multipart/mixed");
364                 if (has_images) {
365                         related_mime_part = tny_platform_factory_new_mime_part (fact);
366                         tny_mime_part_set_content_type (related_mime_part, "multipart/related");
367                         tny_mime_part_add_part (TNY_MIME_PART (result), related_mime_part);
368                 } else {
369                         related_mime_part = g_object_ref (result);
370                 }
371                         
372                 if (!single_body) {
373                         body_mime_part = tny_platform_factory_new_mime_part (fact);
374                         tny_mime_part_set_content_type (body_mime_part, "multipart/alternative");
375                         tny_mime_part_add_part (TNY_MIME_PART (related_mime_part), body_mime_part);
376                         g_object_unref (body_mime_part);
377                 }
378
379                 g_object_unref (related_mime_part);
380         } else if (has_images) {
381                 tny_mime_part_set_content_type (TNY_MIME_PART (result), "multipart/related");
382
383                 if (!single_body) {
384                         body_mime_part = tny_platform_factory_new_mime_part (fact);
385                         tny_mime_part_set_content_type (body_mime_part, "multipart/alternative");
386                         tny_mime_part_add_part (TNY_MIME_PART (result), body_mime_part);
387                         g_object_unref (body_mime_part);
388                 }
389
390         } else if (!single_body) {
391                 tny_mime_part_set_content_type (TNY_MIME_PART (result), "multipart/alternative");
392         }
393
394         return result;
395 }
396
397 TnyMimePart *
398 find_body_parent (TnyMimePart *part)
399 {
400         const gchar *msg_content_type = NULL;
401         msg_content_type = tny_mime_part_get_content_type (part);
402
403         if ((msg_content_type != NULL) &&
404             (!g_strcasecmp (msg_content_type, "multipart/alternative")))
405                 return g_object_ref (part);
406         else if ((msg_content_type != NULL) &&
407                  (g_str_has_prefix (msg_content_type, "multipart/"))) {
408                 TnyIterator *iter = NULL;
409                 TnyMimePart *alternative_part = NULL;
410                 TnyMimePart *related_part = NULL;
411                 TnyList *parts = TNY_LIST (tny_simple_list_new ());
412                 tny_mime_part_get_parts (TNY_MIME_PART (part), parts);
413                 iter = tny_list_create_iterator (parts);
414
415                 while (!tny_iterator_is_done (iter)) {
416                         TnyMimePart *part = TNY_MIME_PART (tny_iterator_get_current (iter));
417                         if (part && !g_strcasecmp(tny_mime_part_get_content_type (part), "multipart/alternative")) {
418                                 alternative_part = part;
419                                 break;
420                         } else if (part && !g_strcasecmp (tny_mime_part_get_content_type (part), "multipart/related")) {
421                                 related_part = part;
422                                 break;
423                         }
424
425                         if (part)
426                                 g_object_unref (part);
427
428                         tny_iterator_next (iter);
429                 }
430                 g_object_unref (iter);
431                 g_object_unref (parts);
432                 if (related_part) {
433                         TnyMimePart *result;
434                         result = find_body_parent (related_part);
435                         g_object_unref (related_part);
436                         return result;
437                 } else if (alternative_part)
438                         return alternative_part;
439                 else 
440                         return g_object_ref (part);
441         } else
442                 return NULL;
443 }
444
445 TnyMimePart * 
446 modest_formatter_create_body_part (ModestFormatter *self, TnyMsg *msg)
447 {
448         TnyMimePart *result = NULL;
449         TnyPlatformFactory *fact = NULL;
450         TnyMimePart *parent = NULL;
451
452         parent = find_body_parent (TNY_MIME_PART (msg));
453         fact = modest_runtime_get_platform_factory ();
454         if (parent != NULL) {
455                 result = tny_platform_factory_new_mime_part (fact);
456                 tny_mime_part_add_part (TNY_MIME_PART (parent), result);
457                 g_object_unref (parent);
458         } else {
459                 result = g_object_ref (msg);
460         }
461
462         return result;
463
464 }