File: display_tracks.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 (2905 lines) | stat: -rw-r--r-- 79,766 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
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
/*
|  Copyright (C) 2002-2007 Jorg Schuler <jcsjcs at users sourceforge 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: display_tracks.c 2216 2009-01-17 05:47:46Z jcsjcs $
*/

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

#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include "prefs.h"
#include "details.h"
#include "display_private.h"
#include "display_itdb.h"
#include "itdb.h"
#include "info.h"
#include "misc.h"
#include "misc_track.h"
#include "file.h"
#include "context_menus.h"
#include "rb_cell_renderer_rating.h"

/* pointer to the treeview for the track display */
static GtkTreeView *track_treeview = NULL;
/* array with pointers to the columns used in the track display */
static GtkTreeViewColumn *tm_columns[TM_NUM_COLUMNS];
/* column in which track pointer is stored */
static const gint READOUT_COL = 0;

/* compare function to be used for string comparisons */
static gint (*string_compare_func) (const gchar *str1, const gchar *str2) = compare_string;

static GtkTreeViewColumn *tm_add_column (TM_item tm_item, gint position);
static TM_item tm_lookup_col_id (GtkTreeViewColumn *column);

/* Drag and drop definitions */
static GtkTargetEntry tm_drag_types [] = {
    { DND_GTKPOD_TM_PATHLIST_TYPE, 0, DND_GTKPOD_TM_PATHLIST },
    { DND_GTKPOD_TRACKLIST_TYPE, 0, DND_GTKPOD_TRACKLIST },
    { "text/uri-list", 0, DND_TEXT_URI_LIST },
    { "text/plain", 0, DND_TEXT_PLAIN },
    { "STRING", 0, DND_TEXT_PLAIN }
};
static GtkTargetEntry tm_drop_types [] = {
    { DND_GTKPOD_TM_PATHLIST_TYPE, 0, DND_GTKPOD_TM_PATHLIST },
    { "text/uri-list", 0, DND_TEXT_URI_LIST },
    { "text/plain", 0, DND_TEXT_PLAIN },
    { "STRING", 0, DND_TEXT_PLAIN }
};

/* prefs strings */
const gchar *TM_PREFS_SEARCH_COLUMN = "tm_prefs_search_column";
const gchar *KEY_DISPLAY_SEARCH_ENTRY ="display_search_entry";

/* Convenience functions */
static gboolean filter_tracks (GtkTreeModel *model, GtkTreeIter *iter, gpointer entry)
{
    Track *tr;
	gboolean result = FALSE;
	const gchar *text = gtk_entry_get_text (GTK_ENTRY (entry));
	int i;

	gtk_tree_model_get(model, iter, READOUT_COL, &tr, -1);

	if (tr)
	{
	    if (text[0] == 0x0)   return TRUE;
	    for (i = 0; i < TM_NUM_COLUMNS; i++)
	    {
		gint visible = prefs_get_int_index("col_visible", i);
		gchar *data;
		
		if (!visible)
			continue;
		
		data = track_get_text (tr, TM_to_T (i));
		
		if (data && utf8_strcasestr (data, text))
		{
			g_free (data);
			result = TRUE;
			break;
		}

		g_free (data);
	    }
	}

	return result;
}

static GtkListStore *get_model_as_store (GtkTreeModel *model)
{
	if (!GTK_IS_TREE_MODEL_FILTER (model))
		return GTK_LIST_STORE (model);
	else
		return GTK_LIST_STORE (gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model)));
}

static void convert_iter (GtkTreeModel *model, GtkTreeIter *from, GtkTreeIter *to)
{
    if (!GTK_IS_TREE_MODEL_FILTER (model))
        *to = *from;
    else
        gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), to, from);
}

/*static void update_model_view (GtkTreeModel *model)
{
    if (GTK_IS_TREE_MODEL_FILTER (model))
        gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (model));
}*/

static GtkTreeModelFilter *get_filter (GtkTreeView *tree)
{
	GtkTreeModel *model = gtk_tree_view_get_model (tree);
	
	if (GTK_IS_TREE_MODEL_FILTER (model))
		return GTK_TREE_MODEL_FILTER (model);
	else
	{
		GtkTreeModelFilter *filter = GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (model, NULL));
		GtkWidget *search_entry = gtkpod_xml_get_widget (main_window_xml, "search_entry");
		
		gtk_tree_model_filter_set_visible_func (filter, filter_tracks, search_entry, NULL);
		gtk_tree_model_filter_refilter (filter);
		gtk_tree_view_set_model (tree, GTK_TREE_MODEL (filter));
		
		return filter;
	}
}

G_MODULE_EXPORT void on_search_entry_changed (GtkEditable *editable, gpointer user_data)
{
	gtk_tree_model_filter_refilter (get_filter (track_treeview));
}

G_MODULE_EXPORT void on_searchbar_down_button_clicked (GtkWidget *widget, gpointer data)
{
	GtkWidget *upbutton = gtkpod_xml_get_widget (main_window_xml, "searchbar_up_button");
	GtkWidget *searchbar = gtkpod_xml_get_widget (main_window_xml, "searchbar_hpanel");
	g_return_if_fail (upbutton);
	g_return_if_fail (searchbar);
	
	prefs_set_int (KEY_DISPLAY_SEARCH_ENTRY, FALSE);
		
	gtk_widget_hide_all (searchbar);
	gtk_widget_show (upbutton);
	gtk_widget_set_sensitive (upbutton, TRUE);
}

G_MODULE_EXPORT void on_searchbar_up_button_clicked (GtkWidget *widget, gpointer data)
{
	GtkWidget *searchbar = gtkpod_xml_get_widget (main_window_xml, "searchbar_hpanel");
	g_return_if_fail (searchbar);
		
	prefs_set_int (KEY_DISPLAY_SEARCH_ENTRY, TRUE);
			
	gtk_widget_show_all (searchbar);
	gtk_widget_hide (widget);
}

/* ---------------------------------------------------------------- */
/* Section for track display                                        */
/* DND -- Drag And Drop                                             */
/* ---------------------------------------------------------------- */

/* Move the paths listed in @data before or after (according to @pos)
   @path. Used for DND */
static gboolean tm_move_pathlist (gchar *data,
				  GtkTreePath *path,
				  GtkTreeViewDropPosition pos)
{
    GtkTreeIter temp;
    GtkTreeIter to_iter;
    GtkTreeIter *from_iter;
    GtkTreeModel *model;
    GtkListStore *store;
    GList *iterlist = NULL;
    GList *link;
    gchar **paths, **pathp;

    g_return_val_if_fail (data, FALSE);
    g_return_val_if_fail (*data, FALSE);

    model = gtk_tree_view_get_model (track_treeview);
    g_return_val_if_fail (model, FALSE);
    store = get_model_as_store (model);
    g_return_val_if_fail (store, FALSE);

    g_return_val_if_fail (gtk_tree_model_get_iter (model, &temp, path), FALSE);
    convert_iter (model, &temp, &to_iter);

    /* split the path list into individual strings */
    paths = g_strsplit (data, "\n", -1);
    pathp = paths;
    
    /* Convert the list of paths into a list of iters */
    while (*pathp)
    {
        if ((strlen (*pathp) > 0) && gtk_tree_model_get_iter_from_string (model, &temp, *pathp))
        {
            from_iter = g_new (GtkTreeIter, 1);
            convert_iter (model, &temp, from_iter);
            iterlist = g_list_append (iterlist, from_iter);
        }
        
        ++pathp;
    }
    
    g_strfreev (paths);
    
    /* Move the iters in iterlist before or after @to_iter */
    switch (pos)
    {
    case GTK_TREE_VIEW_DROP_INTO_OR_AFTER:
    case GTK_TREE_VIEW_DROP_AFTER:
    for (link = g_list_last (iterlist); link; link = link->prev)
    {
        from_iter = (GtkTreeIter *)link->data;
        gtk_list_store_move_after (store, from_iter, &to_iter);
    }
    break;
    case GTK_TREE_VIEW_DROP_INTO_OR_BEFORE:
    case GTK_TREE_VIEW_DROP_BEFORE:
    for (link = g_list_first (iterlist); link; link = link->next)
    {
        from_iter = (GtkTreeIter *)link->data;
        gtk_list_store_move_before (store,
                    from_iter, &to_iter);
    }
    break;
    }

    /* free iterlist */
    for (link = iterlist; link; link = link->next)
	g_free (link->data);
    g_list_free (iterlist);

/*    update_model_view (model); -- not needed */
    tm_rows_reordered ();
    return TRUE;
}


/*
 * utility function for appending ipod track ids for track view (DND)
 */
static void
on_tm_dnd_get_track_foreach(GtkTreeModel *tm, GtkTreePath *tp,
			    GtkTreeIter *i, gpointer data)
{
    Track *tr;
    GString *tracklist = (GString *)data;

    g_return_if_fail (tracklist);

    gtk_tree_model_get(tm, i, READOUT_COL, &tr, -1);
    g_return_if_fail (tr);

    g_string_append_printf (tracklist, "%p\n", tr);
}


/*
 * utility function for appending path for track view (DND)
 */
static void
on_tm_dnd_get_path_foreach(GtkTreeModel *tm, GtkTreePath *tp,
			   GtkTreeIter *iter, gpointer data)
{
    GString *filelist = (GString *)data;
    gchar *ps = gtk_tree_path_to_string (tp);
    g_string_append_printf (filelist, "%s\n", ps);
    g_free (ps);
}

/*
 * utility function for appending file for track view (DND)
 */
static void
on_tm_dnd_get_file_foreach(GtkTreeModel *tm, GtkTreePath *tp,
			   GtkTreeIter *iter, gpointer data)
{
    Track *track;
    GString *filelist = (GString *)data;
    gchar *name;

    gtk_tree_model_get(tm, iter, READOUT_COL, &track, -1);
    name = get_file_name_from_source (track, SOURCE_PREFER_LOCAL);
    if (name)
    {
	g_string_append_printf (filelist, "file:%s\n", name);
	g_free (name);
    }
}

/*
 * utility function for appending file-uri for track view (DND)
 */
static void
on_tm_dnd_get_uri_foreach(GtkTreeModel *tm, GtkTreePath *tp,
			  GtkTreeIter *iter, gpointer data)
{
    Track *track;
    GString *filelist = (GString *)data;
    gchar *name;

    gtk_tree_model_get(tm, iter, READOUT_COL, &track, -1);
    name = get_file_name_from_source (track, SOURCE_PREFER_LOCAL);
    if (name)
    {
	gchar *uri = g_filename_to_uri (name, NULL, NULL);
	if (uri)
	{
	    g_string_append_printf (filelist, "%s\n", uri);
	    g_free (uri);
	}
	g_free (name);
    }
}

static void tm_drag_begin (GtkWidget *widget,
			   GdkDragContext *dc,
			   gpointer user_data)
{
    tm_stop_editing (TRUE);
}


/* remove dragged playlist after successful MOVE */
static void tm_drag_data_delete (GtkWidget *widget,
			   GdkDragContext *dc,
			   gpointer user_data)
{
    GtkTreeSelection *ts;
    Playlist *pl = pm_get_selected_playlist ();
    gint num;

/*     puts ("tm_drag_data_delete"); */

    g_return_if_fail (widget);
    ts = gtk_tree_view_get_selection (GTK_TREE_VIEW(widget));
    g_return_if_fail (ts);
    /* number of selected tracks */
    num = gtk_tree_selection_count_selected_rows (ts);
    if (num == 0) return;

    /* Check if we really have to delete the tracks */
    if (!itdb_playlist_is_mpl (pl))
    {	/* get list of selected tracks */
	GString *reply = g_string_sized_new (2000);
	gchar *str;
	Track *track;

	gtk_tree_selection_selected_foreach(ts,
					    on_tm_dnd_get_track_foreach,
					    reply);
	str = reply->str;
	while(parse_tracks_from_string(&str, &track))
	{
	    gp_playlist_remove_track (pl, track, DELETE_ACTION_PLAYLIST);
	}
	g_string_free (reply, TRUE);

	gtkpod_statusbar_message (ngettext ("Moved one track",
					    "Moved %d tracks", num), num);
    }
    else
    {
	gtkpod_statusbar_message (ngettext ("Copied one track",
					    "Copied %d tracks", num), num);
    }
}



