39f0448a7f570d5c7f3afc4a32e0985c96242d57
[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 gchar *
66 extract_text (ModestFormatter *self, TnyMimePart *body)
67 {
68         TnyStream *stream;
69         GtkTextBuffer *buf;
70         GtkTextIter start, end;
71         gchar *text, *converted_text;
72         ModestFormatterPrivate *priv;
73
74         buf = gtk_text_buffer_new (NULL);
75         stream = TNY_STREAM (tny_gtk_text_buffer_stream_new (buf));
76         tny_stream_reset (stream);
77         tny_mime_part_decode_to_stream (body, stream);
78         tny_stream_reset (stream);
79
80         g_object_unref (G_OBJECT(stream));
81         
82         gtk_text_buffer_get_bounds (buf, &start, &end);
83         text = gtk_text_buffer_get_text (buf, &start, &end, FALSE);
84         g_object_unref (G_OBJECT(buf));
85
86         /* Convert to desired content type if needed */
87         priv = MODEST_FORMATTER_GET_PRIVATE (self);
88
89         if (strcmp (tny_mime_part_get_content_type (body), priv->content_type) == 0) {
90                 if (!strcmp (priv->content_type, "text/html"))
91                         converted_text = modest_text_utils_convert_to_html  (text);
92                 else
93                         converted_text = g_strdup (text);
94
95                 g_free (text);
96                 text = converted_text;
97         }
98         return text;
99 }
100
101 static void
102 construct_from_text (TnyMimePart *part,
103                      const gchar *text,
104                      const gchar *content_type)
105 {
106         TnyStream *text_body_stream;
107
108         /* Create the stream */
109         text_body_stream = TNY_STREAM (tny_camel_stream_new
110                                        (camel_stream_mem_new_with_buffer
111                                         (text, strlen(text))));
112
113         /* Construct MIME part */
114         tny_stream_reset (text_body_stream);
115         tny_mime_part_construct_from_stream (part, text_body_stream, content_type);
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);
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         TnyPlatformFactory *fact;
181
182         fact = modest_runtime_get_platform_factory ();
183         /* Build new part */
184         new_msg     = modest_formatter_create_message (self, TRUE, TRUE);
185         body_part = modest_formatter_create_body_part (self, new_msg);
186
187         /* Create the two parts */
188         priv = MODEST_FORMATTER_GET_PRIVATE (self);
189         construct_from_text (body_part, "", priv->content_type);
190         g_object_unref (body_part);
191
192         /* Add parts */
193         tny_mime_part_add_part (TNY_MIME_PART (new_msg), TNY_MIME_PART (msg));
194
195         return new_msg;
196 }
197
198 ModestFormatter*
199 modest_formatter_new (const gchar *content_type, const gchar *signature)
200 {
201         ModestFormatter *formatter;
202         ModestFormatterPrivate *priv;
203
204         formatter = g_object_new (MODEST_TYPE_FORMATTER, NULL);
205         priv = MODEST_FORMATTER_GET_PRIVATE (formatter);
206         priv->content_type = g_strdup (content_type);
207         priv->signature = g_strdup (signature);
208
209         return formatter;
210 }
211
212 static void
213 modest_formatter_instance_init (GTypeInstance *instance, gpointer g_class)
214 {
215         ModestFormatter *self = (ModestFormatter *)instance;
216         ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
217
218         priv->content_type = NULL;
219 }
220
221 static void
222 modest_formatter_finalize (GObject *object)
223 {
224         ModestFormatter *self = (ModestFormatter *)object;
225         ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
226
227         if (priv->content_type)
228                 g_free (priv->content_type);
229
230         if (priv->signature)
231                 g_free (priv->signature);
232
233         (*parent_class->finalize) (object);
234 }
235
236 static void 
237 modest_formatter_class_init (ModestFormatterClass *class)
238 {
239         GObjectClass *object_class;
240
241         parent_class = g_type_class_peek_parent (class);
242         object_class = (GObjectClass*) class;
243         object_class->finalize = modest_formatter_finalize;
244
245         g_type_class_add_private (object_class, sizeof (ModestFormatterPrivate));
246 }
247
248 GType 
249 modest_formatter_get_type (void)
250 {
251         static GType type = 0;
252
253         if (G_UNLIKELY(type == 0))
254         {
255                 static const GTypeInfo info = 
256                 {
257                   sizeof (ModestFormatterClass),
258                   NULL,   /* base_init */
259                   NULL,   /* base_finalize */
260                   (GClassInitFunc) modest_formatter_class_init,   /* class_init */
261                   NULL,   /* class_finalize */
262                   NULL,   /* class_data */
263                   sizeof (ModestFormatter),
264                   0,      /* n_preallocs */
265                   modest_formatter_instance_init    /* instance_init */
266                 };
267                 
268                 type = g_type_register_static (G_TYPE_OBJECT,
269                         "ModestFormatter",
270                         &info, 0);
271         }
272
273         return type;
274 }
275
276 /****************/
277 static gchar *
278 modest_formatter_wrapper_cite (ModestFormatter *self, const gchar *text, TnyHeader *header,
279                                GList *attachments) 
280 {
281         ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
282         
283         return modest_text_utils_cite (text, 
284                                        priv->content_type, 
285                                        priv->signature,
286                                        tny_header_get_from (header), 
287                                        tny_header_get_date_sent (header));
288 }
289
290 static gchar *
291 modest_formatter_wrapper_inline (ModestFormatter *self, const gchar *text, TnyHeader *header,
292                                  GList *attachments) 
293 {
294         ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
295
296         return modest_text_utils_inline (text, 
297                                          priv->content_type, 
298                                          priv->signature,
299                                          tny_header_get_from (header), 
300                                          tny_header_get_date_sent (header),
301                                          tny_header_get_to (header),
302                                          tny_header_get_subject (header));
303 }
304
305 static gchar *
306 modest_formatter_wrapper_quote (ModestFormatter *self, const gchar *text, TnyHeader *header,
307                                 GList *attachments) 
308 {
309         ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
310         GList *filenames = NULL;
311         GList *node = NULL;
312         gchar *result = NULL;
313        
314         /* First we need a GList of attachments filenames */
315         for (node = attachments; node != NULL; node = g_list_next (node)) {
316                 TnyMimePart *part = (TnyMimePart *) node->data;
317                 gchar *filename = NULL;
318                 if (TNY_IS_MSG (part)) {
319                         TnyHeader *header = tny_msg_get_header (TNY_MSG (part));
320                         filename = g_strdup (tny_header_get_subject (header));
321                         if ((filename == NULL)||(filename[0] == '\0')) {
322                                 g_free (filename);
323                                 filename = g_strdup (_("mail_va_no_subject"));
324                         }
325                         g_object_unref (header);
326                 } else {
327                         filename = g_strdup (tny_mime_part_get_filename (part));
328                         if ((filename == NULL)||(filename[0] == '\0'))
329                                 filename = g_strdup ("");
330                 }
331                 filenames = g_list_append (filenames, filename);
332         }
333         filenames = g_list_reverse (filenames);
334
335         /* TODO: get 80 from the configuration */
336         result = modest_text_utils_quote (text, 
337                                           priv->content_type, 
338                                           priv->signature,
339                                           tny_header_get_from (header), 
340                                           tny_header_get_date_sent (header),
341                                           filenames,
342                                           80);
343
344         g_list_foreach (filenames, (GFunc) g_free, NULL);
345         g_list_free (filenames);
346         return result;
347 }
348
349 TnyMsg * 
350 modest_formatter_create_message (ModestFormatter *self, gboolean single_body, gboolean has_attachments)
351 {
352         TnyMsg *result = NULL;
353         TnyPlatformFactory *fact = NULL;
354         TnyMimePart *body_mime_part = NULL;
355         fact    = modest_runtime_get_platform_factory ();
356         result = tny_platform_factory_new_msg (fact);
357         if (has_attachments) {
358                 tny_mime_part_set_content_type (TNY_MIME_PART (result), "multipart/mixed");
359                 if (!single_body) {
360                         body_mime_part = tny_platform_factory_new_mime_part (fact);
361                         tny_mime_part_set_content_type (body_mime_part, "multipart/alternative");
362                         tny_mime_part_add_part (TNY_MIME_PART (result), body_mime_part);
363                         g_object_unref (body_mime_part);
364                 }
365         } else if (!single_body) {
366                 tny_mime_part_set_content_type (TNY_MIME_PART (result), "multipart/alternative");
367         }
368
369         return result;
370 }
371
372 TnyMimePart * 
373 modest_formatter_create_body_part (ModestFormatter *self, TnyMsg *msg)
374 {
375         TnyMimePart *result = NULL;
376         const gchar *msg_content_type = NULL;
377         TnyPlatformFactory *fact = NULL;
378
379         fact = modest_runtime_get_platform_factory ();
380         msg_content_type = tny_mime_part_get_content_type (TNY_MIME_PART (msg));
381         /* First it checks if the main part is alternative */
382         if ((msg_content_type != NULL) && 
383             (!g_strcasecmp (msg_content_type, "multipart/alternative"))) {
384                 result = tny_platform_factory_new_mime_part (fact);
385                 tny_mime_part_add_part (TNY_MIME_PART (msg), result);
386                 return result;
387         } else if ((msg_content_type != NULL) &&
388                    (!g_strcasecmp (msg_content_type, "multipart/mixed"))) {
389                 TnyList *parts = NULL;
390                 TnyIterator *iter = NULL;
391                 TnyMimePart *alternative_part = NULL;
392                 
393                 parts = TNY_LIST (tny_simple_list_new ());
394                 tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
395                 iter = tny_list_create_iterator (parts);
396                 while (!tny_iterator_is_done (iter)) {
397                         TnyMimePart *part = TNY_MIME_PART (tny_iterator_get_current (iter));
398                         if (part && !g_strcasecmp(tny_mime_part_get_content_type (part), "multipart/alternative")) {
399                                 alternative_part = part;
400                                 break;
401                         }
402
403                         if (part)
404                                 g_object_unref (part);
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 }