* make it compile again with latest Tinymail; does not work yet though.
[modest] / src / modest-tny-attachment.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 /* modest-tny-attachment.c */
32
33 #include "modest-tny-attachment.h"
34 #include "modest-tny-msg-actions.h"
35
36 #include <tny-stream-camel.h>
37 #include <tny-fs-stream.h>
38 #include <camel/camel.h>
39
40
41 /* include other impl specific header files */
42
43 /* 'private'/'protected' functions */
44 static void                       modest_tny_attachment_class_init    (ModestTnyAttachmentClass *klass);
45 static void                       modest_tny_attachment_init          (ModestTnyAttachment *obj);
46 static void                       modest_tny_attachment_finalize      (GObject *obj);
47
48 /* list my signals */
49 enum {
50         /* MY_SIGNAL_1, */
51         /* MY_SIGNAL_2, */
52         LAST_SIGNAL
53 };
54
55 typedef struct _ModestTnyAttachmentPrivate ModestTnyAttachmentPrivate;
56 struct _ModestTnyAttachmentPrivate {
57         gchar *name;
58         gchar *filename;
59         gchar *mime_type;
60         gchar *disposition;
61         gchar *content_id;
62         TnyStreamIface *stream;
63 };
64 #define MODEST_TNY_ATTACHMENT_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
65                                                    MODEST_TYPE_TNY_ATTACHMENT, \
66                                                    ModestTnyAttachmentPrivate))
67 /* globals */
68 static GObjectClass *parent_class = NULL;
69
70 /* uncomment the following if you have defined any signals */
71 /* static guint signals[LAST_SIGNAL] = {0}; */
72
73 GType
74 modest_tny_attachment_get_type (void)
75 {
76         static GType my_type = 0;
77         if (!my_type) {
78                 static const GTypeInfo my_info = {
79                         sizeof(ModestTnyAttachmentClass),
80                         NULL,           /* base init */
81                         NULL,           /* base finalize */
82                         (GClassInitFunc) modest_tny_attachment_class_init,
83                         NULL,           /* class finalize */
84                         NULL,           /* class data */
85                         sizeof(ModestTnyAttachment),
86                         1,              /* n_preallocs */
87                         (GInstanceInitFunc) modest_tny_attachment_init,
88                         NULL
89                 };
90                 my_type = g_type_register_static (G_TYPE_OBJECT,
91                                                   "ModestTnyAttachment",
92                                                   &my_info, 0);
93         }
94         return my_type;
95 }
96
97 static void
98 modest_tny_attachment_class_init (ModestTnyAttachmentClass *klass)
99 {
100         GObjectClass *gobject_class;
101         gobject_class = (GObjectClass*) klass;
102
103         parent_class            = g_type_class_peek_parent (klass);
104         gobject_class->finalize = modest_tny_attachment_finalize;
105
106         g_type_class_add_private (gobject_class, sizeof(ModestTnyAttachmentPrivate));
107
108         /* signal definitions go here, e.g.: */
109 /*      signals[MY_SIGNAL_1] = */
110 /*              g_signal_new ("my_signal_1",....); */
111 /*      signals[MY_SIGNAL_2] = */
112 /*              g_signal_new ("my_signal_2",....); */
113 /*      etc. */
114 }
115
116 static void
117 modest_tny_attachment_init (ModestTnyAttachment *obj)
118 {
119         ModestTnyAttachmentPrivate *priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(obj);
120
121         priv->name = NULL;
122         priv->filename = NULL;
123         priv->mime_type = NULL;
124         priv->disposition = NULL;
125         priv->content_id = NULL;
126         priv->stream = NULL;
127 }
128
129 static void
130 modest_tny_attachment_finalize (GObject *obj)
131 {
132         ModestTnyAttachmentPrivate *priv;
133         
134         priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(MODEST_TNY_ATTACHMENT(obj));
135         g_free(priv->name);
136         g_free(priv->mime_type);
137         g_free(priv->disposition);
138         g_free(priv->content_id);
139         if (priv->stream)
140                 g_object_unref(G_OBJECT(priv->stream));
141 }
142
143 ModestTnyAttachment *
144 modest_tny_attachment_new (void)
145 {
146         return MODEST_TNY_ATTACHMENT(G_OBJECT(g_object_new(MODEST_TYPE_TNY_ATTACHMENT, NULL)));
147 }
148
149
150 void
151 modest_tny_attachment_set_name (ModestTnyAttachment *self, const gchar * thing)
152 {
153         ModestTnyAttachmentPrivate *priv;
154         
155         priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
156         g_free(priv->name);
157         priv->name = g_strdup(thing);
158 }
159
160 const gchar *
161 modest_tny_attachment_get_name (ModestTnyAttachment *self)
162 {
163         ModestTnyAttachmentPrivate *priv;
164         
165         priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
166         if (!priv->name)
167                 if (priv->filename)
168                         priv->name = g_path_get_basename(priv->filename);
169         return priv->name;
170 }
171
172
173 void
174 modest_tny_attachment_set_filename (ModestTnyAttachment *self, const gchar * thing)
175 {
176         ModestTnyAttachmentPrivate *priv;
177         
178         priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
179         g_free(priv->filename);
180         priv->filename = g_strdup(thing);
181 }
182
183 const gchar *
184 modest_tny_attachment_get_filename (ModestTnyAttachment *self)
185 {
186         ModestTnyAttachmentPrivate *priv;
187         
188         priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
189         return priv->filename;
190 }
191
192
193 void
194 modest_tny_attachment_set_mime_type (ModestTnyAttachment *self, const gchar * thing)
195 {
196         ModestTnyAttachmentPrivate *priv;
197         
198         priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
199         g_free(priv->mime_type);
200         priv->mime_type = g_strdup(thing);
201 }
202
203 const gchar *
204 modest_tny_attachment_get_mime_type (ModestTnyAttachment *self)
205 {
206         ModestTnyAttachmentPrivate *priv;
207         
208         priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
209         return priv->mime_type;
210 }
211
212
213 void
214 modest_tny_attachment_guess_mime_type (ModestTnyAttachment *self)
215 {
216         ModestTnyAttachmentPrivate *priv;
217         gchar *suffixes[] = {".jpg", ".gif", ".png", ".mp3", ".ogg", /* default: */ "", NULL};
218         gchar *types[]    = {"image/jpeg", "image/gif", "image/png", "audio/mpeg", "application/ogg", "application/octet-stream", NULL};
219         gchar *low_fn;
220         gint pos;
221         
222         priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
223         if (!priv->filename)
224                 return;
225         
226         low_fn = g_utf8_strdown(priv->filename, -1);
227         for (pos = 0 ; suffixes[pos] ; pos++) {
228                 if (g_str_has_suffix(low_fn, suffixes[pos]))
229                         break;
230         }
231         
232         g_free(low_fn);
233         g_free(priv->mime_type);
234         if (suffixes[pos])
235                 priv->mime_type = g_strdup(types[pos]);
236         else
237                 priv->mime_type = NULL;
238 }
239
240 static TnyStreamIface *
241 make_stream_from_file(const gchar * filename)
242 {
243         return NULL;
244 }
245
246 void
247 modest_tny_attachment_set_stream(ModestTnyAttachment *self, TnyStreamIface *thing)
248 {
249         ModestTnyAttachmentPrivate *priv;
250         
251         priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
252         if (priv->stream)
253                 g_object_unref(G_OBJECT(priv->stream));
254         priv->stream = thing;
255 }
256
257 TnyStreamIface *
258 modest_tny_attachment_get_stream (ModestTnyAttachment *self)
259 {
260         ModestTnyAttachmentPrivate *priv;
261         
262         priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
263         if (!priv->stream)
264                 if (priv->filename)
265                         priv->stream = make_stream_from_file(priv->filename);
266         return priv->stream;
267 }
268
269
270 void
271 modest_tny_attachment_free_list(GList *list)
272 {
273         /* this is pretty generic for a GList of GObjects */
274         GList *pos;
275         
276         for (pos = list; pos; pos = pos->next)
277                 if (pos->data)
278                         g_object_unref(pos->data);
279         g_list_free(list);
280         return;
281 }
282
283
284 ModestTnyAttachment *
285 modest_tny_attachment_new_from_mime_part(TnyMimePartIface *part)
286 {
287         return NULL;
288 }
289
290 ModestTnyAttachment *
291 modest_tny_attachment_new_from_message(const TnyMsgIface *msg)
292 {
293         return NULL;
294 }