1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
32 #include <gtkhtml/gtkhtml.h>
34 /* TODO: put in auto* */
35 #include <tny-text-buffer-stream.h>
36 #include <tny-msg-mime-part-iface.h>
37 #include <tny-msg-iface.h>
38 #include <tny-list-iface.h>
42 #endif /*HAVE_CONFIG_H */
44 #include "modest-tny-msg-actions.h"
45 #include "modest-text-utils.h"
49 static gchar *quote_msg (const TnyMsgIface * src, const gchar * from,
50 time_t sent_date, gint limit);
53 quote_msg (const TnyMsgIface * src, const gchar * from, time_t sent_date, gint limit)
55 TnyStreamIface *stream;
56 TnyMsgMimePartIface *body;
58 GtkTextIter start, end;
59 const gchar *to_quote;
62 /* the cast makes me uneasy... */
63 body = modest_tny_msg_actions_find_body_part(src, FALSE);
67 buf = gtk_text_buffer_new (NULL);
68 stream = TNY_STREAM_IFACE (tny_text_buffer_stream_new (buf));
69 tny_stream_iface_reset (stream);
70 tny_msg_mime_part_iface_decode_to_stream (body, stream);
71 tny_stream_iface_reset (stream);
72 g_object_unref (stream);
74 gtk_text_buffer_get_bounds (buf, &start, &end);
75 to_quote = gtk_text_buffer_get_text (buf, &start, &end, FALSE);
76 quoted = modest_text_utils_quote (to_quote, from, sent_date, limit);
84 modest_tny_msg_actions_quote (const TnyMsgIface * self, const gchar * from,
85 time_t sent_date, gint limit,
86 const gchar * to_quote)
90 /* a) quote text from selection */
92 return modest_text_utils_quote (to_quote, from, sent_date,
95 /* b) try to find a text/plain part in the msg and quote it */
96 return quote_msg (self, from, sent_date, limit);
101 TnyMsgMimePartIface *
102 modest_tny_msg_actions_find_body_part (const TnyMsgIface *msg, gboolean want_html)
104 const gchar *mime_type = want_html ? "text/html" : "text/plain";
105 TnyMsgMimePartIface *part;
106 const TnyListIface *parts;
107 TnyIteratorIface *iter;
114 parts = tny_msg_iface_get_parts ((TnyMsgIface *)msg);
115 iter = tny_list_iface_create_iterator ((TnyListIface*)parts);
117 while (!tny_iterator_iface_is_done(iter)) {
118 part = TNY_MSG_MIME_PART_IFACE(tny_iterator_iface_current (iter));
120 if (tny_msg_mime_part_iface_content_type_is (part, mime_type) &&
121 !tny_msg_mime_part_iface_is_attachment (part)) {
125 tny_iterator_iface_next (iter);
128 g_object_unref (G_OBJECT(iter));
130 /* if were trying to find an HTML part and could find it,
131 * try to find a text/plain part instead
133 if (!found && want_html)
134 return modest_tny_msg_actions_find_body_part (msg, FALSE);
136 return found ? part : NULL;
142 TnyMsgMimePartIface *
143 modest_tny_msg_actions_find_nth_part (const TnyMsgIface *msg, gint index)
145 TnyMsgMimePartIface *part;
146 const TnyListIface *parts;
147 TnyIteratorIface *iter;
149 g_return_val_if_fail (msg, NULL);
150 g_return_val_if_fail (index > 0, NULL);
152 parts = tny_msg_iface_get_parts ((TnyMsgIface *)msg);
153 iter = tny_list_iface_create_iterator ((TnyListIface*)parts);
155 if (!tny_iterator_iface_has_first(iter))
158 part = tny_iterator_iface_nth (iter, index);
160 g_object_unref (G_OBJECT(iter));