File: gnome-vfs-application-registry.c

package info (click to toggle)
gnome-vfs 1:2.24.4-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 17,120 kB
  • ctags: 10,397
  • sloc: ansic: 78,516; sh: 10,341; makefile: 902; perl: 99
file content (2086 lines) | stat: -rw-r--r-- 52,229 bytes parent folder | download | duplicates (3)
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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

/* gnome-vfs-mime-info.c - GNOME mime-information implementation.

   Copyright (C) 1998 Miguel de Icaza
   Copyright (C) 2000 Eazel, Inc
   All rights reserved.

   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.  */
/*
 * Authors: George Lebl
 * 	Based on original mime-info database code by Miguel de Icaza
 */

#include "config.h"
#include "gnome-vfs-application-registry.h"
#include "gnome-vfs-configuration.h"
#include "gnome-vfs-mime-handlers.h"
#include "gnome-vfs-mime-private.h"
#include "gnome-vfs-mime.h"
#include "gnome-vfs-private-utils.h"
#include "gnome-vfs-result.h"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>

#include <glib/gstdio.h>

#if !defined getc_unlocked && !defined HAVE_GETC_UNLOCKED
# define getc_unlocked(fp) getc (fp)
#endif

typedef struct _Application Application;
struct _Application {
	char       *app_id;
	int         ref_count;
	/* The following is true if this was found in the
	 * home directory or if the user changed any keys
	 * here.  It means that it will be saved into a user
	 * file */
	gboolean    user_owned;
	GHashTable *keys;
	GnomeVFSMimeApplicationArgumentType expects_uris;
	GList      *mime_types;
	GList      *supported_uri_schemes;
	/* The user_owned version of this if this is a system
	 * version */
	Application *user_application;
};

/* Describes the directories we scan for information */
typedef struct {
	char *dirname;
	unsigned int valid : 1;
	unsigned int system_dir : 1;
} ApplicationRegistryDir;

/* These ones are used to automatically reload mime info on demand */
static FileDateTracker *registry_date_tracker;
static ApplicationRegistryDir gnome_registry_dir;
static ApplicationRegistryDir user_registry_dir;

/* To initialize the module automatically */
static gboolean gnome_vfs_application_registry_initialized = FALSE;

/* we want to replace the previous key if the current key has a lower
   (ie: better) language level */
static char *previous_key = NULL;
static int previous_key_lang_level = -1;

/*
 * A hash table containing application registry record (Application)
 * keyed by application ids.
 */
static GHashTable *global_applications = NULL;
/*
 * A hash table containing GList's of application registry records (Application)
 * keyed by the mime types
 */
/* specific mime_types (e.g. image/png) */
static GHashTable *specific_mime_types = NULL;
/* generic mime_types (e.g. image/<star>) */
static GHashTable *generic_mime_types = NULL;

/*
 * Dirty flag, just to make sure we don't sync needlessly
 */
static gboolean user_file_dirty = FALSE;

/*
 * Some local prototypes
 */
static void gnome_vfs_application_registry_init (void);
static void application_clear_mime_types (Application *application);

static Application *
application_ref (Application *application)
{
	g_return_val_if_fail(application != NULL, NULL);

	application->ref_count ++;

	return application;
}

static void
hash_foreach_free_key_value(gpointer key, gpointer value, gpointer user_data)
{
	g_free(key);
	g_free(value);
}

static void
application_unref (Application *application)
{
	g_return_if_fail(application != NULL);

	application->ref_count --;

	if (application->ref_count == 0) {
		application_clear_mime_types (application);

		if (application->keys != NULL) {
			g_hash_table_foreach(application->keys, hash_foreach_free_key_value, NULL);
			g_hash_table_destroy(application->keys);
			application->keys = NULL;
		}

		g_free(application->app_id);
		application->app_id = NULL;

		if (application->user_application != NULL) {
			application_unref (application->user_application);
			application->user_application = NULL;
		}

		g_free(application);
	}
}

static Application *
application_new (const char *app_id, gboolean user_owned)
{
	Application *application;

	application = g_new0 (Application, 1);
	application->app_id = g_strdup(app_id);
	application->ref_count = 1;
	application->expects_uris = GNOME_VFS_MIME_APPLICATION_ARGUMENT_TYPE_PATHS;
	application->user_owned = user_owned;

	return application;
}

static Application *
application_lookup_or_create (const char *app_id, gboolean user_owned)
{
	Application *application;

	g_return_val_if_fail(app_id != NULL, NULL);

	gnome_vfs_application_registry_init ();

	application = g_hash_table_lookup (global_applications, app_id);
	if (application != NULL) {
		if ( ! user_owned) {
			/* if we find only a user app, do magic */
			if (application->user_owned) {
				Application *new_application;
				new_application = application_new (app_id, FALSE/*user_owned*/);
				new_application->user_application = application;
				/* override the user application */
				g_hash_table_insert (global_applications, new_application->app_id,
						     new_application);
				return new_application;
			} else {
				return application;
			}
		} else {
			if (application->user_owned) {
				return application;
			} if (application->user_application != NULL) {
				return application->user_application;
			} else {
				Application *new_application;
				new_application = application_new (app_id, TRUE/*user_owned*/);
				application->user_application = new_application;
				return new_application;
			}
		}
	}

	application = application_new (app_id, user_owned);

	g_hash_table_insert (global_applications, application->app_id, application);

	return application;
}

static Application *
application_lookup (const char *app_id)
{
	g_return_val_if_fail(app_id != NULL, NULL);

	if (global_applications == NULL)
		return NULL;

	return g_hash_table_lookup (global_applications, app_id);
}

static const char *
peek_value (const Application *application, const char *key)
{
	g_return_val_if_fail(application != NULL, NULL);
	g_return_val_if_fail(key != NULL, NULL);

	if (application->keys == NULL)
		return NULL;

	return g_hash_table_lookup (application->keys, key);
}

static void
set_value (Application *application, const char *key, const char *value)
{
	char *old_value, *old_key;

	g_return_if_fail(application != NULL);
	g_return_if_fail(key != NULL);
	g_return_if_fail(value != NULL);

	if (application->keys == NULL)
		application->keys = g_hash_table_new (g_str_hash, g_str_equal);

	if (g_hash_table_lookup_extended (application->keys, key,
					  (gpointer *)&old_key,
					  (gpointer *)&old_value)) {
		g_hash_table_insert (application->keys,
				     old_key, g_strdup (value));
		g_free (old_value);
	} else {
		g_hash_table_insert (application->keys,
				     g_strdup (key), g_strdup (value));
	}
}

