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