ff73f89e9d6e30ff2af5cf0768e390d6fb96b071
[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-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>
40
41 typedef struct _ModestFormatterPrivate ModestFormatterPrivate;
42 struct _ModestFormatterPrivate {
43         gchar *content_type;
44 };
45 #define MODEST_FORMATTER_GET_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE((o), \
46                                           MODEST_TYPE_FORMATTER, \
47                                           ModestFormatterPrivate))
48
49 static GObjectClass *parent_class = NULL;
50
51 typedef gchar* FormatterFunc (ModestFormatter *self, const gchar *text, TnyHeader *header);
52
53 static TnyMsg *modest_formatter_do (ModestFormatter *self, TnyMimePart *body,  TnyHeader *header, 
54                                     FormatterFunc func);
55
56 static gchar*  modest_formatter_wrapper_cite   (ModestFormatter *self, const gchar *text, TnyHeader *header);
57 static gchar*  modest_formatter_wrapper_quote  (ModestFormatter *self, const gchar *text, TnyHeader *header);
58 static gchar*  modest_formatter_wrapper_inline (ModestFormatter *self, const gchar *text, TnyHeader *header);
59
60 static gchar *
61 extract_text (ModestFormatter *self, TnyMimePart *body)
62 {
63         TnyStream *stream;
64         GtkTextBuffer *buf;
65         GtkTextIter start, end;
66         gchar *text, *converted_text;
67         ModestFormatterPrivate *priv;
68
69         buf = gtk_text_buffer_new (NULL);
70         stream = TNY_STREAM (tny_gtk_text_buffer_stream_new (buf));
71         tny_stream_reset (stream);
72         tny_mime_part_decode_to_stream (body, stream);
73         tny_stream_reset (stream);
74
75         g_object_unref (G_OBJECT(stream));
76         
77         gtk_text_buffer_get_bounds (buf, &start, &end);
78         text = gtk_text_buffer_get_text (buf, &start, &end, FALSE);
79         g_object_unref (G_OBJECT(buf));
80
81         /* Convert to desired content type if needed */
82         priv = MODEST_FORMATTER_GET_PRIVATE (self);
83
84         if (strcmp (tny_mime_part_get_content_type (body), priv->content_type) == 0) {
85                 if (!strcmp (priv->content_type, "text/html"))
86                         converted_text = modest_text_utils_convert_to_html  (text);
87                 else
88                         converted_text = g_strdup (text);
89
90                 g_free (text);
91                 text = converted_text;
92         }
93         return text;
94 }
95
96 static void
97 construct_from_text (TnyMimePart *part,
98                      const gchar *text,
99                      const gchar *content_type)
100 {
101         TnyStream *text_body_stream;
102
103         /* Create the stream */
104         text_body_stream = TNY_STREAM (tny_camel_stream_new
105                                        (camel_stream_mem_new_with_buffer
106                                         (text, strlen(text))));
107
108         /* Construct MIME part */
109         tny_stream_reset (text_body_stream);
110         tny_mime_part_construct_from_stream (part, text_body_stream, content_type);
111         tny_stream_reset (text_body_stream);
112
113         /* Clean */
114         g_object_unref (G_OBJECT (text_body_stream));
115 }
116
117 static TnyMsg *
118 modest_formatter_do (ModestFormatter *self, TnyMimePart *body, TnyHeader *header, FormatterFunc func)
119 {
120         TnyMsg *new_msg = NULL;
121         gchar *body_text = NULL, *txt = NULL;
122         ModestFormatterPrivate *priv;
123
124         g_return_val_if_fail (self, NULL);
125         g_return_val_if_fail (header, NULL);
126         g_return_val_if_fail (func, NULL);
127
128         /* Build new part */
129         new_msg = tny_platform_factory_new_msg (modest_runtime_get_platform_factory());
130
131         if (body)
132                 body_text = extract_text (self, body);
133         else
134                 body_text = g_strdup ("");
135
136         txt = (gchar *) func (self, (const gchar*) body_text, header);
137         priv = MODEST_FORMATTER_GET_PRIVATE (self);
138         construct_from_text (TNY_MIME_PART (new_msg), (const gchar*) txt, priv->content_type);
139
140         /* Clean */
141         g_free (body_text);
142         g_free (txt);
143
144         return new_msg;
145 }
146
147 TnyMsg *
148 modest_formatter_cite (ModestFormatter *self, TnyMimePart *body, TnyHeader *header)
149 {
150         return modest_formatter_do (self, body, header, modest_formatter_wrapper_cite);
151 }
152
153 TnyMsg *
154 modest_formatter_quote (ModestFormatter *self, TnyMimePart *body, TnyHeader *header)
155 {
156         return modest_formatter_do (self, body, header, modest_formatter_wrapper_quote);
157 }
158
159 TnyMsg *
160 modest_formatter_inline (ModestFormatter *self, TnyMimePart *body, TnyHeader *header)
161 {
162         return modest_formatter_do (self, body, header, modest_formatter_wrapper_inline);
163 }
164
165 TnyMsg *
166 modest_formatter_attach (ModestFormatter *self, TnyMimePart *body, TnyHeader *header)
167 {
168         TnyMsg *new_msg = NULL;
169         gchar *attach_text = NULL;
170         const gchar *subject;
171         TnyMimePart *body_part = NULL, *attach_part = NULL;
172         ModestFormatterPrivate *priv;
173         TnyPlatformFactory *fact;
174
175         fact = modest_runtime_get_platform_factory ();
176         /* Build new part */
177         new_msg     = tny_platform_factory_new_msg (fact);
178         body_part   = tny_platform_factory_new_mime_part (fact);
179         attach_part = tny_platform_factory_new_mime_part (fact);
180
181         /* Create the two parts */
182         priv = MODEST_FORMATTER_GET_PRIVATE (self);
183         attach_text = extract_text (self, body);
184         construct_from_text (body_part, "", priv->content_type);
185         construct_from_text (attach_part, (const gchar*) attach_text, priv->content_type);
186         subject = tny_header_get_subject (header);
187         tny_mime_part_set_filename (attach_part, subject ? subject : _("No subject"));
188
189         /* Add parts */
190         tny_mime_part_add_part (TNY_MIME_PART (new_msg), body_part);
191         tny_mime_part_add_part (TNY_MIME_PART (new_msg), attach_part);
192
193         /* Clean */
194         g_free (attach_text);
195
196         return new_msg;
197 }
198
199 ModestFormatter*
200 modest_formatter_new (const gchar *content_type)
201 {
202         ModestFormatter *formatter;
203         ModestFormatterPrivate *priv;
204
205         formatter = g_object_new (MODEST_TYPE_FORMATTER, NULL);
206         priv = MODEST_FORMATTER_GET_PRIVATE (formatter);
207         priv->content_type = g_strdup (content_type);
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         (*parent_class->finalize) (object);
231 }
232
233 static void 
234 modest_formatter_class_init (ModestFormatterClass *class)
235 {
236         GObjectClass *object_class;
237
238         parent_class = g_type_class_peek_parent (class);
239         object_class = (GObjectClass*) class;
240         object_class->finalize = modest_formatter_finalize;
241
242         g_type_class_add_private (object_class, sizeof (ModestFormatterPrivate));
243 }
244
245 GType 
246 modest_formatter_get_type (void)
247 {
248         static GType type = 0;
249
250         if (G_UNLIKELY(type == 0))
251         {
252                 static const GTypeInfo info = 
253                 {
254                   sizeof (ModestFormatterClass),
255                   NULL,   /* base_init */
256                   NULL,   /* base_finalize */
257                   (GClassInitFunc) modest_formatter_class_init,   /* class_init */
258                   NULL,   /* class_finalize */
259                   NULL,   /* class_data */
260                   sizeof (ModestFormatter),
261                   0,      /* n_preallocs */
262                   modest_formatter_instance_init    /* instance_init */
263                 };
264                 
265                 type = g_type_register_static (G_TYPE_OBJECT,
266                         "ModestFormatter",
267                         &info, 0);
268         }
269
270         return type;
271 }
272
273 /****************/
274 static gchar *
275 modest_formatter_wrapper_cite (ModestFormatter *self, const gchar *text, TnyHeader *header) 
276 {
277         ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
278
279         return modest_text_utils_cite (text, 
280                                        priv->content_type, 
281                                        tny_header_get_from (header), 
282                                        tny_header_get_date_sent (header));
283 }
284
285 static gchar *
286 modest_formatter_wrapper_inline (ModestFormatter *self, const gchar *text, TnyHeader *header) 
287 {
288         ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
289
290         return modest_text_utils_inline (text, 
291                                          priv->content_type, 
292                                          tny_header_get_from (header), 
293                                          tny_header_get_date_sent (header),
294                                          tny_header_get_to (header),
295                                          tny_header_get_subject (header));
296 }
297
298 static gchar *
299 modest_formatter_wrapper_quote (ModestFormatter *self, const gchar *text, TnyHeader *header) 
300 {
301         ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
302
303         /* TODO: get 80 from the configuration */
304         return modest_text_utils_quote (text, 
305                                         priv->content_type, 
306                                         tny_header_get_from (header), 
307                                         tny_header_get_date_sent (header),
308                                         80);
309 }