Open an explorer.exe window at the location of the file when clicking
[pidgin-git.git] / libpurple / theme.c
blobc21e71a053683432c85a44fde8b22a0f19c81a73
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 "internal.h"
24 #include "theme.h"
25 #include "util.h"
27 #define PURPLE_THEME_GET_PRIVATE(PurpleTheme) \
28 ((PurpleThemePrivate *) ((PurpleTheme)->priv))
30 void purple_theme_set_type_string(PurpleTheme *theme, const gchar *type);
32 /******************************************************************************
33 * Structs
34 *****************************************************************************/
36 typedef struct {
37 gchar *name;
38 gchar *description;
39 gchar *author;
40 gchar *type;
41 gchar *dir;
42 gchar *img;
43 } PurpleThemePrivate;
45 /******************************************************************************
46 * Globals
47 *****************************************************************************/
49 static GObjectClass *parent_class = NULL;
51 /******************************************************************************
52 * Enums
53 *****************************************************************************/
55 enum {
56 PROP_ZERO = 0,
57 PROP_NAME,
58 PROP_DESCRIPTION,
59 PROP_AUTHOR,
60 PROP_TYPE,
61 PROP_DIR,
62 PROP_IMAGE
65 /******************************************************************************
66 * GObject Stuff
67 *****************************************************************************/
69 static void
70 purple_theme_get_property(GObject *obj, guint param_id, GValue *value,
71 GParamSpec *psec)
73 PurpleTheme *theme = PURPLE_THEME(obj);
75 switch (param_id) {
76 case PROP_NAME:
77 g_value_set_string(value, purple_theme_get_name(theme));
78 break;
79 case PROP_DESCRIPTION:
80 g_value_set_string(value, purple_theme_get_description(theme));
81 break;
82 case PROP_AUTHOR:
83 g_value_set_string(value, purple_theme_get_author(theme));
84 break;
85 case PROP_TYPE:
86 g_value_set_string(value, purple_theme_get_type_string(theme));
87 break;
88 case PROP_DIR:
89 g_value_set_string(value, purple_theme_get_dir(theme));
90 break;
91 case PROP_IMAGE:
92 g_value_set_string(value, purple_theme_get_image(theme));
93 break;
94 default:
95 G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, psec);
96 break;
100 static void
101 purple_theme_set_property(GObject *obj, guint param_id, const GValue *value,
102 GParamSpec *psec)
104 PurpleTheme *theme = PURPLE_THEME(obj);
106 switch (param_id) {
107 case PROP_NAME:
108 purple_theme_set_name(theme, g_value_get_string(value));
109 break;
110 case PROP_DESCRIPTION:
111 purple_theme_set_description(theme, g_value_get_string(value));
112 break;
113 case PROP_AUTHOR:
114 purple_theme_set_author(theme, g_value_get_string(value));
115 break;
116 case PROP_TYPE:
117 purple_theme_set_type_string(theme, g_value_get_string(value));
118 break;
119 case PROP_DIR:
120 purple_theme_set_dir(theme, g_value_get_string(value));
121 break;
122 case PROP_IMAGE:
123 purple_theme_set_image(theme, g_value_get_string(value));
124 break;
125 default:
126 G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, psec);
127 break;
131 static void
132 purple_theme_init(GTypeInstance *instance,
133 gpointer klass)
135 PurpleTheme *theme = PURPLE_THEME(instance);
136 theme->priv = g_new0(PurpleThemePrivate, 1);
139 static void
140 purple_theme_finalize(GObject *obj)
142 PurpleTheme *theme = PURPLE_THEME(obj);
143 PurpleThemePrivate *priv = PURPLE_THEME_GET_PRIVATE(theme);
145 g_free(priv->name);
146 g_free(priv->description);
147 g_free(priv->author);
148 g_free(priv->type);
149 g_free(priv->dir);
150 g_free(priv->img);
152 G_OBJECT_CLASS (parent_class)->finalize (obj);
155 static void
156 purple_theme_class_init(PurpleThemeClass *klass)
158 GObjectClass *obj_class = G_OBJECT_CLASS(klass);
159 GParamSpec *pspec;
161 parent_class = g_type_class_peek_parent(klass);
163 obj_class->get_property = purple_theme_get_property;
164 obj_class->set_property = purple_theme_set_property;
165 obj_class->finalize = purple_theme_finalize;
167 /* NAME */
168 pspec = g_param_spec_string("name", "Name",
169 "The name of the theme",
170 NULL,
171 G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
172 g_object_class_install_property(obj_class, PROP_NAME, pspec);
174 /* DESCRIPTION */
175 pspec = g_param_spec_string("description", "Description",
176 "The description of the theme",
177 NULL,
178 G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
179 g_object_class_install_property(obj_class, PROP_DESCRIPTION, pspec);
181 /* AUTHOR */
182 pspec = g_param_spec_string("author", "Author",
183 "The author of the theme",
184 NULL,
185 G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
186 g_object_class_install_property(obj_class, PROP_AUTHOR, pspec);
188 /* TYPE STRING (read only) */
189 pspec = g_param_spec_string("type", "Type",
190 "The string representing the type of the theme",
191 NULL,
192 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
193 g_object_class_install_property(obj_class, PROP_TYPE, pspec);
195 /* DIRECTORY */
196 pspec = g_param_spec_string("directory", "Directory",
197 "The directory that contains the theme and all its files",
198 NULL,
199 G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
200 g_object_class_install_property(obj_class, PROP_DIR, pspec);
202 /* PREVIEW IMAGE */
203 pspec = g_param_spec_string("image", "Image",
204 "A preview image of the theme",
205 NULL,
206 G_PARAM_READWRITE);
207 g_object_class_install_property(obj_class, PROP_IMAGE, pspec);
211 GType
212 purple_theme_get_type(void)
214 static GType type = 0;
215 if (type == 0) {
216 static const GTypeInfo info = {
217 sizeof(PurpleThemeClass),
218 NULL, /* base_init */
219 NULL, /* base_finalize */
220 (GClassInitFunc)purple_theme_class_init, /* class_init */
221 NULL, /* class_finalize */
222 NULL, /* class_data */
223 sizeof(PurpleTheme),
224 0, /* n_preallocs */
225 purple_theme_init, /* instance_init */
226 NULL, /* value table */
228 type = g_type_register_static (G_TYPE_OBJECT,
229 "PurpleTheme", &info, G_TYPE_FLAG_ABSTRACT);
231 return type;
234 /******************************************************************************
235 * Helper Functions
236 *****************************************************************************/
238 static gchar *
239 theme_clean_text(const gchar *text)
241 gchar *clean_text = NULL;
242 if (text != NULL) {
243 clean_text = g_markup_escape_text(text, -1);
244 g_strdelimit(clean_text, "\n", ' ');
245 purple_str_strip_char(clean_text, '\r');
247 return clean_text;
250 /*****************************************************************************
251 * Public API function
252 *****************************************************************************/
254 const gchar *
255 purple_theme_get_name(PurpleTheme *theme)
257 PurpleThemePrivate *priv;
259 g_return_val_if_fail(PURPLE_IS_THEME(theme), NULL);
261 priv = PURPLE_THEME_GET_PRIVATE(theme);
262 return priv->name;
265 void
266 purple_theme_set_name(PurpleTheme *theme, const gchar *name)
268 PurpleThemePrivate *priv;
270 g_return_if_fail(PURPLE_IS_THEME(theme));
272 priv = PURPLE_THEME_GET_PRIVATE(theme);
274 g_free(priv->name);
275 priv->name = theme_clean_text(name);
278 const gchar *
279 purple_theme_get_description(PurpleTheme *theme)
281 PurpleThemePrivate *priv;
283 g_return_val_if_fail(PURPLE_IS_THEME(theme), NULL);
285 priv = PURPLE_THEME_GET_PRIVATE(theme);
286 return priv->description;
289 void
290 purple_theme_set_description(PurpleTheme *theme, const gchar *description)
292 PurpleThemePrivate *priv;
294 g_return_if_fail(PURPLE_IS_THEME(theme));
296 priv = PURPLE_THEME_GET_PRIVATE(theme);
298 g_free(priv->description);
299 priv->description = theme_clean_text(description);
302 const gchar *
303 purple_theme_get_author(PurpleTheme *theme)
305 PurpleThemePrivate *priv;
307 g_return_val_if_fail(PURPLE_IS_THEME(theme), NULL);
309 priv = PURPLE_THEME_GET_PRIVATE(theme);
310 return priv->author;
313 void
314 purple_theme_set_author(PurpleTheme *theme, const gchar *author)
316 PurpleThemePrivate *priv;
318 g_return_if_fail(PURPLE_IS_THEME(theme));
320 priv = PURPLE_THEME_GET_PRIVATE(theme);
322 g_free(priv->author);
323 priv->author = theme_clean_text(author);
326 const gchar *
327 purple_theme_get_type_string(PurpleTheme *theme)
329 PurpleThemePrivate *priv;
331 g_return_val_if_fail(PURPLE_IS_THEME(theme), NULL);
333 priv = PURPLE_THEME_GET_PRIVATE(theme);
334 return priv->type;
337 /* < private > */
338 void
339 purple_theme_set_type_string(PurpleTheme *theme, const gchar *type)
341 PurpleThemePrivate *priv;
343 g_return_if_fail(PURPLE_IS_THEME(theme));
345 priv = PURPLE_THEME_GET_PRIVATE(theme);
347 g_free(priv->type);
348 priv->type = g_strdup(type);
351 const gchar *
352 purple_theme_get_dir(PurpleTheme *theme)
354 PurpleThemePrivate *priv;
356 g_return_val_if_fail(PURPLE_IS_THEME(theme), NULL);
358 priv = PURPLE_THEME_GET_PRIVATE(theme);
359 return priv->dir;
362 void
363 purple_theme_set_dir(PurpleTheme *theme, const gchar *dir)
365 PurpleThemePrivate *priv;
367 g_return_if_fail(PURPLE_IS_THEME(theme));
369 priv = PURPLE_THEME_GET_PRIVATE(theme);
371 g_free(priv->dir);
372 priv->dir = g_strdup(dir);
375 const gchar *
376 purple_theme_get_image(PurpleTheme *theme)
378 PurpleThemePrivate *priv;
380 g_return_val_if_fail(PURPLE_IS_THEME(theme), NULL);
382 priv = PURPLE_THEME_GET_PRIVATE(theme);
384 return priv->img;
387 gchar *
388 purple_theme_get_image_full(PurpleTheme *theme)
390 const gchar *filename = purple_theme_get_image(theme);
392 if (filename)
393 return g_build_filename(purple_theme_get_dir(PURPLE_THEME(theme)), filename, NULL);
394 else
395 return NULL;
398 void
399 purple_theme_set_image(PurpleTheme *theme, const gchar *img)
401 PurpleThemePrivate *priv;
403 g_return_if_fail(PURPLE_IS_THEME(theme));
405 priv = PURPLE_THEME_GET_PRIVATE(theme);
407 g_free(priv->img);
408 priv->img = g_strdup(img);