* add ModestTnyAttachment object
[modest] / src / modest-tny-attachment.c
1 /* modest-tny-attachment.c */
2
3 /* insert (c)/licensing information) */
4
5 #include "modest-tny-attachment.h"
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <fcntl.h>
9 #include <tny-stream-camel.h>
10 #include <tny-fs-stream.h>
11 #include <camel/camel.h>
12
13
14 /* include other impl specific header files */
15
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);
20
21 /* list my signals */
22 enum {
23         /* MY_SIGNAL_1, */
24         /* MY_SIGNAL_2, */
25         LAST_SIGNAL
26 };
27
28 typedef struct _ModestTnyAttachmentPrivate ModestTnyAttachmentPrivate;
29 struct _ModestTnyAttachmentPrivate {
30         gchar *name;
31         gchar *filename;
32         gchar *mime_type;
33         gchar *disposition;
34         gchar *content_id;
35         TnyStreamIface *stream;
36 };
37 #define MODEST_TNY_ATTACHMENT_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
38                                                    MODEST_TYPE_TNY_ATTACHMENT, \
39                                                    ModestTnyAttachmentPrivate))
40 /* globals */
41 static GObjectClass *parent_class = NULL;
42
43 /* uncomment the following if you have defined any signals */
44 /* static guint signals[LAST_SIGNAL] = {0}; */
45
46 GType
47 modest_tny_attachment_get_type (void)
48 {
49         static GType my_type = 0;
50         if (!my_type) {
51                 static const GTypeInfo my_info = {
52                         sizeof(ModestTnyAttachmentClass),
53                         NULL,           /* base init */
54                         NULL,           /* base finalize */
55                         (GClassInitFunc) modest_tny_attachment_class_init,
56                         NULL,           /* class finalize */
57                         NULL,           /* class data */
58                         sizeof(ModestTnyAttachment),
59                         1,              /* n_preallocs */
60                         (GInstanceInitFunc) modest_tny_attachment_init,
61                 };
62                 my_type = g_type_register_static (G_TYPE_OBJECT,
63                                                   "ModestTnyAttachment",
64                                                   &my_info, 0);
65         }
66         return my_type;
67 }
68
69 static void
70 modest_tny_attachment_class_init (ModestTnyAttachmentClass *klass)
71 {
72         GObjectClass *gobject_class;
73         gobject_class = (GObjectClass*) klass;
74
75         parent_class            = g_type_class_peek_parent (klass);
76         gobject_class->finalize = modest_tny_attachment_finalize;
77
78         g_type_class_add_private (gobject_class, sizeof(ModestTnyAttachmentPrivate));
79
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",....); */
85 /*      etc. */
86 }
87
88 static void
89 modest_tny_attachment_init (ModestTnyAttachment *obj)
90 {
91         ModestTnyAttachmentPrivate *priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(obj);
92
93         priv->name = NULL;
94         priv->filename = NULL;
95         priv->mime_type = NULL;
96         priv->disposition = NULL;
97         priv->content_id = NULL;
98         priv->stream = NULL;
99 }
100
101 static void
102 modest_tny_attachment_finalize (GObject *obj)
103 {
104         ModestTnyAttachmentPrivate *priv;
105         
106         priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(MODEST_TNY_ATTACHMENT(obj));
107         g_free(priv->name);
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));
112 }
113
114 ModestTnyAttachment *
115 modest_tny_attachment_new (void)
116 {
117         return MODEST_TNY_ATTACHMENT(G_OBJECT(g_object_new(MODEST_TYPE_TNY_ATTACHMENT, NULL)));
118 }
119
120
121 void
122 modest_tny_attachment_set_name (ModestTnyAttachment *self, const gchar * thing)
123 {
124         ModestTnyAttachmentPrivate *priv;
125         
126         priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
127         g_free(priv->name);
128         priv->name = g_strdup(thing);
129 }
130
131 const gchar *
132 modest_tny_attachment_get_name (ModestTnyAttachment *self)
133 {
134         ModestTnyAttachmentPrivate *priv;
135         
136         priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
137         return priv->name;
138 }
139
140
141 void
142 modest_tny_attachment_set_filename (ModestTnyAttachment *self, const gchar * thing)
143 {
144         ModestTnyAttachmentPrivate *priv;
145         
146         priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
147         g_free(priv->filename);
148         priv->filename = g_strdup(thing);
149 }
150
151 const gchar *
152 modest_tny_attachment_get_filename (ModestTnyAttachment *self)
153 {
154         ModestTnyAttachmentPrivate *priv;
155         
156         priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
157         return priv->filename;
158 }
159
160
161 void
162 modest_tny_attachment_set_mime_type (ModestTnyAttachment *self, const gchar * thing)
163 {
164         ModestTnyAttachmentPrivate *priv;
165         
166         priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
167         g_free(priv->mime_type);
168         priv->mime_type = g_strdup(thing);
169 }
170
171 const gchar *
172 modest_tny_attachment_get_mime_type (ModestTnyAttachment *self)
173 {
174         ModestTnyAttachmentPrivate *priv;
175         
176         priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
177         return priv->mime_type;
178 }
179
180
181
182 void
183 modest_tny_attachment_guess_mime_type (ModestTnyAttachment *self)
184 {
185         ModestTnyAttachmentPrivate *priv;
186         gchar *suffixes[] = {".jpg", ".gif", ".mp3", NULL};
187         gchar *types[]    = {"image/jpeg", "image/gif", "audio/mpeg", NULL};
188         gint pos;
189         
190         priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
191         if (!priv->filename)
192                 return;
193         
194         for (pos = 0 ; suffixes[pos] ; pos++) {
195                 if (g_str_has_suffix(priv->filename, suffixes[pos]))
196                         break;
197         }
198         
199         g_free(priv->mime_type);
200         if (suffixes[pos])
201                 priv->mime_type = types[pos];
202         else
203                 priv->mime_type = NULL;
204 }
205
206 static TnyStreamIface *
207 make_stream_from_file(const gchar * filename)
208 {
209         gint file;
210         
211         file = open(filename, O_RDONLY);
212         if (file < 0)
213                 return NULL;
214
215         return TNY_STREAM_IFACE(tny_stream_camel_new(camel_stream_fs_new_with_fd(file)));
216 }
217
218 TnyStreamIface *
219 modest_tny_attachment_get_stream (ModestTnyAttachment *self)
220 {
221         ModestTnyAttachmentPrivate *priv;
222         
223         priv = MODEST_TNY_ATTACHMENT_GET_PRIVATE(self);
224         if (!priv->stream)
225                 if (priv->filename)
226                         priv->stream = make_stream_from_file(priv->filename);
227         return priv->stream;
228 }