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