1 /* modest-tny-attachment.c */
3 /* insert (c)/licensing information) */
5 #include "modest-tny-attachment.h"
6 #include "modest-tny-msg-actions.h"
8 #include <tny-stream-camel.h>
9 #include <tny-fs-stream.h>
10 #include <camel/camel.h>
13 /* include other impl specific header files */
15 /* 'private'/'protected' functions */
16 static void modest_tny_attachment_class_init (ModestTnyAttachmentClass *klass);
17 static void modest_tny_attachment_init (ModestTnyAttachment *obj);
18 static void modest_tny_attachment_finalize (GObject *obj);
27 typedef struct _ModestTnyAttachmentPrivate ModestTnyAttachmentPrivate;
28 struct _ModestTnyAttachmentPrivate {
34 TnyStreamIface *stream;
36 #define MODEST_TNY_ATTACHMENT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
37 MODEST_TYPE_TNY_ATTACHMENT, \
38 ModestTnyAttachmentPrivate))
40 static GObjectClass *parent_class = NULL;
42 /* uncomment the following if you have defined any signals */
43 /* static guint signals[LAST_SIGNAL] = {0}; */
46 modest_tny_attachment_get_type (void)
48 static GType my_type = 0;
50 static const GTypeInfo my_info = {
51 sizeof(ModestTnyAttachmentClass),
53 NULL, /* base finalize */
54 (GClassInitFunc) modest_tny_attachment_class_init,
55 NULL, /* class finalize */
56 NULL, /* class data */
57 sizeof(ModestTnyAttachment),
59 (GInstanceInitFunc) modest_tny_attachment_init,
61 my_type = g_type_register_static (G_TYPE_OBJECT,
62 "ModestTnyAttachment",
69 modest_tny_attachment_class_init (ModestTnyAttachmentClass *klass)
71 GObjectClass *gobject_class;
72 gobject_class = (GObjectClass*) klass;
74 parent_class = g_type_class_peek_parent (klass);
75 gobject_class->finalize = modest_tny_attachment_finalize;
77 g_type_class_add_private (gobject_class, sizeof(ModestTnyAttachmentPrivate));
79 /* signal definitions go here, e.g.: */
80 /* signals[MY_SIGNAL_1] = */
81 /* g_signal_new ("my_signal_1",....); */
82 /* signals[MY_SIGNAL_2] = */
83 /* g_signal_new ("my_signal_2",....); */
88 modest_tny_attachment_init (ModestTnyAttachment *obj)
90 ModestTnyAttachmentPrivate *priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(obj);
93 priv->filename = NULL;
94 priv->mime_type = NULL;
95 priv->disposition = NULL;
96 priv->content_id = NULL;
101 modest_tny_attachment_finalize (GObject *obj)
103 ModestTnyAttachmentPrivate *priv;
105 priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(MODEST_TNY_ATTACHMENT(obj));
107 g_free(priv->mime_type);
108 g_free(priv->disposition);
109 g_free(priv->content_id);
111 g_object_unref(G_OBJECT(priv->stream));
114 ModestTnyAttachment *
115 modest_tny_attachment_new (void)
117 return MODEST_TNY_ATTACHMENT(G_OBJECT(g_object_new(MODEST_TYPE_TNY_ATTACHMENT, NULL)));
122 modest_tny_attachment_set_name (ModestTnyAttachment *self, const gchar * thing)
124 ModestTnyAttachmentPrivate *priv;
126 priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
128 priv->name = g_strdup(thing);
132 modest_tny_attachment_get_name (ModestTnyAttachment *self)
134 ModestTnyAttachmentPrivate *priv;
136 priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
139 priv->name = g_path_get_basename(priv->filename);
145 modest_tny_attachment_set_filename (ModestTnyAttachment *self, const gchar * thing)
147 ModestTnyAttachmentPrivate *priv;
149 priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
150 g_free(priv->filename);
151 priv->filename = g_strdup(thing);
155 modest_tny_attachment_get_filename (ModestTnyAttachment *self)
157 ModestTnyAttachmentPrivate *priv;
159 priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
160 return priv->filename;
165 modest_tny_attachment_set_mime_type (ModestTnyAttachment *self, const gchar * thing)
167 ModestTnyAttachmentPrivate *priv;
169 priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
170 g_free(priv->mime_type);
171 priv->mime_type = g_strdup(thing);
175 modest_tny_attachment_get_mime_type (ModestTnyAttachment *self)
177 ModestTnyAttachmentPrivate *priv;
179 priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
180 return priv->mime_type;
185 modest_tny_attachment_guess_mime_type (ModestTnyAttachment *self)
187 ModestTnyAttachmentPrivate *priv;
188 gchar *suffixes[] = {".jpg", ".gif", ".png", ".mp3", ".ogg", /* default: */ "", NULL};
189 gchar *types[] = {"image/jpeg", "image/gif", "image/png", "audio/mpeg", "application/ogg", "application/octet-stream", NULL};
193 priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
197 low_fn = g_utf8_strdown(priv->filename, -1);
198 for (pos = 0 ; suffixes[pos] ; pos++) {
199 if (g_str_has_suffix(low_fn, suffixes[pos]))
204 g_free(priv->mime_type);
206 priv->mime_type = g_strdup(types[pos]);
208 priv->mime_type = NULL;
211 static TnyStreamIface *
212 make_stream_from_file(const gchar * filename)
216 file = open(filename, O_RDONLY);
220 return TNY_STREAM_IFACE(tny_stream_camel_new(camel_stream_fs_new_with_fd(file)));
224 modest_tny_attachment_set_stream(ModestTnyAttachment *self, TnyStreamIface *thing)
226 ModestTnyAttachmentPrivate *priv;
228 priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
230 g_object_unref(G_OBJECT(priv->stream));
231 priv->stream = thing;
235 modest_tny_attachment_get_stream (ModestTnyAttachment *self)
237 ModestTnyAttachmentPrivate *priv;
239 priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
242 priv->stream = make_stream_from_file(priv->filename);
248 modest_tny_attachment_free_list(GList *list)
250 /* this is pretty generic for a GList of GObjects */
253 for (pos = list; pos; pos = pos->next)
255 g_object_unref(pos->data);
261 ModestTnyAttachment *
262 modest_tny_attachment_new_from_mime_part(TnyMsgMimePartIface *part)
264 TnyStreamIface *mem_stream;
265 ModestTnyAttachment *self;
267 mem_stream = TNY_STREAM_IFACE(tny_stream_camel_new(camel_stream_mem_new()));
268 self = modest_tny_attachment_new();
269 tny_msg_mime_part_iface_decode_to_stream(part, mem_stream);
270 tny_stream_iface_reset(mem_stream);
271 modest_tny_attachment_set_stream(self, mem_stream);
272 modest_tny_attachment_set_mime_type(self,
273 tny_msg_mime_part_iface_get_content_type(part));
274 modest_tny_attachment_set_name(self,
275 tny_msg_mime_part_iface_get_filename(part));
279 ModestTnyAttachment *
280 modest_tny_attachment_new_from_message(const TnyMsgIface *msg)
282 TnyStreamIface *mem_stream, *msg_stream;
283 ModestTnyAttachment *self;
286 mem_stream = TNY_STREAM_IFACE(tny_stream_camel_new(camel_stream_mem_new()));
287 msg_stream = tny_msg_mime_part_iface_get_stream(TNY_MSG_MIME_PART_IFACE(msg));
289 tny_stream_iface_reset(msg_stream);
290 res = tny_stream_iface_write_to_stream(msg_stream, mem_stream);
291 //tny_msg_mime_part_iface_write_to_stream(TNY_MSG_MIME_PART_IFACE(msg), mem_stream);
292 printf("pong, %d\n", res);
293 tny_stream_iface_reset(msg_stream);
294 tny_stream_iface_reset(mem_stream);
295 self = modest_tny_attachment_new();
296 modest_tny_attachment_set_stream(self, mem_stream);
297 modest_tny_attachment_set_mime_type(self, "message/rfc822");
298 modest_tny_attachment_set_name(self, "original message");
303 modest_tny_attachment_new_list_from_msg(const TnyMsgIface *msg, gboolean with_body)
306 const GList *attachments = NULL;
307 TnyMsgMimePartIface *part;
308 ModestTnyAttachment *att;
312 /* TODO: make plain over html configurable */
313 part = modest_tny_msg_actions_find_body_part ((TnyMsgIface *)msg, "text/plain");
315 part = modest_tny_msg_actions_find_body_part ((TnyMsgIface *)msg, "text/html");
317 att = modest_tny_attachment_new_from_mime_part(part);
319 modest_tny_attachment_set_name(att, "original message");
320 list = g_list_append(list, att);
325 list = g_list_append(list, modest_tny_attachment_new_from_message(msg));
327 attachments = tny_msg_iface_get_parts((TnyMsgIface *)msg);
329 while (attachments) {
330 part = attachments->data;
331 if (tny_msg_mime_part_iface_is_attachment(part)) {
332 att = modest_tny_attachment_new_from_mime_part(part);
333 list = g_list_append(list, att);
335 attachments = attachments->next;