Remove unused Meson option for enchant.
[pidgin-git.git] / libpurple / smiley.c
blob63aea7a035c9017a174a85efc4f13756282f085c
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 "smiley.h"
24 /**
25 * PurpleSmiley:
27 * A generic smiley. It can either be a theme smiley, or a custom smiley.
29 struct _PurpleSmiley {
30 /*< private >*/
31 PurpleImage parent;
34 typedef struct {
35 gchar *shortcut;
36 } PurpleSmileyPrivate;
38 enum
40 PROP_0,
41 PROP_SHORTCUT,
42 PROP_LAST
45 static GParamSpec *properties[PROP_LAST];
47 G_DEFINE_TYPE_WITH_PRIVATE(PurpleSmiley, purple_smiley, PURPLE_TYPE_IMAGE);
49 /*******************************************************************************
50 * Helpers
51 ******************************************************************************/
52 static void
53 _purple_smiley_set_shortcut(PurpleSmiley *smiley, const gchar *shortcut) {
54 PurpleSmileyPrivate *priv = purple_smiley_get_instance_private(smiley);
56 g_free(priv->shortcut);
58 priv->shortcut = g_strdup(shortcut);
60 g_object_notify(G_OBJECT(smiley), "shortcut");
63 /*******************************************************************************
64 * Object stuff
65 ******************************************************************************/
67 static void
68 purple_smiley_init(PurpleSmiley *smiley) {
71 static void
72 purple_smiley_finalize(GObject *obj) {
73 PurpleSmiley *smiley = PURPLE_SMILEY(obj);
74 PurpleSmileyPrivate *priv = purple_smiley_get_instance_private(smiley);
76 g_free(priv->shortcut);
78 G_OBJECT_CLASS(purple_smiley_parent_class)->finalize(obj);
81 static void
82 purple_smiley_get_property(GObject *obj, guint param_id, GValue *value,
83 GParamSpec *pspec)
85 PurpleSmiley *smiley = PURPLE_SMILEY(obj);
87 switch (param_id) {
88 case PROP_SHORTCUT:
89 g_value_set_string(value, purple_smiley_get_shortcut(smiley));
90 break;
91 default:
92 G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
93 break;
97 static void
98 purple_smiley_set_property(GObject *obj, guint param_id, const GValue *value,
99 GParamSpec *pspec)
101 PurpleSmiley *smiley = PURPLE_SMILEY(obj);
103 switch (param_id) {
104 case PROP_SHORTCUT:
105 _purple_smiley_set_shortcut(smiley, g_value_get_string(value));
106 break;
107 default:
108 G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
109 break;
113 static void
114 purple_smiley_class_init(PurpleSmileyClass *klass) {
115 GObjectClass *obj_class = G_OBJECT_CLASS(klass);
117 obj_class->get_property = purple_smiley_get_property;
118 obj_class->set_property = purple_smiley_set_property;
119 obj_class->finalize = purple_smiley_finalize;
121 properties[PROP_SHORTCUT] = g_param_spec_string(
122 "shortcut",
123 "Shortcut",
124 "A non-escaped textual representation of a smiley.",
125 NULL,
126 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS
129 g_object_class_install_properties(obj_class, PROP_LAST, properties);
132 /*******************************************************************************
133 * API
134 ******************************************************************************/
135 PurpleSmiley *
136 purple_smiley_new(const gchar *shortcut, const gchar *path)
138 PurpleSmiley *smiley = NULL;
139 GBytes *bytes = NULL;
140 gchar *contents = NULL;
141 gsize length = 0;
143 g_return_val_if_fail(shortcut != NULL, NULL);
144 g_return_val_if_fail(path != NULL, NULL);
146 if(!g_file_get_contents(path, &contents, &length, NULL)) {
147 return NULL;
150 bytes = g_bytes_new_take(contents, length);
152 smiley = g_object_new(
153 PURPLE_TYPE_SMILEY,
154 "path", path,
155 "contents", bytes,
156 "shortcut", shortcut,
157 NULL
160 g_bytes_unref(bytes);
162 return smiley;
165 PurpleSmiley *
166 purple_smiley_new_from_data(const gchar *shortcut,
167 const guint8 *data,
168 gsize length)
170 PurpleSmiley *smiley = NULL;
171 GBytes *bytes = NULL;
173 g_return_val_if_fail(shortcut != NULL, NULL);
175 bytes = g_bytes_new(data, length);
177 smiley = g_object_new(
178 PURPLE_TYPE_SMILEY,
179 "shortcut", shortcut,
180 "contents", bytes,
181 NULL
184 g_bytes_unref(bytes);
186 return smiley;
190 PurpleSmiley *
191 purple_smiley_new_remote(const gchar *shortcut) {
192 g_return_val_if_fail(shortcut != NULL, NULL);
194 return g_object_new(
195 PURPLE_TYPE_SMILEY,
196 "shortcut", shortcut,
197 NULL
201 const gchar *
202 purple_smiley_get_shortcut(PurpleSmiley *smiley)
204 PurpleSmileyPrivate *priv = NULL;
206 g_return_val_if_fail(PURPLE_IS_SMILEY(smiley), NULL);
208 priv = purple_smiley_get_instance_private(smiley);
210 return priv->shortcut;