summaryrefslogtreecommitdiff
path: root/libpurple/purpleattachment.c
blob: 4dc380f046af74767dce2d97af980f8f2e63e2f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
 * purple
 *
 * Purple is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this library; if not, see <https://www.gnu.org/licenses/>.
 */

#include "libpurple/purpleattachment.h"

#include <glib/gi18n-lib.h>

struct _PurpleAttachment {
	GObject parent;

	guint64 id;
	gchar *content_type;

	gchar *local_uri;
	gchar *remote_uri;

	guint64 size;
};

G_DEFINE_TYPE(PurpleAttachment, purple_attachment, G_TYPE_OBJECT);

enum {
	PROP_0 = 0,
	PROP_ID,
	PROP_CONTENT_TYPE,
	PROP_LOCAL_URI,
	PROP_REMOTE_URI,
	PROP_SIZE,
	N_PROPERTIES,
};
static GParamSpec *properties[N_PROPERTIES];

/******************************************************************************
 * Private Setters
 *****************************************************************************/
static void
purple_attachment_set_content_type(PurpleAttachment *attachment,
                                    const gchar *content_type)
{
	if(attachment->content_type == content_type) {
		return;
	}

	g_clear_pointer(&attachment->content_type, g_free);

	attachment->content_type = g_strdup(content_type);

	g_object_notify_by_pspec(G_OBJECT(attachment),
	                         properties[PROP_CONTENT_TYPE]);
}

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
static void
purple_attachment_get_property(GObject *obj, guint prop_id, GValue *value, GParamSpec *pspec) {
	PurpleAttachment *attachment = PURPLE_ATTACHMENT(obj);

	switch(prop_id) {
		case PROP_ID:
			g_value_set_uint64(value, purple_attachment_get_id(attachment));
			break;
		case PROP_CONTENT_TYPE:
			g_value_set_string(value, purple_attachment_get_content_type(attachment));
			break;
		case PROP_LOCAL_URI:
			g_value_set_string(value, purple_attachment_get_local_uri(attachment));
			break;
		case PROP_REMOTE_URI:
			g_value_set_string(value, purple_attachment_get_remote_uri(attachment));
			break;
		case PROP_SIZE:
			g_value_set_uint64(value, purple_attachment_get_size(attachment));
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec);
			break;
	}
}

static void
purple_attachment_set_property(GObject *obj, guint prop_id, const GValue *value, GParamSpec *pspec) {
	PurpleAttachment *attachment = PURPLE_ATTACHMENT(obj);

	switch(prop_id) {
		case PROP_ID:
			purple_attachment_set_id(attachment, g_value_get_uint64(value));
			break;
		case PROP_CONTENT_TYPE:
			purple_attachment_set_content_type(attachment, g_value_get_string(value));
			break;
		case PROP_LOCAL_URI:
			purple_attachment_set_local_uri(attachment, g_value_get_string(value));
			break;
		case PROP_REMOTE_URI:
			purple_attachment_set_remote_uri(attachment, g_value_get_string(value));
			break;
		case PROP_SIZE:
			purple_attachment_set_size(attachment, g_value_get_uint64(value));
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec);
			break;
	}
}

static void
purple_attachment_finalize(GObject *obj) {
	PurpleAttachment *attachment = PURPLE_ATTACHMENT(obj);

	g_clear_pointer(&attachment->content_type, g_free);
	g_clear_pointer(&attachment->local_uri, g_free);
	g_clear_pointer(&attachment->remote_uri, g_free);

	G_OBJECT_CLASS(purple_attachment_parent_class)->finalize(obj);
}

static void
purple_attachment_init(G_GNUC_UNUSED PurpleAttachment *attachment) {
}

static void
purple_attachment_class_init(PurpleAttachmentClass *klass) {
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);

	obj_class->get_property = purple_attachment_get_property;
	obj_class->set_property = purple_attachment_set_property;
	obj_class->finalize = purple_attachment_finalize;

	/* add our properties */
	properties[PROP_ID] = g_param_spec_uint64(
		"id", "id", "The identifier of the attachment",
		0, G_MAXUINT64, 0,
		G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS
	);

	properties[PROP_CONTENT_TYPE] = g_param_spec_string(
		"content-type", "content-type", "The content type of the attachment",
		"application/octet-stream",
		G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS
	);

	properties[PROP_LOCAL_URI] = g_param_spec_string(
		"local-uri", "local-uri", "The local URI of the attachment",
		NULL,
		G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS
	);

	properties[PROP_REMOTE_URI] = g_param_spec_string(
		"remote-uri", "remote-uri", "The remote URI of the attachment",
		NULL,
		G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS
	);

	properties[PROP_SIZE] = g_param_spec_uint64(
		"size", "size", "The file size of the attachment in bytes",
		0, G_MAXUINT64, 0,
		G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS
	);

	g_object_class_install_properties(obj_class, N_PROPERTIES, properties);
}