static void tm_drag_end (GtkWidget *widget,
			 GdkDragContext *dc,
			 gpointer user_data)
{
/*     puts ("tm_drag_end"); */
    display_remove_autoscroll_row_timeout (widget);
    gtkpod_tracks_statusbar_update ();
}


static gboolean tm_drag_drop (GtkWidget *widget,
			      GdkDragContext *dc,
			      gint x,
			      gint y,
			      guint time,
			      gpointer user_data)
{
    GdkAtom target;

/*     puts ("tm_drag_data_drop"); */

    display_remove_autoscroll_row_timeout (widget);

    target = gtk_drag_dest_find_target (widget, dc, NULL);

    if (target != GDK_NONE)
    {
	gtk_drag_get_data (widget, dc, target, time);
	return TRUE;
    }
    return FALSE;
}

static void tm_drag_leave (GtkWidget *widget,
			   GdkDragContext *dc,
			   guint time,
			   gpointer user_data)
{
/*     puts ("tm_drag_leave"); */
    display_remove_autoscroll_row_timeout (widget);
}



static gboolean tm_drag_motion (GtkWidget *widget,
				GdkDragContext *dc,
				gint x,
				gint y,
				guint time,
				gpointer user_data)
{
    GtkTreeView *treeview;
    GdkAtom target;
    GtkTreePath *path = NULL;
    GtkTreeViewDropPosition pos;
    iTunesDB *itdb;
    ExtraiTunesDBData *eitdb;

/*     printf ("drag_motion  suggested: %d actions: %d\n", */
/*  	    dc->suggested_action, dc->actions); */

/*     printf ("x: %d y: %d\n", x, y); */

    g_return_val_if_fail (GTK_IS_TREE_VIEW (widget), FALSE);

    treeview = GTK_TREE_VIEW (widget);

    display_install_autoscroll_row_timeout (widget);

    itdb = gp_get_selected_itdb ();
    /* no drop is possible if no playlist/repository is selected */
    if (itdb == NULL)
    {
	gdk_drag_status (dc, 0, time);
	return FALSE;
    }
    eitdb = itdb->userdata;
    g_return_val_if_fail (eitdb, FALSE);
    /* no drop is possible if no repository is loaded */
    if (!eitdb->itdb_imported)
    {
	gdk_drag_status (dc, 0, time);
	return FALSE;
    }

    /* optically set destination row if available */
    if (gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
					   x, y, &path, &pos))
    {
	/* drops are only allowed before and after -- not onto
	   existing paths */
	switch (pos)
	{
	case GTK_TREE_VIEW_DROP_INTO_OR_AFTER:
	case GTK_TREE_VIEW_DROP_AFTER:
	    gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (widget), path,
					     GTK_TREE_VIEW_DROP_AFTER);
	    break;
	case GTK_TREE_VIEW_DROP_INTO_OR_BEFORE:
	case GTK_TREE_VIEW_DROP_BEFORE:
	    gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (widget), path,
					     GTK_TREE_VIEW_DROP_BEFORE);
	    break;
	}

 	gtk_tree_path_free (path);
 	path = NULL;
    }
    else
    {
	path = gtk_tree_path_new_first ();
	gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (widget), path,
					 GTK_TREE_VIEW_DROP_BEFORE);
	gtk_tree_path_free (path);
	path = NULL;
    }

    target = gtk_drag_dest_find_target (widget, dc, NULL);

    /* no drop possible if no valid target can be found */
    if (target == GDK_NONE)
    {
	gdk_drag_status (dc, 0, time);
	return FALSE;
    }

    if (widget == gtk_drag_get_source_widget (dc))
    {   /* drag is within the same widget */
	gint column;
	GtkSortType order;
	GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
	g_return_val_if_fail (model, FALSE);
	if(gtk_tree_sortable_get_sort_column_id (
	       GTK_TREE_SORTABLE (model), &column, &order))
	{   /* don't allow move because the model is sorted */
	    gdk_drag_status (dc, 0, time);
	    return FALSE;
	}
	else
	{   /* only allow moves within the same widget */
	    gdk_drag_status (dc, GDK_ACTION_MOVE, time);
	}
    }
    else
    {  /* whatever the source suggests */
	gdk_drag_status (dc, dc->suggested_action, time);
    }

    return TRUE;
}


static void tm_drag_data_get (GtkWidget       *widget,
			      GdkDragContext  *context,
			      GtkSelectionData *data,
			      guint            info,
			      guint            time,
			      gpointer         user_data)
{
    GtkTreeSelection *ts = NULL;
    GString *reply = g_string_sized_new (2000);

/*     printf("tm drag get info: %d\n", info); */
    if((data) && (ts = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget))))
    {
	switch (info)
	{
	case DND_GTKPOD_TRACKLIST:
	    gtk_tree_selection_selected_foreach(ts,
				    on_tm_dnd_get_track_foreach, reply);
	    break;
	case DND_GTKPOD_TM_PATHLIST:
	    gtk_tree_selection_selected_foreach(ts,
				    on_tm_dnd_get_path_foreach, reply);
	    break;
	case DND_TEXT_URI_LIST:
	    gtk_tree_selection_selected_foreach(ts,
				    on_tm_dnd_get_uri_foreach, reply);
	    break;
	case DND_TEXT_PLAIN:
	    gtk_tree_selection_selected_foreach(ts,
				    on_tm_dnd_get_file_foreach, reply);
	    break;
	default:
	    g_warning ("Programming error: tm_drag_data_get received unknown info type (%d)\n", info);
	    break;
	}
    }
    gtk_selection_data_set(data, data->target, 8, reply->str, reply->len);
    g_string_free (reply, TRUE);
}

static void tm_drag_data_received (GtkWidget       *widget,
				   GdkDragContext  *dc,
				   gint             x,
				   gint             y,
				   GtkSelectionData *data,
				   guint            info,
				   guint            time,
				   gpointer         user_data)
{
    GtkTreePath *path = NULL;
    GtkTreeModel *model = NULL;
    GtkTreeViewDropPosition pos = 0;
    gboolean result = FALSE;

    /* printf ("sm drop received info: %d\n", info); */

    /* sometimes we get empty dnd data, ignore */
    if(widgets_blocked || (!dc) ||
       (!data) || (data->length < 0)) return;
    /* yet another check, i think it's an 8 bit per byte check */
    if(data->format != 8) return;

    display_remove_autoscroll_row_timeout (widget);

    model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
    g_return_if_fail (model);
    if (!gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
					    x, y, &path, &pos))
    {
	gint py;
	gdk_window_get_pointer (
	    gtk_tree_view_get_bin_window (GTK_TREE_VIEW (widget)),
	    NULL, &py, NULL);
	if (py < 5)
	{
	    /* initialize with first displayed and drop before */
	    GtkTreeIter iter;
	    if (gtk_tree_model_get_iter_first (model, &iter))
	    {
		path = gtk_tree_model_get_path (model, &iter);
		pos = GTK_TREE_VIEW_DROP_BEFORE;
	    }
	}
	else
	{   /* initialize with last path if available and drop after */
	    GtkTreeIter iter;
	    if (gtk_tree_model_get_iter_first (model, &iter))
	    {
		GtkTreeIter last_valid_iter;
		do
		{
		    last_valid_iter = iter;
		} while (gtk_tree_model_iter_next (model, &iter));
		path = gtk_tree_model_get_path (model, &last_valid_iter);
		pos = GTK_TREE_VIEW_DROP_AFTER;
	    }
	}
    }

    if (path)
    {   /* map position onto BEFORE or AFTER */
	switch (pos)
	{
	case GTK_TREE_VIEW_DROP_INTO_OR_AFTER:
	case GTK_TREE_VIEW_DROP_AFTER:
	    pos = GTK_TREE_VIEW_DROP_AFTER;
	    break;
	case GTK_TREE_VIEW_DROP_INTO_OR_BEFORE:
	case GTK_TREE_VIEW_DROP_BEFORE:
	    pos = GTK_TREE_VIEW_DROP_BEFORE;
	    break;
	}
    }

    switch (info)
    {
    case DND_GTKPOD_TM_PATHLIST:
	g_return_if_fail (path);
	result = tm_move_pathlist (data->data, path, pos);
	dc->action = GDK_ACTION_MOVE;
	gtk_drag_finish (dc, TRUE, FALSE, time);
	break;
    case DND_TEXT_PLAIN:
	result = tm_add_filelist (data->data, path, pos);
	dc->action = dc->suggested_action;
	if (dc->action == GDK_ACTION_MOVE)
	    gtk_drag_finish (dc, TRUE, TRUE, time);
	else
	    gtk_drag_finish (dc, TRUE, FALSE, time);
	break;
    case DND_TEXT_URI_LIST:
	result = tm_add_filelist (data->data, path, pos);
	dc->action = dc->suggested_action;
	if (dc->action == GDK_ACTION_MOVE)
	    gtk_drag_finish (dc, TRUE, TRUE, time);
	else
	    gtk_drag_finish (dc, TRUE, FALSE, time);
	break;
    default:
	dc->action = 0;
	gtk_drag_finish (dc, FALSE, FALSE, time);
/* 	puts ("tm_drag_data_received(): should not be reached"); */
	break;
    }
    if (path) gtk_tree_path_free(path);
}

/* ---------------------------------------------------------------- */
/* Section for track display                                        */
/* other callbacks                                                  */
/* ---------------------------------------------------------------- */

static gboolean
on_track_treeview_key_release_event     (GtkWidget       *widget,
					GdkEventKey     *event,
					gpointer         user_data)
{
    guint mods;
    mods = event->state;

    if(!widgets_blocked && (mods & GDK_CONTROL_MASK))
    {
	switch(event->keyval)
	{
/* 	    case GDK_u: */
/* 		gp_do_selected_tracks (update_tracks); */
/* 		break; */
	    default:
		break;
	}
    }
    return FALSE;
}

/* ---------------------------------------------------------------- */
/* Section for track display                                        */
/* ---------------------------------------------------------------- */

/* Append track to the track model (or write into @into_iter if != 0) */
void tm_add_track_to_track_model (Track *track, GtkTreeIter *into_iter)
{
    GtkTreeIter iter;
    GtkTreeModel *model = gtk_tree_view_get_model (track_treeview);

    g_return_if_fail (model);

    if (into_iter)
    {
        convert_iter (model, into_iter, &iter);
    }
    else
    {
        gtk_list_store_append (get_model_as_store (model), &iter);
    }
    
    gtk_list_store_set (get_model_as_store (model), &iter, READOUT_COL, track, -1);
/*    update_model_view (model); -- not needed */
}



/* Used by remove_track() to remove track from model by calling
   gtk_tree_model_foreach ().
   Entry is deleted if data == track */
static gboolean tm_delete_track (GtkTreeModel *model,
				GtkTreePath *path,
				GtkTreeIter *iter,
				gpointer data)
{
    Track *track;

    gtk_tree_model_get (model, iter, READOUT_COL, &track, -1);

    if(track == (Track *)data)
    {
        GtkTreeIter temp;

        GtkTreeSelection *selection = gtk_tree_view_get_selection
        (track_treeview);
        /*       printf("unselect...\n"); */
        gtk_tree_selection_unselect_iter (selection, iter);
        /*       printf("...unselect done\n"); */

        convert_iter (model, iter, &temp);
        gtk_list_store_remove (get_model_as_store (model), &temp);
/*        update_model_view (model); -- not needed */
        return TRUE;
    }
    return FALSE;
}


