Fixed some compilation errors
[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, TnyMimePart *body,  TnyHeader *header, 
53                                     FormatterFunc func);
54
55 static gchar*  modest_formatter_wrapper_cite   (ModestFormatter *self, const gchar *text, TnyHeader *header);
56 static gchar*  modest_formatter_wrapper_quote  (ModestFormatter *self, const gchar *text, TnyHeader *header);
57 static gchar*  modest_formatter_wrapper_inline (ModestFormatter *self, const gchar *text, TnyHeader *header);
58
59 static gchar *
60 extract_text (ModestFormatter *self, TnyMimePart *body)
61 {
62         TnyStream *stream;
63         GtkTextBuffer *buf;
64         GtkTextIter start, end;
65         gchar *text, *converted_text;
66         ModestFormatterPrivate *priv;
67
68         buf = gtk_text_buffer_new (NULL);
69         stream = TNY_STREAM (tny_gtk_text_buffer_stream_new (buf));
70         tny_stream_reset (stream);
71         tny_mime_part_decode_to_stream (body, stream);
72         tny_stream_reset (stream);
73
74         g_object_unref (G_OBJECT(stream));
75         
76         gtk_text_buffer_get_bounds (buf, &start, &end);
77         text = gtk_text_buffer_get_text (buf, &start, &end, FALSE);
78         g_object_unref (G_OBJECT(buf));
79
80         /* Convert to desired content type if needed */
81         priv = MODEST_FORMATTER_GET_PRIVATE (self);
82
83         if (strcmp (tny_mime_part_get_content_type (body), priv->content_type) == 0) {
84                 if (!strcmp (priv->content_type, "text/html"))
85                         converted_text = modest_text_utils_convert_to_html  (text);
86                 else
87                         converted_text = g_strdup (text);
88
89                 g_free (text);
90                 text = converted_text;
91         }
92         return text;
93 }
94
95 static void
96 construct_from_text (TnyMimePart *part,
97                      const gchar *text,
98                      const gchar *content_type)
99 {
100         TnyStream *text_body_stream;
101
102         /* Create the stream */
103         text_body_stream = TNY_STREAM (tny_camel_stream_new
104                                        (camel_stream_mem_new_with_buffer
105                                         (text, strlen(text))));
106
107         /* Construct MIME part */
108         tny_stream_reset (text_body_stream);
109         tny_mime_part_construct_from_stream (part, text_body_stream, content_type);
110         tny_stream_reset (text_body_stream);
111
112         /* Clean */
113         g_object_unref (G_OBJECT (text_body_stream));
114 }
115
116 static TnyMsg *
117 modest_formatter_do (ModestFormatter *self, TnyMimePart *body, TnyHeader *header, FormatterFunc func)
118 {
119         TnyMsg *new_msg = NULL;
120         gchar *body_text = NULL, *txt = NULL;
121         ModestFormatterPrivate *priv;
122         TnyPlatformFactory *fact;
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         fact = modest_tny_platform_factory_get_instance ();
130         new_msg = tny_platform_factory_new_msg (fact);
131
132         if (body)
133                 body_text = extract_text (self, body);
134         else
135                 body_text = g_strdup ("");
136
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);
140
141         /* Clean */
142         g_free (body_text);
143         g_free (txt);
144
145         return new_msg;
146 }
147
148 TnyMsg *
149 modest_formatter_cite (ModestFormatter *self, TnyMimePart *body, TnyHeader *header)
150 {
151         return modest_formatter_do (self, body, header, modest_formatter_wrapper_cite);
152 }
153
154 TnyMsg *
155 modest_formatter_quote (ModestFormatter *self, TnyMimePart *body, TnyHeader *header)
156 {
157         return modest_formatter_do (self, body, header, modest_formatter_wrapper_quote);
158 }
159
160 TnyMsg *
161 modest_formatter_inline (ModestFormatter *self, TnyMimePart *body, TnyHeader *header)
162 {
163         return modest_formatter_do (self, body, header, modest_formatter_wrapper_inline);
164 }
165
166 TnyMsg *
167 modest_formatter_attach (ModestFormatter *self, TnyMimePart *body, TnyHeader *header)
168 {
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;
175
176         fact = modest_tny_platform_factory_get_instance ();
177         /* Build new part */
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);
181
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"));
189
190         /* Add parts */
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);
193
194         /* Clean */
195         g_free (attach_text);
196
197         return new_msg;
198 }
199
200 ModestFormatter*
201 modest_formatter_new (const gchar *content_type)
202 {
203         ModestFormatter *formatter;
204         ModestFormatterPrivate *priv;
205
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
210         return formatter;
211 }
212
213 static void
214 modest_formatter_instance_init (GTypeInstance *instance, gpointer g_class)
215 {
216         ModestFormatter *self = (ModestFormatter *)instance;
217         ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
218
219         priv->content_type = NULL;
220 }
221
222 static void
223 modest_formatter_finalize (GObject *object)
224 {
225         ModestFormatter *self = (ModestFormatter *)object;
226         ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
227
228         if (priv->content_type)
229                 g_free (priv->content_type);
230
231         (*parent_class->finalize) (object);
232 }
233
234 static void 
235 modest_formatter_class_init (ModestFormatterClass *class)
236 {
237         GObjectClass *object_class;
238
239         parent_class = g_type_class_peek_parent (class);
240         object_class = (GObjectClass*) class;
241         object_class->finalize = modest_formatter_finalize;
242
243         g_type_class_add_private (object_class, sizeof (ModestFormatterPrivate));
244 }
245
246 GType 
247 modest_formatter_get_type (void)
248 {
249         static GType type = 0;
250
251         if (G_UNLIKELY(type == 0))
252         {
253                 static const GTypeInfo info = 
254                 {
255                   sizeof (ModestFormatterClass),
256                   NULL,   /* base_init */
257                   NULL,   /* base_finalize */
258                   (GClassInitFunc) modest_formatter_class_init,   /* class_init */
259                   NULL,   /* class_finalize */
260                   NULL,   /* class_data */
261                   sizeof (ModestFormatter),
262                   0,      /* n_preallocs */
263                   modest_formatter_instance_init    /* instance_init */
264                 };
265                 
266                 type = g_type_register_static (G_TYPE_OBJECT,
267                         "ModestFormatter",
268                         &info, 0);
269         }
270
271         return type;
272 }
273
274 /****************/
275 static gchar *
276 modest_formatter_wrapper_cite (ModestFormatter *self, const gchar *text, TnyHeader *header) 
277 {
278         ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
279
280         return modest_text_utils_cite (text, 
281                                        priv->content_type, 
282                                        tny_header_get_from (header), 
283                                        tny_header_get_date_sent (header));
284 }
285
286 static gchar *
287 modest_formatter_wrapper_inline (ModestFormatter *self, const gchar *text, TnyHeader *header) 
288 {
289         ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
290
291         return modest_text_utils_inline (text, 
292                                          priv->content_type, 
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));
297 }
298
299 static gchar *
300 modest_formatter_wrapper_quote (ModestFormatter *self, const gchar *text, TnyHeader *header) 
301 {
302         ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
303
304         /* TODO: get 80 from the configuration */
305         return modest_text_utils_quote (text, 
306                                         priv->content_type, 
307                                         tny_header_get_from (header), 
308                                         tny_header_get_date_sent (header),
309                                         80);
310 }