summaryrefslogtreecommitdiff
path: root/libgnome-desktop/gnome-desktop-item.c
blob: 9b0785ceb3c16aa8fc4597863a57ec4e84822409 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
/* -*- Mode: C; c-set-style: linux indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* gnome-ditem.h - GNOME Desktop File Representation 

   Copyright (C) 1999, 2000 Red Hat Inc.
   All rights reserved.

   This file is part of the Gnome Library.

   Developed by Elliot Lee <sopwith@redhat.com>
   
   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., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.  */
/*
  @NOTATION@
 */

#include "config.h"

#define DI_DEBUG 1

#include <limits.h>
#include <ctype.h>
#include <stdio.h>
#include <glib.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <time.h>
#include "gnome-defs.h"
#include "gnome-portability.h"
#include "gnome-ditem.h"
#include "gnome-util.h"
#include "gnome-config.h"
#include "gnome-exec.h"
#include "gnome-i18nP.h"
#include "gnome-mime.h"
#include <popt.h>

struct _GnomeDesktopItem {
        GHashTable *name; /* key is language, value is translated string */
        GHashTable *comment; /* key is language, value is translated string */

	char *type; /* type as in Application, Directory, URL, ... will
		       indicate what do do with the Exec line */

        char *exec;

        char *icon_path;

        char *location;

        GHashTable *other_attributes; /* string key, string value */

        GSList *order; /* If GNOME_DESKTOP_ITEM_IS_DIRECTORY */

        time_t mtime;

        guchar refcount;

        GnomeDesktopItemFormat item_format : 4;
        GnomeDesktopItemFlags item_flags : 8;
};

/**
 * gnome_desktop_item_new:
 *
 * Creates a GnomeDesktopItem object. The reference count on the returned value is set to '1'.
 *
 * Returns: The new GnomeDesktopItem
 */
GnomeDesktopItem *
gnome_desktop_item_new(void)
{
        GnomeDesktopItem *retval;

        retval = g_new0(GnomeDesktopItem, 1);

        retval->refcount++;

        return retval;
}

static void
ditem_copy_key_value(gpointer key, gpointer value, gpointer user_data)
{
        g_hash_table_insert(user_data, g_strdup(key), g_strdup(value));
}

/**
 * gnome_desktop_item_copy:
 * @item: The item to be copied
 *
 * Creates a copy of a GnomeDesktopItem.  The new copy has a refcount of 1.
 *
 * Returns: The new copy 
 */
GnomeDesktopItem *
gnome_desktop_item_copy (const GnomeDesktopItem *item)
{
        GnomeDesktopItem *retval;
	GSList *li;

        g_return_val_if_fail(item, NULL);

        retval = gnome_desktop_item_new();

        retval->item_format = item->item_format;
        retval->item_flags = item->item_flags;

	/* g_strdup handles NULLs correctly */
        retval->icon_path = g_strdup(item->icon_path);
        retval->location = g_strdup(item->location);
        retval->exec = g_strdup(item->exec);
        retval->type = g_strdup(item->type);

        if(item->name
           && g_hash_table_size(item->name) > 0) {
                retval->name = g_hash_table_new (g_str_hash, g_str_equal);
                g_hash_table_foreach(item->name, ditem_copy_key_value, retval->name);      
        }

        if(item->comment
           && g_hash_table_size(item->comment) > 0) {
                retval->comment = g_hash_table_new (g_str_hash, g_str_equal);
                g_hash_table_foreach(item->comment, ditem_copy_key_value, retval->comment);
        }

        if(item->other_attributes
           && g_hash_table_size(item->other_attributes) > 0) {
                retval->other_attributes = g_hash_table_new (g_str_hash, g_str_equal);
                g_hash_table_foreach(item->other_attributes, ditem_copy_key_value, retval->other_attributes);
        }

        retval->order = g_slist_copy(item->order);
	for(li = retval->order; li; li = li->next)
		li->data = g_strdup(li->data);

        return retval;
}

/* If you find a bug here, odds are that it exists in ditem_gnome_load() too */
static GnomeDesktopItem *
ditem_kde_load (const char *data_file, GnomeDesktopItemLoadFlags flags,
		GnomeDesktopItemFlags item_flags)
{
        GnomeDesktopItem *retval = gnome_desktop_item_new();
        char confpath[PATH_MAX];
        char *key, *value;
        void *iter;

        g_snprintf(confpath, sizeof(confpath), "=%s=/KDE Desktop Entry", data_file);

        retval->item_flags = item_flags;
        retval->name = g_hash_table_new(g_str_hash, g_str_equal);
        retval->comment = g_hash_table_new(g_str_hash, g_str_equal);
        retval->other_attributes = g_hash_table_new(g_str_hash, g_str_equal);

        iter = gnome_config_init_iterator(confpath);
        while((iter = gnome_config_iterator_next(iter, &key, &value))) {
		/* we need to keep things where the value is empty however */
                if(!*key) {
                        g_free(key);
                        g_free(value);
                        continue;
                }

                if(!strcmp(key, "Name")) {
                        g_hash_table_insert(retval->name, g_strdup("C"), value);
                } else if(!strncmp(key, "Name[", 5)) {
                        char *mylang, *ctmp;

                        mylang = key + 5;
                        ctmp = strchr(mylang, ']');
                        if(ctmp) *ctmp = '\0';

                        g_hash_table_insert(retval->name, g_strdup(mylang), value);
                        g_free(key);
                } else if(!strcmp(key, "Comment")) {
                        g_hash_table_insert(retval->comment, g_strdup("C"), value);
                } else if(!strncmp(key, "Comment[", strlen("Comment["))) {
                        char *mylang, *ctmp;

                        mylang = key + strlen("Comment[");
                        ctmp = strchr(mylang, ']');
                        if(ctmp) *ctmp = '\0';

                        g_hash_table_insert(retval->name, g_strdup(mylang), value);
                        g_free(key);
                } else if(!strcmp(key, "Icon")) {
                        retval->icon_path = value;
                        g_free(key);
                } else if(!strcmp(key, "Terminal")) {
                        if(tolower(*value) == 'y' || tolower(*value) == 't' || atoi(value))
                                retval->item_flags |= GNOME_DESKTOP_ITEM_RUN_IN_TERMINAL;
                        else
                                retval->item_flags &= (~GNOME_DESKTOP_ITEM_RUN_IN_TERMINAL);

                        g_free(key);
                        g_free(value);
                } else if(!strcmp(key, "Exec")) {
			retval->exec = value;
			if(strstr(value,"%f")) {
				retval->item_flags |= GNOME_DESKTOP_ITEM_NO_URL_DROP;
			} else if(strstr(value,"%F")) {
				retval->item_flags |= GNOME_DESKTOP_ITEM_NO_URL_DROP |
					GNOME_DESKTOP_ITEM_SINGLE_FILE_DROP_ONLY;
			} else if(strstr(value,"%u")) {
				/* everything is set just right :), don't touch it */
			} else 
				retval->item_flags |= GNOME_DESKTOP_ITEM_OLD_STYLE_DROP;
                        g_free(key);
                } else if(!strcmp(key, "Type")) {
			retval->type = value;
			if(strcmp(value,"KonsoleApplication")==0)
				retval->item_flags |=
					GNOME_DESKTOP_ITEM_RUN_IN_TERMINAL;
                        g_free(key);
                } else if(!strcmp(key, "SortOrder")) {
			char *p;
                        g_free(key);
			p = strtok(value,",");
			while(p) {
				retval->order = g_slist_prepend(retval->order,
								g_strdup(p));
				p = strtok(NULL,",");
			}
                        g_free(value);
			retval->order = g_slist_reverse(retval->order);
                } else {
                        g_hash_table_insert(retval->other_attributes,
					    key, value);
                }
        }

        if ((flags & GNOME_DESKTOP_ITEM_LOAD_ONLY_IF_EXISTS)
            && !(retval->item_flags & GNOME_DESKTOP_ITEM_IS_DIRECTORY)) {
                char *tryme;
                /* We don't use gnome_desktop_item_exists() here because it is more thorough than the TryExec stuff
                   which GNOME_DESKTOP_ITEM_LOAD_ONLY_IF_EXISTS specifies */

                if(!retval->exec)
                        goto errout;

                tryme = g_hash_table_lookup(retval->other_attributes, "TryExec");

                if(tryme && !(tryme = gnome_is_program_in_path(tryme))) {
			g_free(tryme);
                        goto errout;
		}
        }

        return retval;

 errout:
        gnome_desktop_item_unref(retval);
        return NULL;
}