/* Remove track from the display model */
void tm_remove_track (Track *track)
{
    GtkTreeModel *model = gtk_tree_view_get_model (track_treeview);

    if (model)
    {
        gtk_tree_model_foreach (model, tm_delete_track, track);
/*        update_model_view (model); -- not needed */
    }
}


/* Remove all tracks from the display model */
void tm_remove_all_tracks ()
{
    GtkTreeModel *model = gtk_tree_view_get_model (track_treeview);
    GtkTreeModel *realmodel;
    GtkTreeIter iter;
    GtkWidget *search_entry = gtkpod_xml_get_widget (main_window_xml, "search_entry");

    /* remove all tracks, including tracks filtered out */
    if (GTK_IS_TREE_MODEL_FILTER (model))
    {
	realmodel = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER(model));
    }
    else
    {
	realmodel = model;
    }

    while (gtk_tree_model_get_iter_first (realmodel, &iter))
    {
        gtk_list_store_remove (get_model_as_store (realmodel), &iter);
    }

    /* reset filter text -- if many tracks are added with the filter
     * activated, a lot of time is needed */
    gtk_entry_set_text (GTK_ENTRY (search_entry), "");

    tm_store_col_order ();
    tm_update_default_sizes ();
}

/* find out at which position column @tm_item is displayed */
/* static gint tm_get_col_position (TM_item tm_item) */
/* { */
/*     gint i; */
/*     GtkTreeViewColumn *col; */

/*     if (!track_treeview) return -1; */

/*     for (i=0; i<TM_NUM_COLUMNS_PREFS; ++i) */
/*     { */
/* 	col = gtk_tree_view_get_column (track_treeview, i); */
/* 	if (col->sort_column_id == tm_item) return i; */
/*     } */
/*     return -1; */
/* } */


/* store the order of the track view columns */
void tm_store_col_order (void)
{
    gint i;
    GtkTreeViewColumn *col;

    for (i=0; i<TM_NUM_COLUMNS; ++i)
    {
	col = gtk_tree_view_get_column (track_treeview, i);
	prefs_set_int_index("col_order", i, col->sort_column_id);
    }
}


/* Used by tm_track_changed() to find the track that
   changed name. If found, emit a "row changed" signal */
static gboolean tm_model_track_changed (GtkTreeModel *model,
				       GtkTreePath *path,
				       GtkTreeIter *iter,
				       gpointer data)
{
  Track *track;

  gtk_tree_model_get (model, iter, READOUT_COL, &track, -1);
  if(track == (Track *)data) {
    gtk_tree_model_row_changed (model, path, iter);
    return TRUE;
  }
  return FALSE;
}


/* One of the tracks has changed (this happens when the
   iTunesDB is read and some IDs are renumbered */
void tm_track_changed (Track *track)
{
  GtkTreeModel *model = gtk_tree_view_get_model (track_treeview);
  /*  printf("tm_track_changed enter\n");*/
  if (model != NULL)
    gtk_tree_model_foreach (model, tm_model_track_changed, track);
  /*  printf("tm_track_changed exit\n");*/
}



#if ((GTK_MAJOR_VERSION == 2) && (GTK_MINOR_VERSION < 2))
/* gtk_tree_selection_get_selected_rows() was introduced in 2.2 */
struct gtsgsr
{
    GtkTreeModel **model;
    GList        **list;
};

void  gtssf  (GtkTreeModel *model,
	      GtkTreePath *path,
	      GtkTreeIter *iter,
	      gpointer data)
{
    struct gtsgsr *gts = data;
    *gts->model = model;
    *gts->list = g_list_append (*gts->list, gtk_tree_path_copy (path));
}

GList *gtk_tree_selection_get_selected_rows (GtkTreeSelection *selection,
                                             GtkTreeModel     **model)
{
    struct gtsgsr gts;
    GList *list = NULL;

    gts.model = model;
    gts.list = &list;

    gtk_tree_selection_selected_foreach (selection, gtssf, &gts);
    return list;
}
#endif


static void tm_rating_edited (RBCellRendererRating *renderer,
							  const gchar *path_string,
							  double rating)
{
	GtkTreeModel *model = gtk_tree_view_get_model (track_treeview);
	GtkTreeIter iter;
    Track *track;
	GtkTreePath *path = gtk_tree_path_new_from_string (path_string);
		
	g_return_if_fail (model);
	g_return_if_fail (path);
	g_return_if_fail (gtk_tree_model_get_iter (model, &iter, path));
	
	gtk_tree_path_free (path);
	gtk_tree_model_get(model, &iter, READOUT_COL, &track, -1);
	
	if ((int) rating * ITDB_RATING_STEP != track->rating)
	{
		track->rating = (int) rating * ITDB_RATING_STEP;		
		track->time_modified = time (NULL);
		pm_track_changed (track);
		data_changed (track->itdb);

		if (prefs_get_int("id3_write"))
		{
			write_tags_to_file (track);
			gp_duplicate_remove (NULL, NULL);
		}
	}
}

/* Called when editable cell is being edited. Stores new data to the
   track list. ID3 tags in the corresponding files are updated as
   well, if activated in the pref settings */
static void
tm_cell_edited (GtkCellRendererText *renderer,
		const gchar         *path_string,
		const gchar         *new_text,
		gpointer             data)
{
  GtkTreeModel *model;
  GtkTreeSelection *selection;
  TM_item column;
  gboolean multi_edit;
  gint sel_rows_num;
  GList *row_list, *row_node, *first;


  column = (TM_item) g_object_get_data(G_OBJECT(renderer), "column");
  multi_edit = prefs_get_int("multi_edit");
/*  if (column == TM_COLUMN_TITLE)
      multi_edit &= prefs_get_int("multi_edit_title"); */
  selection = gtk_tree_view_get_selection(track_treeview);
  row_list = gtk_tree_selection_get_selected_rows(selection, &model);

/*   printf("tm_cell_edited: column: %d\n", column); */

  sel_rows_num = g_list_length (row_list);

  /* block widgets and update display if multi-edit is active */
  if (multi_edit && (sel_rows_num > 1)) block_widgets ();

  first = g_list_first (row_list);

  for (row_node = first;
       row_node && (multi_edit || (row_node == first));
       row_node = g_list_next(row_node))
  {
     Track *track;
     ExtraTrackData *etr;
     gboolean changed = FALSE;
     GtkTreeIter iter;
     gchar *str;

     gtk_tree_model_get_iter(model, &iter, (GtkTreePath *) row_node->data);
     gtk_tree_model_get(model, &iter, READOUT_COL, &track, -1);
     g_return_if_fail (track);
     etr = track->userdata;
     g_return_if_fail (etr);


     changed = FALSE;

     switch(column)
     {
     case TM_COLUMN_TITLE:
     case TM_COLUMN_ALBUM:
     case TM_COLUMN_ALBUMARTIST:
     case TM_COLUMN_ARTIST:
     case TM_COLUMN_GENRE:
     case TM_COLUMN_COMPOSER:
     case TM_COLUMN_COMMENT:
     case TM_COLUMN_FILETYPE:
     case TM_COLUMN_GROUPING:
     case TM_COLUMN_CATEGORY:
     case TM_COLUMN_DESCRIPTION:
     case TM_COLUMN_PODCASTURL:
     case TM_COLUMN_PODCASTRSS:
     case TM_COLUMN_SUBTITLE:
     case TM_COLUMN_TRACK_NR:
     case TM_COLUMN_TRACKLEN:
     case TM_COLUMN_CD_NR:
     case TM_COLUMN_YEAR:
     case TM_COLUMN_PLAYCOUNT:
     case TM_COLUMN_RATING:
     case TM_COLUMN_TIME_ADDED:
     case TM_COLUMN_TIME_PLAYED:
     case TM_COLUMN_TIME_MODIFIED:
     case TM_COLUMN_TIME_RELEASED:
     case TM_COLUMN_VOLUME:
     case TM_COLUMN_SOUNDCHECK:
     case TM_COLUMN_BITRATE:
     case TM_COLUMN_SAMPLERATE:
     case TM_COLUMN_BPM:
     case TM_COLUMN_MEDIA_TYPE:
     case TM_COLUMN_TV_SHOW:
     case TM_COLUMN_TV_EPISODE:
     case TM_COLUMN_TV_NETWORK:
     case TM_COLUMN_SEASON_NR:
     case TM_COLUMN_EPISODE_NR:
     case TM_COLUMN_SORT_TITLE:
     case TM_COLUMN_SORT_ALBUM:
     case TM_COLUMN_SORT_ALBUMARTIST:
     case TM_COLUMN_SORT_COMPOSER:
     case TM_COLUMN_SORT_TVSHOW:
     case TM_COLUMN_SORT_ARTIST:
	 changed = track_set_text (track, new_text, TM_to_T (column));
	 if (changed && (column == TM_COLUMN_TRACKLEN))
	 {  /* be on the safe side and reset starttime, stoptime and
	     * filesize */
	     gchar *path = get_file_name_from_source (track,
						      SOURCE_PREFER_LOCAL);
	     track->starttime = 0;
	     track->stoptime = 0;
	     if (path)
	     {
		 struct stat filestat;
		 stat (path, &filestat);
		 track->size = filestat.st_size;
	     }
	 }
	 /* redisplay some items to be on the safe side */
	 switch (column)
	 {
	 case TM_COLUMN_TRACK_NR:
	 case TM_COLUMN_CD_NR:
	 case TM_COLUMN_TRACKLEN:
	 case TM_COLUMN_TIME_ADDED:
	 case TM_COLUMN_TIME_PLAYED:
	 case TM_COLUMN_TIME_MODIFIED:
	 case TM_COLUMN_TIME_RELEASED:
	     str = track_get_text (track, TM_to_T (column));
	     g_object_set (G_OBJECT (renderer), "text", str, NULL);
	     g_free (str);
	     break;
	 default:
	     break;
	 }
	 break;
     case TM_COLUMN_IPOD_ID:
     case TM_COLUMN_PC_PATH:
     case TM_COLUMN_TRANSFERRED:
     case TM_COLUMN_SIZE:
     case TM_COLUMN_IPOD_PATH:
     case TM_COLUMN_COMPILATION:
     case TM_COLUMN_THUMB_PATH:
     case TM_COLUMN_LYRICS:
     case TM_NUM_COLUMNS:
	 /* These are not editable text fields */
	 break;
     }
/*      printf ("  changed: %d\n", changed); */
     if (changed)
     {
	track->time_modified = time (NULL);
        pm_track_changed (track);    /* notify playlist model... */
        data_changed (track->itdb); /* indicate that data has changed */

        if (prefs_get_int("id3_write"))
        {
	    /* T_item tag_id;*/
           /* should we update all ID3 tags or just the one
              changed? -- obsoleted in 0.71*/
/*           if (prefs_get_id3_writeall ()) tag_id = T_ALL;
	     else                           tag_id = TM_to_T (column);*/
           write_tags_to_file (track);
           /* display possible duplicates that have been removed */
           gp_duplicate_remove (NULL, NULL);
        }
     }
     while (widgets_blocked && gtk_events_pending ())  gtk_main_iteration ();
  }

  if (multi_edit && (sel_rows_num > 1)) release_widgets ();

  g_list_foreach(row_list, (GFunc) gtk_tree_path_free, NULL);
  g_list_free(row_list);
}


/* The track data is stored in a separate list (static GList *tracks)
   and only pointers to the corresponding Track structure are placed
   into the model.
   This function reads the data for the given cell from the list and
   passes it to the renderer. */
