summaryrefslogtreecommitdiff
path: root/capplets/file-types/file-types-capplet.c
blob: 49c6828f6d0c0f5d09cbd0794ce0ea56bf824587 (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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

/* nautilus-mime-type-capplet.h
 *
 * Copyright (C) 1998 Redhat Software Inc. 
 * Copyright (C) 2000  Free Software Foundaton
 * Copyright (C) 2000  Eazel, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Authors: 	Jonathan Blandford <jrb@redhat.com>
 * 		Gene Z. Ragan <gzr@eazel.com>
 */

#include <config.h>
#include <ctype.h>
#include <dirent.h>
#include <math.h>
#include <sys/types.h>
#include <regex.h>
#include <string.h>

#include <capplet-widget.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gdk/gdkprivate.h>
#include <gnome.h>
#include <gtk/gtk.h>
#include <libgnomevfs/gnome-vfs-mime-handlers.h>
#include <libgnomevfs/gnome-vfs-mime-info.h>
#include <libgnomevfs/gnome-vfs-init.h>
#include <libgnomevfs/gnome-vfs-utils.h>

#include "file-types-capplet-dialogs.h"
#include "file-types-icon-entry.h"

#include "file-types-capplet.h"

#define DEFAULT_REGULAR_ICON "nautilus/i-regular-24.png"
#define DEFAULT_ACTION_ICON "nautilus/i-executable.png"

#define MAX_ICON_WIDTH_IN_LIST	18
#define MAX_ICON_HEIGHT_IN_LIST	18

enum {
	COLUMN_DESCRIPTION = 0,
	COLUMN_MIME_TYPE,
	COLUMN_EXTENSION,
	COLUMN_ACTION,
	TOTAL_COLUMNS
};

/* Local Prototypes */
static void	 init_mime_capplet 	  		(const char 	*scroll_to_mime_type);
static void 	 populate_application_menu 		(GtkWidget 	*menu, 	 
						 	 const char 	*mime_string);
static void 	 populate_viewer_menu			(GtkWidget 	*menu, 	 
						 	 const char 	*mime_string);
static void	 revert_mime_clicked       		(GtkWidget 	*widget, 
						 	 gpointer   	data);
static void	 delete_mime_clicked       		(GtkWidget 	*widget, 
						 	 gpointer   	data);
static void	 add_mime_clicked 	  		(GtkWidget 	*widget, 
						 	 gpointer   	data);
static void	 edit_default_clicked 			(GtkWidget 	*widget, 	
						 	 gpointer   	data);
static GtkWidget *create_mime_list_and_scroller 	(void);
static void 	 ok_callback 		  		(void);
static void	 gtk_widget_make_bold 			(GtkWidget 	*widget);
static GdkFont 	 *gdk_font_get_bold 			(const GdkFont  *plain_font);
static void	 gtk_widget_set_font 			(GtkWidget 	*widget, 
						 	 GdkFont 	*font);
static void	 gtk_style_set_font 			(GtkStyle 	*style, 
						 	 GdkFont 	*font);
static GdkPixbuf *capplet_gdk_pixbuf_scale_to_fit 	(GdkPixbuf 	*pixbuf, 
							 int 		max_width, 
							 int 		max_height);
static void	 update_mime_list_action 		(const char 	*mime_string);
static void      populate_mime_list                     (GList          *type_list, 
							 GtkCList       *clist);
static GdkPixbuf *capplet_get_icon_pixbuf               (const char     *mime_string, 
							 gboolean        is_executable);


/* FIXME: Using global variables here is yucky */
GtkWidget *capplet;
GtkWidget *delete_button;
GtkWidget *remove_button;
GtkWidget *add_button;
GtkWidget *icon_entry, *extension_list, *mime_list;
GtkWidget *default_menu;
GtkWidget *application_button, *viewer_button;
GtkLabel  *mime_label;
GtkWidget *description_entry;
gboolean   description_has_changed;
gboolean sort_column_clicked [TOTAL_COLUMNS];

/*
 *  main
 *
 *  Display capplet
 */

#define MATHIEU_DEBUG

#ifdef MATHIEU_DEBUG
#include <signal.h>

static void
nautilus_stop_in_debugger (void)
{
	void (* saved_handler) (int);

	saved_handler = signal (SIGINT, SIG_IGN);
	raise (SIGINT);
	signal (SIGINT, saved_handler);
}

static void
nautilus_stop_after_default_log_handler (const char *domain,
					 GLogLevelFlags level,
					 const char *message,
					 gpointer data)
{
	g_log_default_handler (domain, level, message, data);
	nautilus_stop_in_debugger ();
}

static void
nautilus_set_stop_after_default_log_handler (const char *domain)
{
	g_log_set_handler (domain, G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING,
			   nautilus_stop_after_default_log_handler, NULL);
}

static void
nautilus_make_warnings_and_criticals_stop_in_debugger (const char *first_domain, ...)
{
	va_list domains;
	const char *domain;

	nautilus_set_stop_after_default_log_handler (first_domain);

	va_start (domains, first_domain);

	for (;;) {
		domain = va_arg (domains, const char *);
		if (domain == NULL) {
			break;
		}
		nautilus_set_stop_after_default_log_handler (domain);
	}

	va_end (domains);
}
#endif /* MATHIEU_DEBUG */

int
main (int argc, char **argv)
{
        int init_results;
	char *mime_type;
	
	mime_type = NULL;
	
	/* */
	if (argc >= 1) {
		mime_type = g_strdup (argv[1]);
	}
		
        bindtextdomain (PACKAGE, GNOMELOCALEDIR);
        textdomain (PACKAGE);
	
	
        init_results = gnome_capplet_init ("file-types-capplet", VERSION, argc, argv, NULL, 0, NULL);

	if (init_results < 0) {
                exit (0);
	}

	gnome_vfs_init ();

#ifdef MATHIEU_DEBUG
	nautilus_make_warnings_and_criticals_stop_in_debugger (G_LOG_DOMAIN, g_log_domain_glib,
							       "Bonobo",
							       "Gdk",
							       "GnomeUI",
							       "GnomeVFS",
							       "GnomeVFS-CORBA",
							       "GnomeVFS-pthread",
							       "Gtk",
							       "Nautilus",
							       "Nautilus-Authenticate",
							       "Nautilus-Tree",
							       "ORBit",
							       NULL);
							       
#endif /* MATHIEU_DEBUG */
	if (init_results == 0) {
		init_mime_capplet (mime_type);
	        capplet_gtk_main ();
	}
        return 0;
}

static void
ok_callback ()
{
}

static void
populate_extension_list (const char *mime_type, GtkCList *list)
{
	GList *extensions, *element;
	gchar *extension[1];
	gint row;
		
	if (mime_type == NULL || list == NULL) {
		return;
	}
		
	/* Clear out old items */
	gtk_clist_clear (list);

	extensions = gnome_vfs_mime_get_extensions_list (mime_type);
	if (extensions == NULL) {
		return;
	}

	for (element = extensions; element != NULL; element = element->next) {
		extension[0] = (char *)element->data;
		if (strlen (element->data) > 0) {
			row = gtk_clist_append (list, extension);
			
			/* Set to deletable */
			gtk_clist_set_row_data (list, row, GINT_TO_POINTER (FALSE));
		}
	}

	gnome_vfs_mime_extensions_list_free (extensions);	
	
	/* Select first item in extension list */
	gtk_clist_select_row (list, 0, 0);
}

void
nautilus_mime_type_capplet_add_extension (const char *extension)
{
	gchar *title[1];
	gchar *token, *search_string;
	gint rownumber;
	const char *mime_type;

	/* Check for empty string */
	if (strlen (extension) <= 0) {
		return;
	}

	/* Copy only contiguous part of string.  No spaces allowed. */	
	search_string = g_strdup (extension);
	token = strtok (search_string, " ");

	if (token == NULL) {		
		title[0] = g_strdup (extension);
	} else if (strlen (token) <= 0) {
		return;
	} else {
		title[0] = g_strdup (token);		
	}
	g_free (search_string);
	
	rownumber = gtk_clist_append (GTK_CLIST (extension_list), title);
	gtk_clist_set_row_data (GTK_CLIST (extension_list), rownumber,
				GINT_TO_POINTER (TRUE));

	mime_type = nautilus_mime_type_capplet_get_selected_item_mime_type ();
	g_assert (mime_type != NULL);
	gnome_vfs_mime_add_extension (mime_type, extension);

	/* Select new item in list */
	gtk_clist_select_row (GTK_CLIST (extension_list), rownumber, 0);

}


static void
mime_list_selected_row_callback (GtkWidget *widget, gint row, gint column, GdkEvent *event, gpointer data)
{
        const char *mime_type;

        mime_type = (const char *) gtk_clist_get_row_data (GTK_CLIST (widget),row);

	/* Update info on selection */
        nautilus_mime_type_capplet_update_info (mime_type);
}

static void
application_button_toggled (GtkToggleButton *button, gpointer user_data)
{
	const char *mime_type;
	
	if (gtk_toggle_button_get_active (button)) {

		mime_type = nautilus_mime_type_capplet_get_selected_item_mime_type ();
		
		gnome_vfs_mime_set_default_action_type (mime_type, GNOME_VFS_MIME_ACTION_TYPE_APPLICATION);

		/* Populate menu with application items */
		populate_application_menu (default_menu, mime_type);

		/* Update mime list */
		update_mime_list_action (mime_type);
	}
}

static void
viewer_button_toggled (GtkToggleButton *button, gpointer user_data)
{
	const char *mime_type;
	
	if (gtk_toggle_button_get_active (button)) {
		mime_type = nautilus_mime_type_capplet_get_selected_item_mime_type ();

		gnome_vfs_mime_set_default_action_type (mime_type, GNOME_VFS_MIME_ACTION_TYPE_COMPONENT);

		/* Populate menu with viewer items */
		populate_viewer_menu (default_menu, mime_type);

		/* Update mime list */
		update_mime_list_action (mime_type);
	}
}

static int
get_selected_row_number (void)
{
	gint row;

	if (GTK_CLIST (mime_list)->selection == NULL) {
		return -1;
	}

	row = GPOINTER_TO_INT ((GTK_CLIST (mime_list)->selection)->data);
	return row;
}
static const char *
get_selected_mime_type (void)
{
        gint row = 0;
        const char *mime_type;


	if (GTK_CLIST (mime_list)->selection == NULL) {
		return NULL;
	}

	row = get_selected_row_number ();
	if (row == -1) {
		return NULL;
	}

	mime_type = (const char *) gtk_clist_get_row_data (GTK_CLIST (mime_list), row);
	
	return mime_type;
}

static void
really_change_icon (gpointer user_data)
{
	NautilusMimeIconEntry *icon_entry;
	char *filename;
	const char *mime_type;

	g_assert (NAUTILUS_MIME_IS_ICON_ENTRY (user_data));

	mime_type = get_selected_mime_type ();
	if (mime_type == NULL) {
		return;
	}

	icon_entry = NAUTILUS_MIME_ICON_ENTRY (user_data);

	filename = nautilus_mime_type_icon_entry_get_relative_filename (icon_entry);
	if (filename == NULL) {
		filename = nautilus_mime_type_icon_entry_get_full_filename (icon_entry);
	}

	gnome_vfs_mime_set_icon (mime_type, filename);

	nautilus_mime_type_capplet_update_mime_list_icon_and_description (mime_type);
	nautilus_mime_type_capplet_update_info (mime_type);

	g_free (filename);
}

static void
icon_chosen_callback (GnomeIconList *gil, gint num, GdkEvent *event, gpointer user_data)
{
	NautilusMimeIconEntry *icon_entry;
	const gchar * icon;
	GnomeIconSelection * gis;
	GtkWidget *gtk_entry;

	g_return_if_fail (user_data != NULL);
	g_return_if_fail (NAUTILUS_MIME_IS_ICON_ENTRY (user_data));

	icon_entry = NAUTILUS_MIME_ICON_ENTRY (user_data);

	gis =  gtk_object_get_user_data (GTK_OBJECT(icon_entry));
	icon = gnome_icon_selection_get_icon(gis, TRUE);

	if (icon != NULL) {
		gtk_entry = nautilus_mime_type_icon_entry_gtk_entry(icon_entry);
		gtk_entry_set_text(GTK_ENTRY(gtk_entry),icon);
		
	}

	if(event && event->type == GDK_2BUTTON_PRESS && ((GdkEventButton *)event)->button == 1) {
		gnome_icon_selection_stop_loading(gis);
		really_change_icon (user_data);
		gtk_widget_hide(icon_entry->pick_dialog);
	}


}

static void
change_icon_clicked_cb_real (GnomeDialog *dialog, gint button_number, gpointer user_data)
{
	if (button_number == GNOME_OK) {
		really_change_icon (user_data);
	}
}

static void 
change_icon_clicked (GtkWidget *entry, gpointer user_data)
{
	GnomeDialog *dialog;
	GnomeIconSelection * gis;

	nautilus_mime_type_show_icon_selection (NAUTILUS_MIME_ICON_ENTRY (user_data));

	dialog = GNOME_DIALOG (NAUTILUS_MIME_ICON_ENTRY (user_data)->pick_dialog);

	gtk_signal_connect (GTK_OBJECT (dialog), "clicked", change_icon_clicked_cb_real, user_data);

	gis = gtk_object_get_user_data(GTK_OBJECT(user_data));
	gtk_signal_connect_after (GTK_OBJECT(GNOME_ICON_SELECTION(gis)->gil), 
				  "select_icon", icon_chosen_callback, user_data);

}

static void
update_extensions_list (const char *mime_type) 
{
	int row;
	char *pretty_string;

	row = get_selected_row_number ();

	pretty_string = gnome_vfs_mime_get_extensions_pretty_string (mime_type);
	if (pretty_string == NULL) {
		pretty_string = g_strdup (" ");
	}
	gtk_clist_set_text (GTK_CLIST (mime_list),
			    row, 2, pretty_string);

	g_free (pretty_string);

}

static void 
change_file_extensions_clicked (GtkWidget *widget, gpointer user_data)
{
	const char *mime_type;
	char *new_extensions;
	gboolean use_new_list;

	mime_type = get_selected_mime_type ();
	if (mime_type == NULL) {
		return;
	}

	new_extensions = nautilus_mime_type_capplet_show_change_extension_window (mime_type, &use_new_list);
	if (use_new_list) {
		gnome_vfs_mime_set_extensions_list (mime_type, new_extensions);
	}

	update_extensions_list (mime_type);
}

/* The following are copied from gtkclist.c and nautilusclist.c */ 
#define CELL_SPACING 1

/* gives the top pixel of the given row in context of
 * the clist's voffset */
#define ROW_TOP_YPIXEL(clist, row) (((clist)->row_height * (row)) + \
				    (((row) + 1) * CELL_SPACING) + \
				    (clist)->voffset)
