Wed, 13 May 2009 20:29:03 +0000
Support custom smileys in MUCs (when all participants support BoB and a maximum
of 10 participants are in the chat).
Always announce support for BoB, since disable custom smileys will still turn
off fetching them, and BoB can be used for other purposes further on.
| 10229 | 1 | /** |
| 15884 | 2 | * @file purple-desktop-item.c Functions for managing .desktop files |
| 10229 | 3 | * @ingroup core |
|
20147
66f05a854eee
applied changes from 8a731bbd0197fbcc91a705c2d8f528154216defa
Richard Laager <rlaager@pidgin.im>
parents:
19859
diff
changeset
|
4 | */ |
|
66f05a854eee
applied changes from 8a731bbd0197fbcc91a705c2d8f528154216defa
Richard Laager <rlaager@pidgin.im>
parents:
19859
diff
changeset
|
5 | |
|
66f05a854eee
applied changes from 8a731bbd0197fbcc91a705c2d8f528154216defa
Richard Laager <rlaager@pidgin.im>
parents:
19859
diff
changeset
|
6 | /* Purple is the legal property of its developers, whose names are too numerous |
| 10229 | 7 | * to list here. Please refer to the COPYRIGHT file distributed with this |
| 8 | * source distribution. | |
| 9 | * | |
| 10 | * This program is free software; you can redistribute it and/or modify | |
| 11 | * it under the terms of the GNU General Public License as published by | |
| 12 | * the Free Software Foundation; either version 2 of the License, or | |
| 13 | * (at your option) any later version. | |
| 14 | * | |
| 15 | * This program is distributed in the hope that it will be useful, | |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 18 | * GNU General Public License for more details. | |
| 19 | * | |
| 20 | * You should have received a copy of the GNU General Public License | |
| 21 | * along with this program; if not, write to the Free Software | |
|
19859
71d37b57eff2
The FSF changed its address a while ago; our files were out of date.
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
15884
diff
changeset
|
22 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA |
| 10229 | 23 | * |
| 24 | */ | |
| 25 | ||
| 26 | /* | |
| 27 | * The following code has been adapted from gnome-desktop-item.[ch], | |
| 28 | * as found on gnome-desktop-2.8.1. | |
| 29 | * | |
| 30 | * Copyright (C) 2004 by Alceste Scalas <alceste.scalas@gmx.net>. | |
| 31 | * | |
| 32 | * Original copyright notice: | |
| 33 | * | |
| 34 | * Copyright (C) 1999, 2000 Red Hat Inc. | |
| 35 | * Copyright (C) 2001 Sid Vicious | |
| 36 | * All rights reserved. | |
| 37 | * | |
| 38 | * This file is part of the Gnome Library. | |
| 39 | * | |
| 40 | * The Gnome Library is free software; you can redistribute it and/or | |
| 41 | * modify it under the terms of the GNU Library General Public License as | |
| 42 | * published by the Free Software Foundation; either version 2 of the | |
| 43 | * License, or (at your option) any later version. | |
|
25888
d0fdd378a635
Remove trailing whitespace
Mark Doliner <markdoliner@pidgin.im>
parents:
25859
diff
changeset
|
44 | * |
| 10229 | 45 | * The Gnome Library is distributed in the hope that it will be useful, |
| 46 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 47 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 48 | * Library General Public License for more details. | |
|
25888
d0fdd378a635
Remove trailing whitespace
Mark Doliner <markdoliner@pidgin.im>
parents:
25859
diff
changeset
|
49 | * |
| 10229 | 50 | * You should have received a copy of the GNU Library General Public |
| 51 | * License along with the Gnome Library; see the file COPYING.LIB. If not, | |
|
19859
71d37b57eff2
The FSF changed its address a while ago; our files were out of date.
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
15884
diff
changeset
|
52 | * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
|
71d37b57eff2
The FSF changed its address a while ago; our files were out of date.
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
15884
diff
changeset
|
53 | * Boston, MA 02111-1301, USA. |
| 10229 | 54 | */ |
| 55 | ||
| 56 | #include <errno.h> | |
| 57 | #include <stdio.h> | |
| 58 | #include <string.h> | |
| 59 | #include <time.h> | |
| 60 | #include "desktopitem.h" | |
| 61 | #include "internal.h" | |
| 62 | ||
| 15884 | 63 | struct _PurpleDesktopItem { |
| 10229 | 64 | int refcount; |
| 65 | ||
| 66 | /* all languages used */ | |
| 67 | GList *languages; | |
| 68 | ||
| 15884 | 69 | PurpleDesktopItemType type; |
|
25888
d0fdd378a635
Remove trailing whitespace
Mark Doliner <markdoliner@pidgin.im>
parents:
25859
diff
changeset
|
70 | |
| 10229 | 71 | /* `modified' means that the ditem has been |
| 72 | * modified since the last save. */ | |
| 73 | gboolean modified; | |
| 74 | ||
| 75 | /* Keys of the main section only */ | |
| 76 | GList *keys; | |
| 77 | ||
| 78 | GList *sections; | |
| 79 | ||
| 80 | /* This includes ALL keys, including | |
| 81 | * other sections, separated by '/' */ | |
| 82 | GHashTable *main_hash; | |
| 83 | ||
| 84 | char *location; | |
| 85 | ||
| 86 | time_t mtime; | |
| 87 | }; | |
| 88 | ||
| 89 | typedef struct { | |
| 90 | char *name; | |
| 91 | GList *keys; | |
| 92 | } Section; | |
| 93 | ||
| 94 | typedef enum { | |
| 95 | ENCODING_UNKNOWN, | |
| 96 | ENCODING_UTF8, | |
| 97 | ENCODING_LEGACY_MIXED | |
| 98 | } Encoding; | |
| 99 | ||
| 100 | /************************************************************************** | |
| 101 | * Private utility functions | |
| 102 | **************************************************************************/ | |
| 15884 | 103 | static PurpleDesktopItemType |
| 10229 | 104 | type_from_string (const char *type) |
| 105 | { | |
| 106 | if (!type) | |
| 15884 | 107 | return PURPLE_DESKTOP_ITEM_TYPE_NULL; |
| 10229 | 108 | |
| 109 | switch (type [0]) { | |
| 110 | case 'A': | |
|
25859
b42be7bb9dac
Patch from Paul Aurich to add purple_strequal to help readability and simplicity of code. Ie, don't need to negate the value of strcmp, since this does a strcmp and does the negation for us
Paul Aurich <darkrain42@pidgin.im>
parents:
21389
diff
changeset
|
111 | if (purple_strequal (type, "Application")) |
| 15884 | 112 | return PURPLE_DESKTOP_ITEM_TYPE_APPLICATION; |
| 10229 | 113 | break; |
| 114 | case 'L': | |
|
25859
b42be7bb9dac
Patch from Paul Aurich to add purple_strequal to help readability and simplicity of code. Ie, don't need to negate the value of strcmp, since this does a strcmp and does the negation for us
Paul Aurich <darkrain42@pidgin.im>
parents:
21389
diff
changeset
|
115 | if (purple_strequal (type, "Link")) |
| 15884 | 116 | return PURPLE_DESKTOP_ITEM_TYPE_LINK; |
| 10229 | 117 | break; |
| 118 | case 'F': | |
|
25859
b42be7bb9dac
Patch from Paul Aurich to add purple_strequal to help readability and simplicity of code. Ie, don't need to negate the value of strcmp, since this does a strcmp and does the negation for us
Paul Aurich <darkrain42@pidgin.im>
parents:
21389
diff
changeset
|
119 | if (purple_strequal (type, "FSDevice")) |
| 15884 | 120 | return PURPLE_DESKTOP_ITEM_TYPE_FSDEVICE; |
| 10229 | 121 | break; |
| 122 | case 'M': | |
|
25859
b42be7bb9dac
Patch from Paul Aurich to add purple_strequal to help readability and simplicity of code. Ie, don't need to negate the value of strcmp, since this does a strcmp and does the negation for us
Paul Aurich <darkrain42@pidgin.im>
parents:
21389
diff
changeset
|
123 | if (purple_strequal (type, "MimeType")) |
| 15884 | 124 | return PURPLE_DESKTOP_ITEM_TYPE_MIME_TYPE; |
| 10229 | 125 | break; |
| 126 | case 'D': | |
|
25859
b42be7bb9dac
Patch from Paul Aurich to add purple_strequal to help readability and simplicity of code. Ie, don't need to negate the value of strcmp, since this does a strcmp and does the negation for us
Paul Aurich <darkrain42@pidgin.im>
parents:
21389
diff
changeset
|
127 | if (purple_strequal (type, "Directory")) |
| 15884 | 128 | return PURPLE_DESKTOP_ITEM_TYPE_DIRECTORY; |
| 10229 | 129 | break; |
| 130 | case 'S': | |
|
25859
b42be7bb9dac
Patch from Paul Aurich to add purple_strequal to help readability and simplicity of code. Ie, don't need to negate the value of strcmp, since this does a strcmp and does the negation for us
Paul Aurich <darkrain42@pidgin.im>
parents:
21389
diff
changeset
|
131 | if (purple_strequal (type, "Service")) |
| 15884 | 132 | return PURPLE_DESKTOP_ITEM_TYPE_SERVICE; |
| 10229 | 133 | |
|
25859
b42be7bb9dac
Patch from Paul Aurich to add purple_strequal to help readability and simplicity of code. Ie, don't need to negate the value of strcmp, since this does a strcmp and does the negation for us
Paul Aurich <darkrain42@pidgin.im>
parents:
21389
diff
changeset
|
134 | else if (purple_strequal (type, "ServiceType")) |
| 15884 | 135 | return PURPLE_DESKTOP_ITEM_TYPE_SERVICE_TYPE; |
| 10229 | 136 | break; |
| 137 | default: | |
| 138 | break; | |
| 139 | } | |
| 140 | ||
| 15884 | 141 | return PURPLE_DESKTOP_ITEM_TYPE_OTHER; |
| 10229 | 142 | } |
| 143 | ||
| 144 | static Section * | |
| 15884 | 145 | find_section (PurpleDesktopItem *item, const char *section) |
| 10229 | 146 | { |
| 147 | GList *li; | |
| 148 | Section *sec; | |
| 149 | ||
| 150 | if (section == NULL) | |
| 151 | return NULL; | |
|
25859
b42be7bb9dac
Patch from Paul Aurich to add purple_strequal to help readability and simplicity of code. Ie, don't need to negate the value of strcmp, since this does a strcmp and does the negation for us
Paul Aurich <darkrain42@pidgin.im>
parents:
21389
diff
changeset
|
152 | if (purple_strequal (section, "Desktop Entry")) |
| 10229 | 153 | return NULL; |
| 154 | ||
| 155 | for (li = item->sections; li != NULL; li = li->next) { | |
| 156 | sec = li->data; | |
|
25859
b42be7bb9dac
Patch from Paul Aurich to add purple_strequal to help readability and simplicity of code. Ie, don't need to negate the value of strcmp, since this does a strcmp and does the negation for us
Paul Aurich <darkrain42@pidgin.im>
parents:
21389
diff
changeset
|
157 | if (purple_strequal (sec->name, section)) |
| 10229 | 158 | return sec; |
| 159 | } | |
| 160 | ||
| 161 | sec = g_new0 (Section, 1); | |
| 162 | sec->name = g_strdup (section); | |
| 163 | sec->keys = NULL; | |
| 164 | ||
| 165 | item->sections = g_list_append (item->sections, sec); | |
| 166 | ||
| 167 | /* Don't mark the item modified, this is just an empty section, | |
| 168 | * it won't be saved even */ | |
| 169 | ||
| 170 | return sec; | |
| 171 | } | |
| 172 | ||
| 173 | static Section * | |
| 15884 | 174 | section_from_key (PurpleDesktopItem *item, const char *key) |
| 10229 | 175 | { |
| 176 | char *p; | |
| 177 | char *name; | |
| 178 | Section *sec; | |
| 179 | ||
| 180 | if (key == NULL) | |
| 181 | return NULL; | |
| 182 | ||
| 183 | p = strchr (key, '/'); | |
| 184 | if (p == NULL) | |
| 185 | return NULL; | |
| 186 | ||
| 187 | name = g_strndup (key, p - key); | |
| 188 | ||
| 189 | sec = find_section (item, name); | |
| 190 | ||
| 191 | g_free (name); | |
| 192 | ||
| 193 | return sec; | |
| 194 | } | |
| 195 | ||
| 196 | static const char * | |
| 197 | key_basename (const char *key) | |
| 198 | { | |
| 199 | char *p = strrchr (key, '/'); | |
| 200 | if (p != NULL) | |
| 201 | return p+1; | |
| 202 | else | |
| 203 | return key; | |
| 204 | } | |
| 205 | ||
| 206 | static void | |
| 15884 | 207 | set (PurpleDesktopItem *item, const char *key, const char *value) |
| 10229 | 208 | { |
| 209 | Section *sec = section_from_key (item, key); | |
| 210 | ||
| 211 | if (sec != NULL) { | |
| 212 | if (value != NULL) { | |
| 213 | if (g_hash_table_lookup (item->main_hash, key) == NULL) | |
| 214 | sec->keys = g_list_append | |
| 215 | (sec->keys, | |
| 216 | g_strdup (key_basename (key))); | |
| 217 | ||
| 218 | g_hash_table_replace (item->main_hash, | |
| 219 | g_strdup (key), | |
| 220 | g_strdup (value)); | |
| 221 | } else { | |
| 222 | GList *list = g_list_find_custom | |
| 223 | (sec->keys, key_basename (key), | |
| 224 | (GCompareFunc)strcmp); | |
| 225 | if (list != NULL) { | |
| 226 | g_free (list->data); | |
| 227 | sec->keys = | |
| 228 | g_list_delete_link (sec->keys, list); | |
| 229 | } | |
| 230 | g_hash_table_remove (item->main_hash, key); | |
| 231 | } | |
| 232 | } else { | |
| 233 | if (value != NULL) { | |
| 234 | if (g_hash_table_lookup (item->main_hash, key) == NULL) | |
| 235 | item->keys = g_list_append (item->keys, | |
| 236 | g_strdup (key)); | |
| 237 | ||
|
25888
d0fdd378a635
Remove trailing whitespace
Mark Doliner <markdoliner@pidgin.im>
parents:
25859
diff
changeset
|
238 | g_hash_table_replace (item->main_hash, |
| 10229 | 239 | g_strdup (key), |
| 240 | g_strdup (value)); | |
| 241 | } else { | |
| 242 | GList *list = g_list_find_custom | |
| 243 | (item->keys, key, (GCompareFunc)strcmp); | |
| 244 | if (list != NULL) { | |
| 245 | g_free (list->data); | |
| 246 | item->keys = | |
| 247 | g_list_delete_link (item->keys, list); | |
| 248 | } | |
| 249 | g_hash_table_remove (item->main_hash, key); | |
| 250 | } | |
| 251 | } | |
| 252 | item->modified = TRUE; | |
| 253 | } | |
| 254 | ||
| 255 | ||
| 256 | static void | |
| 15884 | 257 | _purple_desktop_item_set_string (PurpleDesktopItem *item, |
| 10229 | 258 | const char *attr, |
| 259 | const char *value) | |
| 260 | { | |
| 261 | g_return_if_fail (item != NULL); | |
| 262 | g_return_if_fail (item->refcount > 0); | |
| 263 | g_return_if_fail (attr != NULL); | |
| 264 | ||
| 265 | set (item, attr, value); | |
| 266 | ||
|
25859
b42be7bb9dac
Patch from Paul Aurich to add purple_strequal to help readability and simplicity of code. Ie, don't need to negate the value of strcmp, since this does a strcmp and does the negation for us
Paul Aurich <darkrain42@pidgin.im>
parents:
21389
diff
changeset
|
267 | if (purple_strequal (attr, PURPLE_DESKTOP_ITEM_TYPE)) |
| 10229 | 268 | item->type = type_from_string (value); |
| 269 | } | |
| 270 | ||
| 15884 | 271 | static PurpleDesktopItem * |
| 272 | _purple_desktop_item_new (void) | |
| 10229 | 273 | { |
| 15884 | 274 | PurpleDesktopItem *retval; |
| 10229 | 275 | |
| 15884 | 276 | retval = g_new0 (PurpleDesktopItem, 1); |
| 10229 | 277 | |
| 278 | retval->refcount++; | |
| 279 | ||
| 280 | retval->main_hash = g_hash_table_new_full (g_str_hash, g_str_equal, | |
| 281 | (GDestroyNotify) g_free, | |
| 282 | (GDestroyNotify) g_free); | |
|
25888
d0fdd378a635
Remove trailing whitespace
Mark Doliner <markdoliner@pidgin.im>
parents:
25859
diff
changeset
|
283 | |
| 10229 | 284 | /* These are guaranteed to be set */ |
| 15884 | 285 | _purple_desktop_item_set_string (retval, |
| 286 | PURPLE_DESKTOP_ITEM_NAME, | |
| 10229 | 287 | _("No name")); |
| 15884 | 288 | _purple_desktop_item_set_string (retval, |
| 289 | PURPLE_DESKTOP_ITEM_ENCODING, | |
| 10229 | 290 | "UTF-8"); |
| 15884 | 291 | _purple_desktop_item_set_string (retval, |
| 292 | PURPLE_DESKTOP_ITEM_VERSION, | |
| 10229 | 293 | "1.0"); |
| 294 | ||
| 295 | return retval; | |
| 296 | } | |
| 297 | ||
| 298 | static gpointer | |
| 15884 | 299 | _purple_desktop_item_copy (gpointer boxed) |
| 10229 | 300 | { |
| 15884 | 301 | return purple_desktop_item_copy (boxed); |
| 10229 | 302 | } |
| 303 | ||
| 304 | static void | |
| 15884 | 305 | _purple_desktop_item_free (gpointer boxed) |
| 10229 | 306 | { |
| 15884 | 307 | purple_desktop_item_unref (boxed); |
| 10229 | 308 | } |
| 309 | ||
| 310 | /* Note, does not include the trailing \n */ | |
| 311 | static char * | |
| 312 | my_fgets (char *buf, gsize bufsize, FILE *df) | |
| 313 | { | |
| 314 | int c; | |
| 315 | gsize pos; | |
| 316 | ||
| 317 | g_return_val_if_fail (buf != NULL, NULL); | |
| 318 | g_return_val_if_fail (df != NULL, NULL); | |
| 319 | ||
| 320 | pos = 0; | |
| 321 | buf[0] = '\0'; | |
| 322 | ||
| 323 | do { | |
| 324 | c = getc (df); | |
| 325 | if (c == EOF || c == '\n') | |
| 326 | break; | |
| 327 | buf[pos++] = c; | |
| 328 | } while (pos < bufsize-1); | |
| 329 | ||
| 330 | if (c == EOF && pos == 0) | |
| 331 | return NULL; | |
| 332 | ||
| 333 | buf[pos++] = '\0'; | |
| 334 | ||
| 335 | return buf; | |
| 336 | } | |
| 337 | ||
| 338 | static Encoding | |
| 339 | get_encoding (FILE *df) | |
| 340 | { | |
| 341 | gboolean old_kde = FALSE; | |
| 342 | char buf [BUFSIZ]; | |
| 343 | gboolean all_valid_utf8 = TRUE; | |
| 344 | ||
| 345 | while (my_fgets (buf, sizeof (buf), df) != NULL) { | |
| 15884 | 346 | if (strncmp (PURPLE_DESKTOP_ITEM_ENCODING, |
| 10229 | 347 | buf, |
| 15884 | 348 | strlen (PURPLE_DESKTOP_ITEM_ENCODING)) == 0) { |
| 349 | char *p = &buf[strlen (PURPLE_DESKTOP_ITEM_ENCODING)]; | |
| 10229 | 350 | if (*p == ' ') |
| 351 | p++; | |
| 352 | if (*p != '=') | |
| 353 | continue; | |
| 354 | p++; | |
| 355 | if (*p == ' ') | |
| 356 | p++; | |
|
25859
b42be7bb9dac
Patch from Paul Aurich to add purple_strequal to help readability and simplicity of code. Ie, don't need to negate the value of strcmp, since this does a strcmp and does the negation for us
Paul Aurich <darkrain42@pidgin.im>
parents:
21389
diff
changeset
|
357 | if (purple_strequal (p, "UTF-8")) { |
| 10229 | 358 | return ENCODING_UTF8; |
|
25859
b42be7bb9dac
Patch from Paul Aurich to add purple_strequal to help readability and simplicity of code. Ie, don't need to negate the value of strcmp, since this does a strcmp and does the negation for us
Paul Aurich <darkrain42@pidgin.im>
parents:
21389
diff
changeset
|
359 | } else if (purple_strequal (p, "Legacy-Mixed")) { |
| 10229 | 360 | return ENCODING_LEGACY_MIXED; |
| 361 | } else { | |
| 362 | /* According to the spec we're not supposed | |
| 363 | * to read a file like this */ | |
| 364 | return ENCODING_UNKNOWN; | |
| 365 | } | |
|
25859
b42be7bb9dac
Patch from Paul Aurich to add purple_strequal to help readability and simplicity of code. Ie, don't need to negate the value of strcmp, since this does a strcmp and does the negation for us
Paul Aurich <darkrain42@pidgin.im>
parents:
21389
diff
changeset
|
366 | } else if (purple_strequal ("[KDE Desktop Entry]", buf)) { |
| 10229 | 367 | old_kde = TRUE; |
| 368 | /* don't break yet, we still want to support | |
| 369 | * Encoding even here */ | |
| 370 | } | |
| 371 | if (all_valid_utf8 && ! g_utf8_validate (buf, -1, NULL)) | |
| 372 | all_valid_utf8 = FALSE; | |
| 373 | } | |
| 374 | ||
| 375 | if (old_kde) | |
| 376 | return ENCODING_LEGACY_MIXED; | |
| 377 | ||
| 378 | /* A dilemma, new KDE files are in UTF-8 but have no Encoding | |
| 379 | * info, at this time we really can't tell. The best thing to | |
| 380 | * do right now is to just assume UTF-8 if the whole file | |
| 381 | * validates as utf8 I suppose */ | |
| 382 | ||
| 383 | if (all_valid_utf8) | |
| 384 | return ENCODING_UTF8; | |
| 385 | else | |
| 386 | return ENCODING_LEGACY_MIXED; | |
| 387 | } | |
| 388 | ||
| 389 | static char * | |
| 390 | snarf_locale_from_key (const char *key) | |
| 391 | { | |
| 392 | const char *brace; | |
| 393 | char *locale, *p; | |
| 394 | ||
| 395 | brace = strchr (key, '['); | |
| 396 | if (brace == NULL) | |
| 397 | return NULL; | |
| 398 | ||
| 399 | locale = g_strdup (brace + 1); | |
| 400 | if (*locale == '\0') { | |
| 401 | g_free (locale); | |
| 402 | return NULL; | |
| 403 | } | |
| 404 | p = strchr (locale, ']'); | |
| 405 | if (p == NULL) { | |
| 406 | g_free (locale); | |
| 407 | return NULL; | |
| 408 | } | |
| 409 | *p = '\0'; | |
| 410 | return locale; | |
| 411 | } | |
| 412 | ||
| 413 | static gboolean | |
| 414 | check_locale (const char *locale) | |
| 415 | { | |
| 416 | GIConv cd = g_iconv_open ("UTF-8", locale); | |
| 417 | if ((GIConv)-1 == cd) | |
| 418 | return FALSE; | |
| 419 | g_iconv_close (cd); | |
| 420 | return TRUE; | |
| 421 | } | |
| 422 | ||
| 423 | static void | |
| 424 | insert_locales (GHashTable *encodings, char *enc, ...) | |
| 425 | { | |
| 426 | va_list args; | |
| 427 | char *s; | |
| 428 | ||
| 429 | va_start (args, enc); | |
| 430 | for (;;) { | |
| 431 | s = va_arg (args, char *); | |
| 432 | if (s == NULL) | |
| 433 | break; | |
| 434 | g_hash_table_insert (encodings, s, enc); | |
| 435 | } | |
| 436 | va_end (args); | |
| 437 | } | |
| 438 | ||
| 439 | /* make a standard conversion table from the desktop standard spec */ | |
| 440 | static GHashTable * | |
| 441 | init_encodings (void) | |
| 442 | { | |
| 443 | GHashTable *encodings = g_hash_table_new (g_str_hash, g_str_equal); | |
| 444 | ||
| 445 | /* "C" is plain ascii */ | |
| 446 | insert_locales (encodings, "ASCII", "C", NULL); | |
| 447 | ||
| 448 | insert_locales (encodings, "ARMSCII-8", "by", NULL); | |
| 449 | insert_locales (encodings, "BIG5", "zh_TW", NULL); | |
| 450 | insert_locales (encodings, "CP1251", "be", "bg", NULL); | |
| 451 | if (check_locale ("EUC-CN")) { | |
| 452 | insert_locales (encodings, "EUC-CN", "zh_CN", NULL); | |
| 453 | } else { | |
| 454 | insert_locales (encodings, "GB2312", "zh_CN", NULL); | |
| 455 | } | |
| 456 | insert_locales (encodings, "EUC-JP", "ja", NULL); | |
| 457 | insert_locales (encodings, "EUC-KR", "ko", NULL); | |
| 458 | /*insert_locales (encodings, "GEORGIAN-ACADEMY", NULL);*/ | |
| 459 | insert_locales (encodings, "GEORGIAN-PS", "ka", NULL); | |
| 460 | insert_locales (encodings, "ISO-8859-1", "br", "ca", "da", "de", "en", "es", "eu", "fi", "fr", "gl", "it", "nl", "wa", "no", "pt", "pt", "sv", NULL); | |
| 461 | insert_locales (encodings, "ISO-8859-2", "cs", "hr", "hu", "pl", "ro", "sk", "sl", "sq", "sr", NULL); | |
| 462 | insert_locales (encodings, "ISO-8859-3", "eo", NULL); | |
| 463 | insert_locales (encodings, "ISO-8859-5", "mk", "sp", NULL); | |
| 464 | insert_locales (encodings, "ISO-8859-7", "el", NULL); | |
| 465 | insert_locales (encodings, "ISO-8859-9", "tr", NULL); | |
| 466 | insert_locales (encodings, "ISO-8859-13", "lt", "lv", "mi", NULL); | |
| 467 | insert_locales (encodings, "ISO-8859-14", "ga", "cy", NULL); | |
| 468 | insert_locales (encodings, "ISO-8859-15", "et", NULL); | |
| 469 | insert_locales (encodings, "KOI8-R", "ru", NULL); | |
| 470 | insert_locales (encodings, "KOI8-U", "uk", NULL); | |
| 471 | if (check_locale ("TCVN-5712")) { | |
| 472 | insert_locales (encodings, "TCVN-5712", "vi", NULL); | |
| 473 | } else { | |
| 474 | insert_locales (encodings, "TCVN", "vi", NULL); | |
| 475 | } | |
| 476 | insert_locales (encodings, "TIS-620", "th", NULL); | |
| 477 | /*insert_locales (encodings, "VISCII", NULL);*/ | |
| 478 | ||
| 479 | return encodings; | |
| 480 | } | |
| 481 | ||
| 482 | static const char * | |
| 483 | get_encoding_from_locale (const char *locale) | |
| 484 | { | |
| 485 | char lang[3]; | |
| 486 | const char *encoding; | |
| 487 | static GHashTable *encodings = NULL; | |
| 488 | ||
| 489 | if (locale == NULL) | |
| 490 | return NULL; | |
| 491 | ||
| 492 | /* if locale includes encoding, use it */ | |
| 493 | encoding = strchr (locale, '.'); | |
| 494 | if (encoding != NULL) { | |
| 495 | return encoding+1; | |
| 496 | } | |
| 497 | ||
| 498 | if (encodings == NULL) | |
| 499 | encodings = init_encodings (); | |
| 500 | ||
| 501 | /* first try the entire locale (at this point ll_CC) */ | |
| 502 | encoding = g_hash_table_lookup (encodings, locale); | |
| 503 | if (encoding != NULL) | |
| 504 | return encoding; | |
| 505 | ||
| 506 | /* Try just the language */ | |
| 507 | strncpy (lang, locale, 2); | |
| 508 | lang[2] = '\0'; | |
| 509 | return g_hash_table_lookup (encodings, lang); | |
| 510 | } | |
| 511 | ||
| 512 | static char * | |
| 513 | decode_string_and_dup (const char *s) | |
| 514 | { | |
| 515 | char *p = g_malloc (strlen (s) + 1); | |
| 516 | char *q = p; | |
| 517 | ||
| 518 | do { | |
| 519 | if (*s == '\\'){ | |
| 520 | switch (*(++s)){ | |
| 521 | case 's': | |
| 522 | *p++ = ' '; | |
| 523 | break; | |
| 524 | case 't': | |
| 525 | *p++ = '\t'; | |
| 526 | break; | |
| 527 | case 'n': | |
| 528 | *p++ = '\n'; | |
| 529 | break; | |
| 530 | case '\\': | |
| 531 | *p++ = '\\'; | |
| 532 | break; | |
| 533 | case 'r': | |
| 534 | *p++ = '\r'; | |
| 535 | break; | |
| 536 | default: | |
| 537 | *p++ = '\\'; | |
| 538 | *p++ = *s; | |
| 539 | break; | |
| 540 | } | |
| 541 | } else { | |
| 542 | *p++ = *s; | |
| 543 | } | |
| 544 | } while (*s++); | |
| 545 | ||
| 546 | return q; | |
| 547 | } | |
| 548 | ||
| 549 | static char * | |
| 550 | decode_string (const char *value, Encoding encoding, const char *locale) | |
| 551 | { | |
| 552 | char *retval = NULL; | |
| 553 | ||
| 554 | /* if legacy mixed, then convert */ | |
| 555 | if (locale != NULL && encoding == ENCODING_LEGACY_MIXED) { | |
| 556 | const char *char_encoding = get_encoding_from_locale (locale); | |
| 557 | char *utf8_string; | |
| 558 | if (char_encoding == NULL) | |
| 559 | return NULL; | |
|
25859
b42be7bb9dac
Patch from Paul Aurich to add purple_strequal to help readability and simplicity of code. Ie, don't need to negate the value of strcmp, since this does a strcmp and does the negation for us
Paul Aurich <darkrain42@pidgin.im>
parents:
21389
diff
changeset
|
560 | if (purple_strequal (char_encoding, "ASCII")) { |
| 10229 | 561 | return decode_string_and_dup (value); |
| 562 | } | |
| 563 | utf8_string = g_convert (value, -1, "UTF-8", char_encoding, | |
| 564 | NULL, NULL, NULL); | |
| 565 | if (utf8_string == NULL) | |
| 566 | return NULL; | |
| 567 | retval = decode_string_and_dup (utf8_string); | |
| 568 | g_free (utf8_string); | |
| 569 | return retval; | |
| 570 | /* if utf8, then validate */ | |
| 571 | } else if (locale != NULL && encoding == ENCODING_UTF8) { | |
| 572 | if ( ! g_utf8_validate (value, -1, NULL)) | |
| 573 | /* invalid utf8, ignore this key */ | |
| 574 | return NULL; | |
| 575 | return decode_string_and_dup (value); | |
| 576 | } else { | |
| 577 | /* Meaning this is not a localized string */ | |
| 578 | return decode_string_and_dup (value); | |
| 579 | } | |
| 580 | } | |
| 581 | ||
| 582 | /************************************************************ | |
| 583 | * Parser: * | |
| 584 | ************************************************************/ | |
| 585 | ||
| 586 | static gboolean G_GNUC_CONST | |
| 587 | standard_is_boolean (const char * key) | |
| 588 | { | |
| 589 | static GHashTable *bools = NULL; | |
| 590 | ||
| 591 | if (bools == NULL) { | |
| 592 | bools = g_hash_table_new (g_str_hash, g_str_equal); | |
| 593 | g_hash_table_insert (bools, | |
| 15884 | 594 | PURPLE_DESKTOP_ITEM_NO_DISPLAY, |
| 595 | PURPLE_DESKTOP_ITEM_NO_DISPLAY); | |
| 10229 | 596 | g_hash_table_insert (bools, |
| 15884 | 597 | PURPLE_DESKTOP_ITEM_HIDDEN, |
| 598 | PURPLE_DESKTOP_ITEM_HIDDEN); | |
| 10229 | 599 | g_hash_table_insert (bools, |
| 15884 | 600 | PURPLE_DESKTOP_ITEM_TERMINAL, |
| 601 | PURPLE_DESKTOP_ITEM_TERMINAL); | |
| 10229 | 602 | g_hash_table_insert (bools, |
| 15884 | 603 | PURPLE_DESKTOP_ITEM_READ_ONLY, |
| 604 | PURPLE_DESKTOP_ITEM_READ_ONLY); | |
| 10229 | 605 | } |
| 606 | ||
| 607 | return g_hash_table_lookup (bools, key) != NULL; | |
| 608 | } | |
| 609 | ||
| 610 | static gboolean G_GNUC_CONST | |
| 611 | standard_is_strings (const char *key) | |
| 612 | { | |
| 613 | static GHashTable *strings = NULL; | |
| 614 | ||
| 615 | if (strings == NULL) { | |
| 616 | strings = g_hash_table_new (g_str_hash, g_str_equal); | |
| 617 | g_hash_table_insert (strings, | |
| 15884 | 618 | PURPLE_DESKTOP_ITEM_FILE_PATTERN, |
| 619 | PURPLE_DESKTOP_ITEM_FILE_PATTERN); | |
| 10229 | 620 | g_hash_table_insert (strings, |
| 15884 | 621 | PURPLE_DESKTOP_ITEM_ACTIONS, |
| 622 | PURPLE_DESKTOP_ITEM_ACTIONS); | |
| 10229 | 623 | g_hash_table_insert (strings, |
| 15884 | 624 | PURPLE_DESKTOP_ITEM_MIME_TYPE, |
| 625 | PURPLE_DESKTOP_ITEM_MIME_TYPE); | |
| 10229 | 626 | g_hash_table_insert (strings, |
| 15884 | 627 | PURPLE_DESKTOP_ITEM_PATTERNS, |
| 628 | PURPLE_DESKTOP_ITEM_PATTERNS); | |
| 10229 | 629 | g_hash_table_insert (strings, |
| 15884 | 630 | PURPLE_DESKTOP_ITEM_SORT_ORDER, |
| 631 | PURPLE_DESKTOP_ITEM_SORT_ORDER); | |
| 10229 | 632 | } |
| 633 | ||
| 634 | return g_hash_table_lookup (strings, key) != NULL; | |
| 635 | } | |
| 636 | ||
| 637 | /* If no need to cannonize, returns NULL */ | |
| 638 | static char * | |
| 639 | cannonize (const char *key, const char *value) | |
| 640 | { | |
| 641 | if (standard_is_boolean (key)) { | |
| 642 | if (value[0] == 'T' || | |
| 643 | value[0] == 't' || | |
| 644 | value[0] == 'Y' || | |
| 645 | value[0] == 'y' || | |
| 646 | atoi (value) != 0) { | |
| 647 | return g_strdup ("true"); | |
| 648 | } else { | |
| 649 | return g_strdup ("false"); | |
| 650 | } | |
| 651 | } else if (standard_is_strings (key)) { | |
| 652 | int len = strlen (value); | |
| 653 | if (len == 0 || value[len-1] != ';') { | |
| 654 | return g_strconcat (value, ";", NULL); | |
| 655 | } | |
| 656 | } | |
| 657 | /* XXX: Perhaps we should canonize numeric values as well, but this | |
| 658 | * has caused some subtle problems before so it needs to be done | |
| 659 | * carefully if at all */ | |
| 660 | return NULL; | |
| 661 | } | |
| 662 | ||
| 663 | static void | |
| 15884 | 664 | insert_key (PurpleDesktopItem *item, |
| 10229 | 665 | Section *cur_section, |
| 666 | Encoding encoding, | |
| 667 | const char *key, | |
| 668 | const char *value, | |
| 669 | gboolean old_kde, | |
| 670 | gboolean no_translations) | |
| 671 | { | |
| 672 | char *k; | |
| 673 | char *val; | |
| 674 | /* we always store everything in UTF-8 */ | |
| 675 | if (cur_section == NULL && | |
|
25859
b42be7bb9dac
Patch from Paul Aurich to add purple_strequal to help readability and simplicity of code. Ie, don't need to negate the value of strcmp, since this does a strcmp and does the negation for us
Paul Aurich <darkrain42@pidgin.im>
parents:
21389
diff
changeset
|
676 | purple_strequal (key, PURPLE_DESKTOP_ITEM_ENCODING)) { |
| 10229 | 677 | k = g_strdup (key); |
| 678 | val = g_strdup ("UTF-8"); | |
| 679 | } else { | |
| 680 | char *locale = snarf_locale_from_key (key); | |
| 681 | /* If we're ignoring translations */ | |
| 682 | if (no_translations && locale != NULL) { | |
| 683 | g_free (locale); | |
| 684 | return; | |
| 685 | } | |
| 686 | val = decode_string (value, encoding, locale); | |
| 687 | ||
| 688 | /* Ignore this key, it's whacked */ | |
| 689 | if (val == NULL) { | |
| 690 | g_free (locale); | |
| 691 | return; | |
| 692 | } | |
|
25888
d0fdd378a635
Remove trailing whitespace
Mark Doliner <markdoliner@pidgin.im>
parents:
25859
diff
changeset
|
693 | |
| 10229 | 694 | g_strchomp (val); |
| 695 | ||
| 696 | /* For old KDE entries, we can also split by a comma | |
| 697 | * on sort order, so convert to semicolons */ | |
| 698 | if (old_kde && | |
| 699 | cur_section == NULL && | |
|
25859
b42be7bb9dac
Patch from Paul Aurich to add purple_strequal to help readability and simplicity of code. Ie, don't need to negate the value of strcmp, since this does a strcmp and does the negation for us
Paul Aurich <darkrain42@pidgin.im>
parents:
21389
diff
changeset
|
700 | purple_strequal (key, PURPLE_DESKTOP_ITEM_SORT_ORDER) && |
| 10229 | 701 | strchr (val, ';') == NULL) { |
| 702 | int i; | |
| 703 | for (i = 0; val[i] != '\0'; i++) { | |
| 704 | if (val[i] == ',') | |
| 705 | val[i] = ';'; | |
| 706 | } | |
| 707 | } | |
| 708 | ||
| 709 | /* Check some types, not perfect, but catches a lot | |
| 710 | * of things */ | |
| 711 | if (cur_section == NULL) { | |
| 712 | char *cannon = cannonize (key, val); | |
| 713 | if (cannon != NULL) { | |
| 714 | g_free (val); | |
| 715 | val = cannon; | |
| 716 | } | |
| 717 | } | |
| 718 | ||
| 719 | k = g_strdup (key); | |
| 720 | ||
| 721 | /* Take care of the language part */ | |
| 722 | if (locale != NULL && | |
|
25859
b42be7bb9dac
Patch from Paul Aurich to add purple_strequal to help readability and simplicity of code. Ie, don't need to negate the value of strcmp, since this does a strcmp and does the negation for us
Paul Aurich <darkrain42@pidgin.im>
parents:
21389
diff
changeset
|
723 | purple_strequal (locale, "C")) { |
| 10229 | 724 | char *p; |
| 725 | /* Whack C locale */ | |
| 726 | p = strchr (k, '['); | |
| 13443 | 727 | if(p) *p = '\0'; |
| 10229 | 728 | g_free (locale); |
| 729 | } else if (locale != NULL) { | |
| 730 | char *p, *brace; | |
| 731 | ||
| 732 | /* Whack the encoding part */ | |
| 733 | p = strchr (locale, '.'); | |
| 734 | if (p != NULL) | |
| 735 | *p = '\0'; | |
| 13442 | 736 | |
| 10229 | 737 | if (g_list_find_custom (item->languages, locale, |
| 738 | (GCompareFunc)strcmp) == NULL) { | |
| 739 | item->languages = g_list_prepend | |
| 740 | (item->languages, locale); | |
| 741 | } else { | |
| 742 | g_free (locale); | |
| 743 | } | |
| 744 | ||
| 13442 | 745 | /* Whack encoding from encoding in the key */ |
| 10229 | 746 | brace = strchr (k, '['); |
| 13442 | 747 | if(brace != NULL) { |
| 748 | p = strchr (brace, '.'); | |
| 749 | if (p != NULL) { | |
| 750 | *p = ']'; | |
| 751 | *(p+1) = '\0'; | |
| 752 | } | |
| 10229 | 753 | } |
| 754 | } | |
| 755 | } | |
| 756 | ||
| 757 | ||
| 758 | if (cur_section == NULL) { | |
| 759 | /* only add to list if we haven't seen it before */ | |
| 760 | if (g_hash_table_lookup (item->main_hash, k) == NULL) { | |
| 761 | item->keys = g_list_prepend (item->keys, | |
| 762 | g_strdup (k)); | |
| 763 | } | |
| 764 | /* later duplicates override earlier ones */ | |
| 765 | g_hash_table_replace (item->main_hash, k, val); | |
| 766 | } else { | |
| 767 | char *full = g_strdup_printf | |
| 768 | ("%s/%s", | |
| 769 | cur_section->name, k); | |
| 770 | /* only add to list if we haven't seen it before */ | |
| 771 | if (g_hash_table_lookup (item->main_hash, full) == NULL) { | |
| 772 | cur_section->keys = | |
| 773 | g_list_prepend (cur_section->keys, k); | |
| 774 | } | |
| 775 | /* later duplicates override earlier ones */ | |
| 776 | g_hash_table_replace (item->main_hash, | |
| 777 | full, val); | |
| 778 | } | |
| 779 | } | |
| 780 | ||
| 781 | static const char * | |
| 15884 | 782 | lookup (const PurpleDesktopItem *item, const char *key) |
| 10229 | 783 | { |
| 784 | return g_hash_table_lookup (item->main_hash, key); | |
| 785 | } | |
| 786 | ||
| 787 | static void | |
| 15884 | 788 | setup_type (PurpleDesktopItem *item, const char *uri) |
| 10229 | 789 | { |
| 790 | const char *type = g_hash_table_lookup (item->main_hash, | |
| 15884 | 791 | PURPLE_DESKTOP_ITEM_TYPE); |
| 10229 | 792 | if (type == NULL && uri != NULL) { |
| 793 | char *base = g_path_get_basename (uri); | |
|
25859
b42be7bb9dac
Patch from Paul Aurich to add purple_strequal to help readability and simplicity of code. Ie, don't need to negate the value of strcmp, since this does a strcmp and does the negation for us
Paul Aurich <darkrain42@pidgin.im>
parents:
21389
diff
changeset
|
794 | if (purple_strequal(base, ".directory")) { |
| 10229 | 795 | /* This gotta be a directory */ |
| 796 | g_hash_table_replace (item->main_hash, | |
|
25888
d0fdd378a635
Remove trailing whitespace
Mark Doliner <markdoliner@pidgin.im>
parents:
25859
diff
changeset
|
797 | g_strdup (PURPLE_DESKTOP_ITEM_TYPE), |
| 10229 | 798 | g_strdup ("Directory")); |
| 799 | item->keys = g_list_prepend | |
| 15884 | 800 | (item->keys, g_strdup (PURPLE_DESKTOP_ITEM_TYPE)); |
| 801 | item->type = PURPLE_DESKTOP_ITEM_TYPE_DIRECTORY; | |
| 10229 | 802 | } else { |
| 15884 | 803 | item->type = PURPLE_DESKTOP_ITEM_TYPE_NULL; |
| 10229 | 804 | } |
| 805 | g_free (base); | |
| 806 | } else { | |
| 807 | item->type = type_from_string (type); | |
| 808 | } | |
| 809 | } | |
| 810 | ||
| 811 | static const char * | |
| 15884 | 812 | lookup_locale (const PurpleDesktopItem *item, const char *key, const char *locale) |
| 10229 | 813 | { |
| 814 | if (locale == NULL || | |
|
25859
b42be7bb9dac
Patch from Paul Aurich to add purple_strequal to help readability and simplicity of code. Ie, don't need to negate the value of strcmp, since this does a strcmp and does the negation for us
Paul Aurich <darkrain42@pidgin.im>
parents:
21389
diff
changeset
|
815 | purple_strequal (locale, "C")) { |
| 10229 | 816 | return lookup (item, key); |
| 817 | } else { | |
| 818 | const char *ret; | |
| 819 | char *full = g_strdup_printf ("%s[%s]", key, locale); | |
| 820 | ret = lookup (item, full); | |
| 821 | g_free (full); | |
| 822 | return ret; | |
| 823 | } | |
| 824 | } | |
| 825 | ||
|
26557
b1d3915b9ecd
I wrote a little comment
Mark Doliner <markdoliner@pidgin.im>
parents:
25888
diff
changeset
|
826 | /** |
|
b1d3915b9ecd
I wrote a little comment
Mark Doliner <markdoliner@pidgin.im>
parents:
25888
diff
changeset
|
827 | * Fallback to find something suitable for C locale. |
|
b1d3915b9ecd
I wrote a little comment
Mark Doliner <markdoliner@pidgin.im>
parents:
25888
diff
changeset
|
828 | * |
|
b1d3915b9ecd
I wrote a little comment
Mark Doliner <markdoliner@pidgin.im>
parents:
25888
diff
changeset
|
829 | * @return A newly allocated string which should be g_freed by the caller. |
|
b1d3915b9ecd
I wrote a little comment
Mark Doliner <markdoliner@pidgin.im>
parents:
25888
diff
changeset
|
830 | */ |
| 10229 | 831 | static char * |
| 15884 | 832 | try_english_key (PurpleDesktopItem *item, const char *key) |
| 10229 | 833 | { |
| 834 | char *str; | |
| 835 | char *locales[] = { "en_US", "en_GB", "en_AU", "en", NULL }; | |
| 836 | int i; | |
| 837 | ||
| 838 | str = NULL; | |
| 839 | for (i = 0; locales[i] != NULL && str == NULL; i++) { | |
| 840 | str = g_strdup (lookup_locale (item, key, locales[i])); | |
| 841 | } | |
| 842 | if (str != NULL) { | |
| 843 | /* We need a 7-bit ascii string, so whack all | |
| 844 | * above 127 chars */ | |
| 845 | guchar *p; | |
| 846 | for (p = (guchar *)str; *p != '\0'; p++) { | |
| 847 | if (*p > 127) | |
| 848 | *p = '?'; | |
| 849 | } | |
| 850 | } | |
| 851 | return str; | |
| 852 | } | |
| 853 | ||
| 854 | ||
| 855 | static void | |
| 15884 | 856 | sanitize (PurpleDesktopItem *item, const char *uri) |
| 10229 | 857 | { |
| 858 | const char *type; | |
| 859 | ||
| 15884 | 860 | type = lookup (item, PURPLE_DESKTOP_ITEM_TYPE); |
| 10229 | 861 | |
| 862 | /* understand old gnome style url exec thingies */ | |
|
25859
b42be7bb9dac
Patch from Paul Aurich to add purple_strequal to help readability and simplicity of code. Ie, don't need to negate the value of strcmp, since this does a strcmp and does the negation for us
Paul Aurich <darkrain42@pidgin.im>
parents:
21389
diff
changeset
|
863 | if (purple_strequal(type, "URL")) { |
| 15884 | 864 | const char *exec = lookup (item, PURPLE_DESKTOP_ITEM_EXEC); |
| 865 | set (item, PURPLE_DESKTOP_ITEM_TYPE, "Link"); | |
| 10229 | 866 | if (exec != NULL) { |
| 867 | /* Note, this must be in this order */ | |
| 15884 | 868 | set (item, PURPLE_DESKTOP_ITEM_URL, exec); |
| 869 | set (item, PURPLE_DESKTOP_ITEM_EXEC, NULL); | |
| 10229 | 870 | } |
| 871 | } | |
| 872 | ||
| 873 | /* we make sure we have Name, Encoding and Version */ | |
| 15884 | 874 | if (lookup (item, PURPLE_DESKTOP_ITEM_NAME) == NULL) { |
| 875 | char *name = try_english_key (item, PURPLE_DESKTOP_ITEM_NAME); | |
| 10229 | 876 | /* If no name, use the basename */ |
| 877 | if (name == NULL && uri != NULL) | |
| 878 | name = g_path_get_basename (uri); | |
| 879 | /* If no uri either, use same default as gnome_desktop_item_new */ | |
| 880 | if (name == NULL) | |
| 881 | name = g_strdup (_("No name")); | |
| 882 | g_hash_table_replace (item->main_hash, | |
|
25888
d0fdd378a635
Remove trailing whitespace
Mark Doliner <markdoliner@pidgin.im>
parents:
25859
diff
changeset
|
883 | g_strdup (PURPLE_DESKTOP_ITEM_NAME), |
| 10229 | 884 | name); |
| 885 | item->keys = g_list_prepend | |
| 15884 | 886 | (item->keys, g_strdup (PURPLE_DESKTOP_ITEM_NAME)); |
| 10229 | 887 | } |
| 15884 | 888 | if (lookup (item, PURPLE_DESKTOP_ITEM_ENCODING) == NULL) { |
| 10229 | 889 | /* We store everything in UTF-8 so write that down */ |
| 890 | g_hash_table_replace (item->main_hash, | |
|
25888
d0fdd378a635
Remove trailing whitespace
Mark Doliner <markdoliner@pidgin.im>
parents:
25859
diff
changeset
|
891 | g_strdup (PURPLE_DESKTOP_ITEM_ENCODING), |
| 10229 | 892 | g_strdup ("UTF-8")); |
| 893 | item->keys = g_list_prepend | |
| 15884 | 894 | (item->keys, g_strdup (PURPLE_DESKTOP_ITEM_ENCODING)); |
| 10229 | 895 | } |
| 15884 | 896 | if (lookup (item, PURPLE_DESKTOP_ITEM_VERSION) == NULL) { |
| 10229 | 897 | /* this is the version that we follow, so write it down */ |
| 898 | g_hash_table_replace (item->main_hash, | |
|
25888
d0fdd378a635
Remove trailing whitespace
Mark Doliner <markdoliner@pidgin.im>
parents:
25859
diff
changeset
|
899 | g_strdup (PURPLE_DESKTOP_ITEM_VERSION), |
| 10229 | 900 | g_strdup ("1.0")); |
| 901 | item->keys = g_list_prepend | |
| 15884 | 902 | (item->keys, g_strdup (PURPLE_DESKTOP_ITEM_VERSION)); |
| 10229 | 903 | } |
| 904 | } | |
| 905 | ||
| 906 | enum { | |
| 907 | FirstBrace, | |
| 908 | OnSecHeader, | |
| 909 | IgnoreToEOL, | |
| 910 | IgnoreToEOLFirst, | |
| 911 | KeyDef, | |
| 912 | KeyDefOnKey, | |
| 913 | KeyValue | |
| 914 | }; | |
| 915 | ||
| 15884 | 916 | static PurpleDesktopItem * |
| 10229 | 917 | ditem_load (FILE *df, |
| 918 | gboolean no_translations, | |
| 919 | const char *uri) | |
| 920 | { | |
| 921 | int state; | |
| 922 | char CharBuffer [1024]; | |
| 923 | char *next = CharBuffer; | |
| 924 | int c; | |
| 925 | Encoding encoding; | |
| 15884 | 926 | PurpleDesktopItem *item; |
| 10229 | 927 | Section *cur_section = NULL; |
| 928 | char *key = NULL; | |
| 929 | gboolean old_kde = FALSE; | |
| 930 | ||
| 931 | encoding = get_encoding (df); | |
| 932 | if (encoding == ENCODING_UNKNOWN) { | |
| 933 | fclose(df); | |
| 934 | /* spec says, don't read this file */ | |
| 935 | printf ("Unknown encoding of .desktop file"); | |
| 936 | ||
| 937 | return NULL; | |
| 938 | } | |
| 939 | ||
| 940 | /* Rewind since get_encoding goes through the file */ | |
| 941 | if (fseek(df, 0L, SEEK_SET)) { | |
| 942 | fclose(df); | |
| 943 | /* spec says, don't read this file */ | |
| 944 | printf ("fseek() error on .desktop file"); | |
| 945 | return NULL; | |
| 946 | } | |
| 947 | ||
| 15884 | 948 | item = _purple_desktop_item_new (); |
| 10229 | 949 | item->modified = FALSE; |
| 950 | ||
| 951 | /* Note: location and mtime are filled in by the new_from_file | |
| 952 | * function since it has those values */ | |
| 953 | ||
| 15884 | 954 | #define PURPLE_DESKTOP_ITEM_OVERFLOW (next == &CharBuffer [sizeof(CharBuffer)-1]) |
| 10229 | 955 | |
| 956 | state = FirstBrace; | |
| 957 | while ((c = getc (df)) != EOF) { | |
| 958 | if (c == '\r') /* Ignore Carriage Return */ | |
| 959 | continue; | |
|
25888
d0fdd378a635
Remove trailing whitespace
Mark Doliner <markdoliner@pidgin.im>
parents:
25859
diff
changeset
|
960 | |
| 10229 | 961 | switch (state) { |
| 962 | ||
| 963 | case OnSecHeader: | |
| 15884 | 964 | if (c == ']' || PURPLE_DESKTOP_ITEM_OVERFLOW) { |
| 10229 | 965 | *next = '\0'; |
| 966 | next = CharBuffer; | |
| 967 | ||
| 968 | /* keys were inserted in reverse */ | |
| 969 | if (cur_section != NULL && | |
| 970 | cur_section->keys != NULL) { | |
| 971 | cur_section->keys = g_list_reverse | |
| 972 | (cur_section->keys); | |
| 973 | } | |
|
25859
b42be7bb9dac
Patch from Paul Aurich to add purple_strequal to help readability and simplicity of code. Ie, don't need to negate the value of strcmp, since this does a strcmp and does the negation for us
Paul Aurich <darkrain42@pidgin.im>
parents:
21389
diff
changeset
|
974 | if (purple_strequal (CharBuffer, "KDE Desktop Entry")) { |
| 10229 | 975 | /* Main section */ |
| 976 | cur_section = NULL; | |
| 977 | old_kde = TRUE; | |
|
25859
b42be7bb9dac
Patch from Paul Aurich to add purple_strequal to help readability and simplicity of code. Ie, don't need to negate the value of strcmp, since this does a strcmp and does the negation for us
Paul Aurich <darkrain42@pidgin.im>
parents:
21389
diff
changeset
|
978 | } else if (purple_strequal(CharBuffer, "Desktop Entry")) { |
| 10229 | 979 | /* Main section */ |
| 980 | cur_section = NULL; | |
| 981 | } else { | |
| 982 | cur_section = g_new0 (Section, 1); | |
| 983 | cur_section->name = | |
| 984 | g_strdup (CharBuffer); | |
| 985 | cur_section->keys = NULL; | |
| 986 | item->sections = g_list_prepend | |
| 987 | (item->sections, cur_section); | |
| 988 | } | |
| 989 | state = IgnoreToEOL; | |
| 990 | } else if (c == '[') { | |
| 991 | /* FIXME: probably error out instead of ignoring this */ | |
| 992 | } else { | |
| 993 | *next++ = c; | |
| 994 | } | |
| 995 | break; | |
| 996 | ||
| 997 | case IgnoreToEOL: | |
| 998 | case IgnoreToEOLFirst: | |
| 999 | if (c == '\n'){ | |
| 1000 | if (state == IgnoreToEOLFirst) | |
| 1001 | state = FirstBrace; | |
| 1002 | else | |
| 1003 | state = KeyDef; | |
| 1004 | next = CharBuffer; | |
| 1005 | } | |
| 1006 | break; | |
| 1007 | ||
| 1008 | case FirstBrace: | |
| 1009 | case KeyDef: | |
| 1010 | case KeyDefOnKey: | |
| 1011 | if (c == '#') { | |
| 1012 | if (state == FirstBrace) | |
| 1013 | state = IgnoreToEOLFirst; | |
| 1014 | else | |
| 1015 | state = IgnoreToEOL; | |
| 1016 | break; | |
| 1017 | } | |
| 1018 | ||
| 1019 | if (c == '[' && state != KeyDefOnKey){ | |
| 1020 | state = OnSecHeader; | |
| 1021 | next = CharBuffer; | |
| 1022 | g_free (key); | |
| 1023 | key = NULL; | |
| 1024 | break; | |
| 1025 | } | |
| 1026 | /* On first pass, don't allow dangling keys */ | |
| 1027 | if (state == FirstBrace) | |
| 1028 | break; | |
|
25888
d0fdd378a635
Remove trailing whitespace
Mark Doliner <markdoliner@pidgin.im>
parents:
25859
diff
changeset
|
1029 | |
| 10229 | 1030 | if ((c == ' ' && state != KeyDefOnKey) || c == '\t') |
| 1031 | break; | |
|
25888
d0fdd378a635
Remove trailing whitespace
Mark Doliner <markdoliner@pidgin.im>
parents:
25859
diff
changeset
|
1032 | |
| 15884 | 1033 | if (c == '\n' || PURPLE_DESKTOP_ITEM_OVERFLOW) { /* Abort Definition */ |
| 10229 | 1034 | next = CharBuffer; |
| 1035 | state = KeyDef; | |
| 1036 | break; | |
| 1037 | } | |
|
25888
d0fdd378a635
Remove trailing whitespace
Mark Doliner <markdoliner@pidgin.im>
parents:
25859
diff
changeset
|
1038 | |
| 15884 | 1039 | if (c == '=' || PURPLE_DESKTOP_ITEM_OVERFLOW){ |
| 10229 | 1040 | *next = '\0'; |
| 1041 | ||
| 1042 | g_free (key); | |
| 1043 | key = g_strdup (CharBuffer); | |
| 1044 | state = KeyValue; | |
| 1045 | next = CharBuffer; | |
| 1046 | } else { | |
| 1047 | *next++ = c; | |
| 1048 | state = KeyDefOnKey; | |
| 1049 | } | |
| 1050 | break; | |
| 1051 | ||
| 1052 | case KeyValue: | |
| 15884 | 1053 | if (PURPLE_DESKTOP_ITEM_OVERFLOW || c == '\n'){ |
| 10229 | 1054 | *next = '\0'; |
| 1055 | ||
| 1056 | insert_key (item, cur_section, encoding, | |
| 1057 | key, CharBuffer, old_kde, | |
| 1058 | no_translations); | |
| 1059 | ||
| 1060 | g_free (key); | |
| 1061 | key = NULL; | |
| 1062 | ||
| 1063 | state = (c == '\n') ? KeyDef : IgnoreToEOL; | |
| 1064 | next = CharBuffer; | |
| 1065 | } else { | |
| 1066 | *next++ = c; | |
| 1067 | } | |
| 1068 | break; | |
| 1069 | ||
| 1070 | } /* switch */ | |
|
25888
d0fdd378a635
Remove trailing whitespace
Mark Doliner <markdoliner@pidgin.im>
parents:
25859
diff
changeset
|
1071 | |
| 10229 | 1072 | } /* while ((c = getc_unlocked (f)) != EOF) */ |
| 1073 | if (c == EOF && state == KeyValue) { | |
| 1074 | *next = '\0'; | |
| 1075 | ||
| 1076 | insert_key (item, cur_section, encoding, | |
| 1077 | key, CharBuffer, old_kde, | |
| 1078 | no_translations); | |
| 1079 | ||
| 1080 | g_free (key); | |
| 1081 | key = NULL; | |
| 1082 | } | |
| 1083 | ||
| 15884 | 1084 | #undef PURPLE_DESKTOP_ITEM_OVERFLOW |
| 10229 | 1085 | |
| 1086 | /* keys were inserted in reverse */ | |
| 1087 | if (cur_section != NULL && | |
| 1088 | cur_section->keys != NULL) { | |
| 1089 | cur_section->keys = g_list_reverse (cur_section->keys); | |
| 1090 | } | |
| 1091 | /* keys were inserted in reverse */ | |
| 1092 | item->keys = g_list_reverse (item->keys); | |
| 1093 | /* sections were inserted in reverse */ | |
| 1094 | item->sections = g_list_reverse (item->sections); | |
| 1095 | ||
| 1096 | /* sanitize some things */ | |
| 1097 | sanitize (item, uri); | |
| 1098 | ||
| 1099 | /* make sure that we set up the type */ | |
| 1100 | setup_type (item, uri); | |
| 1101 | ||
| 1102 | fclose (df); | |
| 1103 | ||
| 1104 | return item; | |
| 1105 | } | |
| 1106 | ||
| 1107 | static void | |
| 1108 | copy_string_hash (gpointer key, gpointer value, gpointer user_data) | |
| 1109 | { | |
| 1110 | GHashTable *copy = user_data; | |
| 1111 | g_hash_table_replace (copy, | |
| 1112 | g_strdup (key), | |
| 1113 | g_strdup (value)); | |
| 1114 | } | |
| 1115 | ||
| 1116 | static void | |
| 1117 | free_section (gpointer data, gpointer user_data) | |
| 1118 | { | |
| 1119 | Section *section = data; | |
| 1120 | ||
| 1121 | g_free (section->name); | |
| 1122 | section->name = NULL; | |
| 1123 | ||
| 1124 | g_list_foreach (section->keys, (GFunc)g_free, NULL); | |
| 1125 | g_list_free (section->keys); | |
| 1126 | section->keys = NULL; | |
| 1127 | ||
| 1128 | g_free (section); | |
| 1129 | } | |
| 1130 | ||
| 1131 | static Section * | |
| 1132 | dup_section (Section *sec) | |
| 1133 | { | |
| 1134 | GList *li; | |
| 1135 | Section *retval = g_new0 (Section, 1); | |
| 1136 | ||
| 1137 | retval->name = g_strdup (sec->name); | |
| 1138 | ||
| 1139 | retval->keys = g_list_copy (sec->keys); | |
| 1140 | for (li = retval->keys; li != NULL; li = li->next) | |
| 1141 | li->data = g_strdup (li->data); | |
| 1142 | ||
| 1143 | return retval; | |
| 1144 | } | |
| 1145 | ||
| 1146 | /************************************************************************** | |
| 1147 | * Public functions | |
| 1148 | **************************************************************************/ | |
| 15884 | 1149 | PurpleDesktopItem * |
| 1150 | purple_desktop_item_new_from_file (const char *filename) | |
| 10229 | 1151 | { |
| 15884 | 1152 | PurpleDesktopItem *retval; |
| 10229 | 1153 | FILE *dfile; |
| 1154 | ||
| 1155 | g_return_val_if_fail (filename != NULL, NULL); | |
| 1156 | ||
|
10589
4e10236e06d4
[gaim-migrate @ 11994]
Daniel Atallah <datallah@pidgin.im>
parents:
10229
diff
changeset
|
1157 | dfile = g_fopen(filename, "r"); |
| 10229 | 1158 | if (!dfile) { |
|
21389
e1dd8142bb87
replace most calls to strerror with calls to g_strerror. strerror will return
Nathan Walp <nwalp@pidgin.im>
parents:
20147
diff
changeset
|
1159 | printf ("Can't open %s: %s", filename, g_strerror(errno)); |
| 10229 | 1160 | return NULL; |
| 1161 | } | |
|
25888
d0fdd378a635
Remove trailing whitespace
Mark Doliner <markdoliner@pidgin.im>
parents:
25859
diff
changeset
|
1162 | |
| 10229 | 1163 | retval = ditem_load(dfile, FALSE, filename); |
| 1164 | ||
| 1165 | return retval; | |
| 1166 | } | |
| 1167 | ||
| 15884 | 1168 | PurpleDesktopItemType |
| 1169 | purple_desktop_item_get_entry_type (const PurpleDesktopItem *item) | |
| 10229 | 1170 | { |
| 1171 | g_return_val_if_fail (item != NULL, 0); | |
| 1172 | g_return_val_if_fail (item->refcount > 0, 0); | |
| 1173 | ||
| 1174 | return item->type; | |
| 1175 | } | |
| 1176 | ||
| 1177 | const char * | |
| 15884 | 1178 | purple_desktop_item_get_string (const PurpleDesktopItem *item, |
| 10229 | 1179 | const char *attr) |
| 1180 | { | |
| 1181 | g_return_val_if_fail (item != NULL, NULL); | |
| 1182 | g_return_val_if_fail (item->refcount > 0, NULL); | |
| 1183 | g_return_val_if_fail (attr != NULL, NULL); | |
| 1184 | ||
| 1185 | return lookup (item, attr); | |
| 1186 | } | |
| 1187 | ||
| 15884 | 1188 | PurpleDesktopItem * |
| 1189 | purple_desktop_item_copy (const PurpleDesktopItem *item) | |
| 10229 | 1190 | { |
| 1191 | GList *li; | |
| 15884 | 1192 | PurpleDesktopItem *retval; |
| 10229 | 1193 | |
| 1194 | g_return_val_if_fail (item != NULL, NULL); | |
| 1195 | g_return_val_if_fail (item->refcount > 0, NULL); | |
| 1196 | ||
| 15884 | 1197 | retval = _purple_desktop_item_new (); |
| 10229 | 1198 | |
| 1199 | retval->type = item->type; | |
| 1200 | retval->modified = item->modified; | |
| 1201 | retval->location = g_strdup (item->location); | |
| 1202 | retval->mtime = item->mtime; | |
| 1203 | ||
| 1204 | /* Languages */ | |
| 1205 | retval->languages = g_list_copy (item->languages); | |
| 1206 | for (li = retval->languages; li != NULL; li = li->next) | |
|
25888
d0fdd378a635
Remove trailing whitespace
Mark Doliner <markdoliner@pidgin.im>
parents:
25859
diff
changeset
|
1207 | li->data = g_strdup (li->data); |
| 10229 | 1208 | |
| 1209 | /* Keys */ | |
| 1210 | retval->keys = g_list_copy (item->keys); | |
| 1211 | for (li = retval->keys; li != NULL; li = li->next) | |
| 1212 | li->data = g_strdup (li->data); | |
| 1213 | ||
| 1214 | /* Sections */ | |
| 1215 | retval->sections = g_list_copy (item->sections); | |
| 1216 | for (li = retval->sections; li != NULL; li = li->next) | |
| 1217 | li->data = dup_section (li->data); | |
| 1218 | ||
| 1219 | retval->main_hash = g_hash_table_new_full (g_str_hash, g_str_equal, | |
| 1220 | (GDestroyNotify) g_free, | |
| 1221 | (GDestroyNotify) g_free); | |
| 1222 | ||
| 1223 | g_hash_table_foreach (item->main_hash, | |
| 1224 | copy_string_hash, | |
| 1225 | retval->main_hash); | |
| 1226 | ||
| 1227 | return retval; | |
| 1228 | } | |
| 1229 | ||
| 1230 | void | |
| 15884 | 1231 | purple_desktop_item_unref (PurpleDesktopItem *item) |
| 10229 | 1232 | { |
| 1233 | g_return_if_fail (item != NULL); | |
| 1234 | g_return_if_fail (item->refcount > 0); | |
| 1235 | ||
| 1236 | item->refcount--; | |
| 1237 | ||
| 1238 | if(item->refcount != 0) | |
| 1239 | return; | |
| 1240 | ||
| 1241 | g_list_foreach (item->languages, (GFunc)g_free, NULL); | |
| 1242 | g_list_free (item->languages); | |
| 1243 | item->languages = NULL; | |
| 1244 | ||
| 1245 | g_list_foreach (item->keys, (GFunc)g_free, NULL); | |
| 1246 | g_list_free (item->keys); | |
| 1247 | item->keys = NULL; | |
| 1248 | ||
| 1249 | g_list_foreach (item->sections, free_section, NULL); | |
| 1250 | g_list_free (item->sections); | |
| 1251 | item->sections = NULL; | |
| 1252 | ||
| 1253 | g_hash_table_destroy (item->main_hash); | |
| 1254 | item->main_hash = NULL; | |
| 1255 | ||
| 1256 | g_free (item->location); | |
| 1257 | item->location = NULL; | |
| 1258 | ||
| 1259 | g_free (item); | |
| 1260 | } | |
| 1261 | ||
| 1262 | GType | |
| 15884 | 1263 | purple_desktop_item_get_type (void) |
| 10229 | 1264 | { |
| 1265 | static GType type = 0; | |
| 1266 | ||
| 1267 | if (type == 0) { | |
| 15884 | 1268 | type = g_boxed_type_register_static ("PurpleDesktopItem", |
| 1269 | _purple_desktop_item_copy, | |
| 1270 | _purple_desktop_item_free); | |
| 10229 | 1271 | } |
| 1272 | ||
| 1273 | return type; | |
| 1274 | } |