static void tm_cell_data_func (GtkTreeViewColumn *tree_column,
			       GtkCellRenderer   *renderer,
			       GtkTreeModel      *model,
			       GtkTreeIter       *iter,
			       gpointer           data)
{
	Track *track;
	ExtraTrackData *etr;
	iTunesDB *itdb;
	TM_item column;
	gchar *text;

	column = (TM_item)g_object_get_data (G_OBJECT (renderer), "column");

	g_return_if_fail ((column >= 0) && (column < TM_NUM_COLUMNS));

	gtk_tree_model_get (model, iter, READOUT_COL, &track, -1);
	g_return_if_fail (track);
	etr = track->userdata;
	g_return_if_fail (etr);
	itdb = track->itdb;
	g_return_if_fail (itdb);

	text = track_get_text (track, TM_to_T (column));

	switch (column)
	{
	case TM_COLUMN_RATING:
		g_object_set (G_OBJECT (renderer),
					  "rating", (double)(track->rating / ITDB_RATING_STEP),
					  NULL);
		break;
	case TM_COLUMN_TITLE:
	case TM_COLUMN_ARTIST:
	case TM_COLUMN_ALBUM:
	case TM_COLUMN_GENRE:
	case TM_COLUMN_COMPOSER:
	case TM_COLUMN_COMMENT:
	case TM_COLUMN_FILETYPE:
	case TM_COLUMN_GROUPING:
	case TM_COLUMN_CATEGORY:
	case TM_COLUMN_DESCRIPTION:
	case TM_COLUMN_PODCASTURL:
	case TM_COLUMN_PODCASTRSS:
	case TM_COLUMN_SUBTITLE:
	case TM_COLUMN_TIME_PLAYED:
	case TM_COLUMN_TIME_MODIFIED:
	case TM_COLUMN_TIME_ADDED:
	case TM_COLUMN_TIME_RELEASED:
	case TM_COLUMN_TV_SHOW:
	case TM_COLUMN_TV_EPISODE:
	case TM_COLUMN_TV_NETWORK:
	case TM_COLUMN_ALBUMARTIST:
	case TM_COLUMN_SORT_ARTIST:
	case TM_COLUMN_SORT_TITLE:
	case TM_COLUMN_SORT_ALBUM:
	case TM_COLUMN_SORT_ALBUMARTIST:
	case TM_COLUMN_SORT_COMPOSER:
	case TM_COLUMN_SORT_TVSHOW:
	  g_object_set (G_OBJECT (renderer),
			"text", text,
			"editable", TRUE,
			"xalign", 0.0, NULL);
	  break;
	case TM_COLUMN_MEDIA_TYPE:
	  g_object_set (G_OBJECT (renderer),
			"text", text,
			"editable", FALSE,
			"xalign", 0.0, NULL);
	  break;
	case TM_COLUMN_TRACK_NR:
	case TM_COLUMN_CD_NR:
	case TM_COLUMN_BITRATE:
	case TM_COLUMN_SAMPLERATE:
	case TM_COLUMN_BPM:
	case TM_COLUMN_PLAYCOUNT:
	case TM_COLUMN_YEAR:
	case TM_COLUMN_VOLUME:
	case TM_COLUMN_SOUNDCHECK:
	case TM_COLUMN_TRACKLEN:
	case TM_COLUMN_SEASON_NR:
	case TM_COLUMN_EPISODE_NR:
	  g_object_set (G_OBJECT (renderer),
			"text", text,
			"editable", TRUE,
			"xalign", 1.0, NULL);
	  break;
	case TM_COLUMN_IPOD_ID:
	case TM_COLUMN_SIZE:
	  g_object_set (G_OBJECT (renderer),
			"text", text,
			"editable", FALSE,
			"xalign", 1.0, NULL);
	  break;
	case TM_COLUMN_PC_PATH:
	case TM_COLUMN_IPOD_PATH:
	case TM_COLUMN_THUMB_PATH:
	  g_object_set (G_OBJECT (renderer),
			"text", text,
			"editable", FALSE,
			"xalign", 0.0, NULL);
	  break;
	case TM_COLUMN_LYRICS:
	  g_object_set (G_OBJECT (renderer),
			"active", track->lyrics_flag,
			"activatable", TRUE, NULL);
	  break;
	case TM_COLUMN_TRANSFERRED:
	  g_object_set (G_OBJECT (renderer),
			"active", track->transferred,
			"activatable", FALSE, NULL);
	  break;
	case TM_COLUMN_COMPILATION:
	  g_object_set (G_OBJECT (renderer),
			"active", track->compilation,
			"activatable", TRUE, NULL);
	  break;
	case TM_NUM_COLUMNS:
	  break;
	}
	g_free (text);
}



/* This function is analogous to tm_cell_data_func(), but is only used
   for the title column to distinguish between the text and the toggle
   button there. The other toggle buttons (e.g. compilation) can
   easily be handled in the original tm_cell_data_func() */
static void tm_cell_data_func_toggle (GtkTreeViewColumn *tree_column,
				      GtkCellRenderer   *renderer,
				      GtkTreeModel      *model,
				      GtkTreeIter       *iter,
				      gpointer           data)
{
  Track *track;
  TM_item column;

  column = (TM_item)g_object_get_data (G_OBJECT (renderer), "column");
  gtk_tree_model_get (model, iter, READOUT_COL, &track, -1);

  /* printf ("tm_cell_data_func_toggle() entered\n"); */

  switch (column)
  {
  case TM_COLUMN_TITLE:
      g_object_set (G_OBJECT (renderer),
		    "active", !track->checked,
		    "activatable", TRUE, NULL);
      break;
  default:
      g_warning ("Programming error: unknown column in tm_cell_data_func_toggle: %d\n", column);
      break;
  }
}



/* Called when a toggle cell is being changed. Stores new data to the
   track list. */
static void
tm_cell_toggled (GtkCellRendererToggle *renderer,
		 gchar *arg1,
		 gpointer user_data)
{
  GtkTreeModel *model;
  GtkTreeSelection *selection;
  TM_item column;
  gboolean multi_edit;
  gint sel_rows_num;
  GList *row_list, *row_node, *first;
  gboolean active;
  GList *selected_tracks = NULL;

  column = (TM_item) g_object_get_data(G_OBJECT(renderer), "column");
  multi_edit = prefs_get_int("multi_edit");
  selection = gtk_tree_view_get_selection(track_treeview);
  row_list = gtk_tree_selection_get_selected_rows(selection, &model);

  /* printf("tm_cell_toggled: column: %d, arg1: %p\n", column, arg1); */

  sel_rows_num = g_list_length (row_list);

  /* block widgets and update display if multi-edit is active */
  if (multi_edit && (sel_rows_num > 1)) block_widgets ();

  first = g_list_first (row_list);

  /* active will show the old state -- before the toggle */
  g_object_get (G_OBJECT (renderer), "active", &active, NULL);

  for (row_node = first;
       row_node && (multi_edit || (row_node == first));
       row_node = g_list_next(row_node))
  {
     Track *track;
     gboolean changed;
     GtkTreeIter iter;

     gtk_tree_model_get_iter(model, &iter, (GtkTreePath *) row_node->data);
     gtk_tree_model_get(model, &iter, READOUT_COL, &track, -1);
     changed = FALSE;

     switch(column)
     {
     case TM_COLUMN_TITLE:
	 if ((active && (track->checked == 0)) ||
	     (!active && (track->checked == 1)))
	     changed = TRUE;
	 if (active) track->checked = 1;
	 else        track->checked = 0;
	 break;
     case TM_COLUMN_COMPILATION:
	 if ((!active && (track->compilation == 0)) ||
	     (active && (track->compilation == 1)))
	     changed = TRUE;
	 if (!active) track->compilation = 1;
	 else        track->compilation = 0;
        break;
     case TM_COLUMN_LYRICS:
	 /* collect all selected tracks -- then call "edit details" */
	 selected_tracks = g_list_append (selected_tracks, track);
	 break;
     case TM_COLUMN_ARTIST:
     case TM_COLUMN_ALBUM:
     case TM_COLUMN_GENRE:
     case TM_COLUMN_COMPOSER:
     case TM_COLUMN_TRACK_NR:
     case TM_COLUMN_IPOD_ID:
     case TM_COLUMN_PC_PATH:
     case TM_COLUMN_TRANSFERRED:
     case TM_COLUMN_SIZE:
     case TM_COLUMN_TRACKLEN:
     case TM_COLUMN_BITRATE:
     case TM_COLUMN_PLAYCOUNT:
     case TM_COLUMN_RATING:
     case TM_COLUMN_TIME_PLAYED:
     case TM_COLUMN_TIME_MODIFIED:
     case TM_COLUMN_VOLUME:
     case TM_COLUMN_YEAR:
     case TM_COLUMN_CD_NR:
     case TM_COLUMN_TIME_ADDED:
     case TM_COLUMN_IPOD_PATH:
     case TM_COLUMN_SOUNDCHECK:
     case TM_COLUMN_SAMPLERATE:
     case TM_COLUMN_BPM:
     case TM_COLUMN_FILETYPE:
     case TM_COLUMN_GROUPING:
     case TM_COLUMN_COMMENT:
     case TM_COLUMN_CATEGORY:
     case TM_COLUMN_DESCRIPTION:
     case TM_COLUMN_PODCASTURL:
     case TM_COLUMN_PODCASTRSS:
     case TM_COLUMN_SUBTITLE:
     case TM_COLUMN_TIME_RELEASED:
     case TM_COLUMN_THUMB_PATH:
     case TM_COLUMN_MEDIA_TYPE:
     case TM_COLUMN_TV_SHOW:
     case TM_COLUMN_TV_EPISODE:
     case TM_COLUMN_TV_NETWORK:
     case TM_COLUMN_SEASON_NR:
     case TM_COLUMN_EPISODE_NR:
     case TM_COLUMN_ALBUMARTIST:
     case TM_COLUMN_SORT_ARTIST:
     case TM_COLUMN_SORT_TITLE:
     case TM_COLUMN_SORT_ALBUM:
     case TM_COLUMN_SORT_ALBUMARTIST:
     case TM_COLUMN_SORT_COMPOSER:
     case TM_COLUMN_SORT_TVSHOW:
     case TM_NUM_COLUMNS:
	 /* these are not toggle buttons or are non-editable */
	 break;
     }
/*      printf ("  changed: %d\n", changed); */
     if (changed)
     {
	track->time_modified = time (NULL);
/*        pm_track_changed (track);  notify playlist model... -- not
 *        necessary here because only the track model is affected */
        data_changed (track->itdb);  /* indicate that data has changed */
        
        /* If the changed column is the compilation flag update the file
           if required */
        if (column == TM_COLUMN_COMPILATION)
           if (prefs_get_int("id3_write"))
              write_tags_to_file (track);
        
     }
     while (widgets_blocked && gtk_events_pending ())  gtk_main_iteration ();
  }

  if ((column == TM_COLUMN_LYRICS) && (selected_tracks != NULL))
  {
      /* set displayed page to the lyrics page */
      prefs_set_int (DETAILS_WINDOW_NOTEBOOK_PAGE, 3);
      details_edit (selected_tracks);
      g_list_free (selected_tracks);
  }

  if (multi_edit && (sel_rows_num > 1)) release_widgets ();

  g_list_foreach(row_list, (GFunc) gtk_tree_path_free, NULL);
  g_list_free(row_list);
}




/**
 * tm_get_nr_of_tracks - get the number of tracks displayed
 * currently in the track model Returns - the number of tracks displayed
 * currently
 */
gint
tm_get_nr_of_tracks(void)
{
    gint result = 0;
    GtkTreeModel *tm = NULL;

    tm = gtk_tree_view_get_model (track_treeview);
    if (tm)
    {
	result = gtk_tree_model_iter_n_children (tm, NULL);
    }
    return result;
}


static gint comp_int (gconstpointer a, gconstpointer b)
{
    return (GPOINTER_TO_INT(a)-(GPOINTER_TO_INT(b)));
}


/**
 * Reorder tracks in playlist to match order of tracks displayed in track
 * view. Only the subset of tracks currently displayed is reordered.
 * data_changed() is called when necessary.
 */
