442d69dccb2fafbbb776e33668456acbce6f81b4
[modest] / src / modest-tny-msg-actions.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
31 #include <gtk/gtk.h>
32 #include <gtkhtml/gtkhtml.h>
33 #include <tny-gtk-text-buffer-stream.h>
34 #include <tny-simple-list.h>
35 #include <tny-folder.h>
36
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif /*HAVE_CONFIG_H */
40
41 #include "modest-tny-msg-actions.h"
42 #include "modest-text-utils.h"
43
44 static const gchar *
45 get_body_text (TnyMsg *msg, gboolean want_html)
46 {
47         TnyStream *stream;
48         TnyMimePart *body;
49         GtkTextBuffer *buf;
50         GtkTextIter start, end;
51         const gchar *to_quote;
52
53         body = modest_tny_msg_actions_find_body_part(msg, want_html);
54         if (!body)
55                 return NULL;
56
57         buf = gtk_text_buffer_new (NULL);
58         stream = TNY_STREAM (tny_gtk_text_buffer_stream_new (buf));
59         tny_stream_reset (stream);
60         tny_mime_part_decode_to_stream (body, stream);
61         tny_stream_reset (stream);
62
63         g_object_unref (G_OBJECT(stream));
64         g_object_unref (G_OBJECT(body));
65         
66         gtk_text_buffer_get_bounds (buf, &start, &end);
67         to_quote = gtk_text_buffer_get_text (buf, &start, &end, FALSE);
68         g_object_unref (buf);
69
70         return to_quote;
71 }
72
73 gchar*
74 modest_tny_msg_actions_quote (TnyMsg * self, const gchar * from,
75                               time_t sent_date, gint limit,
76                               const gchar * to_quote)
77 {
78         gchar *quoted_msg = NULL;
79         const gchar *body;
80
81         /* 2 cases: */
82
83         /* a) quote text from selection */
84         if (to_quote != NULL) 
85                 return modest_text_utils_quote (to_quote, from, sent_date,
86                                                 limit);
87         
88         /* b) try to find a text/plain part in the msg and quote it */
89         body = get_body_text (self, FALSE);
90         if (body)
91                 quoted_msg = modest_text_utils_quote (body, from, sent_date, limit);
92         
93         return quoted_msg;
94 }
95
96
97
98 static TnyMimePart*
99 modest_tny_msg_actions_find_body_part_from_mime_part (TnyMimePart *msg, gboolean want_html)
100 {
101         const gchar *mime_type = want_html ? "text/html" : "text/plain";
102         TnyMimePart *part = NULL;
103         TnyList *parts;
104         TnyIterator *iter;
105
106         if (!msg)
107                 return NULL;
108
109         parts = TNY_LIST (tny_simple_list_new());
110         tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
111
112         iter  = tny_list_create_iterator(parts);
113
114         /* no parts? assume it's single-part message */
115         if (tny_iterator_is_done(iter)) 
116                 return TNY_MIME_PART (g_object_ref(G_OBJECT(msg)));
117         else {
118                 do {
119                         const gchar* content_type;
120                         part = TNY_MIME_PART(tny_iterator_get_current (iter));
121                         
122                         if (tny_mime_part_content_type_is (part, mime_type) &&
123                             !tny_mime_part_is_attachment (part)) 
124                                 break;
125                         
126                         content_type = tny_mime_part_get_content_type (part);
127                         if (g_str_has_prefix(content_type, "multipart")) {
128                                 part = modest_tny_msg_actions_find_body_part_from_mime_part (part,
129                                                                                              want_html);
130                                 if (part)
131                                         break;
132                         }
133
134                         part = NULL;
135                         tny_iterator_next (iter);
136
137                 } while (!tny_iterator_is_done(iter));
138         }
139         
140         /* did we find a matching part? */
141         if (part)
142                 g_object_ref (G_OBJECT(part));
143
144         g_object_unref (G_OBJECT(iter));
145         g_object_unref (G_OBJECT(parts));
146
147         /* if were trying to find an HTML part and couldn't find it,
148          * try to find a text/plain part instead
149          */
150         if (!part && want_html) 
151                 return modest_tny_msg_actions_find_body_part_from_mime_part (msg, FALSE);
152
153         if (!part)
154                 g_warning ("cannot find body part");
155         
156         return part ? part : NULL;
157 }
158
159
160 TnyMimePart*
161 modest_tny_msg_actions_find_body_part (TnyMsg *msg, gboolean want_html)
162 {
163         return modest_tny_msg_actions_find_body_part_from_mime_part (TNY_MIME_PART(msg),
164                                                                      want_html);
165 }
166
167
168 TnyMimePart *
169 modest_tny_msg_actions_find_nth_part (TnyMsg *msg, gint index)
170 {
171         TnyMimePart *part;
172         TnyList *parts;
173         TnyIterator *iter;
174
175         g_return_val_if_fail (msg, NULL);
176         g_return_val_if_fail (index > 0, NULL);
177                 
178         parts = TNY_LIST(tny_simple_list_new());
179         tny_mime_part_get_parts (TNY_MIME_PART(msg), parts);
180         iter  = tny_list_create_iterator (parts);
181
182         part = NULL;
183         
184         if (!tny_iterator_is_done(iter)) {
185                 tny_iterator_nth (iter, index);
186                 part = TNY_MIME_PART(tny_iterator_get_current (iter));
187         }
188
189         g_object_unref (G_OBJECT(iter));
190         g_object_unref (G_OBJECT(parts));
191
192         return part;
193 }
194
195 gchar * 
196 modest_tny_msg_actions_find_body (TnyMsg *msg, gboolean want_html)
197 {
198         const gchar *body;
199
200         body = get_body_text (msg, want_html);
201
202         if (body)
203                 return g_strdup (body);
204         else 
205                 return NULL;
206 }