~awn-core/awn/trunk-rewrite

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
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
/*
 * Copyright (C) 2008 Neil Jagdish Patel <njpatel@gmail.com>
 * Copyright (C) 2009 Rodney Cryderman <rcryderman@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as 
 * published by the Free Software Foundation.
 * 
 * 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, see <http://www.gnu.org/licenses/>.
 *
 * Authored by Neil Jagdish Patel <njpatel@gmail.com>
 *             Hannes Verschore <hv1989@gmail.com>
 *             Rodney Cryderman <rcryderman@gmail.com>
 */

#include <stdio.h>
#include <string.h>

#include <gtk/gtk.h>
#include <gdk/gdkx.h>
#include <glib/gstdio.h>

#include <libwnck/libwnck.h>

#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-bindings.h>

#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>


#undef G_DISABLE_SINGLE_INCLUDES
#include <glibtop/procargs.h>

#include "libawn/gseal-transition.h"

#include "task-manager.h"
#include "task-manager-glue.h"

#include "task-drag-indicator.h"
#include "task-icon.h"
#include "task-settings.h"
#include "xutils.h"
#include "util.h"

#include <X11/extensions/shape.h>

G_DEFINE_TYPE (TaskManager, task_manager, AWN_TYPE_APPLET)

#define TASK_MANAGER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj),\
  TASK_TYPE_MANAGER, \
  TaskManagerPrivate))

//#define DEBUG 1

#define DESKTOP_CACHE_FILENAME ".desktop_cache"

static GQuark win_quark = 0;

struct _TaskManagerPrivate
{
  DesktopAgnosticConfigClient *client;
  TaskSettings    *settings;
  WnckScreen      *screen;

  /*Used by Intellihide*/
  guint           autohide_cookie;  
  GdkWindow       *awn_gdk_window;
  GdkRegion       * awn_gdk_region;
  
  guint           attention_cookie;
  guint           attention_source;
  
  /* Dragging properties */
  TaskIcon          *dragged_icon;
  TaskDragIndicator *drag_indicator;
  gint               drag_timeout;

  /* This is what the icons are packed into */
  GtkWidget  *box;
  GSList     *icons;
  GSList     *windows;
  /*list of window res_names that are to be hidden*/
  GList      *hidden_list;
  
  GHashTable *win_table;
  GHashTable *desktops_table;
  /*
   Used during grouping configuration changes for optimization purposes
   */
  GList *grouping_list;
  /* Properties */
  GValueArray *launcher_paths;
  gboolean     show_all_windows;
  gboolean     only_show_launchers;
  gboolean     drag_and_drop;
  gboolean     grouping;
  gboolean     intellihide;
  gint         intellihide_mode;
  gint         match_strength;
  gint         attention_autohide_timer;
  
  guint        attention_required_reminder_id;
  gint         attention_required_reminder;
};

typedef struct
{
  WnckWindow * window;
  TaskManager * manager;
} WindowOpenTimeoutData;

enum
{
  INTELLIHIDE_WORKSPACE,
  INTELLIHIDE_APP,
  INTELLIHIDE_GROUP  
};

enum
{
  PROP_0,

  PROP_SHOW_ALL_WORKSPACES,
  PROP_ONLY_SHOW_LAUNCHERS,
  PROP_LAUNCHER_PATHS,
  PROP_DRAG_AND_DROP,
  PROP_GROUPING,
  PROP_MATCH_STRENGTH,
  PROP_INTELLIHIDE,
  PROP_INTELLIHIDE_MODE,
  PROP_ATTENTION_AUTOHIDE_TIMER,
  PROP_ATTENTION_REQUIRED_REMINDER,
  PROP_ATTENTION_REQUIED_REMINDER_ID
};


enum
{
  GROUPING_CHANGED,
  
  LAST_SIGNAL
};
static guint32 _taskman_signals[LAST_SIGNAL] = { 0 };

/* Forwards */
static void update_icon_visible         (TaskManager   *manager, 
                                         TaskIcon      *icon);
static void on_icon_visible_changed     (TaskManager   *manager, 
                                         TaskIcon      *icon);
static void on_icon_effects_ends        (TaskIcon      *icon,
                                         AwnEffect      effect,
                                         AwnEffects    *instance);
static void on_window_opened            (WnckScreen    *screen, 
                                         WnckWindow    *window,
                                         TaskManager   *manager);
static void on_active_window_changed    (WnckScreen    *screen, 
                                         WnckWindow    *old_window,
                                         TaskManager   *manager);
static void task_manager_set_show_all_windows    (TaskManager *manager,
                                                  gboolean     show_all);
static void task_manager_set_show_only_launchers (TaskManager *manager, 
                                                  gboolean     show_only);
static void task_manager_refresh_launcher_paths  (TaskManager *manager,
                                                  GValueArray *list);
static void task_manager_set_drag_and_drop (TaskManager *manager, 
                                            gboolean     drag_and_drop);

static void task_manager_set_grouping (TaskManager *manager, 
                                            gboolean     grouping);

static void task_manager_set_match_strength (TaskManager *manager, 
                                             gint     drag_and_drop);

static void task_manager_position_changed (AwnApplet *applet, 
                                           GtkPositionType position);
static void task_manager_size_changed   (AwnApplet *applet,
                                         gint       size);
static void task_manager_origin_changed (AwnApplet *applet,
                                         GdkRectangle  *rect,
                                         gpointer       data);

static void task_manager_dispose (GObject *object);

static void task_manager_active_window_changed_cb (WnckScreen *screen,
                                                   WnckWindow *previous_window,
                                                   TaskManager * manager);
static void task_manager_active_workspace_changed_cb (WnckScreen    *screen,
                                                      WnckWorkspace *previous_space,
                                                      TaskManager * manager);
static void task_manager_win_geom_changed_cb (WnckWindow *window, 
                                              TaskManager * manager);
static void task_manager_win_state_changed_cb (WnckWindow * window,
                                               WnckWindowState changed_mask,
                                               WnckWindowState new_state,
                                               TaskManager * manager);

static gboolean _attention_required_reminder_cb (TaskManager * manager);

typedef enum 
{
  TASK_MANAGER_ERROR_UNSUPPORTED_WINDOW_TYPE,
  TASK_MANAGER_ERROR_NO_WINDOW_MATCH
} TaskManagerError;

static GQuark task_manager_error_quark   (void);

static GType task_manager_error_get_type (void);

/* D&D Forwards */
static void _drag_dest_motion (TaskManager *manager,
                               gint x,
                               gint y,
                               GtkWidget *icon);
static void _drag_dest_leave   (TaskManager *manager,
                               GtkWidget *icon);
static void _drag_source_fail  (TaskManager *manager,
                                GtkWidget *icon);
static void _drag_source_begin (TaskManager *manager, 
                                GtkWidget *icon);
static void _drag_source_end   (TaskManager *manager, 
                                GtkWidget *icon);
//static gboolean drag_leaves_task_manager (TaskManager *manager);
static void _drag_add_signals (TaskManager *manager, 
                               GtkWidget *icon);
static void _drag_remove_signals (TaskManager *manager, 
                                  GtkWidget *icon);

/* GObject stuff */
static void
task_manager_get_property (GObject    *object,
                           guint       prop_id,
                           GValue     *value,
                           GParamSpec *pspec)
{
  TaskManager *manager = TASK_MANAGER (object);

  switch (prop_id)
  {
    case PROP_SHOW_ALL_WORKSPACES:
      g_value_set_boolean (value, manager->priv->show_all_windows); 
      break;

    case PROP_ONLY_SHOW_LAUNCHERS:
      g_value_set_boolean (value, manager->priv->only_show_launchers); 
      break;

    case PROP_LAUNCHER_PATHS:
      g_value_set_boxed (value, manager->priv->launcher_paths);
      break;
    case PROP_DRAG_AND_DROP:
      g_value_set_boolean (value, manager->priv->drag_and_drop);
      break;

    case PROP_GROUPING:
      g_value_set_boolean (value, manager->priv->grouping);
      break;

    case PROP_INTELLIHIDE:
      g_value_set_boolean (value, manager->priv->intellihide);
      break;

    case PROP_INTELLIHIDE_MODE:
      g_value_set_int (value, manager->priv->intellihide_mode);
      break;

    case PROP_MATCH_STRENGTH:
      g_value_set_int (value, manager->priv->match_strength);
      break;

    case PROP_ATTENTION_AUTOHIDE_TIMER:
      g_value_set_int (value, manager->priv->attention_autohide_timer);
      break;

    case PROP_ATTENTION_REQUIRED_REMINDER:
      g_value_set_int (value, manager->priv->attention_required_reminder);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  }
}

static void
task_manager_set_property (GObject      *object,
                          guint         prop_id,
                          const GValue *value,
                          GParamSpec   *pspec)
{
  TaskManager *manager = TASK_MANAGER (object);

  switch (prop_id)
  {
    case PROP_SHOW_ALL_WORKSPACES:
      task_manager_set_show_all_windows (manager, g_value_get_boolean (value));
      break;

    case PROP_ONLY_SHOW_LAUNCHERS:
      task_manager_set_show_only_launchers (manager, 
                                            g_value_get_boolean (value));
      break;

    case PROP_LAUNCHER_PATHS:
      if (manager->priv->launcher_paths)
      {
        g_value_array_free (manager->priv->launcher_paths);
        manager->priv->launcher_paths = NULL;
      }
      manager->priv->launcher_paths = (GValueArray*)g_value_dup_boxed (value);
      task_manager_refresh_launcher_paths (manager,
                                           manager->priv->launcher_paths);
      break;
    case PROP_DRAG_AND_DROP:
      task_manager_set_drag_and_drop (manager, 
                                      g_value_get_boolean (value));
      break;

    case PROP_MATCH_STRENGTH:
      task_manager_set_match_strength (manager, 
                                       g_value_get_int (value));
      break;

    case PROP_GROUPING:
      task_manager_set_grouping (manager,
                                 g_value_get_boolean (value));
      break;
      
    case PROP_INTELLIHIDE:
      /* TODO move into a function */
      manager->priv->intellihide = g_value_get_boolean (value);
      if (!manager->priv->intellihide && manager->priv->autohide_cookie)
      {     
        awn_applet_uninhibit_autohide (AWN_APPLET(manager), manager->priv->autohide_cookie);
        manager->priv->autohide_cookie = 0;
      }
      if (manager->priv->intellihide && !manager->priv->autohide_cookie)
      {     
        manager->priv->autohide_cookie = awn_applet_inhibit_autohide (AWN_APPLET(manager),"Intellihide" );
      }      
      break;

    case PROP_INTELLIHIDE_MODE:
      manager->priv->intellihide_mode = g_value_get_int (value);
      break;
      
    case PROP_ATTENTION_AUTOHIDE_TIMER:
      manager->priv->attention_autohide_timer = g_value_get_int (value);
      break;

    case PROP_ATTENTION_REQUIRED_REMINDER:
      if (manager->priv->attention_required_reminder_id)
      {
        g_source_remove (manager->priv->attention_required_reminder_id);
        manager->priv->attention_required_reminder_id = 0;
      }
      manager->priv->attention_required_reminder = g_value_get_int (value);
      if (manager->priv->attention_required_reminder>0)
      {
        manager->priv->attention_required_reminder_id = g_timeout_add_seconds ( manager->priv->attention_required_reminder,
                                                                             (GSourceFunc)_attention_required_reminder_cb,
                                                                             object);
      }
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  }
}

static void
task_manager_constructed (GObject *object)
{
  TaskManagerPrivate *priv;
  GtkWidget          *widget;
  
  G_OBJECT_CLASS (task_manager_parent_class)->constructed (object);
  
  priv = TASK_MANAGER_GET_PRIVATE (object);
  widget = GTK_WIDGET (object);

  priv->desktops_table = g_hash_table_new_full (g_str_hash,g_str_equal,g_free,g_free);

  priv->client = awn_config_get_default_for_applet (AWN_APPLET (object), NULL);

  /* Connect up the important bits */
  desktop_agnostic_config_client_bind (priv->client,
                                       DESKTOP_AGNOSTIC_CONFIG_GROUP_DEFAULT,
                                       "show_all_windows",
                                       object, "show_all_windows", TRUE,
                                       DESKTOP_AGNOSTIC_CONFIG_BIND_METHOD_FALLBACK,
                                       NULL);
  desktop_agnostic_config_client_bind (priv->client,
                                       DESKTOP_AGNOSTIC_CONFIG_GROUP_DEFAULT,
                                       "only_show_launchers",
                                       object, "only_show_launchers", TRUE,
                                       DESKTOP_AGNOSTIC_CONFIG_BIND_METHOD_FALLBACK,
                                       NULL);
  desktop_agnostic_config_client_bind (priv->client,
                                       DESKTOP_AGNOSTIC_CONFIG_GROUP_DEFAULT,
                                       "launcher_paths",
                                       object, "launcher_paths", FALSE,
                                       DESKTOP_AGNOSTIC_CONFIG_BIND_METHOD_FALLBACK,
                                       NULL);
  desktop_agnostic_config_client_bind (priv->client,
                                       DESKTOP_AGNOSTIC_CONFIG_GROUP_DEFAULT,
                                       "drag_and_drop",
                                       object, "drag_and_drop", TRUE,
                                       DESKTOP_AGNOSTIC_CONFIG_BIND_METHOD_FALLBACK,
                                       NULL);
  desktop_agnostic_config_client_bind (priv->client,
                                       DESKTOP_AGNOSTIC_CONFIG_GROUP_DEFAULT,
                                       "grouping",
                                       object, "grouping", TRUE,
                                       DESKTOP_AGNOSTIC_CONFIG_BIND_METHOD_FALLBACK,
                                       NULL);
  desktop_agnostic_config_client_bind (priv->client,
                                       DESKTOP_AGNOSTIC_CONFIG_GROUP_DEFAULT,
                                       "intellihide",
                                       object, "intellihide", TRUE,
                                       DESKTOP_AGNOSTIC_CONFIG_BIND_METHOD_FALLBACK,
                                       NULL);  
  desktop_agnostic_config_client_bind (priv->client,
                                       DESKTOP_AGNOSTIC_CONFIG_GROUP_DEFAULT,
                                       "intellihide_mode",
                                       object, "intellihide_mode", TRUE,
                                       DESKTOP_AGNOSTIC_CONFIG_BIND_METHOD_FALLBACK,
                                       NULL);    
  desktop_agnostic_config_client_bind (priv->client,
                                       DESKTOP_AGNOSTIC_CONFIG_GROUP_DEFAULT,
                                       "match_strength",
                                       object, "match_strength", TRUE,
                                       DESKTOP_AGNOSTIC_CONFIG_BIND_METHOD_FALLBACK,
                                       NULL);
  desktop_agnostic_config_client_bind (priv->client,
                                       DESKTOP_AGNOSTIC_CONFIG_GROUP_DEFAULT,
                                       "attention_autohide_timer",
                                       object, "attention_autohide_timer", FALSE,
                                       DESKTOP_AGNOSTIC_CONFIG_BIND_METHOD_FALLBACK,
                                       NULL);
  desktop_agnostic_config_client_bind (priv->client,
                                       DESKTOP_AGNOSTIC_CONFIG_GROUP_DEFAULT,
                                       "attention_required_reminder",
                                       object, "attention_required_reminder", FALSE,
                                       DESKTOP_AGNOSTIC_CONFIG_BIND_METHOD_FALLBACK,
                                       NULL);

  g_signal_connect (priv->screen,"active-window-changed",
                    G_CALLBACK(task_manager_active_window_changed_cb),object);
  g_signal_connect (priv->screen,"active-workspace-changed",
                    G_CALLBACK(task_manager_active_workspace_changed_cb),object);
}