static void
list_move_vertical (GtkCList *clist, gint row, gfloat align)
{
	gfloat value;

	g_return_if_fail (clist != NULL);

	if (!clist->vadjustment) {
		return;
	}

	value = (ROW_TOP_YPIXEL (clist, row) - clist->voffset -
		 align * (clist->clist_window_height - clist->row_height) +
		 (2 * align - 1) * CELL_SPACING);

	if (value + clist->vadjustment->page_size > clist->vadjustment->upper) {
		value = clist->vadjustment->upper - clist->vadjustment->page_size;
	}

	gtk_adjustment_set_value (clist->vadjustment, value);
}

static void
list_moveto (GtkCList *clist, gint row, gint column, gfloat row_align, gfloat col_align)
{
	g_return_if_fail (clist != NULL);

	if (row < -1 || row >= clist->rows) {
		return;
	}
	
	if (column < -1 || column >= clist->columns) {
		return;
	}

	row_align = CLAMP (row_align, 0, 1);
	col_align = CLAMP (col_align, 0, 1);

	/* adjust vertical scrollbar */
	if (clist->vadjustment && row >= 0) {
		list_move_vertical (clist, row, row_align);
	}
}

static void
list_reveal_row (GtkCList *clist, int row_index)
{
	g_return_if_fail (row_index >= 0 && row_index < clist->rows);
		
	if (ROW_TOP_YPIXEL (clist, row_index) + clist->row_height > clist->clist_window_height) {
		list_moveto (clist, row_index, -1, 1, 0);
     	} else if (ROW_TOP_YPIXEL (clist, row_index) < 0) {
		list_moveto (clist, row_index, -1, 0, 0);
     	}
}



static int
find_row_for_mime_type (const char *mime_type, GtkCList *mime_list)
{
	gboolean found_one;
	int index;
	const char *row_data;
	
	if (mime_type == NULL) {
		return -1;
	}
	
	found_one = FALSE;
	
	for (index = 0; index < mime_list->rows; index++) {
		row_data = gtk_clist_get_row_data (mime_list, index);
		if (row_data != NULL && strcmp (row_data, mime_type) == 0) {
			found_one = TRUE;
			break;	
		}		
	}
				
	if (found_one) {
		return index;
	}
	
	return -1;
}


