Add purple_util_write_data_to_*_file declarations
[pidgin-git.git] / libpurple / theme.c
blobbcf39c48442af7b29075ad49ffef6e2f65e65369
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 "glibcompat.h"
25 #include "theme.h"
26 #include "util.h"
28 #define PURPLE_THEME_GET_PRIVATE(PurpleTheme) \
29 (G_TYPE_INSTANCE_GET_PRIVATE((PurpleTheme), PURPLE_TYPE_THEME, PurpleThemePrivate))
31 void purple_theme_set_type_string(PurpleTheme *theme, const gchar *type);
33 /******************************************************************************
34 * Structs
35 *****************************************************************************/
37 typedef struct {
38 gchar *name;
39 gchar *description;
40 gchar *author;
41 gchar *type;
42 gchar *dir;
43 gchar *img;
44 } PurpleThemePrivate;
46 /******************************************************************************
47 * Enums
48 *****************************************************************************/
50 enum {
51 PROP_ZERO = 0,
52 PROP_NAME,
53 PROP_DESCRIPTION,
54 PROP_AUTHOR,
55 PROP_TYPE,
56 PROP_DIR,
57 PROP_IMAGE,
58 PROP_LAST
61 /******************************************************************************
62 * Globals
63 *****************************************************************************/
65 static GObjectClass *parent_class = NULL;
66 static GParamSpec *properties[PROP_LAST];
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_finalize(GObject *obj)
137 PurpleTheme *theme = PURPLE_THEME(obj);
138 PurpleThemePrivate *priv = PURPLE_THEME_GET_PRIVATE(theme);
140 g_free(priv->name);
141 g_free(priv->description);
142 g_free(priv->author);
143 g_free(priv->type);
144 g_free(priv->dir);
145 g_free(priv->img);
147 G_OBJECT_CLASS (parent_class)->finalize (obj);
150 static void
151 purple_theme_class_init(PurpleThemeClass *klass)
153 GObjectClass *obj_class = G_OBJECT_CLASS(klass);
155 parent_class = g_type_class_peek_parent(klass);
157 g_type_class_add_private(klass, sizeof(PurpleThemePrivate));
159 obj_class->get_property = purple_theme_get_property;
160 obj_class->set_property = purple_theme_set_property;
161 obj_class->finalize = purple_theme_finalize;
163 /* NAME */
164 properties[PROP_NAME] = g_param_spec_string("name", "Name",
165 "The name of the theme",
166 NULL,
167 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
169 /* DESCRIPTION */
170 properties[PROP_DESCRIPTION] = g_param_spec_string("description",
171 "Description",
172 "The description of the theme",
173 NULL,
174 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
176 /* AUTHOR */
177 properties[PROP_AUTHOR] = g_param_spec_string("author", "Author",
178 "The author of the theme",
179 NULL,
180 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
182 /* TYPE STRING (read only) */
183 properties[PROP_TYPE] = g_param_spec_string("type", "Type",
184 "The string representing the type of the theme",
185 NULL,
186 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
187 G_PARAM_STATIC_STRINGS);
189 /* DIRECTORY */
190 properties[PROP_DIR] = g_param_spec_string("directory", "Directory",
191 "The directory that contains the theme and all its files",
192 NULL,
193 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
195 /* PREVIEW IMAGE */
196 properties[PROP_IMAGE] = g_param_spec_string("image", "Image",
197 "A preview image of the theme",
198 NULL,
199 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
201 g_object_class_install_properties(obj_class, PROP_LAST, properties);
205 GType
206 purple_theme_get_type(void)
208 static GType type = 0;
209 if (type == 0) {
210 static const GTypeInfo info = {
211 sizeof(PurpleThemeClass),
212 NULL, /* base_init */
213 NULL, /* base_finalize */
214 (GClassInitFunc)purple_theme_class_init, /* class_init */
215 NULL, /* class_finalize */
216 NULL, /* class_data */
217 sizeof(PurpleTheme),
218 0, /* n_preallocs */
219 NULL, /* instance_init */
220 NULL, /* value table */
222 type = g_type_register_static (G_TYPE_OBJECT,
223 "PurpleTheme", &info, G_TYPE_FLAG_ABSTRACT);
225 return type;
228 /******************************************************************************
229 * Helper Functions
230 *****************************************************************************/
232 static gchar *
233 theme_clean_text(const gchar *text)
235 gchar *clean_text = NULL;
236 if (text != NULL) {
237 clean_text = g_markup_escape_text(text, -1);
238 g_strdelimit(clean_text, "\n", ' ');
239 purple_str_strip_char(clean_text, '\r');
241 return clean_text;
244 /*****************************************************************************
245 * Public API function
246 *****************************************************************************/
248 const gchar *
249 purple_theme_get_name(PurpleTheme *theme)
251 PurpleThemePrivate *priv;
253 g_return_val_if_fail(PURPLE_IS_THEME(theme), NULL);
255 priv = PURPLE_THEME_GET_PRIVATE(theme);
256 return priv->name;
259 void
260 purple_theme_set_name(PurpleTheme *theme, const gchar *name)
262 PurpleThemePrivate *priv;
264 g_return_if_fail(PURPLE_IS_THEME(theme));
266 priv = PURPLE_THEME_GET_PRIVATE(theme);
268 g_free(priv->name);
269 priv->name = theme_clean_text(name);
271 g_object_notify_by_pspec(G_OBJECT(theme), properties[PROP_NAME]);
274 const gchar *
275 purple_theme_get_description(PurpleTheme *theme)
277 PurpleThemePrivate *priv;
279 g_return_val_if_fail(PURPLE_IS_THEME(theme), NULL);
281 priv = PURPLE_THEME_GET_PRIVATE(theme);
282 return priv->description;
285 void
286 purple_theme_set_description(PurpleTheme *theme, const gchar *description)
288 PurpleThemePrivate *priv;
290 g_return_if_fail(PURPLE_IS_THEME(theme));
292 priv = PURPLE_THEME_GET_PRIVATE(theme);
294 g_free(priv->description);
295 priv->description = theme_clean_text(description);
297 g_object_notify_by_pspec(G_OBJECT(theme), properties[PROP_DESCRIPTION]);
300 const gchar *
301 purple_theme_get_author(PurpleTheme *theme)
303 PurpleThemePrivate *priv;
305 g_return_val_if_fail(PURPLE_IS_THEME(theme), NULL);
307 priv = PURPLE_THEME_GET_PRIVATE(theme);
308 return priv->author;
311 void
312 purple_theme_set_author(PurpleTheme *theme, const gchar *author)
314 PurpleThemePrivate *priv;
316 g_return_if_fail(PURPLE_IS_THEME(theme));
318 priv = PURPLE_THEME_GET_PRIVATE(theme);
320 g_free(priv->author);
321 priv->author = theme_clean_text(author);
323 g_object_notify_by_pspec(G_OBJECT(theme), properties[PROP_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);
350 g_object_notify_by_pspec(G_OBJECT(theme), properties[PROP_TYPE]);
353 const gchar *
354 purple_theme_get_dir(PurpleTheme *theme)
356 PurpleThemePrivate *priv;
358 g_return_val_if_fail(PURPLE_IS_THEME(theme), NULL);
360 priv = PURPLE_THEME_GET_PRIVATE(theme);
361 return priv->dir;
364 void
365 purple_theme_set_dir(PurpleTheme *theme, const gchar *dir)
367 PurpleThemePrivate *priv;
369 g_return_if_fail(PURPLE_IS_THEME(theme));
371 priv = PURPLE_THEME_GET_PRIVATE(theme);
373 g_free(priv->dir);
374 priv->dir = g_strdup(dir);
376 g_object_notify_by_pspec(G_OBJECT(theme), properties[PROP_DIR]);
379 const gchar *
380 purple_theme_get_image(PurpleTheme *theme)
382 PurpleThemePrivate *priv;
384 g_return_val_if_fail(PURPLE_IS_THEME(theme), NULL);
386 priv = PURPLE_THEME_GET_PRIVATE(theme);
388 return priv->img;
391 gchar *
392 purple_theme_get_image_full(PurpleTheme *theme)
394 const gchar *filename = purple_theme_get_image(theme);
396 if (filename)
397 return g_build_filename(purple_theme_get_dir(PURPLE_THEME(theme)), filename, NULL);
398 else
399 return NULL;
402 void
403 purple_theme_set_image(PurpleTheme *theme, const gchar *img)
405 PurpleThemePrivate *priv;
407 g_return_if_fail(PURPLE_IS_THEME(theme));
409 priv = PURPLE_THEME_GET_PRIVATE(theme);
411 g_free(priv->img);
412 priv->img = g_strdup(img);
414 g_object_notify_by_pspec(G_OBJECT(theme), properties[PROP_IMAGE]);