File: Plugins.tcl

package info (click to toggle)
coccinella 0.96.20-9
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 13,184 kB
  • sloc: tcl: 124,744; xml: 206; makefile: 66; sh: 62
file content (1434 lines) | stat: -rw-r--r-- 42,455 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
# Plugins.tcl --
#  
#       This file is part of The Coccinella application.
#       It registers the standard "built in" packages:
#           QuickTimeTcl
#           snack
#           Img
#           
#       It also contains support functions for adding external plugins.
#      
#  Copyright (c) 2003-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: Plugins.tcl,v 1.37 2008-05-22 08:11:03 matben Exp $
#
# We need to be very systematic here to handle all possible MIME types
# and extensions supported by each package or helper application.
#
# mimeType2Packages: array that maps a MIME type to a list of one or many 
#                    packages.
# 
# supSuff:           maps package name to supported suffixes for that package,
#                    and maps from MIME type (image, audio, video) to suffixes.
#                    It contains a bin (binary) element as well which is the
#                    the union of all MIME types except text.
#                    This should only be used internally to compile the
#                    dialog -filetypes values.
#                    
# supportedMimeTypes: maps package name to supported MIME types for that package,
#                    and maps from MIME type (image, audio, video) to MIME type.
#                    It contains a bin (binary) element as well which is the
#                    the union of all MIME types except text.
#                    
# prefMimeType2Package:  The preferred package to handle a file with this MIME 
#                    type. Empty if no packages support MIME.
#
# mimeTypeDoWhat:    is one of (unavailable|reject|save|ask|$packageName).
#                    The default is "unavailable". Only "reject" if package
#                    is there but actively picked reject in dialog.
#                    Both "unavailable" and "reject" map to "Reject" in dialog.
#                    The "unavailable" says that there is no package for MIME.
#
# 
# The 'plugin' array defines various aspects of each plugin package:
#
#  plugin(packageName,pack)        Thing to use for 'package require' 
#                                  including possibly version number.
#  plugin(packageName,ver)         The version actually used, if loaded.
#  
#  plugin(packageName,type)        Type of plugin. Any of:
#                                    'internal'    one of the hardcoded ones
#                                    'external'    loaded through our plugin method
#                                    'application' a helper application
#  plugin(packageName,desc)        A longer description of the package.
#  plugin(packageName,platform)    list if platforms
#  plugin(packageName,loaded)      0/1 if loaded or not.
#  plugin(packageName,importProc)  which tcl procedure to call when importing...
#  plugin(packageName,mimes)       List of all MIME types supported.
#  plugin(packageName,winClass)    (optional) 
#  plugin(packageName,saveProc)    (optional) 
#  plugin(packageName,importHttpProc)  (optional) which tcl procedure to call 
#                                  when importing using http.
#  plugin(packageName,trpt,MIME)   (optional) the transport method used,
#                                  which defaults to the built in PUT/GET,
#                                  but can be "http" for certain Mime types
#                                  for the QuickTime package. In that case
#                                  the 'importProc' procedure gets it internally.
#                                  Here MIME can be the mimetype, or the
#                                  mimetype/subtype to be flexible.
#  plugin(packageName,icon,12)     (optional) Tk image to show in various places
#  plugin(packageName,icon,16)     (optional) Tk image to show in various places
#  
#-------------------------------------------------------------------------------

# @@@ TODO: try to use as much from Media as possible.

package require Media
package require Types

package provide Plugins 1.0

namespace eval ::Plugins {
    global tcl_platform
    
    # Define all hooks for preference settings.
    # We init as late as possible.
    # Make sure we are inited before showing any prefs panels.
    ::hooks::register afterFinalHook         ::Plugins::InitPrefsHook
    ::hooks::register prefsBuildHook         ::Plugins::BuildInitPrefsHook 10
    ::hooks::register prefsBuildHook         ::Plugins::BuildPrefsHook
    ::hooks::register prefsUserDefaultsHook  ::Plugins::UserDefaultsHook
    ::hooks::register prefsSaveHook          ::Plugins::SaveHook
    ::hooks::register prefsCancelHook        ::Plugins::CancelHook
    ::hooks::register prefsDestroyHook       ::Plugins::DestroyPrefsHook

    variable inited 0
    
    variable packages2Platform
    variable helpers2Platform
    variable supSuff
    variable supportedMimeTypes
    variable supMacTypes
    variable plugType2DescArr
    
    # Supported binary files, that is, images movies etc.
    # Start with the core Tk supported formats. Mac 'TYPE'.
    set plugin(tk,loaded) 1
    set supSuff(text)  {.txt}
    set supSuff(image) [list]
    set supSuff(audio) [list]
    set supSuff(video) [list]
    set supSuff(application) [list]

    set supMacTypes(text)  {TEXT}
    set supMacTypes(image) [list]
    set supMacTypes(audio) [list]
    set supMacTypes(video) [list]
    set supMacTypes(application) [list]

    # Map keywords and package names to the supported MIME types.
    # Start by initing, MIME types added below.
    set supportedMimeTypes(text)  text/plain
    set supportedMimeTypes(image) [list]
    set supportedMimeTypes(audio) [list]
    set supportedMimeTypes(video) [list]
    set supportedMimeTypes(application) [list]
    set supportedMimeTypes(all) $supportedMimeTypes(text)
    
    # Search only for packages on platforms they can live on.
    array set packages2Platform {
	QuickTimeTcl       {            macosx    windows} 
	snack              {windows     unix}
	Img                {windows     unix}
	tkpng              {macosx      windows     unix}
    }
    array set helpers2Platform {xanim unix} 
        
    array set plugType2DescArr {
	internal    "Internal Plugin"
	external    "External Plugin"
	application "Helper Application"
    }
    
    # Plugin ban list. Do not load these packages.
    variable pluginBanList [list]
}

