1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
30 #include <glib/gi18n.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>
41 typedef struct _ModestFormatterPrivate ModestFormatterPrivate;
42 struct _ModestFormatterPrivate {
46 #define MODEST_FORMATTER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
47 MODEST_TYPE_FORMATTER, \
48 ModestFormatterPrivate))
50 static GObjectClass *parent_class = NULL;
52 typedef gchar* FormatterFunc (ModestFormatter *self, const gchar *text, TnyHeader *header, GList *attachments);
54 static TnyMsg *modest_formatter_do (ModestFormatter *self, TnyMimePart *body, TnyHeader *header,
55 FormatterFunc func, GList *attachments);
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);
64 static TnyMimePart *find_body_parent (TnyMimePart *part);
67 extract_text (ModestFormatter *self, TnyMimePart *body)
71 GtkTextIter start, end;
72 gchar *text, *converted_text;
73 ModestFormatterPrivate *priv;
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);
81 g_object_unref (G_OBJECT(stream));
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));
87 /* Convert to desired content type if needed */
88 priv = MODEST_FORMATTER_GET_PRIVATE (self);
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);
94 converted_text = g_strdup (text);
97 text = converted_text;
103 construct_from_text (TnyMimePart *part,
105 const gchar *content_type)
107 TnyStream *text_body_stream;
109 /* Create the stream */
110 text_body_stream = TNY_STREAM (tny_camel_mem_stream_new_with_buffer
111 (text, strlen(text)));
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);
119 g_object_unref (G_OBJECT (text_body_stream));
123 modest_formatter_do (ModestFormatter *self, TnyMimePart *body, TnyHeader *header, FormatterFunc func,
126 TnyMsg *new_msg = NULL;
127 gchar *body_text = NULL, *txt = NULL;
128 ModestFormatterPrivate *priv;
129 TnyMimePart *body_part = NULL;
131 g_return_val_if_fail (self, NULL);
132 g_return_val_if_fail (header, NULL);
133 g_return_val_if_fail (func, NULL);
136 new_msg = modest_formatter_create_message (self, TRUE, attachments != NULL, FALSE);
137 body_part = modest_formatter_create_body_part (self, new_msg);
140 body_text = extract_text (self, body);
142 body_text = g_strdup ("");
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);
157 modest_formatter_cite (ModestFormatter *self, TnyMimePart *body, TnyHeader *header)
159 return modest_formatter_do (self, body, header, modest_formatter_wrapper_cite, NULL);
163 modest_formatter_quote (ModestFormatter *self, TnyMimePart *body, TnyHeader *header, GList *attachments)
165 return modest_formatter_do (self, body, header, modest_formatter_wrapper_quote, attachments);
169 modest_formatter_inline (ModestFormatter *self, TnyMimePart *body, TnyHeader *header, GList *attachments)
171 return modest_formatter_do (self, body, header, modest_formatter_wrapper_inline, attachments);
175 modest_formatter_attach (ModestFormatter *self, TnyMsg *msg, TnyHeader *header)
177 TnyMsg *new_msg = NULL;
178 TnyMimePart *body_part = NULL;
179 ModestFormatterPrivate *priv;
182 new_msg = modest_formatter_create_message (self, TRUE, TRUE, FALSE);
183 body_part = modest_formatter_create_body_part (self, new_msg);
185 /* Create the two parts */
186 priv = MODEST_FORMATTER_GET_PRIVATE (self);
187 construct_from_text (body_part, "", priv->content_type);
188 g_object_unref (body_part);
192 tny_mime_part_add_part (TNY_MIME_PART (new_msg), TNY_MIME_PART (msg));
199 modest_formatter_new (const gchar *content_type, const gchar *signature)
201 ModestFormatter *formatter;
202 ModestFormatterPrivate *priv;
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);
213 modest_formatter_instance_init (GTypeInstance *instance, gpointer g_class)
215 ModestFormatter *self = (ModestFormatter *)instance;
216 ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
218 priv->content_type = NULL;
222 modest_formatter_finalize (GObject *object)
224 ModestFormatter *self = (ModestFormatter *)object;
225 ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
227 if (priv->content_type)
228 g_free (priv->content_type);
231 g_free (priv->signature);
233 (*parent_class->finalize) (object);
237 modest_formatter_class_init (ModestFormatterClass *class)
239 GObjectClass *object_class;
241 parent_class = g_type_class_peek_parent (class);
242 object_class = (GObjectClass*) class;
243 object_class->finalize = modest_formatter_finalize;
245 g_type_class_add_private (object_class, sizeof (ModestFormatterPrivate));
249 modest_formatter_get_type (void)
251 static GType type = 0;
253 if (G_UNLIKELY(type == 0))
255 static const GTypeInfo info =
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),
265 modest_formatter_instance_init /* instance_init */
268 type = g_type_register_static (G_TYPE_OBJECT,
278 modest_formatter_wrapper_cite (ModestFormatter *self, const gchar *text, TnyHeader *header,
281 gchar *result, *from;
282 ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
284 from = tny_header_dup_from (header);
285 result = modest_text_utils_cite (text,
289 tny_header_get_date_sent (header));
295 modest_formatter_wrapper_inline (ModestFormatter *self, const gchar *text, TnyHeader *header,
298 gchar *result, *from, *to, *subject;
299 ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
301 from = tny_header_dup_from (header);
302 to = tny_header_dup_to (header);
303 subject = tny_header_dup_subject (header);
304 result = modest_text_utils_inline (text,
308 tny_header_get_date_sent (header),
318 modest_formatter_wrapper_quote (ModestFormatter *self, const gchar *text, TnyHeader *header,
321 ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
322 GList *filenames = NULL;
324 gchar *result = NULL;
327 /* First we need a GList of attachments filenames */
328 for (node = attachments; node != NULL; node = g_list_next (node)) {
329 TnyMimePart *part = (TnyMimePart *) node->data;
330 gchar *filename = NULL;
331 if (TNY_IS_MSG (part)) {
332 TnyHeader *header = tny_msg_get_header (TNY_MSG (part));
333 filename = tny_header_dup_subject (header);
334 if ((filename == NULL)||(filename[0] == '\0')) {
336 filename = g_strdup (_("mail_va_no_subject"));
338 g_object_unref (header);
340 filename = g_strdup (tny_mime_part_get_filename (part));
341 if ((filename == NULL)||(filename[0] == '\0'))
342 filename = g_strdup ("");
344 filenames = g_list_append (filenames, filename);
346 filenames = g_list_reverse (filenames);
348 /* TODO: get 80 from the configuration */
349 from = tny_header_dup_from (header);
350 result = modest_text_utils_quote (text,
354 tny_header_get_date_sent (header),
359 g_list_foreach (filenames, (GFunc) g_free, NULL);
360 g_list_free (filenames);
365 modest_formatter_create_message (ModestFormatter *self, gboolean single_body,
366 gboolean has_attachments, gboolean has_images)
368 TnyMsg *result = NULL;
369 TnyPlatformFactory *fact = NULL;
370 TnyMimePart *body_mime_part = NULL;
371 TnyMimePart *related_mime_part = NULL;
373 fact = modest_runtime_get_platform_factory ();
374 result = tny_platform_factory_new_msg (fact);
375 if (has_attachments) {
376 tny_mime_part_set_content_type (TNY_MIME_PART (result), "multipart/mixed");
378 related_mime_part = tny_platform_factory_new_mime_part (fact);
379 tny_mime_part_set_content_type (related_mime_part, "multipart/related");
380 tny_mime_part_add_part (TNY_MIME_PART (result), related_mime_part);
382 related_mime_part = g_object_ref (result);
386 body_mime_part = tny_platform_factory_new_mime_part (fact);
387 tny_mime_part_set_content_type (body_mime_part, "multipart/alternative");
388 tny_mime_part_add_part (TNY_MIME_PART (related_mime_part), body_mime_part);
389 g_object_unref (body_mime_part);
392 g_object_unref (related_mime_part);
393 } else if (has_images) {
394 tny_mime_part_set_content_type (TNY_MIME_PART (result), "multipart/related");
397 body_mime_part = tny_platform_factory_new_mime_part (fact);
398 tny_mime_part_set_content_type (body_mime_part, "multipart/alternative");
399 tny_mime_part_add_part (TNY_MIME_PART (result), body_mime_part);
400 g_object_unref (body_mime_part);
403 } else if (!single_body) {
404 tny_mime_part_set_content_type (TNY_MIME_PART (result), "multipart/alternative");
411 find_body_parent (TnyMimePart *part)
413 const gchar *msg_content_type = NULL;
414 msg_content_type = tny_mime_part_get_content_type (part);
416 if ((msg_content_type != NULL) &&
417 (!g_strcasecmp (msg_content_type, "multipart/alternative")))
418 return g_object_ref (part);
419 else if ((msg_content_type != NULL) &&
420 (g_str_has_prefix (msg_content_type, "multipart/"))) {
421 TnyIterator *iter = NULL;
422 TnyMimePart *alternative_part = NULL;
423 TnyMimePart *related_part = NULL;
424 TnyList *parts = TNY_LIST (tny_simple_list_new ());
425 tny_mime_part_get_parts (TNY_MIME_PART (part), parts);
426 iter = tny_list_create_iterator (parts);
428 while (!tny_iterator_is_done (iter)) {
429 TnyMimePart *part = TNY_MIME_PART (tny_iterator_get_current (iter));
430 if (part && !g_strcasecmp(tny_mime_part_get_content_type (part), "multipart/alternative")) {
431 alternative_part = part;
433 } else if (part && !g_strcasecmp (tny_mime_part_get_content_type (part), "multipart/related")) {
439 g_object_unref (part);
441 tny_iterator_next (iter);
443 g_object_unref (iter);
444 g_object_unref (parts);
447 result = find_body_parent (related_part);
448 g_object_unref (related_part);
450 } else if (alternative_part)
451 return alternative_part;
453 return g_object_ref (part);
459 modest_formatter_create_body_part (ModestFormatter *self, TnyMsg *msg)
461 TnyMimePart *result = NULL;
462 TnyPlatformFactory *fact = NULL;
463 TnyMimePart *parent = NULL;
465 parent = find_body_parent (TNY_MIME_PART (msg));
466 fact = modest_runtime_get_platform_factory ();
467 if (parent != NULL) {
468 result = tny_platform_factory_new_mime_part (fact);
469 tny_mime_part_add_part (TNY_MIME_PART (parent), result);
470 g_object_unref (parent);
472 result = g_object_ref (msg);