/* There is entirely too much duplication between this function and ditem_kde_load() */
static GnomeDesktopItem *
ditem_gnome_load (const char *data_file, GnomeDesktopItemLoadFlags flags,
		  GnomeDesktopItemFlags item_flags)
{
        GnomeDesktopItem *retval = gnome_desktop_item_new();
        char confpath[PATH_MAX];
        char *key, *value;
        void *iter;
	int got_drop_keys = FALSE; /* if any new drop keys were set, otherwise use
				      OLD_STYLE_DROP */

        g_snprintf(confpath, sizeof(confpath), "=%s=/Desktop Entry", data_file);

        retval->item_flags = item_flags;
        retval->name = g_hash_table_new(g_str_hash, g_str_equal);
        retval->comment = g_hash_table_new(g_str_hash, g_str_equal);
        retval->other_attributes = g_hash_table_new(g_str_hash, g_str_equal);

        iter = gnome_config_init_iterator(confpath);
        while((iter = gnome_config_iterator_next(iter, &key, &value))) {
		/* we need to keep things where the value is empty however */
                if(!*key) {
                        g_free(key);
                        g_free(value);
                        continue;
                }

		if(strcmp(key, "FileDropExec")==0 ||
		   strcmp(key, "URLDropExec")==0)
			got_drop_keys = TRUE;

                if(!strcmp(key, "Name")) {
                        g_hash_table_insert(retval->name, g_strdup("C"), value);
                } else if(!strncmp(key, "Name[", 5)) {
                        char *mylang, *ctmp;

                        mylang = key + strlen("Name[");
                        ctmp = strchr(mylang, ']');
                        if(ctmp) *ctmp = '\0';

                        g_hash_table_insert(retval->name, g_strdup(mylang), value);
                        g_free(key);
                } else if(!strcmp(key, "Comment")) {
                        g_hash_table_insert(retval->comment, g_strdup("C"), value);
                } else if(!strncmp(key, "Comment[", strlen("Comment["))) {
                        char *mylang, *ctmp;

                        mylang = key + strlen("Comment[");
                        ctmp = strchr(mylang, ']');
                        if(ctmp) *ctmp = '\0';

                        g_hash_table_insert(retval->name, g_strdup(mylang), value);
                        g_free(key);
                } else if(!strcmp(key, "Icon")) {
                        retval->icon_path = value;
                        g_free(key);
                } else if(!strcmp(key, "Terminal")) {
                        if(tolower(*value) == 'y' || tolower(*value) == 't' || atoi(value))
                                retval->item_flags |= GNOME_DESKTOP_ITEM_RUN_IN_TERMINAL;
                        else
                                retval->item_flags &= (~GNOME_DESKTOP_ITEM_RUN_IN_TERMINAL);

                        g_free(key);
                        g_free(value);
                } else if(!strcmp(key, "SingleFileDropOnly")) {
                        if(tolower(*value) == 'y' || tolower(*value) == 't' || atoi(value))
                                retval->item_flags |= GNOME_DESKTOP_ITEM_SINGLE_FILE_DROP_ONLY;
                        else
                                retval->item_flags &= (~GNOME_DESKTOP_ITEM_SINGLE_FILE_DROP_ONLY);
                        g_free(key);
                        g_free(value);
                } else if(!strcmp(key, "SingleURLDropOnly")) {
                        if(tolower(*value) == 'y' || tolower(*value) == 't' || atoi(value))
                                retval->item_flags |= GNOME_DESKTOP_ITEM_SINGLE_URL_DROP_ONLY;
                        else
                                retval->item_flags &= (~GNOME_DESKTOP_ITEM_SINGLE_URL_DROP_ONLY);
                        g_free(key);
                        g_free(value);
                } else if(!strcmp(key, "Exec")) {
                        retval->exec = value;
                        g_free(key);
                } else if(!strcmp(key, "Type")) {
                        retval->type = value;
                        g_free(key);
                } else {
                        g_hash_table_insert(retval->other_attributes, key, value);
                }
        }

	if(got_drop_keys) {
		char *s;
                s = g_hash_table_lookup(retval->other_attributes, "FileDropExec");
		if(!s || !*s)
			retval->item_flags |= GNOME_DESKTOP_ITEM_NO_FILE_DROP;
                s = g_hash_table_lookup(retval->other_attributes, "URLDropExec");
		if(!s || !*s)
			retval->item_flags |= GNOME_DESKTOP_ITEM_NO_URL_DROP;
	} else
		retval->item_flags |= GNOME_DESKTOP_ITEM_OLD_STYLE_DROP;

        if ((flags & GNOME_DESKTOP_ITEM_LOAD_ONLY_IF_EXISTS)
            && !(retval->item_flags & GNOME_DESKTOP_ITEM_IS_DIRECTORY)) {
                char *tryme;

                if(!retval->exec)
                        goto errout;

                tryme = g_hash_table_lookup(retval->other_attributes, "TryExec");

                if(tryme && !(tryme = gnome_is_program_in_path(tryme))) {
			g_free(tryme);
                        goto errout;
		}
        }

        /* Read the .order file */
        if(retval->item_flags & GNOME_DESKTOP_ITEM_IS_DIRECTORY) {
                FILE *fh;
		char *dn;

		dn = g_path_get_dirname(data_file);
                g_snprintf(confpath, sizeof(confpath), "%s/.order", dn);
		g_free(dn);

                fh = fopen(confpath, "r");

                if(fh) {
                        char buf[PATH_MAX];

			/* we prepend the things onto the list so the
			   list will actually be the reverse of the file */
			while(fgets(buf, PATH_MAX, fh)) {
				retval->order = g_slist_prepend(retval->order,
								g_strdup(buf));
			}

			retval->order = g_slist_reverse(retval->order);

                        fclose(fh);
                }
        }

        return retval;

 errout:
        gnome_desktop_item_unref(retval);
        return NULL;
}

/* load all the extra sections and store them as attributes in form of
   section/attribute */
static void
ditem_load_other_sections(GnomeDesktopItem *item, const char *data_file,
			  GSList *other_sections)
{
	GSList *li;

	for(li = other_sections; li!=NULL; li = li->next) {
		char confpath[PATH_MAX];
		char *key, *value;
		void *iter;
		char *secname = li->data;

		g_snprintf(confpath, sizeof(confpath),
			   "=%s=/%s", data_file, secname);

		iter = gnome_config_init_iterator(confpath);
		while((iter = gnome_config_iterator_next(iter, &key, &value))) {
			char *realkey;
			if(!*key) {
				g_free(key);
				g_free(value);
				continue;
			}
			realkey = g_strconcat(secname,"/",key,NULL);

			g_hash_table_insert(item->other_attributes,
					    realkey, value);
			g_free(key);
		}
        }
}

/**
 * gnome_desktop_item_new_from_file:
 * @file: The filename or directory path to load the GnomeDesktopItem from
 * @flags: Flags to influence the loading process
 *
 * This function loads 'file' and turns it into a GnomeDesktopItem.
 * If 'file' is a directory, it loads all the items under that
 * directory as subitems of the directory's GnomeDesktopItem.
 *
 * Returns: The newly loaded item.
 */