static void
update_description_from_input (GtkEntry *entry)
{
	char *new_description;
	const char *mime_type;

	g_assert (GTK_IS_ENTRY (entry));
	g_assert ((gpointer)entry == (gpointer)description_entry);

	description_has_changed = FALSE;

	mime_type = get_selected_mime_type ();
	if (mime_type == NULL) {
		return;
	}

	new_description = gtk_editable_get_chars (GTK_EDITABLE (entry), 0, -1);
	gnome_vfs_mime_set_description (mime_type, new_description);
	nautilus_mime_type_capplet_update_mime_list_icon_and_description (mime_type);
	g_free (new_description);
}

static void
description_entry_activate (GtkEntry *entry, gpointer user_data)
{
	g_assert (GTK_IS_ENTRY (entry));
	g_assert ((gpointer)entry == (gpointer)description_entry);
	g_assert (user_data == NULL);
	
	if (description_has_changed) {
		update_description_from_input (entry);
	}
}

static void
description_entry_changed (GtkEntry *entry, gpointer user_data)
{
	g_assert (GTK_IS_ENTRY (entry));
	g_assert ((gpointer)entry == (gpointer)description_entry);
	g_assert (user_data == NULL);
	
	description_has_changed = TRUE;
}

static gboolean
description_entry_lost_focus (GtkEntry *entry,
			      GdkEventFocus *event,
			      gpointer user_data)
{
	g_assert (GTK_IS_ENTRY (entry));
	g_assert ((gpointer)entry == (gpointer)description_entry);
	g_assert (user_data == NULL);
	
	if (description_has_changed) {
		update_description_from_input (entry);
	}

	return FALSE;
}

static void
init_mime_capplet (const char *scroll_to_mime_type)
{
	GtkWidget *main_vbox;
        GtkWidget *vbox, *hbox, *left_vbox;
        GtkWidget *button;
        GtkWidget *mime_list_container;
        GtkWidget *frame;
        GtkWidget *table;
	int index, list_width, column_width, found_index;

	capplet = capplet_widget_new ();

	/* Main vertical box */                    
	main_vbox = gtk_vbox_new (FALSE, GNOME_PAD);
	gtk_container_set_border_width (GTK_CONTAINER (main_vbox), GNOME_PAD_SMALL);
        gtk_container_add (GTK_CONTAINER (capplet), main_vbox);

        /* Main horizontal box and mime list */                    
        hbox = gtk_hbox_new (FALSE, GNOME_PAD);
        gtk_box_pack_start (GTK_BOX (main_vbox), hbox, TRUE, TRUE, 0);
        mime_list_container = create_mime_list_and_scroller ();
        gtk_box_pack_start (GTK_BOX (hbox), mime_list_container, TRUE, TRUE, 0);
	
	vbox = gtk_vbox_new (FALSE, GNOME_PAD_SMALL);
        gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, FALSE, 0);

	/* Create table */
	table = gtk_table_new (2, 2, FALSE);
	gtk_box_pack_start (GTK_BOX (main_vbox), table, FALSE, FALSE, 0);
	gtk_table_set_row_spacings (GTK_TABLE (table), GNOME_PAD_SMALL);
	gtk_table_set_col_spacings (GTK_TABLE (table), GNOME_PAD_SMALL);


	left_vbox = gtk_vbox_new (FALSE, 0);
	/* Set up top left area. */
	{
		GtkWidget *small_table;

		small_table =  gtk_table_new (1, 2, FALSE);
		gtk_table_set_col_spacings (GTK_TABLE (small_table), 7);

		gtk_table_attach ( GTK_TABLE (table), small_table, 0, 1, 0, 1,
				   (GtkAttachOptions) (GTK_FILL),
				   (GtkAttachOptions) (0), 0, 0);
				
		icon_entry = nautilus_mime_type_icon_entry_new ("mime_icon_entry", NULL);
		gtk_table_attach (GTK_TABLE (small_table), icon_entry, 0, 1, 0, 1,
				  (GtkAttachOptions) (GTK_FILL),
				  (GtkAttachOptions) (0), 0, 0);
				  
		vbox = gtk_vbox_new (FALSE, GNOME_PAD_SMALL);
		gtk_table_attach (GTK_TABLE (small_table), vbox, 1, 2, 0, 1,
				  (GtkAttachOptions) (GTK_FILL),
				  (GtkAttachOptions) (GTK_FILL), 0, 0);
	
		description_entry = gtk_entry_new ();
		description_has_changed = FALSE;
		gtk_box_pack_start (GTK_BOX (vbox), description_entry, FALSE, FALSE, 0);
		gtk_widget_make_bold (GTK_WIDGET (description_entry));
		
		gtk_signal_connect (GTK_OBJECT (description_entry), "activate",
	      	              	    GTK_SIGNAL_FUNC (description_entry_activate),
	                            NULL);
		
		gtk_signal_connect (GTK_OBJECT (description_entry), "changed",
	      	              	    GTK_SIGNAL_FUNC (description_entry_changed),
	                            NULL);
		
		gtk_signal_connect (GTK_OBJECT (description_entry), "focus_out_event",
	      	              	    GTK_SIGNAL_FUNC (description_entry_lost_focus),
	                            NULL);
		
		hbox = gtk_hbox_new (FALSE, 0);
		gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (hbox), FALSE, FALSE, 0);

		mime_label = GTK_LABEL (gtk_label_new (_("MIME Type")));	
		gtk_label_set_justify (GTK_LABEL (mime_label), GTK_JUSTIFY_LEFT);
		gtk_box_pack_start (GTK_BOX (hbox), GTK_WIDGET (mime_label), FALSE, FALSE, 0);

		hbox = gtk_hbox_new (FALSE, GNOME_PAD_SMALL);
		gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);

		button = gtk_button_new_with_label (_("Change Icon"));
		gtk_signal_connect (GTK_OBJECT (button), "clicked", change_icon_clicked, icon_entry);
		gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);

		/* spacer */
		gtk_box_pack_start (GTK_BOX (hbox), gtk_vbox_new (FALSE, 0), FALSE, FALSE, 10);

		button = gtk_button_new_with_label (_("Change File Extensions"));
		gtk_signal_connect (GTK_OBJECT (button), "clicked", change_file_extensions_clicked, NULL);
		gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);

	}

	/* Set up bottom left area. */
	{
		frame = gtk_frame_new (_("Default Action:"));
		gtk_table_attach ( GTK_TABLE (table), frame, 0, 1, 1, 2,
				   (GtkAttachOptions) (GTK_FILL),
				   (GtkAttachOptions) (0), 0, 0);
		
		vbox = gtk_vbox_new (FALSE, GNOME_PAD_SMALL);
		gtk_container_add (GTK_CONTAINER (frame), vbox);
		gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);

		hbox = gtk_hbox_new (FALSE, GNOME_PAD_SMALL);
		gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);

		viewer_button = gtk_radio_button_new_with_label (NULL, _("Use Viewer"));
		gtk_box_pack_start (GTK_BOX (hbox), viewer_button, FALSE, FALSE, 0);
		gtk_signal_connect (GTK_OBJECT (viewer_button), "toggled",
				    GTK_SIGNAL_FUNC (viewer_button_toggled), NULL);

		application_button = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (viewer_button), 
										  _("Open With Application"));
		gtk_box_pack_start (GTK_BOX (hbox), application_button, FALSE, FALSE, 0);
		gtk_signal_connect (GTK_OBJECT (application_button), "toggled",
				    GTK_SIGNAL_FUNC (application_button_toggled), NULL);

		hbox = gtk_hbox_new (FALSE, GNOME_PAD_SMALL);
		gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);

		default_menu = gtk_option_menu_new();
		gtk_box_pack_start (GTK_BOX (hbox), default_menu, TRUE, TRUE, 0);

		button = gtk_button_new_with_label (_("Edit List"));
		gtk_misc_set_padding (GTK_MISC (GTK_BIN(button)->child), 2, 1);
		gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
		gtk_signal_connect (GTK_OBJECT (button), "clicked", edit_default_clicked, mime_list);
	}

	/* Set up top right area. */
	{
		GtkWidget *separator;
		GtkWidget *small_table;
		
		hbox = gtk_hbox_new (FALSE, 0);
		gtk_table_attach_defaults ( GTK_TABLE (table), hbox, 1, 2, 0, 1);

		small_table =  gtk_table_new (5, 1, FALSE);
		gtk_table_set_row_spacings (GTK_TABLE (small_table), 7);
		gtk_box_pack_end (GTK_BOX (hbox), small_table, FALSE, FALSE, 0);
		
		/* Placed to space top button with top of left table */
		hbox = gtk_hbox_new (FALSE, 0);
		gtk_table_attach (GTK_TABLE (small_table), hbox, 0, 1, 0, 1,
				  (GtkAttachOptions) (GTK_FILL),
				  (GtkAttachOptions) (0), 0, 0);
		gtk_widget_set_usize (hbox, 1, 11);
				
		button = gtk_button_new_with_label (_("Add New MIME Type..."));
		gtk_signal_connect (GTK_OBJECT (button), "clicked", add_mime_clicked, NULL);
		gtk_table_attach (GTK_TABLE (small_table), button, 0, 1, 1, 2,
				  (GtkAttachOptions) (GTK_FILL),
				  (GtkAttachOptions) (0), 0, 0);

		button = gtk_button_new_with_label (_("Delete This MIME Type"));
		gtk_signal_connect (GTK_OBJECT (button), "clicked", delete_mime_clicked, NULL);
		gtk_table_attach (GTK_TABLE (small_table), button, 0, 1, 2, 3,
				  (GtkAttachOptions) (GTK_FILL),
				  (GtkAttachOptions) (0), 0, 0);

		separator = gtk_hseparator_new ();
		gtk_table_attach (GTK_TABLE (small_table), separator, 0, 1, 3, 4,
				  (GtkAttachOptions) (GTK_FILL),
				  (GtkAttachOptions) (GTK_FILL), 0, 0);

		button = gtk_button_new_with_label (_("Revert to System Defaults"));
		gtk_signal_connect (GTK_OBJECT (button), "clicked", revert_mime_clicked, NULL);
		gtk_table_attach (GTK_TABLE (small_table), button, 0, 1, 4, 5,
				  (GtkAttachOptions) (GTK_FILL),
				  (GtkAttachOptions) (0), 0, 0);

	}

	/* Yes, show all widgets */
	gtk_widget_show_all (capplet);

	/* Make columns all fit within capplet list view bounds */
	list_width = GTK_WIDGET (mime_list)->allocation.width;
	column_width = list_width / TOTAL_COLUMNS;
	for (index = 0; index < TOTAL_COLUMNS; index++) 
		gtk_clist_set_column_auto_resize (GTK_CLIST (mime_list), index, TRUE);
