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