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