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-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>
42 typedef struct _ModestFormatterPrivate ModestFormatterPrivate;
43 struct _ModestFormatterPrivate {
47 #define MODEST_FORMATTER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
48 MODEST_TYPE_FORMATTER, \
49 ModestFormatterPrivate))
51 static GObjectClass *parent_class = NULL;
53 typedef gchar* FormatterFunc (ModestFormatter *self, const gchar *text, TnyHeader *header, GList *attachments);
55 static TnyMsg *modest_formatter_do (ModestFormatter *self, TnyMimePart *body, TnyHeader *header,
56 FormatterFunc func, GList *attachments);
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);
63 extract_text (ModestFormatter *self, TnyMimePart *body)
67 GtkTextIter start, end;
68 gchar *text, *converted_text;
69 ModestFormatterPrivate *priv;
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);
77 g_object_unref (G_OBJECT(stream));
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));
83 /* Convert to desired content type if needed */
84 priv = MODEST_FORMATTER_GET_PRIVATE (self);
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);
90 converted_text = g_strdup (text);
93 text = converted_text;
99 construct_from_text (TnyMimePart *part,
101 const gchar *content_type)
103 TnyStream *text_body_stream;
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))));
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);
116 g_object_unref (G_OBJECT (text_body_stream));
120 modest_formatter_do (ModestFormatter *self, TnyMimePart *body, TnyHeader *header, FormatterFunc func, GList *attachments)
122 TnyMsg *new_msg = NULL;
123 gchar *body_text = NULL, *txt = NULL;
124 ModestFormatterPrivate *priv;
125 TnyMimePart *body_part = NULL;
127 g_return_val_if_fail (self, NULL);
128 g_return_val_if_fail (header, NULL);
129 g_return_val_if_fail (func, NULL);
132 new_msg = modest_formatter_create_message (self, TRUE, attachments != NULL);
133 body_part = modest_formatter_create_body_part (self, new_msg);
136 body_text = extract_text (self, body);
138 body_text = g_strdup ("");
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);
153 modest_formatter_cite (ModestFormatter *self, TnyMimePart *body, TnyHeader *header)
155 return modest_formatter_do (self, body, header, modest_formatter_wrapper_cite, NULL);
159 modest_formatter_quote (ModestFormatter *self, TnyMimePart *body, TnyHeader *header, GList *attachments)
161 return modest_formatter_do (self, body, header, modest_formatter_wrapper_quote, attachments);
165 modest_formatter_inline (ModestFormatter *self, TnyMimePart *body, TnyHeader *header, GList *attachments)
167 return modest_formatter_do (self, body, header, modest_formatter_wrapper_inline, attachments);
171 modest_formatter_attach (ModestFormatter *self, TnyMsg *msg, TnyHeader *header)
173 TnyMsg *new_msg = NULL;
174 TnyMimePart *body_part = NULL;
175 ModestFormatterPrivate *priv;
176 TnyPlatformFactory *fact;
178 fact = modest_runtime_get_platform_factory ();
180 new_msg = modest_formatter_create_message (self, TRUE, TRUE);
181 body_part = modest_formatter_create_body_part (self, new_msg);
183 /* Create the two parts */
184 priv = MODEST_FORMATTER_GET_PRIVATE (self);
185 construct_from_text (body_part, "", priv->content_type);
186 g_object_unref (body_part);
189 tny_mime_part_add_part (TNY_MIME_PART (new_msg), TNY_MIME_PART (msg));
195 modest_formatter_new (const gchar *content_type, const gchar *signature)
197 ModestFormatter *formatter;
198 ModestFormatterPrivate *priv;
200 formatter = g_object_new (MODEST_TYPE_FORMATTER, NULL);
201 priv = MODEST_FORMATTER_GET_PRIVATE (formatter);
202 priv->content_type = g_strdup (content_type);
203 priv->signature = g_strdup (signature);
209 modest_formatter_instance_init (GTypeInstance *instance, gpointer g_class)
211 ModestFormatter *self = (ModestFormatter *)instance;
212 ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
214 priv->content_type = NULL;
218 modest_formatter_finalize (GObject *object)
220 ModestFormatter *self = (ModestFormatter *)object;
221 ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
223 if (priv->content_type)
224 g_free (priv->content_type);
227 g_free (priv->signature);
229 (*parent_class->finalize) (object);
233 modest_formatter_class_init (ModestFormatterClass *class)
235 GObjectClass *object_class;
237 parent_class = g_type_class_peek_parent (class);
238 object_class = (GObjectClass*) class;
239 object_class->finalize = modest_formatter_finalize;
241 g_type_class_add_private (object_class, sizeof (ModestFormatterPrivate));
245 modest_formatter_get_type (void)
247 static GType type = 0;
249 if (G_UNLIKELY(type == 0))
251 static const GTypeInfo info =
253 sizeof (ModestFormatterClass),
254 NULL, /* base_init */
255 NULL, /* base_finalize */
256 (GClassInitFunc) modest_formatter_class_init, /* class_init */
257 NULL, /* class_finalize */
258 NULL, /* class_data */
259 sizeof (ModestFormatter),
261 modest_formatter_instance_init /* instance_init */
264 type = g_type_register_static (G_TYPE_OBJECT,
274 modest_formatter_wrapper_cite (ModestFormatter *self, const gchar *text, TnyHeader *header, GList *attachments)
276 ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
278 return modest_text_utils_cite (text,
281 tny_header_get_from (header),
282 tny_header_get_date_sent (header));
286 modest_formatter_wrapper_inline (ModestFormatter *self, const gchar *text, TnyHeader *header, GList *attachments)
288 ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
290 return modest_text_utils_inline (text,
293 tny_header_get_from (header),
294 tny_header_get_date_sent (header),
295 tny_header_get_to (header),
296 tny_header_get_subject (header));
300 modest_formatter_wrapper_quote (ModestFormatter *self, const gchar *text, TnyHeader *header, GList *attachments)
302 ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
303 GList *filenames = NULL;
305 gchar *result = NULL;
307 /* First we need a GList of attachments filenames */
308 for (node = attachments; node != NULL; node = g_list_next (node)) {
309 TnyMimePart *part = (TnyMimePart *) node->data;
310 gchar *filename = NULL;
311 if (TNY_IS_MSG (part)) {
312 TnyHeader *header = tny_msg_get_header (TNY_MSG (part));
313 filename = g_strdup (tny_header_get_subject (header));
314 if ((filename == NULL)||(filename[0] == '\0')) {
316 filename = g_strdup (_("mail_va_no_subject"));
318 g_object_unref (header);
320 filename = g_strdup (tny_mime_part_get_filename (part));
321 if ((filename == NULL)||(filename[0] == '\0'))
322 filename = g_strdup ("");
324 filenames = g_list_append (filenames, filename);
326 filenames = g_list_reverse (filenames);
328 /* TODO: get 80 from the configuration */
329 result = modest_text_utils_quote (text,
332 tny_header_get_from (header),
333 tny_header_get_date_sent (header),
337 g_list_foreach (filenames, (GFunc) g_free, NULL);
338 g_list_free (filenames);
343 modest_formatter_create_message (ModestFormatter *self, gboolean single_body, gboolean has_attachments)
345 TnyMsg *result = NULL;
346 TnyPlatformFactory *fact = NULL;
347 TnyMimePart *body_mime_part = NULL;
348 fact = modest_runtime_get_platform_factory ();
349 result = tny_platform_factory_new_msg (fact);
350 if (has_attachments) {
351 tny_mime_part_set_content_type (TNY_MIME_PART (result), "multipart/mixed");
353 body_mime_part = tny_platform_factory_new_mime_part (fact);
354 tny_mime_part_set_content_type (body_mime_part, "multipart/alternative");
355 tny_mime_part_add_part (TNY_MIME_PART (result), body_mime_part);
356 g_object_unref (body_mime_part);
358 } else if (!single_body) {
359 tny_mime_part_set_content_type (TNY_MIME_PART (result), "multipart/alternative");
366 modest_formatter_create_body_part (ModestFormatter *self, TnyMsg *msg)
368 TnyMimePart *result = NULL;
369 const gchar *msg_content_type = NULL;
370 TnyPlatformFactory *fact = NULL;
372 fact = modest_runtime_get_platform_factory ();
373 msg_content_type = tny_mime_part_get_content_type (TNY_MIME_PART (msg));
374 /* First it checks if the main part is alternative */
375 if ((msg_content_type != NULL) &&
376 (!g_strcasecmp (msg_content_type, "multipart/alternative"))) {
377 result = tny_platform_factory_new_mime_part (fact);
378 tny_mime_part_add_part (TNY_MIME_PART (msg), result);
380 } else if ((msg_content_type != NULL) &&
381 (!g_strcasecmp (msg_content_type, "multipart/mixed"))) {
382 TnyList *parts = NULL;
383 TnyIterator *iter = NULL;
384 TnyMimePart *alternative_part = NULL;
386 parts = TNY_LIST (tny_simple_list_new ());
387 tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
388 iter = tny_list_create_iterator (parts);
389 while (!tny_iterator_is_done (iter)) {
390 TnyMimePart *part = TNY_MIME_PART (tny_iterator_get_current (iter));
391 if (part && !g_strcasecmp(tny_mime_part_get_content_type (part), "multipart/alternative")) {
392 alternative_part = part;
397 g_object_unref (part);
399 tny_iterator_next (iter);
401 result = tny_platform_factory_new_mime_part (fact);
402 if (alternative_part != NULL) {
403 tny_mime_part_add_part (alternative_part, result);
405 tny_mime_part_add_part (TNY_MIME_PART (msg), result);
407 g_object_unref (G_OBJECT (parts));
410 /* We add a reference as this method is intended to obtain
413 return TNY_MIME_PART (msg);