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-gtk-text-buffer-stream.h>
34 #include <tny-camel-stream.h>
35 #include <camel/camel-stream-mem.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);
54 static TnyMsg *modest_formatter_do (ModestFormatter *self, TnyMimePart *body, TnyHeader *header,
57 static gchar* modest_formatter_wrapper_cite (ModestFormatter *self, const gchar *text, TnyHeader *header);
58 static gchar* modest_formatter_wrapper_quote (ModestFormatter *self, const gchar *text, TnyHeader *header);
59 static gchar* modest_formatter_wrapper_inline (ModestFormatter *self, const gchar *text, TnyHeader *header);
62 extract_text (ModestFormatter *self, TnyMimePart *body)
66 GtkTextIter start, end;
67 gchar *text, *converted_text;
68 ModestFormatterPrivate *priv;
70 buf = gtk_text_buffer_new (NULL);
71 stream = TNY_STREAM (tny_gtk_text_buffer_stream_new (buf));
72 tny_stream_reset (stream);
73 tny_mime_part_decode_to_stream (body, stream);
74 tny_stream_reset (stream);
76 g_object_unref (G_OBJECT(stream));
78 gtk_text_buffer_get_bounds (buf, &start, &end);
79 text = gtk_text_buffer_get_text (buf, &start, &end, FALSE);
80 g_object_unref (G_OBJECT(buf));
82 /* Convert to desired content type if needed */
83 priv = MODEST_FORMATTER_GET_PRIVATE (self);
85 if (strcmp (tny_mime_part_get_content_type (body), priv->content_type) == 0) {
86 if (!strcmp (priv->content_type, "text/html"))
87 converted_text = modest_text_utils_convert_to_html (text);
89 converted_text = g_strdup (text);
92 text = converted_text;
98 construct_from_text (TnyMimePart *part,
100 const gchar *content_type)
102 TnyStream *text_body_stream;
104 /* Create the stream */
105 text_body_stream = TNY_STREAM (tny_camel_stream_new
106 (camel_stream_mem_new_with_buffer
107 (text, strlen(text))));
109 /* Construct MIME part */
110 tny_stream_reset (text_body_stream);
111 tny_mime_part_construct_from_stream (part, text_body_stream, content_type);
112 tny_stream_reset (text_body_stream);
115 g_object_unref (G_OBJECT (text_body_stream));
119 modest_formatter_do (ModestFormatter *self, TnyMimePart *body, TnyHeader *header, FormatterFunc func)
121 TnyMsg *new_msg = NULL;
122 gchar *body_text = NULL, *txt = NULL;
123 ModestFormatterPrivate *priv;
125 g_return_val_if_fail (self, NULL);
126 g_return_val_if_fail (header, NULL);
127 g_return_val_if_fail (func, NULL);
130 new_msg = tny_platform_factory_new_msg (modest_runtime_get_platform_factory());
133 body_text = extract_text (self, body);
135 body_text = g_strdup ("");
137 txt = (gchar *) func (self, (const gchar*) body_text, header);
138 priv = MODEST_FORMATTER_GET_PRIVATE (self);
139 construct_from_text (TNY_MIME_PART (new_msg), (const gchar*) txt, priv->content_type);
149 modest_formatter_cite (ModestFormatter *self, TnyMimePart *body, TnyHeader *header)
151 return modest_formatter_do (self, body, header, modest_formatter_wrapper_cite);
155 modest_formatter_quote (ModestFormatter *self, TnyMimePart *body, TnyHeader *header)
157 return modest_formatter_do (self, body, header, modest_formatter_wrapper_quote);
161 modest_formatter_inline (ModestFormatter *self, TnyMimePart *body, TnyHeader *header)
163 return modest_formatter_do (self, body, header, modest_formatter_wrapper_inline);
167 modest_formatter_attach (ModestFormatter *self, TnyMimePart *body, TnyHeader *header)
169 TnyMsg *new_msg = NULL;
170 gchar *attach_text = NULL;
171 const gchar *subject;
172 TnyMimePart *body_part = NULL, *attach_part = NULL;
173 ModestFormatterPrivate *priv;
174 TnyPlatformFactory *fact;
176 fact = modest_runtime_get_platform_factory ();
178 new_msg = tny_platform_factory_new_msg (fact);
179 body_part = tny_platform_factory_new_mime_part (fact);
180 attach_part = tny_platform_factory_new_mime_part (fact);
182 /* Create the two parts */
183 priv = MODEST_FORMATTER_GET_PRIVATE (self);
184 attach_text = extract_text (self, body);
185 construct_from_text (body_part, "", priv->content_type);
186 construct_from_text (attach_part, (const gchar*) attach_text, priv->content_type);
187 subject = tny_header_get_subject (header);
188 tny_mime_part_set_filename (attach_part, subject ? subject : _("No subject"));
191 tny_mime_part_add_part (TNY_MIME_PART (new_msg), body_part);
192 tny_mime_part_add_part (TNY_MIME_PART (new_msg), attach_part);
195 g_free (attach_text);
201 modest_formatter_new (const gchar *content_type, const gchar *signature)
203 ModestFormatter *formatter;
204 ModestFormatterPrivate *priv;
206 formatter = g_object_new (MODEST_TYPE_FORMATTER, NULL);
207 priv = MODEST_FORMATTER_GET_PRIVATE (formatter);
208 priv->content_type = g_strdup (content_type);
209 priv->signature = g_strdup (signature);
215 modest_formatter_instance_init (GTypeInstance *instance, gpointer g_class)
217 ModestFormatter *self = (ModestFormatter *)instance;
218 ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
220 priv->content_type = NULL;
224 modest_formatter_finalize (GObject *object)
226 ModestFormatter *self = (ModestFormatter *)object;
227 ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
229 if (priv->content_type)
230 g_free (priv->content_type);
233 g_free (priv->signature);
235 (*parent_class->finalize) (object);
239 modest_formatter_class_init (ModestFormatterClass *class)
241 GObjectClass *object_class;
243 parent_class = g_type_class_peek_parent (class);
244 object_class = (GObjectClass*) class;
245 object_class->finalize = modest_formatter_finalize;
247 g_type_class_add_private (object_class, sizeof (ModestFormatterPrivate));
251 modest_formatter_get_type (void)
253 static GType type = 0;
255 if (G_UNLIKELY(type == 0))
257 static const GTypeInfo info =
259 sizeof (ModestFormatterClass),
260 NULL, /* base_init */
261 NULL, /* base_finalize */
262 (GClassInitFunc) modest_formatter_class_init, /* class_init */
263 NULL, /* class_finalize */
264 NULL, /* class_data */
265 sizeof (ModestFormatter),
267 modest_formatter_instance_init /* instance_init */
270 type = g_type_register_static (G_TYPE_OBJECT,
280 modest_formatter_wrapper_cite (ModestFormatter *self, const gchar *text, TnyHeader *header)
282 ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
284 return modest_text_utils_cite (text,
287 tny_header_get_from (header),
288 tny_header_get_date_sent (header));
292 modest_formatter_wrapper_inline (ModestFormatter *self, const gchar *text, TnyHeader *header)
294 ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
296 return modest_text_utils_inline (text,
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));
306 modest_formatter_wrapper_quote (ModestFormatter *self, const gchar *text, TnyHeader *header)
308 ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
310 /* TODO: get 80 from the configuration */
311 return modest_text_utils_quote (text,
314 tny_header_get_from (header),
315 tny_header_get_date_sent (header),