54049ac008927ef0e15663ecadfeea7a715f8fd4
[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-text-buffer-stream.h>
34 #include <tny-mime-part-iface.h>
35 #include <tny-msg-iface.h>
36 #include <tny-list-iface.h>
37 #include <tny-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 gchar *
47 quote_msg (TnyMsgIface* src, const gchar * from, time_t sent_date, gint limit)
48 {
49         TnyStreamIface *stream;
50         TnyMimePartIface *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(src, FALSE);
57         if (!body)
58                 return NULL;
59
60         buf = gtk_text_buffer_new (NULL);
61         stream = TNY_STREAM_IFACE (tny_text_buffer_stream_new (buf));
62         tny_stream_iface_reset (stream);
63         tny_mime_part_iface_decode_to_stream (body, stream);
64         tny_stream_iface_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         quoted = modest_text_utils_quote (to_quote, from, sent_date, limit);
72         g_object_unref (buf);
73
74         return quoted;
75 }
76
77
78 gchar*
79 modest_tny_msg_actions_quote (TnyMsgIface * self, const gchar * from,
80                               time_t sent_date, gint limit,
81                               const gchar * to_quote)
82 {
83         /* 2 cases: */
84
85         /* a) quote text from selection */
86         if (to_quote != NULL) 
87                 return modest_text_utils_quote (to_quote, from, sent_date,
88                                                 limit);
89         
90         /* b) try to find a text/plain part in the msg and quote it */
91         return quote_msg (self, from, sent_date, limit);
92 }
93
94
95
96 TnyMimePartIface *
97 modest_tny_msg_actions_find_body_part (TnyMsgIface *msg, gboolean want_html)
98 {
99         const gchar *mime_type = want_html ? "text/html" : "text/plain";
100         TnyMimePartIface *part;
101         TnyListIface *parts;
102         TnyIteratorIface *iter;
103
104         if (!msg)
105                 return NULL;
106
107         parts = TNY_LIST_IFACE(tny_list_new());
108         tny_msg_iface_get_parts ((TnyMsgIface*)msg, parts);
109
110         iter  = tny_list_iface_create_iterator(parts);
111
112         while (!tny_iterator_iface_is_done(iter)) {
113
114                 part = TNY_MIME_PART_IFACE(tny_iterator_iface_current (iter));
115                 
116                 if (tny_mime_part_iface_content_type_is (part, mime_type) &&
117                     !tny_mime_part_iface_is_attachment (part)) {
118                         break;
119                 }
120                 part = NULL;
121                 tny_iterator_iface_next (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 (msg, FALSE);
136
137         return part ? part : NULL;
138 }
139
140
141
142 TnyMimePartIface *
143 modest_tny_msg_actions_find_nth_part (TnyMsgIface *msg, gint index)
144 {
145         TnyMimePartIface *part;
146         TnyListIface *parts;
147         TnyIteratorIface *iter;
148
149         g_return_val_if_fail (msg, NULL);
150         g_return_val_if_fail (index > 0, NULL);
151                 
152         parts = TNY_LIST_IFACE(tny_list_new());
153         tny_msg_iface_get_parts ((TnyMsgIface*)msg, parts);
154         iter  = tny_list_iface_create_iterator ((TnyListIface*)parts);
155         if (!tny_iterator_iface_has_first(iter))
156                 return NULL;
157         
158         tny_iterator_iface_nth (iter, index);
159         part = TNY_MIME_PART_IFACE(tny_iterator_iface_current (iter));
160
161         g_object_unref (G_OBJECT(iter));
162         g_object_unref (G_OBJECT(parts));
163
164         return part;
165 }