# Plugins::InitPrefsHook --
#
#       Handles all initializations. 
#       We may call this any time to make sure it is inited.

proc ::Plugins::InitPrefsHook {} {
    global  prefs
    variable inited

    if {$inited} { return }

    # Plugin ban list. Do not load these packages.
    set prefs(pluginBanList) [list]
    ::Debug 2 "::Plugins::InitPrefsHook"
    
    ::PrefUtils::Add [list  \
      [list prefs(pluginBanList)   prefs_pluginBanList   $prefs(pluginBanList)]]
    
    # Load all plugins available.
    SetBanList $prefs(pluginBanList)
    InitAndLoad
    
    # Make sure the settings are consistent with the available packages.
    VerifyPackagesForMimeTypes
    if {[HavePackage QuickTimeTcl]} {
	QuickTimeTclInitHook
    }
    
    ::Debug 2 "--> whiteboardPrefsInitHook"
    ::hooks::run whiteboardPrefsInitHook
    
    set inited 1
}

proc ::Plugins::BuildInitPrefsHook {wtree nbframe} {
    InitPrefsHook
}

# Plugins::InitAndLoad --
# 
#       The initialization function which loads all plugins available.
#       

proc ::Plugins::InitAndLoad {} {
    global this
    variable mimeTypeDoWhat
    variable prefMimeType2Package
    variable supSuff
    variable supMacTypes
    variable plugin
    
    ::Debug 2 "::Plugins::InitAndLoad"
    
    foreach mime [::Types::GetAllMime] {
	set mimeTypeDoWhat($mime) "unavailable"
	set prefMimeType2Package($mime) ""
    }
    
    # Init the "standard" (internal and application) plugins.
    InitTk
    InitQuickTimeTcl
    InitSnack
    InitImg
    InitTkPNG
    InitXanim
    
    # Load all "external" plugins.
    set pluginDir [file join $this(path) plugins]
    LoadPluginDirectory $pluginDir
    
    # Load all packages and plugins we can.
    CompileAndLoadPackages
    
    # Set up various arrays used internally.
    PostProcessInfo
    
    # This must be done after all plugins identified and loaded.
    MakeTypeListDialogOption
}

proc ::Plugins::InitTk {} {
    global this
    variable plugin
    variable supportedMimeTypes
    variable supSuff

    set plugin(tk,type) "internal"
    set plugin(tk,desc) "Supported by the core"
    set plugin(tk,ver) [info tclversion]
    set plugin(tk,importProc) ::Import::DrawImage
    set plugin(tk,icon,12) [::Theme::FindIcon mime/12x12/application-x-tcl]
    #set supSuff(tk) {.gif}
    set supportedMimeTypes(tk) {image/gif image/x-portable-pixmap}
    set plugin(tk,mimes) $supportedMimeTypes(tk)
}

proc ::Plugins::InitQuickTimeTcl {} {
    global this
    variable plugin
    variable supportedMimeTypes
    variable packages2Platform
    variable supSuff
    
    set plugin(QuickTimeTcl,pack) "QuickTimeTcl 3.1"
    set plugin(QuickTimeTcl,type) "internal"
    set plugin(QuickTimeTcl,desc) \
      {Displays multimedia content such as video, sound, mp3 etc.\
      It also supports a large number of still image formats.}
    set plugin(QuickTimeTcl,platform) $packages2Platform(QuickTimeTcl)
    set plugin(QuickTimeTcl,importProc) ::Import::DrawQuickTimeTcl
    
    # We should get files via its -url option, i.e. http if possible.
    set plugin(QuickTimeTcl,trpt,audio) http
    set plugin(QuickTimeTcl,trpt,video) http
    
    # Define any 16x16 icon to spice up the UI.
    set plugin(QuickTimeTcl,icon,16) \
      [::Theme::FindIcon mime/16x16/video-quicktime]
    set plugin(QuickTimeTcl,icon,12) \
      [::Theme::FindIcon mime/12x12/video-quicktime]
    
    # We must list supported MIME types for each package.
    # For QuickTime:
    set supportedMimeTypes(QuickTimeTcl) {\
      video/quicktime     video/x-dv          video/mpeg\
      video/mpeg4\
      video/x-mpeg        audio/mpeg          audio/x-mpeg\
      video/x-msvideo     application/sdp     audio/aiff\
      audio/x-aiff        audio/basic         audio/x-sd2\
      audio/wav           audio/x-wav         image/x-bmp\
      image/vnd.fpx       image/gif           image/jpeg\
      image/x-macpaint    image/x-photoshop   image/png\
      image/x-png         image/pict          image/x-sgi\
      image/x-targa       image/tiff          image/x-tiff\
      application/x-world application/x-3dmf  video/flc\
      application/x-shockwave-flash           audio/midi\
      audio/x-midi        audio/vnd.qcelp     video/avi\
    }
    set plugin(QuickTimeTcl,mimes) $supportedMimeTypes(QuickTimeTcl)
}

#--- snack ---------------------------------------------------------------------
# On Unix/Linux and Windows we try to find the Snack Sound extension.
# Only the "sound" part of the extension is actually needed.

proc ::Plugins::InitSnack {} {
    variable plugin
    variable supportedMimeTypes
    variable packages2Platform
    
    set plugin(snack,pack) "snack"
    set plugin(snack,type) "internal"
    set plugin(snack,desc) "The Snack Sound extension adds audio capabilities\
      to the application. Presently supported formats include wav, au, aiff and mp3."
    set plugin(snack,platform) $packages2Platform(snack)
    set plugin(snack,importProc) ::Import::DrawSnack
    set supportedMimeTypes(snack) {\
      audio/wav           audio/x-wav         audio/basic\
      audio/aiff          audio/x-aiff        audio/mpeg\
      audio/x-mpeg\
    }
    set plugin(snack,mimes) $supportedMimeTypes(snack)
}

