#!/bin/sh
#\
exec /usr/local/bin/wish8.5 "$0" ${1+"$@"}

#
# 
#  Copyright (c) 1994-1998 A & A Custom Software.
#  All rights reserved.
# 
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions
#  are met:
#  1. Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#  3. All advertising materials mentioning features or use of this software
#     must display the following acknowledgement:
#       This product includes software developed by A & A Custom Software 
#       and its contributors.
#  4. Neither the name of A & A Custom Software nor the names of its 
#     contributors may be used to endorse or promote products derived from this 
#     software without specific prior written permission.
# 
#  THIS SOFTWARE IS PROVIDED BY A & A CUSTOM SOFTWARE AND CONTRIBUTORS ``AS IS''
#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#  ARE DISCLAIMED.  IN NO EVENT SHALL A & A CUSTOM SOFTWARE OR CONTRIBUTORS BE 
#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
#  POSSIBILITY OF SUCH DAMAGE.
# 
# $Id: tkhylafax.unpatched,v 1.1 1998/01/04 23:11:02 abm Exp $ 
#
# Main for tkhylafax.
#

#
# Globals
#

set Th_Info(lwidth) 15 ;# standard label length 
set Th_Info(ewidth) 15 ;# standard entry length 
set Th_Info(plen) 25 ;# phone field length
set Th_Info(nlen) 36 ;# name field length
set Th_Info(flen) 36 ;# from field length
set Th_Info(clen) 36 ;# company field length
set Th_Info(llen) 41 ;# location field length
set Th_Info(rlen) 41 ;# regarding field length
set Th_Info(libraryPath) /usr/local/lib/tkhylafax ;# tkhylafax library path

