File: RosterTree.tcl

package info (click to toggle)
coccinella 0.96.20-7
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 13,108 kB
  • ctags: 5,908
  • sloc: tcl: 124,744; xml: 206; makefile: 66; sh: 62
file content (2099 lines) | stat: -rw-r--r-- 55,314 bytes parent folder | download | duplicates (4)
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
#  RosterTree.tcl ---
#  
#      This file is part of The Coccinella application. 
#      It implements the roster tree using treectrl.
#      
#  Copyright (c) 2005-2008  Mats Bengtsson
#  
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#   
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#   
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#  
# $Id: RosterTree.tcl,v 1.110 2008-08-07 14:57:20 matben Exp $

#-INTERNALS---------------------------------------------------------------------
#
#   We keep an invisible column cTag for tag storage, and an array 'tag2items'
#   that maps a tag to a set of items. 
#   One jid can belong to several groups (bad) which doesn't make {jid $jid}
#   unique!
#   Always use mapped JIDs.
# 
#   tags:
#     {head available/unavailable/transport/pending}
#     {jid $jid}                      <- not unique if belongs to many groups!
#     {group $group $presence}        <- note

package require TreeCtrlDnD

package provide RosterTree 1.0

namespace eval ::RosterTree {

    ::hooks::register initHook               ::RosterTree::InitHook
    ::hooks::register logoutHook             ::RosterTree::LogoutHook
    ::hooks::register quitAppHook            ::RosterTree::QuitHook
    ::hooks::register menuJMainFilePostHook  ::RosterTree::FileMenuPostHook
    ::hooks::register menuJMainEditPostHook  ::RosterTree::EditMenuPostHook
    ::hooks::register onMenuVCardExport      ::RosterTree::OnMenuExportVCardHook
    ::hooks::register nicknameEventHook      ::RosterTree::NicknameEventHook

    # Actual:
    option add *RosterTree.borderWidth        0               50
    option add *RosterTree.relief             flat            50
    #option add *Roster*TreeCtrl.indent          18              widgetDefault

    # Fake:
#     option add *Roster*TreeCtrl.rosterImage     cociexec    widgetDefault
    option add *Roster*TreeCtrl.rosterImage    roster-default   widgetDefault

    # This is tuned with the control slots so that the close buttons line up.
    option add *Roster*TSearch.padding    {6 2 2 2}          50

    # @@@ Should get this from a global reaource.
    variable buttonPressMillis 1000

    # Head titles.
    variable mcHead
    array set mcHead [list \
      available     [mc "Online"]         \
      unavailable   [mc "Offline"]        \
      transport     [mc "Transports"]     \
      pending       [mc "Subscription Pending"]]
    
    # How should JIDs be formatted before export to DnD?
    # Alternative "xmpp:%s?message"
    set ::config(rost,dnd-xmpp-uri-format) "xmpp:%s"
}

namespace eval ::RosterTree {
    
    variable dndSrc
    array set dndSrc {
	suffix,win32    .URL
	suffix,x11      .desktop
	content,win32   "\[InternetShortcut\]\nURL=%s"
	content,x11     "\[Desktop Entry\]\nEncoding=UTF-8\nIcon=xmpp\nType=Link\nURL=%s"
	suffix,aqua     .xxx
	content,aqua    ""
    }    
}

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
# A plugin system for roster styles.

namespace eval ::RosterTree {
    variable plugin

    set plugin(selected) "plain"
    set plugin(previous) ""

    ::PrefUtils::Add [list  \
      [list ::RosterTree::plugin(selected)  rosterTree_selected  $plugin(selected)]]
}

# RosterTree::RegisterStyle --
# 
#       Basic registration function for styles.

proc ::RosterTree::RegisterStyle {
    name 
    label 
    configProc 
    initProc 
    deleteProc
    createItemProc 
    deleteItemProc 
    setItemAltProc
} {
	
    variable plugin
    
    set plugin($name,name)        $name
    set plugin($name,label)       $label
    set plugin($name,config)      $configProc
    set plugin($name,init)        $initProc
    set plugin($name,delete)      $deleteProc
    set plugin($name,createItem)  $createItemProc
    set plugin($name,deleteItem)  $deleteItemProc
    set plugin($name,setItemAlt)  $setItemAltProc
}

# RosterTree::RegisterStyleSort --
# 
#       Optional registration of sort proc.

proc ::RosterTree::RegisterStyleSort {name sortProc} {    
    variable plugin
    
    set plugin($name,sortProc) $sortProc
}

# RosterTree::RegisterStyleFindColumn --
# 
#       The generic find megawidget needs to know which column to search.
#       If a style doesn't register it it means no find possible.

proc ::RosterTree::RegisterStyleFindColumn {name findColumn} {    
    variable plugin
    
    set plugin($name,findColumn) $findColumn
}

proc ::RosterTree::GetStyleFindColumn {} {
    variable plugin
    
    set name $plugin(selected)
    if {[info exists plugin($name,findColumn)]} {
	return $plugin($name,findColumn)
    } else {
	return 
    }
}    

proc ::RosterTree::SetStyle {name} {
    variable plugin

    set plugin(previous) $plugin(selected)
    set plugin(selected) $name
}

proc ::RosterTree::GetStyle {} {
    variable plugin
    
    return $plugin(selected)
}

proc ::RosterTree::GetPreviousStyle {} {
    variable plugin
    
    return $plugin(previous)
}

proc ::RosterTree::GetAllStyles {} {
    variable plugin
 
    set names [list]
    foreach {key name} [array get plugin *,name] {
	lappend names $name
    }
    set styles [list]
    foreach name [lsort $names] {
	lappend styles $name $plugin($name,label)
    }
    return $styles
}

proc ::RosterTree::StyleConfigure {w} {
    variable plugin
    
    set name $plugin(selected)
    $plugin($name,config) $w
}

proc ::RosterTree::StyleInit {} {
    variable plugin
    
    set name $plugin(selected)
    $plugin($name,init)
}

proc ::RosterTree::StyleDelete {name} {
    variable plugin
    
    $plugin($name,delete)
}

# RosterTree::StyleCreateItem --
# 
#       Dispatch tree item creation and configure any alternatives.
# 
# Arguments:
#       jid         for available JID always use the JID as reported in the
#                   presence 'from' attribute.
#                   for unavailable JID always us the roster item JID.
#       presence    "available" or "unavailable"
#       args        list of '-key value' pairs of presence and roster
#                   attributes.
#       
# Results:
#       treectrl item.

proc ::RosterTree::StyleCreateItem {jid presence args} {
    variable plugin
    
    set name $plugin(selected)
    set items [eval {$plugin($name,createItem) $jid $presence} $args]
    StyleConfigureAltImages $jid
    return $items
}

proc ::RosterTree::StyleDeleteItem {jid} {
    variable plugin
    
    set name $plugin(selected)
    $plugin($name,deleteItem) $jid
}

# RosterTree::StyleSetItemAlternative --
# 
#       Sets an alternative image or text for the specified jid.
#       An alternative attribute set here is only a hint to the roster style
#       which is free to ignore it.
#       The roster item may not be displayed, for instance, there can be
#       a higher resource set.
#       
# Arguments:
#       jid
#       key         a unique token that specifies a set of images or texts
#       type        text or image
#       value       the text or the image to set, or empty if unset
#       
# Results:
#       list {treeCtrlWidget item columnTag elementName}

proc ::RosterTree::StyleSetItemAlternative {jid key type value} {
    variable plugin
    
    switch -- $type {
	image {
	    StyleCacheAltImage $jid $key $value
	}
    }
    set name $plugin(selected)
    return [$plugin($name,setItemAlt) $jid $key $type $value]
}