#--- Img -----------------------------------------------------------------------
# The Img extension for reading more image formats than the standard one (gif).

proc ::Plugins::InitImg {} {
    variable plugin
    variable supportedMimeTypes
    variable packages2Platform
    
    set plugin(Img,pack) "Img"
    set plugin(Img,type) "internal"
    set plugin(Img,desc) "Adds more image formats than the standard one (gif)."
    set plugin(Img,platform) $packages2Platform(Img)
    set plugin(Img,importProc) ::Import::DrawImage
    set supportedMimeTypes(Img) {\
      image/x-bmp         image/gif           image/jpeg\
      image/png           image/x-png         image/tiff\
      image/x-tiff\
    }
    set plugin(Img,mimes) $supportedMimeTypes(Img)
}

proc ::Plugins::InitTkPNG {} {
    variable plugin
    variable supportedMimeTypes
    variable packages2Platform
    
    set plugin(tkpng,pack) "tkpng"
    set plugin(tkpng,type) "internal"
    set plugin(tkpng,desc) "Adds png image support."
    set plugin(tkpng,platform) $packages2Platform(tkpng)
    set plugin(tkpng,importProc) ::Import::DrawImage
    set supportedMimeTypes(tkpng) {
	image/png           image/x-png
    }
    set plugin(tkpng,mimes) $supportedMimeTypes(tkpng)
}

#--- xanim ---------------------------------------------------------------------
# Test the 'xanim' app on Unix/Linux for multimedia.
  
proc ::Plugins::InitXanim {} {
    variable plugin
    variable supportedMimeTypes

    set plugin(xanim,type) "application"
    set plugin(xanim,desc) "A unix/Linux only application that is used\
      for displaying multimedia content in the canvas."
    set plugin(xanim,platform) unix
    set plugin(xanim,importProc) ::Import::DrawXanim
    
    # There are many more...
    set supportedMimeTypes(xanim) {\
      audio/wav           audio/x-wav         video/mpeg\
      video/x-mpeg        audio/mpeg          audio/x-mpeg\
      audio/basic         video/quicktime\
    }
    set plugin(xanim,mimes) $supportedMimeTypes(xanim)
}


proc ::Plugins::SetBanList {banList} {
    variable pluginBanList
    
    set pluginBanList $banList
}

# Plugins::CompileAndLoadPackages --
#
#       Compile information of all packages and helper apps to search for.
#       Do 'package require' and record any success.

proc ::Plugins::CompileAndLoadPackages {} {
    global this
    variable plugin
    variable pluginBanList
    
    set plugin(all) [list]
    set plugin(allPacks) [list]
    set plugin(allHelpers) [list]

    set plugin(all) [list]
    set plugin(allPacks) [list]
    set plugin(allInternalPacks) [list]
    set plugin(allExternalPacks) [list]
    set plugin(allApps) [list]
    
    foreach packAndPlat [array names plugin "*,platform"] {
	
	if {[regexp {^([^,]+),platform$} $packAndPlat match packName]} {
	    
	    # Find type, "internal" or "application".
	    switch -- $plugin($packName,type) {
		internal {
		    lappend plugin(allInternalPacks) $packName
		}
		external {
		    lappend plugin(allExternalPacks) $packName
		}
		application {
		    lappend plugin(allApps) $packName
		}
	    }
	}
    }
    set plugin(allPacks)  \
      [concat $plugin(allInternalPacks) $plugin(allExternalPacks)]
    set plugin(all) [concat $plugin(allPacks) $plugin(allApps)]
    
    # Search for the wanted packages in a systematic way.    
    foreach name $plugin(allPacks) {
	
	# Check first if this package can live on this platform.
	if {[lsearch $plugin($name,platform) $this(platform)] >= 0} {
	    
	    # Make sure that not on the ban list.
	    if {[lsearch $pluginBanList $name] >= 0} {
		set plugin($name,loaded) 0
	    } else {
	    
		# Search for it! Be silent.
		if {[string equal $plugin($name,type) "internal"]} {
		    ::Splash::SetMsg [mc "Looking for %s" $name]...
		    if {![catch {
			eval {package require} $plugin($name,pack)
		    } msg]} {
			set plugin($name,loaded) 1
			set plugin($name,ver) $msg
		    } else {
			set plugin($name,loaded) 0
		    }	    
		} else {
		    set plugin($name,loaded) 1
		}
	    }
	    set plugin($name,ishost) 1
	} else {
	    set plugin($name,ishost) 0
	    set plugin($name,loaded) 0
	}
    }
    
    # And all helper applications... only apps on Unix/Linux.
    foreach helperApp $plugin(allApps) {
	if {[string equal $this(platform) "unix"]} {
	    set apath [lindex [auto_execok $helperApp] 0]
	    if {[llength $apath]} {
		set plugin($helperApp,loaded) 1
	    } else  {
		set plugin($helperApp,loaded) 0
	    }
	    set plugin($helperApp,ishost) 1
	} else  {
	    set plugin($helperApp,ishost) 0
	    set plugin($helperApp,loaded) 0
	}
    }
}

# Plugins::PostProcessInfo
#
#       Systematically make the 'supSuff', 'supMacTypes', 
#       'supportedMimeTypes', 'mimeType2Packages'.