GnomeDesktopItem *
gnome_desktop_item_new_from_file (const char *file, GnomeDesktopItemLoadFlags flags)
{
        GnomeDesktopItem *retval;
        char subfn[PATH_MAX], headbuf[256], confpath[PATH_MAX];
        FILE *fh;
        GnomeDesktopItemFlags item_flags;
        GnomeDesktopItemFormat fmt;
        struct stat sbuf;
	GSList *other_sections = NULL; /* names of other sections */

        g_return_val_if_fail (file, NULL);

#ifdef DI_DEBUG
        //  g_print("Loading file %s\n", file);
#endif

        if (stat(file, &sbuf) != 0)
                return NULL;

        /* Step one - figure out what type of file this is */
        flags = 0;
        fmt = GNOME_DESKTOP_ITEM_UNKNOWN;
        item_flags = 0;
        if (S_ISDIR(sbuf.st_mode)) {
                item_flags |= GNOME_DESKTOP_ITEM_IS_DIRECTORY;
                g_snprintf(subfn, sizeof(subfn), "%s/.directory", file);
		/* get the correct time for this file */
		if (stat(subfn, &sbuf) != 0)
			sbuf.st_mtime = 0;
        } else {
		char *base;
		/* here we figure out if we're talking about a directory
		   but have passed the .directory file */
		base = g_path_get_basename(file);
		if(strcmp(base, ".directory") == 0)
			item_flags |= GNOME_DESKTOP_ITEM_IS_DIRECTORY;
		g_free(base);
                strcpy(subfn, file);
        }


        fh = fopen(subfn, "r");

        if(!fh && (item_flags & GNOME_DESKTOP_ITEM_IS_DIRECTORY))
                fmt = GNOME_DESKTOP_ITEM_GNOME; /* Empty dir becomes a GNOME thingie */
        else if(fh) {
		/* we go through the entire file looking for section headers,
		   we do this as the right section header does not have to
		   be at the top.  We however assume that it's either a
		   GNOME desktop entry or a KDE one, and never both,
		   perhaps we should us gnome_config_* for this already
		   as it would probably speed things up, as gnome_config_*
		   would already cache the file for later reading, we could
		   also just do this from the file extention! */
		while(fgets(headbuf, sizeof(headbuf), fh)) {
			char *p;
			g_strchug(headbuf);
			if(headbuf[0] != '[')
				continue;

			if(!strncmp(headbuf, "[Desktop Entry]", strlen("[Desktop Entry]"))) {
				fmt = GNOME_DESKTOP_ITEM_GNOME;
				if(flags & GNOME_DESKTOP_ITEM_LOAD_NO_OTHER_SECTIONS)
					break;
			} else if(!strncmp(headbuf, "[KDE Desktop Entry]", strlen("[KDE Desktop Entry]"))) {
				fmt = GNOME_DESKTOP_ITEM_KDE;
				if(flags & GNOME_DESKTOP_ITEM_LOAD_NO_OTHER_SECTIONS)
					break;
			}

			if(flags & GNOME_DESKTOP_ITEM_LOAD_NO_OTHER_SECTIONS)
				continue;

			p = strchr(headbuf,']');
			if(!p) continue;
			*p = '\0';
			/* store a list of other sections to read */
			other_sections =
				g_slist_prepend(other_sections,
						g_strdup(&headbuf[1]));
		}

                fclose(fh);
        }

        switch (fmt) {
        case GNOME_DESKTOP_ITEM_KDE:
                retval = ditem_kde_load(subfn, flags, item_flags);
                break;
        case GNOME_DESKTOP_ITEM_GNOME:
                retval = ditem_gnome_load(subfn, flags, item_flags);
                break;
        default:
                g_warning(_("Unknown desktop file format %d"), fmt);
		/* if we have names of other sections, free that list */
		if(other_sections) {
			g_slist_foreach(other_sections,(GFunc)g_free,NULL);
			g_slist_free(other_sections);
		}
                return NULL;
                break;
        }

        if(!retval)
                goto out;

	ditem_load_other_sections(retval, subfn, other_sections);

	/* make the 'Type' sane */
        if(item_flags & GNOME_DESKTOP_ITEM_IS_DIRECTORY) {
		/* perhaps a little ineficent way to ensure this
		   is the correct type */
		g_free(retval->type);
		retval->type = g_strdup("Directory");
	} else {
		/* this is not a directory so sanitize it by killing the
		   type altogether if type happens to be "Directory" */
		if(retval->type && strcmp(retval->type, "Directory")==0) {
			g_free(retval->type);
			retval->type = NULL;
		}
	}

	if(subfn[0] == '/')
		retval->location = g_strdup(subfn);
	else {
		char *curdir = g_get_current_dir();
		retval->location = g_concat_dir_and_file(curdir,subfn);
		g_free(curdir);
	}
        retval->item_format = fmt;
        retval->mtime = sbuf.st_mtime;

        if(!g_hash_table_size(retval->name)) {
                g_hash_table_destroy(retval->name);
                retval->name = NULL;
        }
        if(!g_hash_table_size(retval->comment)) {
                g_hash_table_destroy(retval->comment);
                retval->comment = NULL;
        }
        if(!g_hash_table_size(retval->other_attributes)) {
                g_hash_table_destroy(retval->other_attributes);
                retval->other_attributes = NULL;
        }

 out:
	/* if we have names of other sections, free that list */
	if(other_sections) {
		g_slist_foreach(other_sections,(GFunc)g_free,NULL);
		g_slist_free(other_sections);
	}
        if(!(flags & GNOME_DESKTOP_ITEM_LOAD_NO_SYNC)) {
		g_snprintf(confpath, sizeof(confpath), "=%s=", subfn);
                gnome_config_sync_file(confpath);
	}

        return retval;
}

static void
save_lang_val(gpointer key, gpointer value, gpointer user_data)
{
        char *valptr, tmpbuf[256];

        if(!key || !strcmp(key, "C"))
                valptr = user_data;
        else
                g_snprintf((valptr = tmpbuf), sizeof(tmpbuf), "%s[%s]", (char *)user_data, (char *)key);

        gnome_config_set_string(valptr, value);
}

static void
save_key_val(gpointer key, gpointer value, gpointer user_data)
{
	GnomeDesktopItem *item = user_data;
	
	/* in case the user set the OLD_STYLE_DROP flag on ignore
	   the FileDropExec and URLDropExec so that next time we
	   load we get the OLD_STYLE_DROP behaviour */
	if(item->item_flags == GNOME_DESKTOP_ITEM_OLD_STYLE_DROP &&
	   (strcmp(key,"FileDropExec")==0 ||
	    strcmp(key,"URLDropExec")==0))
		return;

        gnome_config_set_string(key, value);
}

/**
 * gnome_desktop_item_save:
 * @item: A desktop item
 * @under: The directory to save the tree underneath, can be NULL
 *
 * Writes the specified item to disk.  If the 'under' is NULL, the original
 * location is used.  It sets the location of this entry to point to the
 * new location.
 *
 * Returns: boolean. %TRUE if the file was saved, %FALSE otherwise
 */
gboolean
gnome_desktop_item_save (GnomeDesktopItem *item, const char *under)
{
        char fnbuf[PATH_MAX + sizeof("==/KDE Desktop Entry/")];

        g_return_val_if_fail(item, FALSE);
        g_return_val_if_fail(under, FALSE);
        g_return_val_if_fail(item->location, FALSE);

	/* first we setup the new location if we need to */
	if(under) {
		char *old_loc = item->location;
		char *base;
		base = g_path_get_basename(item->location);
		item->location = g_concat_dir_and_file(under, base);
		g_free(base);
		g_free(old_loc);
	}

	/* save the .order for gnome directory entries */
	if(item->item_flags & GNOME_DESKTOP_ITEM_IS_DIRECTORY &&
	   item->item_format == GNOME_DESKTOP_ITEM_GNOME) {
                FILE *fh;
		char *dir = g_path_get_dirname(item->location);

                g_snprintf(fnbuf, sizeof(fnbuf), "%s/.order", dir);

		g_free(dir);

		fh = fopen(fnbuf, "w");

		if(fh) {
			GSList *li;
			for(li = item->order; li; li = li->next)
				fprintf(fh, "%s\n", (char *)li->data);
			fclose(fh);
		} else
			return FALSE;
	}

	/* clean the file first, do not drop as if we dropped it the old
	   stuff would be realoded on the next gnome_config_set_*, also
	   this saves a syscall for the file remove */
	g_snprintf(fnbuf, sizeof(fnbuf), "=%s=", item->location);
	gnome_config_clean_file(fnbuf);

	g_snprintf(fnbuf, sizeof(fnbuf), "=%s=/%sDesktop Entry/",
		   item->location,
		   (item->item_format==GNOME_DESKTOP_ITEM_KDE)?"KDE ":"");

        gnome_config_push_prefix(fnbuf);

        if(item->icon_path)
                gnome_config_set_string("Icon", item->icon_path);

        if(item->exec)
                gnome_config_set_string("Exec", item->exec);

	/* set the type, if we haven't got one, make one up, trying to
	   guess the correct one */
        if(item->type)
                gnome_config_set_string("Type", item->type);
	else
                gnome_config_set_string("Type", (item->item_flags & GNOME_DESKTOP_ITEM_IS_DIRECTORY)?"Directory":"Application");

        if(item->name)
                g_hash_table_foreach(item->name, save_lang_val, "Name");
	if(!gnome_desktop_item_get_name(item, "C")) {
		/* whoops, there is not default for name if no language is
		 * found, this could cause this not to be loaded so set it
		 * to the name of the file if possible, or "Unknown"
		 * (not to be translated "C" is supposed to be english) */
		char *name;
		name = g_path_get_basename(item->location);
		if(!name)
			name = g_strdup("Unknown");
		gnome_config_set_string("Name", name);
		g_free(name);
	}

        if(item->comment)
                g_hash_table_foreach(item->comment, save_lang_val, "Comment");
        if(item->other_attributes)
                g_hash_table_foreach(item->other_attributes,
				     save_key_val,
				     item);
        if(item->item_flags & GNOME_DESKTOP_ITEM_RUN_IN_TERMINAL)
                gnome_config_set_bool("Terminal", TRUE);

        if(item->item_format == GNOME_DESKTOP_ITEM_KDE &&
	   item->order) {
		GString *gs;
		GSList *li;

		gs = g_string_new("");
		for(li = item->order; li; li = li->next) {
			if(li != item->order)
				g_string_append_c(gs,',');
			g_string_append(gs,li->data);
		}

                gnome_config_set_string("SortOrder", gs->str);
		g_string_free(gs,TRUE);
        }

        gnome_config_pop_prefix();

        item->mtime = time(NULL);

	/* sync the file onto disk */
	g_snprintf(fnbuf, sizeof(fnbuf), "=%s=", item->location);

	return gnome_config_sync_file(fnbuf);
}