proc ::RosterTree::StyleCacheAltImage {jid key value} {
    variable altImageCache

    # We must cache this info: jid -> {key value ...}
    # @@@ When to free this cache? Logout? Unavailable?
    if {[info exists altImageCache($jid)]} {
	array set tmp $altImageCache($jid)
	if {$value eq ""} {
	    array unset tmp $key
	    if {[array size tmp]} {
		set altImageCache($jid) [array get tmp]
	    } else {
		unset altImageCache($jid)
	    }
	} else {
	    set tmp($key) $value
	    set altImageCache($jid) [array get tmp]
	}
    } else {
	set altImageCache($jid) [list $key $value]
    }
}

proc ::RosterTree::StyleConfigureAltImages {jid} {
    variable plugin
    variable altImageCache
    
    if {[info exists altImageCache($jid)]} {
	set name $plugin(selected)
	foreach {key value} $altImageCache($jid) {
	     $plugin($name,setItemAlt) $jid $key image $value
	}
    }
}

proc ::RosterTree::GetItemAlternatives {jid type} {
    variable altImageCache
    
    switch -- $type {
	image {
	    if {[info exists altImageCache($jid)]} {
		return $altImageCache($jid)
	    } else {
		return {}
	    }
	}
    }
}

# Assumes that alternatives only for online users.

proc ::RosterTree::FreeItemAlternatives {jid} {
    variable altImageCache

    unset -nocomplain altImageCache($jid)
}

proc ::RosterTree::FreeAllAltImagesCache {} {
    variable altImageCache
    
    unset -nocomplain altImageCache
}
    
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

# RosterTree::LoadStyle --
# 
#       Organizes everything to change roster style on the fly.

proc ::RosterTree::LoadStyle {name} {
    variable T

    Free
    SetBinds
    SetStyle $name
    StyleConfigure $T
    ::Roster::RepopulateTree    

    set previous [GetPreviousStyle]
    if {$previous ne ""} {
	StyleDelete $previous
    }
    
    # Guard against a situation where we have no search support.
    if {[GetStyleFindColumn] eq ""} {
	FindDestroy
    } else {
	FindReset
    }
}

# RosterTree::New --
# 
#       Create the treectrl widget and do common initializations.

proc ::RosterTree::New {w} {
    variable T
    variable wfind
    
    # D = -border 1 -relief sunken
    frame $w -class RosterTree

    set wxsc    $w.xsc
    set wysc    $w.ysc
    set wtree   $w.tree
    set wfind   $w.find
    
    set T $wtree
	    
    treectrl $T -selectmode extended  \
      -showroot 0 -showrootbutton 0 -showbuttons 1 -showheader 0  \
      -xscrollcommand [list ::UI::ScrollSetStdGrid $wxsc     \
      [list grid $wxsc -row 1 -column 0 -sticky ew]]         \
      -yscrollcommand [list ::UI::ScrollSetStdGrid $wysc     \
      [list grid $wysc -row 0 -column 1 -sticky ns]]         \
      -borderwidth 0 -highlightthickness 0 -height 0 -width 0
    
    SetBinds
    $T configure -backgroundimage [BackgroundImageGet]
    $T notify bind $T <Selection> {+::RosterTree::Selection }
    
    # RosterTreeTag will be first.
    bindtags $T [concat RosterTreeTag [bindtags $T]]
    
    # This automatically cleans up the tag array.
    $T notify bind RosterTreeTag <ItemDelete> {
	foreach item %i {
	    ::RosterTree::RemoveTags $item
	} 
    }
    
    # Need a post TreeCtrl bind tag for ThemeChanged.
    set idx [lsearch [bindtags $T] TreeCtrl]
    bindtags $T [linsert [bindtags $T] [incr idx] TreeCtrlPost]
    
    StyleConfigure $wtree
    StyleInit

    ttk::scrollbar $wxsc -command [list $wtree xview] -orient horizontal
    ttk::scrollbar $wysc -command [list $wtree yview] -orient vertical

    grid  $wtree  -row 0 -column 0 -sticky news
    grid  $wysc   -row 0 -column 1 -sticky ns
    grid  $wxsc   -row 1 -column 0 -sticky ew
    grid columnconfigure $w 0 -weight 1
    grid rowconfigure    $w 0 -weight 1
    
    # DnD:
    set idx [lsearch [bindtags $T] TreeCtrl]
    bindtags $T [linsert [bindtags $T] $idx TreeCtrlDnD]
    
    $T notify install <Drag-begin>
    $T notify install <Drag-end>
    $T notify install <Drag-receive>
    $T notify install <Drag-enter>
    $T notify install <Drag-leave>
    
    $T notify bind $T <Drag-receive> {
	::RosterTree::NotifyDragReceive %W %l %I
    }    
    if {[tk windowingsystem] ne "aqua"} {
	if {![catch {package require tkdnd}]} {
	    InitDnD $T
	}
    }

    return $T
}

# DnD files to roster items.

proc ::RosterTree::InitDnD {win} {
    
    # Targets:
    dnd bindtarget $win text/uri-list <Drop> {
	::RosterTree::DnDDrop %W %D %T %x %y
    }
    dnd bindtarget $win text/uri-list <Drag> {
	::RosterTree::DnDDrag %W %A %a %D %T %x %y
    }
    dnd bindtarget $win text/uri-list <DragEnter> {
	::RosterTree::DnDEnter %W %A %D %T
    }
    dnd bindtarget $win text/uri-list <DragLeave> {
	::RosterTree::DnDLeave %W %D %T
    }

    # Sources, export both xmpp:JID as text and as a file:
    dnd bindsource $win {text/plain;charset=UTF-8} { 
	::RosterTree::DnDTextSource %W
    }
    dnd bindsource $win text/uri-list { 
	::RosterTree::DnDFileSource %W
    }
    
    # Need to bind on Leave else we destroy the TreeCtrl DnD bindings.
    bind $win <Button1-Leave> { dnd drag %W }
}

# RosterTree::DnDTextSource --
# 
#       Defines what to "export" from roster when dragging to the desktop.

proc ::RosterTree::DnDTextSource {win} {
    global  config
        
    # We shall export a format other applications have a chance to understand.
    # Our own targets must also understand this format.
    set fmt $config(rost,dnd-xmpp-uri-format)
    set jidL [lapply [list format $fmt] [lapply jlib::barejid [GetExtSelectedJID]]]
    set data [join $jidL ", "]
    return $data
}

# RosterTree::DnDFileSource --
# 
#       Defines what to "export" from roster when dragging to the desktop.

proc ::RosterTree::DnDFileSource {win} {
    global  this
    variable dndSrc

    set os [tk windowingsystem]
    set jidL [lapply jlib::jidmap [lapply jlib::barejid [GetExtSelectedJID]]]
    set fileL [list]
    foreach jid $jidL {
	set fileName [file join $this(tmpPath) [::uri::urn::quote $jid]]$dndSrc(suffix,$os)
	set fd [open $fileName w]
	puts $fd [format $dndSrc(content,$os) "xmpp:$jid?message"]
 	close $fd
	# @@@ Do I need a "file://" prefix?
	#lappend fileL $fileName
	lappend fileL "file://$fileName"
    }

    return $fileL
}

proc ::RosterTree::DnDDrop {win data dndtype x y} {

    set T $win
    set f [lindex $data 0]
    set f [string map {"file://" ""} $f]
    set f [::uri::urn::unquote $f]
    set id [$T identify $x $y]
    if {[lindex $id 0] eq "item"} {
	lassign $id where item arg1 arg2 arg3 arg4
	if {($arg1 eq "column") && ($arg3 eq "elem")} {
	    set tag [GetTagOfItem $item]
	    if {[lindex $tag 0] eq "jid"} {
		set jid [lindex $tag 1]
		if {[::Jabber::Jlib roster isavailable $jid]} {
		    ::FTrans::Send $jid -filename $f
		}
	    }
	}
    }
}