proc ::Plugins::PostProcessInfo {} {
    variable plugin
    variable supportedMimeTypes
    variable supSuff
    variable supMacTypes
    variable mimeType2Packages
    variable prefMimeType2Package
    variable mimeTypeDoWhat
    
    # We add the tk library to the other ones.
    foreach name [concat tk $plugin(all)] {
	if {$plugin($name,loaded)}  {
	    set supSuff($name) [list]

	    # Loop over all file MIME types supported by this specific package.
	    foreach mimeType $plugin($name,mimes) {
		
		# Collect all suffixes for this package.
		set suffList [::Types::GetSuffixListForMime $mimeType]
		eval lappend supSuff($name) $suffList
		
		# Get the MIME base: text, image, audio...
		if {[regexp {([^/]+)/} $mimeType match mimeBase]}  {
		    
		    eval lappend supSuff($mimeBase) $suffList
		    
		    # Add upp all "binary" files.
		    if {![string equal $mimeBase "text"]}  {
			eval lappend supSuff(bin) $suffList
		    }
		    
		    # Collect the mac types.
		    eval lappend supMacTypes($mimeBase)  \
		      [::Types::GetMacTypeListForMime $mimeType]
		    lappend supportedMimeTypes($mimeBase) $mimeType
		    lappend mimeType2Packages($mimeType) $name
		    
		    # Add upp all "binary" files.
		    if {![string equal $mimeBase "text"]}  {
			lappend supportedMimeTypes(bin) $mimeType
		    }
		}
	    }
	    if {[llength $supSuff($name)]} {
		eval lappend supSuff(all) $supSuff($name)
	    }
	    eval lappend supportedMimeTypes(all) $plugin($name,mimes)
	}
    }
    
    # Remove duplicates in lists.
    foreach name [concat tk $plugin(all)] {
	if {[info exists supSuff($name)]} {
	    set supSuff($name) [lsort -unique $supSuff($name)]
	}
    }
    foreach mimeBase {text image audio video application} {
	if {[info exists supSuff($mimeBase)]} {
	    set supSuff($mimeBase) [lsort -unique $supSuff($mimeBase)]
	}
	if {[info exists supportedMimeTypes($mimeBase)]} {
	    set supportedMimeTypes($mimeBase) [lsort -unique $supportedMimeTypes($mimeBase)]
	}
    }
    foreach key [array names supMacTypes] {
	set supMacTypes($key) [lsort -unique $supMacTypes($key)]
    }
    set supSuff(all) [lsort -unique $supSuff(all)]
    set supSuff(bin) [lsort -unique $supSuff(bin)]
    set supportedMimeTypes(all) [lsort -unique $supportedMimeTypes(all)]
    set supportedMimeTypes(bin) [lsort -unique $supportedMimeTypes(bin)]
    
    # Some kind of mechanism needed to select which package to choose when
    # more than one package can support a suffix.
    # Here we just takes the first one. Should find a better way!
    # QuickTimeTcl is preferred.
    
    foreach mime [array names mimeType2Packages] {
	if {[lsearch -exact $mimeType2Packages($mime) "QuickTimeTcl"] > 0} {
	    set prefMimeType2Package($mime) QuickTimeTcl
	} else {
	    set prefMimeType2Package($mime) [lindex $mimeType2Packages($mime) 0]
	}
	set mimeTypeDoWhat($mime) $prefMimeType2Package($mime)
    }
    set prefMimeType2Package(image/gif) tk
}

# Plugins::GetAllPackages --
# 
#       Returns the name of packages depending on the 'which' option.

proc ::Plugins::GetAllPackages {{which all}} {
    variable plugin
    
    switch -- $which {
	all {
	    return $plugin(allPacks)
	}
	internal {
	    return $plugin(allInternalPacks)
	}
	external {
	    return $plugin(allExternalPacks)
	}
	platform {
	    set packList [list]
	    foreach name $plugin(all) {
		if {$plugin($name,ishost)} {
		    lappend packList $name
		}
	    }
	    return $packList	    
	}
	loaded {
	    set packList [list]
	    foreach name $plugin(all) {
		if {$plugin($name,loaded)} {
		    lappend packList $name
		}
	    }
	    return $packList	    
	}
    }
}

# Plugins::MakeTypeListDialogOption --
#  
#       Create the 'typelist' option for the Open Image/Movie dialog and 
#       standard text files.

proc ::Plugins::MakeTypeListDialogOption {} {
    global this
    variable supSuff
    variable supMacTypes
    variable supportedMimeTypes
    variable typelist
    
    array set typelist {
	text        {}
	audio       {}
	video       {}
	application {}
    }
    
    set typelist(text) [list [list Text $supSuff(text)]]
    
    switch -- $this(platform) {
	
	macosx - windows - unix {    
	    set typelist(image) [list   \
	      [list Image $supSuff(image)]  \
	      [list Image {} $supMacTypes(image)] ]
	    if {[llength $supSuff(audio)] > 0}  {
		set typelist(audio) [list  \
		  [list Audio $supSuff(audio)]  \
		  [list Audio {} $supMacTypes(audio)]]
	    }
	    if {[llength $supSuff(video)] > 0}  {
		set typelist(video) [list  \
		  [list Video $supSuff(video)]  \
		  [list Video {} $supMacTypes(video)]]
	    }	
	    if {[llength $supSuff(application)] > 0}  {
		set typelist(application) [list  \
		  [list Application $supSuff(application)]  \
		  [list Application {} $supMacTypes(application)]]
	    }	
	    if {[llength $supSuff(text)] > 0}  {
		set typelist(text) [list  \
		  [list Text $supSuff(text)]  \
		  [list Text {} $supMacTypes(text)]]
	    }	
	    
	    # Use mime description as entries.
	    set mimeTypeList {}
	    foreach mime $supportedMimeTypes(all) {
		lappend mimeTypeList   \
		  [list [::Types::GetDescriptionForMime $mime]  \
		  [::Types::GetSuffixListForMime $mime]  \
		  [::Types::GetMacTypeListForMime $mime]]
	    }
	    set mimeTypeList [lsort -index 0 $mimeTypeList]
	    set typelist(binary) [concat $typelist(image) $typelist(audio) \
	      $typelist(video) $typelist(application)]
	    set typelist(binary) [concat $typelist(binary) $mimeTypeList]
	    lappend typelist(binary) [list "Any File" *]
	    
	} 
	
	default {
	    # Make a separate entry for each file extension. Sort.
	    foreach mimeBase {text image audio video application} {
		foreach ext $supSuff($mimeBase) {
		    lappend typelist($mimeBase)  \
		      [list [string toupper [string trim $ext .]] $ext]
		}
	    }
	    set sortlist [lsort -index 0 [concat $typelist(image) $typelist(audio) \
	      $typelist(video) $typelist(application)]]
	    set typelist(binary) $sortlist
	    set typelist(binary) "$sortlist {{Any File} *}"	    
	}  
    }
    
    # Complete -typelist option.
    set typelist(all) [concat $typelist(text) $typelist(binary)]
}