void
tm_rows_reordered (void)
{
    Playlist *current_pl;

    g_return_if_fail (track_treeview);
    current_pl = pm_get_selected_playlist ();

    if(current_pl)
    {
	GtkTreeModel *tm = NULL;
	GtkTreeIter i;
	GList *new_list = NULL, *old_pos_l = NULL;
	gboolean valid = FALSE;
	GList *nlp, *olp;
	gboolean changed = FALSE;
	iTunesDB *itdb = NULL;

	tm = gtk_tree_view_get_model (track_treeview);
	g_return_if_fail (tm);

	valid = gtk_tree_model_get_iter_first (tm,&i);
	while (valid)
	{
	    Track *new_track;
	    gint old_position;

	    gtk_tree_model_get (tm, &i, READOUT_COL, &new_track, -1);
	    g_return_if_fail (new_track);

	    if (!itdb) itdb = new_track->itdb;
	    new_list = g_list_append (new_list, new_track);
	    /* what position was this track in before? */
	    old_position = g_list_index (current_pl->members, new_track);
	    /* check if we already used this position before (can
	       happen if track has been added to playlist more than
	       once */
	    while ((old_position != -1) &&
		   g_list_find (old_pos_l, GINT_TO_POINTER(old_position)))
	    {  /* find next occurence */
		GList *link;
		gint next;
		link = g_list_nth (current_pl->members, old_position + 1);
		next = g_list_index (link, new_track);
		if (next == -1)   old_position = -1;
		else              old_position += (1+next);
	    }
	    /* we make a sorted list of the old positions */
	    old_pos_l = g_list_insert_sorted (old_pos_l,
					      GINT_TO_POINTER(old_position),
					      comp_int);
	    valid = gtk_tree_model_iter_next (tm, &i);
	}
	nlp = new_list;
	olp = old_pos_l;
	while (nlp && olp)
	{
	    GList *old_link;
	    guint position = GPOINTER_TO_INT(olp->data);

	    /* if position == -1 one of the tracks in the track view
	       could not be found in the selected playlist -> stop! */
	    if (position == -1)
	    {
		g_warning ("Programming error: tm_rows_reordered_callback: track in track view was not in selected playlist\n");
		g_return_if_reached ();
	    }
	    old_link = g_list_nth (current_pl->members, position);
	    /* replace old track with new track */
	    if (old_link->data != nlp->data)
	    {
		old_link->data = nlp->data;
		changed = TRUE;
	    }
	    /* next */
	    nlp = nlp->next;
	    olp = olp->next;
	}
	g_list_free (new_list);
	g_list_free (old_pos_l);
	/* if we changed data, mark data as changed and adopt order in
	   sort tabs */
	if (changed)
	{
	    data_changed (itdb);
	    st_adopt_order_in_playlist ();
	}
    }
}


static void
on_trackids_list_foreach ( GtkTreeModel *tm, GtkTreePath *tp,
			   GtkTreeIter *i, gpointer data)
{
    Track *tr = NULL;
    GList *l = *((GList**)data);
    gtk_tree_model_get(tm, i, READOUT_COL, &tr, -1);
    g_return_if_fail (tr);
    l = g_list_append(l, GUINT_TO_POINTER(tr->id));
    *((GList**)data) = l;
}


/* return a list containing the track IDs of all tracks currently being
   selected */
GList *
tm_get_selected_trackids(void)
{
    GList *result = NULL;
    GtkTreeSelection *ts = NULL;

    if((ts = gtk_tree_view_get_selection(GTK_TREE_VIEW(track_treeview))))
    {
	gtk_tree_selection_selected_foreach(ts, on_trackids_list_foreach,
					    &result);
    }
    return(result);
}

/* return a list containing the track IDs of all tracks currently being
   displayed */
GList *
tm_get_all_trackids(void)
{
    gboolean
	on_all_trackids_list_foreach (GtkTreeModel *tm, GtkTreePath *tp,
				      GtkTreeIter *i, gpointer data)
	{
	    on_trackids_list_foreach (tm, tp, i, data);
	    return FALSE;
	}
    GList *result = NULL;
    GtkTreeModel *model;

    if((model = gtk_tree_view_get_model (track_treeview)))
    {
	gtk_tree_model_foreach(model, on_all_trackids_list_foreach,
			       &result);
    }
    return(result);
}

static void
on_tracks_list_foreach ( GtkTreeModel *tm, GtkTreePath *tp,
			 GtkTreeIter *i, gpointer data)
{
    Track *tr = NULL;
    GList *l = *((GList**)data);
    
    gtk_tree_model_get(tm, i, READOUT_COL, &tr, -1);
    g_return_if_fail (tr);
    l = g_list_append(l, tr);
    *((GList**)data) = l;
}


/* return a list containing pointers to all tracks currently being
   selected */
GList *
tm_get_selected_tracks(void)
{
    GList *result = NULL;
    GtkTreeSelection *ts = NULL;

    if((ts = gtk_tree_view_get_selection(GTK_TREE_VIEW(track_treeview))))
    {
	gtk_tree_selection_selected_foreach(ts, on_tracks_list_foreach,
					    &result);
    }
    return(result);
}



    /* used by tm_get_all_tracks */
    static gboolean on_all_tracks_list_foreach (GtkTreeModel *tm,
						GtkTreePath *tp,
						GtkTreeIter *i,
						gpointer data)
    {
	on_tracks_list_foreach (tm, tp, i, data);
	return FALSE;
    }


/* return a list containing pointers to all tracks currently being
   displayed. You must g_list_free() the list after use. */
GList *
tm_get_all_tracks(void)
{
    GList *result = NULL;
    GtkTreeModel *model = gtk_tree_view_get_model (track_treeview);

    g_return_val_if_fail (model, NULL);

    gtk_tree_model_foreach(model, on_all_tracks_list_foreach,
			   &result);
    return result;
}


/* Stop editing. If @cancel is TRUE, the edited value will be
   discarded (I have the feeling that the "discarding" part does not
   work quite the way intended). */
void tm_stop_editing (gboolean cancel)
{
    GtkTreeViewColumn *col;

    if (!track_treeview)  return;

    gtk_tree_view_get_cursor (track_treeview, NULL, &col);
    if (col)
    {
	/* Before removing the widget we set multi_edit to FALSE. That
	   way at most one entry will be changed (this also doesn't
	   seem to work the way intended) */
	gboolean me = prefs_get_int("multi_edit");
	prefs_set_int("multi_edit", FALSE);
	if (!cancel && col->editable_widget)
	    gtk_cell_editable_editing_done (col->editable_widget);
	if (col->editable_widget)
	    gtk_cell_editable_remove_widget (col->editable_widget);
	prefs_set_int("multi_edit", me);
    }
}



/* Function to compare @tm_item of @track1 and @track2. Used by
   tm_data_compare_func() */
static gint tm_data_compare (Track *track1, Track *track2,
			     TM_item tm_item)
{
  gint cmp = 0;
  ExtraTrackData *etr1, *etr2;


  g_return_val_if_fail (track1 && track2, 0);

  switch (tm_item)
  {
  case TM_COLUMN_TITLE:
  case TM_COLUMN_ALBUM:
  case TM_COLUMN_ALBUMARTIST:
  case TM_COLUMN_GENRE:
  case TM_COLUMN_COMPOSER:
  case TM_COLUMN_COMMENT:
  case TM_COLUMN_FILETYPE:
  case TM_COLUMN_GROUPING:
  case TM_COLUMN_ARTIST:
  case TM_COLUMN_CATEGORY:
  case TM_COLUMN_DESCRIPTION:
  case TM_COLUMN_PODCASTURL:
  case TM_COLUMN_PODCASTRSS:
  case TM_COLUMN_SUBTITLE:
  case TM_COLUMN_TV_SHOW:
  case TM_COLUMN_TV_EPISODE:
  case TM_COLUMN_TV_NETWORK:
  case TM_COLUMN_SORT_TITLE:
  case TM_COLUMN_SORT_ALBUM:
  case TM_COLUMN_SORT_ARTIST:
  case TM_COLUMN_SORT_ALBUMARTIST:
  case TM_COLUMN_SORT_COMPOSER:
  case TM_COLUMN_SORT_TVSHOW:
      /* string_compare_func is set to either compare_string_fuzzy or
	 compare_string in tm_sort_column_changed() which is called
	 once before the comparing begins. */
      cmp = string_compare_func (
	  track_get_item (track1, TM_to_T (tm_item)),
	  track_get_item (track2, TM_to_T (tm_item)));
      break;
  case TM_COLUMN_TRACK_NR:
      cmp = track1->tracks - track2->tracks;
      if (cmp == 0) cmp = track1->track_nr - track2->track_nr;
      break;
  case TM_COLUMN_CD_NR:
      cmp = track1->cds - track2->cds;
      if (cmp == 0) cmp = track1->cd_nr - track2->cd_nr;
      break;
  case TM_COLUMN_IPOD_ID:
      cmp = track1->id - track2->id;
      break;
  case TM_COLUMN_PC_PATH:
      etr1 = track1->userdata;
      etr2 = track2->userdata;
      g_return_val_if_fail (etr1 && etr2, 0);
      cmp = g_utf8_collate (etr1->pc_path_utf8, etr2->pc_path_utf8);
      break;
  case TM_COLUMN_IPOD_PATH:
      cmp = g_utf8_collate (track1->ipod_path, track2->ipod_path);
      break;
  case TM_COLUMN_THUMB_PATH:
      etr1 = track1->userdata;
      etr2 = track2->userdata;
      g_return_val_if_fail (etr1 && etr2, 0);
      cmp = g_utf8_collate (etr1->thumb_path_utf8, etr2->thumb_path_utf8);
      break;
  case TM_COLUMN_TRANSFERRED:
      if(track1->transferred == track2->transferred)
	  cmp = 0;
      else if(track1->transferred == TRUE)
	  cmp = 1;
      else
	  cmp = -1;
      break;
  case TM_COLUMN_COMPILATION:
      if(track1->compilation == track2->compilation)
	  cmp = 0;
      else if(track1->compilation == TRUE)
	  cmp = 1;
      else
	  cmp = -1;
      break;
  case TM_COLUMN_SIZE:
      cmp = track1->size - track2->size;
      break;
  case TM_COLUMN_TRACKLEN:
      cmp = track1->tracklen - track2->tracklen;
      break;
  case TM_COLUMN_BITRATE:
      cmp = track1->bitrate - track2->bitrate;
      break;
  case TM_COLUMN_SAMPLERATE:
      cmp = track1->samplerate - track2->samplerate;
      break;
  case TM_COLUMN_BPM:
      cmp = track1->BPM - track2->BPM;
      break;
  case TM_COLUMN_PLAYCOUNT:
      cmp = track1->playcount - track2->playcount;
      break;
  case  TM_COLUMN_RATING:
      cmp = track1->rating - track2->rating;
      break;
  case TM_COLUMN_TIME_ADDED:
  case TM_COLUMN_TIME_PLAYED:
  case TM_COLUMN_TIME_MODIFIED:
  case TM_COLUMN_TIME_RELEASED:
      cmp = COMP (time_get_time (track1, TM_to_T (tm_item)),
		  time_get_time (track2, TM_to_T (tm_item)));
      break;
  case  TM_COLUMN_VOLUME:
      cmp = track1->volume - track2->volume;
      break;
  case  TM_COLUMN_SOUNDCHECK:
      /* If soundcheck is unset (0) use 0 dB (1000) */
      cmp = (track1->soundcheck? track1->soundcheck:1000) - 
	  (track2->soundcheck? track2->soundcheck:1000);
      break;
  case TM_COLUMN_YEAR:
      cmp = track1->year - track2->year;
      break;
  case TM_COLUMN_SEASON_NR:
      cmp = track1->season_nr - track2->season_nr;
      break;
  case TM_COLUMN_EPISODE_NR:
      cmp = track1->episode_nr - track2->episode_nr;
      break;
  case TM_COLUMN_MEDIA_TYPE:
      cmp = track1->mediatype - track2->mediatype;
      break;
  case  TM_COLUMN_LYRICS:
      cmp = track1->lyrics_flag - track2->lyrics_flag;
      break;
  case TM_NUM_COLUMNS:
      break;
  }

  /* implement stable sorting: if two items are the same, revert to
     the last relative positition */
  if (cmp == 0)
  {
      etr1 = track1->userdata;
      etr2 = track2->userdata;
      g_return_val_if_fail (etr1 && etr2, 0);
      cmp = etr1->sortindex - etr2->sortindex;
  }
  return cmp;
}


