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