/**
 * gnome_desktop_item_ref:
 * @item: A desktop item
 *
 * Increases the reference count of the specified item.
 */
void
gnome_desktop_item_ref (GnomeDesktopItem *item)
{
        g_return_if_fail(item);

        item->refcount++;
}

static gboolean
ditem_free_key_value(gpointer key, gpointer value, gpointer user_data)
{
        g_free(key);
        g_free(value);
        return TRUE;
}

/**
 * gnome_desktop_item_ref:
 * @item: A desktop item
 *
 * Decreases the reference count of the specified item, and destroys the item if there are no more references left.
 */
void
gnome_desktop_item_unref (GnomeDesktopItem *item)
{
        g_return_if_fail(item);

        item->refcount--;

        if(item->refcount != 0)
                return;

        g_free(item->type);
        g_free(item->exec);
        g_free(item->icon_path);
        g_free(item->location);

        if(item->name) {
                g_hash_table_foreach_remove(item->name, ditem_free_key_value, NULL);
                g_hash_table_destroy(item->name);
        }

        if(item->comment) {
                g_hash_table_foreach_remove(item->comment, ditem_free_key_value, NULL);
                g_hash_table_destroy(item->comment);
        }

        if(item->other_attributes) {
                g_hash_table_foreach_remove(item->other_attributes, ditem_free_key_value, NULL);
                g_hash_table_destroy(item->other_attributes);
        }

        g_slist_foreach(item->order, (GFunc)g_free, NULL);
        g_slist_free(item->order);

        g_free(item);
}

/* replace %? (ps) with a string, the string can be NULL which is the same
   as "", it works on a vector and appends the 'tofree' with strings which
   should be freed later, returns TRUE if it replaced something, FALSE
   otherwise, if only_once is true we jump out after the first, it can actually
   only replace one 'ps' per argument to avoid infinite loops (in case 'string'
   would contain 'ps' */
static gboolean
replace_percentsign(int argc, char **argv, const char *ps,
		    const char *string, GSList **tofree,
		    gboolean only_once)
{
	int i;
	gboolean ret = FALSE;
	if(!string) string = "";
	for(i=0;i<argc;i++) {
		char *arg = argv[i];
		char *p = strstr(arg,ps);
		char *s;
		int start, string_len, ps_len;
		if(!p) continue;
		string_len = strlen(string);
		ps_len = strlen(ps);
		s = g_new(char, strlen(arg) - ps_len + string_len + 1);
		start = p - arg;
		strncpy(s,arg,start);
		strcpy(&s[start],string);
		strcpy(&s[start+string_len],&arg[start+ps_len]);
		argv[i] = s;
		*tofree = g_slist_prepend(*tofree, s);
		ret = TRUE;
		if(only_once) return ret;
	}
	return ret;
}

/* the appargv, will get modified but not freed, the replacekdefiles
   tells us to replace the %f %u and %F for kde files with nothing */
static int
ditem_execute(const GnomeDesktopItem *item, int appargc, const char *appargv[], int argc, const char *argv[], gboolean replacekdefiles)
{
        char **real_argv;
        int real_argc;
        int i, j, ret;
	char **term_argv = NULL;
	int term_argc = 0;
	GSList *tofree = NULL;

        g_return_val_if_fail(item, -1);
        g_return_val_if_fail(item->exec, -1);

        if(!argv)
                argc = 0;


	/* check the type, if there is one set */
	if(item->type) {
		if(item->item_format == GNOME_DESKTOP_ITEM_KDE) {
			if(strcmp(item->type,"KonsoleApplication")!=0 &&
			   strcmp(item->type,"Application")!=0) {
				/* we are not an application so just exit
				   with an error */
				g_warning(_("Attempting to launch a non-application"));
				return -1;
			}
		} else if(item->item_format == GNOME_DESKTOP_ITEM_GNOME) {
			if(strcmp(item->type,"Application")!=0) {
				/* we are not an application so just exit
				   with an error */
				g_warning(_("Attempting to launch a non-application"));
				return -1;
			}
		} else {
			g_warning(_("Unknown desktop file format %d"),
				  item->item_format);
		}
	}

	if(item->item_flags & GNOME_DESKTOP_ITEM_RUN_IN_TERMINAL) {
		gnome_config_get_vector ("/Gnome/Applications/Terminal",
					 &term_argc, &term_argv);
		if (term_argv == NULL) {
			char *check;
			check = gnome_is_program_in_path("gnome-terminal");
			term_argc = 2;
			if(!check) {
				term_argv = g_new0(char *,3);
				term_argv[0] = g_strdup("xterm");
				term_argv[1] = g_strdup("-e");
			} else {
				term_argv = g_new0(char *,3);
				term_argv[0] = check;
				term_argv[1] = g_strdup("-x");
			}
		}
	}

        real_argc = term_argc + argc + appargc;
        real_argv = g_alloca((real_argc + 1) * sizeof(char *));

        for(i = 0; i < term_argc; i++)
                real_argv[i] = term_argv[i];

        for(j = 0; j < appargc; j++, i++)
                real_argv[i] = (char *)appargv[j];

        for(j = 0; j < argc; j++, i++)
                real_argv[i] = (char *)argv[j];

	/* if we are kde, replace the standard %? thingies */
	if(item->item_format == GNOME_DESKTOP_ITEM_KDE) {
		if(replacekdefiles) {
			replace_percentsign(real_argc, real_argv, "%f", NULL, &tofree, TRUE);
			replace_percentsign(real_argc, real_argv, "%F", NULL, &tofree, TRUE);
			replace_percentsign(real_argc, real_argv, "%u", NULL, &tofree, TRUE);
		}
		replace_percentsign(real_argc, real_argv, "%d", getenv("PWD"), &tofree, FALSE);
		replace_percentsign(real_argc, real_argv, "%i", item->icon_path, &tofree, FALSE);
		replace_percentsign(real_argc, real_argv, "%m", 
				    gnome_desktop_item_get_attribute(item,"MiniIcon"), &tofree, FALSE);
		replace_percentsign(real_argc, real_argv, "%c", 
				    gnome_desktop_item_get_local_comment(item), &tofree, FALSE);
	}

        ret = gnome_execute_async(NULL, real_argc, real_argv);

	if(tofree) {
		g_slist_foreach(tofree,(GFunc)g_free,NULL);
		g_slist_free(tofree);
	}

	if(term_argv)
		g_strfreev(term_argv);

	return ret;
}

/* strip any trailing &, return FALSE if bad things happen and
   we end up with an empty string */
static gboolean
strip_the_amp(char *exec)
{
	int exec_len;

	g_strstrip(exec);
	if(!*exec) return FALSE;

	exec_len = strlen(exec);
	/* kill any trailing '&' */
	if(exec[exec_len-1] == '&') {
		exec[exec_len-1] = '\0';
		g_strchomp(exec);
	}

	/* can't exactly launch an empty thing */
	if(!*exec) return FALSE;

	return TRUE;
}

/**
 * gnome_desktop_item_launch:
 * @item: A desktop item
 * @argc: An optional count of arguments to be added to the arguments defined.
 * @argv: An optional argument array, of length 'argc', to be appended
 *        to the command arguments specified in 'item'. Can be NULL.
 *
 * This function runs the program listed in the specified 'item',
 * optionally appending additional arguments to its command line.  It uses
 * poptParseArgvString to parse the the exec string into a vector which is
 * then passed to gnome_exec_async for execution with argc and argv appended.
 *
 * Returns: The value returned by gnome_execute_async() upon execution of
 * the specified item or -1 on error.  It may also return a 0 on success
 * if pid is not available, such as in a case where the entry is a URL
 * entry.
 */