/* Function used to compare rows with user's search string */
gboolean tm_search_equal_func (GtkTreeModel *model,
			       gint column,
			       const gchar *key,
			       GtkTreeIter *iter,
			       gpointer search_data)
{
  Track *track1;
  gboolean cmp = 0;
  gtk_tree_model_get (model, iter, READOUT_COL, &track1, -1);
  switch ((TM_item)column)
  {
  case TM_COLUMN_TITLE:
  case TM_COLUMN_ALBUM:
  case TM_COLUMN_GENRE:
  case TM_COLUMN_COMPOSER:
  case TM_COLUMN_COMMENT:
  case TM_COLUMN_FILETYPE:
  case TM_COLUMN_GROUPING:
  case TM_COLUMN_ARTIST:
  case TM_COLUMN_CATEGORY:
  case TM_COLUMN_DESCRIPTION:
  case TM_COLUMN_PODCASTURL:
  case TM_COLUMN_PODCASTRSS:
  case TM_COLUMN_SUBTITLE:
  case TM_COLUMN_PC_PATH:
  case TM_COLUMN_YEAR:
  case TM_COLUMN_IPOD_PATH:
  case TM_COLUMN_COMPILATION:
  case TM_COLUMN_THUMB_PATH:
  case TM_COLUMN_TV_SHOW:
  case TM_COLUMN_TV_EPISODE:
  case TM_COLUMN_TV_NETWORK:
  case TM_COLUMN_ALBUMARTIST:
  case TM_COLUMN_SORT_ARTIST:
  case TM_COLUMN_SORT_TITLE:
  case TM_COLUMN_SORT_ALBUM:
  case TM_COLUMN_SORT_ALBUMARTIST:
  case TM_COLUMN_SORT_COMPOSER:
  case TM_COLUMN_SORT_TVSHOW:
      cmp = (compare_string_start_case_insensitive (
		 track_get_item (track1, TM_to_T (column)),
		 key) != 0);
      break;
  case TM_COLUMN_TRACK_NR:
  case TM_COLUMN_IPOD_ID:
  case TM_COLUMN_TRANSFERRED:
  case TM_COLUMN_SIZE:
  case TM_COLUMN_TRACKLEN:
  case TM_COLUMN_BITRATE:
  case TM_COLUMN_PLAYCOUNT:
  case TM_COLUMN_RATING:
  case TM_COLUMN_TIME_PLAYED:
  case TM_COLUMN_TIME_MODIFIED:
  case TM_COLUMN_VOLUME:
  case TM_COLUMN_CD_NR:
  case TM_COLUMN_TIME_ADDED:
  case TM_COLUMN_SOUNDCHECK:
  case TM_COLUMN_SAMPLERATE:
  case TM_COLUMN_BPM:
  case TM_COLUMN_TIME_RELEASED:
  case TM_COLUMN_MEDIA_TYPE:
  case TM_COLUMN_SEASON_NR:
  case TM_COLUMN_EPISODE_NR:
  case TM_COLUMN_LYRICS:
  case TM_NUM_COLUMNS:
      break;
  }
  return cmp;
};

/* Function used to compare two cells during sorting (track view) */
gint tm_data_compare_func (GtkTreeModel *model,
			GtkTreeIter *a,
			GtkTreeIter *b,
			gpointer user_data)
{
  Track *track1;
  Track *track2;
  gint column;
  GtkSortType order;
  gint result;

  gtk_tree_model_get (model, a, READOUT_COL, &track1, -1);
  gtk_tree_model_get (model, b, READOUT_COL, &track2, -1);
  if(gtk_tree_sortable_get_sort_column_id (GTK_TREE_SORTABLE (model),
					   &column, &order) == FALSE)
      return 0;

  result = tm_data_compare (track1, track2, column);
  return result;
}


/* set/read the counter used to remember how often the sort column has
   been clicked.
   @inc: negative: reset counter to 0
   @inc: positive or zero : add to counter
   return value: new value of the counter */
gint tm_sort_counter (gint inc)
{
    static gint cnt = 0;

    if (inc <0)
    {
	cnt = 0;
    }
    else
    {
	cnt += inc;
    }   
    return cnt;
}


/* Redisplays the tracks in the track view according to the order
 * stored in the sort tab view. This only works if the track view is
 * not sorted --> skip if sorted */

void tm_adopt_order_in_sorttab (void)
{
    if (prefs_get_int("tm_sort") == SORT_NONE)
    {
	GList *gl, *tracks = NULL;

	/* retrieve the currently displayed tracks (non ordered) from
	   the last sort tab or from the selected playlist if no sort
	   tabs are being used */
	tm_remove_all_tracks ();
	tracks = display_get_selected_members (prefs_get_int("sort_tab_num")-1);
	for (gl=tracks; gl; gl=gl->next)
	    tm_add_track_to_track_model ((Track *)gl->data, NULL);
    }
}


/* redisplay the contents of the track view in it's unsorted order */
static void tm_unsort (void)
{
    if (track_treeview)
    {
	GtkTreeModel *model= gtk_tree_view_get_model (track_treeview);

	if (GTK_IS_TREE_MODEL_FILTER (model))
	{
	    model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER(model));
	}

	prefs_set_int("tm_sort", SORT_NONE);
	if (!BROKEN_GTK_TREE_SORT)
	{
/* no need to comment this out -- searching still works, but for lack
   of a ctrl-g only the first occurence will be found */
/*	    gtk_tree_view_set_enable_search (GTK_TREE_VIEW
 * (track_treeview), FALSE);*/ 
	    gtk_tree_sortable_set_sort_column_id
		(GTK_TREE_SORTABLE (model),
		 GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID,
		 GTK_SORT_ASCENDING);
	    tm_adopt_order_in_sorttab ();
	}
	else
	{
	    gtkpod_warning (_("Cannot unsort track view because of a bug in the GTK lib you are using (%d.%d.%d < 2.5.4). Once you sort the track view, you cannot go back to the unsorted state.\n\n"), gtk_major_version, gtk_minor_version, gtk_micro_version);
	}
	tm_sort_counter (-1);
    }
}


static void tm_set_search_column (TM_item newcol)
{
/*     printf ("track_treeview: %p, col: %d\n", track_treeview, newcol); */
    g_return_if_fail (track_treeview);

    gtk_tree_view_set_search_column (GTK_TREE_VIEW (track_treeview),
				       newcol);
    switch (newcol)
    {
    case TM_COLUMN_TITLE:
    case TM_COLUMN_ALBUM:
    case TM_COLUMN_GENRE:
    case TM_COLUMN_COMPOSER:
    case TM_COLUMN_COMMENT:
    case TM_COLUMN_FILETYPE:
    case TM_COLUMN_GROUPING:
    case TM_COLUMN_ARTIST:
    case TM_COLUMN_CATEGORY:
    case TM_COLUMN_DESCRIPTION:
    case TM_COLUMN_PODCASTURL:
    case TM_COLUMN_PODCASTRSS:
    case TM_COLUMN_SUBTITLE:
    case TM_COLUMN_PC_PATH:
    case TM_COLUMN_YEAR:
    case TM_COLUMN_IPOD_PATH:
    case TM_COLUMN_COMPILATION:
    case TM_COLUMN_THUMB_PATH:
    case TM_COLUMN_TV_SHOW:
    case TM_COLUMN_TV_EPISODE:
    case TM_COLUMN_TV_NETWORK:
    case TM_COLUMN_ALBUMARTIST:
    case TM_COLUMN_SORT_ARTIST:
    case TM_COLUMN_SORT_TITLE:
    case TM_COLUMN_SORT_ALBUM:
    case TM_COLUMN_SORT_ALBUMARTIST:
    case TM_COLUMN_SORT_COMPOSER:
    case TM_COLUMN_SORT_TVSHOW:
	gtk_tree_view_set_enable_search (GTK_TREE_VIEW (track_treeview), TRUE);
	break;
    case TM_COLUMN_TRACK_NR:
    case TM_COLUMN_IPOD_ID:
    case TM_COLUMN_TRANSFERRED:
    case TM_COLUMN_SIZE:
    case TM_COLUMN_TRACKLEN:
    case TM_COLUMN_BITRATE:
    case TM_COLUMN_PLAYCOUNT:
    case TM_COLUMN_RATING:
    case TM_COLUMN_TIME_PLAYED:
    case TM_COLUMN_TIME_MODIFIED:
    case TM_COLUMN_VOLUME:
    case TM_COLUMN_CD_NR:
    case TM_COLUMN_TIME_ADDED:
    case TM_COLUMN_SOUNDCHECK:
    case TM_COLUMN_SAMPLERATE:
    case TM_COLUMN_BPM:
    case TM_COLUMN_TIME_RELEASED:
    case TM_COLUMN_MEDIA_TYPE:
    case TM_COLUMN_SEASON_NR:
    case TM_COLUMN_EPISODE_NR:
    case TM_COLUMN_LYRICS:
    case TM_NUM_COLUMNS:
	gtk_tree_view_set_enable_search (GTK_TREE_VIEW (track_treeview), FALSE);
	break;
    }
    prefs_set_int (TM_PREFS_SEARCH_COLUMN, newcol);
}


/* This is called before when changing the sort order or the sort
   column, and before doing the sorting */
static void tm_sort_column_changed (GtkTreeSortable *ts,
				    gpointer user_data)
{
    static gint lastcol = -1; /* which column was sorted last time? */
    gchar *buf;
    gint newcol;
    GtkSortType order;
    GList *tracks, *gl;
    gint32 i, inc;

    gtk_tree_sortable_get_sort_column_id (ts, &newcol, &order);

/*     printf ("scc -- col: %d, order: %d\n", newcol, order);  */

    /* set compare function for strings (to speed up sorting) */
    buf = g_strdup_printf ("sort_ign_field_%d", TM_to_T (newcol));
    if (prefs_get_int (buf))
	string_compare_func = compare_string_fuzzy;
    else
	string_compare_func = compare_string;
    g_free (buf);

    /* don't do anything if no sort column is set */
    if (newcol == -2)
    {
	lastcol = newcol;
	return;
    }

    if (newcol != lastcol)
    {
	tm_sort_counter (-1);
	lastcol = newcol;
    }

    if (tm_sort_counter (1) >= 3)
    { /* after clicking three times, reset sort order! */
	tm_unsort ();  /* also resets sort counter */
    }
    else
    {
	prefs_set_int("tm_sort", order);
    }
    prefs_set_int("tm_sortcol", newcol);

    tm_set_search_column (newcol);

    if(prefs_get_int("tm_autostore"))  tm_rows_reordered ();
    sort_window_update ();

    /* stable sorting: index original order */
    tracks = tm_get_all_tracks ();
    /* make numbering ascending or decending depending on sort order
       (then we don't have to worry about the sort order when doing
       the comparison in tm_data_compare_func() */
    if (order == GTK_SORT_ASCENDING)
    {
	i=0;
	inc = 1;
    }
    else
    {
	i=-1;
	inc = -1;
    }
    for (gl=tracks; gl; gl=gl->next)
    {
	ExtraTrackData *etr;
	Track *tr = gl->data;
	g_return_if_fail (tr);
	etr = tr->userdata;
	g_return_if_fail (etr);
	etr->sortindex = i;
	i+=inc;
    }
    g_list_free (tracks);
}