#if 0
	/* This is not working */
	for (index = 0; index < TOTAL_COLUMNS; index++) {
		g_print ("Setting column %i with to %d\n", index, column_width);
		gtk_clist_set_column_width (GTK_CLIST (mime_list), index, column_width);
	}
#endif

        /* Setup capplet signals */
        gtk_signal_connect(GTK_OBJECT(capplet), "ok",
                           GTK_SIGNAL_FUNC(ok_callback), NULL);

	gtk_signal_connect (GTK_OBJECT (mime_list),"select_row",
       	                   GTK_SIGNAL_FUNC (mime_list_selected_row_callback), NULL);

	/* Sort by description. The description is the first column in the list. */
	gtk_clist_set_sort_column (GTK_CLIST (mime_list), COLUMN_DESCRIPTION);
	gtk_clist_sort (GTK_CLIST (mime_list));
	GTK_CLIST (mime_list)->sort_type = GTK_SORT_ASCENDING;

	/* Set up initial column click tracking state. We do this so the initial clicks on
	 * columns will allow us to set the proper sort state for the user.
	 */
	sort_column_clicked[0] = TRUE; /* First sort column has been click by us in setup code */
	for (index = 1; index < TOTAL_COLUMNS; index++) {
		sort_column_clicked[index] = FALSE;
	}
	
	/* Attempt to select specified mime type in list */
	if (scroll_to_mime_type != NULL) {		
		found_index = find_row_for_mime_type (scroll_to_mime_type, GTK_CLIST (mime_list));
		if (found_index != -1) {
			gtk_clist_select_row (GTK_CLIST (mime_list), found_index, 1);
			list_reveal_row (GTK_CLIST (mime_list), found_index);
		} else {
			gtk_clist_select_row (GTK_CLIST (mime_list), 0, 1);
			list_reveal_row (GTK_CLIST (mime_list), 0);
		}			
	} else {
		gtk_clist_select_row (GTK_CLIST (mime_list), 0, 0);
		list_reveal_row (GTK_CLIST (mime_list), 0);
	}
		
	/* Inform control center that our changes are immediate */
	capplet_widget_changes_are_immediate (CAPPLET_WIDGET (capplet));
}

static gboolean
is_full_path (const char *path_or_name)
{
	return path_or_name[0] == '/';
}

static char *
capplet_get_icon_path (const char *path_or_name)
{
	char *result;
	char *alternate_relative_filename;

	if (is_full_path (path_or_name) && g_file_exists (path_or_name)) {
		return g_strdup (path_or_name);
	}

	result = gnome_vfs_icon_path_from_filename (path_or_name);
	if (result != NULL) {
		return result;
	}

	/* FIXME bugzilla.eazel.com 639:
	 * It is somewhat evil to special-case the nautilus directory here.
	 * We should clean this up if/when we come up with a way to handle
	 * Nautilus themes here.
	 */
	alternate_relative_filename = g_strconcat ("nautilus/", path_or_name, NULL);
	result = gnome_vfs_icon_path_from_filename (alternate_relative_filename);
	g_free (alternate_relative_filename);
	if (result != NULL) {
		return result;
	}

	/* FIXME bugzilla.eazel.com 639:
	 * To work correctly with Nautilus themed icons, if there's no
	 * suffix we will also try looking in the nautilus dir for a ".png" name.
	 * This will return the icon for the default theme; there is no
	 * mechanism for getting a themed icon in the capplet.
	 */
	alternate_relative_filename = g_strconcat ("nautilus/", path_or_name, ".png", NULL);
	result = gnome_vfs_icon_path_from_filename (alternate_relative_filename);
	g_free (alternate_relative_filename);

	return result;
}

/*
 *  nautilus_mime_type_capplet_update_info
 *
 *  Update controls with info based on mime type	
 */
 
void
nautilus_mime_type_capplet_update_info (const char *mime_type) {

	GnomeVFSMimeAction *action;
	const char *icon_name, *description;
	char *path;
	
	/* Update text items */
	gtk_label_set_text (GTK_LABEL (mime_label), mime_type);

	description = gnome_vfs_mime_get_description (mime_type);	
	gtk_entry_set_text (GTK_ENTRY (description_entry), description != NULL ? description : "");
	description_has_changed = FALSE;

	/* Update menus */
	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (application_button))) {
		populate_application_menu (default_menu, mime_type);						
	} else {
		populate_viewer_menu (default_menu, mime_type);						
	}

	/* Update extensions list */
	if (extension_list != NULL) {
		populate_extension_list (mime_type, GTK_CLIST (extension_list));
	}

	/* Set icon for mime type */
	icon_name = gnome_vfs_mime_get_icon (mime_type);
	path = NULL;
	if (icon_name != NULL) {
		path = capplet_get_icon_path (icon_name);
	}
	if (path == NULL) {
		/* No custom icon specified, or custom icon not found, use default */
		path = capplet_get_icon_path (DEFAULT_REGULAR_ICON);
	}
	nautilus_mime_type_icon_entry_set_icon (NAUTILUS_MIME_ICON_ENTRY (icon_entry), path);
	g_free (path);

	/* Indicate default action */	
	action = gnome_vfs_mime_get_default_action (mime_type);
	if (action != NULL) {
		switch (action->action_type) {
			case GNOME_VFS_MIME_ACTION_TYPE_APPLICATION:
				gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (application_button), TRUE);
				break;

			case GNOME_VFS_MIME_ACTION_TYPE_COMPONENT:
				gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (viewer_button), TRUE);
				break;
				
			default:
				gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (application_button), TRUE);
				break;
		}
	} else {
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (application_button), TRUE);
	}

}

static void 
application_menu_activated (GtkWidget *menu_item, gpointer data)
{
	const char *id;
	const char *mime_type;

	/* Get our stashed data */
	id = gtk_object_get_data (GTK_OBJECT (menu_item), "id");
	mime_type = gtk_object_get_data (GTK_OBJECT (menu_item), "mime_type");

	if (id == NULL || mime_type == NULL) {
		return;
	}	
	gnome_vfs_mime_set_default_application (mime_type, id);
	update_mime_list_action (mime_type);
}