proc ::RosterTree::DnDDrag {win action actions data dndtype x y} {
    
    set T $win
    set id [$T identify $x $y]
    set act "none"
    $T selection clear

    if {[lindex $id 0] eq "item"} {
	lassign $id where item arg1 arg2 arg3 arg4
	if {($arg1 eq "column") && ($arg3 eq "elem")} {
	    set tag [GetTagOfItem $item]
	    if {[lindex $tag 0] eq "jid"} {
		set jid [lindex $tag 1]
		if {[::Jabber::Jlib roster isavailable $jid]} {
		    $T selection add $item
		    set act "copy"
		}
	    }
	}
    }
    return $act
}

proc ::RosterTree::DnDEnter {win action data dndtype} {
    focus $win
    set act "none"
    return $act
}

proc ::RosterTree::DnDLeave {win data dndtype} {	
    focus [winfo toplevel $win] 
}

# copy or move a contacts between roster groups
# a contact can be a member of multiple groups
proc ::RosterTree::NotifyDragReceive {T dragged target} {
    global  wDlgs
    variable popMenuDefs

    set jlib [::Jabber::GetJlib]
        
    # Protect for a situation where items have disapperared.
    if {[$T item id $target] eq ""} {
	return
    }

    # Find target group or empty.
    set tag [GetTagOfItem $target]
    set tag0 [lindex $tag 0]
    if {$tag0 eq "jid"} {
	set parent [$T item parent $target]
	# some styles, like the avatar, do not have a "head"
	if {$parent eq 0} {
	    set tgroup ""
	} else {
	    set ptag [GetTagOfItem $parent]
	    set ptag0 [lindex $ptag 0]
	    if {$ptag0 eq "group"} {
	        set tgroup [list [lindex $ptag 1]]
	    } elseif {$ptag0 eq "head"} {
	        set tgroup ""
	    } else {
	        return
	    }
	}
    } elseif {$tag0 eq "group"} {
	set tgroup [list [lindex $tag 1]]
    } elseif {$tag0 eq "head"} {
	set tgroup ""	
    } else {
	return
    }
    # due to the fact that a contact can belong to
    # multiple groups, we need to ask the user whether
    # he wants to copy or move the contact to the new group
    # using a small popup menu
    set m $wDlgs(jpopuproster)
    destroy $m
    menu $m -tearoff 0
    set mDef $popMenuDefs(rostertree,dnd,def)
    set mType $popMenuDefs(rostertree,dnd,type)

    ::AMenu::Build $m $mDef -varlist [list T $T dragged $dragged tgroup $tgroup]
    # put the popup menu below the mouse pointer
    set X [winfo pointerx $T]
    set Y [winfo pointery $T]
    tk_popup $m [expr {int($X) - 10}] [expr {int($Y) - 10}]
}

proc ::RosterTree::DnDCopyOrMoveContact {T action dragged targetgroup} {
    set jlib [::Jabber::GetJlib]
    if {$action eq "cancel"} {
	return
    }
    foreach item $dragged {
        set jid ""
        set origgroups ""
        set sparent ""
        set sptag ""
        set tag ""
        if {[$T item id $item] eq ""} {
            continue
        }
        set tag [GetTagOfItem $item]
        if {[lindex $tag 0] eq "jid" } {
            set jid [lindex $tag 1]
            set jid [$jlib roster getrosterjid $jid]
            # because the user can be a member of multiple groups, we need to figure
            # out to which groups the user actually belongs to
            set origgroups [::Jabber::Jlib roster getgroups $jid]
            if { $action eq "copy" } {
                set groups [lsearch -all -inline -not -exact $origgroups $targetgroup]
                lappend groups $targetgroup
                set groups [lsort -unique $groups]
            } else {
                # when we move the contact, we also need to know the group from which
                # the contact needs to be removed
                set sparent [$T item parent $item]
		# some styles do not have a "head" in the tree
		if {$sparent eq 0} {
                    set groups [lappend origgroups $targetgroup]
                    set groups [lsort -unique $groups]
		} else {
                    set sptag [GetTagOfItem $sparent]
                    if {[lindex $sptag 0] eq "group" } {
                        set sgroup [lindex $sptag 1]
                        set groups [lsearch -all -inline -not -exact $origgroups $sgroup]
                        lappend groups $targetgroup
                        set groups [lsort -unique $groups]
                    } elseif {[lindex $sptag 0] eq "head" } {
                        set groups [lappend origgroups $targetgroup]
                        set groups [lsort -unique $groups]
                   }
               }
	    }
            array unset rostA
            set rostA(-groups) [list]
            array set rostA [$jlib roster getrosteritem $jid]
            if {[lsort $rostA(-groups)] ne $groups} {
                unset -nocomplain rostA(-subscription)
                set rostA(-groups) $groups
                eval {$jlib roster send_set $jid} [array get rostA]
            }

        }
    }
}

proc ::RosterTree::SetBinds {} {
    variable T

    # @@@ perhaps a new bindtag instead?
    # We need to do this each time a new style is loaded since all binds
    # have been removed to allow for 'EditSetBinds'.
    bind $T <Button-1>        {+::RosterTree::OnButtonPress %x %y }        
    bind $T <ButtonRelease-1> {+::RosterTree::OnButtonRelease %x %y }        
    bind $T <Double-1>        {+::RosterTree::OnDoubleClick %x %y }        
    bind $T <<ButtonPopup>>   {+::RosterTree::OnPopup %x %y }
    bind $T <Destroy>         {+::RosterTree::OnDestroy }
    bind $T <Key-Return>      {+::RosterTree::OnReturn }
    bind $T <KP_Enter>        {+::RosterTree::OnReturn }
    bind $T <Key-BackSpace>   {+::RosterTree::OnBackSpace }
    bind $T <Button1-Motion>  {+::RosterTree::OnButtonMotion %x %y }
    bind $T <FocusOut>        {+::RosterTree::OnFocusOut }
}

proc ::RosterTree::DBOptions {rosterStyle} {
    variable T
    
    ::treeutil::setdboptions $T [::Roster::GetRosterWindow] $rosterStyle
}

# RosterTree::Find, FindAgain --
# 
#       This is a generic mechanism for searching a TreeCtrl roster.
#       UI::TSearch shall work for any TreeCtrl widget.

proc ::RosterTree::Find {} {
    variable wfind
    variable T    
    
    if {![winfo exists $wfind]} {
	set column [GetStyleFindColumn]
	if {$column ne ""} {
	    UI::TSearch $wfind $T $column \
	      -closecommand [namespace code FindDestroy]
	    grid  $wfind  -column 0 -row 2 -columnspan 2 -sticky ew
	}
    }
}

proc ::RosterTree::FindAgain {dir} {
    variable wfind

    if {[winfo exists $wfind]} {
	$wfind [expr {$dir == 1 ? "Next" : "Previous"}]
    }
}

proc ::RosterTree::FindDestroy {} {
    variable wfind
    set wmaster [winfo parent $wfind]
    destroy $wfind
    
    # Workaround for the grid bug.
    after idle [list grid rowconfigure $wmaster 2 -minsize 0]
}

proc ::RosterTree::FindReset {} {
    variable wfind
    if {[winfo exists $wfind]} {
	$wfind Reset
    }
}

# RosterTree::Free --
# 
#       Free all items, elements, styles, and columns. And binds.

proc ::RosterTree::Free {} {
    variable T
    
    $T item delete all
    $T column delete all
    eval {$T style delete} [$T style names]
    eval {$T element delete} [$T element names]
    foreach sequence [bind $T] {
	bind $T $sequence {}
    }
}

