1 /* modest-tny-attachment.c */
3 /* insert (c)/licensing information) */
5 #include "modest-tny-attachment.h"
9 #include <tny-stream-camel.h>
10 #include <tny-fs-stream.h>
11 #include <camel/camel.h>
14 /* include other impl specific header files */
16 /* 'private'/'protected' functions */
17 static void modest_tny_attachment_class_init (ModestTnyAttachmentClass *klass);
18 static void modest_tny_attachment_init (ModestTnyAttachment *obj);
19 static void modest_tny_attachment_finalize (GObject *obj);
28 typedef struct _ModestTnyAttachmentPrivate ModestTnyAttachmentPrivate;
29 struct _ModestTnyAttachmentPrivate {
35 TnyStreamIface *stream;
37 #define MODEST_TNY_ATTACHMENT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
38 MODEST_TYPE_TNY_ATTACHMENT, \
39 ModestTnyAttachmentPrivate))
41 static GObjectClass *parent_class = NULL;
43 /* uncomment the following if you have defined any signals */
44 /* static guint signals[LAST_SIGNAL] = {0}; */
47 modest_tny_attachment_get_type (void)
49 static GType my_type = 0;
51 static const GTypeInfo my_info = {
52 sizeof(ModestTnyAttachmentClass),
54 NULL, /* base finalize */
55 (GClassInitFunc) modest_tny_attachment_class_init,
56 NULL, /* class finalize */
57 NULL, /* class data */
58 sizeof(ModestTnyAttachment),
60 (GInstanceInitFunc) modest_tny_attachment_init,
62 my_type = g_type_register_static (G_TYPE_OBJECT,
63 "ModestTnyAttachment",
70 modest_tny_attachment_class_init (ModestTnyAttachmentClass *klass)
72 GObjectClass *gobject_class;
73 gobject_class = (GObjectClass*) klass;
75 parent_class = g_type_class_peek_parent (klass);
76 gobject_class->finalize = modest_tny_attachment_finalize;
78 g_type_class_add_private (gobject_class, sizeof(ModestTnyAttachmentPrivate));
80 /* signal definitions go here, e.g.: */
81 /* signals[MY_SIGNAL_1] = */
82 /* g_signal_new ("my_signal_1",....); */
83 /* signals[MY_SIGNAL_2] = */
84 /* g_signal_new ("my_signal_2",....); */
89 modest_tny_attachment_init (ModestTnyAttachment *obj)
91 ModestTnyAttachmentPrivate *priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(obj);
94 priv->filename = NULL;
95 priv->mime_type = NULL;
96 priv->disposition = NULL;
97 priv->content_id = NULL;
102 modest_tny_attachment_finalize (GObject *obj)
104 ModestTnyAttachmentPrivate *priv;
106 priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(MODEST_TNY_ATTACHMENT(obj));
108 g_free(priv->mime_type);
109 g_free(priv->disposition);
110 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);
142 modest_tny_attachment_set_filename (ModestTnyAttachment *self, const gchar * thing)
144 ModestTnyAttachmentPrivate *priv;
146 priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
147 g_free(priv->filename);
148 priv->filename = g_strdup(thing);
152 modest_tny_attachment_get_filename (ModestTnyAttachment *self)
154 ModestTnyAttachmentPrivate *priv;
156 priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
157 return priv->filename;
162 modest_tny_attachment_set_mime_type (ModestTnyAttachment *self, const gchar * thing)
164 ModestTnyAttachmentPrivate *priv;
166 priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
167 g_free(priv->mime_type);
168 priv->mime_type = g_strdup(thing);
172 modest_tny_attachment_get_mime_type (ModestTnyAttachment *self)
174 ModestTnyAttachmentPrivate *priv;
176 priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
177 return priv->mime_type;
183 modest_tny_attachment_guess_mime_type (ModestTnyAttachment *self)
185 ModestTnyAttachmentPrivate *priv;
186 gchar *suffixes[] = {".jpg", ".gif", ".mp3", NULL};
187 gchar *types[] = {"image/jpeg", "image/gif", "audio/mpeg", NULL};
190 priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
194 for (pos = 0 ; suffixes[pos] ; pos++) {
195 if (g_str_has_suffix(priv->filename, suffixes[pos]))
199 g_free(priv->mime_type);
201 priv->mime_type = types[pos];
203 priv->mime_type = NULL;
206 static TnyStreamIface *
207 make_stream_from_file(const gchar * filename)
211 file = open(filename, O_RDONLY);
215 return TNY_STREAM_IFACE(tny_stream_camel_new(camel_stream_fs_new_with_fd(file)));
219 modest_tny_attachment_get_stream (ModestTnyAttachment *self)
221 ModestTnyAttachmentPrivate *priv;
223 priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
226 priv->stream = make_stream_from_file(priv->filename);