#!/usr/local/bin/wish -f

# $Id: xrfax,v 0.5 1995/01/04 08:01:20 juphoff Exp $

# xrfax -- A basic X-windows front-end to 'rfax'.
# Copyright (C) 1994 Jeffrey A. Uphoff
#   
# 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 2 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, write to the Free Software
# Foundation, Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
#   
# Jeffrey A. Uphoff
# National Radio Astronomy Observatory
# 520 Edgemont Road
# Charlottesville, VA 22903, USA
#   
# Phone:     (804) 296-0208
# Internet:  juphoff@nrao.edu

# Written after about 3 hours study of the Tcl language--please
# excuse my style and possible lack of functional elegance.

proc tkerror err {
    global errorInfo
    set info $errorInfo
    if {[tk_dialog .tkerrorDialog "xrfax error" \
	     "Error: $err" error 0 OK "Exit program"] == 0} {
        return
    } else {
	exit
    }
}

proc SmartEntries {w} {
    bind $w <Control-d> "SmartDelete $w"
    bind $w <Button-3> "selection clear $w"
    bind $w <Button-2> "SmartInsert $w"
}

proc SmartInsert {w} {
    if {[selection own] != ""} {
	catch {$w insert insert [selection get]}
    }
}

proc SmartDelete {w} {
    if {[selection own] == $w} {
	catch {$w delete sel.first sel.last}
    }
}

proc SelectFile {{w .fileselect}} {
    global server default_server starting_dir
    cd $starting_dir
    catch {destroy $w}
    toplevel $w
    wm title $w "Select file to fax"
    frame $w.buttons
    listbox $w.entries -relief raised -bd 2 -yscrollcommand "$w.scrolly set"\
	-xscrollcommand "$w.scrollx set"
    pack $w.buttons -side bottom -fill x
    pack $w.entries -side left
    scrollbar $w.scrolly -command "$w.entries yview"
    scrollbar $w.scrollx -orient horizontal -command "$w.entries xview"
    pack $w.scrollx -fill x -side bottom -after $w.buttons
    pack $w.scrolly -fill y -side right -before $w.scrollx
    button $w.button1 -text "FAX" -command "GetFile $w"
    button $w.button2 -text "Cancel" -command "cd $starting_dir ; destroy $w"
    pack $w.button1 $w.button2 -side left -fill x -expand 1 -in $w.buttons
    GetDir . $w
    tk_listboxSingleSelect $w.entries
    bind $w.entries <Button-3> "selection clear $w.entries"
    bind $w.entries <Double-Button-1> "GetFile $w"
}

proc GetFile {w} {
    catch {set file [selection get]}
    if {![info exists file]} {
	return
    }
    if {[file isdirectory $file]} {
	cd $file
	GetDir . $w
    }
}

proc GetDir {dir w} {
    $w.entries delete 0 end
    foreach i [exec ls -aF $dir] {
	$w.entries insert end $i
    }
}

proc GetServer {} {
    set file [open "| rfax -h"]
    gets $file line
    split $line
    close $file
    lindex $line 1
}

proc SetServer {{w .server}} {
    global server default_server
    catch {destroy $w}
    toplevel $w
    wm title $w "Change fax server"
    message $w.message -justify center -width 7c -pady 2 -bd 2\
	-text "Enter name of fax server:"
    entry $w.entry -width 30 -relief sunken -bd 2 -textvariable server
    button $w.button1 -text "OK" -bd 5 -command "destroy $w ; return"
    button $w.button4 -text "Clear"\
	-command "set server \"\" ; pack forget $w.entry ;\
	    pack $w.entry -side top -pady 2 -padx 3m -before $w.button1"
    button $w.button2 -text "Default"\
	-command "set server $default_server"
    pack $w.message -side top
    pack $w.entry -side top -pady 2 -padx 3m
    pack $w.button1 -side left -padx 3m -ipadx 3m -pady 10 -expand 1
    pack $w.button4 -side left -padx 3m -ipadx 2m -pady 10 -expand 1
    pack $w.button2 -side left -padx 3m -pady 10 -expand 1 
    focus none
    bind $w <Enter> "focus $w.entry"
    bind $w.entry <Enter> "focus $w.entry"
    bind $w.entry <Leave> "focus none"
    bind $w <Leave> "focus none"
    SmartEntries $w.entry
    bind $w.entry <Return> "destroy $w"
}

proc RunQ {} {
    global server default_server
    .display delete 1.0 end
    if {$server == ""} {
	.display insert end "No server defined!\n"
	return
    }
    if {$server != $default_server} {
	set file [open "| rfax -s $server -q"]
    } else {
	set file [open "| rfax -q"]
    }
    while {[gets $file line] >= 0}  {
	.display insert end "$line\n"
    }
    close $file
}