# Edit stuff --------------------------

namespace eval ::RosterTree:: {
    
    # @@@ Move to global resource.
    variable waitUntilEditMillis 2000
}

proc ::RosterTree::EditSetBinds {cmd} {
    variable T
    variable editBind
    
    set editBind(cmd) $cmd
    bind $T <Button-1>        {+::RosterTree::EditButtonPress %x %y }        
    bind $T <ButtonRelease-1> {+::RosterTree::EditButtonRelease %x %y }        
    bind $T <Double-1>        {+::RosterTree::EditTimerCancel }        
}

proc ::RosterTree::EditButtonPress {x y} {
    variable T
    variable editTimer
    variable editBind
    
    if {[info exists editTimer(after)]} {
	if {[$T selection count] == 1} {
	    set id [$T identify $x $y]
	    if {$id eq $editTimer(id)} {
		
		# The balloonhelp window on Mac takes focus. Stop it.
		::balloonhelp::cancel
		uplevel #0 $editBind(cmd) [list $id]
	    }
	}
    }
}

proc ::RosterTree::EditButtonRelease {x y} {
    variable T
    variable editTimer
    variable waitUntilEditMillis

    set id [$T identify $x $y]
    if {([lindex $id 0] eq "item") && ([llength $id] == 6)} {
	set editTimer(id) $id
	set editTimer(after) \
	  [after $waitUntilEditMillis ::RosterTree::EditTimerCancel]
    }
}

proc ::RosterTree::EditOnReturn {jid name} {
    variable T
        
    set jid [::Jabber::Jlib roster getrosterjid $jid]
    set groups [::Jabber::Jlib roster getgroups $jid]
    ::Jabber::Jlib roster send_set $jid -name $name -groups $groups
    focus $T
}

proc ::RosterTree::EditEnd {jid} {
    variable T
    
    # Restore item with its original style.
    set jid [::Jabber::Jlib roster getrosterjid $jid]
    eval {::Roster::SetItem $jid} [::Jabber::Jlib roster getrosteritem $jid]
}

proc ::RosterTree::EditTimerCancel {} {
    variable editTimer

    if {[info exists editTimer(after)]} {
	after cancel $editTimer(after)
    }
    unset -nocomplain editTimer
}

# BackgroundImage... Try to make as generic as possible!

# RosterTree::BackgroundImageCmd --
# 
#       There are two separate ways the current background image may be selected:
#         1) as defined by the theme
#         2) a user picked one which is cached in this(backgroundsPath)

proc ::RosterTree::BackgroundImageCmd {} {
    global  this jprefs
    variable T
    
    set mimes [list image/gif image/png image/jpeg]
    set suffL [::Media::GetSupportedSuffixesForMimeList $mimes]
    set typeL [::Media::GetSupportedTypesForMimeList $mimes]
    set types [concat [list [list {Image Files} $suffL]] \
      [::Media::GetDlgFileTypesForMimeList $mimes]]
    
    # Default file (as defined by the theme):
    set defaultFile [BackgroundImageGetThemedFile $suffL]
    
    # Current file:
    set currentFile [BackgroundImageGetFile $suffL $defaultFile]

    # Dialog:
    set typeText [join $typeL ", "]
    # TRANSLATORS: to set background image in services or contacts tab
    set str [mc "Select an image file for the background. To remove a background image press Remove and Save."]
    set dtl [mc "The supported image formats are"]
    append dtl " " $typeText
    append dtl "."
    set mbar [::JUI::GetMainMenu]
    ::UI::MenubarDisableBut $mbar edit
    set fileName [ui::openimage::modal -message $str -detail $dtl -menu $mbar \
      -filetypes $types -initialfile $currentFile -defaultfile $defaultFile \
      -geovariable prefs(winGeom,jbackgroundimage) -title [mc "Background Image"]]
    ::UI::MenubarEnableAll $mbar

    set image ""
    if {$fileName eq ""} {
	return
    } elseif {$fileName eq "-"} {
	set jprefs(rost,useBgImage) 0
    } elseif {[file exists $fileName]} {
	set fileName [file normalize $fileName]
	set jprefs(rost,useBgImage) 1
	if {$fileName eq $defaultFile} {
	    set jprefs(rost,defaultBgImage) 1
	} else {
	    set jprefs(rost,defaultBgImage) 0
	}
	
	# Don't copy file if it is already there.
	set suff [file extension $fileName]
	set dst [file normalize [file join $this(backgroundsPath) roster$suff]]
	
	# Cache file. There shall only be one roster.* file there.
	if {$fileName ne $dst} {
	    
	    # Clear roster.* cache.
	    ::tfileutils::deleteallfiles $this(backgroundsPath) roster.*
	    set suff [file extension $fileName]
	    file copy -force $fileName $dst
	}	
	if {[catch {
	    set image [image create photo -file $fileName]
	}]} {
	    set image ""
	}
    }    
    BackgroundImageConfig $image
}

proc ::RosterTree::BackgroundImageGetThemedFile {suffL} {
    variable T
    
    set name [option get $T rosterImage {}]
    set fileName [::Theme::FindIconFileWithSuffixes backgrounds/$name $suffL]
    return [file normalize $fileName]
}

# RosterTree::BackgroundImageGetFile --
#
#       Return empty if not configured with background image.

proc ::RosterTree::BackgroundImageGetFile {suffL defaultFile} {
    global  this jprefs
    
    set fileName ""
    if {$jprefs(rost,useBgImage)} {
	if {$jprefs(rost,defaultBgImage)} {
	    set fileName $defaultFile
	} else {
	    set pattern [list]
	    foreach suff $suffL {
		lappend pattern "roster$suff"
	    }    
	    set files [eval {glob -nocomplain -directory $this(backgroundsPath)} -- $pattern]
	    set fileName [lindex $files 0]
	}
    }    
    return $fileName
}

proc ::RosterTree::BackgroundImageGet {} {
        
    # This gets called only during creation and we make a shortcut
    # to avoid loading slow packages (QuickTimeTcl etc.)
    set image ""
    set mimes [list image/gif image/png image/jpeg]
    set suffL [::Types::GetSuffixListForMimeList $mimes]   
    set fileName [BackgroundImageGetFile $suffL \
      [BackgroundImageGetThemedFile $suffL]]
    if {[file exists $fileName]} {
	if {[catch {
	    set image [image create photo -file $fileName]
	}]} {
	    # If any custom image has been removed we just ignore it.
	    set image ""
	}
    }
    return $image
}

proc ::RosterTree::BackgroundImageConfig {image} {
    variable T
    
    # Garbage collection.
    set old [$T cget -backgroundimage]
    $T configure -backgroundimage $image
    if {$old ne ""} {
	image delete $old
    }    
    ::hooks::run rosterTreeConfigure -backgroundimage $image
}

proc ::RosterTree::Selection {} {
    variable T

    ::hooks::run rosterTreeSelectionHook
}

proc ::RosterTree::OpenTreeCmd {item} {
    variable T
    
    #empty
}

proc ::RosterTree::CloseTreeCmd {item} {
    variable T
    
    #empty
}

proc ::RosterTree::OnReturn {} {
    variable T
    
    foreach jid [GetSelectedJID] {
	ActionDoubleClick $jid
    }
}

proc ::RosterTree::OnBackSpace {} {
    variable T

    set item [$T selection get]
    if {[$T selection count] == 1} {
	set tags [GetTagOfItem $item]
	if {[lindex $tags 0] eq "jid"} {
	    set jid [lindex $tags 1]
	    set jid2 [jlib::barejid $jid]
	    ::Roster::SendRemove $jid2
	}
    }
}

