comparison finch/libgnt/gntstyle.c @ 15817:0e3a8505ebbe

renamed gaim-text to finch
author Sean Egan <seanegan@gmail.com>
date Sun, 18 Mar 2007 19:38:15 +0000
parents
children 8a54f74fecdf
comparison
equal deleted inserted replaced
15816:317e7613e581 15817:0e3a8505ebbe
1 #include "gntstyle.h"
2 #include "gntcolors.h"
3
4 #include <ctype.h>
5 #include <string.h>
6
7 #if GLIB_CHECK_VERSION(2,6,0)
8 static GKeyFile *gkfile;
9 #endif
10
11 static char * str_styles[GNT_STYLES];
12 static int int_styles[GNT_STYLES];
13 static int bool_styles[GNT_STYLES];
14
15 const char *gnt_style_get(GntStyle style)
16 {
17 return str_styles[style];
18 }
19
20 gboolean gnt_style_get_bool(GntStyle style, gboolean def)
21 {
22 int i;
23 const char * str;
24
25 if (bool_styles[style] != -1)
26 return bool_styles[style];
27
28 str = gnt_style_get(style);
29
30 if (str)
31 {
32 if (strcmp(str, "false") == 0)
33 def = FALSE;
34 else if (strcmp(str, "true") == 0)
35 def = TRUE;
36 else if (sscanf(str, "%d", &i) == 1)
37 {
38 if (i)
39 def = TRUE;
40 else
41 def = FALSE;
42 }
43 }
44
45 bool_styles[style] = def;
46 return bool_styles[style];
47 }
48
49 static void
50 refine(char *text)
51 {
52 char *s = text, *t = text;
53
54 while (*s)
55 {
56 if (*s == '^' && *(s + 1) == '[')
57 {
58 *t = '\033'; /* escape */
59 s++;
60 }
61 else if (*s == '\\')
62 {
63 if (*(s + 1) == '\0')
64 *t = ' ';
65 else
66 {
67 s++;
68 if (*s == 'r' || *s == 'n')
69 *t = '\r';
70 else if (*s == 't')
71 *t = '\t';
72 else
73 *t = *s;
74 }
75 }
76 else
77 *t = *s;
78 t++;
79 s++;
80 }
81 *t = '\0';
82 }
83
84 static char *
85 parse_key(const char *key)
86 {
87 return (char *)gnt_key_translate(key);
88 }
89
90 void gnt_style_read_actions(GType type, GntBindableClass *klass)
91 {
92 #if GLIB_CHECK_VERSION(2,6,0)
93 char *name;
94 GError *error = NULL;
95
96 name = g_strdup_printf("%s::binding", g_type_name(type));
97
98 if (g_key_file_has_group(gkfile, name))
99 {
100 gsize len = 0;
101 char **keys;
102
103 keys = g_key_file_get_keys(gkfile, name, &len, &error);
104 if (error)
105 {
106 g_printerr("GntStyle: %s\n", error->message);
107 g_error_free(error);
108 g_free(name);
109 return;
110 }
111
112 while (len--)
113 {
114 char *key, *action;
115
116 key = g_strdup(keys[len]);
117 action = g_key_file_get_string(gkfile, name, keys[len], &error);
118
119 if (error)
120 {
121 g_printerr("GntStyle: %s\n", error->message);
122 g_error_free(error);
123 error = NULL;
124 }
125 else
126 {
127 const char *keycode = parse_key(key);
128 if (keycode == NULL) {
129 g_printerr("GntStyle: Invalid key-binding %s\n", key);
130 } else {
131 gnt_bindable_register_binding(klass, action, keycode, NULL);
132 }
133 }
134 g_free(key);
135 g_free(action);
136 }
137 g_strfreev(keys);
138 }
139 g_free(name);
140 #endif
141 }
142
143 void gnt_styles_get_keyremaps(GType type, GHashTable *hash)
144 {
145 #if GLIB_CHECK_VERSION(2,6,0)
146 char *name;
147 GError *error = NULL;
148
149 name = g_strdup_printf("%s::remap", g_type_name(type));
150
151 if (g_key_file_has_group(gkfile, name))
152 {
153 gsize len = 0;
154 char **keys;
155
156 keys = g_key_file_get_keys(gkfile, name, &len, &error);
157 if (error)
158 {
159 g_printerr("GntStyle: %s\n", error->message);
160 g_error_free(error);
161 g_free(name);
162 return;
163 }
164
165 while (len--)
166 {
167 char *key, *replace;
168
169 key = g_strdup(keys[len]);
170 replace = g_key_file_get_string(gkfile, name, keys[len], &error);
171
172 if (error)
173 {
174 g_printerr("GntStyle: %s\n", error->message);
175 g_error_free(error);
176 error = NULL;
177 g_free(key);
178 }
179 else
180 {
181 refine(key);
182 refine(replace);
183 g_hash_table_insert(hash, key, replace);
184 }
185 }
186 g_strfreev(keys);
187 }
188
189 g_free(name);
190 #endif
191 }
192
193 #if GLIB_CHECK_VERSION(2,6,0)
194 static void
195 read_general_style(GKeyFile *kfile)
196 {
197 GError *error = NULL;
198 gsize nkeys;
199 char **keys = g_key_file_get_keys(kfile, "general", &nkeys, &error);
200 int i;
201 struct
202 {
203 const char *style;
204 GntStyle en;
205 } styles[] = {{"shadow", GNT_STYLE_SHADOW},
206 {"customcolor", GNT_STYLE_COLOR},
207 {"mouse", GNT_STYLE_MOUSE},
208 {"wm", GNT_STYLE_WM},
209 {"remember_position", GNT_STYLE_REMPOS},
210 {NULL, 0}};
211
212 if (error)
213 {
214 g_printerr("GntStyle: %s\n", error->message);
215 g_error_free(error);
216 }
217 else
218 {
219 for (i = 0; styles[i].style; i++)
220 {
221 error = NULL;
222 str_styles[styles[i].en] =
223 g_key_file_get_string(kfile, "general", styles[i].style, &error);
224 }
225 }
226 g_strfreev(keys);
227 }
228 #endif
229
230 void gnt_style_read_configure_file(const char *filename)
231 {
232 #if GLIB_CHECK_VERSION(2,6,0)
233 GError *error = NULL;
234 gkfile = g_key_file_new();
235
236 if (!g_key_file_load_from_file(gkfile, filename, G_KEY_FILE_NONE, &error))
237 {
238 g_printerr("GntStyle: %s\n", error->message);
239 g_error_free(error);
240 return;
241 }
242 gnt_colors_parse(gkfile);
243 read_general_style(gkfile);
244 #endif
245 }
246
247 void gnt_init_styles()
248 {
249 int i;
250 for (i = 0; i < GNT_STYLES; i++)
251 {
252 str_styles[i] = NULL;
253 int_styles[i] = -1;
254 bool_styles[i] = -1;
255 }
256 }
257
258 void gnt_uninit_styles()
259 {
260 int i;
261 for (i = 0; i < GNT_STYLES; i++)
262 g_free(str_styles[i]);
263
264 #if GLIB_CHECK_VERSION(2,6,0)
265 g_key_file_free(gkfile);
266 #endif
267 }
268