proc tkhylafax_main { args } {

    global Th_Info
    global Comments

    # Make the main panel.
    set Th_Info(main) [mkFrame . main]
    set Th_Info(menubar) [mkFrame $Th_Info(main) menubar {-side top -fill x}] 
    set Th_Info(docs) [mkFrame $Th_Info(main) docs]
    set Th_Info(oper2) [mkFrame $Th_Info(main) oper2 {-side top -fill x}] 
    set Th_Info(cover) [mkFrame $Th_Info(main) cover {-side top -fill both}]
    set Th_Info(schedule) [mkFrame $Th_Info(main) schedule \
        {-side top -fill both}]
    set Th_Info(oper1) [mkFrame $Th_Info(main) oper1 {-side bottom -fill x}]

    # Make the menubuttons, menu items and set bindings
    # File Menubutton
    set Th_Info(fileMB) [mkMenuButton $Th_Info(menubar) file File]
    mkMenuItem $Th_Info(fileMB) Browser {fileSelect .browser} 
    mkMenuItem $Th_Info(fileMB) {Enter File to Fax} fileToFax
    mkMenuItem $Th_Info(fileMB) {Fax Server Status} {svrStatus .qstatus}
    mkMenuItem $Th_Info(fileMB) Send sendFax
    mkMenuItem $Th_Info(fileMB) {View Batch} {viewBatch .viewbatch}
    mkMenuItem $Th_Info(fileMB) {Send Batch} sendBatch 5
    mkMenuItem $Th_Info(fileMB) {Preview Cover Page} previewCover
    mkMenuItem $Th_Info(fileMB) {Clear Cover Sheet} clearCover
    mkMenuSeparator $Th_Info(fileMB)
    mkMenuItem $Th_Info(fileMB) {Exit} {destroy .} 1

    # Phone Menubutton
    set Th_Info(phoneMB) [mkMenuButton $Th_Info(menubar) phone Phone]
    mkMenuItem $Th_Info(phoneMB) Rolodex {rolodex .rolodex} 
    mkMenuItem $Th_Info(phoneMB) {Add Curent} updateRolodex
    mkMenuItem $Th_Info(phoneMB) {Delete Current} deleteEntry
    mkMenuItem $Th_Info(phoneMB) {Select Batch} {listBatches .listbatches}
    mkMenuItem $Th_Info(phoneMB) {Clear Current Batch} {clearBatch .viewbatch}

    # Options Menubutton
    set Th_Info(optionsMB) [mkMenuButton $Th_Info(menubar) options Options]
    mkMenuItem $Th_Info(optionsMB) Preferences {preferencesDialog .pref}
    mkMenuSeparator $Th_Info(optionsMB)
    mkRadioMenuItem $Th_Info(optionsMB) {196 lines/inch} \
        Th_Info(faxResolution) { -m }
    mkRadioMenuItem $Th_Info(optionsMB) {98 lines/inch} \
        Th_Info(faxResolution) { -l }
    mkMenuSeparator $Th_Info(optionsMB)
    mkCheckMenuItem $Th_Info(optionsMB) {Notify when Complete} \
        Th_Info(notifyOnCompletion) { -D }
    mkCheckMenuItem $Th_Info(optionsMB) {Notify on Retry} \
        Th_Info(notifyOnRetry) { -R }
    mkMenuSeparator $Th_Info(optionsMB)
    mkCheckMenuItem $Th_Info(optionsMB) {Initiate Poll} Th_Info(initiatePoll) \
        { -p }
    mkMenuSeparator $Th_Info(optionsMB)
    if {![info exists Th_Info(coverPages)] ||
        [llength $Th_Info(coverPages)] == 0} {
        mkCheckMenuItem $Th_Info(optionsMB) {No Cover Page} \
	   Th_Info(coverPage) { -n }
    } else {
        foreach cvrpg $Th_Info(coverPages) {
	   mkRadioMenuItem $Th_Info(optionsMB) $cvrpg Th_Info(coverPage) \
	       " -C $cvrpg "
        }
        mkRadioMenuItem $Th_Info(optionsMB) {No Cover Page} \
	   Th_Info(coverPage) { -n }
        set Th_Info(coverPage) " -C [lindex $Th_Info(coverPages) 0] "
    }

    # Make the listbox for documents to fax
    mkLabel $Th_Info(docs) label {-side top -anchor n} -text {Documents to Fax}
    set Th_Info(docsLB) [mkListbox $Th_Info(docs) -selectmode browse]

    # Make the edit operations buttons
    mkButton $Th_Info(oper2) edit {-side left} -text {Edit Document} \
        -command {editDocument $Th_Info(docsLB)}
    mkButton $Th_Info(oper2) remove {-side left} -text {Remove Document} \
        -command {removeDocument $Th_Info(docsLB)}
    mkButton $Th_Info(oper2) clear {-side left} -text {Clear List} \
        -command {$Th_Info(docsLB) delete 0 end}

    # Make the cover sheet panel
    mkLabel $Th_Info(cover) label {-side top -anchor n} \
        -text {Cover Sheet Information}
    mkLabeledEntry $Th_Info(cover) phone {Fax Number:} Th_Info(phone) \
        $Th_Info(plen)
    mkLabeledEntry $Th_Info(cover) name Recipient: Th_Info(name) $Th_Info(nlen)
    if {! $Th_Info(autoFrom)} {
	mkLabeledEntry $Th_Info(cover) from From: Th_Info(from) $Th_Info(flen)
    }
    mkLabeledEntry $Th_Info(cover) company Company: Th_Info(company) \
        $Th_Info(clen)
    mkLabeledEntry $Th_Info(cover) location Location: Th_Info(location) \
        $Th_Info(llen)
    mkLabeledEntry $Th_Info(cover) regard Regarding: Th_Info(regard) \
        $Th_Info(rlen)
    mkLabeledEntry $Th_Info(cover) ln1 Comments: Comments(ln1) $Th_Info(rlen)
    for {set i 2} {$i <= $Th_Info(commentLines)} {incr i} {
        mkLabeledEntry $Th_Info(cover) ln${i} {} Comments(ln${i}) $Th_Info(rlen)
    }

    # Add some traces for the entry widgets for phone, name and company
    setTrace Th_Info(phone) isPhoneNumber
    setTrace Th_Info(phone) "entryInputLenChk $Th_Info(plen)"
    setTrace Th_Info(name) "entryInputLenChk $Th_Info(nlen)"
    setTrace Th_Info(company) "entryInputLenChk $Th_Info(clen)"

    # Make the schedule info panel
    mkLabel $Th_Info(schedule) label {-side top -anchor n} \
        -text {Scheduling Information}

    mkTransmitDelay $Th_Info(schedule) td 
    mkMaximumRetries $Th_Info(schedule) retry

    # Make general operation buttons
    mkButton $Th_Info(oper1) send {-side left} -text Send \
	-command sendFax
    mkButton $Th_Info(oper1) rolodex {-side left} -text Rolodex  \
	-command {rolodex .rolodex} 
    mkButton $Th_Info(oper1) add {-side left} -text {Add Current} \
	-command {updateRolodex}
    mkButton $Th_Info(oper1) browser {-side left} -text Browser \
	-command {fileSelect .browser}
    mkButton $Th_Info(oper1) batch {-side left} -text {Select Batch} \
	-command {listBatches .listbatches} 
    mkButton $Th_Info(oper1) exit {-side left} -text Exit -command {destroy .}
    addPreference Th_Info(iconBitmap) iconBitmap
    catch {wm iconbitmap . @$Th_Info(libraryPath)/$Th_Info(iconBitmap)}

    # Here we collect any command line options that are not preferences 
    # and process them appropriately
    set filename [getOption file]
    if {$filename != ""} {
        $Th_Info(docsLB) insert end $filename
    }

}

proc mkTransmitDelay { parent frame } {

    global Th_Info
    frame $parent.$frame
    pack $parent.$frame -fill x -expand true
    mkLabel $parent.$frame tdLabel {-side left} -text {Transmit Delay:} \
        -anchor w -width $Th_Info(lwidth) 
    mkLabel $parent.$frame tdHours {-side left} -text 0 \
        -anchor w -textvar Th_Info(tdHours) 
    mkLabel $parent.$frame tdColon {-side left} -text {:} -anchor w 
    mkLabel $parent.$frame tdMin {-side left} -text 0 \
        -anchor w -textvar Th_Info(tdMin) 

    mkRepeatButton $parent.$frame subMin {-side right} \
        -bitmap @$Th_Info(libraryPath)/dn.xbm \
        -padx 0 -pady 0 -command {incrXmitDelay -5}
    mkRepeatButton $parent.$frame addMin {-side right} \
        -bitmap @$Th_Info(libraryPath)/up.xbm \
        -padx 0 -pady 0 -command {incrXmitDelay 5}
    return $parent.$frame
}