static void
populate_application_menu (GtkWidget *application_menu, const char *mime_type)
{
	GtkWidget *new_menu, *menu_item;
	GList *app_list, *copy_list;
	GnomeVFSMimeApplication *default_app, *application;
	gboolean has_none, found_match;
	char *mime_copy;
	const char *id;
	GList *children;
	int index;

	has_none = TRUE;
	found_match = FALSE;

	mime_copy = g_strdup (mime_type);
	
	new_menu = gtk_menu_new ();
	
	/* Get the default application */
	default_app = gnome_vfs_mime_get_default_application (mime_type);

	/* Get the application short list */
	app_list = gnome_vfs_mime_get_short_list_applications (mime_type);
	if (app_list != NULL) {
		for (copy_list = app_list; copy_list != NULL; copy_list = copy_list->next) {
			has_none = FALSE;

			application = copy_list->data;
			menu_item = gtk_menu_item_new_with_label (application->name);

			/* Store copy of application name and mime type in item; free when item destroyed. */
			gtk_object_set_data_full (GTK_OBJECT (menu_item),
						  "id",
						  g_strdup (application->id),
						  g_free);

			gtk_object_set_data_full (GTK_OBJECT (menu_item),
						  "mime_type",
						  g_strdup (mime_type),
						  g_free);
			
			gtk_menu_append (GTK_MENU (new_menu), menu_item);
			gtk_widget_show (menu_item);

			gtk_signal_connect (GTK_OBJECT (menu_item), "activate", 
					    application_menu_activated, NULL);
		}
	
		gnome_vfs_mime_application_list_free (app_list);
	}

	/* Find all applications or add a "None" item */
	if (has_none && default_app == NULL) {		
		menu_item = gtk_menu_item_new_with_label (_("None"));
		gtk_menu_append (GTK_MENU (new_menu), menu_item);
		gtk_widget_show (menu_item);
	} else {
		/* Check and see if default is in the short list */
		if (default_app != NULL) {
			/* Iterate */
			children = gtk_container_children (GTK_CONTAINER (new_menu));
			for (index = 0; children != NULL; children = children->next, ++index) {				
				id = (const char *)(gtk_object_get_data (GTK_OBJECT (children->data), "id"));
				if (id != NULL) {											
					if (strcmp (default_app->id, id) == 0) {
						found_match = TRUE;
						break;
					}
				}
			}
			g_list_free (children);

			/* See if we have a match */
			if (found_match) {
				/* Have menu appear with default application selected */
				gtk_menu_set_active (GTK_MENU (new_menu), index);
			} else {
				/* No match found.  We need to insert a menu item
				 * and add the application to the default list */
				menu_item = gtk_menu_item_new_with_label (default_app->name);

				/* Store copy of application name and mime type in item; free when item destroyed. */
				gtk_object_set_data_full (GTK_OBJECT (menu_item),
							  "id",
							  g_strdup (default_app->id),
							  g_free);

				gtk_object_set_data_full (GTK_OBJECT (menu_item),
							  "mime_type",
							  g_strdup (mime_type),
							  g_free);
				
				gtk_menu_append (GTK_MENU (new_menu), menu_item);
				gtk_widget_show (menu_item);

				gtk_signal_connect (GTK_OBJECT (menu_item), "activate", 
						    application_menu_activated, NULL);
				
				
				/* Iterate */
				children = gtk_container_children (GTK_CONTAINER (new_menu));
				for (index = 0; children != NULL; children = children->next, ++index) {				
					id = (const char *)(gtk_object_get_data (GTK_OBJECT (children->data), "id"));
					if (id != NULL) {											
						if (strcmp (default_app->id, id) == 0) {
							found_match = TRUE;
							break;
						}
					}
				}
				g_list_free (children);

				/* Set it as active */
				gtk_menu_set_active (GTK_MENU (new_menu), index);
			}
			gnome_vfs_mime_application_free (default_app);
		}
	}
	
	gtk_option_menu_set_menu (GTK_OPTION_MENU (default_menu), new_menu);
}

static void 
component_menu_activated (GtkWidget *menu_item, gpointer data)
{
	const char *iid;
	const char *mime_type;

	/* Get our stashed data */
	iid = gtk_object_get_data (GTK_OBJECT (menu_item), "iid");
	mime_type = gtk_object_get_data (GTK_OBJECT (menu_item), "mime_type");

	if (iid == NULL || mime_type == NULL) {
		return;
	}

	gnome_vfs_mime_set_default_component (mime_type, iid);
	update_mime_list_action (mime_type);
}

static void
populate_viewer_menu (GtkWidget *component_menu, const char *mime_type)
{
	GtkWidget *new_menu, *menu_item;
	GList *component_list, *copy_list;
	OAF_ServerInfo *default_component;
	OAF_ServerInfo *info;
	gboolean has_none, found_match;
	char *mime_copy, *component_name;
	const char *iid;
	GList *children;
	int index;

	has_none = TRUE;
	found_match = FALSE;

	mime_copy = g_strdup (mime_type);
	
	new_menu = gtk_menu_new ();
	
	/* Get the default component */
	default_component = gnome_vfs_mime_get_default_component (mime_type);

	/* Get the component short list */
	component_list = gnome_vfs_mime_get_short_list_components (mime_type);
	if (component_list != NULL) {
		for (copy_list = component_list; copy_list != NULL; copy_list = copy_list->next) {
			has_none = FALSE;

			component_name = name_from_oaf_server_info (copy_list->data);
			menu_item = gtk_menu_item_new_with_label (component_name);
			g_free (component_name);

			/* Store copy of component name and mime type in item; free when item destroyed. */
			info = copy_list->data;
			gtk_object_set_data_full (GTK_OBJECT (menu_item),
						  "iid",
						  g_strdup (info->iid),
						  g_free);

			gtk_object_set_data_full (GTK_OBJECT (menu_item),
						  "mime_type",
						  g_strdup (mime_type),
						  g_free);
			
			gtk_menu_append (GTK_MENU (new_menu), menu_item);
			gtk_widget_show (menu_item);

			gtk_signal_connect (GTK_OBJECT (menu_item), "activate", 
					    component_menu_activated, NULL);
		}
	
		gnome_vfs_mime_component_list_free (component_list);
	}

	/* Find all components or add a "None" item */
	if (has_none && default_component == NULL) {		
		menu_item = gtk_menu_item_new_with_label (_("None"));
		gtk_menu_append (GTK_MENU (new_menu), menu_item);
		gtk_widget_show (menu_item);
	} else {
		/* Check and see if default is in the short list */
		if (default_component != NULL) {
			/* Iterate */
			children = gtk_container_children (GTK_CONTAINER (new_menu));
			for (index = 0; children != NULL; children = children->next, ++index) {				
				iid = (const char *)(gtk_object_get_data (GTK_OBJECT (children->data), "iid"));
				if (iid != NULL) {											
					if (strcmp (default_component->iid, iid) == 0) {
						found_match = TRUE;
						break;
					}
				}
			}
			g_list_free (children);

			/* See if we have a match */
			if (found_match) {
				/* Have menu appear with default component selected */
				gtk_menu_set_active (GTK_MENU (new_menu), index);
			} else {
				/* No match found.  We need to insert a menu item
				 * and add the component to the default list */
				component_name = name_from_oaf_server_info (default_component);
				menu_item = gtk_menu_item_new_with_label (component_name);
				g_free (component_name);

				/* Store copy of component name and mime type in item; free when item destroyed. */
				gtk_object_set_data_full (GTK_OBJECT (menu_item),
							  "iid",
							  g_strdup (default_component->iid),
							  g_free);

				gtk_object_set_data_full (GTK_OBJECT (menu_item),
							  "mime_type",
							  g_strdup (mime_type),
							  g_free);
				
				gtk_menu_append (GTK_MENU (new_menu), menu_item);
				gtk_widget_show (menu_item);

				gtk_signal_connect (GTK_OBJECT (menu_item), "activate", 
						    component_menu_activated, NULL);
				
				
				/* Iterate */
				children = gtk_container_children (GTK_CONTAINER (new_menu));
				for (index = 0; children != NULL; children = children->next, ++index) {				
					iid = (const char *)(gtk_object_get_data (GTK_OBJECT (children->data), "iid"));
					if (iid != NULL) {											
						if (strcmp (default_component->iid, iid) == 0) {
							found_match = TRUE;
							break;
						}
					}
				}
				g_list_free (children);

				/* Set it as active */
				gtk_menu_set_active (GTK_MENU (new_menu), index);
			}
			CORBA_free (default_component);
		}
	}
	
	gtk_option_menu_set_menu (GTK_OPTION_MENU (default_menu), new_menu);
}

static void
revert_real_cb (gint reply, gpointer data)
{
	if (reply == 0) {
		/* YES */
		GList *mime_types_list;
		gnome_vfs_mime_reset ();
		
		gnome_vfs_mime_info_reload ();

		mime_types_list = gnome_vfs_get_registered_mime_types ();
		
		gtk_clist_freeze (GTK_CLIST (mime_list));
		gtk_clist_clear (GTK_CLIST (mime_list));
		populate_mime_list (mime_types_list, GTK_CLIST (mime_list));
				
		/* Sort list using current sort type and select the first item. */
		gtk_clist_sort (GTK_CLIST (mime_list));
		gtk_clist_select_row (GTK_CLIST (mime_list), 0, 0);
		list_reveal_row (GTK_CLIST (mime_list), 0);
		
		gtk_clist_thaw (GTK_CLIST (mime_list));

	} else {
		/* NO */
	}

}

