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