proc mkMaximumRetries { parent frame } {

    global Th_Info
    frame $parent.$frame
    pack $parent.$frame -fill x -expand true
    mkLabel $parent.$frame retryLabel {-side left} -text {Maximum Retries:} \
        -anchor w -width $Th_Info(lwidth) 
    mkLabel $parent.$frame retryCount {-side left} -text 0 \
        -anchor w -textvar Th_Info(retryCount) 

    mkButton $parent.$frame subRetry {-side right} \
        -bitmap @$Th_Info(libraryPath)/dn.xbm \
        -padx 0 -pady 0 -command {incrLabelNum Th_Info(retryCount) -1}
    mkButton $parent.$frame addRetry {-side right} \
        -bitmap @$Th_Info(libraryPath)/up.xbm \
        -padx 0 -pady 0 -command {incrLabelNum Th_Info(retryCount)}
    return $parent.$frame
}

proc incrXmitDelay { {value 1} } {

    global Th_Info
    if {$Th_Info(tdHours) == 0 && $Th_Info(tdMin) == 0 && $value < 0} {
	#tk_dialog .err "Ooops!" "Negative value not allowed." warning 0 OK
	return
    }

    #
    # Adjust time
    #
    if {$value > 0} {
        if {[expr $Th_Info(tdMin) + $value] >= 60} {
	   incr Th_Info(tdHours) 1
	   set Th_Info(tdMin) 0
        } else {
	   incr Th_Info(tdMin) $value
        }
    } else {
        if {$Th_Info(tdMin) <= 0} {
	   incr Th_Info(tdHours) -1
	   set Th_Info(tdMin) [expr 60 + $value] 
        } else {
	   incr Th_Info(tdMin) $value
        }
    }
}

proc incrLabelNum { textvar {value 1}} {

    global Th_Info
    if {[set $textvar] == 0 && $value < 0} {
	#tk_dialog .err "Ooops!" "Negative value not allowed." warning 0 OK
	return
    }
    incr $textvar $value
}


proc editDocument { lb } {
     
    global Th_Info
     
    if {![info exists Th_Info(editorCmd)]} {
         tk_dialog .err "Ooops!" "No editor command was defined." info 0 OK
         $lb select clear 0 end
         return
    }
    
    set idx [$lb curselection]
    if {$idx == ""} {
         return
    }
    set entry [$lb get $idx]
    if {[catch {eval exec $Th_Info(editorCmd) $entry &} err]} {
         tk_dialog .err "Ooops!" "$err" info 0 OK
    }
    $lb select clear 0 end
}    

proc removeDocument { lb } {

    set idx [$lb curselection]
    if {$idx == ""} {
         return
    }
    $lb delete $idx
}

#
# Make a dialog box for user to enter a specific filename, then put
# it in the list to fax. 
#
#

proc fileToFax { args } {

    global Th_Info

    set res [dialog_main -message "Enter file to fax. Remember pathnames!"]
    if {$res != ""} { 
        $Th_Info(docsLB) insert end $res
    }
}

#
# Preview fax cover sheet via ghostview.
#

proc previewCover { args } {

    set faxcmd "[mkCover] | /usr/local/bin/gv -"

    # Exec it
    if {[catch {eval exec $faxcmd &} err]} {
        tk_dialog .err "Ooops!" "$err" questhead 0 OK
    }
}

proc clearCover { args } {

    global Th_Info
    global Comments
    foreach i {phone name company location regard} {
        set Th_Info($i) {}
    }
    foreach i [array name Comments] {
        set Comments($i) {}
    }
}

proc sendFax { args } {

    global Th_Info

    if {$Th_Info(phone) != "" && $Th_Info(batchDest) != ""} {
        set res \
            [tk_dialog .err "Ooops!" "A batch destination AND a cover sheet \
            destination have been defined! \n\nSelect a fax destination to use"\
            questhead 0 "Abort" "Cover Sheet" "Batch" "Both"]
    
        switch -exact -- $res 1 {
            faxIt
        } 2 {
            sendBatch
        } 3 {
            # Must do cover sheet first so data isn't lost
            faxIt
            sendBatch
        } default {}
    } elseif {$Th_Info(phone) != ""} {
        # Send fax from cover sheet.
        faxIt
    } elseif {$Th_Info(batchDest) != ""} {
        # Send batch destinations.
        sendBatch
    } else {
        # Notify user: where do you want to send a fax?
        tk_dialog .err "Ooops!" "Huh!?!? \nNo batch destinations are defined \
            AND no fax number is specified in the cover sheet section!" \
            questhead 0 OK
    }
}

lappend auto_path $Th_Info(libraryPath)
initPreference $Th_Info(libraryPath)/tkhylafax-defaults
setOptions
initRolodex
initBatch
tkhylafax_main
