81af6dca921b9651e27f00b698c3a7cfb5c2252a
[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 <gtk/gtk.h>
31 #include <glib/gi18n.h>
32 #include <string.h>
33 #include <tny-header.h>
34 #include <tny-gtk-text-buffer-stream.h>
35 #include <tny-camel-stream.h>
36 #include <tny-camel-msg.h>
37 #include <camel/camel-stream-mem.h>
38 #include "modest-formatter.h"
39 #include "modest-text-utils.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, 
54                                     TnyMimePart *body, 
55                                     TnyHeader *header, 
56                                     FormatterFunc func);
57
58 static gchar*  modest_formatter_wrapper_cite   (ModestFormatter *self, const gchar *text, TnyHeader *header);
59 static gchar*  modest_formatter_wrapper_quote  (ModestFormatter *self, const gchar *text, TnyHeader *header);
60 static gchar*  modest_formatter_wrapper_inline (ModestFormatter *self, const gchar *text, TnyHeader *header);
61
62 static gchar *
63 extract_text (ModestFormatter *self, TnyMimePart *body)
64 {
65         TnyStream *stream;
66         GtkTextBuffer *buf;
67         GtkTextIter start, end;
68         gchar *text, *converted_text;
69         ModestFormatterPrivate *priv;
70
71         buf = gtk_text_buffer_new (NULL);
72         stream = TNY_STREAM (tny_gtk_text_buffer_stream_new (buf));
73         tny_stream_reset (stream);
74         tny_mime_part_decode_to_stream (body, stream);
75         tny_stream_reset (stream);
76
77         g_object_unref (G_OBJECT(stream));
78         g_object_unref (G_OBJECT(body));
79         
80         gtk_text_buffer_get_bounds (buf, &start, &end);
81         text = gtk_text_buffer_get_text (buf, &start, &end, FALSE);
82         g_object_unref (buf);
83
84         /* Convert to desired content type if needed */
85         priv = MODEST_FORMATTER_GET_PRIVATE (self);
86
87         if (strcmp (tny_mime_part_get_content_type (body), priv->content_type)) {
88                 if (!strcmp (priv->content_type, "text/html"))
89                         converted_text = modest_text_utils_convert_to_html  (text);
90 /*              else */
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;
128         gchar *body_text = NULL, *txt = NULL;
129         ModestFormatterPrivate *priv;
130
131         /* Build new part */
132         new_msg = TNY_MSG (tny_camel_msg_new ());
133         body_text = extract_text (self, body);
134         txt = (gchar *) func (self, (const gchar*) body_text, header);
135         priv = MODEST_FORMATTER_GET_PRIVATE (self);
136         construct_from_text (TNY_MIME_PART (new_msg), (const gchar*) txt, priv->content_type);
137
138         /* Clean */
139         g_free (body_text);
140         g_free (txt);
141
142         return new_msg;
143 }
144
145 TnyMsg *
146 modest_formatter_cite (ModestFormatter *self, TnyMimePart *body, TnyHeader *header)
147 {
148         return modest_formatter_do (self, body, header, modest_formatter_wrapper_cite);
149 }
150
151 TnyMsg *
152 modest_formatter_quote (ModestFormatter *self, TnyMimePart *body, TnyHeader *header)
153 {
154         return modest_formatter_do (self, body, header, modest_formatter_wrapper_quote);
155 }
156
157 TnyMsg *
158 modest_formatter_inline (ModestFormatter *self, TnyMimePart *body, TnyHeader *header)
159 {
160         return modest_formatter_do (self, body, header, modest_formatter_wrapper_inline);
161 }
162
163 TnyMsg *
164 modest_formatter_attach (ModestFormatter *self, TnyMimePart *body, TnyHeader *header)
165 {
166         TnyMsg *new_msg = NULL;
167         gchar *attach_text = NULL;
168         TnyMimePart *body_part = NULL, *attach_part = NULL;
169         ModestFormatterPrivate *priv;
170
171         /* Build new part */
172         new_msg     = TNY_MSG (tny_camel_msg_new ());
173         body_part   = TNY_MIME_PART (tny_camel_mime_part_new (camel_mime_part_new()));
174         attach_part = TNY_MIME_PART (tny_camel_mime_part_new (camel_mime_part_new()));
175
176         /* Create the two parts */
177         priv = MODEST_FORMATTER_GET_PRIVATE (self);
178         attach_text = extract_text (self, body);
179         construct_from_text (body_part, "", priv->content_type);
180         construct_from_text (attach_part, (const gchar*) attach_text, priv->content_type);
181         tny_mime_part_set_filename (attach_part, tny_header_get_subject (header));
182
183         /* Add parts */
184         tny_mime_part_add_part (TNY_MIME_PART (new_msg), body_part);
185         tny_mime_part_add_part (TNY_MIME_PART (new_msg), attach_part);
186
187         /* Clean */
188         g_free (attach_text);
189
190         return new_msg;
191 }
192
193 ModestFormatter*
194 modest_formatter_new (const gchar *content_type)
195 {
196         ModestFormatter *formatter;
197         ModestFormatterPrivate *priv;
198
199         formatter = g_object_new (MODEST_TYPE_FORMATTER, NULL);
200         priv = MODEST_FORMATTER_GET_PRIVATE (formatter);
201         priv->content_type = g_strdup (content_type);
202
203         return formatter;
204 }
205
206 static void
207 modest_formatter_instance_init (GTypeInstance *instance, gpointer g_class)
208 {
209         ModestFormatter *self = (ModestFormatter *)instance;
210         ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
211
212         priv->content_type = NULL;
213 }
214
215 static void
216 modest_formatter_finalize (GObject *object)
217 {
218         ModestFormatter *self = (ModestFormatter *)object;
219         ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
220
221         if (priv->content_type)
222                 g_free (priv->content_type);
223
224         (*parent_class->finalize) (object);
225 }
226
227 static void 
228 modest_formatter_class_init (ModestFormatterClass *class)
229 {
230         GObjectClass *object_class;
231
232         parent_class = g_type_class_peek_parent (class);
233         object_class = (GObjectClass*) class;
234         object_class->finalize = modest_formatter_finalize;
235
236         g_type_class_add_private (object_class, sizeof (ModestFormatterPrivate));
237 }
238
239 GType 
240 modest_formatter_get_type (void)
241 {
242         static GType type = 0;
243
244         if (G_UNLIKELY(type == 0))
245         {
246                 static const GTypeInfo info = 
247                 {
248                   sizeof (ModestFormatterClass),
249                   NULL,   /* base_init */
250                   NULL,   /* base_finalize */
251                   (GClassInitFunc) modest_formatter_class_init,   /* class_init */
252                   NULL,   /* class_finalize */
253                   NULL,   /* class_data */
254                   sizeof (ModestFormatter),
255                   0,      /* n_preallocs */
256                   modest_formatter_instance_init    /* instance_init */
257                 };
258                 
259                 type = g_type_register_static (G_TYPE_OBJECT,
260                         "ModestFormatter",
261                         &info, 0);
262         }
263
264         return type;
265 }
266
267 /****************/
268 static gchar *
269 modest_formatter_wrapper_cite (ModestFormatter *self, const gchar *text, TnyHeader *header) 
270 {
271         ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
272
273         return modest_text_utils_cite (text, 
274                                        priv->content_type, 
275                                        tny_header_get_from (header), 
276                                        tny_header_get_date_sent (header));
277 }
278
279 static gchar *
280 modest_formatter_wrapper_inline (ModestFormatter *self, const gchar *text, TnyHeader *header) 
281 {
282         ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
283
284         return modest_text_utils_inline (text, 
285                                          priv->content_type, 
286                                          tny_header_get_from (header), 
287                                          tny_header_get_date_sent (header),
288                                          tny_header_get_to (header),
289                                          tny_header_get_subject (header));
290 }
291
292 static gchar *
293 modest_formatter_wrapper_quote (ModestFormatter *self, const gchar *text, TnyHeader *header) 
294 {
295         ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
296
297         /* TODO: get 80 from the configuration */
298         return modest_text_utils_quote (text, 
299                                         priv->content_type, 
300                                         tny_header_get_from (header), 
301                                         tny_header_get_date_sent (header),
302                                         80);
303 }