int
gnome_desktop_item_launch (const GnomeDesktopItem *item, int argc, const char **argv)
{
	const char **temp_argv = NULL;
	int temp_argc = 0;
	char *the_exec;
	int ret;

        g_return_val_if_fail(item, -1);

	if(!item->exec) {
		g_warning(_("Trying to execute an item with no 'Exec'"));
		return -1;
	}

	/* This is a URL, so launch it as a url */
	if (item->type != NULL &&
	    item->item_format == GNOME_DESKTOP_ITEM_GNOME &&
	    strcmp (item->type, "URL") == 0) {
		if (gnome_url_show (item->exec))
			return 0;
		else
			return -1;
	}

	/* make a new copy and get rid of spaces */
	the_exec = g_alloca(strlen(item->exec)+1);
	strcpy(the_exec,item->exec);

	if(!strip_the_amp(the_exec))
		return -1;

	/* we use a popt function as it does exactly what we want to do and
	   gnome already uses popt */
	if(poptParseArgvString(item->exec, &temp_argc, &temp_argv) != 0) {
		/* no we have failed in making this a vector what should
		   we do? we can pretend that the exec failed and return -1,
		   perhaps we should give a little better error? */
		return -1;
	}

	ret = ditem_execute(item, temp_argc, temp_argv, argc, argv, TRUE);

	/* free the array done by poptParseArgvString, not by
	   g_free but free, this is not a memory leak the whole thing
	   is one allocation */
	free(temp_argv);

	return ret;
}

/* replace %s, %f, %u or %F in exec with file and run */
static int
ditem_exec_single_file (const GnomeDesktopItem *item, const char *exec, char *file)
{
	const char **temp_argv = NULL;
	int temp_argc = 0;
	char *the_exec;
	GSList *tofree = NULL;
	int ret;

        g_return_val_if_fail(item, -1);
	if(!exec) {
		g_warning(_("No exec field for this drop"));
		return -1;
	}

	/* make a new copy and get rid of spaces */
	the_exec = g_alloca(strlen(exec)+1);
	strcpy(the_exec,exec);

	if(!strip_the_amp(the_exec))
		return -1;

	/* we use a popt function as it does exactly what we want to do and
	   gnome already uses popt */
	if(poptParseArgvString(exec, &temp_argc, &temp_argv) != 0) {
		/* no we have failed in making this a vector what should
		   we do? we can pretend that the exec failed and return -1,
		   perhaps we should give a little better error? */
		return -1;
	}

	/* we are gnomish and we want to use %s, but if someone uses %f, %u or %F
	   we forgive them and do it too */
	if(replace_percentsign(temp_argc, (char **)temp_argv, "%s", file, &tofree,TRUE))
		goto perc_replaced;
	if(replace_percentsign(temp_argc, (char **)temp_argv, "%f", file, &tofree,TRUE))
		goto perc_replaced;
	if(replace_percentsign(temp_argc, (char **)temp_argv, "%u", file, &tofree,TRUE))
		goto perc_replaced;
	if(replace_percentsign(temp_argc, (char **)temp_argv, "%F", file, &tofree,TRUE))
		goto perc_replaced;
	g_free(temp_argv);
	g_warning(_("Nothing to replace with a file in the exec string"));
	return -1;
perc_replaced:

	ret = ditem_execute(item, temp_argc, temp_argv, 0, NULL, FALSE);

	if(tofree) {
		g_slist_foreach(tofree,(GFunc)g_free,NULL);
		g_slist_free(tofree);
	}

	/* free the array done by poptParseArgvString, not by
	   g_free but free, this is not a memory leak the whole thing
	   is one allocation */
	free(temp_argv);

	return ret;
}

/* works sort of like strcmp(g_strstrip(s1),s2), but returns only
 * TRUE or FALSE and does not modify s1, basically it tells if s2
 * is the same as s1 stripped */
static gboolean
stripstreq(const char *s1, const char *s2)
{
	int len2;

	/* skip over initial spaces */
	while(*s1 == ' ' || *s1 == '\t')
		s1++;
	len2 = strlen(s2);
	/* compare inner parts */
	if(strncmp(s1, s2, len2) != 0)
		return FALSE;
	/* skip over beyond the equal parts */
	s1 += len2;
	for(; *s1; s1++) {
		/* if we find anything but whitespace that bad */
		if(*s1 != ' ' && *s1 != '\t')
			return FALSE;
	}

	return TRUE;
}

/* replace %s, %f, %u or %F in exec with files and run, the trick here is
   that we will only replace %? in case it is a complete argument */
static int
ditem_exec_multiple_files (const GnomeDesktopItem *item, const char *exec,
			   GList *files)
{
	const char **temp_argv = NULL;
	int temp_argc = 0;
	char *the_exec;
	int ret;
	int real_argc;
	const char **real_argv;
	int i,j;
	gboolean replaced = FALSE;

        g_return_val_if_fail(item, -1);
	if(!exec) {
		g_warning(_("No exec field for this drop"));
		return -1;
	}

	/* make a new copy and get rid of spaces */
	the_exec = g_alloca(strlen(exec)+1);
	strcpy(the_exec,exec);

	if(!strip_the_amp(the_exec))
		return -1;

	/* we use a popt function as it does exactly what we want to do and
	   gnome already uses popt */
	if(poptParseArgvString(exec, &temp_argc, &temp_argv) != 0) {
		/* no we have failed in making this a vector what should
		   we do? we can pretend that the exec failed and return -1,
		   perhaps we should give a little better error? */
		return -1;
	}

	real_argc = temp_argc - 1 + g_list_length(files);
	/* we allocate +2, one for NULL, one as a buffer in case
	   we don't find anything to replace */
	real_argv = g_alloca((real_argc + 2) * sizeof(char *));

	for(i=0,j=0; i < real_argc && j < temp_argc; i++, j++) {
		GList *li;
		real_argv[i] = temp_argv[j];
		/* we only replace once */
		if(replaced)
			continue;

		if(!stripstreq(real_argv[i],"%s") &&
		   !stripstreq(real_argv[i],"%f")!=0 &&
		   !stripstreq(real_argv[i],"%u")!=0 &&
		   !stripstreq(real_argv[i],"%F")!=0)
			continue;

		/* replace the argument with the files we got passed */
		for(li = files; li!=NULL; li=li->next)
			real_argv[i++] = li->data;

		/* since we will increment in the for loop itself */
		i--;

		/* don't replace again! */
		replaced = TRUE;
	}

	/* we haven't replaced anything */
	if(!replaced) {
		g_warning(_("There was no argument to replace with dropped files"));
		return -1;
	}

	ret = ditem_execute(item, real_argc, real_argv, 0, NULL, FALSE);

	/* free the array done by poptParseArgvString, not by
	   g_free but free, this is not a memory leak the whole thing
	   is one allocation */
	free(temp_argv);

	return ret;
}


/**
 * gnome_desktop_item_drop_uri_list:
 * @item: A desktop item
 * @uri_list: an uri_list as if gotten from #gnome_uri_list_extract_uris
 *
 * A list of files or urls dropped onto an icon, the proper (Url or File)
 * exec is run you can pass directly the output of 
 * #gnome_uri_list_extract_uris.  The input thus must be a GList of
 * strings in the URI format.  If the ditem didn't specify any DND
 * options we assume old style and drop by appending the list to the end
 * of 'Exec' (see #gnome_desktop_item_get_command).  If there were any urls
 * dropped the item must have specified being able to run URLs or you'll get
 * a warning and an error.  Basically if there are ANY urls in the list
 * it's taken as a list of urls and works only on apps that can take urls.
 * If the program can't take that many arguments, as many instances as
 * necessary will be started.
 *
 * Returns: The value returned by gnome_execute_async() upon execution of
 * the specified item or -1 on error.  If multiple instances are run, the
 * return of the last one is returned.
 */
/*FIXME: perhaps we should be able to handle mixed url/file lists better
  rather then treating it as a list of urls */
