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