void tm_sort (TM_item col, GtkSortType order)
{
    if (track_treeview)
    {
	GtkTreeModel *model= gtk_tree_view_get_model (track_treeview);

	if (GTK_IS_TREE_MODEL_FILTER (model))
	{
	    model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER(model));
	}

	if (order != SORT_NONE)
	{
	    gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model),
						  col, order);
	}
	else
	{ /* only unsort if treeview is sorted */
	    gint column;
	    GtkSortType order;
	    if (gtk_tree_sortable_get_sort_column_id
		(GTK_TREE_SORTABLE (model), &column, &order))
	    {
		/* column == -2 actually is not defined, but it means
		   that the model is unsorted. The sortable interface
		   is badly implemented in gtk 2.4 */
		if (column != -2)
		    tm_unsort ();
	    }
	}
    }
}





/* Adds the columns to our track_treeview */
static GtkTreeViewColumn *tm_add_column (TM_item tm_item, gint pos)
{
	GtkTreeModel *model = gtk_tree_view_get_model (track_treeview);
	GtkTreeViewColumn *col = NULL;
	const gchar *text;
	GtkCellRenderer *renderer = NULL;  /* default */
	GtkTooltips *tt;

	g_return_val_if_fail (gtkpod_window, NULL);
	tt = g_object_get_data (G_OBJECT (gtkpod_window), "main_tooltips");
	g_return_val_if_fail (tt, NULL);

	g_return_val_if_fail (tm_item >= 0, NULL);
	g_return_val_if_fail (tm_item < TM_NUM_COLUMNS, NULL);

	text = gettext (get_tm_string (tm_item));

	g_return_val_if_fail (text, NULL);

	col = gtk_tree_view_column_new ();

	switch (tm_item)
	{
	case TM_COLUMN_TITLE:
	  /* Add additional toggle box for 'checked' property */
	  renderer = gtk_cell_renderer_toggle_new ();
	  g_object_set_data (G_OBJECT (renderer), "column",
			 (gint *)tm_item);
	  g_signal_connect (G_OBJECT (renderer), "toggled",
			G_CALLBACK (tm_cell_toggled), model);
	  gtk_tree_view_column_pack_start (col, renderer, FALSE);
	  gtk_tree_view_column_set_cell_data_func (col, renderer,
						   tm_cell_data_func_toggle,
						   NULL, NULL);
	  renderer = NULL;
	  break;
	case TM_COLUMN_ARTIST:
	case TM_COLUMN_ALBUM:
	case TM_COLUMN_GENRE:
	case TM_COLUMN_COMPOSER:
	case TM_COLUMN_COMMENT:
	case TM_COLUMN_FILETYPE:
	case TM_COLUMN_GROUPING:
	case TM_COLUMN_BITRATE:
	case TM_COLUMN_SAMPLERATE:
	case TM_COLUMN_BPM:
	case TM_COLUMN_CATEGORY:
	case TM_COLUMN_DESCRIPTION:
	case TM_COLUMN_PODCASTURL:
	case TM_COLUMN_PODCASTRSS:
	case TM_COLUMN_SUBTITLE:
	case TM_COLUMN_PC_PATH:
	case TM_COLUMN_IPOD_PATH:
	case TM_COLUMN_THUMB_PATH:
	case TM_COLUMN_SIZE:
	case TM_COLUMN_MEDIA_TYPE:
	case TM_COLUMN_TV_SHOW:
	case TM_COLUMN_TV_EPISODE:
	case TM_COLUMN_TV_NETWORK:
	case TM_COLUMN_SEASON_NR:
	case TM_COLUMN_EPISODE_NR:
	case TM_COLUMN_ALBUMARTIST:
	case TM_COLUMN_SORT_ARTIST:
	case TM_COLUMN_SORT_TITLE:
	case TM_COLUMN_SORT_ALBUM:
	case TM_COLUMN_SORT_ALBUMARTIST:
	case TM_COLUMN_SORT_COMPOSER:
	case TM_COLUMN_SORT_TVSHOW:
	  break;
	/* for some column names we want to use shorter alternatives to
	 get_tm_string() */
	case TM_COLUMN_RATING:
	  text = _("Rtng");
	  break;
	case TM_COLUMN_TRACK_NR:
	  text = _("#");
	  break;
	case TM_COLUMN_CD_NR:
	  text = _("CD");
	  break;
	case TM_COLUMN_IPOD_ID:
	  text = _("ID");
	  break;
	case TM_COLUMN_TRANSFERRED:
	  text = _("Trnsfrd");
	  renderer = gtk_cell_renderer_toggle_new ();
	  break;
	case TM_COLUMN_LYRICS:
	  renderer = gtk_cell_renderer_toggle_new ();
	  g_signal_connect (G_OBJECT (renderer), "toggled",
			G_CALLBACK (tm_cell_toggled), model);
	  break;
	case TM_COLUMN_COMPILATION:
	  text = _("Cmpl");
	  renderer = gtk_cell_renderer_toggle_new ();
	  g_signal_connect (G_OBJECT (renderer), "toggled",
			G_CALLBACK (tm_cell_toggled), model);
	  break;
	case TM_COLUMN_TRACKLEN:
	  text = _("Time");
	  break;
	case TM_COLUMN_PLAYCOUNT:
	  text = _("Plycnt");
	  break;
	case TM_COLUMN_TIME_PLAYED:
	  text = _("Played");
	  break;
	case TM_COLUMN_TIME_MODIFIED:
	  text = _("Modified");
	  break;
	case TM_COLUMN_TIME_ADDED:
	  text = _("Added");
	  break;
	case TM_COLUMN_TIME_RELEASED:
	  text = _("Released");
	  break;
	case TM_COLUMN_YEAR:
	  text = _("Year");
	  break;
	case TM_COLUMN_VOLUME:
	  text = _("Vol.");
	  break;
	case TM_COLUMN_SOUNDCHECK:
	  text = _("Sndchk.");
	  break;
	case TM_NUM_COLUMNS:
	  g_return_val_if_reached (NULL);
	  break;
	}

	if (!renderer)
	{
		if (tm_item == TM_COLUMN_RATING)
		{
			renderer = rb_cell_renderer_rating_new ();
			g_signal_connect (G_OBJECT (renderer), "rated",
							  G_CALLBACK (tm_rating_edited), NULL);
		}
		else
		{
			/* text renderer -- editable/not editable is done in
			   tm_cell_data_func() */
			renderer = gtk_cell_renderer_text_new ();
			g_signal_connect (G_OBJECT (renderer), "edited",
							  G_CALLBACK (tm_cell_edited), model);
		}
	}

	g_object_set_data (G_OBJECT (renderer), "column",
			 (gint *)tm_item);

	gtk_tree_view_column_set_title (col, text);
	gtk_tree_view_column_pack_start (col, renderer, FALSE);
	gtk_tree_view_column_set_cell_data_func (col, renderer,
					   tm_cell_data_func, NULL, NULL);
	gtk_tree_view_column_set_sort_column_id (col, tm_item);
	gtk_tree_view_column_set_resizable (col, TRUE);
	gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_FIXED);
	gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (model), tm_item,
				   tm_data_compare_func, NULL, NULL);
	gtk_tree_view_column_set_reorderable (col, TRUE);
	
	gtk_tree_view_insert_column (track_treeview, col, pos);
	tm_columns[tm_item] = col;
	
	if (pos != -1)
	{
	  gtk_tree_view_column_set_visible (col,
					prefs_get_int_index("col_visible", tm_item));
	}
	if (get_tm_tooltip (tm_item))
	  gtk_tooltips_set_tip (tt, col->button, 
				gettext (get_tm_tooltip (tm_item)),
				NULL);
	return col;
}


/* Adds the columns to our track_treeview */
static void tm_add_columns (void)
{
    gint i;

    for (i = 0 ; i < TM_NUM_COLUMNS; ++i)
    {
        tm_add_column (prefs_get_int_index("col_order", i), -1);
    }
    
    tm_show_preferred_columns ();
}

static void tm_select_current_position (gint x, gint y)
{
    if (track_treeview)
    {
	GtkTreePath *path;

	gtk_tree_view_get_path_at_pos (track_treeview,
				       x, y, &path, NULL, NULL, NULL);
	if (path)
	{
	    GtkTreeSelection *ts = gtk_tree_view_get_selection (track_treeview);
	    gtk_tree_selection_select_path (ts, path);
	    gtk_tree_path_free (path);
	}
    }
}

static gboolean
tm_button_press_event(GtkWidget *w, GdkEventButton *e, gpointer data)
{
    if(w && e)
    {
	switch(e->button)
	{
	case 1:
/*
	    printf ("Pressed in cell %d (%f/%f)\n\n",
		    tree_view_get_cell_from_pos(GTK_TREE_VIEW(w),
						(guint)e->x, (guint)e->y, NULL),
		    e->x, e->y);
*/
	    break;
	case 3:
	    tm_select_current_position (e->x, e->y);
	    tm_context_menu_init ();
	    return TRUE;
	default:
	    break;
	}
    }
    return(FALSE);
}

/* called when the track selection changes */
static void
tm_selection_changed_event(GtkTreeSelection *selection, gpointer data)
{
    GtkTreeView *treeview = gtk_tree_selection_get_tree_view (selection);
    GtkTreePath *path;
    GtkTreeViewColumn *column;
    TM_item col_id;

    gtk_tree_view_get_cursor (treeview, &path, &column);
    if (path)
    {
	col_id = tm_lookup_col_id (column);
	if (col_id != -1)  tm_set_search_column (col_id);
    }
    info_update_track_view ();
    
    /* update the coverart display */
    GList *selected = display_get_selection (prefs_get_int("sort_tab_num"));
    if (selected != NULL)
    {
    	Track *track = selected->data;
    	if(track != NULL)
    		coverart_select_cover (track);
    }
}


