File: prefs.c

package info (click to toggle)
gtkpod 0.99.14-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 7,716 kB
  • ctags: 3,266
  • sloc: ansic: 43,120; sh: 4,837; xml: 1,316; lex: 637; perl: 264; makefile: 236; awk: 73; python: 35
file content (2309 lines) | stat: -rw-r--r-- 60,967 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
/*
|  Copyright (C) 2002-2007 Jorg Schuler <jcsjcs at users sourceforge net>
|  Copyright (C) 2006 James Liggett <jrliggett at cox.net>
|  Part of the gtkpod project.
|
|  URL: http://www.gtkpod.org/
|  URL: http://gtkpod.sourceforge.net/
|
|  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
|
|  iTunes and iPod are trademarks of Apple
|
|  This product is not supported/written/published by Apple!
|
|  $Id: prefs.c 2216 2009-01-17 05:47:46Z jcsjcs $
*/

/* -------------------------------------------------------------------
 *
 * HOWTO add a new_option to the prefs dialog
 *
 * - add the desired option to the prefs window using glade-2
 *
 * - set the default value of new_option in set_default_preferences() in prefs.c
 *
 * - add a callback on_new_option_*() to prefs_windows.c to set the
 *   new value.
 *   The value is applied to the actual prefs when pressing the "OK"
 *   or "Apply" button in the prefs window.
 *
 * - add code to prefs_window_create() in prefs_window.c to set the
 *   correct state of the option in the prefs window.
 *
 * - if you want new_option to be a command line option as well, add
 *   code to usage() and read_commandline().
 *
 * - for environment variables, add code to read_environment().
 *
 * ---------------------------------------------------------------- */

/* ----------------------------------------------------------------
 *
 * The prefs module should be thread safe. The hash table is locked
 * before each read or write access.
 *
 * The temp_prefs module is not thread-safe. If necessary a locking
 * mechanism can be implemented.
 *
 * ---------------------------------------------------------------- */




/* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
   Ditto for AIX 3.2 and <stdlib.h>.  */
#ifndef _NO_PROTO
# define _NO_PROTO
#endif

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <stdio.h>
#include <string.h>
#include <glib/gstdio.h>
#ifdef HAVE_GETOPT_LONG_ONLY
#  include <getopt.h>
#else
#  include "getopt.h"
#endif

#include "clientserver.h"
#include "misc.h"
#include "prefs.h"

/*
 * Data global to this module only
 */

/* End-of-list marker for variable-length lists */
#define LIST_END_MARKER "----++++----"

struct temp_prefs_save
{
    GIOChannel *gio;
    GError **error;
    gboolean success;
};

struct sub_data
{
    TempPrefs *temp_prefs;
    TempPrefs *temp_prefs_orig;
    const gchar *subkey;
    const gchar *subkey2;
    gboolean exists;
};

/* Pointer to preferences hash table */
static GHashTable *prefs_table = NULL;
static GMutex *prefs_table_mutex = NULL;

/*
 * Functions used by this module only
 */
void discard_prefs (void);

/* Different paths that can be set in the prefs window */
typedef enum
{
    PATH_PLAY_NOW = 0,
    PATH_PLAY_ENQUEUE,
    PATH_MP3GAIN,
    PATH_SYNC_CONTACTS,
    PATH_SYNC_CALENDAR,
    PATH_MSERV_MUSIC_ROOT,
    PATH_MSERV_TRACKINFO_ROOT,
    PATH_SYNC_NOTES,
    PATH_AACGAIN,
    PATH_NUM
} PathType;

/* enum for reading of options */
enum {
  GP_HELP,
  GP_PLAYCOUNT,
  GP_MOUNT,
  GP_PRINT_HASH,
};


/* Lock the prefs table. If the table is already locked the calling
 * thread will remain blocked until the lock is released by the other thread. */
static void lock_prefs_table ()
{
    g_return_if_fail (prefs_table_mutex);
    g_mutex_lock (prefs_table_mutex);
}

/* Unlock the prefs table again. */
static void unlock_prefs_table ()
{
    g_return_if_fail (prefs_table_mutex);
    g_mutex_unlock (prefs_table_mutex);
}


/* Set default preferences */
static void set_default_preferences()
{
    int i;
    gchar *str;

    prefs_set_int("update_existing", FALSE);
    prefs_set_int("id3_write", FALSE);
    prefs_set_int("id3_write_id3v24", FALSE);
    prefs_set_int(KEY_SYNC_DELETE_TRACKS, TRUE);
    prefs_set_int(KEY_SYNC_CONFIRM_DELETE, TRUE);
    prefs_set_int(KEY_SYNC_SHOW_SUMMARY, TRUE);
    prefs_set_int("show_duplicates", TRUE);
    prefs_set_int("show_non_updated", TRUE);
    prefs_set_int("show_updated", TRUE);
    prefs_set_int("photo_library_confirm_delete", TRUE);
    prefs_set_int("mserv_report_probs", TRUE);
    prefs_set_int("delete_ipod", TRUE);
    prefs_set_int("delete_file", TRUE);
    prefs_set_int("delete_local_file", TRUE);
    prefs_set_int("delete_database", TRUE);
    prefs_set_string("initial_mountpoint", "/mnt/ipod");
    prefs_set_string ("path_play_now", "xmms %s");
    prefs_set_string ("path_play_enqueue", "xmms -e %s");
    prefs_set_string ("path_mserv_trackinfo_root", "/var/lib/mserv/trackinfo/");
    prefs_set_string ("coverart_display_bg_color", "#000000");
    prefs_set_string ("coverart_display_fg_color", "#FFFFFF");
    
    str = g_build_filename (SCRIPTDIR, "convert-ogg2mp3.sh", NULL);
    prefs_set_string ("path_conv_ogg", str);
    g_free (str);
    prefs_set_int ("convert_ogg", TRUE);

    str = g_build_filename (SCRIPTDIR, "convert-flac2mp3.sh", NULL);
    prefs_set_string ("path_conv_flac", str);
    g_free (str);
    prefs_set_int ("convert_flac", TRUE);

    str = g_build_filename (SCRIPTDIR, "convert-wav2mp3.sh", NULL);
    prefs_set_string ("path_conv_wav", str);
    g_free (str);
    prefs_set_int ("convert_wav", FALSE);


    /* Set sorting tab defaults */
    for (i = 0; i < SORT_TAB_MAX; i++)
    {
	prefs_set_int_index("st_autoselect", i, TRUE);
	prefs_set_int_index("st_category", i, (i < ST_CAT_NUM ? i : 0));
	prefs_set_int_index("sp_or", i, FALSE);
	prefs_set_int_index("sp_rating_cond", i, FALSE);
	prefs_set_int_index("sp_playcount_cond", i, FALSE);
	prefs_set_int_index("sp_played_cond", i, FALSE);
	prefs_set_int_index("sp_modified_cond", i, FALSE);
	prefs_set_int_index("sp_added_cond", i, FALSE);
	prefs_set_int_index("sp_rating_state", i, 0);
	prefs_set_string_index("sp_played_state", i, ">4w");
	prefs_set_string_index("sp_modified_state", i, "<1d");
	prefs_set_string_index("sp_added_state", i, "<1d");
	prefs_set_int_index("sp_playcount_low", i, 0);
	prefs_set_int_index("sp_playcount_high", i, -1);
	prefs_set_int_index("sp_autodisplay", i, FALSE);
    }
    
    prefs_set_int("sort_tab_num", 2);
    
    /* Set colum preferences */
    for (i = 0; i < TM_NUM_COLUMNS; i++)
    {
	prefs_set_int_index("col_order", i, i);
    }

    prefs_set_int_index("col_visible", TM_COLUMN_ARTIST, TRUE);
    prefs_set_int_index("col_visible", TM_COLUMN_ALBUM, TRUE);
    prefs_set_int_index("col_visible", TM_COLUMN_TITLE, TRUE);
    prefs_set_int_index("col_visible", TM_COLUMN_TRACKLEN, TRUE);
    prefs_set_int_index("col_visible", TM_COLUMN_RATING, TRUE);

    for (i = 0; i < TM_NUM_TAGS_PREFS; i++)
    {
	prefs_set_int_index("tag_autoset", i, FALSE);
    }

    prefs_set_int_index("tag_autoset", TM_COLUMN_TITLE, TRUE);
    
    prefs_set_int("horizontal_scrollbar", TRUE);
    prefs_set_int("filter_tabs_top", FALSE);

    /* Set pane positions--Let gtk worry about position */
    for (i = 0; i < PANED_NUM; i++)
	prefs_set_int_index("paned_pos_", i, -1);

    prefs_set_int("mpl_autoselect", TRUE);

    /* Set window sizes */
    prefs_set_int("size_gtkpod.x", 780);
    prefs_set_int("size_gtkpod.y", 580);
    prefs_set_int("size_cal.x", 500);
    prefs_set_int("size_cal.y", 300);
    prefs_set_int("size_conf_sw.x", 300);
    prefs_set_int("size_conf_sw.y", 300);
    prefs_set_int("size_conf.x", 300);
    prefs_set_int("size_conf.y", -1);
    prefs_set_int("size_dirbr.x", 300);
    prefs_set_int("size_dirbr.y", 400);
    prefs_set_int("size_prefs.x", -1);
    prefs_set_int("size_prefs.y", 480);
    prefs_set_int("size_info.x", 510);
    prefs_set_int("size_info.y", 300);

    /* size of file dialog if there is not a details textview */
    prefs_set_int("size_file_dialog.x", 320);
    prefs_set_int("size_file_dialog.y", 140);

    /* size of file dialog if there is a details textview */
    prefs_set_int("size_file_dialog_details.x", 320);
    prefs_set_int("size_file_dialog_details.y", 140);

    prefs_set_int("readtags", TRUE);
    prefs_set_int("write_extended_info", TRUE);
    prefs_set_int("parsetags", FALSE);
    prefs_set_int("parsetags_overwrite", FALSE);
    prefs_set_string("parsetags_template", "%a - %A/%T %t.mp3;%t.wav");
    prefs_set_int("coverart_apic", TRUE);
    prefs_set_int("coverart_file", TRUE);
    prefs_set_string("coverart_template", "%A;folder.jpg");
    prefs_set_string("video_thumbnailer_prog", "totem-video-thumbnailer %f %o");
    prefs_set_int("mserv_use", FALSE);
    prefs_set_string("mserv_username", "");
    prefs_set_int("startup_messages", TRUE);
    prefs_set_int("add_recursively", TRUE);
    prefs_set_int("info_window", FALSE);
    prefs_set_int("last_prefs_page", 0);
    prefs_set_int("multi_edit_title", TRUE);
    prefs_set_int("multi_edit", FALSE);
    prefs_set_int("not_played_track", TRUE);
    prefs_set_int("misc_track_nr", 25);
    prefs_set_int("update_charset", FALSE);
    prefs_set_int("display_tooltips_main", TRUE);
    prefs_set_int("display_tooltips_prefs", TRUE);
    prefs_set_int("display_toolbar", TRUE);
    prefs_set_int("toolbar_style", GTK_TOOLBAR_BOTH);
    prefs_set_int("sha1", TRUE);
    prefs_set_int("file_dialog_details_expanded", FALSE);

    /* Set last browsed directory */
    str = g_get_current_dir();

    if (str)
    {
	prefs_set_string("last_dir_browsed", str);
	g_free(str);
    }
    else
	prefs_set_string("last_dir_browsed", g_get_home_dir());
    
    /* Set sorting prefs */
    prefs_set_int("case_sensitive", FALSE);
    prefs_set_int("tm_autostore", FALSE);
    prefs_set_int("st_sort", SORT_NONE);
    prefs_set_int("pm_sort", SORT_NONE);
    prefs_set_int("tm_sortcol", TM_COLUMN_TITLE);
    prefs_set_int("tm_sort", SORT_NONE);
	
	/* New conversion preferences */
	prefs_set_int("conversion_target_format", TARGET_FORMAT_MP3);
}