proc ::RosterTree::OnButtonPress {x y} {
    variable T
    variable buttonAfterId
    variable buttonPressMillis

    if {[tk windowingsystem] eq "aqua"} {
	if {[info exists buttonAfterId]} {
	    catch {after cancel $buttonAfterId}
	}
	set cmd [list ::RosterTree::OnPopup $x $y]
	set buttonAfterId [after $buttonPressMillis $cmd]
    }
    set id [$T identify $x $y]
    if {$id eq ""} {
	$T selection clear all
    }
}

proc ::RosterTree::OnButtonRelease {x y} {
    variable T
    variable buttonAfterId
    
    if {[info exists buttonAfterId]} {
	catch {after cancel $buttonAfterId}
	unset buttonAfterId
    }    
}

proc ::RosterTree::OnDoubleClick {x y} {
    variable T

    ::balloonhelp::cancel

    # According to XMPP def sect. 4.1, we should use user@domain when
    # initiating a new chat or sending a new message that is not a reply.
    set id [$T identify $x $y]
    if {([lindex $id 0] eq "item") && ([llength $id] == 6)} {
	set item [lindex $id 1]
	set tags [GetTagOfItem $item]
	if {[lindex $tags 0] eq "jid"} {
	    set jid [lindex $tags 1]
	    ActionDoubleClick $jid
	}
    }
}

proc ::RosterTree::ActionDoubleClick {jid} {
    global jprefs
    
    set jid2 [jlib::barejid $jid]
	    
    if {[string equal $jprefs(rost,dblClk) "normal"]} {
	::NewMsg::Build -to $jid2
    } elseif {[string equal $jprefs(rost,dblClk) "chat"]} {
	if {[::Jabber::Jlib roster isavailable $jid]} {
	    
	    # We let Chat handle this internally.
	    ::Chat::StartThread $jid2
	} else {
	    ::NewMsg::Build -to $jid2
	}
    }
}

proc ::RosterTree::OnButtonMotion {x y} {
    variable buttonAfterId
    
    if {[info exists buttonAfterId]} {
	catch {after cancel $buttonAfterId}
	unset buttonAfterId
    }    
}

proc ::RosterTree::OnFocusOut {} {
    variable buttonAfterId
    
    if {[info exists buttonAfterId]} {
	catch {after cancel $buttonAfterId}
	unset buttonAfterId
    }    
}

# RosterTree::GetSelected --
# 
#       Returns a list of tags of selected items.

proc ::RosterTree::GetSelected {} {
    variable T
    
    set selected [list]
    foreach item [$T selection get] {
	lappend selected [GetTagOfItem $item]
    }
    return $selected
}

# RosterTree::GetSelectedJID --
# 
#       Returns a list of JIDs of selected contacts.

proc ::RosterTree::GetSelectedJID {} {
    
    set jidL [list]
    set tags [GetSelected]
    foreach tag $tags {
	if {[lindex $tag 0] eq "jid"} {
	    lappend jidL [lindex $tag 1]
	}
    }
    return $jidL
}

# RosterTree::GetExtSelectedJID --
# 
#       As 'GetSelectedJID' but searches recursively selected parents as well.

proc ::RosterTree::GetExtSelectedJID {} {
    variable T
    
    set jidL [list]
    foreach item [$T selection get] {
	set tag [GetTagOfItem $item]
	if {[lindex $tag 0] eq "jid"} {
	    lappend jidL [lindex $tag 1]
	} else {
	    set all [$T item descendants $item]
	    foreach aitem $all {
		set tag [GetTagOfItem $aitem]
		if {[lindex $tag 0] eq "jid"} {
		    lappend jidL [lindex $tag 1]
		}
	    }
	}
    }
    return [lsort -unique $jidL]
}

# RosterTree::OnPopup --
# 
#       Treectrl binding for popup event.

proc ::RosterTree::OnPopup {x y} {
    variable T
    
    ::Debug 2 "::RosterTree::OnPopup"

    ::balloonhelp::cancel
    
    set jidL    [list]
    set itemL   [list]
    set groupL  [list]
    
    # 1: Assemble itemL
    set id [$T identify $x $y]    
    if {[lindex $id 0] eq "item"} {
	set selected [$T selection get]
	set item [lindex $id 1]

	# If clicked an unselected item, pick this.
	# If clicked any selected, pick the complete selection.
	if {[lsearch $selected $item] >= 0} {
	    set itemL $selected
	} else {
	    set itemL [list $item]
	}
    }
    
    # 2: From itemL, collect jidL    
    foreach item $itemL {
     	set tags [GetTagOfItem $item]
	set mtag [lindex $tags 0]
	
	switch -- $mtag {
	    jid {
		set jid [lindex $tags 1]
		lappend jidL $jid
	    }
	    group {
		lappend groupL [lindex $tags 1]
		set jidL [concat $jidL [FindAllJIDInItem $item]]
	    }
	    head {
		if {[regexp {(available|unavailable)} [lindex $tags 1]]} {
		    lappend groupL [lindex $tags 1]
		    set jidL [concat $jidL [FindAllJIDInItem $item]]
		}
	    }
	}
    }
    ::Roster::DoPopup $jidL $groupL $x $y
}

proc ::RosterTree::FindAllJIDInItem {item} {
    return [FindAllWithTagInItem $item jid]
}

# RosterTree::FindAllWithTagInItem --
#
#       Gets a list of all jids in item.
#
# Arguments:
#       item        tree item
#       type        jid
#       
# Results:
#       list of jids

proc ::RosterTree::FindAllWithTagInItem {item type} {
    variable T
    
    set jidL [list]
    foreach citem [$T item children $item] {
	if {[$T item numchildren $citem]} {
	    set jidL [concat $jidL [FindAllWithTagInItem $citem $type]]
	}
	set tags [GetTagOfItem $citem]
	if {[lindex $tags 0] eq $type} {
	    lappend jidL [lindex $tags 1]
	}
    }
    return $jidL
}

# ---------------------------------------------------------------------------- #
#
# There are two methods to keep track of mapping tags to items:
#    o Keeping an external array (fastest): "array"
#    o Internal to TreeCtrl: "treectrl"

set rosterTreeTagMethod "array"