/* Create tracks treeview */
void tm_create_treeview (void)
{
    GtkTreeModel *model = NULL;
    GtkWidget *track_window = gtkpod_xml_get_widget (main_window_xml, "track_window");
    GtkTreeSelection *select;
    gint col;
    GtkWidget *stv = gtk_tree_view_new ();

    /* create tree view */
    if (track_treeview)
    {   /* delete old tree view */
      model = gtk_tree_view_get_model (track_treeview);
      /* FIXME: how to delete model? */
      gtk_widget_destroy (GTK_WIDGET (track_treeview));
    }
    track_treeview = GTK_TREE_VIEW (stv);
    gtk_widget_show (stv);
    gtk_container_add (GTK_CONTAINER (track_window), stv);
    /* create model (we only need one column for the model -- only a
    * pointer to the track has to be stored) */
    model = GTK_TREE_MODEL (
      gtk_list_store_new (1, G_TYPE_POINTER));
    gtk_tree_view_set_model (track_treeview, GTK_TREE_MODEL (model));
    gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (track_treeview), TRUE);
    gtk_tree_view_set_search_equal_func (GTK_TREE_VIEW (track_treeview), 
                       tm_search_equal_func,
                       NULL,
                       NULL);
    select = gtk_tree_view_get_selection (track_treeview);
    gtk_tree_selection_set_mode (select,
                   GTK_SELECTION_MULTIPLE);
    g_signal_connect (G_OBJECT (select) , "changed",
            G_CALLBACK (tm_selection_changed_event),
            NULL);

    tm_add_columns ();

    /*   gtk_drag_source_set (GTK_WIDGET (track_treeview), GDK_BUTTON1_MASK, */
    /* 		       tm_drag_types, TGNR (tm_drag_types), */
    /* 		       GDK_ACTION_COPY|GDK_ACTION_MOVE); */
    /*   gtk_tree_view_enable_model_drag_dest(track_treeview, tm_drop_types, */
    /* 				       TGNR (tm_drop_types), */
    /* 				       GDK_ACTION_COPY|GDK_ACTION_MOVE); */
    /*   /\* need the gtk_drag_dest_set() with no actions ("0") so that the */
    /*      data_received callback gets the correct info value. This is most */
    /*      likely a bug... *\/ */
    /*   gtk_drag_dest_set_target_list (GTK_WIDGET (track_treeview), */
    /* 				 gtk_target_list_new (tm_drop_types, */
    /* 						      TGNR (tm_drop_types))); */


    gtk_drag_source_set (GTK_WIDGET (track_treeview),
               GDK_BUTTON1_MASK,
               tm_drag_types, TGNR (tm_drag_types),
               GDK_ACTION_COPY|GDK_ACTION_MOVE);
    gtk_drag_dest_set (GTK_WIDGET (track_treeview),
             0,
             tm_drop_types, TGNR (tm_drop_types),
             GDK_ACTION_COPY|GDK_ACTION_MOVE);


    g_signal_connect ((gpointer) track_treeview, "drag-begin",
            G_CALLBACK (tm_drag_begin),
            NULL);

    g_signal_connect ((gpointer) track_treeview, "drag-data-delete",
            G_CALLBACK (tm_drag_data_delete),
            NULL);

    g_signal_connect ((gpointer) track_treeview, "drag-data-get",
            G_CALLBACK (tm_drag_data_get),
            NULL);

    g_signal_connect ((gpointer) track_treeview, "drag-data-received",
            G_CALLBACK (tm_drag_data_received),
            NULL);

    g_signal_connect ((gpointer) track_treeview, "drag-drop",
            G_CALLBACK (tm_drag_drop),
            NULL);

    g_signal_connect ((gpointer) track_treeview, "drag-end",
            G_CALLBACK (tm_drag_end),
            NULL);

    g_signal_connect ((gpointer) track_treeview, "drag-leave",
            G_CALLBACK (tm_drag_leave),
            NULL);

    g_signal_connect ((gpointer) track_treeview, "drag-motion",
            G_CALLBACK (tm_drag_motion),
            NULL);

    g_signal_connect_after ((gpointer) stv, "key_release_event",
              G_CALLBACK (on_track_treeview_key_release_event),
              NULL);
    g_signal_connect ((gpointer) track_treeview, "button-press-event",
            G_CALLBACK (tm_button_press_event),
            NULL);
    g_signal_connect (G_OBJECT (model), "sort-column-changed",
            G_CALLBACK (tm_sort_column_changed),
            (gpointer)0);

    /* set correct column for typeahead */
    if (prefs_get_int_value (TM_PREFS_SEARCH_COLUMN, &col))
    {
      tm_set_search_column (col);
    }
    else
    {   /* reasonable default */
      tm_set_search_column (TM_COLUMN_TITLE);
    }
}


void
tm_show_preferred_columns (void)
{
    gint i;
    gboolean horizontal_scrollbar = prefs_get_int ("horizontal_scrollbar");
    GtkScrolledWindow *scroller = GTK_SCROLLED_WINDOW (gtkpod_xml_get_widget (main_window_xml,
                                                                              "track_window"));
    
    if (horizontal_scrollbar)
        gtk_scrolled_window_set_policy (scroller, GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
    else
        gtk_scrolled_window_set_policy (scroller, GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);

    for (i = 0; i < TM_NUM_COLUMNS; ++i)
    {
	TM_item tm_item = prefs_get_int_index ("col_order", i);
	GtkTreeViewColumn *tvc = gtk_tree_view_get_column (track_treeview, i);
	gboolean visible = prefs_get_int_index ("col_visible", tm_item);
	gint col_width;

        gtk_tree_view_column_set_visible (tvc, visible);

	col_width = prefs_get_int_index ("tm_col_width", tm_item);

	if (col_width == 0)
	    col_width = 80;

        if (horizontal_scrollbar)
        {
	    gtk_tree_view_column_set_fixed_width (tvc, col_width);
            gtk_tree_view_column_set_min_width (tvc, -1);
            gtk_tree_view_column_set_expand (tvc, FALSE);
        }
        else
        {
            switch (tm_item)
            {
            case TM_COLUMN_TITLE:
            case TM_COLUMN_ARTIST:
            case TM_COLUMN_ALBUM:
            case TM_COLUMN_GENRE:
            case TM_COLUMN_COMPOSER:
            case TM_COLUMN_COMMENT:
            case TM_COLUMN_CATEGORY:
            case TM_COLUMN_DESCRIPTION:
            case TM_COLUMN_PODCASTURL:
            case TM_COLUMN_PODCASTRSS:
            case TM_COLUMN_SUBTITLE:
            case TM_COLUMN_PC_PATH:
            case TM_COLUMN_IPOD_PATH:
            case TM_COLUMN_THUMB_PATH:
            case TM_COLUMN_TV_SHOW:
            case TM_COLUMN_TV_EPISODE:
            case TM_COLUMN_TV_NETWORK:
            case TM_COLUMN_ALBUMARTIST:
                gtk_tree_view_column_set_min_width (tvc, 0);
                gtk_tree_view_column_set_expand (tvc, TRUE);
                break;
            default:
                gtk_tree_view_column_set_min_width (tvc, 80);
                gtk_tree_view_column_set_fixed_width (tvc, col_width);
                gtk_tree_view_column_set_expand (tvc, FALSE);
                break;
            }
        }
    }
}


/* update the cfg structure (preferences) with the current sizes /
   positions (called by display_update_default_sizes():
   column widths of track model */
void tm_update_default_sizes (void)
{
    TM_item tm_item;
    GtkTreeViewColumn *col;
    gint col_width;

    /* column widths */
    for (tm_item=0; tm_item<TM_NUM_COLUMNS; ++tm_item)
    {
	col = tm_columns [tm_item];
	if (col)
	{
	    col_width = gtk_tree_view_column_get_width (col);
    
	    if (col_width > 0)
	    {   /* columns not displayed seem to be 0 pixels wide --
		   only change the visible columns */
		prefs_set_int_index ("tm_col_width", tm_item, col_width);
	    }
	}
    }
}


/* get the TM_ITEM column id for @column. Returns -1 if column could
   not be found */
static TM_item tm_lookup_col_id (GtkTreeViewColumn *column)
{
    gint i;

    if (column)
    {
	for (i=0; i<TM_NUM_COLUMNS; ++i)
	{
	    if (column == tm_columns[i]) return i;
	}
    }
    return -1;
}


/* Compare function to avoid sorting */
static gint tm_nosort_comp (GtkTreeModel *model,
			    GtkTreeIter *a,
			    GtkTreeIter *b,
			    gpointer user_data)
{
    return 0;
}


/* Disable sorting of the view during lengthy updates. */
/* @enable: TRUE: enable, FALSE: disable */
void tm_enable_disable_view_sort (gboolean enable)
{
    static gint disable_count = 0;

    if (enable)
    {
	disable_count--;
	if (disable_count < 0)
	    fprintf (stderr, "Programming error: disable_count < 0\n");
	if (disable_count == 0 && track_treeview)
	{
	    if (prefs_get_int("tm_sort") != SORT_NONE)
	    {
		/* Re-enable sorting */
		GtkTreeModel *model = gtk_tree_view_get_model (track_treeview);

		if (GTK_IS_TREE_MODEL_FILTER (model))
		{
		    model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER(model));
		}

		if (BROKEN_GTK_TREE_SORT)
		{
		    gtk_tree_sortable_set_sort_func (
			GTK_TREE_SORTABLE (model),
			prefs_get_int("tm_sortcol"),
			tm_data_compare_func, NULL, NULL);
		}
		else
		{
		    gtk_tree_sortable_set_sort_column_id (
			GTK_TREE_SORTABLE (model),
			prefs_get_int("tm_sortcol"),
			prefs_get_int("tm_sort"));
		}
	    }
	}
    }
    else
    {
	if (disable_count == 0 && track_treeview)
	{
	    if (prefs_get_int("tm_sort") != SORT_NONE)
	    {
		/* Disable sorting */
		GtkTreeModel *model = gtk_tree_view_get_model (track_treeview);

		if (GTK_IS_TREE_MODEL_FILTER (model))
		{
		    model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER(model));
		}

		if (BROKEN_GTK_TREE_SORT)
		{
		    gtk_tree_sortable_set_sort_func (
			GTK_TREE_SORTABLE (model),
			prefs_get_int("tm_sortcol"),
			tm_nosort_comp, NULL, NULL);
		}
		else
		{
		    gtk_tree_sortable_set_sort_column_id (
			GTK_TREE_SORTABLE (model),
			GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID,
			prefs_get_int("tm_sort"));
		}
	    }
	}
	disable_count++;
    }
}



/* Callback for adding tracks within tm_add_filelist */
void tm_addtrackfunc (Playlist *plitem, Track *track, gpointer data)
{
    struct asf_data *asf = (struct asf_data *)data;
    GtkTreeModel *model;
    GtkTreeIter new_iter;

    model = gtk_tree_view_get_model (track_treeview);

/*    printf("plitem: %p\n", plitem);
      if (plitem) printf("plitem->type: %d\n", plitem->type);*/
    /* add to playlist but not to the display */
    gp_playlist_add_track (plitem, track, FALSE);

    /* create new iter in track view */
    switch (asf->pos)
    {
    case GTK_TREE_VIEW_DROP_INTO_OR_BEFORE:
    case GTK_TREE_VIEW_DROP_INTO_OR_AFTER:
    case GTK_TREE_VIEW_DROP_AFTER:
	gtk_list_store_insert_after (get_model_as_store (model),
				     &new_iter, asf->to_iter);
	break;
    case GTK_TREE_VIEW_DROP_BEFORE:
	gtk_list_store_insert_before (get_model_as_store (model),
				      &new_iter, asf->to_iter);
	break;
    }
    /* set the iter */
    tm_add_track_to_track_model (track, &new_iter);
}


/* DND: insert a list of files before/after @path
   @data: list of files
   @path: where to drop (NULL to drop at the end)
   @pos:  before/after... (ignored if @path is NULL)
*/
gboolean tm_add_filelist (gchar *data,
			  GtkTreePath *path,
			  GtkTreeViewDropPosition pos)
{
    GtkTreeModel *model;
    gchar *buf = NULL, *use_data;
    gchar **files, **filep;
    Playlist *current_playlist = pm_get_selected_playlist ();

    g_return_val_if_fail (data, FALSE);
    g_return_val_if_fail (*data, FALSE);
    g_return_val_if_fail (current_playlist, FALSE);

    model = gtk_tree_view_get_model (track_treeview);
    g_return_val_if_fail (model, FALSE);
    if (path)
    {
    }

    if (pos != GTK_TREE_VIEW_DROP_BEFORE)
    {   /* need to reverse the list of files -- otherwise wie add them
	 * in reverse order */
	/* split the path list into individual strings */
	gint len = strlen (data) + 1;
	files = g_strsplit (data, "\n", -1);
	filep = files;
	/* find the end of the list */
	while (*filep) ++filep;
	/* reserve memory */
	buf = g_malloc0 (len);
	/* reverse the list */
	while (filep != files)
	{
	    --filep;
	    g_strlcat (buf, *filep, len);
	    g_strlcat (buf, "\n", len);
	}
	g_strfreev (files);
	use_data = buf;
    }
    else
    {
	use_data = data;
    }

/*     printf("filelist: (%s) -> (%s)\n", data, use_data); */
    /* initialize add-track-struct */
    if (path)
    {   /* add where specified (@path/@pos) */
        GtkTreeIter to_iter;
        GtkTreeIter temp;
            
        struct asf_data asf;
        if (!gtk_tree_model_get_iter (model, &to_iter, path))
            g_return_val_if_reached (FALSE);
            
        convert_iter (model, &to_iter, &temp);
        asf.to_iter = &temp;
        asf.pos = pos;
        add_text_plain_to_playlist (current_playlist->itdb,
                        current_playlist, use_data, 0,
                        tm_addtrackfunc, &asf);
    }
    else
    {   /* add to the end */
	add_text_plain_to_playlist (current_playlist->itdb,
				    current_playlist, use_data, 0,
				    NULL, NULL);
    }
    
/*    update_model_view (model); -- not needed */
    tm_rows_reordered ();
    C_FREE (buf);
    return TRUE;
}