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