if {$rosterTreeTagMethod eq "array"} {
    
    # A few more generic functions.
    # They isolate the 'tag2items' array from the rest.
    
    namespace eval ::RosterTree {
	
	# Internal array.
	variable tag2items
    }
    
    proc ::RosterTree::CreateWithTag {tag parent} {
	variable T
	variable tag2items
    
	set item [$T item create -parent $parent]
	
	# Handle the hidden cTag column.
	$T item style set $item cTag styTag
	$T item element configure $item cTag eText -text $tag
	lappend tag2items($tag) $item	
	return $item
    }
    
    proc ::RosterTree::DeleteWithTag {tag} {
	variable T
	variable tag2items
	
	if {[info exists tag2items($tag)]} {
	    foreach item $tag2items($tag) {
		$T item delete $item
	    }    
	}
    }
    
    # RosterTree::RemoveTags --
    # 
    #       Callback for <ItemDelete> events used to cleanup the tag2items array.
    
    proc ::RosterTree::RemoveTags {item} {
	variable T
	variable tag2items
	
	# We must delete all 'tag2items' that may point to us.
	set tag [$T item element cget $item cTag eText -text]
	set items $tag2items($tag)
	set idx [lsearch $items $item]
	if {$idx >= 0} {
	    set tag2items($tag) [lreplace $items $idx $idx]
	}
	if {$tag2items($tag) eq {}} {
	    unset tag2items($tag)
	}
    }
    
    proc ::RosterTree::FindWithTag {tag} {
	variable tag2items
	
	if {[info exists tag2items($tag)]} {
	    return $tag2items($tag)
	} else {
	    return
	}
    }
    
    proc ::RosterTree::FindWithFirstTag {tag0} {
	variable tag2items
	
	# Note that we don't need escaping here since tag0 is guranteed to be free
	# from any special chars.
	set items [list]
	foreach {key value} [array get tag2items "$tag0 *"] {
	    set items [concat $items $value]
	}
	return $items
    }
    
    # RosterTree::FindChildrenOfTag --
    # 
    #       The caller MUST verify that the tag is actually unique.
    
    proc ::RosterTree::FindChildrenOfTag {tag} {
	variable T
	variable tag2items
	
	# NEVER use the non unique 'jid *' tag here!
	set items [list]
	if {[info exists tag2items($tag)]} {
	    set pitem [lindex $tag2items($tag) 0]
	    set items [$T item children $pitem]
	}
	return $items
    }
        
    proc ::RosterTree::GetTagOfItem {item} {
	variable T    
	return [$T item element cget $item cTag eText -text]
    }
    
    proc ::RosterTree::ExistsWithTag {tag} {
	variable tag2items
	return [info exists tag2items($tag)]
    }
    
    proc ::RosterTree::FreeTags {} {
	variable tag2items    
	unset -nocomplain tag2items
    }
    
    proc ::RosterTree::OnDestroy {} {
	FreeTags
    }
    
# ---------------------------------------------------------------------------- #

# This was an attempt to use the built in tags of treectrl 2.2 but timings
# showed that this code was slower. We keep it here for backup storage.

} else {
    
    # A few generic functions to isolate the tags handlings in treectrl.
    # 
    #       Use tags as {0-tag0 1-tag1 2-tag2 ...} to resolve issues like:
    #       {group jid} and {jid group}. Although "group" isn't likely to be a 
    #       domain name we don't want to depend on it.
    #       In other words, tags are ordered.

    proc ::RosterTree::PrepTags {tags} {
	set n -1
	set tagL [list]
	set tags [treeutil::protect $tags]
	foreach tag $tags {
	    lappend tagL [incr n]-$tag
	}
	return $tagL
    }

    proc ::RosterTree::CreateWithTag {tag parent} {
	variable T
	return [$T item create -parent $parent -tags [PrepTags $tag]]
    }

    proc ::RosterTree::DeleteWithTag {tag} {
	variable T
	
	# This must be failsafe if item not exists.
	foreach item [FindWithTag $tag] {
	    $T item delete $item
	}
    }

    proc ::RosterTree::FindWithTag {tag} {
	variable T
	return [$T item id [list tag [join [PrepTags $tag] " && "]]]
    }

    proc ::RosterTree::FindWithFirstTag {tag0} {
	variable T
	return [$T item id [list tag [treeutil::protect 0-$tag0]]]
    }

    # RosterTree::FindChildrenOfTag --
    # 
    #       The caller MUST verify that the tag is actually unique.

    proc ::RosterTree::FindChildrenOfTag {tag} {
	variable T
	return [$T item children [list tag [join [PrepTags $tag] " && "]]]
    }

    proc ::RosterTree::GetTagOfItem {item} {
	variable T
	
	set tagL [list]
	foreach tag [treeutil::deprotect [$T item cget $item -tags]] {
	    lappend tagL [string range $tag 2 end]
	}
	return $tagL
    }

    proc ::RosterTree::ExistsWithTag {tag} {
	return [llength [FindWithTag $tag]]
    }

    proc ::RosterTree::DbgDumpRoster {} {
	variable T
	
	puts "::RosterTree::DbgDumpRoster:"
	foreach item [$T item id all] {
	    set tags [treeutil::deprotect [$T item cget $item -tags]]
	    puts "\t item=$item, tags=$tags"
	}
    }
}

# ---------------------------------------------------------------------------- #

# Support code for roster styles ...

# RosterTree::CreateItemBase --
# 
#       Helper function for roster styles for creating items.
#       It creates the necessary items in the right places but doesn't
#       configure them with styles and elements (except for tag).
#       Online users shall be put with full 3-tier jid.
#       Offline and other are stored with 2-tier jid with no resource.
#       
# Arguments:
#       jid         as reported by the presence
#                   if from roster element any nonempty resource is appended
#       presence    "available" or "unavailable"
#       args        list of '-key value' pairs of presence and roster
#                   attributes.
#       
# Results:
#       a list {item tag ?item tag? ...}

proc ::RosterTree::CreateItemBase {jid presence args} {    
    global  jprefs
    variable T

    if {($presence ne "available") && ($presence ne "unavailable")} {
	return
    }
    
    # Filter out those we don't want to see.
    set istrpt [::Roster::IsTransportEx $jid]
    if {$istrpt} {
	if {!$jprefs(rost,showTrpts)} {
	    return
	}	
    } else {
	if {$presence eq "available"} {
	    set show [::Jabber::Jlib roster getshow $jid]
	    if {$show ne ""} {
		if {[info exists jprefs(rost,show-$show)]} {
		    if {!$jprefs(rost,show-$show)} {
			return
		    }
		}
	    }
	} else {
	    if {!$jprefs(rost,showOffline)} {
		return
	    }	    
	}
    }
    array set argsA $args
    set itemTagL [list]

    set mjid [jlib::jidmap $jid]
    
    # Keep track of any dirs created.
    set dirtags [list]
	
    if {$istrpt} {
	
	# Transports:
	set itemTagL [CreateJIDItemWithParent $mjid transport]
	if {[llength $itemTagL] == 4} {
	    lappend dirtags [lindex $itemTagL 1]
	    
	    # Always put pending last, after any transport.
	    set pending [FindWithTag [list head pending]]
	    if {$pending ne ""} {
		$T item prevsibling $pending [lindex $itemTagL 0]
	    }
	}
    } elseif {[info exists argsA(-ask)] && ($argsA(-ask) eq "subscribe")} {
	
	# Pending:
	set itemTagL [CreateJIDItemWithParent $mjid pending]
	if {[llength $itemTagL] == 4} {
	    lappend dirtags [lindex $itemTagL 1]
	}
    } elseif {[info exists argsA(-groups)] && [llength $argsA(-groups)]} {
	
	# Group(s):
	foreach group $argsA(-groups) {
	    
	    # Make group if not exists already.
	    set ptag [list group $group $presence]
	    set pitem [FindWithTag $ptag]
	    if {$pitem eq ""} {
		set pptag [list head $presence]
		set ppitem [FindWithTag $pptag]
		set pitem [CreateWithTag $ptag $ppitem]
		$T item configure $pitem -button 1
		lappend dirtags $ptag
		lappend itemTagL $pitem $ptag
	    }
	    set tag [list jid $mjid]
	    set item [CreateWithTag $tag $pitem]
	    lappend itemTagL $item $tag 
	}
    } else {
	
	# No groups associated with this item.
	set tag  [list jid $mjid]
	set ptag [list head $presence]
	set pitem [FindWithTag $ptag]
	set item [CreateWithTag $tag $pitem]
	lappend itemTagL $item $tag 
    }
    
    # If we created a directory and that is on the closed item list.
    # Default is to have -open.
    foreach dtag $dirtags {
	if {[lsearch $jprefs(rost,closedItems) $dtag] >= 0} {	    
	    set citem [FindWithTag $dtag]
	    $T item collapse $citem
	}
    }
        
    return $itemTagL
}

# RosterTree::CreateJIDItemWithParent --
# 
#       Helper to create a jid item including any missing parent.
#       No styles are associated with items. Do not use this alone.
#       
#       item tag list {item tag ?item tag?}