/* Initialize default variable-length list entries */
static void set_default_list_entries()
{
    if (!prefs_get_string_value_index("sort_ign_string_", 0, NULL))
    {
	prefs_set_string_index("sort_ign_string_", 0, "a ");
	prefs_set_string_index("sort_ign_string_", 1, "an ");
	prefs_set_string_index("sort_ign_string_", 2, LIST_END_MARKER);
    }
}

/* A printf-like function that outputs in the system locale */
static void locale_fprintf(FILE *fp, const gchar *format, ...)
{
    gchar *utf8_string; /* Raw UTF-8 string */
    gchar *locale_string;  /* String in system locale format */
    va_list format_list;  /* Printf-like formatting arguments */
	
    /* Create the locale format string based on the given format */
    va_start(format_list, format);
    utf8_string = g_strdup_vprintf(format, format_list);
    va_end(format_list);
	
    locale_string = g_locale_from_utf8 (utf8_string, -1, NULL, NULL, NULL);
	
    if (fp)
	fprintf(fp, "%s", locale_string);
	
    g_free(utf8_string);
    g_free(locale_string);
}

/* Print commandline usage information */
static void usage(FILE *fp)
{
  locale_fprintf(fp, _("gtkpod version %s usage:\n"), VERSION);
  locale_fprintf(fp, _("  -h, --help:   display this message\n"));
  locale_fprintf(fp, _("  -p <file>:    increment playcount for file by one\n"));
  locale_fprintf(fp, _("  --hash <file>:print gtkpod hash for file\n"));
  locale_fprintf(fp, _("  -m path:      define the mountpoint of your iPod\n"));
  locale_fprintf(fp, _("  --mountpoint: same as '-m'.\n"));
}

/* Parse commandline based options */
static void read_commandline(int argc, char *argv[])
{
    int option; /* Code returned by getopt */
	
    /* The options data structure. The format is standard getopt. */
    struct option const options[] =
	{
	    { "h",           no_argument,	NULL, GP_HELP },
	    { "help",        no_argument,	NULL, GP_HELP },
	    { "p",           required_argument, NULL, GP_PLAYCOUNT },
	    { "hash",        required_argument, NULL, GP_PRINT_HASH },
	    { "m",           required_argument,	NULL, GP_MOUNT },
	    { "mountpoint",  required_argument,	NULL, GP_MOUNT },
	    { 0, 0, 0, 0 }
	};
	
    /* Handle commandline options */
    while ((option = getopt_long_only(argc, argv, "", options, NULL)) != -1)
    {
	switch (option)
	{
	case GP_HELP:
	    usage(stdout);
	    exit(0);
	    break;
	case GP_PLAYCOUNT:
	    client_playcount(optarg);
	    exit(0);
	    break;
	case GP_PRINT_HASH:
	    print_sha1_hash (optarg);
	    exit(0);
	    break;
	case GP_MOUNT:
	    prefs_set_string("initial_mountpoint", optarg);
	    break;
	default:
	    locale_fprintf(stderr, "Unknown option: %s\n", argv[optind]);
	    usage(stderr);
	    exit(1);
	    break;
	};
    }
}

/* Read options from environment variables */
static void read_environment()
{
    gchar *buf; 
  
    buf = convert_filename(getenv("IPOD_MOUNTPOINT"));
    if (buf)
	prefs_set_string("initial_mountpoint", buf);
    g_free(buf);
}
 
/* Create a full numbered key from a base key string and a number.
 * Free returned string. */
static gchar *create_full_key(const gchar *base_key, gint index)
{
    if (base_key)
	return g_strdup_printf("%s%i", base_key, index);
    else 
	return NULL;
}

/* Remove key present in the temp prefs tree from the hash table */
static gboolean flush_key (gpointer key, gpointer value, gpointer user_data)
{
    g_return_val_if_fail (prefs_table, FALSE);

    g_hash_table_remove (prefs_table, key);

    return FALSE;
}


/* Copy key data from the temp prefs tree to the hash table (or to
 * sub_data->temp_prefs_orig if non-NULL). The old key is removed. */
static gboolean subst_key (gpointer key, gpointer value, gpointer user_data)
{
    struct sub_data *sub_data = user_data;
    gint len;

    g_return_val_if_fail (key && value && user_data, FALSE);
    g_return_val_if_fail (sub_data->subkey && sub_data->subkey2, FALSE);
    if (!sub_data->temp_prefs_orig)
	g_return_val_if_fail (prefs_table, FALSE);
    if (sub_data->temp_prefs_orig)
	g_return_val_if_fail (sub_data->temp_prefs_orig->tree, FALSE);

    len = strlen (sub_data->subkey);

    if (strncmp (key, sub_data->subkey, len) == 0)
    {
	gchar *new_key = g_strdup_printf ("%s%s",
					  sub_data->subkey2,
					  ((gchar *)key)+len);
	if (sub_data->temp_prefs_orig)
	{
	    g_tree_remove (sub_data->temp_prefs_orig->tree, key);
	    g_tree_insert (sub_data->temp_prefs_orig->tree,
			   new_key, g_strdup(value));
	}
	else
	{
	    g_hash_table_remove (prefs_table, key);
	    g_hash_table_insert (prefs_table, new_key, g_strdup(value));
	}
    }
    return FALSE;
}

/* return TRUE if @key starts with @subkey */
static gboolean match_subkey (gpointer key, gpointer value, gpointer subkey)
{
    g_return_val_if_fail (key && subkey, FALSE);

    if (strncmp (key, subkey, strlen (subkey)) == 0)  return TRUE;
    return FALSE;
}