static void
revert_mime_clicked (GtkWidget *widget, gpointer data)
{
	GtkWidget *dialog;
	
	dialog = gnome_question_dialog_modal (_("Reverting to system settings will lose any changes\n"
						"you have ever made to File Types and Programs.\n"
						"Revert anyway?"), 
					      revert_real_cb, NULL);

}
/*
 *  delete_mime_clicked	
 *
 *  Delete mime type if it is a user defined type.
 */
 
static void
delete_mime_clicked (GtkWidget *widget, gpointer data)
{
        const char *mime_type;
        gint row = 0;

        if (GTK_CLIST (mime_list)->selection)
                row = GPOINTER_TO_INT ((GTK_CLIST (mime_list)->selection)->data);
        else
                return;
        mime_type = (const char *) gtk_clist_get_row_data (GTK_CLIST (mime_list), row);

        gtk_clist_remove (GTK_CLIST (mime_list), row);

	gnome_vfs_mime_registered_mime_type_delete (mime_type);
}

static void
add_mime_clicked (GtkWidget *widget, gpointer data)
{
	char *text[4], *tmp_text;
	const char *description;
	char *extensions, *mime_string, *filename;
        gint row;
	GdkPixbuf *pixbuf;
	GdkPixmap *pixmap;
	GdkBitmap *bitmap;
	GnomeVFSMimeAction *action;
	GnomeVFSMimeApplication *default_app;
	OAF_ServerInfo *default_component;
	int found_index;
	
	mime_string = nautilus_mime_type_capplet_show_new_mime_window ();
	if (mime_string != NULL && mime_string[0] != '\0') {
		/* Add new type to mime list */
		pixbuf = NULL;
			
		/* Add description to first column */
		description = gnome_vfs_mime_get_description (mime_string);	
		if (description != NULL && strlen (description) > 0) {
			text[0] = g_strdup (description);
		} else {
			text[0] = g_strdup ("");
		}

		/* Add mime type to second column */
		text[1] = g_strdup (mime_string);

		/* Add extension to third columns */
		extensions = gnome_vfs_mime_get_extensions_pretty_string (mime_string);
		if (extensions != NULL) {
			text[2] = g_strdup (extensions);
		} else {
			text[2] = g_strdup ("");
		}

		/* Add default action to fourth column */
		text[3] = g_strdup(_("none"));

		/* Insert item into list */
		row = gtk_clist_insert (GTK_CLIST (mime_list), 1, text);
	        gtk_clist_set_row_data (GTK_CLIST (mime_list), row, g_strdup (mime_string));

		/* Set description column icon */
		pixbuf = capplet_get_icon_pixbuf (mime_string, FALSE);

		if (pixbuf != NULL) {
			pixbuf = capplet_gdk_pixbuf_scale_to_fit (pixbuf, MAX_ICON_WIDTH_IN_LIST, MAX_ICON_HEIGHT_IN_LIST);
			gdk_pixbuf_render_pixmap_and_mask (pixbuf, &pixmap, &bitmap, 100);
			gtk_clist_set_pixtext (GTK_CLIST (mime_list), row, 0, text[0], 5, pixmap, bitmap);
			gdk_pixbuf_unref (pixbuf);
		}

		/* Set up action column */
		pixbuf = NULL;
		action = gnome_vfs_mime_get_default_action (mime_string);
		if (action != NULL) {
			switch (action->action_type) {
				case GNOME_VFS_MIME_ACTION_TYPE_APPLICATION:
					/* Get the default application */
					default_app = gnome_vfs_mime_get_default_application (mime_string);
					g_free (text[3]);
					text[3] = g_strdup (default_app->name);

					filename = capplet_get_icon_path (DEFAULT_ACTION_ICON);
					if (filename != NULL) {
						pixbuf = gdk_pixbuf_new_from_file (filename);
						g_free (filename);
					}

					gnome_vfs_mime_application_free (default_app);
					break;

				case GNOME_VFS_MIME_ACTION_TYPE_COMPONENT:
					/* Get the default component */
					default_component = gnome_vfs_mime_get_default_component (mime_string);
					g_free (text[3]);					
					tmp_text = name_from_oaf_server_info (default_component);
					text[3] = g_strdup_printf (_("View as %s"), tmp_text);
					g_free (tmp_text);
					filename = capplet_get_icon_path ("nautilus/gnome-library.png");
					if (filename != NULL) {
						pixbuf = gdk_pixbuf_new_from_file (filename);
						g_free (filename);
					}
					CORBA_free (default_component);
					break;
					
				default:
					gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (application_button), TRUE);
					break;
			}
		}
				
		/* Set column icon */
		if (pixbuf != NULL) {
			pixbuf = capplet_gdk_pixbuf_scale_to_fit (pixbuf, MAX_ICON_WIDTH_IN_LIST, MAX_ICON_HEIGHT_IN_LIST);
			gdk_pixbuf_render_pixmap_and_mask (pixbuf, &pixmap, &bitmap, 100);
			gtk_clist_set_pixtext (GTK_CLIST (mime_list), row, 3, text[3], 5, pixmap, bitmap);
			gdk_pixbuf_unref (pixbuf);
		}
		
		/* Sort, select and scroll to new mime type */
		gtk_clist_sort (GTK_CLIST (mime_list));
		found_index = find_row_for_mime_type (mime_string, GTK_CLIST (mime_list));
		if (found_index != -1) {
			gtk_clist_select_row (GTK_CLIST (mime_list), found_index, 1);
			list_reveal_row (GTK_CLIST (mime_list), found_index);
		}			

		g_free (text[0]);
		g_free (text[1]);
		g_free (text[2]);
		g_free (text[3]);
		g_free (extensions);
		g_free (mime_string);
	}

}

static void
edit_default_clicked (GtkWidget *widget, gpointer data)
{
	GtkWidget *list;
	const char *mime_type;
	GList *p;
	GtkCListRow *row;

	g_return_if_fail (GTK_IS_CLIST (data));

	list = data;
	row = NULL;

	/* Get first selected row.  This will be the only selection for this list */
	for (p = GTK_CLIST (list)->row_list; p != NULL; p = p->next) {
		row = p->data;
		if (row->state == GTK_STATE_SELECTED) {
			break;
		}
	}

	if (row == NULL) {
		return;
	}
	
	/* Show dialog */
	mime_type = (const char *) row->data;

	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (application_button))) {
		show_edit_applications_dialog (mime_type);
	} else {
		show_edit_components_dialog (mime_type);
	}
}


void
nautilus_mime_type_capplet_update_mime_list_icon_and_description (const char *mime_string)
{
	char *text;        
	const char *description;
        gint row;
	GdkPixbuf *pixbuf;
	GdkPixmap *pixmap;
	GdkBitmap *bitmap;
	GtkCList *clist;

	clist = GTK_CLIST (mime_list);
	
	pixbuf = NULL;
	
	row = GPOINTER_TO_INT (clist->selection->data);

	gnome_vfs_mime_info_reload ();

	/* Get description text */
	description = gnome_vfs_mime_get_description (mime_string);	
	if (description != NULL && strlen (description) > 0) {
		text = g_strdup (description);
	} else {
		text = g_strdup ("");
	}

	/* Set description column icon */
	pixbuf = capplet_get_icon_pixbuf (mime_string, FALSE);

	if (pixbuf != NULL) {
		pixbuf = capplet_gdk_pixbuf_scale_to_fit (pixbuf, MAX_ICON_WIDTH_IN_LIST, MAX_ICON_HEIGHT_IN_LIST);
		gdk_pixbuf_render_pixmap_and_mask (pixbuf, &pixmap, &bitmap, 100);
		gtk_clist_set_pixtext (clist, row, 0, text, 5, pixmap, bitmap);
		gdk_pixbuf_unref (pixbuf);
	}

	g_free (text);
}

/*
 * nautilus_mime_type_capplet_update_application_info
 * 
 * Update state of the applications menu.  This function is called
 * when the Edit Applications dialog is closed with an OK.
 */
 
void
nautilus_mime_type_capplet_update_application_info (const char *mime_type)
{
	populate_application_menu (default_menu, mime_type);
}

/*
 * nautilus_mime_type_capplet_update_component_info
 * 
 * Update state of the components menu.  This function is called
 * when the Edit Componests dialog is closed with an OK.
 */
 
void
nautilus_mime_type_capplet_update_viewer_info (const char *mime_type)
{
	populate_viewer_menu (default_menu, mime_type);
}