proc ::RosterTree::CreateJIDItemWithParent {jid type} {
    variable T
    
    set itemTagL [list]
    set ptag [list head $type]
    set pitem [FindWithTag $ptag]
    if {$pitem eq ""} {
	set pitem [CreateWithTag $ptag root]
	$T item configure $pitem -button 1
	lappend itemTagL $pitem $ptag
    }
    set tag [list jid $jid]
    set item [CreateWithTag $tag $pitem]
    lappend itemTagL $item $tag
    
    return $itemTagL
}

# RosterTree::DeleteItemBase --
# 
#       Complement to 'CreateItemBase' above when deleting an item associated
#       with a jid.

proc ::RosterTree::DeleteItemBase {jid} {
        
    # If have 3-tier jid:
    #    presence = 'available'   => remove jid2 + jid3
    #    presence = 'unavailable' => remove jid2 + jid3
    # Else if 2-tier jid:  => remove jid2
    
    set mjid3 [jlib::jidmap $jid]
    jlib::splitjid $mjid3 mjid2 res
    
    set tag [list jid $mjid2]
    DeleteWithTag $tag
    if {$res ne ""} {
	DeleteWithTag [list jid $mjid3]
    }
}

# RosterTree::BasePostProcessDiscoInfo --
# 
#       Disco info post processing must handle two sets of JID:
#         1) transports: 
#             - move to transport folder if not there
#             - set associated icon
#         2) users from this transport:
#             - set associated icon
#       
#       Note that the 'from', disco item, may differ by a resource to the
#       roster item JID.

proc ::RosterTree::BasePostProcessDiscoInfo {from column elem} {
    variable T
	
    # Investigate all roster items that are in any way related to the discoe'd
    # item. We'll get the roster JIDs, usually bare JID.
    set jidL [::Roster::GetUsersWithSameHost $from]
    
    set jlib [::Jabber::GetJlib]

    foreach jid $jidL {
	
	# Ordinary users and so far unrecognized transports.
	set istrpt [::Roster::IsTransportEx $jid]
	set icon [::Roster::GetPresenceIconFromJid $jid]
	set tag [list jid $jid]	
	
	if {$istrpt} {
	    	    
	    # If we find a transport not in {head transport} move it.
	    # Delete it and put it back using generic method to get all set.
	    foreach item [FindWithTag $tag] {
		#if {[GetItemsHeadClass $item] ne "transport"}
		if {![HasItemAncestorWithTag $item [list head transport]]} {
		    DeleteWithTag $tag
		    eval {::Roster::SetItem $jid} [$jlib roster getrosteritem $jid]
		    break
		}
	    }
	}
	
	# Set icons for transports and users from this transport.
	if {$icon ne ""} {
	    foreach item [FindWithTag $tag] {
		$T item element configure $item $column $elem -image $icon
	    }
	}	
    }
}

proc ::RosterTree::MCHead {str} {
    variable mcHead

    return $mcHead($str)
}

# RosterTree::MakeDisplayText --
# 
#       Make a standard display text.

proc ::RosterTree::MakeDisplayText {jid presence args} {

    array set argsA $args
    
    # Format item:
    #  - If 'name' attribute, use this, else
    #  - If nickname
    #  - if user belongs to login server, use only prefix, else
    #  - show complete 2-tier jid
    # If resource add it within parenthesis '(presence)' but only if Online.
    # 
    # For Online users, the tree item must be a 3-tier jid with resource 
    # since a user may be logged in from more than one resource.
    # Note that some (icq) transports have 3-tier items that are unavailable!

    set istrpt [::Roster::IsTransportEx $jid]
    set server [::Jabber::Jlib getserver]

    if {[info exists argsA(-name)] && ($argsA(-name) ne "")} {
	set str $argsA(-name)
    } else {
	set str [::Nickname::Get [jlib::barejid $jid]]
	if {$str eq ""} {	
	    jlib::splitjidex $jid node domain res
	    if {$domain eq $server} {
		set str [jlib::unescapestr $node]
	    } else {
		set ujid [jlib::unescapejid $jid]
		set str [jlib::barejid $ujid]
	    }
	}
    }
    if {$istrpt} {
	# @@@ A bit ad hoc...
	if {[info exists argsA(-show)]} {
	    set sstr [::Roster::MapShowToText $argsA(-show)]
	    append str " ($sstr)" 
	} elseif {[info exists argsA(-status)] && ($argsA(-status) ne "")} {
	    append str " ($argsA(-status))"
	}
    }
    if {$presence eq "available"} {
	if {[info exists argsA(-resource)] && ($argsA(-resource) ne "")} {
	    
	    # Configurable?
	    #append str " ($argsA(-resource))"
	}
    }
    return $str
}

# RosterTree::Balloon --
# 
#       jid:        as reported in xml

proc ::RosterTree::Balloon {jid presence item args} {
    variable T    
    variable balloon

    array set argsA $args
    
    set mjid [jlib::jidmap $jid]

    # Design the balloon help window message.
    set msg [jlib::unescapejid $jid]
    if {[info exists argsA(-show)]} {
	set show $argsA(-show)
    } else {
	set show $presence
    }
    append msg "\n" [::Roster::MapShowToText $show]

    if {[string equal $presence "available"]} {
	set delay [::Jabber::Jlib roster getx $jid "jabber:x:delay"]
	if {$delay ne ""} {
	    
	    # An ISO 8601 point-in-time specification. clock works!
	    set stamp [wrapper::getattribute $delay stamp]
	    set tstr [::Utils::SmartClockFormat [clock scan $stamp -timezone :UTC]]
	    append msg "\n" "Online since: $tstr"
	}
    }
    if {[info exists argsA(-status)] && ($argsA(-status) ne "")} {
	append msg "\n" $argsA(-status)
    }
    
    # Append any registered balloon messages. Both bare and full JIDs.
    if {[array exists balloon]} {
	set bnames [array names balloon *,[jlib::ESC $mjid]]
	if {[jlib::isfulljid $mjid]} {
	    set xnames [array names balloon *,[jlib::ESC [jlib::barejid $mjid]]]
	    set bnames [concat $bnames $xnames]
	}
	foreach key [lsort $bnames] {
	    append msg "\n" $balloon($key)
	}
    }
    
    ::balloonhelp::treectrl $T $item $msg

    ::hooks::run rosterBalloonhelp $T $item $jid
}

# RosterTree::BalloonRegister --
# 
#       @@@ Better place is in ::Roster

proc ::RosterTree::BalloonRegister {key jid msg} {
    variable balloon
    
    set mjid [jlib::jidmap $jid]
    if {$msg eq ""} {
	puts "\t unset -nocomplain balloon($key,$mjid)"
	unset -nocomplain balloon($key,$mjid)
    } else {
	set balloon($key,$mjid) $msg
    }
    BalloonSetForJID $jid
}

# RosterTree::BalloonSetForJID --
# 
#       jid:    bare or full JID; if bare set for all full JIDs

proc ::RosterTree::BalloonSetForJID {jid} {
    
    jlib::splitjid $jid jid2 res
    if {[jlib::isfulljid $jid]} {
	array set presA [::Jabber::Jlib roster getpresence $jid2 -resource $res]
	
	set tag [list jid $jid]
	foreach item [FindWithTag $tag] {
	    #puts "\t item=$item"
	    eval {Balloon $jid $presA(-type) $item} [array get presA]
	}
    } else {
	set presL [::Jabber::Jlib roster getpresence $jid2]
	#puts "presL=$presL"
	foreach p $presL {
	    array unset presA
	    array set presA $p
	    if {$presA(-type) eq "available" && $presA(-resource) ne ""} {
		set pjid $jid2/$presA(-resource)
	    } else {
		set pjid $jid	    
	    }
	    set tag [list jid $pjid]
	    foreach item [FindWithTag $tag] {
		#puts "\t item=$item"
		eval {Balloon $pjid $presA(-type) $item} [array get presA]
	    }
	}
    }
}