int
gnome_desktop_item_drop_uri_list (const GnomeDesktopItem *item,
				  GList *uri_list)
{
	GList *li;
	gboolean any_urls = FALSE;
	GList *file_list = NULL;
	int ret = -1;

	g_return_val_if_fail (item != NULL, -1);
	g_return_val_if_fail (uri_list != NULL, -1);

	/* How could you drop something on a URL entry, that would
	 * be bollocks */
	if (item->type != NULL &&
	    item->item_format == GNOME_DESKTOP_ITEM_GNOME &&
	    strcmp (item->type, "URL") == 0) {
		return -1;
	}

	/* do we even allow drops? */
	if(item->item_flags & GNOME_DESKTOP_ITEM_NO_FILE_DROP &&
	   item->item_flags & GNOME_DESKTOP_ITEM_NO_URL_DROP)
		return -1;

	/* the simple case */
	if(item->item_flags & GNOME_DESKTOP_ITEM_OLD_STYLE_DROP) {
		int i;
		int argc = g_list_length(uri_list);
		const char **argv = g_alloca((argc + 1) * sizeof(char *));

		for(i=0,li=uri_list; li; i++, li=li->next) {
			char *p = gnome_uri_extract_filename(li->data);
			if(p) {
				char *s = g_alloca(strlen(p)+1);
				strcpy(s, p);
				g_free(p);
				argv[i] = s;
			} else {
				argv[i] = li->data;
			}
		}
		argv[i] = NULL;
		return gnome_desktop_item_launch(item, argc, argv);
	}

	/* figure out if we have any urls in the dropped files,
	   this also builds a list of all the files */
	for(li=uri_list; li; li=li->next) {
		char *p = gnome_uri_extract_filename(li->data);
		if(p)
			file_list = g_list_prepend(file_list, p);
		else
			any_urls = TRUE;
	}
	if(file_list)
		file_list = g_list_reverse(file_list);

	/* if there are any urls, or if the app didn't specify a command
	   for files use the URL command */
	if(any_urls || item->item_flags & GNOME_DESKTOP_ITEM_NO_FILE_DROP) {
		if(item->item_flags & GNOME_DESKTOP_ITEM_NO_URL_DROP) {
			g_warning(_("Dropped URLs on an app that can only take files"));
			return -1;
		}

		if(item->item_flags & GNOME_DESKTOP_ITEM_SINGLE_URL_DROP_ONLY) {
			GList *li;
			const char *exec = NULL;
			if(item->item_format == GNOME_DESKTOP_ITEM_GNOME)
				exec = gnome_desktop_item_get_attribute(item,"URLDropExec");
			if(!exec) exec = item->exec;
			for(li = uri_list; li; li=li->next)
				ret = ditem_exec_single_file(item, exec, li->data);
		} else {
			const char *exec = NULL;
			if(item->item_format == GNOME_DESKTOP_ITEM_GNOME)
				exec = gnome_desktop_item_get_attribute(item,"URLDropExec");
			if(!exec) exec = item->exec;
			ret = ditem_exec_multiple_files(item, exec, uri_list);
		}
	} else {
		if(item->item_flags & GNOME_DESKTOP_ITEM_SINGLE_FILE_DROP_ONLY) {
			GList *li;
			const char *exec = NULL;
			if(item->item_format == GNOME_DESKTOP_ITEM_GNOME)
				exec = gnome_desktop_item_get_attribute(item,"FileDropExec");
			if(!exec) exec = item->exec;
			for(li = file_list; li; li=li->next)
				ret = ditem_exec_single_file(item, exec,
							     li->data);
		} else {
			const char *exec = NULL;
			if(item->item_format == GNOME_DESKTOP_ITEM_GNOME)
				exec = gnome_desktop_item_get_attribute(item,"FileDropExec");
			if(!exec) exec = item->exec;
			ret = ditem_exec_multiple_files(item, exec, file_list);
		}
	}

	if(file_list) {
		g_list_foreach(file_list,(GFunc)g_free,NULL);
		g_list_free(file_list);
	}

	return ret;
}

/**
 * gnome_desktop_item_exists:
 * @item: A desktop item
 *
 * Attempt to figure out if the program that can be executed by this item
 * actually exists.  First it tries the TryExec attribute to see if that
 * contains a program that is in the path.  Then if there is no such
 * attribute, it tries the first word of the Exec attribute.
 *
 * Returns: A boolean, %TRUE if it exists, %FALSE otherwise.
 */
gboolean
gnome_desktop_item_exists (const GnomeDesktopItem *item)
{
        g_return_val_if_fail(item, FALSE);

        if(item->other_attributes) {
                char *tryme;

                tryme = g_hash_table_lookup(item->other_attributes, "TryExec");
                if(tryme) {
                        tryme = gnome_is_program_in_path(tryme);
                        if(tryme) {
                                g_free(tryme);
                                return TRUE;
                        }
                        else
                                return FALSE;
                }
        }

        if(item->exec) {
		const char **temp_argv = NULL;
		int temp_argc = 0;
		gboolean ret = FALSE;

		/* we use a popt function as it does exactly what we want to
		   do and gnome already uses popt */
		if(poptParseArgvString(item->exec,
				       &temp_argc,
				       &temp_argv) != 0) {
			/* we have failed, so we can't actually parse the
			   exec string and do the only sane thing and
			   return FALSE */
			return FALSE;
		}

                if(temp_argv[0][0] == PATH_SEP)
                        ret = g_file_exists(temp_argv[0]);
                else {
                        char *tryme = gnome_is_program_in_path(temp_argv[0]);
                        if(tryme) {
                                g_free(tryme);
                                ret = TRUE;
                        }
                        else
                                ret = FALSE;
                }
		/* free the array done by poptParseArgvString, not by
		   g_free but free, this is not a memory leak the whole
		   thing is one allocation */
		free(temp_argv);
		return ret;
        }

        return TRUE;
}

/**
 * gnome_desktop_item_get_flags:
 * @item: A desktop item
 *
 * Returns: The flags associated with the specified 'item'
 */
GnomeDesktopItemFlags
gnome_desktop_item_get_flags (const GnomeDesktopItem *item)
{
        g_return_val_if_fail(item, 0);

        return item->item_flags;
}

/**
 * gnome_desktop_item_get_type:
 * @item: A desktop item
 *
 * Gets the type attribute (the 'Type' field) of the item.  This should
 * usually be 'Application' for an application, but it can be 'Directory'
 * for a directory description.  There are other types available as well.
 * The type usually indicates how the desktop item should be handeled and
 * how the 'Exec' field should be handeled.
 *
 * Returns: The type of the specified 'item'. The returned
 * memory remains owned by the GnomeDesktopItem and should not be freed.
 */
const char *
gnome_desktop_item_get_type (const GnomeDesktopItem *item)
{
        g_return_val_if_fail(item, NULL);

        return item->type;
}

/**
 * gnome_desktop_item_get_command:
 * @item: A desktop item
 *
 * Gets the command attribute (the 'Exec' field) of the item.  
 * The command should be a single string which can be parsed into
 * a vector directly for execution.  Note that it is NOT a shell
 * expression.  It is allowed to have escapes and quoting as per
 * the standard quoting rules however.
 *
 * Returns: The command associated with the specified 'item'. The returned
 * memory remains owned by the GnomeDesktopItem and should not be freed.
 */
const char *
gnome_desktop_item_get_command (const GnomeDesktopItem *item)
{
        g_return_val_if_fail(item, NULL);

        return item->exec;
}

/**
 * gnome_desktop_item_get_icon_path:
 * @item: A desktop item
 *
 * Returns: The icon filename associated with the specified 'item'.
 * The returned memory remains owned by the GnomeDesktopItem
 * and should not be freed.
 */
const char *
gnome_desktop_item_get_icon_path (const GnomeDesktopItem *item)
{
        g_return_val_if_fail(item, NULL);

        return item->icon_path;
}

/**
 * gnome_desktop_item_get_name:
 * @item: A desktop item
 * @language: The language translation for which the name should be returned. If NULL is passed, it defaults to "C".
 *
 * Returns: The human-readable name for the specified 'item', in the
 *          specified 'language'. The returned memory remains owned by
 *          the GnomeDesktopItem and should not be freed.
 * */
const char *
gnome_desktop_item_get_name (const GnomeDesktopItem *item, const char *language)
{
        g_return_val_if_fail(item, NULL);

        if(item->name)
                return g_hash_table_lookup(item->name, language?language:"C");
        else
                return NULL;
}

/**
 * gnome_desktop_item_get_comment:
 * @item: A desktop item
 * @language: The language translation for which the comment should be returned. If NULL is passed, it defaults to "C".
 *
 * Returns: The human-readable comment for the specified 'item', in the
 *          specified 'language'. The returned memory remains owned by
 *          the GnomeDesktopItem and should not be freed.
 *
 */
const char *
gnome_desktop_item_get_comment (const GnomeDesktopItem *item, const char *language)
{
        g_return_val_if_fail(item, NULL);

        if(item->comment)
                return g_hash_table_lookup(item->comment, language?language:"C");
        else
                return NULL;
}

