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.
31 /* modest-tny-attachment.c */
33 #include "modest-tny-attachment.h"
34 #include "modest-tny-msg-actions.h"
36 #include <tny-stream-camel.h>
37 #include <tny-fs-stream.h>
38 #include <camel/camel.h>
41 /* include other impl specific header files */
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);
55 typedef struct _ModestTnyAttachmentPrivate ModestTnyAttachmentPrivate;
56 struct _ModestTnyAttachmentPrivate {
62 TnyStreamIface *stream;
64 #define MODEST_TNY_ATTACHMENT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
65 MODEST_TYPE_TNY_ATTACHMENT, \
66 ModestTnyAttachmentPrivate))
68 static GObjectClass *parent_class = NULL;
70 /* uncomment the following if you have defined any signals */
71 /* static guint signals[LAST_SIGNAL] = {0}; */
74 modest_tny_attachment_get_type (void)
76 static GType my_type = 0;
78 static const GTypeInfo my_info = {
79 sizeof(ModestTnyAttachmentClass),
81 NULL, /* base finalize */
82 (GClassInitFunc) modest_tny_attachment_class_init,
83 NULL, /* class finalize */
84 NULL, /* class data */
85 sizeof(ModestTnyAttachment),
87 (GInstanceInitFunc) modest_tny_attachment_init,
90 my_type = g_type_register_static (G_TYPE_OBJECT,
91 "ModestTnyAttachment",
98 modest_tny_attachment_class_init (ModestTnyAttachmentClass *klass)
100 GObjectClass *gobject_class;
101 gobject_class = (GObjectClass*) klass;
103 parent_class = g_type_class_peek_parent (klass);
104 gobject_class->finalize = modest_tny_attachment_finalize;
106 g_type_class_add_private (gobject_class, sizeof(ModestTnyAttachmentPrivate));
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",....); */
117 modest_tny_attachment_init (ModestTnyAttachment *obj)
119 ModestTnyAttachmentPrivate *priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(obj);
122 priv->filename = NULL;
123 priv->mime_type = NULL;
124 priv->disposition = NULL;
125 priv->content_id = NULL;
130 modest_tny_attachment_finalize (GObject *obj)
132 ModestTnyAttachmentPrivate *priv;
134 priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(MODEST_TNY_ATTACHMENT(obj));
136 g_free(priv->mime_type);
137 g_free(priv->disposition);
138 g_free(priv->content_id);
140 g_object_unref(G_OBJECT(priv->stream));
143 ModestTnyAttachment *
144 modest_tny_attachment_new (void)
146 return MODEST_TNY_ATTACHMENT(G_OBJECT(g_object_new(MODEST_TYPE_TNY_ATTACHMENT, NULL)));
151 modest_tny_attachment_set_name (ModestTnyAttachment *self, const gchar * thing)
153 ModestTnyAttachmentPrivate *priv;
155 priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
157 priv->name = g_strdup(thing);
161 modest_tny_attachment_get_name (ModestTnyAttachment *self)
163 ModestTnyAttachmentPrivate *priv;
165 priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
168 priv->name = g_path_get_basename(priv->filename);
174 modest_tny_attachment_set_filename (ModestTnyAttachment *self, const gchar * thing)
176 ModestTnyAttachmentPrivate *priv;
178 priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
179 g_free(priv->filename);
180 priv->filename = g_strdup(thing);
184 modest_tny_attachment_get_filename (ModestTnyAttachment *self)
186 ModestTnyAttachmentPrivate *priv;
188 priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
189 return priv->filename;
194 modest_tny_attachment_set_mime_type (ModestTnyAttachment *self, const gchar * thing)
196 ModestTnyAttachmentPrivate *priv;
198 priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
199 g_free(priv->mime_type);
200 priv->mime_type = g_strdup(thing);
204 modest_tny_attachment_get_mime_type (ModestTnyAttachment *self)
206 ModestTnyAttachmentPrivate *priv;
208 priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
209 return priv->mime_type;
214 modest_tny_attachment_guess_mime_type (ModestTnyAttachment *self)
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};
222 priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
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]))
233 g_free(priv->mime_type);
235 priv->mime_type = g_strdup(types[pos]);
237 priv->mime_type = NULL;
240 static TnyStreamIface *
241 make_stream_from_file(const gchar * filename)
245 file = open(filename, O_RDONLY);
249 return TNY_STREAM_IFACE(tny_stream_camel_new(camel_stream_fs_new_with_fd(file)));
253 modest_tny_attachment_set_stream(ModestTnyAttachment *self, TnyStreamIface *thing)
255 ModestTnyAttachmentPrivate *priv;
257 priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
259 g_object_unref(G_OBJECT(priv->stream));
260 priv->stream = thing;
264 modest_tny_attachment_get_stream (ModestTnyAttachment *self)
266 ModestTnyAttachmentPrivate *priv;
268 priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
271 priv->stream = make_stream_from_file(priv->filename);
277 modest_tny_attachment_free_list(GList *list)
279 /* this is pretty generic for a GList of GObjects */
282 for (pos = list; pos; pos = pos->next)
284 g_object_unref(pos->data);
290 ModestTnyAttachment *
291 modest_tny_attachment_new_from_mime_part(TnyMimePartIface *part)
293 TnyStreamIface *mem_stream;
294 ModestTnyAttachment *self;
296 mem_stream = TNY_STREAM_IFACE(tny_stream_camel_new(camel_stream_mem_new()));
297 self = modest_tny_attachment_new();
298 tny_mime_part_iface_decode_to_stream(part, mem_stream);
299 tny_stream_iface_reset(mem_stream);
300 modest_tny_attachment_set_stream(self, mem_stream);
301 modest_tny_attachment_set_mime_type(self,
302 tny_mime_part_iface_get_content_type(part));
303 modest_tny_attachment_set_name(self,
304 tny_mime_part_iface_get_filename(part));
308 ModestTnyAttachment *
309 modest_tny_attachment_new_from_message(const TnyMsgIface *msg)
311 TnyStreamIface *mem_stream, *msg_stream;
312 ModestTnyAttachment *self;
315 mem_stream = TNY_STREAM_IFACE(tny_stream_camel_new(camel_stream_mem_new()));
316 msg_stream = tny_mime_part_iface_get_stream(TNY_MIME_PART_IFACE(msg));
318 tny_stream_iface_reset(msg_stream);
319 res = tny_stream_iface_write_to_stream(msg_stream, mem_stream);
320 //tny_msg_mime_part_iface_write_to_stream(TNY_MSG_MIME_PART_IFACE(msg), mem_stream);
321 printf("pong, %d\n", res);
322 tny_stream_iface_reset(msg_stream);
323 tny_stream_iface_reset(mem_stream);
324 self = modest_tny_attachment_new();
325 modest_tny_attachment_set_stream(self, mem_stream);
326 modest_tny_attachment_set_mime_type(self, "message/rfc822");
327 modest_tny_attachment_set_name(self, "original message");