static void
unset_key (Application *application, const char *key)
{
	char *old_value, *old_key;

	g_return_if_fail(application != NULL);
	g_return_if_fail(key != NULL);

	if (application->keys == NULL)
		return;

	if (g_hash_table_lookup_extended (application->keys, key,
					  (gpointer *)&old_key,
					  (gpointer *)&old_value)) {
		g_hash_table_remove (application->keys, old_key);
		g_free (old_key);
		g_free (old_value);
	}
}

static gboolean
value_looks_true (const char *value)
{
	if (value &&
	    (value[0] == 'T' ||
	     value[0] == 't' ||
	     value[0] == 'Y' ||
	     value[0] == 'y' ||
	     atoi (value) != 0)) {
		return TRUE;
	} else {
		return FALSE;
	}
}

static gboolean
get_bool_value (const Application *application, const char *key,
		gboolean *got_key)
{
	const char *value = peek_value (application, key);
	if (got_key) {
		if (value != NULL)
			*got_key = TRUE;
		else
			*got_key = FALSE;
	}
	return value_looks_true (value);

}

static void
set_bool_value (Application *application, const char *key,
		gboolean value)
{
	set_value (application, key, value ? "true" : "false");
}

static int
application_compare (Application *application1,
		     Application *application2)
{
	return strcmp (application1->app_id, application2->app_id);
}

static void
add_application_to_mime_type_table (Application *application,
				    const char *mime_type)
{
	GList *application_list;
	GHashTable *table;
	char *old_key;

	if (gnome_vfs_mime_type_is_supertype (mime_type))
		table = generic_mime_types;
	else
		table = specific_mime_types;
	
	g_assert (table != NULL);

	if (g_hash_table_lookup_extended (table, mime_type,
					  (gpointer *)&old_key,
					  (gpointer *)&application_list)) {
		/* Sorted order is important as we can then easily
		 * uniquify the results */
		application_list = g_list_insert_sorted
			(application_list,
			 application_ref (application),
			 (GCompareFunc) application_compare);
		g_hash_table_insert (table, old_key, application_list);
	} else {
		application_list = g_list_prepend (NULL,
						   application_ref (application));
		g_hash_table_insert (table, g_strdup (mime_type), application_list);
	}
}

static void
add_mime_type_to_application (Application *application, const char *mime_type)
{
	/* if this exists already, just return */
	if (g_list_find_custom (application->mime_types,
				/*glib is const incorrect*/(gpointer)mime_type,
				(GCompareFunc) strcmp) != NULL)
		return;
	
	application->mime_types =
		g_list_prepend (application->mime_types,
				g_strdup (mime_type));
	
	add_application_to_mime_type_table (application, mime_type);

}

static void
add_supported_uri_scheme_to_application (Application *application,
					 const char *supported_uri_scheme)
{
	if (g_list_find_custom (application->supported_uri_schemes,
				/*glib is const incorrect*/(gpointer) supported_uri_scheme,
				(GCompareFunc) strcmp) != NULL) {
		return;
	}
	
	application->supported_uri_schemes =
		g_list_prepend (application->supported_uri_schemes,
				g_strdup (supported_uri_scheme));

}

static GList *
supported_uri_scheme_list_copy (GList *supported_uri_schemes)
{
	GList *copied_list, *node;

	copied_list = NULL;
	for (node = supported_uri_schemes; node != NULL;
	     node = node->next) {
		copied_list = g_list_prepend (copied_list,
					      g_strdup ((char *) node->data));
	}

	return copied_list;
}

static void
remove_application_from_mime_type_table (Application *application,
					 const char *mime_type)
{
	GHashTable *table;
	char *old_key;
	GList *application_list, *entry;

	if (gnome_vfs_mime_type_is_supertype (mime_type))
		table = generic_mime_types;
	else
		table = specific_mime_types;

	g_assert (table != NULL);

	if (g_hash_table_lookup_extended (table, mime_type,
					  (gpointer *)&old_key,
					  (gpointer *)&application_list)) {
		entry = g_list_find (application_list, application);

		/* if this fails we're in deep doodoo I guess */
		g_assert (entry != NULL);

		application_list = g_list_remove_link (application_list, entry);
		entry->data = NULL;
		application_unref (application);

		if (application_list != NULL) {
			g_hash_table_insert (table, old_key, application_list);
		} else {
			g_hash_table_remove (table, old_key);
			g_free(old_key);
		}
	} else
		g_assert_not_reached ();
}

static void
remove_mime_type_for_application (Application *application, const char *mime_type)
{
	GList *entry;

	g_return_if_fail(application != NULL);
	g_return_if_fail(mime_type != NULL);

	entry = g_list_find_custom
		(application->mime_types,
		 /*glib is const incorrect*/(gpointer)mime_type,
		 (GCompareFunc) strcmp);

	/* if this doesn't exist, just return */
	if (entry == NULL) {
		return;
	}

	remove_application_from_mime_type_table (application, mime_type);

	/* Free data last, in case caller passed in mime_type string
	 * that was stored in this table.
	 */
	application->mime_types =
		g_list_remove_link (application->mime_types, entry);
	g_free (entry->data);
	g_list_free_1 (entry);	
}


static void
application_clear_mime_types (Application *application)
{
	g_return_if_fail (application != NULL);

	while (application->mime_types)
		remove_mime_type_for_application (application, application->mime_types->data);
}

static void
application_remove (Application *application)
{
	Application *main_application;

	g_return_if_fail (application != NULL);

	if (global_applications == NULL) {
		return;
	}

	main_application = application_lookup (application->app_id);
	if (main_application == NULL) {
		return;
	}

	/* We make sure the mime types are killed even if the application
	 * entry lives after unreffing it */
	application_clear_mime_types (application);

	if (main_application == application) {
		if (application->user_application)
			application_clear_mime_types (application->user_application);

		g_hash_table_remove (global_applications, application->app_id);
	} else {
		/* This must be a user application */
		g_assert (main_application->user_application == application);

		main_application->user_application = NULL;
	}

	application_unref (application);

}

static void
sync_key (gpointer key, gpointer value, gpointer user_data)
{
	char *key_string = key;
	char *value_string = value;
	FILE *fp = user_data;

	fprintf (fp, "\t%s=%s\n", key_string, value_string);
}

/* write an application to a file */
static void
application_sync (Application *application, FILE *fp)
{
	GList *li;

	g_return_if_fail (application != NULL);
	g_return_if_fail (application->app_id != NULL);
	g_return_if_fail (fp != NULL);

	fprintf (fp, "%s\n", application->app_id);

	if (application->keys != NULL)
		g_hash_table_foreach (application->keys, sync_key, fp);

	if (application->mime_types != NULL) {
		char *separator;
		fprintf (fp, "\tmime_types=");
		separator = "";
		for (li = application->mime_types; li != NULL; li = li->next) {
			char *mime_type = li->data;
			fprintf (fp, "%s%s", separator, mime_type);
			separator = ",";
		}
		fprintf (fp, "\n");
	}
	fprintf (fp, "\n");
}

