[gaim-migrate @ 5229]
[pidgin-git.git] / src / themes.c
blob52f5b5c09b7cb97cf2f45287a4bd61eea37bd166
1 /*
2 * Themes for Gaim
4 * Copyright (C) 2003, Sean Egan <bj91704@binghamton.edu>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "gaim.h"
23 #include "ui.h"
24 #include "gtkimhtml.h"
25 #include "prpl.h"
26 #include <stdio.h>
27 #include <string.h>
28 #include <ctype.h>
29 #include <sys/stat.h>
31 #ifdef _WIN32
32 #include "win32dep.h"
33 #endif
35 struct smiley_list {
36 char *sml;
37 GSList *smileys;
38 struct smiley_list *next;
41 GSList *smiley_themes = NULL;
42 struct smiley_theme *current_smiley_theme;
44 void smiley_themeize(GtkWidget *imhtml)
46 struct smiley_list *list;
47 if (!current_smiley_theme)
48 return;
50 gtk_imhtml_remove_smileys(GTK_IMHTML(imhtml));
51 list = current_smiley_theme->list;
52 while (list) {
53 char *sml = !strcmp(list->sml, "default") ? NULL : list->sml;
54 GSList *icons = list->smileys;
55 while (icons) {
56 gtk_imhtml_associate_smiley(GTK_IMHTML(imhtml), sml, icons->data);
57 icons = icons->next;
59 list = list->next;
63 void load_smiley_theme(const char *file, gboolean load)
65 FILE *f = fopen(file, "r");
66 char buf[256];
67 char *i;
68 struct smiley_theme *theme=NULL;
69 struct smiley_list *list = NULL;
70 GSList *lst = smiley_themes;
71 char *dirname;
73 if (!f)
74 return;
76 while (lst) {
77 struct smiley_theme *thm = lst->data;
78 if (!strcmp(thm->path, file)) {
79 theme = thm;
80 break;
82 lst = lst->next;
85 if (!theme) {
86 theme = g_new0(struct smiley_theme, 1);
87 theme->path = g_strdup(file);
88 smiley_themes = g_slist_append(smiley_themes, theme);
91 dirname = g_path_get_dirname(file);
92 if (load) {
93 if (current_smiley_theme) {
94 GSList *already_freed = NULL;
95 struct smiley_list *wer = current_smiley_theme->list, *wer2;
96 while (wer) {
97 while (wer->smileys) {
98 GtkIMHtmlSmiley *uio = wer->smileys->data;
99 if (uio->icon)
100 g_object_unref(uio->icon);
101 if (!g_slist_find(already_freed, uio->file)) {
102 g_free(uio->file);
103 already_freed = g_slist_append(already_freed, uio->file);
105 g_free(uio->smile);
106 g_free(uio);
107 wer->smileys=g_slist_remove(wer->smileys, uio);
109 wer2 = wer->next;
110 g_free(wer->sml);
111 g_free(wer);
112 wer = wer2;
114 current_smiley_theme->list = NULL;
115 g_slist_free(already_freed);
117 current_smiley_theme = theme;
121 while (!feof(f)) {
122 if (!fgets(buf, sizeof(buf), f)) {
123 break;
126 if (buf[0] == '#' || buf[0] == '\0')
127 continue;
129 i = buf;
130 while (isspace(*i))
131 i++;
133 if (*i == '[' && strchr(i, ']') && load) {
134 struct smiley_list *child = g_new0(struct smiley_list, 1);
135 child->sml = g_strndup(i+1, (int)strchr(i, ']') - (int)i - 1);
136 if (theme->list)
137 list->next = child;
138 else
139 theme->list = child;
140 list = child;
141 } else if (!g_ascii_strncasecmp(i, "Name=", strlen("Name="))) {
142 if(theme->name)
143 g_free(theme->name);
144 theme->name = g_strdup(i+ strlen("Name="));
145 theme->name[strlen(theme->name)-1] = 0;
146 } else if (!g_ascii_strncasecmp(i, "Description=", strlen("Description="))) {
147 if(theme->desc)
148 g_free(theme->desc);
149 theme->desc = g_strdup(i + strlen("Description="));
150 theme->desc[strlen(theme->desc)-1] = 0;
151 } else if (!g_ascii_strncasecmp(i, "Icon=", strlen("Icon="))) {
152 if(theme->icon)
153 g_free(theme->icon);
154 theme->icon = g_build_filename(dirname, i + strlen("Icon="), NULL);
155 theme->icon[strlen(theme->icon)-1] = 0;
156 } else if (!g_ascii_strncasecmp(i, "Author=", strlen("Author="))) {
157 if(theme->author)
158 g_free(theme->author);
159 theme->author = g_strdup(i + strlen("Author="));
160 theme->author[strlen(theme->author)-1] = 0;
161 } else if (load && list) {
162 gboolean hidden = FALSE;
163 char *sfile = NULL;
165 if (*i == '!' && *(i + 1) == ' ') {
166 hidden = TRUE;
167 i = i + 2;
169 while (*i) {
170 char l[64];
171 int li = 0;
172 while (!isspace(*i))
173 l[li++] = *(i++);
174 if (!sfile) {
175 l[li] = 0;
176 sfile = g_build_filename(dirname, l, NULL);
177 } else {
178 GtkIMHtmlSmiley *smiley = g_new0(GtkIMHtmlSmiley, 1);
179 l[li] = 0;
180 smiley->file = sfile;
181 smiley->smile = g_strdup(l);
182 smiley->hidden = hidden;
183 list->smileys = g_slist_append(list->smileys, smiley);
185 while (isspace(*i))
186 i++;
192 if (load) {
193 GList *cnv;
195 for (cnv = gaim_get_conversations(); cnv != NULL; cnv = cnv->next) {
196 struct gaim_conversation *conv = cnv->data;
198 if (GAIM_IS_GTK_CONVERSATION(conv))
199 smiley_themeize(GAIM_GTK_CONVERSATION(conv)->imhtml);
203 g_free(dirname);
206 void smiley_theme_probe()
208 GDir *dir;
209 const gchar *file;
210 gchar *path;
211 int l;
213 char* probedirs[3];
214 probedirs[0] = g_build_filename(DATADIR, "pixmaps", "gaim", "smileys", NULL);
215 probedirs[1] = g_build_filename(gaim_user_dir(), "smileys", NULL);
216 probedirs[2] = 0;
217 for (l=0; probedirs[l]; l++) {
218 dir = g_dir_open(probedirs[l], 0, NULL);
219 if (dir) {
220 while ((file = g_dir_read_name(dir))) {
221 path = g_build_filename(probedirs[l], file, "theme", NULL);
223 /* Here we check to see that the theme has proper syntax.
224 * We set the second argument to FALSE so that it doesn't load
225 * the theme yet.
227 load_smiley_theme(path, FALSE);
228 g_free(path);
230 g_dir_close(dir);
231 } else if (l == 1) {
232 mkdir(probedirs[l], S_IRUSR | S_IWUSR | S_IXUSR);
234 g_free(probedirs[l]);
238 GSList *get_proto_smileys(int protocol) {
239 struct prpl *proto = find_prpl(protocol);
240 struct smiley_list *list, *def;
242 if(!current_smiley_theme)
243 return NULL;
245 def = list = current_smiley_theme->list;
247 while(list) {
248 if(!strcmp(list->sml, "default"))
249 def = list;
250 else if(proto && !strcmp(proto->name, list->sml))
251 break;
253 list = list->next;
256 return list ? list->smileys : def->smileys;