* fix all compiler warnings (when using gtk/ frontend)
[modest] / src / modest-tny-msg.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.h>
41 #include "modest-text-utils.h"
42
43
44 gchar * 
45 modest_tny_msg_get_body (TnyMsg *msg, gboolean want_html)
46 {
47         TnyStream *stream;
48         TnyMimePart *body;
49         GtkTextBuffer *buf;
50         GtkTextIter start, end;
51         gchar *to_quote;
52
53         body = modest_tny_msg_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
74 TnyMimePart*
75 modest_tny_msg_find_body_part_from_mime_part (TnyMimePart *msg, gboolean want_html)
76 {
77         const gchar *mime_type = want_html ? "text/html" : "text/plain";
78         TnyMimePart *part = NULL;
79         TnyList *parts;
80         TnyIterator *iter;
81
82         if (!msg)
83                 return NULL;
84
85         parts = TNY_LIST (tny_simple_list_new());
86         tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
87
88         iter  = tny_list_create_iterator(parts);
89
90         /* no parts? assume it's single-part message */
91         if (tny_iterator_is_done(iter)) 
92                 return TNY_MIME_PART (g_object_ref(G_OBJECT(msg)));
93         else {
94                 gchar *content_type = NULL;
95                 do {
96                         part = TNY_MIME_PART(tny_iterator_get_current (iter));
97
98                         /* we need to strdown the content type, because
99                          * tny_mime_part_has_content_type does not do it...
100                          */
101                         content_type = g_ascii_strdown
102                                 (tny_mime_part_get_content_type (part), -1);
103                                                         
104                         if (g_str_has_prefix (content_type, mime_type) &&
105                             !tny_mime_part_is_attachment (part))
106                                 break;
107                         
108                         if (g_str_has_prefix(content_type, "multipart")) {
109                                 part = modest_tny_msg_find_body_part_from_mime_part (part, want_html);
110                                 if (part)
111                                         break;
112                         }
113                         if (part)
114                                 g_object_unref (G_OBJECT(part));
115
116                         part = NULL;
117                         
118                         g_free (content_type);
119                         content_type = NULL;
120
121                         tny_iterator_next (iter);
122                         
123                 } while (!tny_iterator_is_done(iter));
124                 g_free (content_type);
125         }
126         
127         g_object_unref (G_OBJECT(iter));
128         g_object_unref (G_OBJECT(parts));
129
130         /* if were trying to find an HTML part and couldn't find it,
131          * try to find a text/plain part instead
132          */
133         if (!part && want_html) 
134                 return modest_tny_msg_find_body_part_from_mime_part (msg, FALSE);
135         else
136                 return part; /* this maybe NULL, this is not an error; some message just don't have a body
137                               * part */
138 }
139
140
141 TnyMimePart*
142 modest_tny_msg_find_body_part (TnyMsg *msg, gboolean want_html)
143 {
144         return modest_tny_msg_find_body_part_from_mime_part (TNY_MIME_PART(msg),
145                                                              want_html);
146 }