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