static void
task_manager_class_init (TaskManagerClass *klass)
{
  GParamSpec     *pspec;
  GObjectClass   *obj_class = G_OBJECT_CLASS (klass);
  AwnAppletClass *app_class = AWN_APPLET_CLASS (klass);

  obj_class->constructed = task_manager_constructed;
  obj_class->set_property = task_manager_set_property;
  obj_class->get_property = task_manager_get_property;
  obj_class->dispose = task_manager_dispose;

  app_class->position_changed = task_manager_position_changed;
  app_class->size_changed   = task_manager_size_changed;

  /* Install properties first */
  pspec = g_param_spec_boolean ("show_all_windows",
                                "show-all-workspaces",
                                "Show windows from all workspaces",
                                TRUE,
                                G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
  g_object_class_install_property (obj_class, PROP_SHOW_ALL_WORKSPACES, pspec);

  pspec = g_param_spec_boolean ("only_show_launchers",
                                "only-show-launchers",
                                "Only show launchers",
                                FALSE,
                                G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
  g_object_class_install_property (obj_class, PROP_ONLY_SHOW_LAUNCHERS, pspec);

  pspec = g_param_spec_boxed ("launcher-paths",
                              "launcher paths",
                              "List of paths to launcher desktop files",
                              G_TYPE_VALUE_ARRAY,
                              G_PARAM_READWRITE);
  g_object_class_install_property (obj_class, PROP_LAUNCHER_PATHS, pspec);

  pspec = g_param_spec_boolean ("drag_and_drop",
                                "drag-and-drop",
                                "Show windows from all workspaces",
                                TRUE,
                                G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
  g_object_class_install_property (obj_class, PROP_DRAG_AND_DROP, pspec);

  pspec = g_param_spec_boolean ("grouping",
                                "grouping",
                                "Group windows",
                                TRUE,
                                G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
  g_object_class_install_property (obj_class, PROP_GROUPING, pspec);

  pspec = g_param_spec_boolean ("intellihide",
                                "intellihide",
                                "Intellihide",
                                TRUE,
                                G_PARAM_READWRITE);
  g_object_class_install_property (obj_class, PROP_INTELLIHIDE, pspec);

  pspec = g_param_spec_int ("intellihide_mode",
                                "intellihide mode",
                                "Intellihide mode",
                                0,
                                2,
                                1,
                                G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
  g_object_class_install_property (obj_class, PROP_INTELLIHIDE_MODE, pspec);

  pspec = g_param_spec_int ("match_strength",
                            "match_strength",
                            "How radical matching is applied for grouping items",
                            0,
                            99,
                            0,
                            G_PARAM_READWRITE);
  g_object_class_install_property (obj_class, PROP_MATCH_STRENGTH, pspec);
  
  pspec = g_param_spec_int ("attention_autohide_timer",
                            "Attention Autohide Timer",
                            "Number of seconds to inhibit autohide when a window requests attention",
                            0,
                            9999,
                            10,
                            G_PARAM_READWRITE);
  g_object_class_install_property (obj_class, PROP_ATTENTION_AUTOHIDE_TIMER, pspec);

  pspec = g_param_spec_int ("attention_required_reminder",
                            "Attention Required Reminder Timer",
                            "Attention Required Reminder Timer",
                            -1,
                            9999,
                            120,
                            G_PARAM_READWRITE);
  g_object_class_install_property (obj_class, PROP_ATTENTION_REQUIRED_REMINDER, pspec);

  /* Install signals */
  _taskman_signals[GROUPING_CHANGED] =
		g_signal_new ("grouping_changed",
			      G_OBJECT_CLASS_TYPE (obj_class),
			      G_SIGNAL_RUN_LAST,
			      G_STRUCT_OFFSET (TaskManagerClass, grouping_changed),
			      NULL, NULL,
			      g_cclosure_marshal_VOID__BOOLEAN, 
			      G_TYPE_NONE, 1,
            G_TYPE_BOOLEAN);

  g_type_class_add_private (obj_class, sizeof (TaskManagerPrivate));

  dbus_g_object_type_install_info (G_TYPE_FROM_CLASS (klass),
                                   &dbus_glib_task_manager_object_info);

  dbus_g_error_domain_register (task_manager_error_quark (), NULL, 
                                task_manager_error_get_type ());
}

static void
task_manager_init (TaskManager *manager)
{
  TaskManagerPrivate *priv;
  	
  priv = manager->priv = TASK_MANAGER_GET_PRIVATE (manager);

  priv->screen = wnck_screen_get_default ();
  priv->launcher_paths = NULL;
  priv->hidden_list = NULL;

  wnck_set_client_type (WNCK_CLIENT_TYPE_PAGER);

  win_quark = g_quark_from_string ("task-window-quark");

  priv->settings = task_settings_get_default (AWN_APPLET(manager));
  
  /* Create the icon box */
  priv->box = awn_icon_box_new_for_applet (AWN_APPLET (manager));
  gtk_container_add (GTK_CONTAINER (manager), priv->box);
  gtk_widget_show (priv->box);

  /* Create drag indicator */
  priv->drag_indicator = TASK_DRAG_INDICATOR(task_drag_indicator_new());
  gtk_container_add (GTK_CONTAINER (priv->box), GTK_WIDGET(priv->drag_indicator));
  gtk_widget_hide (GTK_WIDGET(priv->drag_indicator));
  if(priv->drag_and_drop)
    _drag_add_signals(manager, GTK_WIDGET(priv->drag_indicator));
  /* TODO: free !!! */
  priv->dragged_icon = NULL;
  priv->drag_timeout = 0;

  /* connect to the relevent WnckScreen signals */
  g_signal_connect (priv->screen, "window-opened", 
                    G_CALLBACK (on_window_opened), manager);
  g_signal_connect (priv->screen, "active-window-changed",  
                    G_CALLBACK (on_active_window_changed), manager);

  /* connect to our origin-changed signal for updating icon geometry */
  g_signal_connect (manager, "origin-changed",
                    G_CALLBACK (task_manager_origin_changed), NULL);
}

AwnApplet *
task_manager_new (const gchar *name,
                  const gchar *uid,
                  gint         panel_id)
{
  static AwnApplet *manager = NULL;

  if (!manager)
    manager = g_object_new (TASK_TYPE_MANAGER,
                            "canonical-name", name,
                            "display-name", "Task Manager",
                            "uid", uid,
                            "panel-id", panel_id,
                            NULL);
  return manager;
}

/*
 * AwnApplet stuff
 */
static void 
task_manager_position_changed (AwnApplet *applet, 
                               GtkPositionType position)
{
  TaskManagerPrivate *priv;

  g_return_if_fail (TASK_IS_MANAGER (applet));
  priv = TASK_MANAGER (applet)->priv;

  if (priv->settings)
    priv->settings->position = position;

  task_drag_indicator_refresh (priv->drag_indicator);
}

static void 
task_manager_size_changed   (AwnApplet *applet,
                             gint       size)
{
  TaskManagerPrivate *priv;
  GSList *i;

  g_return_if_fail (TASK_IS_MANAGER (applet));
  priv = TASK_MANAGER (applet)->priv;

  if (priv->settings)
    priv->settings->panel_size = size;

  for (i = priv->icons; i; i = i->next)
  {
    TaskIcon *icon = i->data;

    if (TASK_IS_ICON (icon))
      task_icon_refresh_icon (icon,size);
  }

  task_drag_indicator_refresh (priv->drag_indicator);
}

static void task_manager_origin_changed (AwnApplet *applet,
                                         GdkRectangle  *rect,
                                         gpointer       data)
{
  TaskManagerPrivate *priv;
  GSList *i;

  g_return_if_fail (TASK_IS_MANAGER (applet));
  priv = TASK_MANAGER (applet)->priv;

  // our origin changed, we need to update icon geometries
#ifdef DEBUG
  g_debug ("Got origin-changed, updating icon geometries...");
#endif

  for (i = priv->icons; i; i = i->next)
  {
    TaskIcon *icon = i->data;

    if (TASK_IS_ICON (icon))
      task_icon_schedule_geometry_refresh (icon);
  }
}

static void
task_manager_dispose (GObject *object)
{
  TaskManagerPrivate *priv;

  priv = TASK_MANAGER_GET_PRIVATE (object);

  desktop_agnostic_config_client_unbind_all_for_object (priv->client,
                                                        object,
                                                        NULL);
  if (priv->autohide_cookie)
  {     
    awn_applet_uninhibit_autohide (AWN_APPLET(object), priv->autohide_cookie);
    priv->autohide_cookie = 0;
  }
  if (priv->awn_gdk_window)
  {
    g_object_unref (priv->awn_gdk_window);
    priv->awn_gdk_window = NULL;
  }

  G_OBJECT_CLASS (task_manager_parent_class)->dispose (object);
}

/*
 * WNCK_SCREEN CALLBACKS
 */

/* 
 check for state change to and from skip tasklist.
 It appears we don't need to monitor loss of skip tasklist, but going to leave
 as is for the moment.  FIXME ?
 */
static void
on_window_state_changed (WnckWindow      *window,
                         WnckWindowState  changed_mask,
                         WnckWindowState  new_state,
                         TaskManager     *manager)
{
  TaskManagerPrivate *priv;
  TaskWindow        * task_win=NULL;
  TaskIcon          * icon=NULL;
  
  g_return_if_fail (TASK_IS_MANAGER (manager));

  priv = TASK_MANAGER_GET_PRIVATE (manager);

  /* test if they don't skip-tasklist anymore*/
  if (changed_mask & WNCK_WINDOW_STATE_SKIP_TASKLIST)
  {
    /*find the TaskWindow and destroy it*/
    for (GSList *icon_iter = priv->icons;icon_iter != NULL && !task_win;icon_iter = icon_iter->next)
    {
      icon = icon_iter->data;
      GSList *items = task_icon_get_items (icon);
      for (GSList *item_iter = items;item_iter != NULL;item_iter = item_iter->next)
      {
        TaskItem *item = item_iter->data;
        
        if (TASK_IS_WINDOW (item) && window==task_window_get_window(TASK_WINDOW(item)) )
        {
          task_win = TASK_WINDOW(item);
          break;
        }
      }
    }
    if (new_state & WNCK_WINDOW_STATE_SKIP_TASKLIST)
    {
        /*looks like we're ok in this case*/
    }  
    else
    {
      if (!task_win)
      {
        /*do this if we don't have TaskWindow for window yet*/
        g_signal_handlers_disconnect_by_func (window,G_CALLBACK(on_window_state_changed),manager);
        on_window_opened (NULL, window, manager);
      }
    }    
  }
}


static gboolean
uninhibit_timer (gpointer manager)
{
  TaskManagerPrivate *priv;
  
  g_return_val_if_fail (TASK_IS_MANAGER (manager),FALSE);
  priv = TASK_MANAGER(manager)->priv;

  awn_applet_uninhibit_autohide (AWN_APPLET(manager),priv->attention_cookie);
  priv->attention_cookie = 0;
  priv->attention_source = 0;
  /* This will reset the nag timer*/
  g_object_set (manager,
                "attention_required_reminder",priv->attention_required_reminder,
                NULL);
  return FALSE;
}


static gboolean
_attention_required_reminder_cb (TaskManager * manager)
{
  TaskManagerPrivate *priv;
  GSList * iter;
  
  g_return_val_if_fail (TASK_IS_MANAGER (manager),FALSE);
  priv = manager->priv;  
  for (iter=priv->windows; iter; iter = iter->next)
  {
    WnckWindowState win_state = wnck_window_get_state (task_window_get_window(iter->data));
    if ( (win_state  & WNCK_WINDOW_STATE_DEMANDS_ATTENTION) || 
        (win_state & WNCK_WINDOW_STATE_URGENT) )
    {
      if (priv->attention_autohide_timer && priv->attention_required_reminder)
      {
        if (priv->attention_cookie)
        {
          g_source_remove (priv->attention_source);
        }
        else
        {
          priv->attention_cookie = awn_applet_inhibit_autohide (AWN_APPLET(manager),
                                                                "Attention" );      
        }
        priv->attention_source = g_timeout_add_seconds (priv->attention_autohide_timer,
                                                        uninhibit_timer,manager);
      }
    }
  }
  return TRUE;
}

static void
check_attention_requested (WnckWindow      *window,
                         WnckWindowState  changed_mask,
                         WnckWindowState  new_state,
                         TaskManager     *manager)
{
  TaskManagerPrivate *priv;
  
  g_return_if_fail (TASK_IS_MANAGER (manager));
  priv = manager->priv;

  WnckWindowState win_state = wnck_window_get_state (window);
  
  /* inhibit autohide for a length of time.  If it's already inhibited due 
    to attention then reset timer
   */
  if (priv->attention_autohide_timer)
  {
    if ( (win_state  & WNCK_WINDOW_STATE_DEMANDS_ATTENTION) || 
        (win_state & WNCK_WINDOW_STATE_URGENT) )
    {
      if (priv->attention_cookie)
      {
        g_source_remove (priv->attention_source);
      }
      else
      {
        priv->attention_cookie = awn_applet_inhibit_autohide (AWN_APPLET(manager),
                                                              "Attention" );      
      }
      priv->attention_source = g_timeout_add_seconds (priv->attention_autohide_timer,
                                                      uninhibit_timer,manager);
    }
  }
}


/*
 * The active WnckWindow has changed.
 * Retrieve the TaskWindows and update their active state 
 *
 * TODO Store the active TaskWin into a TaskManager field so it can be used
 * for group value in intellihide_mode and minimize code bloat in other places.
 * It'll probably end up being useful to store this info for other purposes also.
 */
static void
on_active_window_changed (WnckScreen    *screen, 
                          WnckWindow    *old_window,
                          TaskManager   *manager)
{
  TaskManagerPrivate *priv;
  WnckWindow         *active = NULL;
  TaskWindow         *taskwin = NULL;
  TaskWindow         *old_taskwin = NULL;
  
  g_return_if_fail (TASK_IS_MANAGER (manager));
  priv = manager->priv;

  active = wnck_screen_get_active_window (priv->screen);

  if (WNCK_IS_WINDOW (old_window))
    old_taskwin = (TaskWindow *)g_object_get_qdata (G_OBJECT (old_window),
                                                    win_quark);
  if (WNCK_IS_WINDOW (active))
    taskwin = (TaskWindow *)g_object_get_qdata (G_OBJECT (active), win_quark);

  if (TASK_IS_WINDOW (old_taskwin))
    task_window_set_is_active (old_taskwin, FALSE);
  if (TASK_IS_WINDOW (taskwin))
    task_window_set_is_active (taskwin, TRUE);
}

/*
 * When the property 'show_all_windows' is False,
 * workspace switches are monitored. Whenever one happens
 * all TaskWindows are notified.
 */
static void
on_workspace_changed (TaskManager *manager) /*... has more arguments*/
{
  TaskManagerPrivate *priv;
  GSList             *w;
  WnckWorkspace      *space;
  
  g_return_if_fail (TASK_IS_MANAGER (manager));

  priv = manager->priv;
  space = wnck_screen_get_active_workspace (priv->screen);
  
  for (w = priv->windows; w; w = w->next)
  {
    TaskWindow *window = w->data;

    if (!TASK_IS_WINDOW (window)) continue;

    task_window_set_active_workspace (window, space);
  }
}

/*
 * TASK_ICON CALLBACKS
 */

static void
update_icon_visible (TaskManager *manager, TaskIcon *icon)
{
  TaskManagerPrivate *priv;
  gboolean visible = FALSE;
  /*if show_all_windows config key is false then we show/hide different
   icons on workspace switches.  We don't want to play closing 
   animations on them, opening animations are played as it seems to provide
   a better visual cue of what has changed*/
  gboolean do_hide_animation = FALSE;

  g_return_if_fail (TASK_IS_MANAGER (manager));

  priv = manager->priv;
  
  if ( task_icon_is_visible (icon) && ( !priv->only_show_launchers || 
        (task_icon_contains_launcher (icon) && !task_icon_count_ephemeral_items(icon))))
  {
    visible = TRUE;
  }

  if ( !task_icon_contains_launcher (icon) )
  {
    if (task_icon_count_items(icon)==0)
    {
      do_hide_animation = TRUE;
    }
  }
  else
  {
    if (task_icon_count_items (icon) <= task_icon_count_ephemeral_items (icon))
    {
      do_hide_animation = TRUE;
    }
  }

  
  if (visible && !gtk_widget_get_visible (GTK_WIDGET(icon)))
  {
    gtk_widget_show (GTK_WIDGET (icon));
    awn_effects_start_ex (awn_overlayable_get_effects (AWN_OVERLAYABLE (icon)), 
                        AWN_EFFECT_OPENING, 1, FALSE, FALSE);
  }

  if (!visible && gtk_widget_get_visible (GTK_WIDGET(icon)))
  {
    if (!do_hide_animation)
    {
      gtk_widget_hide (GTK_WIDGET(icon));
    }
    else
    {
      awn_effects_start_ex (awn_overlayable_get_effects (AWN_OVERLAYABLE (icon)), 
                          AWN_EFFECT_CLOSING, 1, FALSE, TRUE);
    } 
  }
}

static void
on_icon_visible_changed (TaskManager *manager, TaskIcon *icon)
{
  g_return_if_fail (TASK_IS_MANAGER (manager));

  update_icon_visible (manager, icon);
}

static gboolean
_destroy_icon_cb (GtkWidget * icon)
{
  gtk_widget_destroy (icon);
  return FALSE;
}

static void
on_icon_effects_ends (TaskIcon   *icon,
                      AwnEffect   effect,
                      AwnEffects *instance)
{  
  g_return_if_fail (TASK_IS_ICON (icon));
  if (effect == AWN_EFFECT_CLOSING)
  {
    gboolean destroy = FALSE;
    /*conditional operator*/
    if ( !task_icon_contains_launcher (icon) )
    {
      if (task_icon_count_items(icon)==0)
      {
        destroy = TRUE;
      }
    }
    else
    {
      if (task_icon_count_items (icon) <= task_icon_count_ephemeral_items (icon))
      {
        destroy = TRUE;
      }
    }
    if (destroy)
    {
      /*we're done... disconnect this handler or it's going to get called again...
       for this object*/
      g_signal_handlers_disconnect_by_func (awn_overlayable_get_effects (AWN_OVERLAYABLE (icon)),
                            G_CALLBACK (on_icon_effects_ends), icon);
      /*
       In the case of simple effects we receive this signal _before_ 
       the TaskItems are finalized.  By using the g_idle_add we defer the 
       destruction of the icon until after the TaskItems are gone (it's just 
       far less nasty than the alternative
      */
      gtk_widget_hide (GTK_WIDGET(icon));
      g_idle_add ((GSourceFunc)_destroy_icon_cb,icon);
    }
    else
    {
//      gtk_widget_hide (GTK_WIDGET(icon));
    }
  }
}

/*
 * This function gets called whenever a task-window gets finalized.
 * It removes the task-window from the list.
 * State: done
 */
static void
window_closed (TaskManager *manager, GObject *old_item)
{
  TaskManagerPrivate *priv;

  g_return_if_fail (TASK_IS_MANAGER (manager));
  priv = manager->priv;
  priv->windows = g_slist_remove (priv->windows, old_item);
}

/*
 * This function gets called whenever a task-icon gets finalized.
 * It removes the task-icon from the gslist and update layout
 * (so it gets removed from the bar)
 * State: done
 */
static void
icon_closed (TaskManager *manager, GObject *old_icon)
{
  TaskManagerPrivate *priv;

  g_return_if_fail (TASK_IS_MANAGER (manager));

  priv = manager->priv;
  priv->icons = g_slist_remove (priv->icons, old_icon);
}


static TaskItem *
get_launcher(TaskManager * manager, gchar * desktop)
{
  TaskItem     *launcher = NULL;  
  DesktopAgnosticFDODesktopEntry *entry=NULL;
  DesktopAgnosticVFSFile *file;

  g_assert (TASK_IS_MANAGER (manager));
  file = desktop_agnostic_vfs_file_new_for_path (desktop, NULL);
  if (file)
  {
    if (desktop_agnostic_vfs_file_exists (file) )
    {
      entry = desktop_agnostic_fdo_desktop_entry_new_for_file (file, NULL);
    }
    g_object_unref (file);
  }
  if (entry)
  {
    gboolean key_exists = desktop_agnostic_fdo_desktop_entry_key_exists (entry,"NoDisplay");
    if (!key_exists || !desktop_agnostic_fdo_desktop_entry_get_boolean (entry,"NoDisplay"))
    {
      launcher = task_launcher_new_for_desktop_file (AWN_APPLET(manager),desktop);
    }
    g_object_unref (entry);                                                    
  }
  return launcher;
}
/*
See note in util.c
 
 The desktop matching code will be refactored and put into its own object
 early in the 0.6 dev cycle.
 
 */

/*
 Create a path <systemdir><name>.desktop
 Does the desktop file exist (case sensitive)?
 If so then append as an ephemeral item to the item list.  An ephemeral item
 disappears when their are only ephemeral items left in the list.
 
 If the desktop file is not discovered then it will recursively call itself
 for any subdirectories within system_dir while doing a case insensitive match
 on all the filenames.
 
 FIXME: cleanup.
 */
static gchar *
task_icon_check_system_dir_for_desktop (TaskIcon *icon,
                                        gchar * system_dir, 
                                        gchar * name)
{
  gchar * desktop;
  TaskItem     *launcher = NULL;
  GDir      * dir;
  gchar     * result;
  AwnApplet * applet;

  g_assert (TASK_IS_ICON(icon));
  if (check_if_blacklisted(name) )
  {
    return NULL;
  }
  desktop = g_strdup_printf ("%s%s.desktop",system_dir,name);
//#define DEBUG
#ifdef DEBUG
  g_debug ("%s: desktop = %s",__func__,desktop);
#endif      
  g_object_get (icon,
                "applet",&applet,
                NULL);
  launcher = get_launcher(TASK_MANAGER(applet),desktop);
  g_object_unref (applet);
  if (launcher)
  {
#ifdef DEBUG
    g_debug ("launcher %p",launcher);
#endif
    task_icon_append_ephemeral_item (TASK_ICON (icon), launcher);
    return desktop;
  }
  g_free (desktop);
  
  dir = g_dir_open (system_dir,0,NULL);
  if (dir)
  {
    const char * fname;	
    while ( (fname = g_dir_read_name (dir)))
    {
//      g_debug ("fname = %s",fname);
      gchar * new_path = g_strdup_printf ("%s%s/",system_dir,fname);      
      if ( g_file_test (new_path,G_FILE_TEST_IS_DIR) )
      {
        result = task_icon_check_system_dir_for_desktop (icon,new_path,name);
        if (result)
        {
          g_free (new_path);
          g_dir_close (dir);
          return result;
        }
      }
      else
      {
        gchar * filename_lower;
        g_free (new_path);
        gchar * name_desktop = g_strdup_printf ("/%s.desktop",name);
        new_path = g_strdup_printf ("%s%s",system_dir,fname);
        desktop = new_path;
        filename_lower = g_utf8_strdown (desktop, -1);
        if ( g_strrstr (filename_lower,name_desktop) )
        {
          g_object_get (icon,
                        "applet",&applet,
                        NULL);
          launcher = get_launcher(TASK_MANAGER(applet),desktop);
          g_object_unref (applet);
          if (launcher)
          {
#ifdef DEBUG
            g_debug ("%s: found %s",__func__,desktop);
#endif
            g_free (filename_lower);
            g_free (new_path);            
            task_icon_append_ephemeral_item (TASK_ICON (icon), launcher);
            return name_desktop;
          }
        }
        g_free (name_desktop);        
        g_free (filename_lower);        
      }
      g_free (new_path);
    }
    g_dir_close (dir);    
  }
  return FALSE;
}

/*
 TODO: refactor into a few smaller functions or possibly just clean up the logic
 with judicious use of goto.
 */
static gchar *
find_desktop (TaskIcon *icon, gchar * name)
{
  gchar * lower;
  gchar * desktop;
  const gchar* const * system_dirs = g_get_system_data_dirs ();
  GStrv   iter;
  GStrv   tokens;
  TaskItem     *launcher = NULL;
  gchar * name_stripped = NULL;
  const gchar * extensions[] = { ".py",".pl",".exe",NULL};
  gchar * result;
  AwnApplet * applet = NULL;
  
  g_return_val_if_fail (name,FALSE);
  
#ifdef DEBUG
  g_debug ("%s:  name = %s",__func__,name);
#endif
  
  /*if the name has an of extensions then get rid of them*/
  for (iter = (GStrv)extensions; *iter; iter++)
  {
    gchar * filename_lower = g_utf8_strdown (*iter, -1);
    if ( g_strrstr (name,filename_lower) )
    {
      g_free (filename_lower);
      name_stripped = g_strdup (name);
      *g_strrstr (name_stripped,*iter) = '\0';
      break;
    }
    g_free (filename_lower);
  }  

  lower = g_utf8_strdown (name_stripped?name_stripped:name, -1);
  g_free (name_stripped);
//#define DEBUG
#ifdef DEBUG
  g_debug ("%s: name = %s",__func__,name);
  g_debug ("%s: lower = %s",__func__,lower);
#endif      

  /*
   Check in the application dirs of all System dirs
   If we find the desktop file it will get added as an ephemeral item and 
   TRUE will be returned.
   */
  for (iter = (GStrv)system_dirs; *iter; iter++)
  {
    gchar * applications_dir = g_strdup_printf ("%s/applications/",*iter);
    result = task_icon_check_system_dir_for_desktop (icon,applications_dir,lower);
    if (result)
    {
      g_free (lower);
      g_free (applications_dir);
      return result;
    }
    g_free (applications_dir);
  }

  /*
   Didn't find a desktop file in the system dirs...
   So check the user's data dir directory for an applications subdir containing
   our desktop file
   */
  
  desktop = g_strdup_printf ("%s/applications/%s.desktop",g_get_user_data_dir (),lower);
  
#ifdef DEBUG  
  g_debug ("%s: local desktop search = %s",__func__,desktop);
#endif
  g_object_get (icon,
                "applet",&applet,
                NULL);
  launcher = get_launcher(TASK_MANAGER(applet),desktop);
  g_object_unref (applet);
  if (launcher)
  {
#ifdef DEBUG
    g_debug ("launcher %p",launcher);
#endif
    task_icon_append_ephemeral_item (TASK_ICON (icon), launcher);
    g_free  (lower);
    return desktop;
  }
  else
  {
    gchar * local_dir = g_strdup_printf ("%s/applications/",g_get_user_data_dir ());
    result = task_icon_check_system_dir_for_desktop (icon,local_dir,lower);
    if (result)
    {
      g_free (local_dir);
      g_free (lower);
      return result;
    }
    g_free (local_dir);
  }
  g_free (desktop);
  
  /*
   Didn't find anything... so attempt to strip out odd characters and retry 
   the process if any were successfully removed.
   */
  g_strdelimit (lower,"-.:,=+_~!@#$%^()[]{}'",' ');
  tokens = g_strsplit (lower," ",-1);
  if (tokens)
  {
    gchar * stripped = g_strjoinv(NULL,tokens);
    g_strfreev (tokens);    
    if (g_strcmp0 (stripped, name) !=0)
    {
      result = find_desktop (icon,stripped);
      if (result)
      {
        g_free (lower);
        g_free (stripped);
        return result;
      }
    }  
    g_free (stripped);    
  }
  g_free (lower);  
  return FALSE;
}

/*
 Similar to find_desktop but attempting to match using regexes instead of 
 direct string comparisions
 This is fuzzier so if a desktop file is matched it will do a comparision
 of Exec to the cmd... if they don't match then the desktop file match will
 be discarded.
 */
static gchar *
find_desktop_fuzzy (TaskIcon *icon, gchar * class_name, gchar *cmd)
{
  gchar * lower;
  gchar * lower_regex_escaped;
  gchar * desktop_regex_str;
  GRegex  * desktop_regex;
  const gchar* const * system_dirs = g_get_system_data_dirs ();
  GStrv   iter;
  TaskItem     *launcher = NULL;
  gchar ** tokens;
  AwnApplet * applet;
  
  g_return_val_if_fail (class_name,NULL);
  lower = g_utf8_strdown (class_name, -1);
  lower_regex_escaped = g_regex_escape_string (lower,-1);
//#define DEBUG 1
#ifdef DEBUG
  g_debug ("%s: wm class = %s",__func__,class_name);
  g_debug ("%s: lower = %s",__func__,lower);
#endif      

  g_return_val_if_fail (strlen (lower),NULL);
  /*
   TODO compile the regex
   */
  desktop_regex_str = g_strdup_printf (".*%s.*desktop",lower_regex_escaped);
  desktop_regex = g_regex_new (desktop_regex_str,G_REGEX_CASELESS,0,NULL);
#ifdef DEBUG
    g_debug ("%s: desktop regex = %s",__func__,desktop_regex_str);
#endif      
  g_free (desktop_regex_str);
  desktop_regex_str = NULL;
  g_free (lower_regex_escaped);
  lower_regex_escaped = NULL;
  g_return_val_if_fail (desktop_regex,NULL);
  
  for (iter = (GStrv)system_dirs; *iter; iter++)
  {
    gchar * dir_name = g_strdup_printf ("%sapplications",*iter);
    GDir  * dir = g_dir_open (dir_name,0,NULL);
    if (dir)
    {
      const gchar * filename;
      while ( (filename = g_dir_read_name (dir)) )
      {
        if ( g_regex_match (desktop_regex, filename,0,NULL) )
        {
          gchar * full_path = g_strdup_printf ("%s/%s",dir_name,filename);
#ifdef DEBUG
          g_debug ("%s:  regex matched full path =   %s",__func__,full_path);
#endif       
          // TODO handle GErrors.  Do we really want to handle them?
          DesktopAgnosticVFSFile *file = desktop_agnostic_vfs_file_new_for_path (full_path, NULL);
          DesktopAgnosticFDODesktopEntry * desktop = desktop_agnostic_fdo_desktop_entry_new_for_file (file, NULL);
          if (desktop)
          {
            const gchar * fdo_options[] = {"%f","%F","%u","%U","%d","%D","%n","%N","%i","%c","%k","%v","%m",NULL};
            const gchar ** i;
            gchar ** j;
            gchar * exec = NULL;
            gchar * exec_regex_escaped = NULL;
            gchar ** exec_tokens;
            gchar * exec_base;
            gchar * cmd_base;
            gchar ** cmd_tokens;

            if (desktop_agnostic_fdo_desktop_entry_key_exists (desktop,"Exec"))
            {
              exec = desktop_agnostic_fdo_desktop_entry_get_string (desktop, "Exec");
              if (exec)
              {
                exec_regex_escaped = g_regex_escape_string (exec,-1);            
              }
            }
            g_free (exec);
            exec = NULL;
            if (!exec_regex_escaped)
            {
              continue;
            }
            g_object_unref (desktop);
            for (i=fdo_options; *i;i++)
            {
              exec_tokens = g_strsplit (exec_regex_escaped,*i,-1);
              g_free (exec_regex_escaped);
              exec_regex_escaped = g_strjoinv ("",exec_tokens);
              g_strfreev (exec_tokens);
            }
            exec_tokens = g_strsplit(exec_regex_escaped," ",-1);
            exec_base = g_path_get_basename (exec_tokens[0]);
            g_free (exec_tokens[0]);
            exec_tokens[0] = exec_base;
            for (j=exec_tokens; *j; j++)
            {
              g_strstrip (*j);
            }
            g_free (exec_regex_escaped);
            exec_regex_escaped = g_strjoinv (".*",exec_tokens);
            g_strfreev (exec_tokens);

            cmd_tokens = g_strsplit (cmd," ",-1);
            cmd_base = g_path_get_basename (cmd_tokens[0]);
            g_free (cmd_tokens[0]);
            cmd_tokens [0] = cmd_base;
            for (j = cmd_tokens; *j; j++)
            {
              gchar * str = *j;
              g_strstrip (str);
              if ( str[0] == '-')
              {
                *str = '\0';
              }
            }
            cmd_base = g_strjoinv (" ",cmd_tokens);
            g_strfreev (cmd_tokens);
#ifdef DEBUG            
            g_debug ("%s:  exec =   '%s'",__func__,exec_regex_escaped);
            g_debug ("%s:  cmd_base =   '%s'",__func__,cmd_base);
#endif              
            if ( g_regex_match_simple (exec_regex_escaped, cmd,G_REGEX_CASELESS,0) )              
            {
              g_object_get (icon,
                            "applet",&applet,
                            NULL);
              launcher = get_launcher(TASK_MANAGER(applet),full_path);
              g_object_unref (applet);
              if (launcher)
              {
                task_icon_append_ephemeral_item (TASK_ICON (icon), launcher);
                g_regex_unref (desktop_regex);
                g_free (exec_regex_escaped);
                g_free (lower);
                g_free (cmd_base);
                return full_path;
              }
            }
            g_free (cmd_base);
            cmd_base = NULL;
            g_free (exec_regex_escaped);
            exec_regex_escaped = NULL;
          }
          g_object_unref (file);
          g_free (full_path);
          full_path = NULL;
        }
      }
      g_dir_close (dir);
    }
    g_free (dir_name);
    dir_name = NULL;
  }
  g_regex_unref (desktop_regex);
  
  tokens = g_strsplit (lower,"-",-1);
  g_free (lower);  
  lower = NULL;
  if (tokens && tokens[0] && tokens[1] )
  {
    gchar * result = find_desktop_fuzzy (icon, tokens[0], cmd);
    g_strfreev (tokens);
    if (result)
    {
      return result;
    }
  }
  
  return NULL;
//#undef DEBUG
  
}

/*
 If other attempts to find a desktop file fail then this function will
 typically get called which use tables of data to match problem apps to 
 desktop file names (refer to util.c)
 */
static gchar *
find_desktop_special_case (TaskIcon *icon, gchar * cmd, gchar *res_name, 
                                 gchar * class_name,const gchar *title)
{
  gchar * result = NULL;
  GSList * desktops = get_special_desktop_from_window_data (cmd,res_name,class_name,title);
  if (desktops)
  {
    GSList * iter;
    for (iter = desktops; iter; iter = iter->next)
    {
      result = find_desktop (icon,iter->data);
      if ( !result && cmd && (strlen (cmd) > 8) ) 
      {
        result = find_desktop_fuzzy (icon,iter->data,cmd);
      }
      if (result)
      {
        break;
      }
    }
    g_slist_foreach (desktops,(GFunc)g_free,NULL);
    g_slist_free (desktops);
  }

  return result;
}

static gchar *
search_desktop_cache (TaskManager * manager, TaskIcon * icon,gchar * class_name, 
                      gchar * res_name, gchar * cmd, gchar *id)
{
  TaskManagerPrivate *priv;
  gchar * key;
  gchar * desktop_path;
  TaskItem *launcher = NULL;

  priv = manager->priv;
  key = g_strdup_printf("%s::%s::%s::%s",class_name,res_name,cmd,id);
  desktop_path = g_hash_table_lookup (priv->desktops_table,key);

  if (desktop_path)
  {
    launcher = get_launcher (manager,desktop_path);
    if (launcher)
    {
      task_icon_append_ephemeral_item (TASK_ICON (icon), launcher);      
      return g_strdup (desktop_path);
    }
  }
  return NULL;
}

void
task_manager_add_icon(TaskManager *manager, TaskIcon * icon)
{
  TaskManagerPrivate *priv;
  
  priv = manager->priv;  
  priv->icons = g_slist_append (priv->icons, icon);
  gtk_container_add (GTK_CONTAINER (priv->box), GTK_WIDGET(icon));
  
  /* reordening through D&D */
  if(priv->drag_and_drop)
  {
    _drag_add_signals(manager, GTK_WIDGET(icon));
  }

  g_object_weak_ref (G_OBJECT (icon), (GWeakNotify)icon_closed, manager);
  g_signal_connect_swapped (icon, 
                            "visible-changed",
                            G_CALLBACK (on_icon_visible_changed), 
                            manager);
  g_signal_connect_swapped (awn_overlayable_get_effects (AWN_OVERLAYABLE (icon)), 
                            "animation-end", 
                            G_CALLBACK (on_icon_effects_ends), 
                            icon);
  update_icon_visible (manager, TASK_ICON (icon));
  task_icon_refresh_icon (TASK_ICON(icon),awn_applet_get_size(AWN_APPLET(manager)));
}  

static gboolean
task_manager_check_for_hidden (TaskManager * manager, TaskWindow * item)
{
  TaskManagerPrivate *priv;
  priv = manager->priv;
  gboolean hidden = FALSE;

  /*see if the new window is on the hidden_list*/
  gchar * res_name=NULL;
  gchar * class_name=NULL;
  task_window_get_wm_class(TASK_WINDOW (item), &res_name, &class_name);    
  /*see if the new window is on the hidden_list*/
  for (GList *iter=priv->hidden_list;iter;iter=g_list_next(iter) )
  {
    if (  (g_strcmp0(iter->data,res_name)==0)||
        (g_strcmp0(iter->data,class_name)==0) )
    {
      task_window_set_hidden (TASK_WINDOW(item),TRUE);
      hidden = TRUE;
      break;
    }
  }
  g_free (res_name);
  g_free (class_name);
  return hidden;
}

static void
process_window_opened (WnckWindow    *window,
                  TaskManager   *manager)
{
  /*TODO
   This functions is becoming a beast.  look into chopping into bits
   */
  TaskManagerPrivate *priv;
  GtkWidget          *icon;
  TaskItem           *item;
  WnckWindowType      type;
  GSList             *w;
  TaskIcon *match      = NULL;
  gint match_score     = 0;
  gint max_match_score = 0;
  gchar*             found_desktop = FALSE;

  g_return_if_fail (TASK_IS_MANAGER (manager));
  g_return_if_fail (WNCK_IS_WINDOW (window));

  g_signal_handlers_disconnect_by_func(window, process_window_opened, manager);
  
  priv = manager->priv;
  type = wnck_window_get_window_type (window);
    
  /*
   For Intellihide.  It may be ok to connect this after we the switch (where
   some windows are filtered out)
   */
  g_signal_connect (window,"geometry-changed",
                  G_CALLBACK(task_manager_win_geom_changed_cb),manager);
  g_signal_connect (window,"state-changed",
                    G_CALLBACK(task_manager_win_state_changed_cb),manager);
  
  switch (type)
  {
    case WNCK_WINDOW_DESKTOP:
    case WNCK_WINDOW_DOCK:
    case WNCK_WINDOW_TOOLBAR:
    case WNCK_WINDOW_MENU:
    case WNCK_WINDOW_SPLASHSCREEN:
      return; /* No need to worry about these */

    default:
      break;
  }

#ifdef DEBUG  
  g_debug ("%s: Window opened: %s",__func__,wnck_window_get_name (window));  
  g_debug ("xid = %lu, pid = %d",wnck_window_get_xid (window),wnck_window_get_pid (window));
#endif  
  /* 
    for some reason the skip tasklist property for the taskmanager toggles briefly
   off and on in certain circumstances.  Nip this in the bud.
   TODO:  Investigate wth this is happening...  it bothers me.
   */
//  if ( wnck_window_get_pid (window) == getpid() || 
  if ( g_strcmp0 (wnck_window_get_name (window),"awn-applet")==0 )
  {
    return;
  }
  /* 
   * Monitors state change to and from skip tasklist.
   */
  g_signal_connect (window, "state-changed", G_CALLBACK (on_window_state_changed), manager);

  if (wnck_window_is_skip_tasklist (window))
  {
    return;
  }

  g_signal_connect (window, "state-changed", 
                    G_CALLBACK (check_attention_requested), manager);    
  
  /* create a new TaskWindow containing the WnckWindow*/
  item = task_window_new (AWN_APPLET(manager), window);
  g_object_set_qdata (G_OBJECT (window), win_quark, TASK_WINDOW (item));

  priv->windows = g_slist_append (priv->windows, item);
  g_object_weak_ref (G_OBJECT (item), (GWeakNotify)window_closed, manager);

  /* see if there is a icon that matches*/
  for (w = priv->icons; w; w = w->next)
  {
    TaskIcon *taskicon = w->data;

#ifdef DEBUG
    if (!TASK_IS_ICON (taskicon))
      g_debug ("Not a task icon");
#endif
    if (!TASK_IS_ICON (taskicon)) continue;
#ifdef DEBUG
    g_debug ("contains launcher is %d",task_icon_contains_launcher (taskicon) );
#endif
    match_score = task_icon_match_item (taskicon, item);
    if (match_score > max_match_score)
    {
      max_match_score = match_score;
      match = taskicon;
    }
  }
#ifdef DEBUG
  g_debug("Matching score: %i, must be bigger then:%i, groups: %i", max_match_score, 99-priv->match_strength, max_match_score > 99-priv->match_strength);
#endif  
  /*
   if match is not 0
   and 
   we're doing grouping || there are 1 items in the TaskIcon and that is a Launcher
   and 
   the matching meets the match strength
   */
  task_manager_check_for_hidden (manager,TASK_WINDOW(item));

  if  (match
       &&
       (priv->grouping || ( (task_icon_count_items(match)==1) && (task_icon_contains_launcher (match)) ) ) 
       &&
       ( max_match_score > 99-priv->match_strength))
  {
    task_icon_append_item (match, item);
  }
  else
  {
    /* grab the class name.
     look through the various freedesktop system/user dirs for the 
     associated desktop file.  If we find it then dump the associated 
     launcher into the the dialog.
    */
    gchar   *res_name = NULL;
    gchar   *class_name = NULL;
    gchar   *client_name = NULL;
    gchar   *cmd;
    gchar   *full_cmd;
    gchar   *cmd_basename = NULL;
    gchar * id = NULL;
    glibtop_proc_args buf;
    
    cmd = glibtop_get_proc_args (&buf,wnck_window_get_pid (window),1024);    
    full_cmd = get_full_cmd_from_pid (wnck_window_get_pid (window));
    if (cmd)
    {
      cmd_basename = g_path_get_basename (cmd);
    }
    
    icon = task_icon_new (AWN_APPLET (manager));
    task_window_get_wm_class(TASK_WINDOW (item), &res_name, &class_name);
    task_window_get_wm_client(TASK_WINDOW (item), &client_name);
    if (!client_name)
    {
      /*
       WM_CLIENT_NAME is not necessarily set... in those case we'll assume 
       that it's the host
       */
      gchar buffer[256];
      gethostname (buffer, sizeof(buffer));
      buffer [sizeof(buffer) - 1] = '\0';
      client_name = g_strdup (buffer);
    }
    g_free (client_name);
#ifdef DEBUG
      g_debug ("client name is %s",client_name);    
      g_debug ("%s: class name  = %s, res name = %s",__func__,class_name,res_name);
#endif      
    id = get_special_id_from_window_data (cmd, res_name, class_name, wnck_window_get_name(window));
    found_desktop = search_desktop_cache (manager,TASK_ICON(icon),class_name,res_name,cmd_basename,id);
/*
     Possible TODO:
     Check for saved signature.
     If we save a signature,desktopfile pair when we successfully discover a
     desktop file then we don't need to search through a bunch of directories
     for an app multiple times...
     Question: How bad is the performance hit atm?
     
     TODO Note:  Desktop file lookup is important if we want to be able to offer
     a favourite apps options (automatically show the top 4 apps in the bar for 
     example).   
     
     We may as well implement both the signature/desktop caching along with 
     stats for favourite app usage in a unified manner.
*/

    /*
     Various permutations on finding a desktop file for the app.
     
     Class name may eventually be shown to give a false positive for something.
     
     */
    if (!found_desktop)
    {
      if (res_name && strlen (res_name))
      {
        found_desktop = find_desktop (TASK_ICON(icon), res_name);
      }

      if (!found_desktop && class_name && strlen (class_name))
      {
        found_desktop = find_desktop (TASK_ICON(icon), class_name);
      }    
      /*This _may_ result in unacceptable false positives.  Testing.*/
      if (!found_desktop && full_cmd && strlen (full_cmd))
      {
        #ifdef DEBUG
        g_debug ("%s:  full cmd = '%s'",__func__,full_cmd);
        #endif
        found_desktop = find_desktop (TASK_ICON(icon), full_cmd);
      }
      if (!found_desktop && cmd && strlen (cmd))
      {
        #ifdef DEBUG
        g_debug ("%s:  cmd = '%s'",__func__,cmd);
        #endif
        found_desktop = find_desktop (TASK_ICON(icon), cmd);
      }
      
      if (!found_desktop && full_cmd && strlen (full_cmd) )
      {
        found_desktop = find_desktop_fuzzy (TASK_ICON(icon),class_name, full_cmd);
      }
      if (!found_desktop && cmd && strlen (cmd))
      {
        found_desktop = find_desktop_fuzzy (TASK_ICON(icon),class_name, cmd);
      }
      if (!found_desktop && full_cmd && strlen (full_cmd) )
      {
        found_desktop = find_desktop_fuzzy (TASK_ICON(icon),res_name, full_cmd);
      }
      if (!found_desktop && cmd && strlen (cmd))
      {
        found_desktop = find_desktop_fuzzy (TASK_ICON(icon),res_name, cmd);
      }
      
      if (!found_desktop && cmd_basename && strlen (cmd_basename) )
      {
        found_desktop = find_desktop (TASK_ICON(icon), cmd_basename);
      }
      
      if (!found_desktop)
      {
        found_desktop = find_desktop_special_case (TASK_ICON(icon),full_cmd,res_name,
                                                   class_name,
                                                   wnck_window_get_name (window));
      }
      if (!found_desktop)
      {
        found_desktop = find_desktop_special_case (TASK_ICON(icon),cmd,res_name,
                                                   class_name,
                                                   wnck_window_get_name (window));
      }
      if (found_desktop)
      {
        gchar * key = g_strdup_printf("%s::%s::%s::%s",class_name,res_name,cmd_basename,id);
        
        g_hash_table_insert (priv->desktops_table, key,found_desktop);
      }
    }
/*
     Possible TODO
     if found and signature has not already been saved then save it.
*/
    if (TASK_IS_WINDOW(item))
    {
      task_window_set_use_win_icon (TASK_WINDOW(item),get_win_icon_use (full_cmd,
                                                                      res_name,
                                                                      class_name,
                                                                      wnck_window_get_name (window)));
    }
    g_free (id);    
    g_free (full_cmd);
    g_free (cmd);     
    g_free (class_name);
    g_free (res_name);
    g_free (cmd_basename);
    task_icon_append_item (TASK_ICON (icon), item);
    task_manager_add_icon (manager,TASK_ICON (icon));    
  }
}
  
static gboolean
_wait_for_name_change_timeout (WindowOpenTimeoutData * data)
{
  gchar * res_name = NULL;
  gchar * class_name = NULL;

  _wnck_get_wmclass (wnck_window_get_xid (data->window),
                     &res_name, &class_name);
  /*only go ahead and process if the title is still useless*/
  if (get_special_wait_from_window_data (res_name, 
                                         class_name,
                                         wnck_window_get_name(data->window) ) )
  {
    process_window_opened (data->window,data->manager);
  }  
  g_free (res_name);
  g_free (class_name);
  return FALSE;
}

/*
 * Whenever a new window gets opened it will try to place it
 * in an awn-icon or will create a new awn-icon.
 * State: adjusted
 *
 * TODO: document all the possible match ratings in one place.  Evaluate if they
 * are sane.
 */
static void 
on_window_opened (WnckScreen    *screen, 
                  WnckWindow    *window,
                  TaskManager   *manager)
{
  /*this function is about certain windows setting a very unexpected, unuseful,
   title in certain circumstances when it first opens then changing it almost
   immediately to something that is useful.  get_special_wait_from_window_data()
   checks for one of those cases (open office being opened with a through a 
   data file is one), and if that is the situations it connect a "name-change" 
   signal and defers processing till then (after 500ms it will just go ahead*/
  gchar * res_name = NULL;
  gchar * class_name = NULL;
  WindowOpenTimeoutData * win_timeout_data;

  g_return_if_fail (TASK_IS_MANAGER (manager));
  g_return_if_fail (WNCK_IS_WINDOW (window));
  
  win_timeout_data = g_malloc (sizeof (WindowOpenTimeoutData));
  win_timeout_data->window = window;
  win_timeout_data->manager = manager;
  _wnck_get_wmclass (wnck_window_get_xid (window),
                     &res_name, &class_name);
  if (get_special_wait_from_window_data (res_name, 
                                         class_name,
                                         wnck_window_get_name(window) ) )
  {
    g_signal_connect (window,"name-changed",G_CALLBACK(process_window_opened),manager);
    g_timeout_add (500,(GSourceFunc)_wait_for_name_change_timeout,win_timeout_data);
  }
  else
  {
    process_window_opened (window,manager);
  }
  g_free (res_name);
  g_free (class_name);  
}

/*
 * PROPERTIES
 */
static void
task_manager_set_show_all_windows (TaskManager *manager,
                                   gboolean     show_all)
{
  TaskManagerPrivate *priv;
  GSList             *w;
  WnckWorkspace      *space = NULL;
  
  g_return_if_fail (TASK_IS_MANAGER (manager));

  priv = manager->priv;

  if (priv->show_all_windows == show_all) return;
 
  manager->priv->show_all_windows = show_all;

  if (show_all)
  {
    /* Remove signals of workspace changes*/
    g_signal_handlers_disconnect_by_func(priv->screen, 
                                         G_CALLBACK (on_workspace_changed), 
                                         manager);
    
    /* Set workspace to NULL, so TaskWindows aren't tied to workspaces anymore*/
    space = NULL;
  }
  else
  {
    /* Add signals to WnckScreen for workspace changes*/
    g_signal_connect_swapped (priv->screen, "viewports-changed",
                              G_CALLBACK (on_workspace_changed), manager);
    g_signal_connect_swapped (priv->screen, "active-workspace-changed",
                              G_CALLBACK (on_workspace_changed), manager);

    // Retrieve the current active workspace
    space = wnck_screen_get_active_workspace (priv->screen);
  }

  /* Update the workspace for every TaskWindow.
   * NULL if the windows aren't tied to a workspace anymore */
  for (w = priv->windows; w; w = w->next)
  {
    TaskWindow *window = w->data;
    if (!TASK_IS_WINDOW (window)) 
    {
      continue;
    }
    task_window_set_active_workspace (window, space);
  }
}

/*
 * The property 'show_only_launchers' changed.
 * So update the property and update the visiblity of every icon.
 */
static void
task_manager_set_show_only_launchers (TaskManager *manager, 
                                      gboolean     only_show_launchers)
{
  TaskManagerPrivate *priv;
  GSList             *w;

  g_return_if_fail (TASK_IS_MANAGER (manager));

  priv = manager->priv;
  priv->only_show_launchers = only_show_launchers;

  for (w = priv->icons; w; w = w->next)
  {
    TaskIcon *icon = w->data;

    if (!TASK_IS_ICON (icon)) 
    {
      continue;
    }

    update_icon_visible (manager, icon);
  }
}

void 
task_manager_remove_task_icon (TaskManager  *manager, GtkWidget *icon)
{
  TaskManagerPrivate *priv;

  g_return_if_fail (TASK_IS_MANAGER (manager));
  priv = manager->priv;
  
  priv->icons = g_slist_remove (priv->icons,icon);
}

void
task_manager_append_launcher(TaskManager  *manager, const gchar * launcher_path)
{
  TaskManagerPrivate *priv;
  GValueArray *launcher_paths;
  GValue val = {0,};
  
  g_return_if_fail (TASK_IS_MANAGER (manager));
  priv = manager->priv;

  g_object_get (G_OBJECT (manager), "launcher_paths", &launcher_paths, NULL);
  g_value_init (&val, G_TYPE_STRING);
  g_value_set_string (&val, launcher_path);
  launcher_paths = g_value_array_append (launcher_paths, &val);
  g_object_set (G_OBJECT (manager), "launcher_paths", launcher_paths, NULL);
  g_value_unset (&val);
  task_manager_refresh_launcher_paths (manager, launcher_paths);
  g_value_array_free (launcher_paths);
}

/*
 * Checks when launchers got added/removed in the list in gconf/file.
 * It removes the launchers from the task-icons and add those
 * that aren't already on the bar.
 */
static void
task_manager_refresh_launcher_paths (TaskManager *manager,
                                     GValueArray *list)
{
  TaskManagerPrivate *priv;

  g_return_if_fail (TASK_IS_MANAGER (manager));
  priv = manager->priv;
  /*
   Find launchers in the the launcher list do not yet have a TaskIcon and 
   add them
   */
  for (guint idx = 0; idx < list->n_values; idx++)
  {
    gchar *path;
    gboolean found;

    path = g_value_dup_string (g_value_array_get_nth (list, idx));
    found = FALSE;

    for (GSList *icon_iter = priv->icons;
         icon_iter != NULL;
         icon_iter = icon_iter->next)
    {
      GSList *items = task_icon_get_items (TASK_ICON (icon_iter->data));

      for (GSList *item_iter = items;
           item_iter != NULL;
           item_iter = item_iter->next)
      {
        TaskItem *item = item_iter->data;

        if (TASK_IS_LAUNCHER (item) &&
            g_strcmp0 (task_launcher_get_desktop_path (TASK_LAUNCHER (item)),
                       path) == 0)
        {
          gint cur_pos;
          found = TRUE;
          cur_pos = g_slist_position (priv->icons,icon_iter);
          if (cur_pos != (gint)idx)
          {
            TaskIcon * icon = icon_iter->data;
            priv->icons = g_slist_remove_link (priv->icons,icon_iter);
            priv->icons = g_slist_insert (priv->icons,icon,idx);
            gtk_box_reorder_child (GTK_BOX (priv->box), GTK_WIDGET(icon), idx);            
          }
          break;
        }
      }
      if (found)
      {
        break;
      }
    }
    if (!found)
    {
      TaskItem  *launcher = NULL;
      GtkWidget *icon;

      launcher = task_launcher_new_for_desktop_file (AWN_APPLET(manager),path);
      
      if (launcher)
      {
        icon = task_icon_new (AWN_APPLET (manager));
        task_icon_append_item (TASK_ICON (icon), launcher);
        gtk_container_add (GTK_CONTAINER (priv->box), icon);
        gtk_box_reorder_child (GTK_BOX (priv->box), icon, idx);
        priv->icons = g_slist_insert (priv->icons, icon, idx);

        g_object_weak_ref (G_OBJECT (icon), (GWeakNotify)icon_closed, manager);
        g_signal_connect_swapped (icon,
                                  "visible-changed",
                                  G_CALLBACK (on_icon_visible_changed),
                                  manager);
        g_signal_connect_swapped (awn_overlayable_get_effects (AWN_OVERLAYABLE (icon)),
                                  "animation-end",
                                  G_CALLBACK (on_icon_effects_ends),
                                  icon);

        update_icon_visible (manager, TASK_ICON (icon));

        /* reordening through D&D */
        if (priv->drag_and_drop)
        {
          _drag_add_signals(manager, icon);
        }
      }
      else
      {
        g_debug ("%s: Bad desktop file '%s'", __func__, path);
      }
    }
    g_free (path);
  }
  
  /*
   Find non-ephemeral launchers in the TaskIcons that are not represented in the
   launcher list and make them ephemeral  (Removes them from the TaskIcons once
   they contain no TaskWindows)
   */
  for (GSList *icon_iter = priv->icons;
       icon_iter != NULL;
       icon_iter = icon_iter->next)
  {
    TaskLauncher * launcher = TASK_LAUNCHER(task_icon_get_launcher (icon_iter->data));
    gboolean found;
    const gchar * launcher_name = NULL;
    found = FALSE;
    if (launcher)
    {
      launcher_name = task_launcher_get_desktop_path (launcher);
      for (guint idx = 0; idx < list->n_values; idx++)
      {
        gchar *path;
        path = g_value_dup_string (g_value_array_get_nth (list, idx));
        if (g_strcmp0(launcher_name,path)==0)
        {
          found = TRUE;
        }
        g_free (path);
      }
    } 
    if (launcher && !found)
    {
      if ( !task_icon_count_ephemeral_items (icon_iter->data) )
      {
        /*if there are only ephemeral items in the icon then it will 
         trigger it's removal.  Otherwise the it will be removed when it is
         no longer managing ay TaskWindows*/        
        task_icon_increment_ephemeral_count (icon_iter->data);
        icon_iter = priv->icons;
      }
    }
  }
}

static void
task_manager_set_match_strength (TaskManager *manager,
                                 gint match_strength)
{
  g_return_if_fail (TASK_IS_MANAGER (manager));
  manager->priv->match_strength = match_strength;
}

static void
task_manager_set_drag_and_drop (TaskManager *manager, 
                                gboolean     drag_and_drop)
{
  g_return_if_fail (TASK_IS_MANAGER (manager));

  TaskManagerPrivate *priv = manager->priv;
  GSList         *i;

  priv->drag_and_drop = drag_and_drop;

  /*connect or dissconnect the dragging signals*/
  for (i = priv->icons; i; i = i->next)
  {
    TaskIcon *icon = i->data;
    
    if (!TASK_IS_ICON (icon)) 
    {
      continue;
    }

    if(drag_and_drop)
    {
      _drag_add_signals (manager, GTK_WIDGET(icon));
    }
    else
    {
      /*FIXME: Stop any ongoing move*/
      _drag_remove_signals (manager, GTK_WIDGET(icon));
    }
  }
  if(drag_and_drop)
  {
    _drag_add_signals (manager, GTK_WIDGET(priv->drag_indicator));
  }
  else
  {
    _drag_remove_signals (manager, GTK_WIDGET(priv->drag_indicator));
  }
}

static void
task_manager_regroup_launcher_icon (TaskManager * manager,TaskIcon * grouping_icon)
{
  g_assert (TASK_IS_ICON(grouping_icon));
  g_assert (TASK_IS_MANAGER(manager));
  
  GSList * i;
  GSList * j;
  TaskManagerPrivate * priv = manager->priv;
  GtkWidget * grouping_launcher = GTK_WIDGET(task_icon_get_launcher (grouping_icon));

  g_assert (grouping_launcher);
  for (i=task_icon_get_items(grouping_icon);i; i=i->next)
  {
    TaskItem * item = i->data;
    if (TASK_IS_LAUNCHER(item))
    {
      continue;
    }
    if (g_list_find (priv->grouping_list,item))
    {
      continue;
    }
    priv->grouping_list = g_list_prepend (priv->grouping_list,item);
  }
  for (i=priv->icons; i; i=i->next)
  {
    GtkWidget * launcher;
    TaskIcon  * icon;
    icon = i->data;
    if (icon == grouping_icon)
    {
      continue;
    }
    launcher = GTK_WIDGET(task_icon_get_launcher (icon));
    if (!launcher ) /* no launcher...  need to do matches on these eventually TODO */
    {
      continue;
    }/* Is i an existing permanent launcher... if so then ignore.*/
    else if ( task_icon_count_ephemeral_items (icon) == 0)
    {
      continue;
    }
    /*ok... non-permanent launcher check if the desktop file paths match.*/
    if ( g_strcmp0 (task_launcher_get_desktop_path(TASK_LAUNCHER(grouping_launcher)),
                    task_launcher_get_desktop_path(TASK_LAUNCHER(launcher)) )==0)
    {
      for (j=task_icon_get_items(icon);j; j?j=j->next:j)
      {

        TaskItem * item = j->data;
        if (TASK_IS_LAUNCHER(item))
        {
          continue;
        }
        if (g_list_find (priv->grouping_list,item))
        {
          continue;
        }
        task_icon_moving_item (grouping_icon,icon,item);
        priv->grouping_list = g_list_prepend (priv->grouping_list,item);
        j = task_icon_get_items(icon);
      }
    }
  }
}

static void
task_manager_regroup_nonlauncher_icon (TaskManager * manager,TaskIcon * grouping_icon)
{
  g_assert (TASK_IS_ICON(grouping_icon));
  g_assert (TASK_IS_MANAGER(manager));
  
  GSList * i;
  GSList * j;
  GSList * w; 
  GSList * k;
  TaskManagerPrivate * priv = manager->priv;
  for (i=task_icon_get_items(grouping_icon);i; i=i->next)
  {
    TaskItem * item = i->data;
    if (g_list_find (priv->grouping_list,item))
    {
      continue;
    }
    priv->grouping_list = g_list_prepend (priv->grouping_list,item);
  }

  for (i=priv->icons; i; i=i->next)
  {
    TaskIcon  * icon;
    icon = i->data;    
    if (icon == grouping_icon)
    {
      continue;
    }
    if (task_icon_contains_launcher (icon) )
    {
      continue;
    }
    for (j=task_icon_get_items(icon);j;j?j=j->next:j)
    {
      gint match_score = 0;
      gint max_match_score = 0;
      TaskIcon * match = NULL;
      TaskItem * item = j->data;
      if (g_list_find (priv->grouping_list,item))
      {
        continue;
      }      
      for (w = priv->icons; w; w = w->next)
      {
        TaskIcon *taskicon = w->data;
        /*optimization....  if the icon we're checking against has a launcher
         then the item would have been grouped with it on open if it could have 
         been*/
        if ( task_icon_contains_launcher (taskicon))
        {
          continue;
        }
        match_score = task_icon_match_item (taskicon, item);
        if (match_score > max_match_score)
        {
          max_match_score = match_score;
          match = taskicon;
        }
      }
      if  (
           match && (match==grouping_icon)
           &&
           (priv->grouping || ( (task_icon_count_items(match)==1) && (task_icon_contains_launcher (match)) ) ) 
           &&
           ( max_match_score > 99-priv->match_strength))
      {
        /*we have one match in this icon.  dump all the other items in also and
         get the hell out of the inner loop.  3 nested loops nasty... short 
         circuiting as much as possible*/
        task_icon_moving_item (grouping_icon,icon,item);
        priv->grouping_list = g_list_prepend (priv->grouping_list,item);
        for (k=task_icon_get_items (icon);k;k=k->next)
        {
          if (g_list_find (priv->grouping_list,k->data))
          {
            continue;
          }                
          task_icon_moving_item (grouping_icon,icon,k->data);
          priv->grouping_list = g_list_prepend (priv->grouping_list,k->data);
        }
        j = NULL;
      }
    }
  }
}

static void
task_manager_regroup (TaskManager * manager)
{
  GSList * i;

  TaskManagerPrivate * priv = manager->priv;
  /*
   find the permanent Launchers and regroup them*/
  for (i=priv->icons; i; i=i->next)
  {
    GtkWidget * launcher;
    TaskIcon  * icon;
    icon = i->data;
    launcher = GTK_WIDGET(task_icon_get_launcher (icon));
    if (launcher && (task_icon_count_ephemeral_items (icon) == 0) )
    {
      task_manager_regroup_launcher_icon (manager,icon);      
    }
  }

  /* Find the ephemeral launchers and regroup them */
  for (i=priv->icons; i; i=i->next)
  {
    GtkWidget * launcher;
    TaskIcon  * icon;
    icon = i->data;
    launcher = GTK_WIDGET(task_icon_get_launcher (icon));
    if (launcher && (task_icon_count_ephemeral_items (icon) != 0) )
    {
      task_manager_regroup_launcher_icon (manager,icon);      
    }
  }
  /* Find the icons that do not have a launcher. */
  for (i=priv->icons; i; i=i->next)
  {
    TaskIcon  * icon;
    icon = i->data;
    if ( !task_icon_contains_launcher (icon) )
    {
      GSList *items = task_icon_get_items(icon);
      if (items)
      {
        task_manager_regroup_nonlauncher_icon (manager,icon);
      }
    }
  }
  g_list_free (priv->grouping_list);
  priv->grouping_list = NULL;
}

static void
task_manager_set_grouping (TaskManager *manager, 
                                gboolean  grouping)
{
  g_return_if_fail (TASK_IS_MANAGER (manager));

  TaskManagerPrivate *priv = manager->priv;
  priv->grouping = grouping;
  if (grouping)
  {
    task_manager_regroup(manager);
  }
  g_signal_emit (manager,_taskman_signals[GROUPING_CHANGED],0,grouping);
}

GSList *
task_manager_get_icons (TaskManager * manager)
{
  g_return_val_if_fail (TASK_IS_MANAGER (manager),NULL);

  TaskManagerPrivate *priv = manager->priv;
  return priv->icons;
}
/**
 * D-BUS functionality
 */

gboolean
task_manager_get_capabilities (TaskManager *manager,
                               GStrv *supported_keys,
                               GError **error)
{
  const gchar *known_keys[] =
  {
    "icon-file",
    "progress",
    "message",
    "visible",
    NULL
  };

  *supported_keys = g_strdupv ((char **)known_keys);

  return TRUE;
}

/*
 * Find the window that corresponds to the given window name.
 * First try to match the application name, then the normal name.
 */
static TaskWindow*
_match_name (TaskManager *manager, const gchar* window)
{
  TaskManagerPrivate *priv;
  WnckApplication *wnck_app = NULL;
  const gchar *name = NULL;
  GSList *w;

  g_return_val_if_fail (TASK_IS_MANAGER (manager), FALSE);
  priv = manager->priv;  
  
  for (w = priv->windows; w; w = w->next)
  {
    TaskWindow *taskwindow = w->data;

    if (!TASK_IS_WINDOW (taskwindow)) 
    {
      continue;
    }
    wnck_app = task_window_get_application (taskwindow);
    if (WNCK_IS_APPLICATION(wnck_app))
    {
      name = wnck_application_get_name(wnck_app);
      if (name && g_ascii_strcasecmp (window, name) == 0) 
      {/* FIXME: UTF-8 ?!*/
        return taskwindow;
      }
    }
    name = task_window_get_name (taskwindow);
    if (name && g_ascii_strcasecmp (window, name) == 0) /* FIXME: UTF-8 ?!*/
    {
      return taskwindow;
    }
  }
  return NULL;
}

/*
 * Find the window that corresponds to the given xid
 */
static TaskWindow*
_match_xid (TaskManager *manager, gint64 window)
{
  TaskManagerPrivate *priv;
  gint64 xid;
  GSList *w;

  g_return_val_if_fail (TASK_IS_MANAGER (manager), FALSE);
  priv = manager->priv;
  
  for (w = priv->windows; w; w = w->next)
  {
    TaskWindow *taskwindow = w->data;
    
    if (!TASK_IS_WINDOW (taskwindow)) continue;

    xid = task_window_get_xid (taskwindow);
    if (xid && window == xid)
      return taskwindow;
  }
  
  return NULL;
}

static GdkRegion*
xutils_get_input_shape (GdkWindow *window)
{
  GdkRegion *region = gdk_region_new ();

  GdkRectangle rect;
  XRectangle *rects;
  int count = 0;
  int ordering = 0;

  gdk_error_trap_push ();
  rects = XShapeGetRectangles (GDK_WINDOW_XDISPLAY (window),
                               GDK_WINDOW_XID (window),
                               ShapeInput, &count, &ordering);
  gdk_error_trap_pop ();

  for (int i=0; i<count; ++i)
  {
    rect.x = rects[i].x;
    rect.y = rects[i].y;
    rect.width = rects[i].width;
    rect.height = rects[i].height;

    gdk_region_union_with_rect (region, &rect);
  }
  if (rects) free(rects);

  return region;
}

/* 
 Governs the panel autohide when Intellihide is enabled.
 If a window in the relevant window list intersects with the awn panel then
 autohide will be uninhibited otherwise it will be inhibited.
 */

static void
task_manager_check_for_intersection (TaskManager * manager,
                                     WnckWorkspace * space,
                                     WnckApplication * app)
{
  TaskManagerPrivate  *priv;
  GList * windows;
  GList * iter;
  gboolean  intersect = FALSE;
  GdkRectangle awn_rect;
  gint depth;
  gint64 xid; 
  GdkRegion * updated_region;
  
  g_return_if_fail (TASK_IS_MANAGER (manager));
  priv = manager->priv;

  /*
   Generate a GdkRegion for the awn panel_size
   */
  if (!priv->awn_gdk_window)
  {
    g_object_get (manager, "panel-xid", &xid, NULL);    
    priv->awn_gdk_window = gdk_window_foreign_new ( xid);
  }
  g_return_if_fail (priv->awn_gdk_window);
  gdk_window_get_geometry (priv->awn_gdk_window,&awn_rect.x,
                           &awn_rect.y,&awn_rect.width,
                           &awn_rect.height,&depth);  
  /*
   gdk_window_get_geometry gives us an x,y or 0,0
   Fix that using get root origin.
   */
  gdk_window_get_root_origin (priv->awn_gdk_window,&awn_rect.x,&awn_rect.y);
  /*
   We cache the region for reuse for situations where the input mask is an empty
   region when the panel is hidden.  In that case we reuse the last good 
   region.
   */
  updated_region = xutils_get_input_shape (priv->awn_gdk_window);
  g_return_if_fail (updated_region);
  if (gdk_region_empty(updated_region))
  {
    gdk_region_destroy (updated_region);
  }
  else
  {
    if (priv->awn_gdk_region)
    {
      gdk_region_destroy (priv->awn_gdk_region);
    }
    priv->awn_gdk_region = updated_region; 
    gdk_region_offset (priv->awn_gdk_region,awn_rect.x,awn_rect.y);    
  }
  
  /*
   Get the list of windows to check for intersection
   */
  switch (priv->intellihide_mode)
  {
    case INTELLIHIDE_WORKSPACE:
      windows = wnck_screen_get_windows (priv->screen);
      break;
    case INTELLIHIDE_GROUP:  /*TODO... Implement this for now same as app*/
    case INTELLIHIDE_APP:
    default:
      windows = wnck_application_get_windows (app);
      break;
  }
  
  /*
   Check window list for intersection... ignoring skip tasklist and those on
   non-active workspaces
   */
  for (iter = windows; iter; iter = iter->next)
  {
    GdkRectangle win_rect;

    if (!wnck_window_is_visible_on_workspace (iter->data,space))
    {
      continue;
    }
    if (wnck_window_is_minimized(iter->data) )
    {
      continue;
    }
    if ( wnck_window_get_window_type (iter->data) == WNCK_WINDOW_DESKTOP )
    {
      continue;
    }
    if ( wnck_window_get_window_type (iter->data) == WNCK_WINDOW_DOCK )
    {
      continue;
    }   
    /*
     It may be a good idea to go the same route as we go with the 
     panel to get the GdkRectangle.  But in practice it's _probably_
     not necessary
     */
    wnck_window_get_geometry (iter->data,&win_rect.x,
                              &win_rect.y,&win_rect.width,
                              &win_rect.height);

    if (gdk_region_rect_in (priv->awn_gdk_region, &win_rect) != 
          GDK_OVERLAP_RECTANGLE_OUT)
    {
#ifdef DEBUG
      g_debug ("Intersect with %s, %d",wnck_window_get_name (iter->data),
               wnck_window_get_pid(iter->data));
#endif
      intersect = TRUE;
      break;
    }
  }
  
  /*
   Allow panel to hide (if necessary)
   */
  if (intersect && priv->autohide_cookie)
  {     
    awn_applet_uninhibit_autohide (AWN_APPLET(manager), priv->autohide_cookie);
#ifdef DEBUG
    g_debug ("me eat cookie: %u",priv->autohide_cookie);
#endif
    priv->autohide_cookie = 0;
  }
  
  /*
   Inhibit Hide if not already doing so
   */
  if (!intersect && !priv->autohide_cookie)
  {
    priv->autohide_cookie = awn_applet_inhibit_autohide (AWN_APPLET(manager), "Intellihide");
#ifdef DEBUG    
    g_debug ("cookie is %u",priv->autohide_cookie);
#endif
  }

}

/*
  Active window has changed.  If intellhide is active we need to check for 
 window instersections
 */
static void 
task_manager_active_window_changed_cb (WnckScreen *screen,
                                                   WnckWindow *previous_window,
                                                   TaskManager * manager)
{
  TaskManagerPrivate  *priv;
  WnckWindow          *win;
  WnckApplication     *app;
  WnckWorkspace       *space;

  g_return_if_fail (TASK_IS_MANAGER (manager));
  priv = manager->priv;

  if (!priv->intellihide)
  {
/*    g_warning ("%s: Intellihide callback invoked with Intellihide off",__func__);*/
    return;
  }
  
  win = wnck_screen_get_active_window (screen);
  if (!win)
  {
    /*
     This tends to happen when the last window on workspace is moved to a
     different workspace or minimized.  In which case we have a problem if 
     we had intersection and the panel was hidden, it will continue hide.
     So inhibit the autohide if there is no active window.
     */
    if (!priv->autohide_cookie)
    {
      priv->autohide_cookie = awn_applet_inhibit_autohide (AWN_APPLET(manager), "Intellihide");
#ifdef DEBUG    
      g_debug ("%s: cookie is %u",__func__,priv->autohide_cookie);
#endif
    }
    return;
  }
  app = wnck_window_get_application (win);
  space = wnck_screen_get_active_workspace (screen);
  task_manager_check_for_intersection (manager,space,app);
}
/*
 Workspace changed... check window intersections for new workspace if Intellidide
 is active 
 */
static void 
task_manager_active_workspace_changed_cb (WnckScreen    *screen,
                                                      WnckWorkspace *previous_space,
                                                      TaskManager * manager)
{
  TaskManagerPrivate *priv;
  WnckApplication     *app;
  WnckWorkspace       *space;
  WnckWindow          *win;

  g_return_if_fail (TASK_IS_MANAGER (manager));
  priv = manager->priv;
  if (!priv->intellihide)
  {
/*    g_warning ("%s: Intellihide callback invoked with Intellihide off",__func__);    */
    return;
  }
  win = wnck_screen_get_active_window (screen);
  if (!win)
  {
    if (!priv->autohide_cookie)
    {
      priv->autohide_cookie = awn_applet_inhibit_autohide (AWN_APPLET(manager), "Intellihide");
#ifdef DEBUG    
      g_debug ("%s: cookie is %u",__func__,priv->autohide_cookie);
#endif
    }    
    return;
  }
  
  app = wnck_window_get_application (win);
  space = wnck_screen_get_active_workspace (screen);

  task_manager_check_for_intersection (manager,space,app);
}
/*
 A window's geometry has channged.  If Intellihide is active then check for
 intersections
 */
static void
task_manager_win_geom_changed_cb (WnckWindow *window, TaskManager * manager)
{
/*
   TODO... only check for intersect if window is in the active application.
   practically speaking it should be in most cases */
  TaskManagerPrivate  *priv;
  WnckWindow          *win;
  WnckApplication     *app;
  WnckWorkspace       *space;
  g_return_if_fail (TASK_IS_MANAGER (manager));
  priv = manager->priv;
 
  if (!priv->intellihide)
  {
/*    g_warning ("%s: Intellihide callback invoked with Intellihide off",__func__);*/
    return;
  }
  win = wnck_screen_get_active_window (priv->screen);
  if (!win)
  {
    return;
  }
  app = wnck_window_get_application (win);
  space = wnck_screen_get_active_workspace (priv->screen);
  task_manager_check_for_intersection (manager,space,app);  
}

static void task_manager_win_state_changed_cb (WnckWindow * window,
                                               WnckWindowState changed_mask,
                                               WnckWindowState new_state,
                                               TaskManager * manager)
{
  TaskManagerPrivate  *priv;
  WnckWindow          *win;
  WnckApplication     *app;
  WnckWorkspace       *space;
  g_return_if_fail (TASK_IS_MANAGER (manager));
  priv = manager->priv;
   
  if (!priv->intellihide)
  {
    return;
  }
  win = wnck_screen_get_active_window (priv->screen);
  if (!win)
  {
    return;
  }
  app = wnck_window_get_application (win);
  space = wnck_screen_get_active_workspace (priv->screen);
  task_manager_check_for_intersection (manager,space,app);  
  
}

static GQuark
task_manager_error_quark (void)
{
  static GQuark quark = 0;
  if (!quark)
    quark = g_quark_from_static_string ("task_manager_error");
  return quark;
}

static GType
task_manager_error_get_type (void)
{
  static GType etype = 0;

  if (!etype)
  {
#define ENUM_ENTRY(NAME, DESC) { NAME, "" #NAME "", DESC }
    static const GEnumValue values[] =
    {
      // argh! DBus enums can't have spaces in the nick!
      ENUM_ENTRY(TASK_MANAGER_ERROR_UNSUPPORTED_WINDOW_TYPE,
                 "UnsupportedWindowType"),
      ENUM_ENTRY(TASK_MANAGER_ERROR_NO_WINDOW_MATCH,
                 "NoWindowMatch"),
      { 0, 0, 0 }
    };

    etype = g_enum_register_static ("TaskManagerError", values);
  }

  return etype;
}

static void
task_manager_set_windows_visibility (TaskManager *manager,const gchar * name,gboolean visible)
{
  GSList * iter;
  TaskManagerPrivate *priv;

  priv = manager->priv;
  for (iter = priv->windows;iter; iter=iter->next)
  {
    gchar * res_name=NULL;
    gchar * class_name=NULL;
    TaskItem * item = iter->data;
    
    g_assert (TASK_IS_WINDOW(item));
    task_window_get_wm_class(TASK_WINDOW (item), &res_name, &class_name);    

    if (  (g_strcmp0(name,res_name)==0)||
        (g_strcmp0(name,class_name)==0) )
    {
      task_window_set_hidden (TASK_WINDOW(item),!visible);
    }
    g_free (res_name);
    g_free (class_name);
  }
}

gboolean
task_manager_update (TaskManager *manager,
                     GValue *window,
                     GHashTable *hints, /* mappings from string to GValue */
                     GError **error)
{
  TaskManagerPrivate *priv;
  TaskWindow *matched_window = NULL;

  g_return_val_if_fail (TASK_IS_MANAGER (manager), FALSE);
  
  priv = manager->priv;
  
  if (G_VALUE_HOLDS_STRING (window))
  {
    matched_window = _match_name (manager, g_value_get_string (window));
  }
  else if (G_VALUE_HOLDS_INT64 (window))
  {
    matched_window = _match_xid (manager, g_value_get_int64 (window));
  }
  else if (G_VALUE_HOLDS_INT (window))
  {
    matched_window = _match_xid (manager, g_value_get_int (window));
  }
  else
  {
    g_set_error (error, 
                 task_manager_error_quark (),
                 TASK_MANAGER_ERROR_UNSUPPORTED_WINDOW_TYPE,
                 "Window can be specified only by its name or XID");

    return FALSE;
  }
  
  if (matched_window)
  {
    GHashTableIter iter;
    gpointer key, value;

    g_hash_table_iter_init (&iter, hints);
    while (g_hash_table_iter_next (&iter, &key, &value)) 
    {
      gchar *key_name = (gchar *)key;
      if (strcmp ("icon-file", key_name) == 0)
      {
        //g_debug ("Request to change icon-file...");
        TaskItem *item = TASK_ITEM (matched_window);
        if (item->icon_overlay == NULL)
        {
          item->icon_overlay = awn_overlay_pixbuf_file_new (NULL);
          g_object_set (G_OBJECT (item->icon_overlay),
                        "use-source-op", TRUE,
                        "scale", 1.0, NULL);
          GtkWidget *image = task_item_get_image_widget (item);
          AwnOverlayable *over = AWN_OVERLAYABLE (image);
          awn_overlayable_add_overlay (over,
                                       AWN_OVERLAY (item->icon_overlay));
        }

        const gchar* filename = g_value_get_string (value);
        g_object_set (G_OBJECT (item->icon_overlay),
                      "active", filename && filename[0] != '\0', NULL);
        if (filename && filename[0] != '\0')
        {
          g_object_set_property (G_OBJECT (item->icon_overlay),
                                 "file-name", value);
        }

        // this refreshes the overlays on TaskIcon
        task_item_set_task_icon (item, task_item_get_task_icon (item));
      }
      else if (strcmp ("progress", key_name) == 0)
      {
        //g_debug ("Request to change progress...");
        TaskItem *item = TASK_ITEM (matched_window);
        if (item->progress_overlay == NULL)
        {
          item->progress_overlay = awn_overlay_progress_circle_new ();
          GtkWidget *image = task_item_get_image_widget (item);
          AwnOverlayable *over = AWN_OVERLAYABLE (image);
          awn_overlayable_add_overlay (over,
                                       AWN_OVERLAY (item->progress_overlay));
        }

        g_object_set (G_OBJECT (item->progress_overlay),
                      "active", g_value_get_int (value) != -1, NULL);
        if (g_value_get_int (value) != -1)
        {
          g_object_set_property (G_OBJECT (item->progress_overlay),
                                 "percent-complete", value);
        }

        // this refreshes the overlays on TaskIcon
        task_item_set_task_icon (item, task_item_get_task_icon (item));
      }
      else if (strcmp ("message", key_name) == 0)
      {
        //g_debug ("Request to change message...");
        TaskItem *item = TASK_ITEM (matched_window);
        if (item->text_overlay == NULL)
        {
          item->text_overlay = awn_overlay_text_new ();
          g_object_set (G_OBJECT (item->text_overlay),
                        "font-sizing", AWN_FONT_SIZE_LARGE, NULL);
          GtkWidget *image = task_item_get_image_widget (item);
          AwnOverlayable *over = AWN_OVERLAYABLE (image);
          awn_overlayable_add_overlay (over, AWN_OVERLAY (item->text_overlay));
        }

        const gchar* text = g_value_get_string (value);
        g_object_set (G_OBJECT (item->text_overlay),
                      "active", text && text[0] != '\0', NULL);
        if (text && text[0] != '\0')
        {
          g_object_set_property (G_OBJECT (item->text_overlay), "text", value);
        }

        // this refreshes the overlays on TaskIcon
        task_item_set_task_icon (item, task_item_get_task_icon (item));
      }
      else if (strcmp ("visible", key_name) == 0)
      {
        gboolean visible = g_value_get_boolean (value);
        if (G_VALUE_HOLDS_STRING (window))
        {
          GList * needle = g_list_find_custom (priv->hidden_list,
                                               g_value_get_string(window),
                                               (GCompareFunc)g_strcmp0);
          if (visible)
          {
            if (needle)
            {
              g_free (needle->data);
              priv->hidden_list = g_list_delete_link (priv->hidden_list,needle);
            }
          }
          else
          {
            if (!needle)
            {
              priv->hidden_list = g_list_append (priv->hidden_list, g_value_dup_string(window));
            }
          }
          task_manager_set_windows_visibility (manager,g_value_get_string(window),visible);
        }
        task_window_set_hidden (TASK_WINDOW(matched_window),!visible);
      }
      else
      {
        g_debug ("Taskmanager doesn't understand the key: %s", key_name);
      }
    }
    
    return TRUE;
  }
  else
  {
    GHashTableIter iter;
    gpointer key, value;
    gboolean null_op = TRUE;

    g_hash_table_iter_init (&iter, hints);
    while (g_hash_table_iter_next (&iter, &key, &value)) 
    {
      gchar *key_name = (gchar *)key;
      if (strcmp ("visible", key_name) == 0)
      {
        gboolean visible = g_value_get_boolean (value);
        null_op = FALSE;
        if (G_VALUE_HOLDS_STRING (window))
        {
          GList * needle = g_list_find_custom (priv->hidden_list,
                                               g_value_get_string(window),
                                               (GCompareFunc)g_strcmp0);          
          if (visible)
          {
            if (needle)
            {
              g_free (needle->data);
              priv->hidden_list = g_list_delete_link (priv->hidden_list,needle);
            }
          }
          else
          {
            if (!needle)
            {
              priv->hidden_list = g_list_append (priv->hidden_list, g_value_dup_string(window));
            }
          }
          task_manager_set_windows_visibility (manager,g_value_get_string(window),visible);          
        }
      }
    }
    if (null_op)
    {
      g_set_error (error,
                 task_manager_error_quark (),
                 TASK_MANAGER_ERROR_NO_WINDOW_MATCH,
                 "No matching window found to update.");
      return FALSE;
    }
    return TRUE;
  }
}


/*
 * Position Icons through dragging
 */

static void
_drag_dest_motion(TaskManager *manager, gint x, gint y, GtkWidget *icon)
{
  gint move_to, moved;
  GList* childs;
  TaskManagerPrivate *priv;
  GtkPositionType position;
  guint size;
  double action;
  
  g_return_if_fail (TASK_IS_MANAGER (manager));

  priv = TASK_MANAGER_GET_PRIVATE (manager);

  g_return_if_fail(priv->dragged_icon != NULL);

  /* There was a timeout to check if the cursor left the bar */
  if(priv->drag_timeout)
  {
    g_source_remove (priv->drag_timeout);
    priv->drag_timeout = 0;
  }

  position = awn_applet_get_pos_type (AWN_APPLET(manager));
  size = awn_applet_get_size (AWN_APPLET(manager));
  childs = gtk_container_get_children (GTK_CONTAINER(priv->box));
  move_to = g_list_index (childs, GTK_WIDGET(icon));
  moved = g_list_index (childs, GTK_WIDGET(priv->drag_indicator));

  g_return_if_fail (move_to != -1);
  g_return_if_fail (moved != -1);

  if(position == GTK_POS_TOP || position == GTK_POS_BOTTOM)
    action = (double)x/size;
  else
    action = (double)y/size;

  if(action < 0.5)
  {
    if(moved > move_to)
    {
      gtk_box_reorder_child (GTK_BOX(priv->box), GTK_WIDGET(priv->drag_indicator), move_to);
    }
    gtk_widget_show(GTK_WIDGET(priv->drag_indicator));
  }
  else
  {
    if(moved < move_to)
    {
      gtk_box_reorder_child (GTK_BOX(priv->box), GTK_WIDGET(priv->drag_indicator), move_to);
    }
    gtk_widget_show(GTK_WIDGET(priv->drag_indicator));
  }
}

static void 
_drag_source_begin(TaskManager *manager, GtkWidget *icon)
{
  g_return_if_fail (TASK_IS_MANAGER (manager));

  TaskManagerPrivate *priv = TASK_MANAGER_GET_PRIVATE (manager);

  /* dragging starts, so setup everything */
  if(!priv->dragged_icon)
  {
    g_return_if_fail (TASK_IS_ICON (icon));
    priv->dragged_icon = TASK_ICON(icon);
    gtk_widget_hide (GTK_WIDGET(icon));

    //it will move the drag_indicator to the right spot
    _drag_dest_motion(manager, 0, 0, icon);
  }
#ifdef DEBUG  
  else 
  {   
    g_print("ERROR: previous drag wasn't done yet ?!");
    g_return_if_reached();
  }
#endif  
}

static void 
_drag_source_fail(TaskManager *manager, GtkWidget *icon)
{
  g_return_if_fail (TASK_IS_MANAGER (manager));

  TaskManagerPrivate *priv = TASK_MANAGER_GET_PRIVATE (manager);

  if(!priv->dragged_icon) return;

  //gtk_widget_hide (GTK_WIDGET (priv->drag_indicator));
  //gtk_widget_show (GTK_WIDGET (priv->dragged_icon));
  //priv->dragged_icon = NULL;

  // Handle a fail like a drop for now
  _drag_source_end(manager, NULL);
}

static void 
_drag_dest_leave (TaskManager *manager, GtkWidget *icon)
{
  g_return_if_fail (TASK_IS_MANAGER (manager));

  /*
  TaskManagerPrivate *priv = TASK_MANAGER_GET_PRIVATE (manager);

  //FIXME: REMOVE OLD TIMER AND SET NEW ONE
  if(!priv->drag_timeout)
    priv->drag_timeout = g_timeout_add (200, (GSourceFunc)drag_leaves_task_manager, manager);
  */
}

//static gboolean
//drag_leaves_task_manager (TaskManager *manager)
//{
//  g_return_val_if_fail (TASK_IS_MANAGER (manager), FALSE);
//
//  TaskManagerPrivate *priv = TASK_MANAGER_GET_PRIVATE (manager);
//
//  gtk_widget_hide(GTK_WIDGET(priv->drag_indicator));
//
//  return FALSE;
//}

static void 
_drag_source_end(TaskManager *manager, GtkWidget *icon)
{
  TaskManagerPrivate *priv;
  gint move_to;
  GList * childs;
  GList * iter;
  const gchar * moved_desktop_file;
  const gchar * following_desktop_file = NULL;
  GValueArray *launcher_paths;
  GValue moving_val = {0,};
  TaskLauncher * launcher = NULL;
  
  g_return_if_fail (TASK_IS_MANAGER (manager));

  priv = TASK_MANAGER_GET_PRIVATE (manager);
  if(priv->dragged_icon == NULL)
    return;

  if(priv->drag_timeout)
  {
    g_source_remove (priv->drag_timeout);
    priv->drag_timeout = 0;
  }

  // Move the AwnIcon to the dropped place 
  childs = gtk_container_get_children (GTK_CONTAINER(priv->box));
  move_to = g_list_index (childs, GTK_WIDGET (priv->drag_indicator));
  gtk_box_reorder_child (GTK_BOX (priv->box), GTK_WIDGET (priv->dragged_icon), move_to);

  // Hide the DragIndicator and show AwnIcon again 
  gtk_widget_hide (GTK_WIDGET (priv->drag_indicator));
  gtk_widget_show (GTK_WIDGET (priv->dragged_icon));

  // If workspace isn't the same as the active one,
  // it means you switched to another workspace when dragging.
  // This means you want the window on that screen.
  // FIXME: Make a way to get the WnckWindow out of AwnIcon
 
  //screen = wnck_window_get_screen(priv->window);
  //active_workspace = wnck_screen_get_active_workspace (screen);
  //if (active_workspace && !wnck_window_is_on_workspace(priv->window, active_workspace))
  //  wnck_window_move_to_workspace(priv->window, active_workspace);

  // Update the position in the config (Gconf) if the AwnIcon is a launcher.
  // FIXME: support multiple launchers in one AwnIcon?

  /*
   Get the name of the desktop file.
   Get the name of the desktop file that next appears after it.
   Search the launcherlist for the moved desktop file.
     If there is no follower then move it to the end.
   Else
    Search the launcherlist for the following desktop file
    Place the moved desktop in front of it.
   */
  if ( priv->dragged_icon && !task_icon_count_ephemeral_items (priv->dragged_icon))
  {
    g_assert (TASK_IS_ICON (priv->dragged_icon) );
    launcher = TASK_LAUNCHER(task_icon_get_launcher (priv->dragged_icon));
  }
      
  if (launcher)
  {
    g_assert (TASK_IS_LAUNCHER(launcher));
    moved_desktop_file = task_launcher_get_desktop_path (launcher);
    // get the updated list
    childs = gtk_container_get_children (GTK_CONTAINER(priv->box));
    for (iter = g_list_first (childs);iter;iter=g_list_next(iter))
    {
      if (!TASK_IS_ICON(iter->data))
      {
        continue;
      }
      g_assert (TASK_IS_ICON (iter->data) );      
      if (priv->dragged_icon == iter->data)
      {
        for (iter = g_list_next(iter);iter;iter = g_list_next(iter) )
        {
          if (!iter)
          {
            continue;
          }
          if (!TASK_IS_ICON(iter->data))
          {
            continue;
          }
          g_assert (TASK_IS_ICON (iter->data) );
          TaskItem * item = task_icon_get_launcher (iter->data);
          if (!item || task_icon_count_ephemeral_items (iter->data))
          {
            continue;
          }
          following_desktop_file = task_launcher_get_desktop_path (TASK_LAUNCHER(item));
          break;
        }
        break;
      }
    }
    g_object_get (G_OBJECT (manager), "launcher_paths", &launcher_paths, NULL);
    g_value_init (&moving_val,G_TYPE_STRING);
    g_value_set_string (&moving_val, moved_desktop_file);
    
    for (guint idx = 0; idx < launcher_paths->n_values; idx++)
    {
      gchar *path;
      path = g_value_dup_string (g_value_array_get_nth (launcher_paths, idx));
      if (g_strcmp0 (path,moved_desktop_file)==0)
      {
        g_free (path);        
        g_value_array_remove (launcher_paths,idx);
        if (!following_desktop_file)
        {
          g_value_array_append (launcher_paths,&moving_val);
        }
        else
        {
          for (idx = 0; idx < launcher_paths->n_values; idx++)
          {
            path = g_value_dup_string (g_value_array_get_nth (launcher_paths, idx));
            if (g_strcmp0 (path,following_desktop_file)==0)
            {
              g_value_array_insert (launcher_paths,idx,&moving_val);
              g_free (path);
              break;
            }
          }
        }
        g_object_set (G_OBJECT (manager), "launcher_paths", launcher_paths, NULL);                            
        break;
      }
      g_free (path);
      task_manager_refresh_launcher_paths (manager, launcher_paths);
    }
    g_value_unset (&moving_val);    
    g_value_array_free (launcher_paths);
  }
  priv->dragged_icon = NULL;
}

static void 
_drag_add_signals (TaskManager *manager, GtkWidget *icon)
{
  g_return_if_fail (TASK_IS_MANAGER (manager));
  g_return_if_fail (TASK_IS_ICON (icon)||TASK_IS_DRAG_INDICATOR (icon));

  // listen to signals of 'source' start getting dragged, only if it is an icon
  if(TASK_IS_ICON (icon))
  {
    g_object_set(icon, "draggable", TRUE, NULL);
    g_signal_connect_swapped (icon, "source_drag_begin", G_CALLBACK (_drag_source_begin), manager);
    g_signal_connect_swapped (icon, "source_drag_end", G_CALLBACK (_drag_source_end), manager);
    g_signal_connect_swapped (icon, "source_drag_fail", G_CALLBACK (_drag_source_fail), manager);
  }

  g_signal_connect_swapped (icon, "dest_drag_motion", G_CALLBACK (_drag_dest_motion), manager);
  g_signal_connect_swapped (icon, "dest_drag_leave", G_CALLBACK (_drag_dest_leave), manager);
}

static void 
_drag_remove_signals (TaskManager *manager, GtkWidget *icon)
{
  g_return_if_fail (TASK_IS_MANAGER (manager));
  g_return_if_fail (TASK_IS_ICON (icon)||TASK_IS_DRAG_INDICATOR (icon));

  if(TASK_IS_ICON (icon))
  {
    g_object_set(icon, "draggable", FALSE, NULL);
    g_signal_handlers_disconnect_by_func(icon, G_CALLBACK (_drag_source_begin), manager);
    g_signal_handlers_disconnect_by_func(icon, G_CALLBACK (_drag_source_end), manager);
    g_signal_handlers_disconnect_by_func(icon, G_CALLBACK (_drag_source_fail), manager);
  }

  g_signal_handlers_disconnect_by_func(icon, G_CALLBACK (_drag_dest_motion), manager);
  g_signal_handlers_disconnect_by_func(icon, G_CALLBACK (_drag_dest_leave), manager);
}