/* this gives us a number of the language in the current language list,
   the lower the number the "better" the translation */
static int
language_level (const char *lang)
{
	const char * const *lang_list;
	int i;

	if (lang == NULL)
		lang = "C";

	/* The returned list is sorted from most desirable to least
            desirable and always contains the default locale "C". */
	lang_list = g_get_language_names();

	for (i = 0; lang_list[i]; i++)
		if (strcmp (lang_list[i], lang) == 0)
			return i;

	return -1;
}

static void
application_add_key (Application *application, const char *key,
		     const char *lang, const char *value)
{
	int lang_level;

	g_return_if_fail (application != NULL);
	g_return_if_fail (key != NULL);
	g_return_if_fail (value != NULL);

	if (strcmp (key, "mime_types") == 0 ||
	    strcmp (key, "supported_uri_schemes") == 0) {
		char *value_copy = g_strdup (value);
		char *next_value;
		/* FIXME: There used to be a check here for
		   the value of "lang", but spamming
		   the terminal about it is not really
		   the right way to deal with that, nor
		   is "MIME Types can't have languages, bad!"
		   which is what was here before */
		next_value = strtok (value_copy, ", \t");
		while (next_value != NULL) {
			if (strcmp (key, "mime_types") == 0) {
				add_mime_type_to_application (application, next_value);
			}
			else {
				add_supported_uri_scheme_to_application (application, next_value);
			}
			next_value = strtok (NULL, ", \t");
		}
		g_free(value_copy);
		/* fall through so that we can store the values as keys too */
	}
	else if (strcmp (key, "expects_uris") == 0) {
		if (strcmp (value, "non-file") == 0) {
			application->expects_uris = GNOME_VFS_MIME_APPLICATION_ARGUMENT_TYPE_URIS_FOR_NON_FILES;
		}
		else if (value_looks_true (value)) {
			application->expects_uris = GNOME_VFS_MIME_APPLICATION_ARGUMENT_TYPE_URIS;
		}
		else {
			application->expects_uris = GNOME_VFS_MIME_APPLICATION_ARGUMENT_TYPE_PATHS;
		}
	}

	lang_level = language_level (lang);
	/* wrong language completely */
	if (lang_level < 0)
		return;

	/* if we have some language defined and
	   if there was a previous_key */
	if (lang_level > 0 &&
	    previous_key &&
	    /* our language level really sucks and the previous
	       translation was of better language quality so just
	       ignore us */
	    previous_key_lang_level < lang_level) {
		return;
	}

	set_value (application, key, value);

	/* set this as the previous key */
	g_free(previous_key);
	previous_key = g_strdup(key);
	previous_key_lang_level = lang_level;
}

typedef enum {
	STATE_NONE,
	STATE_LANG,
	STATE_LOOKING_FOR_KEY,
	STATE_ON_APPLICATION,
	STATE_ON_KEY,
	STATE_ON_VALUE
} ParserState;

/**
 * strip_trailing_whitespace
 *
 * string
 *
 * strips the white space from a string.
 *
 */

static void
strip_trailing_whitespace (GString *string)
{
	int i;

	for (i = string->len - 1; i >= 0; i--) {
		if (!g_ascii_isspace (string->str[i]))
			break;
	}

	g_string_truncate (string, i + 1);
}

/**
 * load_application_info_from
 *
 * filename:  Target filename to application info from.
 * user_owned: if application is user owned or not.
 *
 * This function will load application info from a file and parse through the
 * application loading the registry with the information contained in the file.
 *
 * 
 **/

static void
load_application_info_from (const char *filename, gboolean user_owned)
{
	FILE *fp;
	gboolean in_comment, app_used;
	GString *line;
	int column, c;
	ParserState state;
	Application *application;
	char *key;
	char *lang;

	fp = g_fopen (filename, "r");
	if (fp == NULL)
		return;

	in_comment = FALSE;
	app_used = FALSE;
	column = -1;
	application = NULL;
	key = NULL;
	lang = NULL;
	line = g_string_sized_new (120);
	state = STATE_NONE;
	
	while ((c = getc_unlocked (fp)) != EOF){
		column++;
		if (c == '\r')
			continue;

		if (c == '#' && column == 0){		
			in_comment = TRUE;
			continue;
		}
		
		if (c == '\n'){
			in_comment = FALSE;
			column = -1;
			if (state == STATE_ON_APPLICATION) {

				/* set previous key to nothing
				   for this mime type */
				g_free(previous_key);
				previous_key = NULL;
				previous_key_lang_level = -1;

				strip_trailing_whitespace (line);
				application = application_lookup_or_create (line->str, user_owned);
				app_used = FALSE;
				g_string_assign (line, "");
				state = STATE_LOOKING_FOR_KEY;
				continue;
			}
			if (state == STATE_ON_VALUE){
				app_used = TRUE;
				application_add_key (application, key, lang, line->str);
				g_string_assign (line, "");
				g_free (key);
				key = NULL;
				g_free (lang);
				lang = NULL;
				state = STATE_LOOKING_FOR_KEY;
				continue;
			}
			continue;
		}

		if (in_comment)
			continue;

		switch (state){
		case STATE_NONE:
			if (c != ' ' && c != '\t')
				state = STATE_ON_APPLICATION;
			else
				break;
			/* fall down */
			
		case STATE_ON_APPLICATION:
			if (c == ':'){
				in_comment = TRUE;
				break;
			}
			g_string_append_c (line, c);
			break;

		case STATE_LOOKING_FOR_KEY:
			if (c == '\t' || c == ' ')
				break;

			if (c == '['){
				state = STATE_LANG;
				break;
			}

			if (column == 0){
				state = STATE_ON_APPLICATION;
				g_string_append_c (line, c);
				break;
			}
			state = STATE_ON_KEY;
			/* falldown */

		case STATE_ON_KEY:
			if (c == '\\'){
				c = getc (fp);
				if (c == EOF)
					break;
			}
			if (c == '='){
				key = g_strdup (line->str);
				g_string_assign (line, "");
				state = STATE_ON_VALUE;
				break;
			}
			g_string_append_c (line, c);
			break;

		case STATE_ON_VALUE:
			g_string_append_c (line, c);
			break;
			
		case STATE_LANG:
			if (c == ']'){
				state = STATE_ON_KEY;      
				if (line->str [0]){
					g_free(lang);
					lang = g_strdup(line->str);
				} else {
					in_comment = TRUE;
					state = STATE_LOOKING_FOR_KEY;
				}
				g_string_assign (line, "");
				break;
			}
			g_string_append_c (line, c);
			break;
		}
	}

	if (application){
		if (key && line->str [0])
			application_add_key (application, key, lang, line->str);
		else
			if ( ! app_used)
				application_remove (application);
	}

	g_string_free (line, TRUE);
	g_free (key);
	g_free (lang);

	/* free the previous_key stuff */
	g_free(previous_key);
	previous_key = NULL;
	previous_key_lang_level = -1;

	fclose (fp);

	_gnome_vfs_file_date_tracker_start_tracking_file (registry_date_tracker, filename);
}