# proc ::Plugins::InitHook {} {
#     
#     VerifyPackagesForMimeTypes
#     if {[HavePackage QuickTimeTcl]} {
# 	QuickTimeTclInitHook
#     }
# }

proc ::Plugins::QuickTimeTclInitHook {} {
    
    ::WB::RegisterHandler QUICKTIME ::Import::QuickTimeHandler    
}

# Plugins::VerifyPackagesForMimeTypes --
#
#       Goes through all the logic of verifying the 'mimeTypeDoWhat' 
#       and the actual packages available on our system.
#       The 'mimeTypeDoWhat' is stored in our preference file, but the
#       'prefMimeType2Package' is partly determined at runtime, and depends
#       on which packages found at launch.
#       
# Arguments:
#       none.
#   
# Results:
#       updates the 'mimeTypeDoWhat' and 'prefMimeType2Package' arrays.

proc ::Plugins::VerifyPackagesForMimeTypes {} {
    global prefs
    variable plugin
    variable prefMimeType2Package
    variable mimeTypeDoWhat
    variable mimeType2Packages
    
    ::Debug 2 "::Plugins::VerifyPackagesForMimeTypes"
    
    foreach mime [::Types::GetAllMime] {
	if {![info exists mimeTypeDoWhat($mime)]} {
	    set mimeTypeDoWhat($mime) unavailable
	}
	if {![info exists prefMimeType2Package($mime)]} {
	    set prefMimeType2Package($mime) ""
	}
	
	switch -- $mimeTypeDoWhat($mime) {
	    unavailable {
		if {[llength $prefMimeType2Package($mime)]} {
		    
		    # In case there was a new package(s) added, pick that one.
		    set mimeTypeDoWhat($mime) $prefMimeType2Package($mime)
		}
	    }
	    reject {
		if {[llength $prefMimeType2Package($mime)] == 0} {
		    
		    # In case a package was removed.
		    set mimeTypeDoWhat($mime) unavailable
		}
	    }    
	    save - ask {
		# Do nothing.
	    }
	    default {
		
		# This should be a package name. 
		if {![info exists mimeType2Packages($mime)] ||  \
		  ([lsearch -exact $mimeType2Packages($mime)   \
		  $mimeTypeDoWhat($mime)] < 0)} {
		    
		    # There are either no package that supports this mime,
		    # or the selected package is not there.
		    if {[llength $prefMimeType2Package($mime)]} {
			set mimeTypeDoWhat($mime) $prefMimeType2Package($mime)
		    } else {
			set mimeTypeDoWhat($mime) unavailable
		    }
		} else {		
		    set prefMimeType2Package($mime) $mimeTypeDoWhat($mime)
		}
	    }
	}
    }
}

# Plugins::GetPreferredPackageForMime, ... --
#
#       Various accesor functions.

proc ::Plugins::GetPreferredPackageForMime {mime} {
    variable prefMimeType2Package

    if {[info exists prefMimeType2Package($mime)]} {
	return $prefMimeType2Package($mime)
    } else {
	return
    }
}

proc ::Plugins::SetPreferredPackageForMime {mime packName} {
    variable prefMimeType2Package

    set prefMimeType2Package($mime) $packName
}

proc ::Plugins::GetPackageListForMime {mime} {
    variable mimeType2Packages
    
    if {[info exists mimeType2Packages($mime)]} {
	return $mimeType2Packages($mime)
    } else {
	return
    }
}

proc ::Plugins::GetPreferredPackageArr {} {
    variable prefMimeType2Package

    return [array get prefMimeType2Package]
}

proc ::Plugins::SetPreferredPackageArr {prefMime2PackArrName} {
    variable prefMimeType2Package
    upvar $prefMime2PackArrName locArrName

    unset -nocomplain prefMimeType2Package
    array set prefMimeType2Package [array get locArrName]
}

proc ::Plugins::GetDoWhatForMime {mime} {
    variable mimeTypeDoWhat

    if {[info exists mimeTypeDoWhat($mime)]} {
	return $mimeTypeDoWhat($mime)
    } else {
	return
    }
}

proc ::Plugins::HavePackage {name} {
    variable plugin
    
    if {[info exists plugin($name,loaded)]} {
	return $plugin($name,loaded)
    } else {
	return 0
    }
}

proc ::Plugins::HaveImporterForMime {mime} {
    variable plugin
    variable prefMimeType2Package
    
    set ans 0
    if {[info exists prefMimeType2Package($mime)]} {
	set name $prefMimeType2Package($mime)
	if {[string length $name] &&  \
	  [string length $plugin($name,importProc)]} {
	    set ans 1
	}
    }
    return $ans
}

proc ::Plugins::GetImportProcForMime {mime} {
    variable plugin
    variable prefMimeType2Package
    
    if {[info exists prefMimeType2Package($mime)]} {
	set name $prefMimeType2Package($mime)
	if {[string length $name] &&  \
	  [string length $plugin($name,importProc)]} {
	    return $plugin($name,importProc)
	}
    }
    return -code error "No importer found for mime \"$mime\""
}

