1#
2# Copyright (C) 1997, 1998, 1999  Motoyuki Kasahara
3#
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 2, or (at your option)
7# any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14
15# Whether BookView connects to server1 at start.
16# (used in the setup window only)
17set setup_start_server1 1
18
19#
20# `Setup...'
21#
22proc setup {} {
23    global start_server1
24    global setup_start_server1
25    global server_names
26
27    setup_make_window
28
29    if {[llength $server_names] == 0} {
30	set setup_start_server1 1
31    } else {
32	set setup_start_server1 $start_server1
33    }
34
35    set oldfocus [focus]
36
37    #
38    # Popup a sub window.
39    #
40    wm title .setup [_ "bookview setup"]
41    wm iconname .setup "bookview setup"
42    wm withdraw .setup
43
44    set x [expr [winfo rootx .] + 24]
45    set y [expr [winfo rooty .] + 24]
46    wm geometry .setup +$x+$y
47    wm deiconify .setup
48
49    focus .setup
50    grab .setup
51    tkwait window .setup
52
53    if {![catch {winfo .setup}]} {
54	destroy .setup
55    }
56    focus $oldfocus
57}
58
59#
60# Make a setup window.
61#
62proc setup_make_window {} {
63    global max_servers
64    global start_server1
65    global setup_start_server1
66    global server_names
67    global server_ports
68    global server_options
69
70    #
71    # Toplevel
72    #
73    toplevel .setup
74    bind .setup <Return> setup_ok
75    bind .setup <Control-m> setup_ok
76    bind .setup <Escape> {destroy .setup}
77
78    #
79    # Server 1, 2, ... n
80    #
81    set length [llength $server_names]
82    for {set i 1} {$i <= $max_servers} {incr i} {
83	frame .setup.server$i -relief sunken -borderwidth 2
84	pack .setup.server$i -side top -padx 2 -fill x
85
86	label .setup.server$i.id -text [format [_ "Server %d:"] $i]
87	pack .setup.server$i.id -side top -anchor w -pady 2
88
89	frame .setup.server$i.hostname
90	pack .setup.server$i.hostname -side top -fill x
91	label .setup.server$i.hostname.label -text [_ "hostname:"] \
92		-width 11
93	pack .setup.server$i.hostname.label -side left
94	entry .setup.server$i.hostname.entry -relief sunken -borderwidth 2 \
95		-width 50
96	pack .setup.server$i.hostname.entry -side left -anchor w -expand 1 \
97		-fill x
98
99	frame .setup.server$i.port
100	pack .setup.server$i.port -side top -fill x
101	label .setup.server$i.port.label -text [_ "    port:"] \
102		-width 11
103	pack .setup.server$i.port.label -side left
104	entry .setup.server$i.port.entry -relief sunken -borderwidth 2 \
105		-width 50
106	pack .setup.server$i.port.entry -side left -anchor w -expand 1 \
107		-fill x
108
109	frame .setup.server$i.options
110	pack .setup.server$i.options -side top -fill x
111	label .setup.server$i.options.label -text [_ " options:"] \
112		-width 11
113	pack .setup.server$i.options.label -side left
114	entry .setup.server$i.options.entry -relief sunken -borderwidth 2 \
115		-width 50
116	pack .setup.server$i.options.entry -side left -anchor w -expand 1 \
117		-fill x
118
119	#
120	# Set default values.
121	#
122	if {$i <= $length} {
123	    .setup.server$i.hostname.entry insert 0 \
124		    [lindex $server_names [expr $i - 1]]
125	    .setup.server$i.port.entry insert 0 \
126		    [lindex $server_ports [expr $i - 1]]
127	    .setup.server$i.options.entry insert 0 \
128		    [lindex $server_options [expr $i - 1]]
129	} else {
130	    .setup.server$i.hostname.entry insert 0 ""
131	    .setup.server$i.port.entry insert 0 "ndtp"
132	    .setup.server$i.options.entry insert 0 "start=@all"
133	}
134    }
135
136    #
137    # Whether connect to server 1 at start of BookView.
138    #
139    frame .setup.at_start
140    pack .setup.at_start -side top
141    checkbutton .setup.at_start.check -variable setup_start_server1 \
142  	    -text [_ "Connect to server 1 at start of BookView"] -onvalue 1 \
143  	    -offvalue 0
144    pack .setup.at_start.check -side top -pady 2 -fill x
145
146    #
147    # Ok and Cancel buttons.
148    #
149    frame .setup.ok-cancel
150    pack .setup.ok-cancel -side top -pady 4 -fill x
151
152    button .setup.ok-cancel.ok -text [_ "Ok"] -command setup_ok -default active
153    pack .setup.ok-cancel.ok -side left -expand 1
154
155    button .setup.ok-cancel.cancel -text [_ "Cancel"] -command {destroy .setup}
156    pack .setup.ok-cancel.cancel -side left -expand 1
157}
158
159#
160# `Ok' on the setup window.
161#
162proc setup_ok {} {
163    global max_servers
164    global start_server1
165    global setup_start_server1
166    global server_names
167    global server_ports
168    global server_options
169    global server_starts
170    global server_demand_loads
171    global server_book_groups
172
173    #
174    # Close the current connection.
175    #
176    select_server 0
177
178    #
179    # Set a server list.
180    #
181    set server_names {}
182    set server_ports {}
183    set server_options {}
184    set server_starts {}
185    set server_demand_loads {}
186    set server_book_groups {}
187    for {set i 1} {$i <= $max_servers} {incr i} {
188	set hostname [.setup.server$i.hostname.entry get]
189	set port [.setup.server$i.port.entry get]
190	set option [.setup.server$i.options.entry get]
191	regsub {[ 	:].*} $hostname {} hostname
192	regsub {[ 	:].*} $port {} port
193	regsub -all ":" $option " " option
194
195	if {![regexp {^[ 	]*$} $hostname]} {
196	    if {[regexp {^[ 	]*$} $port]} {
197		set $port "ndtp"
198	    }
199	    lappend server_names $hostname
200	    lappend server_ports $port
201	    lappend server_options $option
202	    setup_parse_options $option
203	}
204    }
205
206    #
207    # Whether connect to server 1 at start of BookView.
208    #
209    if {![regexp {^[ 	]*$} [.setup.server1.hostname.entry get]] \
210	    && $setup_start_server1} {
211	set start_server1 1
212    } else {
213	set start_server1 0
214    }
215
216    #
217    # Update `Server'.
218    #
219    set_server_menu
220
221    #
222    # Save a setup file.
223    #
224    setup_save_file
225
226    destroy .setup
227}
228
229#
230# Load a setup file.
231#
232proc setup_load_file {} {
233    global system_setup_file
234    global user_setup_file
235    global server_names
236    global server_ports
237    global server_options
238    global server_starts
239    global server_demand_loads
240    global server_book_groups
241    global start_server1
242    global bitmap_checkbutton
243    global toolbar_checkbutton
244    global method_radiobutton
245    global max_servers
246    global text_spacing
247
248    set server_names {}
249    set server_ports {}
250    set server_starts {}
251    set start_server1 0
252    set server_demand_loads {}
253    set server_book_groups {}
254
255    #
256    # Open a setup-file.
257    # Try user setup file first, and then system setup file.
258    #
259    set setup_file $user_setup_file
260    output_information [format [_ "Load %s..."] $setup_file]
261    if {[catch {set file [open $setup_file "r"]}]} {
262	set setup_file $system_setup_file
263	output_information [format [_ "Load %s..."] $setup_file]
264	if {[catch {set file [open $setup_file "r"]}]} {
265	    output_information [_ "Cannot open a setup file."]
266	    return 0
267	}
268    }
269
270    #
271    # Read a setup-file.
272    #
273    set lineno 0
274    while {1} {
275	if {[catch {gets $file line}]} {
276	    output_information [format [_ "Cannot read %s."] $setup_file]
277	    catch {close $file}
278	    return 0
279	}
280	incr lineno
281	if {[regexp {^server ([^:]+):([^:]+):(.*)} $line dummy \
282		hostname port options]} {
283	    lappend server_names $hostname
284	    lappend server_ports $port
285	    lappend server_options $options
286	    setup_parse_options $options
287	} elseif {[regexp {^start-server1 ([01])$} $line dummy arg]} {
288	    set start_server1 $arg
289	} elseif {[regexp {^display-bitmaps ([0-9]+)$} $line dummy arg]} {
290	    set bitmap_checkbutton $arg
291	} elseif {[regexp {^display-toolbar ([0-9]+)$} $line dummy arg]} {
292	    set toolbar_checkbutton $arg
293	} elseif {[regexp {^text-spacing ([0-9]+)$} $line dummy arg]} {
294	    set text_spacing $arg
295	} elseif {[regexp {^default-method (.+)$} $line dummy arg]} {
296	    ndtp_set_default_method $arg
297	    set method_radiobutton $arg
298	}
299	if {[eof $file]} {
300	    break
301	}
302    }
303
304    #
305    # Close a setup-file.
306    #
307    output_information [format [_ "Load %s...done."] $setup_file]
308    catch {close $file}
309
310    #
311    # Check for the setup.
312    #
313    set length [llength $server_names]
314    if {$max_servers < $length} {
315	set server_names [lrange $server_names 0 [expr $max_servers - 1]]
316	set server_ports [lrange $server_ports 0 [expr $max_servers - 1]]
317	set server_starts [lrange $server_starts 0 [expr $max_servers - 1]]
318    } elseif {$length == 0} {
319	set start_server1 0
320    }
321
322    if {$text_spacing < 0} {
323	set text_spacing 0
324    }
325
326    return 1
327}
328
329#
330# Save a setup file.
331#
332proc setup_save_file {} {
333    global user_setup_file
334    global server_names
335    global server_ports
336    global server_options
337    global start_server1
338    global bitmap_checkbutton
339    global toolbar_checkbutton
340    global method_radiobutton
341    global text_spacing
342
343    #
344    # Open a setup file.
345    #
346    output_information [format [_ "Save %s..."] $user_setup_file]
347    if {[catch {set file [open $user_setup_file "w"]}]} {
348	output_information [format [_ "Cannot open %s"] $user_setup_file]
349	return
350    }
351
352    #
353    # Write to a setup file.
354    #
355    set length [llength $server_names]
356    for {set i 0} {$i < $length} {incr i} {
357	set hostname [lindex $server_names $i]
358	set port [lindex $server_ports $i]
359	set option [lindex $server_options $i]
360	catch {puts $file "server $hostname:$port:$option"}
361    }
362    catch {puts $file "start-server1 $start_server1"}
363    catch {puts $file "prefer-sizes 18"}
364    catch {puts $file "display-bitmaps $bitmap_checkbutton"}
365    catch {puts $file "display-toolbar $toolbar_checkbutton"}
366    catch {puts $file "text-spacing $text_spacing"}
367    catch {puts $file "default-method $method_radiobutton"}
368    if {[catch {flush $file}]} {
369	output_information [format [_ "Cannot write to %s."] $user_setup_file]
370	catch {close $file}
371	return
372    }
373
374    #
375    # Close a setup file.
376    #
377    catch {close $file}
378    output_information [format [_ "Save %s....done."] $user_setup_file]
379}
380
381#
382# Parse a server option stinrg.
383#
384proc setup_parse_options {options} {
385    global server_starts
386    global server_demand_loads
387    global server_book_groups
388
389    set start ""
390    set demand_load {}
391    set book_group {}
392
393    append options ";"
394    while {[regexp {([^;]*);(.*)} $options dummy opt options]} {
395	regsub {^[ 	](.*)[ 	]*$} $opt {\1} opt
396	if {[regexp {^start=(.*)$} $opt dummy arg]} {
397	    set start $arg
398	} elseif {[regexp {^demand-load=(.*)$} $opt dummy arg]} {
399	    regsub "," $arg " " arg
400	    foreach i $arg {
401		lappend demand_load $i
402	    }
403	} elseif {[regexp {^group=([^,]*),([^,]*),(.*)$} \
404		$opt dummy name title arg]} {
405	    if {![string match $name {@*}]} {
406		set name "@$name"
407	    }
408	    regsub -all "," $arg " " arg
409	    set expanded {}
410	    foreach i $arg {
411		lappend expanded $i
412	    }
413	    lappend book_group [list $name $title $expanded]
414	}
415    }
416
417    lappend server_starts $start
418    lappend server_demand_loads $demand_load
419    lappend server_book_groups $book_group
420}
421