Minor changes really.
[ir-tcl-moved-to-github.git] / client.tcl
1 #
2 # $Log: client.tcl,v $
3 # Revision 1.7  1995-03-16 17:54:03  adam
4 # Minor changes really.
5 #
6 # Revision 1.6  1995/03/15  19:10:20  adam
7 # Database setup in protocol-setup (rather target setup).
8 #
9 # Revision 1.5  1995/03/15  13:59:23  adam
10 # Minor changes.
11 #
12 # Revision 1.4  1995/03/14  17:32:29  adam
13 # Presentation of full Marc record in popup window.
14 #
15 # Revision 1.3  1995/03/12  19:31:52  adam
16 # Pattern matching implemented when retrieving MARC records. More
17 # diagnostic functions.
18 #
19 # Revision 1.2  1995/03/10  18:00:15  adam
20 # Actual presentation in line-by-line format. RPN query support.
21 #
22 # Revision 1.1  1995/03/09  16:15:07  adam
23 # First presentRequest attempts. Hot-target list.
24 #
25 #
26 set hotTargets {}
27 set hotInfo {}
28 set busy 0
29
30 set profile(Default) {{} {} 16384 8192 tcpip {books names demo} }
31 set hostname Default
32
33 wm minsize . 360 200
34
35 if {[file readable "~/.tk-c"]} {
36     source "~/.tk-c"
37 }
38
39 proc top-down-window {w} {
40     frame $w.top -relief raised -border 1
41     frame $w.bot -relief raised -border 1
42     
43     pack  $w.top $w.bot -side top -fill both -expand yes
44 }
45
46 proc top-down-ok-cancel {w ok-action} {
47     frame $w.bot.left -relief sunken -border 1
48     pack $w.bot.left -side left -expand yes -padx 5 -pady 5
49     button $w.bot.left.ok -width 6 -text {Ok} \
50             -command ${ok-action}
51     pack $w.bot.left.ok -expand yes -padx 3 -pady 3
52     button $w.bot.cancel -width 6 -text {Cancel} \
53             -command "destroy $w"
54     pack $w.bot.cancel -side left -expand yes    
55     
56     # Grab ...
57     grab $w
58     
59     tkwait window $w
60 }
61
62 proc show-target {target} {
63     .bot.target configure -text "$target"
64 }
65
66 proc show-busy {v1 v2} {
67     global busy
68     if {$busy != 0} {
69         .bot.status configure -fg $v1
70         after 200 [list show-busy $v2 $v1]
71     }
72 }
73         
74 proc show-status {status b} {
75     global busy
76     global statusbg
77     .bot.status configure -text "$status"
78     .bot.status configure -fg black
79     if {$b != 0} {
80         if {$busy == 0} {
81             set busy $b   
82             show-busy red blue
83         }
84         #        . config -cursor {watch black white}
85     } else {
86         #        . config -cursor {top_left_arrow black white}
87         puts "Normal"
88     }
89     set busy $b
90 }
91
92 proc show-message {msg} {
93     .bot.message configure -text "$msg"
94 }
95
96 proc insertWithTags {w text args} {
97     set start [$w index insert]
98     $w insert insert $text
99     foreach tag [$w tag names $start] {
100         $w tag remove $tag $start insert
101     }
102     foreach i $args {
103         $w tag add $i $start insert
104     }
105 }
106
107 proc show-full-marc {no} {
108     global setNo
109
110     set w .full-marc
111
112     if {[winfo exists $w]} {
113         $w.top.record delete 0.0 end
114         set new 0
115     } else {
116
117         toplevel $w
118
119         wm minsize $w 200 200
120         
121         frame $w.top -relief raised -border 1
122         frame $w.bot -relief raised -border 1
123
124         #        pack  $w.top $w.bot -side top -fill both -expand yes
125         pack  $w.top -side top -fill both -expand yes
126         pack  $w.bot -fill both
127
128         text $w.top.record -width 60 -height 10 \
129                 -yscrollcommand [list $w.top.s set]
130         scrollbar $w.top.s -command [list $w.top.record yview]
131
132         set new 1
133     }
134     incr no
135     
136     set r [z39.$setNo recordMarc $no line * * *]
137
138     $w.top.record tag configure marc-tag -foreground blue
139     $w.top.record tag configure marc-data -foreground black
140     $w.top.record tag configure marc-id -foreground red
141
142     foreach line $r {
143         set tag [lindex $line 0]
144         set indicator [lindex $line 1]
145         set fields [lindex $line 2]
146
147         if {$indicator != ""} {
148             insertWithTags $w.top.record "$tag $indicator" marc-tag
149         } else {
150             insertWithTags $w.top.record "$tag    " marc-tag
151         }
152         foreach field $fields {
153             set id [lindex $field 0]
154             set data [lindex $field 1]
155             if {$id != ""} {
156                 insertWithTags $w.top.record " $id " marc-id
157             }
158             set start [$w.top.record index insert]
159             insertWithTags $w.top.record $data {}
160         }
161         $w.top.record insert end "\n"
162     }
163     if {$new} {
164         bind $w <Return> {destroy .full-marc}
165         
166         pack $w.top.s -side right -fill y
167         pack $w.top.record -expand yes -fill both
168         
169         frame $w.bot.left -relief sunken -border 1
170         pack $w.bot.left -side left -expand yes -padx 5 -pady 5
171         button $w.bot.left.close -width 6 -text {Close} \
172                 -command {destroy .full-marc}
173         pack $w.bot.left.close -expand yes -padx 3 -pady 3
174         button $w.bot.edit -width 6 -text {Edit} \
175                 -command {destroy .full-marc}
176         pack $w.bot.edit -side left -expand yes
177     }
178 }
179
180 proc update-target-hotlist {target} {
181     global hotTargets
182
183     set len [llength $hotTargets]
184     if {$len > 0} {
185         .top.target.m delete 5 [expr 5+[llength $hotTargets]]
186     }
187     set indx [lsearch $hotTargets $target]
188     if {$indx >= 0} {
189         set hotTargets [lreplace $hotTargets $indx $indx]
190     }
191     set hotTargets [linsert $hotTargets 0 $target]
192     set-target-hotlist    
193
194
195 proc set-target-hotlist {} {
196     global hotTargets
197
198     set i 1
199     foreach target $hotTargets {
200         .top.target.m add command -label $target -command \
201             "menu-open-target $target"
202         incr i
203         if {$i > 8} {
204              break
205         }
206     }
207 }
208
209 proc menu-open-target {target} {
210     open-target $target
211     update-target-hotlist $target
212 }
213
214 proc open-target-action {} {
215     set host [.target-connect.top.host.entry get]
216     set port [.target-connect.top.port.entry get]
217
218     if {$host == ""} {
219         return
220     }
221     if {$port == ""} {
222         set port 210
223     }
224     open-target "${host}:${port}"
225     update-target-hotlist ${host}:${port}
226     destroy .target-connect
227 }
228
229 proc open-target {target} {
230     z39 disconnect
231     global csRadioType
232     z39 comstack ${csRadioType}
233     show-target $target
234     z39 connect $target
235
236     init-request
237 }
238
239 proc load-set-action {} {
240     global setNo
241
242     incr setNo
243     ir-set z39.$setNo
244
245     set fname [.load-set.top.filename.entry get]
246     destroy .load-set
247     if {$fname != ""} {
248         .data.list delete 0 end
249
250         show-status {Loading} 1
251         z39.$setNo loadFile $fname
252
253         set no [z39.$setNo numberOfRecordsReturned]
254         add-title-lines $no 1
255     }
256     show-status {Ready} 0
257 }
258
259 proc load-set {} {
260     set w .load-set
261
262     toplevel $w
263
264     place-force $w .
265
266     top-down-window $w
267
268     frame $w.top.filename
269     
270     pack $w.top.filename -side top -anchor e -pady 2
271     
272     entry-fields $w.top {filename} \
273             {{Filename:}} \
274             {load-set-action} {destroy .load-set}
275     
276     top-down-ok-cancel $w {load-set-action}
277 }
278
279 proc init-request {} {
280     global setNo
281     
282     z39 callback {init-response}
283     z39 init
284     show-status {Initializing} 1
285     set setNo 0
286 }
287
288 proc init-response {} {
289     show-status {Ready} 0
290     pack .mid.searchlabel .mid.searchentry -side left
291     bind .mid.searchentry <Return> search-request
292     focus .mid.searchentry
293 }
294
295 proc search-request {} {
296     global setNo
297
298     incr setNo
299     ir-set z39.$setNo
300
301     z39 callback {search-response}
302     z39.$setNo search [.mid.searchentry get]
303     show-status {Search} 1
304 }
305
306 proc search-response {} {
307     global setNo
308     global setOffset
309     global setMax
310
311     .data.list delete 0 end
312     show-status {Ready} 0
313     show-message "[z39.$setNo resultCount] hits"
314     set setMax [z39.$setNo resultCount]
315     puts $setMax
316     if {$setMax > 30} {
317         set setMax 30
318     }
319     z39 callback {present-response}
320     set setOffset 1
321     z39.$setNo present 1 $setMax
322     show-status {Retrieve} 1
323 }
324
325 proc add-title-lines {no offset} {
326     global setNo
327
328     for {set i 0} {$i < $no} {incr i} {
329         set o [expr $i + $offset]
330         set title [lindex [z39.$setNo recordMarc $o field 245 * a] 0]
331         set year  [lindex [z39.$setNo recordMarc $o field 260 * c] 0]
332         .data.list insert end "$title - $year"
333     }
334 }
335
336 proc present-response {} {
337     global setNo
338     global setOffset
339     global setMax
340
341     puts "In present-response"
342     set no [z39.$setNo numberOfRecordsReturned]
343     puts "Returned $no records, setOffset $setOffset"
344     add-title-lines $no $setOffset
345     set setOffset [expr $setOffset + $no]
346     if { $setOffset <= $setMax} {
347         z39.$setNo present $setOffset [expr $setMax - $setOffset + 1]
348     } else {
349         show-status {Finished} 0
350     }
351 }
352
353 proc bind-fields {list returnAction escapeAction} {
354     set max [expr [llength $list]-1]
355     for {set i 0} {$i < $max} {incr i} {
356         bind [lindex $list $i] <Return> $returnAction
357         bind [lindex $list $i] <Escape> $escapeAction
358         bind [lindex $list $i] <Tab> [list focus [lindex $list [expr $i+1]]]
359     }
360     bind [lindex $list $i] <Return> $returnAction
361     bind [lindex $list $i] <Escape> $escapeAction
362     bind [lindex $list $i] <Tab>    [list focus [lindex $list 0]]
363     focus [lindex $list 0]
364 }
365
366 proc entry-fields {parent list tlist returnAction escapeAction} {
367     set alist {}
368     set i 0
369     foreach field $list {
370         set label ${parent}.${field}.label
371         set entry ${parent}.${field}.entry
372         label $label -text [lindex $tlist $i] -anchor e
373         entry $entry -width 24 -relief sunken
374         pack $label -side left
375         pack $entry -side right
376         lappend alist $entry
377         incr i
378     }
379     bind-fields $alist $returnAction $escapeAction
380 }
381
382 proc open-target-dialog {} {
383     set w .target-connect
384
385     toplevel $w
386
387     place-force $w .
388
389     top-down-window $w
390
391     frame $w.top.host
392     frame $w.top.port
393
394     pack $w.top.host $w.top.port \
395             -side top -anchor e -pady 2 
396
397     entry-fields $w.top {host port } \
398             {{Hostname:} {Port number:}} \
399             {open-target-action} {destroy .target-connect}
400
401     top-down-ok-cancel $w {open-target-action}
402 }
403
404 proc close-target {} {
405     pack forget .mid.searchlabel .mid.searchentry
406     z39 disconnect
407     show-target {None}
408     show-status {Not connected} 0
409     show-message {}
410 }
411
412 proc protocol-setup-action {} {
413     global hostname
414     global profile
415     global csRadioType
416
417     set w .protocol-setup.top
418     
419     set len [$w.databases.list size]
420     for {set i 0} {$i < $len} {incr i} {
421         lappend b [$w.databases.list get $i]
422     }
423     set profile($hostname) [list [$w.description.entry get] \
424             [$w.idAuthentication.entry get] \
425             [$w.maximumRecordSize.entry get] \
426             [$w.preferredMessageSize.entry get] \
427             $csRadioType \
428             $b]
429
430     puts $profile($hostname)
431     destroy .protocol-setup
432 }
433
434
435 proc place-force {window parent} {
436     set g [wm geometry $parent]
437
438     set p1 [string first + $g]
439     set p2 [string last + $g]
440
441     set x [expr 40+[string range $g [expr $p1 +1] [expr $p2 -1]]]
442     set y [expr 60+[string range $g [expr $p2 +1] end]]
443     wm geometry $window +${x}+${y}
444 }
445
446
447 proc add-database-action {} {
448     .protocol-setup.top.databases.list insert end \
449             [.database-select.top.database.entry get]
450     destroy .database-select
451 }
452
453 proc add-database {} {
454     set w .database-select
455
456     toplevel $w
457
458     place-force $w .protocol-setup
459
460     top-down-window $w
461
462     frame $w.top.database
463
464     pack $w.top.database -side top -anchor e -pady 2
465     
466     entry-fields $w.top {database} \
467             {{Database to add:}} \
468             {add-database-action} {destroy .database-select}
469
470     top-down-ok-cancel $w {add-database-action}
471 }
472
473 proc delete-database {} {
474     foreach i [lsort -decreasing \
475             [.protocol-setup.top.databases.list curselection]] {
476         .protocol-setup.top.databases.list delete $i
477     }
478 }
479
480 proc protocol-setup {} {
481     set w .protocol-setup
482
483     global hostname
484     global profile
485     global csRadioType
486
487     toplevel $w
488
489     place-force $w .
490
491     top-down-window $w
492     
493     if {$hostname == ""} {
494         set hostname Default
495     }
496     puts hostname
497     puts $profile($hostname)
498
499     frame $w.top.description
500     frame $w.top.idAuthentication
501     frame $w.top.maximumRecordSize
502     frame $w.top.preferredMessageSize
503     frame $w.top.cs-type -relief ridge -border 2
504     frame $w.top.query -relief ridge -border 2
505     frame $w.top.databases -relief ridge -border 2
506
507     # Maximum/preferred/idAuth ...
508     pack $w.top.description \
509             $w.top.idAuthentication $w.top.maximumRecordSize \
510             $w.top.preferredMessageSize -side top -anchor e -pady 2
511     #-anchor e
512     
513     entry-fields $w.top {description idAuthentication maximumRecordSize \
514             preferredMessageSize} \
515             {{Description:} {Id Authentification:} {Maximum Record Size:}
516     {Preferred Message Size:}} \
517             {protocol-setup-action} {destroy .protocol-setup}
518     
519     $w.top.description.entry insert 0 [lindex $profile($hostname) 0]
520     $w.top.idAuthentication.entry insert 0 [lindex $profile($hostname) 1]
521     $w.top.maximumRecordSize.entry insert 0 [lindex $profile($hostname) 2]
522     $w.top.preferredMessageSize.entry insert 0 [lindex $profile($hostname) 3]
523
524     # Databases ....
525     pack $w.top.databases -side left -pady 6 -padx 6 -expand yes -fill x
526
527     label $w.top.databases.label -text "Databases"
528     button $w.top.databases.add -text "Add" -command {add-database}
529     button $w.top.databases.delete -text "Delete" -command {delete-database}
530     listbox $w.top.databases.list -geometry 20x6 \
531             -yscrollcommand "$w.top.databases.scroll set"
532     scrollbar $w.top.databases.scroll -orient vertical -border 1
533     pack $w.top.databases.label -side top -fill x \
534             -padx 2 -pady 2
535     pack $w.top.databases.add $w.top.databases.delete -side top -fill x \
536             -padx 2 -pady 2
537     pack $w.top.databases.list -side left -fill both -expand yes \
538             -padx 2 -pady 2
539     pack $w.top.databases.scroll -side right -fill y \
540             -padx 2 -pady 2
541     $w.top.databases.scroll config -command "$w.top.databases.list yview"
542
543     foreach b [lindex $profile($hostname) 5] {
544         $w.top.databases.list insert end $b
545     }
546     
547     # Transport ...
548     set csRadioType [lindex $profile($hostname) 4]
549
550     pack $w.top.cs-type -pady 6 -padx 6 -side top
551     
552     label $w.top.cs-type.label -text "Transport" 
553     radiobutton $w.top.cs-type.tcpip -text "TCP/IP" \
554             -command {puts tcp/ip} -variable csRadioType -value tcpip
555     radiobutton $w.top.cs-type.mosi -text "MOSI" \
556             -command {puts mosi} -variable csRadioType -value mosi
557     
558     pack $w.top.cs-type.label $w.top.cs-type.tcpip $w.top.cs-type.mosi \
559             -padx 4 -side top -fill x
560
561     # Query ...
562     pack $w.top.query -pady 6 -padx 6 -side top
563
564     label $w.top.query.label -text "Query support" -anchor e
565     checkbutton $w.top.query.c1 -text "CCL query"   
566     checkbutton $w.top.query.c2 -text "RPN query"
567     checkbutton $w.top.query.c3 -text "Result sets"
568
569     pack $w.top.query.label -side top 
570     pack $w.top.query.c1 $w.top.query.c2 $w.top.query.c3 \
571             -padx 4 -side top -fill x
572     
573     top-down-ok-cancel $w {protocol-setup-action}
574 }
575
576 proc database-select-action {} {
577     set w .database-select.top
578     set b {}
579     foreach indx [$w.databases.list curselection] {
580         lappend b [$w.databases.list get $indx]
581     }
582     if {$b != ""} {
583         z39 databaseNames $b
584     }
585     destroy .database-select
586 }
587
588 proc database-select {} {
589     set w .database-select
590     global profile
591     global hostname
592
593     toplevel $w
594
595     place-force $w .
596
597     top-down-window $w
598
599     if {$hostname == ""} {
600         set hostname Default
601     }
602
603     #frame $w.top.database
604     frame $w.top.databases -relief ridge -border 2
605
606     #pack $w.top.database -side top -anchor e -pady 2
607     
608     #entry-fields $w.top {database} \
609     #        {{Database:}} \
610     #        {database-select-action} {destroy .database-select}
611
612     pack $w.top.databases -side left -pady 6 -padx 6 -expand yes -fill x
613
614     label $w.top.databases.label -text "List"
615     listbox $w.top.databases.list -geometry 20x6 \
616             -yscrollcommand "$w.top.databases.scroll set"
617     scrollbar $w.top.databases.scroll -orient vertical -border 1
618     pack $w.top.databases.label -side top -fill x \
619             -padx 2 -pady 2
620     pack $w.top.databases.list -side left -fill both -expand yes \
621             -padx 2 -pady 2
622     pack $w.top.databases.scroll -side right -fill y \
623             -padx 2 -pady 2
624     $w.top.databases.scroll config -command "$w.top.databases.list yview"
625
626     foreach b [lindex $profile($hostname) 5] {
627         $w.top.databases.list insert end $b
628     }
629     top-down-ok-cancel $w {database-select-action}
630 }
631
632 proc save-settings {} {
633     global hotTargets 
634
635     set f [open "~/.tk-c" w]
636     puts $f "# Setup file"
637     puts $f "set hotTargets \{ $hotTargets \}"
638     close $f
639 }
640
641 frame .top -border 1 -relief raised
642 frame .mid  -border 1 -relief raised
643 frame .data -border 1 -relief raised
644 frame .bot -border 1 -relief raised
645 pack .top .mid -side top -fill x
646 pack .data      -side top -fill both -expand yes
647 pack .bot      -fill x
648
649 menubutton .top.file -text "File" -menu .top.file.m
650 menu .top.file.m
651 .top.file.m add command -label "Save settings" -command {save-settings}
652 .top.file.m add command -label "Load Set" -command {load-set}
653 .top.file.m add separator
654 .top.file.m add command -label "Exit" -command {destroy .}
655
656 menubutton .top.target -text "Target" -menu .top.target.m
657 menu .top.target.m
658 .top.target.m add command -label "Connect" -command {open-target-dialog}
659 .top.target.m add command -label "Disconnect" -command {close-target}
660 .top.target.m add command -label "Initialize" -command {init-request}
661 .top.target.m add command -label "Setup" -command {protocol-setup}
662 .top.target.m add separator
663 set-target-hotlist
664
665 menubutton .top.database -text "Database" -menu .top.database.m
666 menu .top.database.m
667 .top.database.m add command -label "Select ..." -command {database-select}
668 .top.database.m add command -label "Add ..." -command {puts "Add"}
669
670 menubutton .top.help -text "Help" -menu .top.help.m
671 menu .top.help.m
672 .top.help.m add command -label "Help on help" -command {puts "Help on help"}
673 .top.help.m add command -label "About" -command {puts "About"}
674
675 pack .top.file .top.target .top.database -side left
676 pack .top.help -side right
677
678 label .mid.searchlabel -text {Search:}
679 entry .mid.searchentry -width 50 -relief sunken
680
681 listbox .data.list -yscrollcommand {.data.scroll set}
682 #-geometry 50x10
683 scrollbar .data.scroll -orient vertical -border 1
684 pack .data.list -side left -fill both -expand yes
685 pack .data.scroll -side right -fill y
686 .data.scroll config -command {.data.list yview}
687
688 message .bot.target -text "None" -aspect 1000 -relief sunken -border 1
689 label .bot.status -text "Not connected" -width 12 -relief \
690     sunken -anchor w -border 1
691 label .bot.message -text "" -width 20 -relief \
692     sunken -anchor w -border 1
693 pack .bot.target .bot.status .bot.message -anchor nw -side left -padx 2 -pady 2
694
695 bind .data.list <Double-Button-1> {set indx [.data.list nearest %y]
696 show-full-marc $indx}
697
698 set setNo 0
699 ir z39
700 z39 comstack tcpip
701 set csRadioType [z39 comstack]
702 z39 preferredMessageSize 12000