/* return TRUE and set sub_data->exists to TRUE if @key starts with
 * @subkey */
static gboolean check_subkey (gpointer key, gpointer value, gpointer user_data)
{
    struct sub_data *sub_data = user_data;

    g_return_val_if_fail (key && user_data, TRUE);
    g_return_val_if_fail (sub_data->subkey, TRUE);

    if (strncmp (key, sub_data->subkey, strlen (sub_data->subkey)) == 0)
    {
	sub_data->exists = TRUE;
	return TRUE;
    }
    return FALSE;
}



/* Add key/value to temp_prefs if it matches subkey -- called by
 * prefs_create_subset() and temp_prefs_create_subset() */
static gboolean get_subset (gpointer key, gpointer value, gpointer user_data)
{
    struct sub_data *sub_data = user_data;

    g_return_val_if_fail (key && value && user_data, TRUE);
    g_return_val_if_fail (sub_data->subkey && sub_data->temp_prefs, TRUE);

    if (strncmp (key, sub_data->subkey,
		 strlen (sub_data->subkey)) == 0)
    {  /* match */
	temp_prefs_set_string (sub_data->temp_prefs, key, value);
    }
    return FALSE; /* continue traversal (g_tree), ignored for g_hash */
}


/* Copy a variable-length list to the prefs table */
static gboolean copy_list(gpointer key, gpointer value, gpointer user_data)
{
	prefs_apply_list((gchar*)key, (GList*)value);
	return FALSE;
}
 
/* Callback that writes pref table data to a file */
static void write_key(gpointer key, gpointer value, gpointer user_data)
{
    FILE *fp;  /* file pointer passed in through user_data */
	
    /* Write out each key and value to the given file */
    fp = (FILE*)user_data;
	
    if (fp)
	fprintf(fp, "%s=%s\n", (gchar*)key, (gchar*)value);
}

/* Gets a string that contains ~/.gtkpod/ If the folder doesn't exist,
 * create it. Free the string when you are done with it.
 * If the folder wasn't found, and couldn't be created, return NULL */
gchar *prefs_get_cfgdir()
{
    gchar *folder;  /* Folder path */
	
    /* Create the folder path. If the folder doesn't exist, create it. */
    folder = g_build_filename(g_get_home_dir(), ".gtkpod", NULL);
	
    if (!g_file_test(folder, G_FILE_TEST_IS_DIR))
    {
	if ((g_mkdir(folder, 0777)) == -1)
	{
	    printf(_("Couldn't create '%s'\n"), folder);
	    g_free (folder);
	    return NULL;
	}
    }
    return folder;
}


/* get @key and @value from a string like "key=value" */
/* you must g_free (*key) and (*value) after use */
static gboolean read_prefs_get_key_value (const gchar *buf,
					  gchar **key, gchar **value)
{
    size_t len;  /* string length */
    const gchar *buf_start; /* Pointer to where actual useful data starts in line */

    g_return_val_if_fail (buf && key && value, FALSE);

    /* Strip out any comments (lines that begin with ; or #) */
    if ((buf[0] == ';') || (buf[0] == '#')) 
	return FALSE;

    /* Find the key and value, and look for malformed lines */
    buf_start = strchr (buf, '=');

    if ((!buf_start) || (buf_start == buf))
    {
	printf("Parse error reading prefs: %s", buf);
	return FALSE;
    }

    /* Find the key name */
    *key = g_strndup (buf, (buf_start - buf));

    /* Strip whitespace */
    g_strstrip (*key);

    /* Find the value string */
    *value = strdup (buf_start+1);

    /* remove newline */
    len = strlen (*value);
    if ((len > 0) && ((*value)[len - 1] == 0x0a))
	(*value)[len - 1] = 0;

    /* Don't strip whitespace! If there is any, there's a reason for it. */
    /* g_strstrip (*value); */

    return TRUE;
}


/* Read preferences from a file */
static void read_prefs_from_file(FILE *fp)
{
    gchar buf[PATH_MAX];  /* Buffer that contains one line */
    gchar *key;  /* Pref value key */
    gchar *value; /* Pref value */


    g_return_if_fail (prefs_table && fp);

    while (fgets(buf, PATH_MAX, fp))
    {
	if (read_prefs_get_key_value (buf, &key, &value))
	{
	    g_hash_table_insert (prefs_table, key, value);
	}
    }
}

/* Write prefs to file */
static void write_prefs_to_file(FILE *fp)
{
    lock_prefs_table ();

    if (!prefs_table)
    {
	unlock_prefs_table ();
	g_return_if_reached ();
    }

    g_hash_table_foreach(prefs_table, write_key, (gpointer)fp);

    unlock_prefs_table ();
}

/* Load preferences, first loading the defaults, and then overwrite that with
 * preferences in the user home folder. */
static void load_prefs()
{
    gchar *filename; /* Config path to open */
    gchar *config_dir;  /* Directory where config is (usually ~/.gtkpod) */
    FILE *fp;
	
    /* Start by initializing the prefs to their default values */
    set_default_preferences();
	
    /* and then override those values with those found in the home folder. */
    config_dir = prefs_get_cfgdir();
	
    if (config_dir)
    {
	filename = g_build_filename(config_dir, "prefs", NULL);
		
	if (filename)
	{
	    fp = fopen(filename, "r");
			
	    if (fp)
	    {
		read_prefs_from_file(fp);
		fclose(fp);
	    }
			
	    g_free(filename);
	}
		
	g_free(config_dir);
    }
	
    /* Finally, initialize variable-length lists. Do this after everything else
     * so that list defaults don't hang out in the table after prefs have been
     * read from the file. */
    set_default_list_entries();
}

/* Save preferences to user home folder (~/.gtkpod/prefs) */
void prefs_save ()
{
    gchar *filename;  /* Path of file to write to */
    gchar *config_dir;   /* Folder where prefs file is */
    FILE *fp;  /* File pointer */
	
    /* Open $HOME/.gtkpod/prefs, and write prefs */
    config_dir = prefs_get_cfgdir();
	
    if (config_dir)
    {
	filename = g_build_filename(config_dir, "prefs", NULL);
		
	if (filename)
	{
	    fp = fopen(filename, "w");

	    if (fp)
	    {
		write_prefs_to_file(fp);
		fclose(fp);
	    }
		
	    g_free(filename);
	}
		
	g_free(config_dir);
    }
}


static gboolean temp_prefs_save_fe (gchar *key, gchar *value,
				    struct temp_prefs_save *tps)
{
    gchar *buf;
    GIOStatus status;

    buf=g_strdup_printf ("%s=%s\n", key, value);
    status = g_io_channel_write_chars (tps->gio, buf, -1, NULL, tps->error);
    g_free (buf);
    if (status != G_IO_STATUS_NORMAL)
    {
	tps->success = FALSE;
	return TRUE;  /* stop traversal */
    }
    return FALSE;
}


/* Save @temp_prefs to @filename in the same manner as prefs_save is
 * saving to ~/.gtkpod/prefs. @error: location where to store
 * information about errors or NULL.
 * 
 * Return value: TRUE on success, FALSE if an error occured, in which
 * case @error will be set accordingly.
 */
gboolean temp_prefs_save (TempPrefs *temp_prefs,
			  const gchar *filename,
			  GError **error)
{
    GIOChannel *gio;
    struct temp_prefs_save tps;

    g_return_val_if_fail (temp_prefs && filename, FALSE);

    gio = g_io_channel_new_file (filename, "w", error);
    tps.gio = gio;
    tps.error = error;
    tps.success = TRUE;
    if (gio)
    {
	g_tree_foreach (temp_prefs->tree, (GTraverseFunc)temp_prefs_save_fe, &tps);
	g_io_channel_unref (gio);
    }

    return tps.success;
}


TempPrefs *temp_prefs_load (const gchar *filename, GError **error)
{
    GIOChannel *gio;
    TempPrefs *temp_prefs = NULL;

    g_return_val_if_fail (filename, NULL);

    gio = g_io_channel_new_file (filename, "r", error);
    if (gio)
    {
	GIOStatus status;

	temp_prefs = temp_prefs_create ();

	do
	{
	    gchar *line = NULL;

	    status = g_io_channel_read_line (gio, &line, NULL, NULL, error);
	    if (status == G_IO_STATUS_NORMAL)
	    {
		gchar *key, *value;
		if (read_prefs_get_key_value (line, &key, &value))
		{
		    temp_prefs_set_string (temp_prefs, key, value);
		    g_free (key);
		    g_free (value);
		}
	    }
	    g_free (line);
	} while (status == G_IO_STATUS_NORMAL);

	g_io_channel_unref (gio);

	if (status != G_IO_STATUS_EOF)
	{
	    temp_prefs_destroy (temp_prefs);
	    temp_prefs = NULL;
	}
    }

    return temp_prefs;
}