proc ::Plugins::GetImportProcForPlugin {name} {
    variable plugin
    variable prefMimeType2Package
    
    if {[string length $plugin($name,importProc)]} {
	return $plugin($name,importProc)
    } else {
	return -code error "No importer procedure found for plugin \"$name\""
    }
}

proc ::Plugins::GetImportProcForWinClass {winClass} {
    variable plugin
    
    if {[info exists plugin($winClass,importProc)] &&  \
      [string length $plugin($winClass,importProc)]} {
	return $plugin($winClass,importProc)
    } else {
	return -code error "No import procedure found for window class \"$winClass\""
    }
}

proc ::Plugins::SetDoWhatForMime {mime action} {
    variable mimeTypeDoWhat
    
    set mimeTypeDoWhat($mime) $action
}

proc ::Plugins::GetDoWhatForMimeArr {} {
    variable mimeTypeDoWhat

    return [array get mimeTypeDoWhat]
}

proc ::Plugins::SetDoWhatForMimeArr {doWhatArrName} {
    variable mimeTypeDoWhat
    upvar $doWhatArrName locArrName

    unset -nocomplain mimeTypeDoWhat
    array set mimeTypeDoWhat [array get locArrName]
}

proc ::Plugins::GetTypeListDialogOption {{what all}} {
    variable typelist
    
    return $typelist($what)
}

proc ::Plugins::IsHost {name} {    
    variable plugin

    if {[info exists plugin($name,ishost)]} {
	return $plugin($name,ishost)
    } else {
	return 0
    }
}

proc ::Plugins::IsLoaded {name} {    
    variable plugin

    if {[info exists plugin($name,loaded)]} {
	return $plugin($name,loaded)
    } else {
	return 0
    }
}

proc ::Plugins::GetType {name} {
    variable plugin

    if {[info exists plugin($name,type)]} {
	return $plugin($name,type)
    } else {
	return
    }
}

proc ::Plugins::GetTypeDesc {name} {
    variable plugin

    if {[info exists plugin($name,type)]} {
	return [::Plugins::GetDescForPlugType $plugin($name,type)]
    } else {
	return
    }
}

#       key         this can be any of the array keys.

proc ::Plugins::GetSuffixes {key} {
    variable supSuff

    if {[info exists supSuff($key)]} {
	return $supSuff($key)
    } else {
	return
    }
}

proc ::Plugins::GetDescForPlugin {name} {
    variable plugin

    if {[info exists plugin($name,desc)]} {
	return $plugin($name,desc)
    } else {
	return
    }
}

proc ::Plugins::GetDescForPlugType {plugtype} {
    variable plugType2DescArr

    if {[info exists plugType2DescArr($plugtype)]} {
	return $plugType2DescArr($plugtype)
    } else {
	return
    }
}

proc ::Plugins::HaveHTTPTransportForPlugin {name} {
    variable plugin

    if {[info exists plugin($name,importHttpProc)] &&  \
      [string length $plugin($name,importHttpProc)]} {
	return 1
    } else {
	return 0
    }
}

proc ::Plugins::GetHTTPImportProcForPlugin {name} {
    variable plugin
    
    if {[info exists plugin($name,importHttpProc)] &&  \
      [string length $plugin($name,importHttpProc)]} {
	return $plugin($name,importHttpProc)
    } else {
	return -code error "No HTTP importer procedure found for plugin \"$name\""
    }
}

proc ::Plugins::HaveHTTPTransportForMimeAndPlugin {name mime} {
    variable plugin

    set httpTrpt 0
    regexp {^([^/]+)/.*} $mime match mimeBase
    if {[info exists plugin($name,trpt,$mime)] && \
      [string equal $plugin($name,trpt,$mime) "http"]} {
	set httpTrpt 1      
    } elseif {[info exists plugin($name,trpt,$mimeBase)] && \
      [string equal $plugin($name,trpt,$mimeBase) "http"]} {
	set httpTrpt 1
    }
    return $httpTrpt
}

proc ::Plugins::HaveSaveProcForWinClass {winClass} {
    variable plugin

    if {[info exists plugin($winClass,saveProc)] &&  \
      [string length $plugin($winClass,saveProc)]} {
	return 1
    } else {
	return 0
    }
}

proc ::Plugins::GetSaveProcForWinClass {winClass} {
    variable plugin
    
    if {[info exists plugin($winClass,saveProc)] &&  \
      [string length $plugin($winClass,saveProc)]} {
	return $plugin($winClass,saveProc)
    } else {
	return -code error "No save procedure found for window class \"$winClass\""
    }
}

proc ::Plugins::GetVersionForPackage {name} {
    variable plugin

    if {[info exists plugin($name,ver)]} {
	return $plugin($name,ver)
    } else {
	return
    }
}

proc ::Plugins::GetIconForPackage {doWhat size} {
    variable plugin

    if {[info exists plugin($doWhat,icon,$size)]} {
	return $plugin($doWhat,icon,$size)
    } else {
	return
    }
}

# Plugins::NewMimeType, DeleteMimeType --
# 
#       Some consistency checks necassary after using these functions...

proc ::Plugins::NewMimeType {mime packList prefPack doWhat} {
    variable plugin
    variable mimeType2Packages 
    variable prefMimeType2Package
    variable mimeTypeDoWhat
    
    set mimeType2Packages($mime) $packList
    set prefMimeType2Package($mime) $prefPack
    set mimeTypeDoWhat($mime) $doWhat
}

proc ::Plugins::DeleteMimeType {mime} {
    variable plugin
    variable mimeType2Packages 
    variable prefMimeType2Package
    variable mimeTypeDoWhat

    unset -nocomplain mimeType2Packages($mime) \
      prefMimeType2Package($mime) \
      mimeTypeDoWhat($mime)
}

