summaryrefslogtreecommitdiff
path: root/libpurple/purpleavatar.c
blob: bf521975079bbc0fbeb645fc3dfc132d164a02e6 (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
/*
 * Purple - Internet Messaging Library
 * Copyright (C) Pidgin Developers <devel@pidgin.im>
 *
 * 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 program; if not, see <https://www.gnu.org/licenses/>.
 */

#include "purpleavatar.h"

struct _PurpleAvatar {
	GObject parent;

	char *filename;

	GdkPixbuf *pixbuf;

	gboolean animated;
	GdkPixbufAnimation *animation;

	PurpleTags *tags;
};

enum {
	PROP_0,
	PROP_FILENAME,
	PROP_PIXBUF,
	PROP_ANIMATED,
	PROP_ANIMATION,
	PROP_TAGS,
	N_PROPERTIES,
};
static GParamSpec *properties[N_PROPERTIES] = {NULL, };

G_DEFINE_TYPE(PurpleAvatar, purple_avatar, G_TYPE_OBJECT)

/******************************************************************************
 * Helpers
 *****************************************************************************/
static void
purple_avatar_set_filename(PurpleAvatar *avatar, const char *filename) {
	g_return_if_fail(PURPLE_IS_AVATAR(avatar));

	g_free(avatar->filename);
	avatar->filename = g_strdup(filename);

	g_object_notify_by_pspec(G_OBJECT(avatar), properties[PROP_FILENAME]);
}

static PurpleAvatar *
purple_avatar_new_common(const char *filename, GdkPixbufAnimation *animation) {
	PurpleAvatar *avatar = NULL;

	avatar = g_object_new(PURPLE_TYPE_AVATAR, "filename", filename, NULL);

	if(gdk_pixbuf_animation_is_static_image(animation)) {
		/* If we loaded a static image, grab the static image and set it to our
		 * pixbuf member, clear the animation, and return the new avatar.
		 */

		avatar->pixbuf = gdk_pixbuf_animation_get_static_image(animation);
		g_object_ref(avatar->pixbuf);

		g_clear_object(&animation);
	} else {
		/* If we did load an animation, set the appropriate properties and
		 * return the avatar.
		 */
		avatar->animated = TRUE;
		avatar->animation = animation;
	}

	return avatar;
}

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
static void
purple_avatar_get_property(GObject *obj, guint param_id, GValue *value,
                           GParamSpec *pspec)
{
	PurpleAvatar *avatar = PURPLE_AVATAR(obj);

	switch(param_id) {
		case PROP_FILENAME:
			g_value_set_string(value, purple_avatar_get_filename(avatar));
			break;
		case PROP_PIXBUF:
			g_value_set_object(value, purple_avatar_get_pixbuf(avatar));
			break;
		case PROP_ANIMATED:
			g_value_set_boolean(value, purple_avatar_get_animated(avatar));
			break;
		case PROP_ANIMATION:
			g_value_set_object(value, purple_avatar_get_animation(avatar));
			break;
		case PROP_TAGS:
			g_value_set_object(value, purple_avatar_get_tags(avatar));
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
			break;
	}
}

static void
purple_avatar_set_property(GObject *obj, guint param_id, const GValue *value,
                           GParamSpec *pspec)
{
	PurpleAvatar *avatar = PURPLE_AVATAR(obj);

	switch(param_id) {
		case PROP_FILENAME:
			purple_avatar_set_filename(avatar, g_value_get_string(value));
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
			break;
	}
}

static void
purple_avatar_finalize(GObject *obj) {
	PurpleAvatar *avatar = PURPLE_AVATAR(obj);

	g_clear_pointer(&avatar->filename, g_free);
	g_clear_object(&avatar->pixbuf);
	g_clear_object(&avatar->animation);
	g_clear_object(&avatar->tags);

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

static void
purple_avatar_init(PurpleAvatar *avatar) {
	avatar->tags = purple_tags_new();
}

static void
purple_avatar_class_init(PurpleAvatarClass *klass) {
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);

	obj_class->finalize = purple_avatar_finalize;
	obj_class->get_property = purple_avatar_get_property;
	obj_class->set_property = purple_avatar_set_property;

	/**
	 * PurpleAvatar:filename:
	 *
	 * The filename that this avatar was created from.
	 *
	 * Since: 3.0.0
	 */
	properties[PROP_FILENAME] = g_param_spec_string(
		"filename", "filename",
		"The filename to save/load the avatar from.",
		NULL,
		G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);

	/**
	 * PurpleAvatar:pixbuf:
	 *
	 * The [class@GdkPixbuf.Pixbuf] of the avatar. If
	 * [property@Purple.Avatar:animated] is %TRUE, this will be a static frame
	 * from the animation.
	 *
	 * Since: 3.0.0
	 */
	properties[PROP_PIXBUF] = g_param_spec_object(
		"pixbuf", "pixbuf",
		"The pixbuf of the avatar.",
		GDK_TYPE_PIXBUF,
		G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

	/**
	 * PurpleAvatar:animated:
	 *
	 * Whether or not this avatar is animated.
	 *
	 * Since: 3.0.0
	 */
	properties[PROP_ANIMATED] = g_param_spec_boolean(
		"animated", "animated",
		"Whether or not the avatar is animated.",
		FALSE,
		G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

	/**
	 * PurpleAvatar:animation:
	 *
	 * The [class@GdkPixbuf.PixbufAnimation] if
	 * [property@Purple.Avatar:animated] is %TRUE.
	 *
	 * Since: 3.0.0
	 */
	properties[PROP_ANIMATION] = g_param_spec_object(
		"animation", "animation",
		"The animation of the avatar.",
		GDK_TYPE_PIXBUF_ANIMATION,
		G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

	/**
	 * PurpleAvatar:tags:
	 *
	 * The [class@Purple.Tags] for the avatar.
	 *
	 * Since: 3.0.0
	 */
	properties[PROP_TAGS] = g_param_spec_object(
		"tags", "tags",
		"The tags for the avatar.",
		PURPLE_TYPE_TAGS,
		G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

	g_object_class_install_properties(obj_class, N_PROPERTIES, properties);
}

/******************************************************************************
 * Public API
 *****************************************************************************/
PurpleAvatar *
purple_avatar_new_from_filename(const char *filename, GError **error) {
	GdkPixbufAnimation *animation = NULL;
	GError *local_error = NULL;

	g_return_val_if_fail(filename != NULL, NULL);

	animation = gdk_pixbuf_animation_new_from_file(filename, &local_error);
	if(!GDK_IS_PIXBUF_ANIMATION(animation) || local_error != NULL) {
		g_clear_object(&animation);

		g_propagate_error(error, local_error);

		return NULL;
	}

	return purple_avatar_new_common(filename, animation);
}

PurpleAvatar *
purple_avatar_new_from_resource(const char *resource_path, GError **error) {
	GdkPixbufAnimation *animation = NULL;
	GError *local_error = NULL;

	g_return_val_if_fail(resource_path != NULL, NULL);

	animation = gdk_pixbuf_animation_new_from_resource(resource_path,
	                                                   &local_error);
	if(!GDK_IS_PIXBUF_ANIMATION(animation) || local_error != NULL) {
		g_clear_object(&animation);

		g_propagate_error(error, local_error);

		return NULL;
	}

	return purple_avatar_new_common(NULL, animation);
}

const char *
purple_avatar_get_filename(PurpleAvatar *avatar) {
	g_return_val_if_fail(PURPLE_IS_AVATAR(avatar), NULL);

	return avatar->filename;
}

GdkPixbuf *
purple_avatar_get_pixbuf(PurpleAvatar *avatar) {
	g_return_val_if_fail(PURPLE_IS_AVATAR(avatar), NULL);

	if(avatar->animated) {
		return gdk_pixbuf_animation_get_static_image(avatar->animation);
	}

	return avatar->pixbuf;
}

gboolean
purple_avatar_get_animated(PurpleAvatar *avatar) {
	g_return_val_if_fail(PURPLE_IS_AVATAR(avatar), FALSE);

	return avatar->animated;
}

GdkPixbufAnimation *
purple_avatar_get_animation(PurpleAvatar *avatar) {
	g_return_val_if_fail(PURPLE_IS_AVATAR(avatar), NULL);

	return avatar->animation;
}

PurpleTags *
purple_avatar_get_tags(PurpleAvatar *avatar) {
	g_return_val_if_fail(PURPLE_IS_AVATAR(avatar), NULL);

	return avatar->tags;
}