Move the "Change status to" menu to be beside the checkbox controlling it.
[pidgin-git.git] / libpurple / theme.c
blob49f5c89fadd8ec52e7f8d1b34a5911ae2af47592
1 /*
2 * Themes for libpurple
4 * Pidgin 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.
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.
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.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
23 #include <glib.h>
24 #include <string.h>
26 #include "internal.h"
27 #include "theme.h"
28 #include "util.h"
30 #define PURPLE_THEME_GET_PRIVATE(PurpleTheme) \
31 ((PurpleThemePrivate *) ((PurpleTheme)->priv))
33 void purple_theme_set_type_string(PurpleTheme *theme, const gchar *type);
35 /******************************************************************************
36 * Structs
37 *****************************************************************************/
39 typedef struct {
40 gchar *name;
41 gchar *description;
42 gchar *author;
43 gchar *type;
44 gchar *dir;
45 gchar *img;
46 } PurpleThemePrivate;
48 /******************************************************************************
49 * Globals
50 *****************************************************************************/
52 static GObjectClass *parent_class = NULL;
54 /******************************************************************************
55 * Enums
56 *****************************************************************************/
58 enum {
59 PROP_ZERO = 0,
60 PROP_NAME,
61 PROP_DESCRIPTION,
62 PROP_AUTHOR,
63 PROP_TYPE,
64 PROP_DIR,
65 PROP_IMAGE
68 /******************************************************************************
69 * GObject Stuff
70 *****************************************************************************/
72 static void
73 purple_theme_get_property(GObject *obj, guint param_id, GValue *value,
74 GParamSpec *psec)
76 PurpleTheme *theme = PURPLE_THEME(obj);
78 switch (param_id) {
79 case PROP_NAME:
80 g_value_set_string(value, purple_theme_get_name(theme));
81 break;
82 case PROP_DESCRIPTION:
83 g_value_set_string(value, purple_theme_get_description(theme));
84 break;
85 case PROP_AUTHOR:
86 g_value_set_string(value, purple_theme_get_author(theme));
87 break;
88 case PROP_TYPE:
89 g_value_set_string(value, purple_theme_get_type_string(theme));
90 break;
91 case PROP_DIR:
92 g_value_set_string(value, purple_theme_get_dir(theme));
93 break;
94 case PROP_IMAGE:
95 g_value_set_string(value, purple_theme_get_image(theme));
96 break;
97 default:
98 G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, psec);
99 break;
103 static void
104 purple_theme_set_property(GObject *obj, guint param_id, const GValue *value,
105 GParamSpec *psec)
107 PurpleTheme *theme = PURPLE_THEME(obj);
109 switch (param_id) {
110 case PROP_NAME:
111 purple_theme_set_name(theme, g_value_get_string(value));
112 break;
113 case PROP_DESCRIPTION:
114 purple_theme_set_description(theme, g_value_get_string(value));
115 break;
116 case PROP_AUTHOR:
117 purple_theme_set_author(theme, g_value_get_string(value));
118 break;
119 case PROP_TYPE:
120 purple_theme_set_type_string(theme, g_value_get_string(value));
121 break;
122 case PROP_DIR:
123 purple_theme_set_dir(theme, g_value_get_string(value));
124 break;
125 case PROP_IMAGE:
126 purple_theme_set_image(theme, g_value_get_string(value));
127 break;
128 default:
129 G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, psec);
130 break;
134 static void
135 purple_theme_init(GTypeInstance *instance,
136 gpointer klass)
138 PurpleTheme *theme = PURPLE_THEME(instance);
139 theme->priv = g_new0(PurpleThemePrivate, 1);
142 static void
143 purple_theme_finalize(GObject *obj)
145 PurpleTheme *theme = PURPLE_THEME(obj);
146 PurpleThemePrivate *priv = PURPLE_THEME_GET_PRIVATE(theme);
148 g_free(priv->name);
149 g_free(priv->description);
150 g_free(priv->author);
151 g_free(priv->type);
152 g_free(priv->dir);
153 g_free(priv->img);
155 G_OBJECT_CLASS (parent_class)->finalize (obj);
158 static void
159 purple_theme_class_init(PurpleThemeClass *klass)
161 GObjectClass *obj_class = G_OBJECT_CLASS(klass);
162 GParamSpec *pspec;
164 parent_class = g_type_class_peek_parent(klass);
166 obj_class->get_property = purple_theme_get_property;
167 obj_class->set_property = purple_theme_set_property;
168 obj_class->finalize = purple_theme_finalize;
170 /* NAME */
171 pspec = g_param_spec_string("name", "Name",
172 "The name of the theme",
173 NULL,
174 G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
175 g_object_class_install_property(obj_class, PROP_NAME, pspec);
177 /* DESCRIPTION */
178 pspec = g_param_spec_string("description", "Description",
179 "The description of the theme",
180 NULL,
181 G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
182 g_object_class_install_property(obj_class, PROP_DESCRIPTION, pspec);
184 /* AUTHOR */
185 pspec = g_param_spec_string("author", "Author",
186 "The author of the theme",
187 NULL,
188 G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
189 g_object_class_install_property(obj_class, PROP_AUTHOR, pspec);
191 /* TYPE STRING (read only) */
192 pspec = g_param_spec_string("type", "Type",
193 "The string representing the type of the theme",
194 NULL,
195 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
196 g_object_class_install_property(obj_class, PROP_TYPE, pspec);
198 /* DIRECTORY */
199 pspec = g_param_spec_string("directory", "Directory",
200 "The directory that contains the theme and all its files",
201 NULL,
202 G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
203 g_object_class_install_property(obj_class, PROP_DIR, pspec);
205 /* PREVIEW IMAGE */
206 pspec = g_param_spec_string("image", "Image",
207 "A preview image of the theme",
208 NULL,
209 G_PARAM_READWRITE);
210 g_object_class_install_property(obj_class, PROP_IMAGE, pspec);
214 GType
215 purple_theme_get_type(void)
217 static GType type = 0;
218 if (type == 0) {
219 static const GTypeInfo info = {
220 sizeof(PurpleThemeClass),
221 NULL, /* base_init */
222 NULL, /* base_finalize */
223 (GClassInitFunc)purple_theme_class_init, /* class_init */
224 NULL, /* class_finalize */
225 NULL, /* class_data */
226 sizeof(PurpleTheme),
227 0, /* n_preallocs */
228 purple_theme_init, /* instance_init */
229 NULL, /* value table */
231 type = g_type_register_static (G_TYPE_OBJECT,
232 "PurpleTheme", &info, G_TYPE_FLAG_ABSTRACT);
234 return type;
237 /******************************************************************************
238 * Helper Functions
239 *****************************************************************************/
241 static gchar *
242 theme_clean_text(const gchar *text)
244 gchar *clean_text = NULL;
245 if (text != NULL) {
246 clean_text = g_markup_escape_text(text, -1);
247 g_strdelimit(clean_text, "\n", ' ');
248 purple_str_strip_char(clean_text, '\r');
250 return clean_text;
253 /*****************************************************************************
254 * Public API function
255 *****************************************************************************/
257 const gchar *
258 purple_theme_get_name(PurpleTheme *theme)
260 PurpleThemePrivate *priv;
262 g_return_val_if_fail(PURPLE_IS_THEME(theme), NULL);
264 priv = PURPLE_THEME_GET_PRIVATE(theme);
265 return priv->name;
268 void
269 purple_theme_set_name(PurpleTheme *theme, const gchar *name)
271 PurpleThemePrivate *priv;
273 g_return_if_fail(PURPLE_IS_THEME(theme));
275 priv = PURPLE_THEME_GET_PRIVATE(theme);
277 g_free(priv->name);
278 priv->name = theme_clean_text(name);
281 const gchar *
282 purple_theme_get_description(PurpleTheme *theme)
284 PurpleThemePrivate *priv;
286 g_return_val_if_fail(PURPLE_IS_THEME(theme), NULL);
288 priv = PURPLE_THEME_GET_PRIVATE(theme);
289 return priv->description;
292 void
293 purple_theme_set_description(PurpleTheme *theme, const gchar *description)
295 PurpleThemePrivate *priv;
297 g_return_if_fail(PURPLE_IS_THEME(theme));
299 priv = PURPLE_THEME_GET_PRIVATE(theme);
301 g_free(priv->description);
302 priv->description = theme_clean_text(description);
305 const gchar *
306 purple_theme_get_author(PurpleTheme *theme)
308 PurpleThemePrivate *priv;
310 g_return_val_if_fail(PURPLE_IS_THEME(theme), NULL);
312 priv = PURPLE_THEME_GET_PRIVATE(theme);
313 return priv->author;
316 void
317 purple_theme_set_author(PurpleTheme *theme, const gchar *author)
319 PurpleThemePrivate *priv;
321 g_return_if_fail(PURPLE_IS_THEME(theme));
323 priv = PURPLE_THEME_GET_PRIVATE(theme);
325 g_free(priv->author);
326 priv->author = theme_clean_text(author);
329 const gchar *
330 purple_theme_get_type_string(PurpleTheme *theme)
332 PurpleThemePrivate *priv;
334 g_return_val_if_fail(PURPLE_IS_THEME(theme), NULL);
336 priv = PURPLE_THEME_GET_PRIVATE(theme);
337 return priv->type;
340 /* < private > */
341 void
342 purple_theme_set_type_string(PurpleTheme *theme, const gchar *type)
344 PurpleThemePrivate *priv;
346 g_return_if_fail(PURPLE_IS_THEME(theme));
348 priv = PURPLE_THEME_GET_PRIVATE(theme);
350 g_free(priv->type);
351 priv->type = g_strdup(type);
354 const gchar *
355 purple_theme_get_dir(PurpleTheme *theme)
357 PurpleThemePrivate *priv;
359 g_return_val_if_fail(PURPLE_IS_THEME(theme), NULL);
361 priv = PURPLE_THEME_GET_PRIVATE(theme);
362 return priv->dir;
365 void
366 purple_theme_set_dir(PurpleTheme *theme, const gchar *dir)
368 PurpleThemePrivate *priv;
370 g_return_if_fail(PURPLE_IS_THEME(theme));
372 priv = PURPLE_THEME_GET_PRIVATE(theme);
374 g_free(priv->dir);
375 priv->dir = g_strdup(dir);
378 const gchar *
379 purple_theme_get_image(PurpleTheme *theme)
381 PurpleThemePrivate *priv;
383 g_return_val_if_fail(PURPLE_IS_THEME(theme), NULL);
385 priv = PURPLE_THEME_GET_PRIVATE(theme);
387 return priv->img;
390 gchar *
391 purple_theme_get_image_full(PurpleTheme *theme)
393 const gchar *filename = purple_theme_get_image(theme);
395 if (filename)
396 return g_build_filename(purple_theme_get_dir(PURPLE_THEME(theme)), filename, NULL);
397 else
398 return NULL;
401 void
402 purple_theme_set_image(PurpleTheme *theme, const gchar *img)
404 PurpleThemePrivate *priv;
406 g_return_if_fail(PURPLE_IS_THEME(theme));
408 priv = PURPLE_THEME_GET_PRIVATE(theme);
410 g_free(priv->img);
411 priv->img = g_strdup(img);