#--- Support Functions ---------------------------------------------------------
#
#       Provide functions for external plugins and packages to call to register
#       themselves with the applications plugin architecture.
#
# canvasBindList:
#       {buttonName bindCommand ?buttonName bindCommand ...?}
#       ex: { point {{bind MyFrame <Button-1>} {::My::Clicked %W %X %Y}} }
#       ex: { point {{%W bind card <Button-1>} {::My::Clicked %W %X %Y}} }

proc ::Plugins::LoadPluginDirectory {dir} {
    
    ::Debug 2 "::Plugins::LoadPluginDirectory"
    
    # The 'pluginDefs' file is sourced in own namespace. 
    # It needs the 'dir' to be there.
    set indexFile [file join $dir pluginDefs.tcl]
    if {[file exists $indexFile]} {
	source $indexFile
    }
}

proc ::Plugins::Load {fileName initProc} {
    
    uplevel #0 [list source $fileName]
    uplevel #0 $initProc
}

# Plugins::Register --
# 
#       Invoked to register an "external" plugin for whiteboard.

proc ::Plugins::Register {name defList canvasBindList} {
    variable plugin
    variable canvasClassBinds
    
    ::Debug 2 "::Plugins::Register name=$name"
    
    set defListDefaults {
      type          external
      desc          ""
      platform      ""
      importProc    ""
      mimes         ""
    }    
    
    # Set default values that may be overwritten.
    foreach {key value} $defListDefaults {
	set plugin($name,$key) $value
    }
    
    # Cache various info.
    foreach {key value} $defList {
	set plugin($name,$key) $value
    }
    if {[info exists plugin($name,winClass)]} {
	set winClass $plugin($name,winClass)
	set plugin($winClass,importProc) $plugin($name,importProc)
	set plugin($winClass,saveProc)   $plugin($name,saveProc)
    }
    
    # Cache canvas binds for each whiteboard tool.
    foreach {btname bindDef} $canvasBindList {
	lappend canvasClassBinds($name,$btname) $bindDef
    }
    set canvasClassBinds($name,name) $name
    set plugin($name,loaded) 1
}

# Plugins::DeRegister --
# 
#       Invoked to deregister an "external" plugin.

proc ::Plugins::DeRegister {name} {
    variable plugin
    variable canvasClassBinds
    
    set plugin(allExternalPacks) \
      [lsearch -all -inline -not $plugin(allExternalPacks) $name]
    set plugin(allPacks)  \
      [concat $plugin(allInternalPacks) $plugin(allExternalPacks)]
    set plugin(all) [concat $plugin(allPacks) $plugin(allApps)]
    array unset plugin "${name},*"
    array unset canvasClassBinds "${name},*"
}

# Plugins::RegisterCanvasClassBinds --
# 
#       Register canvas bindings directly. These are applied to all whiteboards.
#       
#       bindList: {{move    {bindDef     tclProc}} {...} ...}
#       We do %W -> canvas substitution on bindDef, and $wcan substitution
#       on the tclProc. % substitution is done as usal in tclProc.

proc ::Plugins::RegisterCanvasClassBinds {name bindList} {
    variable plugin
    variable canvasClassBinds
    
    # Cache canvas binds for each whiteboard tool.
    foreach {btname bindDef} $bindList {
	lappend canvasClassBinds($name,$btname) $bindDef
    }
    set canvasClassBinds($name,name) $name
}

proc ::Plugins::DeregisterCanvasClassBinds {{name {}}} {
    variable canvasClassBinds

    if {$name eq ""} {
	array unset canvasClassBinds
    } else {
	array unset canvasClassBinds "${name},*"
    }
}

# Plugins::RegisterCanvasInstBinds --
# 
#       Register canvas bindings directly for the specific canvas instance.

proc ::Plugins::RegisterCanvasInstBinds {w name bindList} {
    variable plugin
    variable canvasInstBinds
    
    # Cache canvas binds for each whiteboard tool.
    foreach {btname bindDef} $bindList {
	lappend canvasInstBinds($w,$name,$btname) $bindDef
    }
    set canvasInstBinds($w,$name,name) $name
}

proc ::Plugins::DeregisterCanvasInstBinds {w {name {}}} {
    variable canvasInstBinds

    if {$name eq ""} {
	array unset canvasInstBinds "${w},*"
    } else {
	array unset canvasInstBinds "${w},${name},*"
    }
}

# Plugins::SetCanvasBinds --
# 
#       Invoked when tool button in whiteboard clicked to update and set
#       canvas bindings.

