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;
186 modest_tny_attachment_guess_mime_type (ModestTnyAttachment *self)
188 ModestTnyAttachmentPrivate *priv;
189 gchar *suffixes[] = {".jpg", ".gif", ".mp3", NULL};
190 gchar *types[] = {"image/jpeg", "image/gif", "audio/mpeg", NULL};
193 priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
197 for (pos = 0 ; suffixes[pos] ; pos++) {
198 if (g_str_has_suffix(priv->filename, suffixes[pos]))
202 g_free(priv->mime_type);
204 priv->mime_type = g_strdup(types[pos]);
206 priv->mime_type = NULL;
209 static TnyStreamIface *
210 make_stream_from_file(const gchar * filename)
214 file = open(filename, O_RDONLY);
218 return TNY_STREAM_IFACE(tny_stream_camel_new(camel_stream_fs_new_with_fd(file)));
222 modest_tny_attachment_set_stream(ModestTnyAttachment *self, TnyStreamIface *thing)
224 ModestTnyAttachmentPrivate *priv;
226 priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
228 g_object_unref(G_OBJECT(priv->stream));
229 priv->stream = thing;
233 modest_tny_attachment_get_stream (ModestTnyAttachment *self)
235 ModestTnyAttachmentPrivate *priv;
237 priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
240 priv->stream = make_stream_from_file(priv->filename);
246 modest_tny_attachment_free_list(GList *list)
250 for (pos = list; pos; pos = pos->next)
252 g_object_unref(pos->data);
258 ModestTnyAttachment *
259 modest_tny_attachment_new_from_mime_part(TnyMsgMimePartIface *part)
261 TnyStreamIface *mem_stream;
262 ModestTnyAttachment *self;
264 mem_stream = TNY_STREAM_IFACE(tny_stream_camel_new(camel_stream_mem_new()));
265 self = modest_tny_attachment_new();
266 tny_msg_mime_part_iface_decode_to_stream(part, mem_stream);
267 tny_stream_iface_reset(mem_stream);
268 modest_tny_attachment_set_stream(self, mem_stream);
269 modest_tny_attachment_set_mime_type(self,
270 tny_msg_mime_part_iface_get_content_type(part));
271 modest_tny_attachment_set_name(self,
272 tny_msg_mime_part_iface_get_filename(part));
276 ModestTnyAttachment *
277 modest_tny_attachment_new_from_message(const TnyMsgIface *msg)
279 TnyStreamIface *mem_stream, *msg_stream;
280 ModestTnyAttachment *self;
283 mem_stream = TNY_STREAM_IFACE(tny_stream_camel_new(camel_stream_mem_new()));
284 msg_stream = tny_msg_mime_part_iface_get_stream(TNY_MSG_MIME_PART_IFACE(msg));
286 tny_stream_iface_reset(msg_stream);
287 res = tny_stream_iface_write_to_stream(msg_stream, mem_stream);
288 //tny_msg_mime_part_iface_write_to_stream(TNY_MSG_MIME_PART_IFACE(msg), mem_stream);
289 printf("pong, %d\n", res);
290 tny_stream_iface_reset(msg_stream);
291 tny_stream_iface_reset(mem_stream);
292 self = modest_tny_attachment_new();
293 modest_tny_attachment_set_stream(self, mem_stream);
294 modest_tny_attachment_set_mime_type(self, "message/rfc822");
295 modest_tny_attachment_set_name(self, "original message");
300 modest_tny_attachment_new_list_from_msg(const TnyMsgIface *msg, gboolean with_body)
303 const GList *attachments = NULL;
304 TnyMsgMimePartIface *part;
305 ModestTnyAttachment *att;
309 /* TODO: make plain over html configurable */
310 part = modest_tny_msg_actions_find_body_part ((TnyMsgIface *)msg, "text/plain");
312 part = modest_tny_msg_actions_find_body_part ((TnyMsgIface *)msg, "text/html");
314 att = modest_tny_attachment_new_from_mime_part(part);
316 modest_tny_attachment_set_name(att, "original message");
317 list = g_list_append(list, att);
322 list = g_list_append(list, modest_tny_attachment_new_from_message(msg));
324 attachments = tny_msg_iface_get_parts((TnyMsgIface *)msg);
326 while (attachments) {
327 part = attachments->data;
328 if (tny_msg_mime_part_iface_is_attachment(part)) {
329 att = modest_tny_attachment_new_from_mime_part(part);
330 list = g_list_append(list, att);
332 attachments = attachments->next;