Wed, 22 Jan 2003 23:46:59 +0000
[gaim-migrate @ 4667]
Made use of the new GAIM_IS_GTK_WINDOW() and GAIM_IS_GTK_CONVERSATION()
macros.
| 4263 | 1 | /* |
| 2 | * Themes for Gaim | |
| 3 | * | |
| 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. | |
| 10 | * | |
| 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. | |
| 15 | * | |
| 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 | |
| 19 | * | |
| 20 | */ | |
| 21 | ||
| 4288 | 22 | #include "gaim.h" |
| 23 | #include "ui.h" | |
| 4263 | 24 | #include "gtkimhtml.h" |
| 25 | #include <stdio.h> | |
| 4298 | 26 | #include <string.h> |
| 27 | #include <ctype.h> | |
| 4341 | 28 | #include <sys/stat.h> |
| 4298 | 29 | #include "gaim.h" |
| 4263 | 30 | |
|
4321
a8ad659e5ea2
[gaim-migrate @ 4576]
Herman Bloggs <herman@bluedigits.com>
parents:
4301
diff
changeset
|
31 | #ifdef _WIN32 |
|
a8ad659e5ea2
[gaim-migrate @ 4576]
Herman Bloggs <herman@bluedigits.com>
parents:
4301
diff
changeset
|
32 | #include "win32dep.h" |
|
a8ad659e5ea2
[gaim-migrate @ 4576]
Herman Bloggs <herman@bluedigits.com>
parents:
4301
diff
changeset
|
33 | #endif |
|
a8ad659e5ea2
[gaim-migrate @ 4576]
Herman Bloggs <herman@bluedigits.com>
parents:
4301
diff
changeset
|
34 | |
| 4263 | 35 | struct smiley_list { |
| 36 | char *sml; | |
| 37 | GSList *smileys; | |
| 38 | struct smiley_list *next; | |
| 39 | }; | |
| 40 | ||
| 41 | GSList *smiley_themes; | |
| 4288 | 42 | struct smiley_theme *current_smiley_theme; |
| 4263 | 43 | |
| 44 | void smiley_themeize(GtkWidget *imhtml) | |
| 45 | { | |
| 46 | struct smiley_list *list; | |
| 47 | if (!current_smiley_theme) | |
| 48 | return; | |
| 49 | ||
| 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; | |
| 58 | } | |
| 59 | list = list->next; | |
| 60 | } | |
| 61 | } | |
| 62 | ||
| 4298 | 63 | struct smiley_theme *load_smiley_theme(const char *file, gboolean load) |
| 4263 | 64 | { |
| 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; | |
| 4323 | 72 | gboolean old=FALSE; |
| 4263 | 73 | |
| 74 | while (lst) { | |
| 75 | struct smiley_theme *thm = lst->data; | |
| 4288 | 76 | if (!strcmp(thm->path, file)) { |
| 4263 | 77 | theme = thm; |
| 4323 | 78 | old = TRUE; |
| 4263 | 79 | break; |
| 80 | } | |
| 81 | lst = lst->next; | |
| 82 | } | |
| 83 | if (!theme) { | |
| 84 | theme = g_new0(struct smiley_theme, 1); | |
| 4288 | 85 | theme->path = g_strdup(file); |
| 4263 | 86 | } |
| 87 | if (!f) | |
| 88 | return NULL; | |
| 89 | ||
| 90 | dirname = g_path_get_dirname(file); | |
| 91 | if (load) { | |
| 92 | if (current_smiley_theme) { | |
| 4288 | 93 | GSList *already_freed = NULL; |
| 4263 | 94 | struct smiley_list *wer = current_smiley_theme->list; |
| 95 | while (wer) { | |
| 4288 | 96 | GSList *already_freed = NULL; |
| 97 | while (wer->smileys) { | |
| 98 | GtkIMHtmlSmiley *uio = wer->smileys->data; | |
| 4263 | 99 | if (uio->icon) |
| 100 | g_object_unref(uio->icon); | |
| 4288 | 101 | if (!g_slist_find(already_freed, uio->file)) { |
| 102 | g_free(uio->file); | |
| 103 | already_freed = g_slist_append(already_freed, uio->file); | |
| 104 | } | |
| 4263 | 105 | g_free(uio->smile); |
| 4288 | 106 | g_free(uio); |
| 107 | wer->smileys=g_slist_remove(wer->smileys, uio); | |
| 4263 | 108 | } |
| 109 | wer = wer->next; | |
| 110 | } | |
| 4288 | 111 | current_smiley_theme->list = NULL; |
| 112 | g_slist_free(already_freed); | |
| 4263 | 113 | } |
| 4288 | 114 | current_smiley_theme = theme; |
| 4263 | 115 | } |
| 116 | ||
| 117 | ||
| 118 | while (!feof(f)) { | |
| 119 | if (!fgets(buf, sizeof(buf), f)) { | |
| 4288 | 120 | break; |
| 4263 | 121 | } |
| 122 | ||
| 123 | if (buf[0] == '#' || buf[0] == '\0') | |
| 124 | continue; | |
| 125 | ||
| 126 | i = buf; | |
| 127 | while (isspace(*i)) | |
| 128 | i++; | |
| 129 | ||
| 130 | if (*i == '[' && strchr(i, ']') && load) { | |
| 131 | struct smiley_list *child = g_new0(struct smiley_list, 1); | |
| 132 | child->sml = g_strndup(i+1, (int)strchr(i, ']') - (int)i - 1); | |
| 133 | if (theme->list) | |
| 134 | list->next = child; | |
| 135 | else | |
| 136 | theme->list = child; | |
| 137 | list = child; | |
| 138 | } else if (!g_strncasecmp(i, "Name=", strlen("Name="))) { | |
| 139 | theme->name = g_strdup(i+ strlen("Name=")); | |
| 4288 | 140 | theme->name[strlen(theme->name)-1] = 0; |
| 4263 | 141 | } else if (!g_strncasecmp(i, "Description=", strlen("Description="))) { |
| 142 | theme->desc = g_strdup(i + strlen("Description=")); | |
| 4288 | 143 | theme->desc[strlen(theme->desc)-1] = 0; |
| 4263 | 144 | } else if (!g_strncasecmp(i, "Icon=", strlen("Icon="))) { |
| 145 | theme->icon = g_build_filename(dirname, i + strlen("Icon="), NULL); | |
| 4288 | 146 | theme->icon[strlen(theme->icon)-1] = 0; |
| 147 | } else if (!g_strncasecmp(i, "Author=", strlen("Author="))) { | |
| 148 | theme->author = g_strdup(i + strlen("Author=")); | |
| 149 | theme->author[strlen(theme->author)-1] = 0; | |
| 4263 | 150 | } else if (load && list) { |
| 151 | gboolean hidden; | |
| 4288 | 152 | char *sfile = NULL; |
| 4263 | 153 | GtkIMHtmlSmiley *smiley = g_new0(GtkIMHtmlSmiley, 1); |
| 154 | ||
| 4266 | 155 | if (*i == '!' && *(i + 1) == ' ') { |
| 4263 | 156 | hidden = TRUE; |
| 157 | i = i + 2; | |
| 158 | } | |
| 159 | while (*i) { | |
| 160 | char l[64]; | |
| 161 | int li = 0; | |
| 162 | while (!isspace(*i)) | |
| 163 | l[li++] = *(i++); | |
| 4288 | 164 | if (!sfile) { |
| 4263 | 165 | l[li] = 0; |
| 4288 | 166 | sfile = g_build_filename(dirname, l, NULL); |
| 4263 | 167 | } else { |
| 168 | l[li] = 0; | |
| 169 | smiley = g_new0(GtkIMHtmlSmiley, 1); | |
| 4288 | 170 | smiley->file = sfile; |
| 4263 | 171 | smiley->smile = g_strdup(l); |
| 172 | list->smileys = g_slist_append(list->smileys, smiley); | |
| 173 | } | |
| 4288 | 174 | while (isspace(*i)) |
| 4263 | 175 | i++; |
| 4288 | 176 | |
| 4263 | 177 | } |
| 178 | } | |
| 179 | } | |
| 4288 | 180 | |
| 181 | if (load) { | |
|
4359
cf899ee07d1d
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4341
diff
changeset
|
182 | GList *cnv; |
|
cf899ee07d1d
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4341
diff
changeset
|
183 | |
|
4375
1788e74229eb
[gaim-migrate @ 4641]
Christian Hammond <chipx86@chipx86.com>
parents:
4359
diff
changeset
|
184 | for (cnv = gaim_get_conversations(); cnv != NULL; cnv = cnv->next) { |
|
4359
cf899ee07d1d
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4341
diff
changeset
|
185 | struct gaim_conversation *conv = cnv->data; |
|
cf899ee07d1d
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4341
diff
changeset
|
186 | |
|
4398
ba901bb913e5
[gaim-migrate @ 4667]
Christian Hammond <chipx86@chipx86.com>
parents:
4375
diff
changeset
|
187 | if (GAIM_IS_GTK_CONVERSATION(conv)) |
|
4359
cf899ee07d1d
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4341
diff
changeset
|
188 | smiley_themeize(GAIM_GTK_CONVERSATION(conv)->imhtml); |
|
4338
e4ad226358d1
[gaim-migrate @ 4602]
Christian Hammond <chipx86@chipx86.com>
parents:
4323
diff
changeset
|
189 | } |
| 4288 | 190 | } |
| 191 | ||
| 4263 | 192 | g_free(dirname); |
| 4323 | 193 | return old ? NULL : theme; |
| 4263 | 194 | } |
| 195 | ||
| 196 | void smiley_theme_probe() | |
| 197 | { | |
| 198 | GDir *dir; | |
| 199 | const gchar *file; | |
| 200 | gchar *path; | |
| 201 | struct smiley_theme *smile; | |
| 202 | int l; | |
| 203 | ||
| 204 | char* probedirs[3]; | |
| 205 | probedirs[0] = g_build_filename(DATADIR, "pixmaps", "gaim", "smileys", NULL); | |
| 206 | probedirs[1] = g_build_filename(gaim_user_dir(), "smileys", NULL); | |
| 207 | probedirs[2] = 0; | |
| 208 | for (l=0; probedirs[l]; l++) { | |
| 209 | dir = g_dir_open(probedirs[l], 0, NULL); | |
| 210 | if (dir) { | |
| 211 | while ((file = g_dir_read_name(dir))) { | |
| 4301 | 212 | path = g_build_filename(probedirs[l], file, "theme", NULL); |
| 4263 | 213 | |
| 214 | /* Here we check to see that the theme has proper syntax. | |
| 215 | * We set the second argument to FALSE so that it doesn't load | |
| 216 | * the theme yet. | |
| 217 | */ | |
| 4298 | 218 | if ((smile = load_smiley_theme(path, FALSE))) { |
| 4263 | 219 | smiley_themes = g_slist_append(smiley_themes, smile); |
| 220 | } | |
| 221 | g_free(path); | |
| 222 | } | |
| 223 | g_dir_close(dir); | |
| 4341 | 224 | } else if (l == 1) { |
| 225 | mkdir(probedirs[l], S_IRUSR | S_IWUSR | S_IXUSR); | |
| 226 | } | |
| 4263 | 227 | g_free(probedirs[l]); |
| 228 | } | |
| 229 | } |