proc ::Plugins::SetCanvasBinds {wcan oldTool newTool} {
    variable plugin
    variable canvasClassBinds
    variable canvasInstBinds
    
    ::Debug 4 "::Plugins::SetCanvasBinds oldTool=$oldTool, newTool=$newTool"

    # Canvas class bindings.
    foreach key [array names canvasClassBinds *,name] {
	set name $canvasClassBinds($key)
    
	# Remove any previous binds.
	if {($oldTool ne "") && [info exists canvasClassBinds($name,$oldTool)]} {
	    foreach binds $canvasClassBinds($name,$oldTool) {
		foreach {bindDef cmd} $binds {
		    regsub -all {\\|&} $bindDef {\\\0} bindDef
		    regsub -all {%W} $bindDef $wcan bindDef
		    eval $bindDef {{}}
		}
	    }
	}
	
	# Add registered binds.
	set bindList [list]
	if {[info exists canvasClassBinds($name,$newTool)]} {
	    set bindList [concat $bindList $canvasClassBinds($name,$newTool)]
	}
	if {[info exists canvasClassBinds($name,*)]} {
	    set bindList [concat $bindList $canvasClassBinds($name,*)]
	} 
	foreach binds $bindList {
	    foreach {bindDef cmd} $binds {
		regsub -all {\\|&} $bindDef {\\\0} bindDef
		regsub -all {%W} $bindDef $wcan bindDef
		set cmd [subst -nocommands -nobackslashes $cmd]
		eval $bindDef [list $cmd]
	    }
	}
    }
    
    # Instance specific bindings.
    # Beware: 'bind itemID' fails if not exists. Catch.
    foreach key [array names canvasInstBinds $wcan,*,name] {
	set name $canvasInstBinds($key)
    
	# Remove any previous binds.
	if {($oldTool ne "") && [info exists canvasInstBinds($wcan,$name,$oldTool)]} {
	    foreach binds $canvasInstBinds($wcan,$name,$oldTool) {
		foreach {bindDef cmd} $binds {
		    regsub -all {\\|&} $bindDef {\\\0} bindDef
		    regsub -all {%W} $bindDef $wcan bindDef
		    catch {eval $bindDef {{}}}
		}
	    }
	}
	
	# Add registered binds.
	set bindList [list]
	if {[info exists canvasInstBinds($wcan,$name,$newTool)]} {
	    set bindList [concat $bindList  \
	      $canvasInstBinds($wcan,$name,$newTool)]
	}
	if {[info exists canvasInstBinds($wcan,$name,*)]} {
	    set bindList [concat $bindList $canvasInstBinds($wcan,$name,*)]
	} 
	foreach binds $bindList {
	    foreach {bindDef cmd} $binds {
		regsub -all {\\|&} $bindDef {\\\0} bindDef
		regsub -all {%W} $bindDef $wcan bindDef
		set cmd [subst -nocommands -nobackslashes $cmd]
		catch {eval $bindDef [list $cmd]}
	    }
	}
    }    
}

# Preference pages -------------------------------------------------------------

proc ::Plugins::BuildPrefsHook {wtree nbframe} {
    
    if {![::Preferences::HaveTableItem Whiteboard]} {
	::Preferences::NewTableItem {Whiteboard} [mc "Whiteboard"]
    }
    ::Preferences::NewTableItem {Whiteboard Plugins2} [mc "Plugins"]

    set wpage [$nbframe page Plugins2]
    BuildPrefsPage $wpage
}

proc ::Plugins::BuildPrefsPage {page} {
    global  prefs
    
    variable prefplugins
    variable tmpPrefPlugins

    set wc $page.c
    ttk::frame $wc -padding [option get . notebookPageSmallPadding {}]
    pack $wc -side top -anchor [option get . dialogAnchor {}]

    set pbl $wc.fr
    ttk::frame $pbl
    pack  $pbl  -side top -anchor w
    
    set str [mc "Select the plugins you want to be loaded."]
    append str " " [mc "Requires a restart of %s" $prefs(appName)].
    ttk::label $pbl.lhead -wraplength 300 -justify left \
      -text $str -padding {0 0 0 4}
    pack $pbl.lhead -side top -anchor w
        
    set pfr $pbl.p
    ttk::frame $pfr
    pack $pfr -side top -anchor w
    
    set i 0
    foreach plug [GetAllPackages platform] {
	set tmpPrefPlugins($plug) [IsLoaded $plug]
	set prefplugins($plug) $tmpPrefPlugins($plug)

	ttk::checkbutton $pfr.c$i -text $plug  \
	  -variable [namespace current]::tmpPrefPlugins($plug)
	grid  $pfr.c$i  -sticky w  -padx 4
	
	set tooltip [dict create]
	dict set tooltip snack        [mc "Support for mp3, wav, and a few other audio formats"]
	dict set tooltip Img          [mc "Support for common image formats"]
	dict set tooltip tkpng        [mc "Support for PNG images"]
	dict set tooltip xanim        [mc "Support for several animation, video and audio formats"]
	dict set tooltip QuickTimeTcl [mc "Support for several animation, image, video and audio formats"]
	dict set tooltip TextImporter [mc "Import plain text documents"]
	dict set tooltip HtmlImport   [mc "Import HTML pages"]
	dict set tooltip ItclApplets  [mc "Import Itcl applets"]
	dict set tooltip WinImport    [mc "Import documents on Windows"]
	
	::balloonhelp::balloonforwindow $pfr.c$i [dict get $tooltip $plug]
	
	incr i
    }
    foreach plug $prefs(pluginBanList) {
	set tmpPrefPlugins($plug) 0
	set prefplugins($plug) $tmpPrefPlugins($plug)
    }
}

proc ::Plugins::SaveHook {} {
    global prefs
    variable prefplugins
    variable tmpPrefPlugins
    
    # To be correct we should also have loaded the pack here. TODO.
    set banList [list]
    foreach name [array names tmpPrefPlugins] {
	if {$tmpPrefPlugins($name) == 0} {
	    lappend banList $name
	}
	if {$tmpPrefPlugins($name) != $prefplugins($name)} {
	    ::Preferences::NeedRestart
	}
    }
    set prefs(pluginBanList) [lsort -unique $banList]
}

proc ::Plugins::CancelHook {} {
    variable prefplugins
    variable tmpPrefPlugins
    
    # Detect any changes.
    foreach p [array names tmpPrefPlugins] {
	if {$tmpPrefPlugins($p) != $prefplugins($p)} {
	    ::Preferences::HasChanged
	    return
	}
    }    
}

proc ::Plugins::UserDefaultsHook {} {
    variable prefplugins
    variable tmpPrefPlugins
    
    foreach plug [::Plugins::GetAllPackages platform] {
	set tmpPrefPlugins($plug) [::Plugins::IsLoaded $plug]
	set prefplugins($plug) $tmpPrefPlugins($plug)
    }    
}

proc ::Plugins::DestroyPrefsHook {} {
    variable prefplugins
    variable tmpPrefPlugins
    
    unset -nocomplain tmpPrefPlugins
}

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