proc RemoveQ {{w .removal}} {
    global server default_server
    if {$server == ""} {
	.display delete 1.0 end
	.display insert end "No server defined!\n"
	return
    }
    catch {destroy $w}
    toplevel $w
    wm title $w "Cancel fax"
    if {$server != $default_server} {
	set file [open "| rfax -s $server -q"]
    } else {
	set file [open "| rfax -q"]
    }
    frame $w.buttons
    listbox $w.entries -relief raised -bd 2 -yscrollcommand "$w.scroll set"
    pack $w.buttons -side bottom -fill x
    pack $w.entries -side left
    scrollbar $w.scroll -command "$w.entries yview"
    pack $w.scroll -fill y -side right
    button $w.button1 -text "Remove" -command DoRemove
    button $w.button2 -text "Cancel" -command "destroy $w"
    pack $w.button1 $w.button2 -side left -fill x -expand 1 -in $w.buttons
    while {[gets $file line] >= 0} {
	if {[regexp ^no $line] == 0} {
	    $w.entries insert end "[lindex [split $line /] 0]\n"
	}   
    }
    close $file
    tk_listboxSingleSelect $w.entries
    bind $w.entries <Double-Button-1> DoRemove
    bind $w.entries <Button-3> "selection clear $w.entries"
}

proc DoRemove {{w .removal}} {
    global server default_server
    catch {set remove [lindex [selection get] 0]}
    destroy $w
    if {![info exists remove]} {
	return
    }
    .display delete 1.0 end
    if {$server != $default_server} {
	set file [open "| rfax -s $server -r $remove"]
    } else {
	set file [open "| rfax -r $remove"]
    }
    while {[gets $file line] >= 0} {
	.display insert end $line
    }
    close $file
}

proc Info {{w .info}} {
    catch {destroy $w}
    toplevel $w
    wm title $w "About xrfax"
    message $w.message -width 40c -justify left\
	-text "xrfax:  A Tcl/Tk X-windows front-end to\
	    rfax/rfaxd.\n\nrfax/rfaxd is a Perl-based,\
	    network front-end to mgetty+sendfax.\n\n\nxrfax,\
	    rfax, and rfaxd are (c) 1994\
	    Jeffrey A. Uphoff <juphoff@nrao.edu>.\nmgetty+sendfax\
	    is (c) 1994 Gert Doering\
	    <gert@greenie.muc.de>."    
    button $w.button -text "OK" -command "destroy $w"
    pack $w.message
    pack $w.button -pady 10
}


wm title . "xrfax"
set server [set default_server [GetServer]]
set starting_dir [pwd]
frame .statusbar -relief raised -bd 2
message .serveridentlabel -width 2c -text "Server: "
entry .serverident -width 30  -relief sunken -bd 2 -textvariable server
frame .menu -relief raised -bd 2
pack .menu -side top -fill x
menubutton .menu.fax -text "Fax" -menu .menu.fax.m
menu .menu.fax.m
.menu.fax.m add command -label "Select file" -command SelectFile
.menu.fax.m add separator
.menu.fax.m add command -label "Exit" -command exit
menubutton .menu.server -text "Server" -menu .menu.server.m
menu .menu.server.m
.menu.server.m add command -label "Change" -command "SetServer"
menubutton .menu.queue -text "Queue" -menu .menu.queue.m
menu .menu.queue.m
.menu.queue.m add command -label "View items" -command RunQ
.menu.queue.m add command -label "Remove item" -command RemoveQ
menubutton .menu.misc -text "Misc" -menu .menu.misc.m
menu .menu.misc.m
.menu.misc.m add command -label "About" -command Info
pack .menu.fax .menu.queue .menu.server .menu.misc -side left -padx 1m
text .display -yscrollcommand ".scroll set" -width 80 -height 10
pack .display -side left -fill both -expand 1
scrollbar .scroll -command ".display yview"
pack .scroll -side right -fill y
pack .statusbar -side bottom -fill x -before .display
pack .serveridentlabel -side left -in .statusbar
pack .serverident -side left -in .statusbar
focus none
bind .display <Enter> {.display config -state disabled}
bind .display <Leave> {.display config -state normal}
bind .serverident <Enter> {focus .serverident}
bind .serverident <Leave> {focus none}
bind .serverident <Return> {.display config -state normal}
SmartEntries .serverident
bind .display <Button-3> "selection clear .display"
