Mercurial > pidgin
comparison gtk/gtkthemes.c @ 14191:009db0b357b5
This is a hand-crafted commit to migrate across subversion revisions
16854:16861, due to some vagaries of the way the original renames were
done. Witness that monotone can do in one revision what svn had to
spread across several.
author | Ethan Blanton <elb@pidgin.im> |
---|---|
date | Sat, 16 Dec 2006 04:59:55 +0000 |
parents | |
children | 84b215400b6b |
comparison
equal
deleted
inserted
replaced
14190:366be2ce35a7 | 14191:009db0b357b5 |
---|---|
1 /* | |
2 * Themes for Gaim | |
3 * | |
4 * Gaim 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. | |
7 * | |
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. | |
12 * | |
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. | |
17 * | |
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 * | |
22 */ | |
23 #include "internal.h" | |
24 #include "gtkgaim.h" | |
25 | |
26 #include "conversation.h" | |
27 #include "debug.h" | |
28 #include "prpl.h" | |
29 #include "util.h" | |
30 | |
31 #include "gtkconv.h" | |
32 #include "gtkdialogs.h" | |
33 #include "gtkimhtml.h" | |
34 #include "gtkthemes.h" | |
35 | |
36 GSList *smiley_themes = NULL; | |
37 struct smiley_theme *current_smiley_theme; | |
38 | |
39 gboolean gaim_gtkthemes_smileys_disabled() | |
40 { | |
41 if (!current_smiley_theme) | |
42 return 1; | |
43 | |
44 return strcmp(current_smiley_theme->name, "none") == 0; | |
45 } | |
46 | |
47 void gaim_gtkthemes_smiley_themeize(GtkWidget *imhtml) | |
48 { | |
49 struct smiley_list *list; | |
50 if (!current_smiley_theme) | |
51 return; | |
52 | |
53 gtk_imhtml_remove_smileys(GTK_IMHTML(imhtml)); | |
54 list = current_smiley_theme->list; | |
55 while (list) { | |
56 char *sml = !strcmp(list->sml, "default") ? NULL : list->sml; | |
57 GSList *icons = list->smileys; | |
58 while (icons) { | |
59 gtk_imhtml_associate_smiley(GTK_IMHTML(imhtml), sml, icons->data); | |
60 icons = icons->next; | |
61 } | |
62 list = list->next; | |
63 } | |
64 } | |
65 | |
66 void gaim_gtkthemes_load_smiley_theme(const char *file, gboolean load) | |
67 { | |
68 FILE *f = g_fopen(file, "r"); | |
69 char buf[256]; | |
70 char *i; | |
71 struct smiley_theme *theme=NULL; | |
72 struct smiley_list *list = NULL; | |
73 GSList *lst = smiley_themes; | |
74 char *dirname; | |
75 gboolean new_theme = FALSE; | |
76 | |
77 if (!f) | |
78 return; | |
79 | |
80 while (lst) { | |
81 struct smiley_theme *thm = lst->data; | |
82 if (!strcmp(thm->path, file)) { | |
83 theme = thm; | |
84 break; | |
85 } | |
86 lst = lst->next; | |
87 } | |
88 | |
89 if (!theme) { | |
90 new_theme = TRUE; | |
91 theme = g_new0(struct smiley_theme, 1); | |
92 theme->path = g_strdup(file); | |
93 } else if (theme == current_smiley_theme) { | |
94 /* Don't reload the theme if it is already loaded */ | |
95 fclose(f); | |
96 return; | |
97 } | |
98 | |
99 dirname = g_path_get_dirname(file); | |
100 | |
101 while (!feof(f)) { | |
102 if (!fgets(buf, sizeof(buf), f)) { | |
103 break; | |
104 } | |
105 | |
106 if (buf[0] == '#' || buf[0] == '\0') | |
107 continue; | |
108 | |
109 i = buf; | |
110 while (isspace(*i)) | |
111 i++; | |
112 | |
113 if (*i == '[' && strchr(i, ']') && load) { | |
114 struct smiley_list *child = g_new0(struct smiley_list, 1); | |
115 child->sml = g_strndup(i+1, strchr(i, ']') - i - 1); | |
116 if (theme->list) | |
117 list->next = child; | |
118 else | |
119 theme->list = child; | |
120 list = child; | |
121 } else if (!g_ascii_strncasecmp(i, "Name=", strlen("Name="))) { | |
122 g_free(theme->name); | |
123 theme->name = g_strdup(i+ strlen("Name=")); | |
124 theme->name[strlen(theme->name)-1] = 0; | |
125 } else if (!g_ascii_strncasecmp(i, "Description=", strlen("Description="))) { | |
126 g_free(theme->desc); | |
127 theme->desc = g_strdup(i + strlen("Description=")); | |
128 theme->desc[strlen(theme->desc)-1] = 0; | |
129 } else if (!g_ascii_strncasecmp(i, "Icon=", strlen("Icon="))) { | |
130 g_free(theme->icon); | |
131 theme->icon = g_build_filename(dirname, i + strlen("Icon="), NULL); | |
132 theme->icon[strlen(theme->icon)-1] = 0; | |
133 } else if (!g_ascii_strncasecmp(i, "Author=", strlen("Author="))) { | |
134 g_free(theme->author); | |
135 theme->author = g_strdup(i + strlen("Author=")); | |
136 theme->author[strlen(theme->author)-1] = 0; | |
137 } else if (load && list) { | |
138 gboolean hidden = FALSE; | |
139 char *sfile = NULL; | |
140 | |
141 if (*i == '!' && *(i + 1) == ' ') { | |
142 hidden = TRUE; | |
143 i = i + 2; | |
144 } | |
145 while (*i) { | |
146 char l[64]; | |
147 int li = 0; | |
148 while (!isspace(*i) && li < sizeof(l) - 1) { | |
149 if (*i == '\\' && *(i+1) != '\0' && *(i+1) != '\n' && *(i+1) != '\r') | |
150 i++; | |
151 l[li++] = *(i++); | |
152 } | |
153 if (!sfile) { | |
154 l[li] = 0; | |
155 sfile = g_build_filename(dirname, l, NULL); | |
156 } else { | |
157 GtkIMHtmlSmiley *smiley = g_new0(GtkIMHtmlSmiley, 1); | |
158 l[li] = 0; | |
159 smiley->file = sfile; | |
160 smiley->smile = g_strdup(l); | |
161 smiley->hidden = hidden; | |
162 list->smileys = g_slist_append(list->smileys, smiley); | |
163 } | |
164 while (isspace(*i)) | |
165 i++; | |
166 | |
167 } | |
168 } | |
169 } | |
170 | |
171 g_free(dirname); | |
172 fclose(f); | |
173 | |
174 if (!theme->name || !theme->desc || !theme->author) { | |
175 GSList *already_freed = NULL; | |
176 struct smiley_list *wer = theme->list, *wer2; | |
177 | |
178 gaim_debug_error("gtkthemes", "Invalid file format, not loading smiley theme from '%s'\n", file); | |
179 | |
180 while (wer) { | |
181 while (wer->smileys) { | |
182 GtkIMHtmlSmiley *uio = wer->smileys->data; | |
183 if (uio->icon) | |
184 g_object_unref(uio->icon); | |
185 if (!g_slist_find(already_freed, uio->file)) { | |
186 g_free(uio->file); | |
187 already_freed = g_slist_append(already_freed, uio->file); | |
188 } | |
189 g_free(uio->smile); | |
190 g_free(uio); | |
191 wer->smileys = g_slist_remove(wer->smileys, uio); | |
192 } | |
193 wer2 = wer->next; | |
194 g_free(wer->sml); | |
195 g_free(wer); | |
196 wer = wer2; | |
197 } | |
198 theme->list = NULL; | |
199 g_slist_free(already_freed); | |
200 | |
201 g_free(theme->name); | |
202 g_free(theme->desc); | |
203 g_free(theme->author); | |
204 g_free(theme->icon); | |
205 g_free(theme->path); | |
206 g_free(theme); | |
207 | |
208 return; | |
209 } | |
210 | |
211 if (new_theme) { | |
212 smiley_themes = g_slist_append(smiley_themes, theme); | |
213 } | |
214 | |
215 if (load) { | |
216 GList *cnv; | |
217 | |
218 if (current_smiley_theme) { | |
219 GSList *already_freed = NULL; | |
220 struct smiley_list *wer = current_smiley_theme->list, *wer2; | |
221 while (wer) { | |
222 while (wer->smileys) { | |
223 GtkIMHtmlSmiley *uio = wer->smileys->data; | |
224 if (uio->icon) | |
225 g_object_unref(uio->icon); | |
226 if (!g_slist_find(already_freed, uio->file)) { | |
227 g_free(uio->file); | |
228 already_freed = g_slist_append(already_freed, uio->file); | |
229 } | |
230 g_free(uio->smile); | |
231 g_free(uio); | |
232 wer->smileys = g_slist_remove(wer->smileys, uio); | |
233 } | |
234 wer2 = wer->next; | |
235 g_free(wer->sml); | |
236 g_free(wer); | |
237 wer = wer2; | |
238 } | |
239 current_smiley_theme->list = NULL; | |
240 g_slist_free(already_freed); | |
241 } | |
242 current_smiley_theme = theme; | |
243 | |
244 for (cnv = gaim_get_conversations(); cnv != NULL; cnv = cnv->next) { | |
245 GaimConversation *conv = cnv->data; | |
246 | |
247 if (GAIM_IS_GTK_CONVERSATION(conv)) { | |
248 gaim_gtkthemes_smiley_themeize(GAIM_GTK_CONVERSATION(conv)->imhtml); | |
249 gaim_gtkthemes_smiley_themeize(GAIM_GTK_CONVERSATION(conv)->entry); | |
250 } | |
251 } | |
252 } | |
253 } | |
254 | |
255 void gaim_gtkthemes_smiley_theme_probe() | |
256 { | |
257 GDir *dir; | |
258 const gchar *file; | |
259 gchar *path; | |
260 int l; | |
261 | |
262 char* probedirs[3]; | |
263 probedirs[0] = g_build_filename(DATADIR, "pixmaps", "gaim", "smileys", NULL); | |
264 probedirs[1] = g_build_filename(gaim_user_dir(), "smileys", NULL); | |
265 probedirs[2] = 0; | |
266 for (l=0; probedirs[l]; l++) { | |
267 dir = g_dir_open(probedirs[l], 0, NULL); | |
268 if (dir) { | |
269 while ((file = g_dir_read_name(dir))) { | |
270 path = g_build_filename(probedirs[l], file, "theme", NULL); | |
271 | |
272 /* Here we check to see that the theme has proper syntax. | |
273 * We set the second argument to FALSE so that it doesn't load | |
274 * the theme yet. | |
275 */ | |
276 gaim_gtkthemes_load_smiley_theme(path, FALSE); | |
277 g_free(path); | |
278 } | |
279 g_dir_close(dir); | |
280 } else if (l == 1) { | |
281 g_mkdir(probedirs[l], S_IRUSR | S_IWUSR | S_IXUSR); | |
282 } | |
283 g_free(probedirs[l]); | |
284 } | |
285 } | |
286 | |
287 GSList *gaim_gtkthemes_get_proto_smileys(const char *id) { | |
288 GaimPlugin *proto; | |
289 struct smiley_list *list, *def; | |
290 | |
291 if ((current_smiley_theme == NULL) || (current_smiley_theme->list == NULL)) | |
292 return NULL; | |
293 | |
294 def = list = current_smiley_theme->list; | |
295 | |
296 if (id == NULL) | |
297 return def->smileys; | |
298 | |
299 proto = gaim_find_prpl(id); | |
300 | |
301 while (list) { | |
302 if (!strcmp(list->sml, "default")) | |
303 def = list; | |
304 else if (proto && !strcmp(proto->info->name, list->sml)) | |
305 break; | |
306 | |
307 list = list->next; | |
308 } | |
309 | |
310 return list ? list->smileys : def->smileys; | |
311 } | |
312 | |
313 void gaim_gtkthemes_init() | |
314 { | |
315 GSList *l; | |
316 const char *current_theme = | |
317 gaim_prefs_get_string("/gaim/gtk/smileys/theme"); | |
318 | |
319 gaim_gtkthemes_smiley_theme_probe(); | |
320 | |
321 for (l = smiley_themes; l; l = l->next) { | |
322 struct smiley_theme *smile = l->data; | |
323 if (smile->name && strcmp(current_theme, smile->name) == 0) { | |
324 gaim_gtkthemes_load_smiley_theme(smile->path, TRUE); | |
325 break; | |
326 } | |
327 } | |
328 | |
329 /* If we still don't have a smiley theme, choose the first one */ | |
330 if (!current_smiley_theme && smiley_themes) { | |
331 struct smiley_theme *smile = smiley_themes->data; | |
332 gaim_gtkthemes_load_smiley_theme(smile->path, TRUE); | |
333 } | |
334 | |
335 } |