static void
update_mime_list_action (const char *mime_string)
{
	GdkPixbuf *pixbuf;
	GdkPixmap *pixmap;
	GdkBitmap *bitmap;
	GnomeVFSMimeAction *action;
	GnomeVFSMimeApplication *default_app;
	OAF_ServerInfo *default_component;
	char *text, *tmp_text, *icon_path;
	int row;
	
	pixbuf = NULL;
	row = GPOINTER_TO_INT (GTK_CLIST (mime_list)->selection->data);

	text = g_strdup(_("none"));

	action = gnome_vfs_mime_get_default_action (mime_string);
	if (action != NULL) {
		switch (action->action_type) {
			/* FIXME: Big hunks of this code are copied/pasted in several
			 * places in this file. Need to use common routines. One way
			 * to find them is to search for "nautilus/gnome-library.png"
			 */
			case GNOME_VFS_MIME_ACTION_TYPE_APPLICATION:
				/* Get the default application */
				default_app = gnome_vfs_mime_get_default_application (mime_string);
				g_free (text);
				text = g_strdup (default_app->name);							
				icon_path = capplet_get_icon_path (DEFAULT_ACTION_ICON);
				if (icon_path != NULL) {
					pixbuf = gdk_pixbuf_new_from_file (icon_path);
					g_free (icon_path);
				}
				gnome_vfs_mime_application_free (default_app);
				break;

			case GNOME_VFS_MIME_ACTION_TYPE_COMPONENT:
				/* Get the default component */
				default_component = gnome_vfs_mime_get_default_component (mime_string);
				g_free (text);
				tmp_text = name_from_oaf_server_info (default_component);
				text = g_strdup_printf (_("View as %s"), tmp_text);
				g_free (tmp_text);
				icon_path = capplet_get_icon_path ("nautilus/gnome-library.png");
				if (icon_path != NULL) {
					pixbuf = gdk_pixbuf_new_from_file (icon_path);
					g_free (icon_path);
				}
				CORBA_free (default_component);
				break;
				
			default:
				gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (application_button), TRUE);				
				break;
		}
	}

	/* Set column icon */
	if (pixbuf != NULL) {
		pixbuf = capplet_gdk_pixbuf_scale_to_fit (pixbuf, MAX_ICON_WIDTH_IN_LIST, MAX_ICON_HEIGHT_IN_LIST);
		gdk_pixbuf_render_pixmap_and_mask (pixbuf, &pixmap, &bitmap, 100);
		gtk_clist_set_pixtext (GTK_CLIST (mime_list), row, 3, text, 5, pixmap, bitmap);
		gdk_pixbuf_unref (pixbuf);
	} else {
		/* Just set text with no icon */
		gtk_clist_set_text (GTK_CLIST (mime_list), row, 3, text);
	}
	g_free (text);
}

/* FIXME:
 * This routine is never called with is_executable TRUE anymore. It
 * could be simplified, possibly out of existence.
 */
static GdkPixbuf *
capplet_get_icon_pixbuf (const char *mime_string, gboolean is_executable)
{
	const char *icon_name;
	char *icon_path;
	GdkPixbuf *pixbuf;

	pixbuf = NULL;

	icon_name = gnome_vfs_mime_get_icon (mime_string);
	if (icon_name == NULL) {
		icon_name = is_executable
			? DEFAULT_ACTION_ICON
			: DEFAULT_REGULAR_ICON;
	}

	icon_path = capplet_get_icon_path (icon_name);
	if (icon_path != NULL) {
		pixbuf = gdk_pixbuf_new_from_file (icon_path);
		g_free (icon_path);
	}

	return pixbuf;
}

static void
populate_mime_list (GList *type_list, GtkCList *clist)
{
	char *text[4], *tmp_text;        
	const char *description;
	char *icon_path;
	char *extensions, *mime_string;
        gint row;
	GList *element;
	GdkPixbuf *pixbuf;
	GdkPixmap *pixmap;
	GdkBitmap *bitmap;
	GnomeVFSMimeAction *action;
	GnomeVFSMimeApplication *default_app;
	OAF_ServerInfo *default_component;

	for (element = type_list; element != NULL; element= element->next) {
		mime_string = (char *)element->data;
		
		pixbuf = NULL;
			
		/* Add description to first column */
		description = gnome_vfs_mime_get_description (mime_string);	
		if (description != NULL && strlen (description) > 0) {
			text[0] = g_strdup (description);
		} else {
			text[0] = g_strdup ("");
		}

		/* Add mime type to second column */
		text[1] = mime_string;

		/* Add extension to third columns */
		extensions = gnome_vfs_mime_get_extensions_pretty_string (mime_string);
		if (extensions != NULL) {
			text[2] = extensions;
		} else {
			text[2] = "";
		}
		
		/* Add default action to fourth column */
		text[3] = g_strdup(_("none"));
		
		/* Insert item into list */
		row = gtk_clist_insert (GTK_CLIST (clist), 1, text);
		gtk_clist_set_row_data (GTK_CLIST (clist), row, g_strdup (mime_string));
		
		/* Set description column icon */
		pixbuf = capplet_get_icon_pixbuf (mime_string, FALSE);
		
		if (pixbuf != NULL) {
			/* FIXME: Big hunks of this code are copied/pasted in several
			 * places in this file. Need to use common routines. One way
			 * to find them is to search for MAX_ICON_WIDTH_IN_LIST
			 */
			pixbuf = capplet_gdk_pixbuf_scale_to_fit (pixbuf, MAX_ICON_WIDTH_IN_LIST, MAX_ICON_HEIGHT_IN_LIST);
			gdk_pixbuf_render_pixmap_and_mask (pixbuf, &pixmap, &bitmap, 100);
			gtk_clist_set_pixtext (clist, row, 0, text[0], 5, pixmap, bitmap);
			gdk_pixbuf_unref (pixbuf);
		}

		/* Set up action column */
		pixbuf = NULL;
		action = gnome_vfs_mime_get_default_action (mime_string);
		if (action != NULL) {				
			switch (action->action_type) {
			case GNOME_VFS_MIME_ACTION_TYPE_APPLICATION:
				/* Get the default application */
				default_app = gnome_vfs_mime_get_default_application (mime_string);						
				g_free (text[3]);
				text[3] = g_strdup (default_app->name);
				
				icon_path = capplet_get_icon_path (DEFAULT_ACTION_ICON);
				if (icon_path != NULL) {
					pixbuf = gdk_pixbuf_new_from_file (icon_path);
					g_free (icon_path);
				}
				gnome_vfs_mime_application_free (default_app);
				break;

			case GNOME_VFS_MIME_ACTION_TYPE_COMPONENT:
				/* Get the default component */
				default_component = gnome_vfs_mime_get_default_component (mime_string);
				g_free (text[3]);								
				tmp_text = name_from_oaf_server_info (default_component);
				text[3] = g_strdup_printf (_("View as %s"), tmp_text);
				g_free (tmp_text);
				icon_path = capplet_get_icon_path ("nautilus/gnome-library.png");
				if (icon_path != NULL) {
					pixbuf = gdk_pixbuf_new_from_file (icon_path);
					g_free (icon_path);
				}
				CORBA_free (default_component);
				break;
				
			default:
				gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (application_button), TRUE);
				break;
			}
		}
			
		/* Set column icon */
		if (pixbuf != NULL) {
			pixbuf = capplet_gdk_pixbuf_scale_to_fit (pixbuf, MAX_ICON_WIDTH_IN_LIST, MAX_ICON_HEIGHT_IN_LIST);
			gdk_pixbuf_render_pixmap_and_mask (pixbuf, &pixmap, &bitmap, 100);
			gtk_clist_set_pixtext (clist, row, 3, text[3], 5, pixmap, bitmap);
			gdk_pixbuf_unref (pixbuf);
		}
		
		g_free (text[0]);
		g_free (text[3]);
		g_free (extensions);			
	}
}

static gint
sort_case_insensitive (GtkCList *clist, gpointer ptr1, gpointer ptr2)
{
	const char *text1 = NULL;
	const char *text2 = NULL;
	
	GtkCListRow *row1 = (GtkCListRow *) ptr1;
	GtkCListRow *row2 = (GtkCListRow *) ptr2;
	
	switch (row1->cell[clist->sort_column].type) {
		case GTK_CELL_TEXT:
			text1 = GTK_CELL_TEXT (row1->cell[clist->sort_column])->text;
			break;
		
		case GTK_CELL_PIXTEXT:
			text1 = GTK_CELL_PIXTEXT (row1->cell[clist->sort_column])->text;
			break;
		
		default:
			break;
	}
	
	switch (row2->cell[clist->sort_column].type) {
		case GTK_CELL_TEXT:
			text2 = GTK_CELL_TEXT (row2->cell[clist->sort_column])->text;
			break;
			
		case GTK_CELL_PIXTEXT:
			text2 = GTK_CELL_PIXTEXT (row2->cell[clist->sort_column])->text;
			break;
	
		default:
			break;
	}
	
	if (text2 == NULL) {
		return (text1 != NULL);
	}
	
	if (text1 == NULL) {
		return -1;
	}
	
	return strcasecmp (text1, text2);
}