/**
 * application_info_load
 *
 * source:
 *
 * 
 */

static void
application_info_load (ApplicationRegistryDir *source)
{
	GDir *dir;
	const char *dent;
	const int extlen = sizeof (".applications") - 1;
	char *filename;
	struct stat s;

	if (g_stat (source->dirname, &s) != -1)
		source->valid = TRUE;
	else
		source->valid = FALSE;
	
	dir = g_dir_open (source->dirname, 0, NULL);
	if (dir == NULL) {
		source->valid = FALSE;
		return;
	}
	if (source->system_dir) {
		filename = g_build_filename (source->dirname,
					     "gnome-vfs.applications",
					     NULL);
		load_application_info_from (filename, FALSE /*user_owned*/);
		g_free (filename);
	}

	while ((dent = g_dir_read_name (dir)) != NULL){
		
		int len = strlen (dent);

		if (len <= extlen)
			continue;
		if (strcmp (dent + len - extlen, ".applications"))
			continue;
		if (source->system_dir && strcmp (dent, "gnome-vfs.applications") == 0)
			continue;
		if ( ! source->system_dir && strcmp (dent, "user.applications") == 0)
			continue;
		filename = g_build_filename (source->dirname, dent, NULL);
		load_application_info_from (filename, FALSE /*user_owned*/);
		g_free (filename);
	}

	if ( ! source->system_dir) {
		filename = g_build_filename (source->dirname, "user.applications", NULL);
		/* Currently this is the only file that is "user
		 * owned".  It actually makes sense.  Editting of
		 * other files from the API would be too complex
		 */
		load_application_info_from (filename, TRUE /*user_owned*/);
		g_free (filename);
	}
	g_dir_close (dir);

	_gnome_vfs_file_date_tracker_start_tracking_file (registry_date_tracker, source->dirname);
}

/**
 * load_application_info
 *
 * This function will load the registry for an application from disk.
 **/

static void
load_application_info (void)
{
	application_info_load (&gnome_registry_dir);
	application_info_load (&user_registry_dir);
}

/**
 * gnome_vfs_application_registry_init
 *
 * This function initializes the gnome-vfs application registry.  
 *
 **/

static void
gnome_vfs_application_registry_init (void)
{
	if (gnome_vfs_application_registry_initialized)
		return;

	registry_date_tracker = _gnome_vfs_file_date_tracker_new ();

	/*
	 * The hash tables that store the mime keys.
	 */
	global_applications = g_hash_table_new (g_str_hash, g_str_equal);
	generic_mime_types  = g_hash_table_new (g_str_hash, g_str_equal);
	specific_mime_types  = g_hash_table_new (g_str_hash, g_str_equal);
	
	/*
	 * Setup the descriptors for the information loading
	 */

	gnome_registry_dir.dirname = g_build_filename (GNOME_VFS_DATADIR,
						       "application-registry",
						       NULL);
	gnome_registry_dir.system_dir = TRUE;
	
	user_registry_dir.dirname = g_build_filename (gnome_vfs_get_home_dir(),
						      ".gnome",
						      "application-info",
						      NULL);
	user_registry_dir.system_dir = FALSE;

	/* Make sure user directory exists */
	if (g_mkdir (user_registry_dir.dirname, 0700) &&
	    errno != EEXIST) {
		g_warning("Could not create per-user Gnome application-registry directory: %s",
			  user_registry_dir.dirname);
	}

	/* Things have been initialized flag it as ready so that we can load
	 * the applications without attempting to reinitialize
	 */
	gnome_vfs_application_registry_initialized = TRUE;

	load_application_info ();
}

/*
 * maybe_reload
 *
 * This function will initialize the registry in memory and then reloads the
 *
 */

static void
maybe_reload (void)
{
	gnome_vfs_application_registry_init ();

	if (!_gnome_vfs_file_date_tracker_date_has_changed (registry_date_tracker)) {
		return;
	}
	
        gnome_vfs_application_registry_reload ();
}

/**
 * remove_apps
 *
 * key: 
 * value:
 * user_data:
 *
 * FIXME: I need a clearer explanation on what this does.
 *
 */

static gboolean
remove_apps (gpointer key, gpointer value, gpointer user_data)
{
	Application *application = value;

	application_clear_mime_types (application);

	application_unref (application);
	
	return TRUE;
}

/**
 * gnome_vfs_application_registry_clear:
 *
 * This will wipe the registry clean removing everything from the registry.
 * This is different from gnome_vfs_application_registry_shutdown which will
 * actually delete the registry and leave it in an uninitialized state.
 *

 */

static void
gnome_vfs_application_registry_clear (void)
{
	if (global_applications != NULL)
		g_hash_table_foreach_remove (global_applications, remove_apps, NULL);
}

/**
 * gnome_vfs_application_registry_shutdown
 *
 * Synchronize gnome-vfs application registry data to disk, and free
 * resources.
 * 
 * Deprecated: All application registry functions have been
 * deprecated. Use the functions available in gnome-mime-handlers
 * instead.
 */

void
gnome_vfs_application_registry_shutdown (void)
{
	gnome_vfs_application_registry_clear ();

	if (global_applications != NULL) {
		g_hash_table_destroy (global_applications);
		global_applications = NULL;
	}

	if(generic_mime_types != NULL) {
		g_hash_table_destroy (generic_mime_types);
		generic_mime_types = NULL;
	}

	if(specific_mime_types != NULL) {
		g_hash_table_destroy (specific_mime_types);
		specific_mime_types = NULL;
	}

	_gnome_vfs_file_date_tracker_free (registry_date_tracker);

	g_free(gnome_registry_dir.dirname);
	gnome_registry_dir.dirname = NULL;
	g_free(user_registry_dir.dirname);
	user_registry_dir.dirname = NULL;

	gnome_vfs_application_registry_initialized = FALSE;
}