/* Removes already existing list keys from the prefs table */
static void wipe_list(const gchar *key)
{
    gchar *full_key; /* Complete key, with its number suffix */
    guint i;  /* Loop counter */
	
    /* Go through the prefs table, starting at key<number>, delete it and go 
     * through key<number+1>... until there are no keys left */
	
    for (i = 0;;i++)
    {
	full_key = create_full_key(key, i);
		
	if (g_hash_table_remove(prefs_table, full_key))
	{
	    g_free(full_key);
	    continue;
	}
	else /* We got all the unneeded keys, leave the loop... */
	{
	    g_free(full_key);
	    break;
	}
    }		
}

/* Delete and rename keys */
static void cleanup_keys()
{
    gchar *buf;
    gint int_buf;
    gint i;
    gint x, y, p;  /* Window position */
    float version=0;

    /* Get version */
    version = prefs_get_double("version");

    /* rename mountpoint to initial_mountpoint */
    if (prefs_get_string_value(KEY_MOUNTPOINT, &buf))
    {
	prefs_set_string("initial_mountpoint", buf);
	g_free(buf);
	prefs_set_string(KEY_MOUNTPOINT, NULL);
    }
  
    /* rename coverart to coverart_file */
    if (prefs_get_string_value("coverart", &buf))
    {
	prefs_set_string("coverart_file", buf);
	g_free(buf);
	prefs_set_string("coverart", NULL);
    }
  
    /* rename tm_sort_ to tm_sort */
    if (prefs_get_string_value("tm_sort_", &buf))
    {
	prefs_set_string("tm_sort", buf);
	g_free(buf);
	prefs_set_string("tm_sort_", NULL);
    }

    /* rename md5 to sha1 */
    if (prefs_get_string_value("md5", &buf))
    {
	prefs_set_string("sha1", buf);
	g_free(buf);
	prefs_set_string("md5", NULL);
    }

    /* Convert old path numbered keys to named ones */
  
    /* Play Now */
    if (prefs_get_string_value_index("path", PATH_PLAY_NOW, &buf))
    {
	prefs_set_string("path_play_now", buf);
	prefs_set_string_index("path", PATH_PLAY_NOW, NULL);
	if (version < 0.87)
	{  /* default changed from "xmms -p %s" to "xmms
	      %s" which avoids xmms from hanging --
	      thanks to Chris Vine */
	    if (strcmp (buf, "xmms -p %s") == 0)
	    {
		prefs_set_string ("path_play_now", "xmms %s");
	    }
	}
	g_free(buf);
    }
  
    if (prefs_get_string_value_index("toolpath", PATH_PLAY_NOW, &buf))
    {
	prefs_set_string("path_play_now", buf);
	g_free(buf);
	prefs_set_string_index("toolpath", PATH_PLAY_NOW, NULL);
    }
  
    /* Enqueue */
    if (prefs_get_string_value_index("path", PATH_PLAY_ENQUEUE, &buf))
    {
	prefs_set_string("path_play_enqueue", buf);
	g_free(buf);
	prefs_set_string_index("path", PATH_PLAY_ENQUEUE, NULL);
    }
  
    if (prefs_get_string_value_index("toolpath", PATH_PLAY_ENQUEUE, &buf))
    {
	prefs_set_string("path_play_enqueue", buf);
	g_free(buf);
	prefs_set_string_index("toolpath", PATH_PLAY_ENQUEUE, NULL);
    }
  
    /* MP3 Gain */
    if (prefs_get_string_value_index("path", PATH_MP3GAIN, &buf))
    {
	prefs_set_string("path_mp3gain", buf);
	g_free(buf);
	prefs_set_string_index("path", PATH_MP3GAIN, NULL);
    }
  
    if (prefs_get_string_value_index("toolpath", PATH_MP3GAIN, &buf))
    {
	prefs_set_string("path_mp3gain", buf);
	g_free(buf);
	prefs_set_string_index("toolpath", PATH_MP3GAIN, NULL);
    }
  
    /* Sync contacts */
    if (prefs_get_string_value_index("path", PATH_SYNC_CONTACTS, &buf))
    {
	prefs_set_string("itdb_0_path_sync_contacts", buf);
	g_free(buf);
	prefs_set_string_index("path", PATH_SYNC_CONTACTS, NULL);
    }
  
    if (prefs_get_string_value_index("toolpath", PATH_SYNC_CONTACTS, &buf))
    {
	prefs_set_string("itdb_0_path_sync_contacts", buf);
	g_free(buf);
	prefs_set_string_index("toolpath", PATH_SYNC_CONTACTS, NULL);
    }
  
    /* Sync calendar */
    if (prefs_get_string_value_index("path", PATH_SYNC_CALENDAR, &buf))
    {
	prefs_set_string("itdb_0_path_sync_calendar", buf);
	g_free(buf);
	prefs_set_string_index("path", PATH_SYNC_CALENDAR, NULL);
    }
  
    if (prefs_get_string_value_index("toolpath", PATH_SYNC_CALENDAR, &buf))
    {
	prefs_set_string("itdb_0_path_sync_calendar", buf);
	g_free(buf);
	prefs_set_string_index("toolpath", PATH_SYNC_CALENDAR, NULL);
    }
  
    /* Sync notes */
    if (prefs_get_string_value_index("path", PATH_SYNC_NOTES, &buf))
    {
	prefs_set_string("itdb_0_path_sync_notes", buf);
	g_free(buf);
	prefs_set_string_index("path", PATH_SYNC_NOTES, NULL);
    }
  
    if (prefs_get_string_value_index("toolpath", PATH_SYNC_NOTES, &buf))
    {
	prefs_set_string("itdb_0_path_sync_notes", buf);
	g_free(buf);
	prefs_set_string_index("toolpath", PATH_SYNC_NOTES, NULL);
    }
  
    /* MSERV music root */
    if (prefs_get_string_value_index("path", PATH_MSERV_MUSIC_ROOT, &buf))
    {
	prefs_set_string("path_mserv_music_root", buf);
	g_free(buf);
	prefs_set_string_index("path", PATH_MSERV_MUSIC_ROOT, NULL);
    }
  
    if (prefs_get_string_value_index("toolpath", PATH_MSERV_MUSIC_ROOT, &buf))
    {
	prefs_set_string("path_mserv_music_root", buf);
	g_free(buf);
	prefs_set_string_index("toolpath", PATH_MSERV_MUSIC_ROOT, NULL);
    }
  
    /* MSERV track info root */
    if (prefs_get_string_value_index("path", PATH_MSERV_TRACKINFO_ROOT, &buf))
    {
	prefs_set_string("path_mserv_trackinfo_root", buf);
	g_free(buf);
	prefs_set_string_index("path", PATH_MSERV_TRACKINFO_ROOT, NULL);
    }
  
    if (prefs_get_string_value_index("toolpath", PATH_MSERV_TRACKINFO_ROOT, &buf))
    {
	prefs_set_string("path_mserv_trackinfo_root", buf);
	g_free(buf);
	prefs_set_string_index("toolpath", PATH_MSERV_TRACKINFO_ROOT, NULL);
    }

    /* If there's an extra (PATH_NUM) key, delete it */
    prefs_set_string_index("path", PATH_NUM, NULL);
    prefs_set_string_index("toolpath", PATH_NUM, NULL);

    /* Ignore/remove some keys */
    prefs_set_string("play_now_path", NULL);
    prefs_set_string("sync_remove", NULL);
    prefs_set_string("sync_remove_confirm", NULL);
    prefs_set_string("show_sync_dirs", NULL);
    prefs_set_string("play_enqueue_path", NULL);
    prefs_set_string("mp3gain_path", NULL);
    prefs_set_string("statusbar_timeout", NULL);
    prefs_set_string("offline", NULL);
    prefs_set_string("time_format", NULL);
    prefs_set_string("id3_all", NULL);
    prefs_set_string("pm_autostore", NULL);
    prefs_set_string("backups", NULL);
    prefs_set_string("save_sorted_order", NULL);
    prefs_set_string("fix_path", NULL);
    prefs_set_string("write_gaintag", NULL);
    prefs_set_string("automount", NULL);
    prefs_set_string("display_artcovers", NULL);
    prefs_set_string("block_display", NULL);
    prefs_set_string("tmp_disable_sort", NULL);
    prefs_set_string("size_file_dialog.x", NULL);
    prefs_set_string("size_file_dialog.y", NULL);
    prefs_set_string("size_file_dialog_details.x", NULL);
    prefs_set_string("size_file_dialog_details.y", NULL);
    prefs_set_string("autoimport", NULL);
    prefs_set_string("auto_import", NULL);
    prefs_set_string("conversion_enable", NULL);
    prefs_set_string("export_template", NULL);
    prefs_set_string("filename_format", NULL);

    /* sp_created_cond renamed to sp_added_cond */
    for (i = 0; i < SORT_TAB_MAX; i++)
    {
	if (prefs_get_int_value_index("sp_created_cond", i, &int_buf))
	{
	    prefs_set_int_index("sp_added_cond", i, int_buf);
	    prefs_set_string("sp_created_cond", NULL);
	}
    }
  
    /* sp_created_state renamed to sp_added_state */
    for (i = 0; i < SORT_TAB_MAX; i++)
    {
	if (prefs_get_int_value_index("sp_created_state", i, &int_buf))
	{
	    prefs_set_int_index("sp_added_state", i, int_buf);
	    prefs_set_string("sp_created_state", NULL);
	}
    }
  
    /* sm_col_width renamed to tm_col_width */
    for (i = 0; i < TM_NUM_COLUMNS; i++)
    {
	if (prefs_get_int_value_index("sm_col_width", i, &int_buf))
	{
	    prefs_set_int_index("tm_col_width", i, int_buf);
	    prefs_set_string_index("sm_col_width", i, NULL);
	}
    }
  
    /* handle version changes in prefs */
    if (version == 0.0)
    {
	/* most likely prefs file written by V0.50 */
	/* I added two new PANED elements since V0.50 --> shift */
	for (i=PANED_NUM_ST-1; i>=0; --i)
	{
	    prefs_set_int_index("paned_pos_", PANED_NUM_GLADE + i,
				prefs_get_int_index("paned_pos_", PANED_NUM_GLADE + i - 2));
	}
	prefs_set_int_index("paned_pos_", PANED_STATUS1, -1);
	prefs_set_int_index("paned_pos_", PANED_STATUS2, -1);
    }

    /* set statusbar paned to a decent value if unset */
    if (prefs_get_int_index("paned_pos_", PANED_STATUS1) == -1)
    {
	x = prefs_get_int("size_gtkpod.x");
	/* set to about 2/3 of the window width */
	if (x>0)   
	    prefs_set_int_index("paned_pos_", PANED_STATUS1, 20*x/30);
    }
  
    if (prefs_get_int_index("paned_pos_", PANED_STATUS2) == -1)
    {
	x = prefs_get_int("size_gtkpod.x");
	y = prefs_get_int("size_gtkpod.y");
	p = prefs_get_int_index("paned_pos_", PANED_STATUS1);
	/* set to about half of the remaining window */
	if (x>0)   
	    prefs_set_int_index("paned_pos_", PANED_STATUS2, (x-p)/2 );
    }

    /* Changed layout of info window between 0.72 and 0.73 */
    if (version < 0.73)
    {
	prefs_set_string("size_info.x", NULL);
	prefs_set_string("size_info.y", NULL);
    }
    
    /* not_played_song renamed to not_played_track */
    if (prefs_get_int_value("not_played_song", &int_buf))
    {
	prefs_set_int("not_played_track", int_buf);
	prefs_set_string("not_played_song", NULL);
    }

    /* misc_song_nr renamed to misc_track_nr */
    if (prefs_get_int_value("misc_song_nr", &int_buf))
    {
	prefs_set_int("misc_track_nr", int_buf);
	prefs_set_string("misc_song_nr", NULL);
    }

    /* sm_autostore renamed to tm_autostore */
    if (prefs_get_int_value("sm_autostore", &int_buf))
    {
	prefs_set_int("tm_autostore", int_buf);
	prefs_set_string("sm_autostore", NULL);
    }

    /* sm_sortcol renamed to tm_sortcol */
    if (prefs_get_int_value("sm_sortcol", &int_buf))
    {
	prefs_set_int("tm_sortcol", int_buf);
	prefs_set_string("sm_sortcol", NULL);
    }

    /* sm_sort_ renamed to tm_sort */
    if (prefs_get_int_value("sm_sort_", &int_buf))
    {
	prefs_set_int("tm_sort", int_buf);
	prefs_set_string("sm_sort_", NULL);
    }

    /* For versions < 0.91, remove all itdb keys */
    if (version < 0.91)
	prefs_flush_subkey("itdb_");

    /* rename convert scripts */
    if (version <= 0.99001)
    {

	const gchar **keyp;
	const gchar *keys[] =
	    {"path_conv_m4a", "path_conv_wav", "path_conv_mp3",
	     "path_conv_ogg", "path_conv_flac", NULL};

	for (keyp=keys; *keyp; ++keyp)
	{
	    buf = prefs_get_string (*keyp);
	    if (buf)
	    {
		const gchar *needles[] =
		    { "convert-flac2mp3.sh", "convert-m4a2mp3.sh",
		      "convert-ogg2mp3.sh", "convert-wav2mp3.sh",
		      "convert-flac2m4a.sh", "convert-m4a2m4a.sh",
		      "convert-ogg2m4a.sh", "convert-wav2m4a.sh", NULL };
		const gchar *replacements[] =
		    { "convert-2mp3.sh    ", "convert-2mp3.sh   ",
		      "convert-2mp3.sh   ", "convert-2mp3.sh   " ,
		      "convert-2m4a.sh    ", "convert-2m4a.sh   ",
		      "convert-2m4a.sh   ", "convert-2m4a.sh   " };
		const gchar **needlep;
		const gchar **replp;
		replp = replacements;
		for (needlep=needles; *needlep; ++needlep, ++replp)
		{
		    gchar *bufp = strstr (buf, *needlep);
		    if (bufp)
		    {
			g_memmove (bufp, *replp, strlen (*replp));
			prefs_set_string (*keyp, buf);
			break;
		    }
		}
		g_free (buf);
	    }
	}
    }

    prefs_set_string ("version", VERSION);
}

