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