/**
 * gnome_desktop_item_get_local_name:
 * @item: A desktop item
 *
 * Returns: The human-readable name for the specified @item, in
 *          the user's preferred locale or a fallback if
 *          necessary. The returned memory remains owned by the
 *          GnomeDesktopItem and should not be freed. If no
 *          name is set in any relevant locale, NULL is returned.
 * */

const char *
gnome_desktop_item_get_local_name   (const GnomeDesktopItem     *item)
{
	g_return_val_if_fail(item, NULL);

	if(item->name) {
		const GList *iter;

		iter = gnome_i18n_get_language_list(NULL);

		while (iter != NULL) {
			const gchar* name;

			name = g_hash_table_lookup(item->name, iter->data);

			if (name)
				return name;
			else
				iter = g_list_next(iter);
		}
		return NULL;
	} else
                return NULL;
}

/**
 * gnome_desktop_item_get_local_comment:
 * @item: A desktop item
 *
 * Returns: The human-readable comment for the specified @item, in
 *          the user's preferred locale or a fallback if
 *          necessary. The returned memory remains owned by the
 *          GnomeDesktopItem and should not be freed. If no comment
 *          exists then NULL is returned.
 * */
const char *
gnome_desktop_item_get_local_comment(const GnomeDesktopItem     *item)
{
        g_return_val_if_fail(item, NULL);

	if(item->comment) {
		const GList *iter;

		iter = gnome_i18n_get_language_list(NULL);

		while (iter != NULL) {
			const gchar* comment;

			comment = g_hash_table_lookup(item->comment, iter->data);

			if (comment)
				return comment;
			else
				iter = g_list_next(iter);
		}
		return NULL;
	} else
                return NULL;
}


/**
 * gnome_desktop_item_get_attribute:
 * @item: A desktop item
 * @attr_name: Name of the attribute to get
 *
 * Gets an extra attribute from the desktop item.  If there were other
 * sections in the item, they can be accessed as "section/key".  If you
 * attribute is a translatable string, use the
 * #gnome_desktop_item_get_local_attribute function.
 *
 * Returns: The value of the attribute 'attr_name' on the 'item'. The
 *          returned memory remains owned by the GnomeDesktopItem and
 *          should not be freed.
 *
 */
const char *
gnome_desktop_item_get_attribute (const GnomeDesktopItem *item, const char *attr_name)
{
        g_return_val_if_fail(item, NULL);
        g_return_val_if_fail(attr_name, NULL);

        if(item->other_attributes)
                return g_hash_table_lookup(item->other_attributes, attr_name);
        else
                return NULL;
}

/**
 * gnome_desktop_item_get_local_attribute:
 * @item: A desktop item
 * @attr_name: Name of the attribute to get
 *
 * Gets an extra attribute as a local string (if it was a translatable
 * attribute) from the desktop item.  If there were other
 * sections in the item, they can be accessed as "section/key".  If you
 * attribute is not a translatable string, use the
 * #gnome_desktop_item_get_attribute function.
 *
 * Returns: The value of the attribute 'attr_name' on the 'item'. The
 *          returned memory remains owned by the GnomeDesktopItem and
 *          should not be freed.
 *
 */
const char *
gnome_desktop_item_get_local_attribute (const GnomeDesktopItem *item,
					const char *attr_name)
{
        g_return_val_if_fail(item, NULL);
        g_return_val_if_fail(attr_name, NULL);

        if(item->other_attributes) {
		const GList *iter;

		iter = gnome_i18n_get_language_list(NULL);

		while (iter != NULL) {
			char *key;
			const gchar* value;
			char *lang = iter->data;

			if(lang && strcmp(lang,"C")!=0)
				key = g_strconcat(attr_name,"[",lang,"]",NULL);
			else
				key = (char *)attr_name;

			value = g_hash_table_lookup(item->other_attributes,
						    key);
			if(key != attr_name) g_free(key);

			if (value)
				return value;
			else
				iter = g_list_next(iter);
		}
	} 
	return NULL;
}

/**
 * gnome_desktop_item_get_order:
 * @item: A desktop item
 *
 * Get the order of the items in a subdirectory which this item describes.
 * When you load a directory you should load these first and then load
 * any remaining ones after.
 *
 * Returns: A GSList of strings which are base filesnames in the correct
 * order.  The returned list should not be freed as it is owned by the item.
 *
 */
const GSList *
gnome_desktop_item_get_order (const GnomeDesktopItem *item)
{
        g_return_val_if_fail(item, NULL);

        return item->order;
}

static void
ditem_add_key(gpointer key, gpointer value, gpointer user_data)
{
        GSList **list = user_data;

        *list = g_slist_prepend(*list, key);
}

/* add key to the list (in user_data), but test to see if it's
   there first */
static void
ditem_add_key_test(gpointer key, gpointer value, gpointer user_data)
{
        GSList **list = user_data;

	if(g_slist_find_custom(*list, key, (GCompareFunc)strcmp)==NULL)
		*list = g_slist_prepend(*list, key);
}

/**
 * gnome_desktop_item_get_languages:
 * @item: A desktop item
 * 
 * Gets a list of languages that you can use to get names and comments.
 *
 * Returns: A GSList of the language name strings.  The returned GSList
 * should be freed with g_slist_free, but the strings inside the list
 * should not.
 *
 */
GSList *
gnome_desktop_item_get_languages(const GnomeDesktopItem *item)
{
        GSList *retval = NULL;

        g_return_val_if_fail(item, NULL);

        if(item->name) {
                g_hash_table_foreach(item->name, ditem_add_key, &retval);
        }
        if(item->comment) {
                g_hash_table_foreach(item->name, ditem_add_key_test, &retval);
        }

        return retval;
}

/**
 * gnome_desktop_item_get_attributes:
 * @item: A desktop item
 *
 * Gets the list of the attributes which you can can get with the
 * #gnome_desktop_item_get_attribute function.
 *
 * Returns: A GSList of the attribute name strings.  The returned GSList
 * should be freed with g_slist_free, but the strings inside the list
 * should not.
 *
 */
GSList *
gnome_desktop_item_get_attributes(const GnomeDesktopItem *item)
{
        GSList *retval = NULL;

        g_return_val_if_fail(item, NULL);

        if(item->other_attributes) {
                g_hash_table_foreach(item->other_attributes, ditem_add_key, &retval);
        }

        return retval;
}

/**
 * gnome_desktop_item_get_format:
 * @item: A desktop item
 *
 * Returns: The format that the specified 'item' is stored on disk with
 */
GnomeDesktopItemFormat
gnome_desktop_item_get_format (const GnomeDesktopItem *item)
{
        g_return_val_if_fail(item, GNOME_DESKTOP_ITEM_UNKNOWN);

        return item->item_format;
}

/**
 * gnome_desktop_item_get_file_status:
 * @item: A desktop item
 *
 * This function checks the modification time of the on-disk file to
 * see if it is more recent than the in-memory data.
 *
 * Returns: An enum value that specifies whether the item has changed since being loaded.
 */
GnomeDesktopItemStatus
gnome_desktop_item_get_file_status (GnomeDesktopItem *item)
{
        struct stat sbuf;
        GnomeDesktopItemStatus retval;

        g_return_val_if_fail(item, GNOME_DESKTOP_ITEM_DISAPPEARED);
        g_return_val_if_fail(item->location, GNOME_DESKTOP_ITEM_DISAPPEARED);

        if(stat(item->location, &sbuf) != 0)
                return GNOME_DESKTOP_ITEM_DISAPPEARED;

        retval = (sbuf.st_mtime > item->mtime)?GNOME_DESKTOP_ITEM_CHANGED:GNOME_DESKTOP_ITEM_UNCHANGED;

        item->mtime = sbuf.st_mtime;

        return retval;
}

/**
 * gnome_desktop_item_get_location:
 * @item: A desktop item
 *
 * Returns: The file location associated with 'item'. The returned
 *          memory remains owned by the GnomeDesktopItem and should
 *          not be freed.
 *
 */
const char *
gnome_desktop_item_get_location (const GnomeDesktopItem *item)
{
        return item->location;
}

/******* Set... ******/

/**
 * gnome_desktop_item_clear_name:
 * @item: A desktop item
 *
 * Clear the name field for this item for all the languages.
 *
 * Returns:
 */
void
gnome_desktop_item_clear_name(GnomeDesktopItem *item)
{
        if(item->name) {
                g_hash_table_foreach_remove(item->name,
					    ditem_free_key_value, NULL);
                g_hash_table_destroy(item->name);
		item->name = NULL;
        }
}

