* now fix NB#78955:
[modest] / src / modest-tny-mime-part.c
1 /* Copyright (c) 2006, 2007, 2008 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 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif /*HAVE_CONFIG_H */
33
34 #include "modest-tny-mime-part.h"
35 #include <tny-simple-list.h>
36 #include <tny-msg.h>
37 #include <string.h> /* for strlen */
38 #include <modest-tny-msg.h>
39 #include "modest-text-utils.h"
40
41
42 gchar*
43 modest_tny_mime_part_get_header_value (TnyMimePart *part, const gchar *header)
44 {
45         TnyList *pairs;
46         TnyIterator *iter;
47         gchar *val;
48         
49         g_return_val_if_fail (part && TNY_IS_MIME_PART(part), NULL);
50         g_return_val_if_fail (header, NULL);
51
52         pairs = tny_simple_list_new ();
53         
54         tny_mime_part_get_header_pairs (part, pairs);
55         iter = tny_list_create_iterator (pairs);
56
57         val = NULL;
58         while (!tny_iterator_is_done(iter) && !val) {
59
60                 TnyPair *pair = (TnyPair*)tny_iterator_get_current(iter);
61                 if (strcasecmp (header, tny_pair_get_name(pair)) == 0)
62                         val = g_strdup (tny_pair_get_value(pair));
63                 g_object_unref (pair);          
64
65                 tny_iterator_next (iter);
66         }
67
68         g_object_unref (pairs);
69         g_object_unref (iter);
70
71         return val;
72 }
73
74
75
76
77 /* we consider more things attachments than tinymail does...
78  */
79 gboolean
80 modest_tny_mime_part_is_attachment_for_modest (TnyMimePart *part)
81 {
82         gchar *tmp, *content_type;
83         gboolean has_content_disp_name = FALSE;
84
85         g_return_val_if_fail (part && TNY_IS_MIME_PART(part), FALSE);
86         
87         /* if tinymail thinks it's an attachment, it definitely is */
88         if (tny_mime_part_is_attachment (part))
89                 return TRUE; 
90
91         /* if the mime part is a message itself (ie. embedded), it's an attachment */
92         if (TNY_IS_MSG (part))
93                 return TRUE;
94         
95         tmp = modest_tny_mime_part_get_header_value (part, "Content-Disposition");
96         if (tmp) {
97                 gchar *content_disp = g_ascii_strdown(tmp, -1);
98                 g_free (tmp);
99                 has_content_disp_name = g_strstr_len (content_disp, strlen(content_disp), "name=") != NULL;
100                 g_free (content_disp);
101         }
102                 
103         /* if it doesn't have a content deposition with a name= attribute, it's not an attachment */
104         if (!has_content_disp_name)
105                 return FALSE;
106         
107         /* ok, it must be content-disposition "inline" then, because "attachment"
108          * is already handle above "...is_attachment". modest consider these "inline" things
109          * attachments as well, unless they are embedded images for html mail 
110          */
111         content_type = g_ascii_strdown (tny_mime_part_get_content_type (part), -1);
112         if (!g_str_has_prefix (content_type, "image/")) {
113                 g_free (content_type);
114                 return TRUE; /* it's not an image, so it must be an attachment */
115         }
116         g_free (content_type);
117
118
119         /* now, if it's an inline-image, and it has a content-id or location, we
120          * we guess it's an inline image, and not an attachment */
121         if (tny_mime_part_get_content_id (part) || tny_mime_part_get_content_location(part))
122                 return FALSE;
123                 
124         /* in other cases... */
125         return TRUE;
126 }
127
128