Migrate xmpp-caps.xml to XDG cache dir
[pidgin-git.git] / libpurple / smiley.c
blob7f5aa59a5fa7434c2a14c499eb1f20a183dbfe79
1 /* purple
3 * Purple is the legal property of its developers, whose names are too numerous
4 * to list here. Please refer to the COPYRIGHT file distributed with this
5 * source distribution.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
22 #include "internal.h"
23 #include "glibcompat.h"
24 #include "dbus-maybe.h"
25 #include "debug.h"
26 #include "smiley.h"
27 #include "util.h"
28 #include "xmlnode.h"
30 #define PURPLE_SMILEY_GET_PRIVATE(obj) \
31 (G_TYPE_INSTANCE_GET_PRIVATE((obj), PURPLE_TYPE_SMILEY, PurpleSmileyPrivate))
33 typedef struct {
34 gchar *shortcut;
35 PurpleImage *image;
36 } PurpleSmileyPrivate;
38 enum
40 PROP_0,
41 PROP_SHORTCUT,
42 PROP_LAST
45 static GObjectClass *parent_class;
46 static GParamSpec *properties[PROP_LAST];
48 /*******************************************************************************
49 * API implementation
50 ******************************************************************************/
52 PurpleSmiley *
53 purple_smiley_new(const gchar *shortcut, const gchar *path)
55 PurpleSmiley *smiley;
56 PurpleSmileyPrivate *priv;
58 g_return_val_if_fail(shortcut != NULL, NULL);
59 g_return_val_if_fail(path != NULL, NULL);
61 smiley = g_object_new(PURPLE_TYPE_SMILEY,
62 "shortcut", shortcut,
63 NULL);
64 priv = PURPLE_SMILEY_GET_PRIVATE(smiley);
66 priv->image = purple_image_new_from_file(path, FALSE);
67 if (!priv->image) {
68 purple_debug_error("smiley", "Couldn't load smiley data ");
69 g_object_unref(smiley);
70 return NULL;
73 return smiley;
76 PurpleSmiley *
77 purple_smiley_new_remote(const gchar *shortcut)
79 PurpleSmiley *smiley;
80 PurpleSmileyPrivate *priv;
82 g_return_val_if_fail(shortcut != NULL, NULL);
84 smiley = g_object_new(PURPLE_TYPE_SMILEY,
85 "shortcut", shortcut,
86 NULL);
87 priv = PURPLE_SMILEY_GET_PRIVATE(smiley);
89 priv->image = purple_image_transfer_new();
91 return smiley;
94 const gchar *
95 purple_smiley_get_shortcut(const PurpleSmiley *smiley)
97 PurpleSmileyPrivate *priv = PURPLE_SMILEY_GET_PRIVATE(smiley);
99 g_return_val_if_fail(priv != NULL, NULL);
101 return priv->shortcut;
104 PurpleImage *
105 purple_smiley_get_image(PurpleSmiley *smiley)
107 PurpleSmileyPrivate *priv = PURPLE_SMILEY_GET_PRIVATE(smiley);
109 g_return_val_if_fail(priv != NULL, NULL);
110 g_return_val_if_fail(priv->image != NULL, NULL);
112 return priv->image;
116 /*******************************************************************************
117 * Object stuff
118 ******************************************************************************/
120 static void
121 purple_smiley_init(GTypeInstance *instance, gpointer klass)
123 PurpleSmiley *smiley = PURPLE_SMILEY(instance);
124 PURPLE_DBUS_REGISTER_POINTER(smiley, PurpleSmiley);
127 static void
128 purple_smiley_finalize(GObject *obj)
130 PurpleSmiley *smiley = PURPLE_SMILEY(obj);
131 PurpleSmileyPrivate *priv = PURPLE_SMILEY_GET_PRIVATE(smiley);
133 g_free(priv->shortcut);
135 if (priv->image)
136 g_object_unref(priv->image);
138 PURPLE_DBUS_UNREGISTER_POINTER(smiley);
140 G_OBJECT_CLASS(parent_class)->finalize(obj);
143 static void
144 purple_smiley_get_property(GObject *object, guint par_id, GValue *value,
145 GParamSpec *pspec)
147 PurpleSmiley *smiley = PURPLE_SMILEY(object);
148 PurpleSmileyPrivate *priv = PURPLE_SMILEY_GET_PRIVATE(smiley);
150 switch (par_id) {
151 case PROP_SHORTCUT:
152 g_value_set_string(value, priv->shortcut);
153 break;
154 default:
155 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, par_id, pspec);
156 break;
160 static void
161 purple_smiley_set_property(GObject *object, guint par_id, const GValue *value,
162 GParamSpec *pspec)
164 PurpleSmiley *smiley = PURPLE_SMILEY(object);
165 PurpleSmileyPrivate *priv = PURPLE_SMILEY_GET_PRIVATE(smiley);
167 switch (par_id) {
168 case PROP_SHORTCUT:
169 g_free(priv->shortcut);
170 priv->shortcut = g_strdup(g_value_get_string(value));
171 break;
172 default:
173 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, par_id, pspec);
174 break;
178 static void
179 purple_smiley_class_init(PurpleSmileyClass *klass)
181 GObjectClass *gobj_class = G_OBJECT_CLASS(klass);
183 parent_class = g_type_class_peek_parent(klass);
185 g_type_class_add_private(klass, sizeof(PurpleSmileyPrivate));
187 gobj_class->get_property = purple_smiley_get_property;
188 gobj_class->set_property = purple_smiley_set_property;
189 gobj_class->finalize = purple_smiley_finalize;
191 properties[PROP_SHORTCUT] = g_param_spec_string("shortcut", "Shortcut",
192 "A non-escaped textual representation of a smiley.", NULL,
193 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
195 g_object_class_install_properties(gobj_class, PROP_LAST, properties);
198 GType
199 purple_smiley_get_type(void)
201 static GType type = 0;
203 if (G_UNLIKELY(type == 0)) {
204 static const GTypeInfo info = {
205 .class_size = sizeof(PurpleSmileyClass),
206 .class_init = (GClassInitFunc)purple_smiley_class_init,
207 .instance_size = sizeof(PurpleSmiley),
208 .instance_init = purple_smiley_init,
211 type = g_type_register_static(G_TYPE_OBJECT,
212 "PurpleSmiley", &info, 0);
215 return type;