/**
 * gnome_vfs_application_registry_reload
 *
 * If this function is called for the first time it will initialize the
 * registry.  Subsequent calls to the function will clear out the current
 * registry contents and load registry contents from the save file.  Make
 * certain that you've saved your registry before calling this function.  It
 * will destroy unsaved changes.
 *
 * Deprecated: All application registry functions have been
 * deprecated. Use the functions available in gnome-mime-handlers
 * instead.
 */

void
gnome_vfs_application_registry_reload (void)
{
	if ( ! gnome_vfs_application_registry_initialized) {
		/* If not initialized, initialization will do a "reload" */
		gnome_vfs_application_registry_init ();
	} else {
		gnome_vfs_application_registry_clear ();
		load_application_info ();
	}
}

/*
 * Existance check
 */

/**
 * gnome_vfs_application_registry_exists
 * @app_id: an application ID
 *
 * This function will return TRUE if there is an entry for @app_id in
 * the registry, otherwise FALSE.
 *
 * Returns: TRUE if the application is in the registry, FALSE if not 
 *
 * Deprecated: All application registry functions have been
 * deprecated. Use the functions available in gnome-mime-handlers
 * instead.
 *
 */

gboolean
gnome_vfs_application_registry_exists (const char *app_id)
{
	g_return_val_if_fail (app_id != NULL, FALSE);

	maybe_reload ();

	if (application_lookup (app_id) != NULL)
		return TRUE;
	else
		return FALSE;
}


/*
 * Getting arbitrary keys
 */


/**
 * get_keys_foreach
 *
 * key:
 * user_data:
 *
 * FIXME: I have no idea what this function does.
 **/

static void
get_keys_foreach(gpointer key, gpointer value, gpointer user_data)
{
	GList **listp = user_data;

	/* make sure we only insert unique keys */
	if ( (*listp) && strcmp ((const char *) (*listp)->data, (const char *) key) == 0)
		return;

	(*listp) = g_list_insert_sorted ((*listp), key,
					 (GCompareFunc) strcmp);
}

/**
 * gnome_vfs_application_registry_get_keys:
 * @app_id: the application ID for which to get keys
 * 
 * This function wil return a list of strings which is the list of
 * keys set for @app_id in the application registry.
 *
 * Returns: A list of the keys set for @app_id
 *
 * Deprecated: All application registry functions have been
 * deprecated. Use the functions available in gnome-mime-handlers
 * instead.
 *
 */

GList *
gnome_vfs_application_registry_get_keys (const char *app_id)
{
	GList *retval;
	Application *application;

	g_return_val_if_fail (app_id != NULL, NULL);

	maybe_reload ();

	application = application_lookup (app_id);
	if (application == NULL)
		return NULL;

	retval = NULL;

	if (application->keys != NULL)
		g_hash_table_foreach (application->keys, get_keys_foreach,
				      &retval);

	if (application->user_application != NULL &&
	    application->user_application->keys)
		g_hash_table_foreach (application->user_application->keys,
				      get_keys_foreach, &retval);

	return retval;
}


/**
 * real_peek_value:
 *
 * @application: registry application
 * @key: target key
 *
 * Returns: the value associated with the key, or NULL if there is no
 * associated value.
 *
 * This function looks and returns the value of the target key in the registry
 * application
 *
 */

static const char *
real_peek_value (const Application *application, const char *key)
{
	const char *retval;

	g_return_val_if_fail (application != NULL, NULL);
	g_return_val_if_fail (key != NULL, NULL);

	retval = NULL;

	if (application->user_application)
		retval = peek_value (application->user_application, key);

	if (retval == NULL)
		retval = peek_value (application, key);

	return retval;
}

/**
 * real_get_bool_value
 *
 * application:  Application structure
 * key: taget key
 * got_key: actual key stored in application if key exists.
 *
 * This function will try to determine whether a key exists in the application.
 * It first checks the user applications and then the system applications and
 * then returns whether the key exists and what the value is from the value of
 * got_key.
 *
 * Returns: gboolean
 **/

static gboolean
real_get_bool_value (const Application *application, const char *key, gboolean *got_key)
{
	gboolean sub_got_key, retval;

	g_return_val_if_fail (application != NULL, FALSE);
	g_return_val_if_fail (key != NULL, FALSE);

	sub_got_key = FALSE;
	retval = FALSE;
	if (application->user_application)
		retval = get_bool_value (application->user_application, key,
					 &sub_got_key);

	if ( ! sub_got_key)
		retval = get_bool_value (application, key, &sub_got_key);

	if (got_key != NULL)
		*got_key = sub_got_key;

	return retval;
}


/**
 * gnome_vfs_application_registry_peek_value
 * @app_id: the application ID for which to look up a value
 * @key: the key to look up
 *
 * This will return the value associated with @key for @app_id in the
 * application registry. There is no need to free the return value.
 *
 * Returns: the value associated with the key, or NULL if there is no
 * associated value
 *
 *
 * Deprecated: All application registry functions have been
 * deprecated. Use the functions available in gnome-mime-handlers
 * instead.
 */

const char *
gnome_vfs_application_registry_peek_value (const char *app_id, const char *key)
{
	Application *application;

	g_return_val_if_fail (app_id != NULL, NULL);
	g_return_val_if_fail (key != NULL, NULL);

	maybe_reload ();

	application = application_lookup (app_id);
	if (application == NULL)
		return NULL;

	return real_peek_value (application, key);
}

/**
 * gnome_vfs_application_registry_get_bool_value
 * @app_id:  registry id of the application
 * @key: key to look up
 * @got_key: TRUE if a setting was dound, otherwise FALSE
 *
 * This will look up a key in the structure pointed to by app_id and return the
 * boolean value of that key.  It will return false if there are no
 * applications associated with the app_id.
 *
 * Returns: TRUE if @key is set to "true" or "yes" for @app_id, otherwise FALSE
 *
 * Deprecated: All application registry functions have been
 * deprecated. Use the functions available in gnome-mime-handlers
 * instead.
 *
 */

gboolean
gnome_vfs_application_registry_get_bool_value (const char *app_id, const char *key,
					       gboolean *got_key)
{
	Application *application;

	g_return_val_if_fail (app_id != NULL, FALSE);
	g_return_val_if_fail (key != NULL, FALSE);

	maybe_reload ();

	application = application_lookup (app_id);
	if (application == NULL)
		return FALSE;

	return real_get_bool_value (application, key, got_key);
}

/*
 * Setting stuff
 */