/******************************************************************************
 * Public API
 *****************************************************************************/

PurpleAttachment *
purple_attachment_new(guint64 id, const gchar *content_type) {
	g_return_val_if_fail(content_type != NULL, NULL);

	return PURPLE_ATTACHMENT(g_object_new(
		PURPLE_TYPE_ATTACHMENT,
		"id", id,
		"content-type", content_type,
		NULL
	));
}

guint64
purple_attachment_get_id(PurpleAttachment *attachment) {
	g_return_val_if_fail(PURPLE_IS_ATTACHMENT(attachment), 0);

	return attachment->id;
}

guint64 *
purple_attachment_get_hash_key(PurpleAttachment *attachment) {
	g_return_val_if_fail(PURPLE_IS_ATTACHMENT(attachment), NULL);

	return &attachment->id;
}

void
purple_attachment_set_id(PurpleAttachment *attachment, guint64 id) {
	g_return_if_fail(PURPLE_IS_ATTACHMENT(attachment));

	if(attachment->id == id) {
		return;
	}

	attachment->id = id;

	g_object_notify_by_pspec(G_OBJECT(attachment), properties[PROP_ID]);
}

const gchar *
purple_attachment_get_content_type(PurpleAttachment *attachment) {
	g_return_val_if_fail(PURPLE_IS_ATTACHMENT(attachment), NULL);

	return attachment->content_type;
}

const gchar *
purple_attachment_get_local_uri(PurpleAttachment *attachment) {
	g_return_val_if_fail(PURPLE_IS_ATTACHMENT(attachment), NULL);

	return attachment->local_uri;
}

void
purple_attachment_set_local_uri(PurpleAttachment *attachment,
                                 const gchar *local_uri)
{
	g_return_if_fail(PURPLE_IS_ATTACHMENT(attachment));

	if(attachment->local_uri == local_uri) {
		return;
	}

	g_free(attachment->local_uri);

	if(local_uri != NULL) {
		gchar *scheme = g_uri_parse_scheme(local_uri);
		if(scheme == NULL) {
			attachment->local_uri = g_filename_to_uri(local_uri, NULL, NULL);
		} else {
			g_free(scheme);
			attachment->local_uri = g_strdup(local_uri);
		}
	} else {
		attachment->local_uri = NULL;
	}

	g_object_notify_by_pspec(G_OBJECT(attachment), properties[PROP_LOCAL_URI]);
}

const gchar *
purple_attachment_get_remote_uri(PurpleAttachment *attachment) {
	g_return_val_if_fail(PURPLE_IS_ATTACHMENT(attachment), NULL);

	return attachment->remote_uri;
}

void
purple_attachment_set_remote_uri(PurpleAttachment *attachment,
                                  const gchar *remote_uri)
{
	g_return_if_fail(PURPLE_IS_ATTACHMENT(attachment));

	if(attachment->remote_uri == remote_uri) {
		return;
	}

	g_free(attachment->remote_uri);
	attachment->remote_uri = g_strdup(remote_uri);

	g_object_notify_by_pspec(G_OBJECT(attachment), properties[PROP_REMOTE_URI]);
}

guint64
purple_attachment_get_size(PurpleAttachment *attachment) {
	g_return_val_if_fail(PURPLE_IS_ATTACHMENT(attachment), 0);

	return attachment->size;
}

void
purple_attachment_set_size(PurpleAttachment *attachment, guint64 size) {
	g_return_if_fail(PURPLE_IS_ATTACHMENT(attachment));

	attachment->size = size;

	g_object_notify_by_pspec(G_OBJECT(attachment), properties[PROP_SIZE]);
}

gchar *
purple_attachment_get_filename(PurpleAttachment *attachment) {
	g_return_val_if_fail(PURPLE_IS_ATTACHMENT(attachment), NULL);

	if(attachment->remote_uri != NULL && attachment->remote_uri[0] != '\0') {
		return g_path_get_basename(attachment->remote_uri);
	}

	if(attachment->local_uri != NULL && attachment->local_uri[0] != '\0') {
		return g_path_get_basename(attachment->local_uri);
	}

	return g_strdup("unknown");
}