proc ::RosterTree::InitHook {} {
    InitMenus
}

proc ::RosterTree::InitMenus {} {
    # Template for the DnD popup menu.
    variable popMenuDefs
    set mDefs {
        {command        mCopy   {[mc "&Copy"]} {::RosterTree::DnDCopyOrMoveContact $T "copy" $dragged $tgroup}}
        {command        mMove   {[mc "&Move"]} {::RosterTree::DnDCopyOrMoveContact $T "move" $dragged $tgroup}}
        {command        mCancel {[mc "Canc&el"]} {::RosterTree::DnDCopyOrMoveContact $T "cancel" $dragged $tgroup}}
    }
    set mTypes {
        {mCopy          {normal}                }
        {mMove          {normal}                }
        {mCancel        {normal}                }
    }
    set popMenuDefs(rostertree,dnd,def)  $mDefs
    set popMenuDefs(rostertree,dnd,type) $mTypes
}

proc ::RosterTree::LogoutHook {} {
    variable balloon

    unset -nocomplain balloon 
}

proc ::RosterTree::QuitHook { } {
    variable T        
    
    if {[info exists T] && [winfo exists $T]} {
	GetClosed
    }
}

proc ::RosterTree::NicknameEventHook {xmldata jid nickname} {
    
    # Some servers seem to push out my own nickname.
    if {[jlib::jidequal $jid [::Jabber::Jlib myjid2]]} {
	return
    }
    
    # Just repopulate item.
    set jid [::Jabber::Jlib roster getrosterjid $jid]
    eval {::Roster::SetItem $jid} [::Jabber::Jlib roster getrosteritem $jid]    
}

# RosterTree::GetClosed --
# 
#       Keep track of all closed tree items. Default is all open.

proc ::RosterTree::GetClosed {} {
    global jprefs
    
    set jprefs(rost,closedItems) [GetClosedItems root]
}

proc ::RosterTree::GetClosedItems {item} {
    variable T        
    
    set closed {}
    if {[$T item numchildren $item]} {
	if {![$T item isopen $item] && [$T item compare root != $item]} {
	    set tags [GetTagOfItem $item]
	    lappend closed $tags
	}
	foreach citem [$T item children $item] {
	    set closed [concat $closed [GetClosedItems $citem]]
	}
    }
    return $closed
}

proc ::RosterTree::GetParent {item} {
    variable T
    
    return [$T item parent $item]
}

# RosterTree::GetItemsHeadClass --
# 
#       Method to get an items head tag or class.
#       This relies on that it exists a head item where transports etc. are put.

proc ::RosterTree::GetItemsHeadClass {item} {
    variable T
    
    set ancestors [$T item ancestors $item]
    if {[llength $ancestors] >= 2} {
	set head [lindex $ancestors end-1]
	return [lindex [GetTagOfItem $head] 1]
    } else {
	return ""
    }
}

# RosterTree::HasItemAncestorWithTag --
# 
#       Searches its ancestors for an item with given tag.
#       Smarter than 'GetItemsHeadClass'?

proc ::RosterTree::HasItemAncestorWithTag {item tag} {
    variable T
    
    set aitem [FindWithTag $tag]
    if {$aitem ne ""} {
	return [$T item isancestor $item $aitem]
    } else {
	return 0
    }
}

# RosterTree::SortAtIdle --
# 
#       Doing 'after idle' is not perfect since it is executed after the 
#       items have been drawn.

proc ::RosterTree::SortAtIdle {item {order -increasing}} {
    variable sortID
    
    if {![info exists sortID($item)]} {
	
	# If we get a request to sort 'root' then cancel all other idle sorts.
	if {($item eq "root") || ($item == 0)} {
	    foreach id [array names sortID] {
		after cancel $id
	    }
	}
	set sortID($item) [after idle [namespace current]::SortIdle $item $order]
    }
}

proc ::RosterTree::SortIdle {item order} {
    variable T    
    variable sortID
    
    unset -nocomplain sortID($item)
    if {[$T item id $item] ne ""} {
	Sort $item $order
    }
}

proc ::RosterTree::Sort {item {order -increasing}} {
    variable plugin

    set name $plugin(selected)
    if {[info exists plugin($name,sortProc)]} {
	$plugin($name,sortProc) $item $order
    } else {
	SortDefault $item $order
    }
}

proc ::RosterTree::SortDefault {item order} {
    variable T    
    
    # Sort recursively for each directory.
    foreach citem [$T item children $item] {
	if {[$T item numchildren $citem]} {
	    SortDefault $citem $order
	}
    }
    
    # Do not sort the first children of root!
    if {($item ne "root") && ($item != 0)} {
	
	# TreeCtrl 2.2.3 has a problem doing custom sorting (-command)
	# for large rosters, see:
	#   [ 1706359 ] custom sorting can be extremly slow
	#   at tktreectrl.sf.net
	set n [$T item numchildren $item]
	if {$n} {
	    if {$n > 50} {
		$T item sort $item $order -column cTree -dictionary
	    } else {
		$T item sort $item $order -column cTree  \
		  -command ::RosterTree::SortCommand
	    }
	}
    }
}

proc ::RosterTree::SortCommand {item1 item2} {
    variable T
    
    if {$item1 == $item2} {
	return 0
    }
    set n1 [$T item numchildren $item1]
    set n2 [$T item numchildren $item2]
    if {$n1 && !$n2} {
	set ans -1
    } elseif {!$n1 && $n2} {
	set ans 1
    } else {
	set text1 [$T item text $item1 cTree]
	set text2 [$T item text $item2 cTree]
	set ans [string compare -nocase $text1 $text2]
    }
    return $ans
}

proc ::RosterTree::DeleteEmptyGroups {} {
    variable T
    
    foreach item [FindWithFirstTag group] {
	if {[$T item numchildren $item] == 0} {
	    $T item delete $item

	}
    }
}

# RosterTree::DeleteEmptyPendTrpt --
# 
#       Cleanup empty pending and transport dirs.

proc ::RosterTree::DeleteEmptyPendTrpt {} {
    variable T

    foreach key {pending transport} {
	set tag [list head $key]
	set item [FindWithTag $tag]
	if {$item ne ""} {
	    if {[$T item numchildren $item] == 0} {
		$T item delete $item
	    }
	}
    }
}

proc ::RosterTree::FileMenuPostHook {wmenu} {
    variable T
    
    if {[winfo ismapped $T]} {
	set jidL [GetSelectedJID]
	if {[llength $jidL] == 1} {
	    set m [::UI::MenuMethod $wmenu entrycget mExport -menu]
	    ::UI::MenuMethod $m entryconfigure mBC... -state normal -label [mc "&Business Card"]...
	}
    }
}

# JUI::EditMenuPostHook --
# 
#       Menu post command hook for doing roster/disco searches.

proc ::RosterTree::EditMenuPostHook {wmenu} {
    variable T
    variable wfind
    
    if {[winfo ismapped $T] && [string length [GetStyleFindColumn]]} {
	::UI::MenuMethod $wmenu entryconfigure mFind -state normal -label [mc "Find"]
	if {[winfo exists $wfind]} {
	    ::UI::MenuMethod $wmenu entryconfigure mFindNext -state normal -label [mc "Find Next"]
	    ::UI::MenuMethod $wmenu entryconfigure mFindPrevious -state normal -label [mc "Find Previous"]
	}
    }
}

proc ::RosterTree::OnMenuExportVCardHook {} {
    variable T
    
    if {[winfo ismapped $T]} {
	set jidL [GetSelectedJID]
	if {[llength $jidL] == 1} {
	    ::VCard::ExportXMLFromJID [lindex $jidL 0]
	}
    }
}