/**
 * gnome_vfs_application_registry_remove_application
 * @app_id:  registry id of the application
 *
 * Given the registry id this function will remove all applications that has
 * been set by the user.  You will need to call
 * gnome_vfs_application_registry_sync to save the changes.
 *
 * Deprecated: All application registry functions have been
 * deprecated. Use the functions available in gnome-mime-handlers
 * instead.
 *
 */

void
gnome_vfs_application_registry_remove_application (const char *app_id)
{
	Application *application;

	g_return_if_fail (app_id != NULL);

	maybe_reload ();

	application = application_lookup (app_id);
	if (application == NULL)
		return;

	/* Only remove the user_owned stuff */
	if (application->user_owned) {
		application_remove (application);
		user_file_dirty = TRUE;
	} else if (application->user_application != NULL) {
		application_remove (application->user_application);
		user_file_dirty = TRUE;
	}
}

/**
 * gnome_vfs_application_registry_set_value
 * @app_id:  registry id of the application
 * @key: target key
 * @value: value to set the target key to
 *
 * This function will set values pertaining to registry entry pointed to by
 * app_id.  You will need to call gnome_vfs_application_registry_sync to
 * realize the changes.
 *
 * Deprecated: All application registry functions have been
 * deprecated. Use the functions available in gnome-mime-handlers
 * instead.
 *
 */

void
gnome_vfs_application_registry_set_value (const char *app_id,
					  const char *key,
					  const char *value)
{
	Application *application;

	g_return_if_fail (app_id != NULL);
	g_return_if_fail (key != NULL);
	g_return_if_fail (value != NULL);

	maybe_reload ();

	application = application_lookup_or_create (app_id, TRUE/*user_owned*/);

	set_value (application, key, value);

	user_file_dirty = TRUE;
}

/**
 * gnome_vfs_application_registry_set_bool_value:
 * @app_id:  registry id of the application
 * @key: target key
 * @value: value you want to set the target key to
 *
 * This function will modify those registry values that are of type boolean to
 * a value specified by the user.  You will need to call
 * gnome_vfs_application_registry_sync to save your changes.
 *
 * Deprecated: All application registry functions have been
 * deprecated. Use the functions available in gnome-mime-handlers
 * instead.
 *
 */
void
gnome_vfs_application_registry_set_bool_value (const char *app_id,
					       const char *key,
					       gboolean value)
{
	Application *application;

	g_return_if_fail (app_id != NULL);
	g_return_if_fail (key != NULL);

	maybe_reload ();

	application = application_lookup_or_create (app_id, TRUE/*user_owned*/);

	set_bool_value (application, key, value);

	user_file_dirty = TRUE;
}

/**
 * gnome_vfs_application_registry_unset_key:
 * @app_id: registry id of the application
 * @key: search key
 *
 * This function given the application and the target will wipe the current
 * value that the key contains.
 *
 * Deprecated: All application registry functions have been
 * deprecated. Use the functions available in gnome-mime-handlers
 * instead.
 * 
 */

void
gnome_vfs_application_registry_unset_key (const char *app_id,
					  const char *key)
{
	Application *application;

	g_return_if_fail (app_id != NULL);
	g_return_if_fail (key != NULL);

	maybe_reload ();

	application = application_lookup_or_create (app_id, TRUE/*user_owned*/);

	unset_key (application, key);

	user_file_dirty = TRUE;
}

/*
 * Query functions
 */


static void
cb_application_collect (gpointer key, gpointer value, gpointer user_data)
{
	Application  *application = value;
	GList       **list = user_data;
	*list = g_list_prepend (*list, application->app_id);
}

/**
 * gnome_vfs_application_registry_get_applications
 * @mime_type:  mime type string
 *
 * This will return all applications from the registry that are associated with
 * the given mime type string, if NULL it returns all applications.
 *
 * Returns: a list of the application IDs for all applications which
 * support the given mime type.
 *
 * Deprecated: All application registry functions have been
 * deprecated. Use the functions available in gnome-mime-handlers
 * instead.
 *
 */

GList *
gnome_vfs_application_registry_get_applications (const char *mime_type)
{
	GList *app_list, *app_list2, *retval, *li;
	char *supertype;

	retval = NULL;
	app_list2 = NULL;

	maybe_reload ();

	if (mime_type == NULL) {
		g_hash_table_foreach (global_applications, cb_application_collect, &retval);
		return retval;
	}

	if (gnome_vfs_mime_type_is_supertype (mime_type)) {
		app_list = g_hash_table_lookup (generic_mime_types, mime_type);
	} else {
		app_list = g_hash_table_lookup (specific_mime_types, mime_type);

		supertype = gnome_vfs_get_supertype_from_mime_type (mime_type);
		if (supertype != NULL) {
			app_list2 = g_hash_table_lookup (generic_mime_types, supertype);
			g_free (supertype);
		}
	}

	for (li = app_list; li != NULL; li = li->next) {
		Application *application = li->data;
		/* Note that this list is sorted so to kill duplicates
		 * in app_list we only need to check the first entry */
		if (retval == NULL ||
		    strcmp ((const char *) retval->data, application->app_id) != 0)
			retval = g_list_prepend (retval, application->app_id);
	}

	for (li = app_list2; li != NULL; li = li->next) {
		Application *application = li->data;
		if (g_list_find_custom (retval, application->app_id,
					(GCompareFunc) strcmp) == NULL)
			retval = g_list_prepend (retval, application->app_id);
	}

	return retval;
}

/**
 * gnome_vfs_application_registry_get_mime_types
 * @app_id: registry id of application
 *
 * This function returns a list of strings that represent the mime
 * types that can be handled by an application.
 *
 * Returns: a list of the mime types supported
 *
 * Deprecated: All application registry functions have been
 * deprecated. Use the functions available in gnome-mime-handlers
 * instead.
 *
 */

GList *
gnome_vfs_application_registry_get_mime_types (const char *app_id)
{
	Application *application;
	GList *retval;

	g_return_val_if_fail (app_id != NULL, NULL);

	maybe_reload ();

	application = application_lookup (app_id);
	if (application == NULL)
		return NULL;

	retval = g_list_copy (application->mime_types);

	/* merge in the mime types from the user_application,
	 * if it exists */
	if (application->user_application) {
		GList *li;
		for (li = application->user_application->mime_types;
		     li != NULL;
		     li = li->next) {
			char *mimetype = li->data;
			if (g_list_find_custom (retval, mimetype,
						(GCompareFunc) strcmp) == NULL)
				retval = g_list_prepend (retval,
							 mimetype);
		}
	}

	return retval;
}

