Migrate certificates, icons, logs to XDG dirs
[pidgin-git.git] / pidgin / gtkblist-theme-loader.c
blobf9e07b0cfcd9327ed5661287e35b07be4c496fe4
1 /*
2 * GTKBlistThemeLoader for Pidgin
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"
25 #include <stdlib.h>
26 #include <string.h>
28 #include "xmlnode.h"
29 #include "debug.h"
30 #include "util.h"
32 #include "gtkblist-theme-loader.h"
33 #include "gtkblist-theme.h"
35 /******************************************************************************
36 * Globals
37 *****************************************************************************/
39 #define DEFAULT_TEXT_COLOR "black"
41 /*****************************************************************************
42 * Buddy List Theme Builder
43 *****************************************************************************/
45 static PidginThemeFont *
46 pidgin_theme_font_parse(PurpleXmlNode *node)
48 const char *font;
49 const char *colordesc;
50 GdkRGBA color;
52 font = purple_xmlnode_get_attrib(node, "font");
54 if ((colordesc = purple_xmlnode_get_attrib(node, "color")) == NULL ||
55 !gdk_rgba_parse(&color, colordesc))
56 gdk_rgba_parse(&color, DEFAULT_TEXT_COLOR);
58 return pidgin_theme_font_new(font, &color);
61 static GdkRGBA *
62 parse_color(PurpleXmlNode *node, const char *tag)
64 const char *temp = purple_xmlnode_get_attrib(node, tag);
65 GdkRGBA color;
67 if (temp && gdk_rgba_parse(&color, temp)) {
68 return gdk_rgba_copy(&color);
69 } else {
70 return NULL;
74 static PurpleTheme *
75 pidgin_blist_loader_build(const gchar *theme_dir)
77 PurpleXmlNode *root_node = NULL, *sub_node, *sub_sub_node;
78 gchar *dir, *filename_full, *data = NULL;
79 const gchar *temp, *name;
80 gboolean success = TRUE;
81 GdkRGBA *bgcolor, *expanded_bgcolor, *collapsed_bgcolor, *contact_color;
82 PidginThemeFont *expanded, *collapsed, *contact, *online, *away, *offline, *idle, *message, *message_nick_said, *status;
83 PidginBlistLayout layout;
84 PidginBlistTheme *theme;
85 int i;
86 struct {
87 const char *tag;
88 PidginThemeFont **font;
89 } lookups[] = {
90 {"contact_text", &contact},
91 {"online_text", &online},
92 {"away_text", &away},
93 {"offline_text", &offline},
94 {"idle_text", &idle},
95 {"message_text", &message},
96 {"message_nick_said_text", &message_nick_said},
97 {"status_text", &status},
98 {NULL, NULL}
101 bgcolor = expanded_bgcolor = collapsed_bgcolor = contact_color = NULL;
102 expanded = NULL;
103 collapsed = NULL;
104 contact = NULL;
105 online = NULL;
106 away = NULL;
107 offline = NULL;
108 idle = NULL;
109 message = NULL;
110 message_nick_said = NULL;
111 status = NULL;
113 /* Find the theme file */
114 g_return_val_if_fail(theme_dir != NULL, NULL);
115 dir = g_build_filename(theme_dir, "purple", "blist", NULL);
116 filename_full = g_build_filename(dir, "theme.xml", NULL);
118 if (g_file_test(filename_full, G_FILE_TEST_IS_REGULAR))
119 root_node = purple_xmlnode_from_file(dir, "theme.xml", "buddy list themes", "blist-loader");
121 g_free(filename_full);
122 if (root_node == NULL) {
123 g_free(dir);
124 return NULL;
127 sub_node = purple_xmlnode_get_child(root_node, "description");
128 data = purple_xmlnode_get_data(sub_node);
130 name = purple_xmlnode_get_attrib(root_node, "name");
132 /* <blist> */
133 success = name && purple_strequal(purple_xmlnode_get_attrib(root_node, "type"), "pidgin buddy list");
135 if (!success)
136 purple_debug_warning("gtkblist-theme-loader", "Missing attribute or problem with the root element\n");
138 if (success) {
139 if ((success = (sub_node = purple_xmlnode_get_child(root_node, "blist")) != NULL))
140 bgcolor = parse_color(sub_node, "color");
141 else
142 purple_debug_warning("gtkblist-theme-loader", "Missing or problem with tags: <blist>.\n");
145 /* <groups> */
146 if (success) {
147 if ((success = (sub_node = purple_xmlnode_get_child(root_node, "groups")) != NULL
148 && (sub_sub_node = purple_xmlnode_get_child(sub_node, "expanded")) != NULL)) {
149 expanded = pidgin_theme_font_parse(sub_sub_node);
150 expanded_bgcolor = parse_color(sub_sub_node, "background");
151 } else
152 purple_debug_warning("gtkblist-theme-loader", "Missing or problem with tags: <groups> <expanded>.\n");
155 if (success) {
156 if ((success = sub_node != NULL && (sub_sub_node = purple_xmlnode_get_child(sub_node, "collapsed")) != NULL)) {
157 collapsed = pidgin_theme_font_parse(sub_sub_node);
158 collapsed_bgcolor = parse_color(sub_sub_node, "background");
159 } else
160 purple_debug_warning("gtkblist-theme-loader", "Missing or problem with tags: <groups> <collapsed>.\n");
163 /* <buddys> */
164 if (success) {
165 if ((success = (sub_node = purple_xmlnode_get_child(root_node, "buddys")) != NULL &&
166 (sub_sub_node = purple_xmlnode_get_child(sub_node, "placement")) != NULL)) {
168 layout.status_icon = (temp = purple_xmlnode_get_attrib(sub_sub_node, "status_icon")) != NULL ? atoi(temp) : 0;
169 layout.text = (temp = purple_xmlnode_get_attrib(sub_sub_node, "name")) != NULL ? atoi(temp) : 1;
170 layout.emblem = (temp = purple_xmlnode_get_attrib(sub_sub_node, "emblem")) != NULL ? atoi(temp) : 2;
171 layout.protocol_icon = (temp = purple_xmlnode_get_attrib(sub_sub_node, "protocol_icon")) != NULL ? atoi(temp) : 3;
172 layout.buddy_icon = (temp = purple_xmlnode_get_attrib(sub_sub_node, "buddy_icon")) != NULL ? atoi(temp) : 4;
173 layout.show_status = (temp = purple_xmlnode_get_attrib(sub_sub_node, "status_icon")) != NULL ? atoi(temp) != 0 : 1;
175 } else purple_debug_warning("gtkblist-theme-loader", "Missing or problem with tags: <buddys> <placement>.\n");
178 if (success) {
179 if ((success = (sub_node != NULL && (sub_sub_node = purple_xmlnode_get_child(sub_node, "background")) != NULL)))
180 contact_color = parse_color(sub_sub_node, "color");
181 else
182 purple_debug_warning("gtkblist-theme-loader", "Missing or problem with tags: <buddys> <background>.\n");
185 for (i = 0; success && lookups[i].tag; i++) {
186 if ((success = (sub_node != NULL &&
187 (sub_sub_node = purple_xmlnode_get_child(sub_node, lookups[i].tag)) != NULL))) {
188 *(lookups[i].font) = pidgin_theme_font_parse(sub_sub_node);
189 } else {
190 *(lookups[i].font) = NULL;
194 /* name is required for theme manager */
195 success = (success && purple_xmlnode_get_attrib(root_node, "name") != NULL);
197 /* the new theme */
198 theme = g_object_new(PIDGIN_TYPE_BLIST_THEME,
199 "type", "blist",
200 "name", name,
201 "author", purple_xmlnode_get_attrib(root_node, "author"),
202 "image", purple_xmlnode_get_attrib(root_node, "image"),
203 "directory", dir,
204 "description", data,
205 "background-color", bgcolor,
206 "layout", &layout,
207 "expanded-color", expanded_bgcolor,
208 "expanded-text", expanded,
209 "collapsed-color", collapsed_bgcolor,
210 "collapsed-text", collapsed,
211 "contact-color", contact_color,
212 "contact", contact,
213 "online", online,
214 "away", away,
215 "offline", offline,
216 "idle", idle,
217 "message", message,
218 "message-nick-said", message_nick_said,
219 "status", status, NULL);
221 for (i = 0; lookups[i].tag; i++) {
222 if (*lookups[i].font) {
223 pidgin_theme_font_free(*lookups[i].font);
227 pidgin_theme_font_free(expanded);
228 pidgin_theme_font_free(collapsed);
230 purple_xmlnode_free(root_node);
231 g_free(data);
232 g_free(dir);
234 /* malformed xml file - also frees all partial data*/
235 if (!success) {
236 g_object_unref(theme);
237 theme = NULL;
240 if (bgcolor)
241 gdk_rgba_free(bgcolor);
242 if (expanded_bgcolor)
243 gdk_rgba_free(expanded_bgcolor);
244 if (collapsed_bgcolor)
245 gdk_rgba_free(collapsed_bgcolor);
246 if (contact_color)
247 gdk_rgba_free(contact_color);
249 return PURPLE_THEME(theme);
252 /******************************************************************************
253 * GObject Stuff
254 *****************************************************************************/
256 static void
257 pidgin_blist_theme_loader_class_init(PidginBlistThemeLoaderClass *klass)
259 PurpleThemeLoaderClass *loader_klass = PURPLE_THEME_LOADER_CLASS(klass);
261 loader_klass->purple_theme_loader_build = pidgin_blist_loader_build;
264 GType
265 pidgin_blist_theme_loader_get_type(void)
267 static GType type = 0;
268 if (type == 0) {
269 static const GTypeInfo info = {
270 sizeof(PidginBlistThemeLoaderClass),
271 NULL, /* base_init */
272 NULL, /* base_finalize */
273 (GClassInitFunc)pidgin_blist_theme_loader_class_init, /* class_init */
274 NULL, /* class_finalize */
275 NULL, /* class_data */
276 sizeof(PidginBlistThemeLoader),
277 0, /* n_preallocs */
278 NULL, /* instance_init */
279 NULL, /* value table */
281 type = g_type_register_static(PURPLE_TYPE_THEME_LOADER,
282 "PidginBlistThemeLoader", &info, 0);
284 return type;