/**
 * gnome_desktop_item_set_name:
 * @item: A desktop item
 * @language: The language for which to set the item name
 * @name: The item's name in the specified 'language'
 *
 * Set the name for language 'language' to 'name'.  If 'language' is
 * NULL it defaults to "C".  If 'name' is NULL it removes the name
 * for that language.
 *
 * Returns:
 */
void
gnome_desktop_item_set_name (GnomeDesktopItem *item, const char *language, const char *name)
{
        gpointer old_val = NULL, old_key = NULL;

        g_return_if_fail(item);

	if(!language) language = "C";

	if(!name) {
		if(!item->name ||
		   !g_hash_table_lookup_extended(item->name,
						 language,
						 &old_key, &old_val))
			return;
		g_hash_table_remove(item->name, old_key);
		g_free(old_key);
		g_free(old_val);
		return;
	}

        if(!item->name)
                item->name = g_hash_table_new(g_str_hash,
					      g_str_equal);

	if(g_hash_table_lookup_extended(item->name, language,
					&old_key, &old_val)) {
		g_hash_table_insert(item->name,
				    old_key, g_strdup(name));
		g_free(old_val);
        } else
		g_hash_table_insert(item->name,
				    g_strdup(language), g_strdup(name));
}

/**
 * gnome_desktop_item_set_type:
 * @item: A desktop item
 * @type: A string with the type
 *
 * Sets the type attribute (the 'Type' field) of the item.  This should
 * usually be 'Application' for an application, but it can be 'Directory'
 * for a directory description.  There are other types available as well.
 * The type usually indicates how the desktop item should be handeled and
 * how the 'Exec' field should be handeled.
 *
 * Returns:
 *
 */
void
gnome_desktop_item_set_type (GnomeDesktopItem *item, const char *type)
{
        g_return_if_fail(item);

        g_free(item->type);
        if(type)
                item->type = g_strdup(type);
        else
                item->type = NULL;
}

/**
 * gnome_desktop_item_set_command:
 * @item: A desktop item
 * @command: A string with the command
 *
 * Sets the command attribute (the 'Exec' field) of the item.  
 * The command should be a single string which can be parsed into
 * a vector directly for execution.  Note that it is NOT a shell
 * expression.  It is allowed to have escapes and quoting as per
 * the standard quoting rules however.
 *
 * Returns:
 *
 */
void
gnome_desktop_item_set_command (GnomeDesktopItem *item, const char *command)
{
        g_return_if_fail(item);

        g_free(item->exec);
        if(command)
                item->exec = g_strdup(command);
        else
                item->exec = NULL;
}

/**
 * gnome_desktop_item_set_icon_path:
 * @item: A desktop item
 * @icon_path: A string specifying the path to the icon file for this item.
 *
 */
void
gnome_desktop_item_set_icon_path (GnomeDesktopItem *item, const char *icon_path)
{
        g_return_if_fail(item);

        g_free(item->icon_path);
        if(icon_path)
                item->icon_path = g_strdup(icon_path);
        else
                item->icon_path = NULL;
}

/**
 * gnome_desktop_item_clear_comment:
 * @item: A desktop item
 *
 * Clear the comment field for this item for all the languages.
 *
 * Returns:
 */
void
gnome_desktop_item_clear_comment(GnomeDesktopItem *item)
{

        if(item->comment) {
                g_hash_table_foreach_remove(item->comment,
					    ditem_free_key_value, NULL);
                g_hash_table_destroy(item->comment);
		item->comment = NULL;
        }
}

/**
 * gnome_desktop_item_set_comment:
 * @item: A desktop item
 * @language: The language for which to set the item comment
 * @comment: The item's comment in the specified 'language'
 *
 * Set the comment for language 'language' to 'comment'.  If 'language' is
 * NULL it defaults to "C".  If 'comment' is NULL it removes the comment
 * for that language.
 *
 * Returns:
 */
void
gnome_desktop_item_set_comment (GnomeDesktopItem *item, const char *language, const char *comment)
{
        gpointer old_val = NULL, old_key = NULL;

        g_return_if_fail(item);

	if(!language) language = "C";

	if(!comment) {
		if(!item->comment ||
		   !g_hash_table_lookup_extended(item->comment,
						 language,
						 &old_key, &old_val))
			return;
		g_hash_table_remove(item->comment, old_key);
		g_free(old_key);
		g_free(old_val);
		return;
	}

        if(!item->comment)
                item->comment = g_hash_table_new(g_str_hash,
						 g_str_equal);

        if(g_hash_table_lookup_extended(item->comment, language,
					&old_key, &old_val)) {
		g_hash_table_insert(item->comment,
				    old_key, g_strdup(comment));
		g_free(old_val);
        } else
		g_hash_table_insert(item->comment,
				    g_strdup(language), g_strdup(comment));
}

/**
 * gnome_desktop_item_set_attribute:
 * @item: A desktop item
 * @attr_name: The name of the attribute
 * @attr_value: The value of the attribute
 *
 * Set an attribute of name 'attr_name' to 'attr_value'.  If attr_value
 * is NULL it removes the attribute completely.
 *
 */
void
gnome_desktop_item_set_attribute (GnomeDesktopItem *item,
				  const char *attr_name,
				  const char *attr_value)
{
        gpointer old_val = NULL, old_key = NULL;

        g_return_if_fail(item);
        g_return_if_fail(attr_name);

	if(!attr_value) {
		if(!item->other_attributes ||
		   !g_hash_table_lookup_extended(item->other_attributes,
						 attr_name,
						 &old_key, &old_val))
			return;
		g_hash_table_remove(item->other_attributes, old_key);
		g_free(old_key);
		g_free(old_val);
		return;
	}

        if(!item->other_attributes)
                item->other_attributes = g_hash_table_new(g_str_hash,
							  g_str_equal);

        if(g_hash_table_lookup_extended(item->other_attributes, attr_name,
					&old_key, &old_val)) {
		g_hash_table_insert(item->other_attributes,
				    old_key, g_strdup(attr_value));
		g_free(old_val);
        } else
		g_hash_table_insert(item->other_attributes,
				    g_strdup(attr_name), g_strdup(attr_value));
}

/**
 * gnome_desktop_item_set_order:
 * @item: A desktop item for a directory
 * @order: A GSList strings of the basenames of the files in this order
 *
 * This function sets the list of strings 'order' as the order of this item
 * which is assumed is a directory item.  It makes a local copy of the
 * 'order' list and the strings within it.
 *
 * Returns:
 */
void
gnome_desktop_item_set_order (GnomeDesktopItem *item, GSList *order)
{
	GSList *li;

        g_return_if_fail(item);
        g_return_if_fail(item->item_flags & GNOME_DESKTOP_ITEM_IS_DIRECTORY);

        g_slist_foreach(item->order, (GFunc)g_free, NULL);
        g_slist_free(item->order);

	/* copy the items */
        item->order = g_slist_copy(order);
	for(li = item->order; li; li = li->next)
		li->data = g_strdup(li->data);
}

/**
 * gnome_desktop_item_set_flags:
 * @item: A desktop item
 * @flags: The flags to be set
 *
 * Sets the flags of 'item' to 'flags'
 *
 * Returns:
 */
void
gnome_desktop_item_set_flags (GnomeDesktopItem *item, GnomeDesktopItemFlags flags)
{
        g_return_if_fail(item);

        item->item_flags = flags;
}

/**
 * gnome_desktop_item_set_format:
 * @item: A desktop item
 * @fmt: The format to be set
 */
void
gnome_desktop_item_set_format (GnomeDesktopItem *item, GnomeDesktopItemFormat fmt)
{
        g_return_if_fail(item);

	/* correctly convert from kde to gnome if the type is
	   KonsoleApplication */
	if(fmt == GNOME_DESKTOP_ITEM_GNOME &&
	   item->item_format == GNOME_DESKTOP_ITEM_KDE &&
	   item->type &&
	   strcmp(item->type,"KonsoleApplication")==0) {
		g_free(item->type);
		item->type = g_strdup("Application");
		item->item_flags |= GNOME_DESKTOP_ITEM_RUN_IN_TERMINAL;
	}

        item->item_format = fmt;

}

/**
 * gnome_desktop_item_set_location:
 * @item: A desktop item
 * @location: A string specifying the file location of this particular item.
 *
 * Set's the 'location' of this item.  It should be a full path to the item.
 *
 * Returns:
 */
void
gnome_desktop_item_set_location (GnomeDesktopItem *item, const char *location)
{
        g_free(item->location);
        item->location = g_strdup(location);
}