/**
 * gnome_vfs_application_registry_supports_uri_scheme
 * @app_id: registry id of application
 * @uri_scheme: uri schme string
 *
 * Given the id of the application this function will determine if the
 * uri scheme will given is supported.
 *
 * Returns: TRUE if @app_id supports @uri_scheme, otherwise FALSE
 *
 * Deprecated: All application registry functions have been
 * deprecated. Use the functions available in gnome-mime-handlers
 * instead.
 *
 */

gboolean
gnome_vfs_application_registry_supports_uri_scheme (const char *app_id,
						    const char *uri_scheme)
{
	Application *application;
	gboolean uses_gnomevfs;
	
	g_return_val_if_fail (app_id != NULL, FALSE);
	g_return_val_if_fail (uri_scheme != NULL, FALSE);

	maybe_reload ();

	application = application_lookup (app_id);
	if (application == NULL)
		return FALSE;

	uses_gnomevfs = real_get_bool_value (application, GNOME_VFS_APPLICATION_REGISTRY_USES_GNOMEVFS, NULL);

	if (strcmp (uri_scheme, "file") == 0 &&
	    uses_gnomevfs == FALSE &&
	    application->supported_uri_schemes == NULL &&
	    application->user_application->supported_uri_schemes == NULL) {
		return TRUE;
	}

	/* check both the application and the user application
	 * mime_types lists */
	/* FIXME: This method does not allow a user to override and remove
	   uri schemes that an application can handle.  Is this an issue? */

	if ((g_list_find_custom (application->supported_uri_schemes,
				 /*glib is const incorrect*/(gpointer)uri_scheme,
				(GCompareFunc) strcmp) != NULL) ||
	    (application->user_application &&
	     g_list_find_custom (application->user_application->supported_uri_schemes,
				 /*glib is const incorrect*/(gpointer) uri_scheme,
				 (GCompareFunc) strcmp) != NULL)) {
		return TRUE;
	}
	/* check in the list of uris supported by gnome-vfs if necessary */
	else if (uses_gnomevfs) {
		GList *supported_uris;
		gboolean res;
		
		supported_uris = _gnome_vfs_configuration_get_methods_list();
		res = (g_list_find_custom(supported_uris,
					  /*glib is const incorrect*/(gpointer) uri_scheme,
					  (GCompareFunc) strcmp) != NULL);

		g_list_foreach(supported_uris, (GFunc) g_free, NULL);
		g_list_free(supported_uris);

		return res;
	}

	return FALSE;
}

/**
 * gnome_vfs_application_registry_supports_mime_type
 * @app_id: registry id of application
 * @mime_type: mime type string
 *
 * Use this function to see if there is an application associated with a given
 * mime type.  The function will return true or false.
 *
 * Returns: TRUE if @app_id supports @mime_type, otherwise FALSE.
 *
 * Deprecated: All application registry functions have been
 * deprecated. Use the functions available in gnome-mime-handlers
 * instead.
 *
 */

gboolean
gnome_vfs_application_registry_supports_mime_type (const char *app_id,
						   const char *mime_type)
{
	Application *application;

	g_return_val_if_fail (app_id != NULL, FALSE);
	g_return_val_if_fail (mime_type != NULL, FALSE);

	maybe_reload ();

	application = application_lookup (app_id);
	if (application == NULL)
		return FALSE;

	/* check both the application and the user application
	 * mime_types lists */
	/* FIXME: This method does not allow a user to override and remove
	   mime types that an application can handle.  Is this an issue? */
	if ((g_list_find_custom (application->mime_types,
				 /*glib is const incorrect*/(gpointer)mime_type,
				(GCompareFunc) strcmp) != NULL) ||
	    (application->user_application &&
	     g_list_find_custom (application->user_application->mime_types,
				 /*glib is const incorrect*/(gpointer)mime_type,
				 (GCompareFunc) strcmp) != NULL))
		return TRUE;
	else
		return FALSE;
}


/*
 * Mime type functions
 * Note that mime_type can be a specific (image/png) or generic (image/<star>) type
 */


/**
 * gnome_vfs_application_registry_clear_mime_types
 * @app_id: Application id
 *
 * This function will remove the mime types associated with the application.
 * Changes are not realized until the  gnome_vfs_application_registry_sync
 * function is called to save the changes to the file.
 *
 * Deprecated: All application registry functions have been
 * deprecated. Use the functions available in gnome-mime-handlers
 * instead.
 *
 */

void
gnome_vfs_application_registry_clear_mime_types (const char *app_id)
{
	Application *application;

	g_return_if_fail (app_id != NULL);

	maybe_reload ();

	application = application_lookup_or_create (app_id, TRUE/*user_owned*/);

	application_clear_mime_types (application);

	user_file_dirty = TRUE;
}

/**
 * gnome_vfs_application_registry_add_mime_type
 * @app_id: registry id of application
 * @mime_type: mime type string
 *
 * This function will associate a mime type with an application given the   
 * application registry id and the mime type.  Changes are not realized until
 * the gnome_vfs_application_registry_sync function is called to save the
 * changes to the file.
 *
 * Deprecated: All application registry functions have been
 * deprecated. Use the functions available in gnome-mime-handlers
 * instead.
 *
 */

void
gnome_vfs_application_registry_add_mime_type (const char *app_id,
					      const char *mime_type)
{
	Application *application;

	g_return_if_fail (app_id != NULL);
	g_return_if_fail (mime_type != NULL);

	maybe_reload ();

	application = application_lookup_or_create (app_id, TRUE/*user_owned*/);

	add_mime_type_to_application (application, mime_type);

	user_file_dirty = TRUE;
}

/**
 * gnome_vfs_application_registry_remove_mime_type
 * @app_id: registry id of the application
 * @mime_type: mime type string
 *
 * This function will de-associate a mime type from an application registry.  
 * Given the application registry id and the mime type.  Changes are not
 * realized until the gnome_vfs_application_registry_sync function is called to
 * save the changes to the file.
 *
 * Deprecated: All application registry functions have been
 * deprecated. Use the functions available in gnome-mime-handlers
 * instead.
 *
 */

void
gnome_vfs_application_registry_remove_mime_type (const char *app_id,
						 const char *mime_type)
{
	Application *application;

	g_return_if_fail (app_id != NULL);

	maybe_reload ();

	application = application_lookup_or_create (app_id, TRUE/*user_owned*/);

	remove_mime_type_for_application (application, mime_type);

	user_file_dirty = TRUE;
}

/*
 * Syncing to disk
 */

static void
application_sync_foreach (gpointer key, gpointer value, gpointer user_data)
{
	Application *application = value;
	FILE *fp = user_data;

	/* Only sync things that are user owned */
	if (application->user_owned)
		application_sync (application, fp);
	else if (application->user_application)
		application_sync (application->user_application, fp);
}