/* Initialize the prefs table and read configuration */
void prefs_init (int argc, char *argv[])
{
    if (!prefs_table_mutex)
	prefs_table_mutex = g_mutex_new ();

    lock_prefs_table ();

    /* Create the prefs hash table */
    prefs_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
					g_free);

    unlock_prefs_table ();

    /* Load preferences */
    load_prefs();

    /* Clean up old prefs keys */
    cleanup_keys();
	
    /* Read environment variables */
    read_environment(); 
	
    /* Read commandline arguments */
    read_commandline(argc, argv);  
}

/* Delete the hash table */
void prefs_shutdown ()
{
    lock_prefs_table ();

    if (!prefs_table)
    {
	unlock_prefs_table ();
	g_return_if_reached ();
    }

    /* Delete the prefs hash table */
    g_hash_table_destroy(prefs_table);
    prefs_table = NULL;

    unlock_prefs_table ();

    /* We can't free the prefs_table_mutex in a thread-safe way */
}

/* Create the temp prefs tree */
/* Free the returned structure with delete_temp_prefs() */
TempPrefs *temp_prefs_create ()
{
    TempPrefs *temp_prefs;  /* Returned temp prefs structure */

    temp_prefs = (TempPrefs*)g_malloc(sizeof(TempPrefs));

    temp_prefs->tree = g_tree_new_full((GCompareDataFunc)strcmp, NULL,
				       g_free, g_free);

    return temp_prefs;	
}

static void copy_key_to_temp_prefs (gpointer key, gpointer value, gpointer user_data)
{
    temp_prefs_set_string ((TempPrefs *)user_data, key, value);
}

void temp_prefs_copy_prefs (TempPrefs *temp_prefs)
{
    g_return_if_fail (prefs_table);
    g_return_if_fail (temp_prefs);
    g_return_if_fail (temp_prefs->tree);

	lock_prefs_table ();
    g_hash_table_foreach (prefs_table, copy_key_to_temp_prefs, temp_prefs);
	unlock_prefs_table ();
}

/* Delete temp prefs */
void temp_prefs_destroy (TempPrefs *temp_prefs)
{
    g_return_if_fail (temp_prefs);
    g_return_if_fail (temp_prefs->tree);

    g_tree_destroy(temp_prefs->tree);
    g_free(temp_prefs);
}

/* Copy key data from the temp prefs tree to the hash table */
static gboolean copy_key (gpointer key, gpointer value, gpointer user_data)
{
    prefs_set_string(key, value);
	
    return FALSE;
}

/* Copy the data from the temp prefs tree to the permanent prefs table */
void temp_prefs_apply (TempPrefs *temp_prefs)
{
    g_return_if_fail (temp_prefs);
    g_return_if_fail (temp_prefs->tree);

    g_tree_foreach (temp_prefs->tree, copy_key, NULL);
}


/* Create a temp_prefs tree containing a subset of keys in the
   permanent prefs table (those starting with @subkey */