static void
column_clicked (GtkCList *clist, gint column, gpointer user_data)
{
	gtk_clist_set_sort_column (clist, column);

	/* If the user has not clicked the column yet, make sure
	 * that the sort type is descending the first time.
	 */
	 if (!sort_column_clicked [column]) {
		clist->sort_type = GTK_SORT_DESCENDING;
		sort_column_clicked [column] = TRUE;
	}
		
	/* Toggle sort type */
	if (clist->sort_type == GTK_SORT_ASCENDING) {
		gtk_clist_set_sort_type (clist, GTK_SORT_DESCENDING);
	} else {
		gtk_clist_set_sort_type (clist, GTK_SORT_ASCENDING);
	}
	
	gtk_clist_sort (clist);
}

static void
mime_list_reset_row_height (GtkCList *list)
{
	guint height_for_icon;
	guint height_for_text;

	height_for_icon = MAX_ICON_HEIGHT_IN_LIST + 1;
	height_for_text = GTK_WIDGET (list)->style->font->ascent +
			  GTK_WIDGET (list)->style->font->descent + 1;
	gtk_clist_set_row_height (list, MAX (height_for_icon, height_for_text));
}

static GtkWidget *
create_mime_list_and_scroller (void)
{
        GtkWidget *window;
        gchar *titles[TOTAL_COLUMNS];
	GList *type_list;
	int index;
	
        titles[0] = _("Description");
        titles[1] = _("MIME Type");
        titles[2] = _("Extension");
        titles[3] = _("Default Action");
        
        window = gtk_scrolled_window_new (NULL, NULL);
	gtk_widget_set_usize (window, 500, 200);
        gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (window),
                                        GTK_POLICY_AUTOMATIC,
                                        GTK_POLICY_AUTOMATIC);
	mime_list = gtk_clist_new_with_titles (TOTAL_COLUMNS, titles);
        gtk_clist_set_selection_mode (GTK_CLIST (mime_list), GTK_SELECTION_BROWSE);
        gtk_clist_set_compare_func (GTK_CLIST (mime_list), (GtkCListCompareFunc) sort_case_insensitive);

	type_list = gnome_vfs_get_registered_mime_types ();
	populate_mime_list (type_list, GTK_CLIST (mime_list));
	gnome_vfs_mime_registered_mime_type_list_free (type_list);
	
        gtk_clist_columns_autosize (GTK_CLIST (mime_list));
        gtk_clist_select_row (GTK_CLIST (mime_list), 0, 0);
        gtk_container_add (GTK_CONTAINER (window), mime_list);

        /* Enable all titles */
	gtk_clist_column_titles_active (GTK_CLIST (mime_list));
	gtk_signal_connect (GTK_OBJECT (mime_list), "click_column", 
			    column_clicked, NULL);
	
	/* Turn off autoresizing of columns */
	for (index = 0; index < TOTAL_COLUMNS; index++) {
		gtk_clist_set_column_auto_resize (GTK_CLIST (mime_list), index, FALSE);
	}

	/* Make height tall enough for icons to look good.
	 * This must be done after the list widget is realized, due to
	 * a bug/design flaw in nautilus_clist_set_row_height. Connecting to
	 * the "realize" signal is slightly too early, so we connect to
	 * "map".
	 */
	gtk_signal_connect (GTK_OBJECT (mime_list),
			    "map",
			    mime_list_reset_row_height,
			    NULL);
		
        return window;
}

const char * 
nautilus_mime_type_capplet_get_selected_item_mime_type (void)
{
	const char *mime_type;
	int row;
	GtkCList *clist;

	clist = GTK_CLIST (mime_list);
	
	if (clist->selection == NULL) {
		return NULL;
	}

	/* This is a single selection list, so we just use the first item in 
	 * the list to retrieve the data */
	row = GPOINTER_TO_INT (clist->selection->data);

	mime_type = (const char *) gtk_clist_get_row_data (clist, row);

	return mime_type;
}

/**
 * gtk_label_make_bold.
 *
 * Switches the font of label to a bold equivalent.
 * @label: The label.
 **/

static void
gtk_widget_make_bold (GtkWidget *widget)
{
	GtkStyle *style;
	GdkFont *bold_font;

	g_return_if_fail (GTK_IS_WIDGET (widget));
	style = gtk_widget_get_style (widget);

	bold_font = gdk_font_get_bold (style->font);

	if (bold_font == NULL) {
		return;
	}

	gtk_widget_set_font (widget, bold_font);
	gdk_font_unref (bold_font);
}

/**
 * gtk_widget_set_font
 *
 * Sets the font for a widget's style, managing the style objects.
 * @widget: The widget.
 * @font: The font.
 **/
static void
gtk_widget_set_font (GtkWidget *widget, GdkFont *font)
{
	GtkStyle *new_style;
	
	g_return_if_fail (GTK_IS_WIDGET (widget));
	g_return_if_fail (font != NULL);
	
	new_style = gtk_style_copy (gtk_widget_get_style (widget));

	gtk_style_set_font (new_style, font);
	
	gtk_widget_set_style (widget, new_style);
	gtk_style_unref (new_style);
}

/**
 * gtk_style_set_font
 *
 * Sets the font in a style object, managing the ref. counts.
 * @style: The style to change.
 * @font: The new font.
 **/
static void
gtk_style_set_font (GtkStyle *style, GdkFont *font)
{
	g_return_if_fail (style != NULL);
	g_return_if_fail (font != NULL);
	
	gdk_font_ref (font);
	gdk_font_unref (style->font);
	style->font = font;
}

/**
 * gdk_font_get_bold
 * @plain_font: A font.
 * Returns: A bold variant of @plain_font or NULL.
 *
 * Tries to find a bold flavor of a given font. Returns NULL if none is available.
 */
static GdkFont *
gdk_font_get_bold (const GdkFont *plain_font)
{
	const char *plain_name;
	const char *scanner;
	char *bold_name;
	int count;
	GSList *p;
	GdkFont *result;
	GdkFontPrivate *private_plain;

	private_plain = (GdkFontPrivate *)plain_font;

	if (private_plain->names == NULL) {
		return NULL;
	}


	/* -foundry-family-weight-slant-sel_width-add-style-pixels-points-hor_res-ver_res-spacing-average_width-char_set_registry-char_set_encoding */

	bold_name = NULL;
	for (p = private_plain->names; p != NULL; p = p->next) {
		plain_name = (const char *)p->data;
		scanner = plain_name;

		/* skip past foundry and family to weight */
		for (count = 2; count > 0; count--) {
			scanner = strchr (scanner + 1, '-');
			if (!scanner) {
				break;
			}
		}

		if (!scanner) {
			/* try the other names in the list */
			continue;
		}
		g_assert (*scanner == '-');

		/* copy "-foundry-family-" over */
		scanner++;
		bold_name = g_strndup (plain_name, scanner - plain_name);

		/* skip weight */
		scanner = strchr (scanner, '-');
		g_assert (scanner != NULL);

		/* add "bold" and copy everything past weight over */
		bold_name = g_strconcat (bold_name, "bold", scanner, NULL);
		break;
	}
	
	if (bold_name == NULL) {
		return NULL;
	}
	
	result = gdk_font_load (bold_name);
	g_free (bold_name);

	return result;
}


/* scale the passed in pixbuf to conform to the passed-in maximum width and height */
/* utility routine to scale the passed-in pixbuf to be smaller than the maximum allowed size, if necessary */
static GdkPixbuf *
capplet_gdk_pixbuf_scale_to_fit (GdkPixbuf *pixbuf, int max_width, int max_height)
{
	double scale_factor;
	double h_scale = 1.0;
	double v_scale = 1.0;

	int width  = gdk_pixbuf_get_width(pixbuf);
	int height = gdk_pixbuf_get_height(pixbuf);
	
	if (width > max_width) {
		h_scale = max_width / (double) width;
	}
	if (height > max_height) {
		v_scale = max_height  / (double) height;
	}
	scale_factor = MIN (h_scale, v_scale);
	
	if (scale_factor < 1.0) {
		GdkPixbuf *scaled_pixbuf;
		/* the width and scale factor are always > 0, so it's OK to round by adding here */
		int scaled_width  = floor(width * scale_factor + .5);
		int scaled_height = floor(height * scale_factor + .5);
				
		scaled_pixbuf = gdk_pixbuf_scale_simple (pixbuf, scaled_width, scaled_height, GDK_INTERP_BILINEAR);	
		gdk_pixbuf_unref (pixbuf);
		pixbuf = scaled_pixbuf;
	}
	
	return pixbuf;
}