/**
 * gnome_vfs_application_registry_sync
 *
 * This function will sync the registry.  Typically you would use this function
 * after a modification of the registry.  When you modify the registry a dirty
 * flag is set.  Calling this function will save your modifications to disk and
 * reset the flag.
 *
 * If successful, will return GNOME_VFS_OK
 *
 * Returns: GnomeVFSResult
 *
 * Deprecated: All application registry functions have been
 * deprecated. Use the functions available in gnome-mime-handlers
 * instead.
 *
 */

GnomeVFSResult
gnome_vfs_application_registry_sync (void)
{
	FILE *fp;
	char *file;
	time_t curtime;

	if ( ! user_file_dirty)
		return GNOME_VFS_OK;

	maybe_reload ();

	file = g_build_filename (user_registry_dir.dirname, "user.applications", NULL);
	fp = g_fopen (file, "w");

	if ( ! fp) {
		g_warning ("Cannot open '%s' for writing", file);
		g_free (file);
		return gnome_vfs_result_from_errno ();
	}

	g_free (file);

	time(&curtime);

	fprintf (fp, "# This file is automatically generated by gnome-vfs "
		 "application registry\n"
		 "# Do NOT edit by hand\n# Generated: %s\n",
		 ctime (&curtime));

	if (global_applications != NULL)
		g_hash_table_foreach (global_applications, application_sync_foreach, fp);

	fclose (fp);

	user_file_dirty = FALSE;

	return GNOME_VFS_OK;
}


/**
 * gnome_vfs_application_registry_get_mime_application
 * @app_id:  registry id of the application
 *
 * Returns a structure that contains the application that handles
 * the mime type associated by the application referred by app_id.
 *
 * Returns: GnomeVFSMimeApplication
 *
 * Deprecated: All application registry functions have been
 * deprecated. Use the functions available in gnome-mime-handlers
 * instead.
 *
 */

GnomeVFSMimeApplication *
gnome_vfs_application_registry_get_mime_application (const char *app_id)
{
	Application *i_application;
	GnomeVFSMimeApplication *application;
	gboolean uses_gnomevfs = FALSE;

	g_return_val_if_fail (app_id != NULL, NULL);

	maybe_reload ();

	i_application = application_lookup (app_id);

	if (i_application == NULL)
		return NULL;

	application = g_new0 (GnomeVFSMimeApplication, 1);

	application->id = g_strdup (app_id);

	application->name =
		g_strdup (real_peek_value
			  (i_application,
			   GNOME_VFS_APPLICATION_REGISTRY_NAME));
	application->command =
		g_strdup (real_peek_value
			  (i_application,
			   GNOME_VFS_APPLICATION_REGISTRY_COMMAND));

	application->can_open_multiple_files =
		real_get_bool_value
			(i_application,
			 GNOME_VFS_APPLICATION_REGISTRY_CAN_OPEN_MULTIPLE_FILES,
			 NULL);
	application->expects_uris = i_application->expects_uris;
	application->supported_uri_schemes = 
		supported_uri_scheme_list_copy (i_application->supported_uri_schemes);

	application->requires_terminal =
		real_get_bool_value
			(i_application,
			 GNOME_VFS_APPLICATION_REGISTRY_REQUIRES_TERMINAL,
			 NULL);

	uses_gnomevfs = real_get_bool_value (i_application, GNOME_VFS_APPLICATION_REGISTRY_USES_GNOMEVFS, NULL);

	if (uses_gnomevfs) {
		GList *methods_list = 
			_gnome_vfs_configuration_get_methods_list();
		GList *l;
		if (application->expects_uris == GNOME_VFS_MIME_APPLICATION_ARGUMENT_TYPE_PATHS) {
			application->expects_uris = GNOME_VFS_MIME_APPLICATION_ARGUMENT_TYPE_URIS;
		}
		for (l = methods_list; l != NULL; l = l->next) {
			if (g_list_find_custom (application->supported_uri_schemes,
						/*glib is const incorrect*/(gpointer) l->data,
						(GCompareFunc) strcmp) == NULL) {
				application->supported_uri_schemes = 
					g_list_prepend(application->supported_uri_schemes, 
						       l->data);
			} else {
				g_free(l->data);
			}			
		}
		g_list_free(methods_list);
	}	

	return application;
}

/**
 * gnome_vfs_application_registry_save_mime_application
 * @application: application associated with the mime type
 * 
 * This will save to the registry the application that will be associated with
 * a defined mime type.  The defined mime type is located within the
 * GnomeVFSMimeApplication structure.  Changes are not realized until the
 * gnome_vfs_application_registry_sync function is called.
 *
 *
 * Deprecated: All application registry functions have been
 * deprecated. Use the functions available in gnome-mime-handlers
 * instead.
 */

void
gnome_vfs_application_registry_save_mime_application (const GnomeVFSMimeApplication *application)
{
	Application *i_application;

	g_return_if_fail (application != NULL);

	/* make us a new user application */
	i_application = application_lookup_or_create (application->id, TRUE);

	application_ref (i_application);

	set_value (i_application, GNOME_VFS_APPLICATION_REGISTRY_NAME,
		   application->name);
	set_value (i_application, GNOME_VFS_APPLICATION_REGISTRY_COMMAND,
		   application->command);
	set_bool_value (i_application, GNOME_VFS_APPLICATION_REGISTRY_CAN_OPEN_MULTIPLE_FILES,
			application->can_open_multiple_files);
	i_application->expects_uris = application->expects_uris;
	set_bool_value (i_application, GNOME_VFS_APPLICATION_REGISTRY_REQUIRES_TERMINAL,
			application->requires_terminal);
	/* FIXME: Need to save supported_uri_schemes information */
	user_file_dirty = TRUE;
}

/**
 * gnome_vfs_application_is_user_owned_application
 * @application:  data structure of the mime application
 *
 * This function will determine if a mime application is user owned or not.  By
 * user ownered this means that the application is not a system application
 * located in the prerequisite /usr area but rather in the user's area.
 *
 * Returns: gboolean
 *
 * Deprecated: All application registry functions have been
 * deprecated. Use the functions available in gnome-mime-handlers
 * instead.
 */

gboolean
gnome_vfs_application_is_user_owned_application (const GnomeVFSMimeApplication *application)
{
	Application *i_application;

	g_return_val_if_fail (application != NULL, FALSE);

	/* make us a new user application */
	i_application = g_hash_table_lookup (global_applications, application->id);
	if (i_application != NULL) {
		return i_application->user_owned;
	}
	
	return FALSE;
}