libpurple/purpleattachment.c

changeset 40526
c8cc1a4c4a02
child 40539
2941deda6d8d
equal deleted inserted replaced
40525:96fc115d6c36 40526:c8cc1a4c4a02
1 /*
2 * purple
3 *
4 * Purple is the legal property of its developers, whose names are too numerous
5 * to list here. Please refer to the COPYRIGHT file distributed with this
6 * source distribution.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "libpurple/purpleattachment.h"
23
24 #include <glib/gi18n-lib.h>
25
26 struct _PurpleAttachment {
27 GObject parent;
28
29 guint64 id;
30 gchar *content_type;
31
32 gchar *local_uri;
33 gchar *remote_uri;
34
35 guint64 size;
36 };
37
38 G_DEFINE_TYPE(PurpleAttachment, purple_attachment, G_TYPE_OBJECT);
39
40 enum {
41 PROP_0 = 0,
42 PROP_ID,
43 PROP_CONTENT_TYPE,
44 PROP_LOCAL_URI,
45 PROP_REMOTE_URI,
46 PROP_SIZE,
47 N_PROPERTIES,
48 };
49 static GParamSpec *properties[N_PROPERTIES];
50
51 /******************************************************************************
52 * Private Setters
53 *****************************************************************************/
54 static void
55 purple_attachment_set_content_type(PurpleAttachment *attachment,
56 const gchar *content_type)
57 {
58 if(attachment->content_type == content_type) {
59 return;
60 }
61
62 g_clear_pointer(&attachment->content_type, g_free);
63
64 attachment->content_type = g_strdup(content_type);
65
66 g_object_notify_by_pspec(G_OBJECT(attachment),
67 properties[PROP_CONTENT_TYPE]);
68 }
69
70 /******************************************************************************
71 * GObject Implementation
72 *****************************************************************************/
73 static void
74 purple_attachment_get_property(GObject *obj, guint prop_id, GValue *value, GParamSpec *pspec) {
75 PurpleAttachment *attachment = PURPLE_ATTACHMENT(obj);
76
77 switch(prop_id) {
78 case PROP_ID:
79 g_value_set_uint64(value, purple_attachment_get_id(attachment));
80 break;
81 case PROP_CONTENT_TYPE:
82 g_value_set_string(value, purple_attachment_get_content_type(attachment));
83 break;
84 case PROP_LOCAL_URI:
85 g_value_set_string(value, purple_attachment_get_local_uri(attachment));
86 break;
87 case PROP_REMOTE_URI:
88 g_value_set_string(value, purple_attachment_get_remote_uri(attachment));
89 break;
90 case PROP_SIZE:
91 g_value_set_uint64(value, purple_attachment_get_size(attachment));
92 break;
93 default:
94 G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec);
95 break;
96 }
97 }
98
99 static void
100 purple_attachment_set_property(GObject *obj, guint prop_id, const GValue *value, GParamSpec *pspec) {
101 PurpleAttachment *attachment = PURPLE_ATTACHMENT(obj);
102
103 switch(prop_id) {
104 case PROP_ID:
105 purple_attachment_set_id(attachment, g_value_get_uint64(value));
106 break;
107 case PROP_CONTENT_TYPE:
108 purple_attachment_set_content_type(attachment, g_value_get_string(value));
109 break;
110 case PROP_LOCAL_URI:
111 purple_attachment_set_local_uri(attachment, g_value_get_string(value));
112 break;
113 case PROP_REMOTE_URI:
114 purple_attachment_set_remote_uri(attachment, g_value_get_string(value));
115 break;
116 case PROP_SIZE:
117 purple_attachment_set_size(attachment, g_value_get_uint64(value));
118 break;
119 default:
120 G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec);
121 break;
122 }
123 }
124
125 static void
126 purple_attachment_finalize(GObject *obj) {
127 PurpleAttachment *attachment = PURPLE_ATTACHMENT(obj);
128
129 g_clear_pointer(&attachment->content_type, g_free);
130 g_clear_pointer(&attachment->local_uri, g_free);
131 g_clear_pointer(&attachment->remote_uri, g_free);
132
133 G_OBJECT_CLASS(purple_attachment_parent_class)->finalize(obj);
134 }
135
136 static void
137 purple_attachment_init(PurpleAttachment *attachment) {
138 }
139
140 static void
141 purple_attachment_class_init(PurpleAttachmentClass *klass) {
142 GObjectClass *obj_class = G_OBJECT_CLASS(klass);
143
144 obj_class->get_property = purple_attachment_get_property;
145 obj_class->set_property = purple_attachment_set_property;
146 obj_class->finalize = purple_attachment_finalize;
147
148 /* add our properties */
149 properties[PROP_ID] = g_param_spec_uint64(
150 "id", "id", "The identifier of the attachment",
151 0, G_MAXUINT64, 0,
152 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS
153 );
154
155 properties[PROP_CONTENT_TYPE] = g_param_spec_string(
156 "content-type", "content-type", "The content type of the attachment",
157 "application/octet-stream",
158 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS
159 );
160
161 properties[PROP_LOCAL_URI] = g_param_spec_string(
162 "local-uri", "local-uri", "The local URI of the attachment",
163 NULL,
164 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS
165 );
166
167 properties[PROP_REMOTE_URI] = g_param_spec_string(
168 "remote-uri", "remote-uri", "The remote URI of the attachment",
169 NULL,
170 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS
171 );
172
173 properties[PROP_SIZE] = g_param_spec_uint64(
174 "size", "size", "The file size of the attachment in bytes",
175 0, G_MAXUINT64, 0,
176 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS
177 );
178
179 g_object_class_install_properties(obj_class, N_PROPERTIES, properties);
180 }
181
182 /******************************************************************************
183 * Public API
184 *****************************************************************************/
185
186 PurpleAttachment *
187 purple_attachment_new(guint64 id, const gchar *content_type) {
188 g_return_val_if_fail(content_type != NULL, NULL);
189
190 return PURPLE_ATTACHMENT(g_object_new(
191 PURPLE_TYPE_ATTACHMENT,
192 "id", id,
193 "content-type", content_type,
194 NULL
195 ));
196 }
197
198 guint64
199 purple_attachment_get_id(PurpleAttachment *attachment) {
200 g_return_val_if_fail(PURPLE_IS_ATTACHMENT(attachment), 0);
201
202 return attachment->id;
203 }
204
205 guint64 *
206 purple_attachment_get_hash_key(PurpleAttachment *attachment) {
207 g_return_val_if_fail(PURPLE_IS_ATTACHMENT(attachment), NULL);
208
209 return &attachment->id;
210 }
211
212 void
213 purple_attachment_set_id(PurpleAttachment *attachment, guint64 id) {
214 g_return_if_fail(PURPLE_IS_ATTACHMENT(attachment));
215
216 if(attachment->id == id) {
217 return;
218 }
219
220 attachment->id = id;
221
222 g_object_notify_by_pspec(G_OBJECT(attachment), properties[PROP_ID]);
223 }
224
225 const gchar *
226 purple_attachment_get_content_type(PurpleAttachment *attachment) {
227 g_return_val_if_fail(PURPLE_IS_ATTACHMENT(attachment), NULL);
228
229 return attachment->content_type;
230 }
231
232 const gchar *
233 purple_attachment_get_local_uri(PurpleAttachment *attachment) {
234 g_return_val_if_fail(PURPLE_IS_ATTACHMENT(attachment), NULL);
235
236 return attachment->local_uri;
237 }
238
239 void
240 purple_attachment_set_local_uri(PurpleAttachment *attachment,
241 const gchar *local_uri)
242 {
243 g_return_if_fail(PURPLE_IS_ATTACHMENT(attachment));
244
245 if(attachment->local_uri == local_uri) {
246 return;
247 }
248
249 g_free(attachment->local_uri);
250
251 if(local_uri != NULL) {
252 gchar *scheme = g_uri_parse_scheme(local_uri);
253 if(scheme == NULL) {
254 attachment->local_uri = g_filename_to_uri(local_uri, NULL, NULL);
255 } else {
256 g_free(scheme);
257 attachment->local_uri = g_strdup(local_uri);
258 }
259 } else {
260 attachment->local_uri = NULL;
261 }
262
263 g_object_notify_by_pspec(G_OBJECT(attachment), properties[PROP_LOCAL_URI]);
264 }
265
266 const gchar *
267 purple_attachment_get_remote_uri(PurpleAttachment *attachment) {
268 g_return_val_if_fail(PURPLE_IS_ATTACHMENT(attachment), NULL);
269
270 return attachment->remote_uri;
271 }
272
273 void
274 purple_attachment_set_remote_uri(PurpleAttachment *attachment,
275 const gchar *remote_uri)
276 {
277 g_return_if_fail(PURPLE_IS_ATTACHMENT(attachment));
278
279 if(attachment->remote_uri == remote_uri) {
280 return;
281 }
282
283 g_free(attachment->remote_uri);
284 attachment->remote_uri = g_strdup(remote_uri);
285
286 g_object_notify_by_pspec(G_OBJECT(attachment), properties[PROP_REMOTE_URI]);
287 }
288
289 guint64
290 purple_attachment_get_size(PurpleAttachment *attachment) {
291 g_return_val_if_fail(PURPLE_IS_ATTACHMENT(attachment), 0);
292
293 return attachment->size;
294 }
295
296 void
297 purple_attachment_set_size(PurpleAttachment *attachment, guint64 size) {
298 g_return_if_fail(PURPLE_IS_ATTACHMENT(attachment));
299
300 attachment->size = size;
301
302 g_object_notify_by_pspec(G_OBJECT(attachment), properties[PROP_SIZE]);
303 }
304
305 gchar *
306 purple_attachment_get_filename(PurpleAttachment *attachment) {
307 g_return_val_if_fail(PURPLE_IS_ATTACHMENT(attachment), NULL);
308
309 if(attachment->remote_uri != NULL && attachment->remote_uri[0] != '\0') {
310 return g_path_get_basename(attachment->remote_uri);
311 }
312
313 if(attachment->local_uri != NULL && attachment->local_uri[0] != '\0') {
314 return g_path_get_basename(attachment->local_uri);
315 }
316
317 return g_strdup("unknown");
318 }

mercurial