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