static TempPrefs *prefs_create_subset_unlocked (const gchar *subkey)
{
    struct sub_data sub_data;

    g_return_val_if_fail (prefs_table, NULL);

    sub_data.temp_prefs = temp_prefs_create ();
    sub_data.subkey = subkey;

    g_hash_table_foreach (prefs_table, (GHFunc)get_subset, &sub_data);

    return sub_data.temp_prefs;
}

/* Create a temp_prefs tree containing a subset of keys in the
   permanent prefs table (those starting with @subkey */
TempPrefs *prefs_create_subset (const gchar *subkey)
{
    TempPrefs *temp_prefs;

    lock_prefs_table ();

    temp_prefs = prefs_create_subset_unlocked (subkey);

    unlock_prefs_table ();

    return temp_prefs;
}


/* Create a temp_prefs tree containing a subset of keys in the
   permanent prefs table (those starting with @subkey */
TempPrefs *temp_prefs_create_subset (TempPrefs *temp_prefs,
				     const gchar *subkey)
{
    struct sub_data sub_data;

    g_return_val_if_fail (temp_prefs, NULL);
    g_return_val_if_fail (temp_prefs->tree, NULL);

    sub_data.temp_prefs = temp_prefs_create ();
    sub_data.subkey = subkey;

    g_tree_foreach (temp_prefs->tree, get_subset, &sub_data);

    return sub_data.temp_prefs;
}


/* Remove all keys in the temp prefs tree from the permanent prefs
   table */
void temp_prefs_flush (TempPrefs *temp_prefs)
{
    g_return_if_fail (temp_prefs);
    g_return_if_fail (temp_prefs->tree);

    lock_prefs_table ();

    g_tree_foreach (temp_prefs->tree, flush_key, NULL);

    unlock_prefs_table ();
}

/* Return the number of keys stored in @temp_prefs */
gint temp_prefs_size (TempPrefs *temp_prefs)
{
    g_return_val_if_fail (temp_prefs, 0);
    g_return_val_if_fail (temp_prefs->tree, 0);

    return g_tree_nnodes (temp_prefs->tree);
}


/* Returns TRUE if at least one key starting with @subkey exists */
gboolean temp_prefs_subkey_exists (TempPrefs *temp_prefs,
				   const gchar *subkey)
{
    struct sub_data sub_data;

    g_return_val_if_fail (temp_prefs && subkey, FALSE);

    sub_data.temp_prefs = NULL;
    sub_data.subkey = subkey;
    sub_data.exists = FALSE;

    g_tree_foreach (temp_prefs->tree, check_subkey, &sub_data);

    return sub_data.exists;
}


/* Special functions */

/* Remove all keys that start with @subkey */
void prefs_flush_subkey (const gchar *subkey)
{
    lock_prefs_table ();

    if (!prefs_table)
    {
	unlock_prefs_table ();
	g_return_if_reached ();
    }

    g_hash_table_foreach_remove (prefs_table, match_subkey, (gchar *)subkey);

    unlock_prefs_table ();
}


/* Rename all keys that start with @subkey_old in such a way that they
   start with @subkey_new */
void prefs_rename_subkey (const gchar *subkey_old, const gchar *subkey_new){
    struct sub_data sub_data;

    g_return_if_fail (subkey_old);
    g_return_if_fail (subkey_new);

    lock_prefs_table ();

    if (!prefs_table)
    {
	unlock_prefs_table ();
	g_return_if_reached ();
    }

    sub_data.temp_prefs = prefs_create_subset_unlocked (subkey_old);
    sub_data.temp_prefs_orig = NULL;

    if (temp_prefs_size (sub_data.temp_prefs) > 0)
    {
	sub_data.subkey = subkey_old;
	sub_data.subkey2 = subkey_new;
	g_tree_foreach (sub_data.temp_prefs->tree, subst_key, &sub_data);
    }

    temp_prefs_destroy (sub_data.temp_prefs);

    unlock_prefs_table ();
}


/* Rename all keys that start with @subkey_old in such a way that they
   start with @subkey_new */
void temp_prefs_rename_subkey (TempPrefs *temp_prefs,
			       const gchar *subkey_old,
			       const gchar *subkey_new)
{
    struct sub_data sub_data;

    g_return_if_fail (temp_prefs);
    g_return_if_fail (subkey_old);
    g_return_if_fail (subkey_new);

    sub_data.temp_prefs_orig = temp_prefs;
    sub_data.temp_prefs = temp_prefs_create_subset (temp_prefs,
						    subkey_old);

    if (temp_prefs_size (sub_data.temp_prefs) > 0)
    {
	sub_data.subkey = subkey_old;
	sub_data.subkey2 = subkey_new;
	g_tree_foreach (sub_data.temp_prefs->tree, subst_key, &sub_data);
    }

    temp_prefs_destroy (sub_data.temp_prefs);
}

/* Functions for non-numbered pref keys */

/* Set a string value with the given key, or remove key if @value is
   NULL */
void prefs_set_string(const gchar *key, const gchar *value)
{
    g_return_if_fail (key);

    lock_prefs_table ();

    if (!prefs_table)
    {
	unlock_prefs_table ();
	g_return_if_reached ();
    }

    if (value)
	g_hash_table_insert (prefs_table,
			     g_strdup(key), g_strdup(value));
    else
	g_hash_table_remove (prefs_table, key);

    unlock_prefs_table ();
}

/* Set a key value to a given integer */
void prefs_set_int(const gchar *key, const gint value)
{
    gchar *strvalue; /* String value converted from integer */

    lock_prefs_table ();

    if (!prefs_table)
    {
	unlock_prefs_table ();
	g_return_if_reached ();
    }

    strvalue = g_strdup_printf("%i", value);
    g_hash_table_insert(prefs_table, g_strdup(key), strvalue);

    unlock_prefs_table ();
}

/* Set a key to an int64 value */
void prefs_set_int64(const gchar *key, const gint64 value)
{
    gchar *strvalue; /* String value converted from int64 */
	
    lock_prefs_table ();

    if (!prefs_table)
    {
	unlock_prefs_table ();
	g_return_if_reached ();
    }

    strvalue = g_strdup_printf("%" G_GINT64_FORMAT, value);
    g_hash_table_insert(prefs_table, g_strdup(key), strvalue);	

    unlock_prefs_table ();
}

void prefs_set_double(const gchar *key, gdouble value)
{
    gchar *strvalue; /* String value converted from integer */

    lock_prefs_table ();

    if (!prefs_table)
    {
	unlock_prefs_table ();
	g_return_if_reached ();
    }

    strvalue = g_strdup_printf("%f", value);
    g_hash_table_insert(prefs_table, g_strdup(key), strvalue);

    unlock_prefs_table ();
}

/* Get a string value associated with a key. Free returned string. */
gchar *prefs_get_string(const gchar *key)
{
    gchar *string = NULL;

    lock_prefs_table ();

    g_return_val_if_fail (prefs_table, (unlock_prefs_table(), NULL));

    string = g_strdup(g_hash_table_lookup(prefs_table, key));

    unlock_prefs_table ();

    return string;
}

/* Use this if you need to know if the given key actually exists */
/* The value parameter can be NULL if you don't need the value itself. */
gboolean prefs_get_string_value(const gchar *key, gchar **value)
{
    const gchar *string;  /* String value from prefs table */
    gboolean valid = FALSE;

    lock_prefs_table ();

    g_return_val_if_fail (prefs_table, (unlock_prefs_table(), FALSE));

    string = g_hash_table_lookup(prefs_table, key);
		
    if (value)
	*value = g_strdup (string);
    if (string)
	valid = TRUE;

    unlock_prefs_table ();

    return valid;
}

/* Get an integer value from a key */
gint prefs_get_int(const gchar *key)
{
    gchar *string; /* Hash value string */
    gint value;  /* Returned value */
	
    value = 0;
	
    lock_prefs_table ();

    g_return_val_if_fail (prefs_table, (unlock_prefs_table(), value));

    string = g_hash_table_lookup(prefs_table, key);
		
    if (string)
	value = atoi(string);

    unlock_prefs_table ();

    return value;
}

/* Use this if you need to know if the given key actually exists */
/* The value parameter can be NULL if you don't need the value itself. */
gboolean prefs_get_int_value(const gchar *key, gint *value)
{
    gchar *string;  /* String value from prefs table */
    gboolean valid = FALSE;

    lock_prefs_table ();

    g_return_val_if_fail (prefs_table, (unlock_prefs_table(), FALSE));

    string = g_hash_table_lookup(prefs_table, key);

    if (value)
    {
	if (string)
	    *value = atoi(string);
	else
	    *value = 0;
    }

    if (string)
	valid = TRUE;

    unlock_prefs_table ();

    return valid;
}

