Simplify handling of GG xfer auth queue.
[pidgin-git.git] / pidgin / gtkblist-theme-loader.c
blob7c15bd13e12d521e43cb8f83498decf657f4b1db
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 * PidginBlistThemeLoader:
38 * A pidgin buddy list theme loader. extends PurpleThemeLoader (theme-loader.h)
39 * This is a class designed to build sound themes
41 struct _PidginBlistThemeLoader
43 PurpleThemeLoader parent;
46 /******************************************************************************
47 * Globals
48 *****************************************************************************/
50 #define DEFAULT_TEXT_COLOR "black"
52 /*****************************************************************************
53 * Buddy List Theme Builder
54 *****************************************************************************/
56 static PidginThemeFont *
57 pidgin_theme_font_parse(PurpleXmlNode *node)
59 const char *font;
60 const char *colordesc;
61 GdkRGBA color;
63 font = purple_xmlnode_get_attrib(node, "font");
65 if ((colordesc = purple_xmlnode_get_attrib(node, "color")) == NULL ||
66 !gdk_rgba_parse(&color, colordesc))
67 gdk_rgba_parse(&color, DEFAULT_TEXT_COLOR);
69 return pidgin_theme_font_new(font, &color);
72 static GdkRGBA *
73 parse_color(PurpleXmlNode *node, const char *tag)
75 const char *temp = purple_xmlnode_get_attrib(node, tag);
76 GdkRGBA color;
78 if (temp && gdk_rgba_parse(&color, temp)) {
79 return gdk_rgba_copy(&color);
80 } else {
81 return NULL;
85 static PurpleTheme *
86 pidgin_blist_loader_build(const gchar *theme_dir)
88 PurpleXmlNode *root_node = NULL, *sub_node, *sub_sub_node;
89 gchar *dir, *filename_full, *data = NULL;
90 const gchar *temp, *name;
91 gboolean success = TRUE;
92 GdkRGBA *bgcolor, *expanded_bgcolor, *collapsed_bgcolor, *contact_color;
93 PidginThemeFont *expanded, *collapsed, *contact, *online, *away, *offline, *idle, *message, *message_nick_said, *status;
94 PidginBlistLayout layout;
95 PidginBlistTheme *theme;
96 int i;
97 struct {
98 const char *tag;
99 PidginThemeFont **font;
100 } lookups[] = {
101 {"contact_text", &contact},
102 {"online_text", &online},
103 {"away_text", &away},
104 {"offline_text", &offline},
105 {"idle_text", &idle},
106 {"message_text", &message},
107 {"message_nick_said_text", &message_nick_said},
108 {"status_text", &status},
109 {NULL, NULL}
112 bgcolor = expanded_bgcolor = collapsed_bgcolor = contact_color = NULL;
113 expanded = NULL;
114 collapsed = NULL;
115 contact = NULL;
116 online = NULL;
117 away = NULL;
118 offline = NULL;
119 idle = NULL;
120 message = NULL;
121 message_nick_said = NULL;
122 status = NULL;
124 /* Find the theme file */
125 g_return_val_if_fail(theme_dir != NULL, NULL);
126 dir = g_build_filename(theme_dir, "purple", "blist", NULL);
127 filename_full = g_build_filename(dir, "theme.xml", NULL);
129 if (g_file_test(filename_full, G_FILE_TEST_IS_REGULAR))
130 root_node = purple_xmlnode_from_file(dir, "theme.xml", "buddy list themes", "blist-loader");
132 g_free(filename_full);
133 if (root_node == NULL) {
134 g_free(dir);
135 return NULL;
138 sub_node = purple_xmlnode_get_child(root_node, "description");
139 data = purple_xmlnode_get_data(sub_node);
141 name = purple_xmlnode_get_attrib(root_node, "name");
143 /* <blist> */
144 success = name && purple_strequal(purple_xmlnode_get_attrib(root_node, "type"), "pidgin buddy list");
146 if (!success)
147 purple_debug_warning("gtkblist-theme-loader", "Missing attribute or problem with the root element\n");
149 if (success) {
150 if ((success = (sub_node = purple_xmlnode_get_child(root_node, "blist")) != NULL))
151 bgcolor = parse_color(sub_node, "color");
152 else
153 purple_debug_warning("gtkblist-theme-loader", "Missing or problem with tags: <blist>.\n");
156 /* <groups> */
157 if (success) {
158 if ((success = (sub_node = purple_xmlnode_get_child(root_node, "groups")) != NULL
159 && (sub_sub_node = purple_xmlnode_get_child(sub_node, "expanded")) != NULL)) {
160 expanded = pidgin_theme_font_parse(sub_sub_node);
161 expanded_bgcolor = parse_color(sub_sub_node, "background");
162 } else
163 purple_debug_warning("gtkblist-theme-loader", "Missing or problem with tags: <groups> <expanded>.\n");
166 if (success) {
167 if ((success = sub_node != NULL && (sub_sub_node = purple_xmlnode_get_child(sub_node, "collapsed")) != NULL)) {
168 collapsed = pidgin_theme_font_parse(sub_sub_node);
169 collapsed_bgcolor = parse_color(sub_sub_node, "background");
170 } else
171 purple_debug_warning("gtkblist-theme-loader", "Missing or problem with tags: <groups> <collapsed>.\n");
174 /* <buddys> */
175 if (success) {
176 if ((success = (sub_node = purple_xmlnode_get_child(root_node, "buddys")) != NULL &&
177 (sub_sub_node = purple_xmlnode_get_child(sub_node, "placement")) != NULL)) {
179 layout.status_icon = (temp = purple_xmlnode_get_attrib(sub_sub_node, "status_icon")) != NULL ? atoi(temp) : 0;
180 layout.text = (temp = purple_xmlnode_get_attrib(sub_sub_node, "name")) != NULL ? atoi(temp) : 1;
181 layout.emblem = (temp = purple_xmlnode_get_attrib(sub_sub_node, "emblem")) != NULL ? atoi(temp) : 2;
182 layout.protocol_icon = (temp = purple_xmlnode_get_attrib(sub_sub_node, "protocol_icon")) != NULL ? atoi(temp) : 3;
183 layout.buddy_icon = (temp = purple_xmlnode_get_attrib(sub_sub_node, "buddy_icon")) != NULL ? atoi(temp) : 4;
184 layout.show_status = (temp = purple_xmlnode_get_attrib(sub_sub_node, "status_icon")) != NULL ? atoi(temp) != 0 : 1;
186 } else purple_debug_warning("gtkblist-theme-loader", "Missing or problem with tags: <buddys> <placement>.\n");
189 if (success) {
190 if ((success = (sub_node != NULL && (sub_sub_node = purple_xmlnode_get_child(sub_node, "background")) != NULL)))
191 contact_color = parse_color(sub_sub_node, "color");
192 else
193 purple_debug_warning("gtkblist-theme-loader", "Missing or problem with tags: <buddys> <background>.\n");
196 for (i = 0; success && lookups[i].tag; i++) {
197 if ((success = (sub_node != NULL &&
198 (sub_sub_node = purple_xmlnode_get_child(sub_node, lookups[i].tag)) != NULL))) {
199 *(lookups[i].font) = pidgin_theme_font_parse(sub_sub_node);
200 } else {
201 *(lookups[i].font) = NULL;
205 /* name is required for theme manager */
206 success = (success && purple_xmlnode_get_attrib(root_node, "name") != NULL);
208 /* the new theme */
209 theme = g_object_new(PIDGIN_TYPE_BLIST_THEME,
210 "type", "blist",
211 "name", name,
212 "author", purple_xmlnode_get_attrib(root_node, "author"),
213 "image", purple_xmlnode_get_attrib(root_node, "image"),
214 "directory", dir,
215 "description", data,
216 "background-color", bgcolor,
217 "layout", &layout,
218 "expanded-color", expanded_bgcolor,
219 "expanded-text", expanded,
220 "collapsed-color", collapsed_bgcolor,
221 "collapsed-text", collapsed,
222 "contact-color", contact_color,
223 "contact", contact,
224 "online", online,
225 "away", away,
226 "offline", offline,
227 "idle", idle,
228 "message", message,
229 "message-nick-said", message_nick_said,
230 "status", status, NULL);
232 for (i = 0; lookups[i].tag; i++) {
233 if (*lookups[i].font) {
234 pidgin_theme_font_free(*lookups[i].font);
238 pidgin_theme_font_free(expanded);
239 pidgin_theme_font_free(collapsed);
241 purple_xmlnode_free(root_node);
242 g_free(data);
243 g_free(dir);
245 /* malformed xml file - also frees all partial data*/
246 if (!success) {
247 g_object_unref(theme);
248 theme = NULL;
251 if (bgcolor)
252 gdk_rgba_free(bgcolor);
253 if (expanded_bgcolor)
254 gdk_rgba_free(expanded_bgcolor);
255 if (collapsed_bgcolor)
256 gdk_rgba_free(collapsed_bgcolor);
257 if (contact_color)
258 gdk_rgba_free(contact_color);
260 return PURPLE_THEME(theme);
263 /******************************************************************************
264 * GObject Stuff
265 *****************************************************************************/
267 static void
268 pidgin_blist_theme_loader_class_init(PidginBlistThemeLoaderClass *klass)
270 PurpleThemeLoaderClass *loader_klass = PURPLE_THEME_LOADER_CLASS(klass);
272 loader_klass->purple_theme_loader_build = pidgin_blist_loader_build;
275 GType
276 pidgin_blist_theme_loader_get_type(void)
278 static GType type = 0;
279 if (type == 0) {
280 static const GTypeInfo info = {
281 sizeof(PidginBlistThemeLoaderClass),
282 NULL, /* base_init */
283 NULL, /* base_finalize */
284 (GClassInitFunc)pidgin_blist_theme_loader_class_init, /* class_init */
285 NULL, /* class_finalize */
286 NULL, /* class_data */
287 sizeof(PidginBlistThemeLoader),
288 0, /* n_preallocs */
289 NULL, /* instance_init */
290 NULL, /* value table */
292 type = g_type_register_static(PURPLE_TYPE_THEME_LOADER,
293 "PidginBlistThemeLoader", &info, 0);
295 return type;