/* Get a 64 bit integer value from a key */
gint64 prefs_get_int64(const gchar *key)
{
    gchar *string;  /* Key value string */
    gint64 value;  /* Returned value */
	
    value = 0;

    lock_prefs_table ();

    g_return_val_if_fail (prefs_table, (unlock_prefs_table(), value));

    string = g_hash_table_lookup(prefs_table, key);

    if (string)
	value = g_ascii_strtoull(string, NULL, 10);

    unlock_prefs_table ();
	
    return value;
}

/* Get a 64 bit integer value from a key */
/* Use this if you need to know if the given key actually exists */
/* The value parameter can be NULL if you don't need the value itself. */
gboolean prefs_get_int64_value(const gchar *key, gint64 *value)
{
    gchar *string;  /* String value from prefs table */
    gboolean valid = FALSE;

    lock_prefs_table ();

    g_return_val_if_fail (prefs_table, (unlock_prefs_table(), FALSE));

    string = g_hash_table_lookup(prefs_table, key);
		
    if (value)
    {
	if (string)
	    *value = g_ascii_strtoull(string, NULL, 10);
	else
	    *value = 0;
    }

    if (string)
	valid = TRUE;

    unlock_prefs_table ();

    return valid;
}

gdouble prefs_get_double(const gchar *key)
{
    gchar *string;  /* Key value string */
    gdouble value;  /* Returned value */
	
    value = 0;

    lock_prefs_table ();

    g_return_val_if_fail (prefs_table, (unlock_prefs_table(), value));

    string = g_hash_table_lookup(prefs_table, key);

    if (string)
	value = g_ascii_strtod(string, NULL);

    unlock_prefs_table ();
	
    return value;
}

gboolean prefs_get_double_value(const gchar *key, gdouble *value)
{
    gchar *string;  /* String value from prefs table */
    gboolean valid = FALSE;

    lock_prefs_table ();

    g_return_val_if_fail (prefs_table, (unlock_prefs_table(), FALSE));

    string = g_hash_table_lookup(prefs_table, key);

    if (value)
    {
	if (string)
	    *value = g_ascii_strtod(string, NULL);
	else
	    *value = 0;
    }

    if (string)
	valid = TRUE;

    unlock_prefs_table ();

    return valid;
}

/* Functions for numbered pref keys */

/* Set a string value with the given key */
void prefs_set_string_index(const gchar *key, const guint index, 
			    const gchar *value)
{
    gchar *full_key; /* Complete numbered key */
	
    full_key = create_full_key(key, index);
    prefs_set_string(full_key, value);
	
    g_free(full_key);
}

/* Set a key value to a given integer */
void prefs_set_int_index(const gchar *key, const guint index, 
			 const gint value)
{
    gchar *full_key; /* Complete numbered key */
	
    full_key = create_full_key(key, index);
    prefs_set_int(full_key, value);
	
    g_free(full_key);
}

/* Set a key to an int64 value */
void prefs_set_int64_index(const gchar *key, const guint index, 
			   const gint64 value)
{
    gchar *full_key; /* Complete numbered key */
	
    full_key = create_full_key(key, index);
    prefs_set_int64(full_key, value);
	
    g_free(full_key);
}

/* Set a key to a gdouble value */
void prefs_set_double_index(const gchar *key, guint index,
			     gdouble value)
{
    gchar *full_key; /* Complete numbered key */
	
    full_key = create_full_key(key, index);
    prefs_set_double(full_key, value);
	
    g_free(full_key);
}

/* Get a string value associated with a key. Free returned string. */
gchar *prefs_get_string_index(const gchar *key, const guint index)
{	
    gchar *full_key; /* Complete numbered key */
    gchar *string;  /* Return string */
	
    full_key = create_full_key(key, index);
    string = prefs_get_string(full_key);
	
    g_free(full_key);
    return string;
}

/* Get a string value associated with a key. Free returned string. */
/* Use this if you need to know if the given key actually exists */
gboolean prefs_get_string_value_index(const gchar *key, const guint index, 
				      gchar **value)
{
    gchar *full_key; /* Complete numbered key */
    gboolean ret; /* Return value */
	
    full_key = create_full_key(key, index);
    ret = prefs_get_string_value(full_key, value);
	
    g_free(full_key);
    return ret;
}

/* Get an integer value from a key */
gint prefs_get_int_index(const gchar *key, const guint index)
{
    gchar *full_key; /* Complete numbered key */
    gint value;  /* Returned integer value */

    full_key = create_full_key(key, index);
    value = prefs_get_int(full_key);
	
    g_free(full_key);
    return value;
}

/* Get an integer value from a key */
/* Use this if you need to know if the given key actually exists */
gboolean prefs_get_int_value_index(const gchar *key, const guint index,  
				   gint *value)
{
    gchar *full_key; /* Complete numbered key */
    gboolean ret; /* Return value */
	
    full_key = create_full_key(key, index);
    ret = prefs_get_int_value(full_key, value);
	
    g_free(full_key);
    return ret;
}

/* Get a 64 bit integer value from a key */
gint64 prefs_get_int64_index(const gchar *key, const guint index)
{
    gchar *full_key; /* Complete numbered key */
    gint64 value; /* Return value */
	
    full_key = create_full_key(key, index);
    value = prefs_get_int64(full_key);
	
    g_free(full_key);
    return value;
}

/* Get a 64 bit integer value from a key */
/* Use this if you need to know if the given key actually exists */
gboolean prefs_get_int64_value_index(const gchar *key, const guint index, 
				     gint64 *value)
{
    gchar *full_key; /* Complete numbered key */
    gboolean ret; /* Return value */
	
    full_key = create_full_key(key, index);
    ret = prefs_get_int64_value(full_key, value);
	
    g_free(full_key);
    return ret;
}

gdouble prefs_get_double_index(const gchar *key, guint index)
{
    gchar *full_key; /* Complete numbered key */
    gdouble value; /* Return value */
	
    full_key = create_full_key(key, index);
    value = prefs_get_double(full_key);
	
    g_free(full_key);
    return value;
}

gboolean prefs_get_double_value_index(const gchar *key, guint index,
				      gdouble *value)
{
    gchar *full_key; /* Complete numbered key */
    gboolean ret; /* Return value */
	
    full_key = create_full_key(key, index);
    ret = prefs_get_double_value(full_key, value);
	
    g_free(full_key);
    return ret;
}

/* Add string value with the given key to temp prefs. Note: use
 * temp_prefs_remove_key() to remove key from the temp prefs. Setting
 * it to NULL will not remove the key. It will instead remove the key
 * in the main prefs table when you call temp_prefs_apply(). */
void temp_prefs_set_string (TempPrefs *temp_prefs, const gchar *key, 
			   const gchar *value)
{
    g_return_if_fail (temp_prefs && temp_prefs->tree);
    g_return_if_fail (key);

    g_tree_insert (temp_prefs->tree, g_strdup(key), g_strdup(value));
}

/* Add string value with the given key to temp prefs. Remove the key
 * if @value is NULL. */
void temp_prefs_remove_key (TempPrefs *temp_prefs, const gchar *key)
{
    g_return_if_fail (temp_prefs && temp_prefs->tree);
    g_return_if_fail (key);

    g_tree_remove (temp_prefs->tree, key);
}

/* Add an integer value to temp prefs */
void temp_prefs_set_int(TempPrefs *temp_prefs, const gchar *key, 
			const gint value)
{
    gchar *strvalue; /* String value converted from integer */

    g_return_if_fail (temp_prefs && temp_prefs->tree);
    g_return_if_fail (key);

    strvalue = g_strdup_printf("%i", value);
    g_tree_insert(temp_prefs->tree, g_strdup(key), strvalue);
}

/* Add an int64 to temp prefs */
void temp_prefs_set_int64(TempPrefs *temp_prefs, const gchar *key, 
			  const gint64 value)
{
    gchar *strvalue; /* String value converted from int64 */
	
    g_return_if_fail (temp_prefs && temp_prefs->tree);
    g_return_if_fail (key);

    strvalue = g_strdup_printf("%" G_GINT64_FORMAT, value);
    g_tree_insert(temp_prefs->tree, g_strdup(key), strvalue);
}

void temp_prefs_set_double(TempPrefs *temp_prefs, const gchar *key,
			   gdouble value)
{
    gchar *strvalue; /* String value converted from int64 */
	
    g_return_if_fail (temp_prefs && temp_prefs->tree);
    g_return_if_fail (key);

    strvalue = g_strdup_printf("%fu", value);
    g_tree_insert(temp_prefs->tree, g_strdup(key), strvalue);
}

/* Get a string value associated with a key. Free returned string. */
gchar *temp_prefs_get_string(TempPrefs *temp_prefs, const gchar *key)
{	
    g_return_val_if_fail (temp_prefs && temp_prefs->tree, NULL);
    g_return_val_if_fail (key, NULL);

    return g_strdup (g_tree_lookup (temp_prefs->tree, key));
}

/* Use this if you need to know if the given key actually exists */
/* The value parameter can be NULL if you don't need the value itself. */
gboolean temp_prefs_get_string_value(TempPrefs *temp_prefs,
				     const gchar *key, gchar **value)
{
    gchar *string;  /* String value from prefs table */
	
    g_return_val_if_fail (temp_prefs && temp_prefs->tree, FALSE);
    g_return_val_if_fail (key, FALSE);

    string = g_tree_lookup (temp_prefs->tree, key);

    if (value)
	*value = g_strdup (string);

    if (string)
	return TRUE;
    else
	return FALSE;
}

/* Get an integer value from a key */
gint temp_prefs_get_int(TempPrefs *temp_prefs, const gchar *key)
{
    gchar *string; /* Hash value string */
    gint value;  /* Returned value */
	
    g_return_val_if_fail (temp_prefs && temp_prefs->tree, 0);
    g_return_val_if_fail (key, 0);

    value = 0;
	
    string = g_tree_lookup (temp_prefs->tree, key);
		
    if (string)
	value = atoi(string);

    return value;
}

/* Use this if you need to know if the given key actually exists */
/* The value parameter can be NULL if you don't need the value itself. */
gboolean temp_prefs_get_int_value(TempPrefs *temp_prefs,
				  const gchar *key, gint *value)
{
    gchar *string;  /* String value from prefs table */
	
    g_return_val_if_fail (temp_prefs && temp_prefs->tree, FALSE);
    g_return_val_if_fail (key, FALSE);

    string = g_tree_lookup (temp_prefs->tree, key);

    if (value)
    {
	if (string)
	    *value = atoi(string);
	else
	    *value = 0;
    }

    if (string)
	return TRUE;
    else
	return FALSE;
}

gdouble temp_prefs_get_double(TempPrefs *temp_prefs,
			      const gchar *key)
{
    gchar *string; /* Hash value string */
    gdouble value;  /* Returned value */
	
    g_return_val_if_fail (temp_prefs && temp_prefs->tree, 0);
    g_return_val_if_fail (key, 0);

    value = 0.0f;
	
    string = g_tree_lookup (temp_prefs->tree, key);
		
    if (string)
	value = g_ascii_strtod(string, NULL);

    return value;    
}

gboolean temp_prefs_get_double_value(TempPrefs *temp_prefs,
				    const gchar *key, gdouble *value)
{
    gchar *string;  /* String value from prefs table */
	
    g_return_val_if_fail (temp_prefs && temp_prefs->tree, FALSE);
    g_return_val_if_fail (key, FALSE);

    string = g_tree_lookup (temp_prefs->tree, key);

    if (value)
    {
	if (string)
	    *value = g_ascii_strtod(string, NULL);
	else
	    *value = 0;
    }

    if (string)
	return TRUE;
    else
	return FALSE;
}

/* Functions for numbered pref keys */

/* Set a string value with the given key */
void temp_prefs_set_string_index(TempPrefs *temp_prefs, const gchar *key,
				 const guint index, const gchar *value)
{
    gchar *full_key; /* Complete numbered key */
	
    g_return_if_fail (temp_prefs && temp_prefs->tree);
    g_return_if_fail (key);

    full_key = create_full_key(key, index);
    temp_prefs_set_string(temp_prefs, full_key, value);
	
    g_free(full_key);
}

/* Set a key value to a given integer */
void temp_prefs_set_int_index(TempPrefs *temp_prefs, const gchar *key,
			      const guint index, const gint value)
{
    gchar *full_key; /* Complete numbered key */

    g_return_if_fail (temp_prefs && temp_prefs->tree);
    g_return_if_fail (key);

    full_key = create_full_key(key, index);
    temp_prefs_set_int(temp_prefs, full_key, value);

    g_free(full_key);
}

/* Set a key to an int64 value */
void temp_prefs_set_int64_index(TempPrefs *temp_prefs, const gchar *key,  
				const guint index, const gint64 value)
{
    gchar *full_key; /* Complete numbered key */

    g_return_if_fail (temp_prefs && temp_prefs->tree);
    g_return_if_fail (key);

    full_key = create_full_key(key, index);
    temp_prefs_set_int64(temp_prefs, full_key, value);

    g_free(full_key);
}

/* Functions for variable-length lists */

/* Create a tree that contains lists that need to be rebuilt */
/* Free the returned structure with destroy_temp_lists */
TempLists *temp_lists_create()
{
    TempLists *temp_lists;  /* Allocated temp list structure */
	
    temp_lists = (TempLists*)g_malloc(sizeof(TempLists));
	

    temp_lists->tree = g_tree_new_full((GCompareDataFunc)strcmp, NULL,
				       g_free,
				       (GDestroyNotify)prefs_free_list);
    return temp_lists;
}

/* Destroys the list tree */
void temp_lists_destroy(TempLists *temp_lists)
{
    if (temp_lists)
    {
	if (temp_lists->tree)
	    g_tree_destroy(temp_lists->tree);
		
	g_free(temp_lists);
    }
}

/* Add a list with the given key prefix to a temp list tree */
void temp_list_add(TempLists *temp_lists, const gchar *key, GList *list)
{
    if (temp_lists)
    {
	if (temp_lists->tree)
	    g_tree_insert(temp_lists->tree, g_strdup(key), list);
    }
}
		
/* Copy the items of the lists in the given tree to the prefs table */
void temp_lists_apply(TempLists *temp_lists)
{
    if (temp_lists)
    {
	if (temp_lists->tree)
	    g_tree_foreach(temp_lists->tree, copy_list, NULL);
    }
}

/* Copy one list to the prefs table. Useful for lists not changed by a window */
void prefs_apply_list(gchar *key, GList *list)
{
    GList *node;  /* Current list node */
    guint i;  /* Counter */
	
    i = 0;
	
    if (prefs_table)
    {
	/* Clean the existing list */
	wipe_list(key);
		
	node = list;
		
	/* Add the new list items to the table */
	while (node)
	{
	    g_hash_table_insert(prefs_table, create_full_key(key, i), 
				g_strdup(node->data));
				
	    node = g_list_next(node);
	    i++;
	}
		
	/* Add the end marker */
	g_hash_table_insert(prefs_table, create_full_key(key, i),
			    g_strdup(LIST_END_MARKER));
    }
}

/* Get the items in a variable-length list from the prefs table */
GList *prefs_get_list(const gchar *key)
{
    guint end_marker_hash;  /* Hash value of the list end marker */
    guint item_hash;  /* Hash value of current list string */
    gchar *item_string;  /* List item string */
    guint i;  /* Counter */
    GList *list;  /* List that contains items */
	
    /* Go through each key in the table until we find the end marker */
    end_marker_hash = g_str_hash(LIST_END_MARKER);
    list = NULL;
	
    for (i = 0;;i++)
    {
	item_string = prefs_get_string_index(key, i);
		
	if (item_string)
	{
	    item_hash = g_str_hash(item_string);
			
	    if (item_hash != end_marker_hash)
	    {
		list = g_list_append(list, item_string);
		continue;
	    }
	    else
	    {
		g_free(item_string);
		break;
	    }
	}
    }
	
    return list;
}

/* Free a list and its strings */
void prefs_free_list(GList *list)
{
    GList *node;  /* Current list node */
	
    node = list;
	
    /* Go through the list, freeing the strings */
	
    while (node)
    {
	if (node->data)
	    g_free(node->data);
		
	node = g_list_next(node);
    }
	
    g_list_free(list);
}

/* Creates a list from lines in a GtkTextBuffer. Free the list when done. */
GList *get_list_from_buffer(GtkTextBuffer *buffer)
{
    GtkTextIter start_iter; /* Start of buffer text */
    GtkTextIter end_iter; /* End of buffer text */
    gchar *text_buffer; /* Raw text buffer */
    gchar **string_array; /* Contains each line of the buffer */
    gchar **string_iter;  /* Pointer for iterating through the string vector */
    GList *list; /* List that contains each string */
	
    list = NULL;
	
    /* Grab the text from the buffer, and then split it up by lines */
    gtk_text_buffer_get_start_iter(buffer, &start_iter);
    gtk_text_buffer_get_end_iter(buffer, &end_iter);
	
    text_buffer = gtk_text_buffer_get_text(buffer, &start_iter, &end_iter, FALSE);
    string_array = g_strsplit(text_buffer, "\n", -1);
    string_iter = string_array;
	
    /* Go through each string and put it in the list */
    while (*string_iter)
    {
	if (strlen(*string_iter) != 0)
	    list = g_list_append(list, g_strdup(*string_iter));
		
	string_iter++;
    }

    g_free (text_buffer);
    g_strfreev (string_array);
    
    return list;
}