#		
#  Module Interface.tcl
#
#
# $Log: Interface.tcl,v $
# Revision 1.3  2006/10/26 09:39:17  vfom
# Added code for netstate server query
#
# Revision 1.2  2006/09/29 09:22:57  vfom
# snmpwalk output processing correction
#
# Revision 1.1.1.1  2006/09/21 10:57:02  vfom
# Loading TkNetmon to CVS
#
#
namespace eval Interface {
variable	editIf_exitFlag	0 \
		nodeKey		"" \
		iflist		[list] \
		ifs		[list] \
		tmpSnmp		"" \
		errSnmp		"" \
		err_msg		{} \
		snmp_exitFlag	"" 
array set 	mArray		[list]
}
#
#             
proc EditIf_GUIconstructor {  } {
    set w .nodeProperties.editIf
    set e $w.entryFrame
    set m $w.methodsFrame
    set mg $m.grid
    set mb $m.buttons
    set b $w.buttonFrame
#
    toplevel $w -class Dialog
    wm transient $w [winfo toplevel [winfo parent $w]]	
    wm withdraw $w
#
    frame $e -borderwidth 1 -relief sunken
    pack $e -side top -padx 5 -pady 5 -expand on -fill both
    set curr_row 0
    label  $e.lname -text [msgcat::mc "Interface name:"]
    entry  $e.ename -width 48
    button $e.bname -text "..." -command "tk_chooseOption $w $e.ename ::Interface::ifs"
    grid $e.lname -in $e -row $curr_row -column 0 -sticky e
    grid $e.ename -in $e -row $curr_row -column 1 -sticky w
    grid $e.bname -in $e -row $curr_row -column 2 -sticky w
    grid configure $e.lname $e.ename $e.bname -padx 5 -pady 5
    incr curr_row
#
    label  $e.ldescr -text [msgcat::mc "Description:"]
    entry  $e.edescr -width 48
    grid $e.ldescr -in $e -row $curr_row -column 0 -sticky e
    grid $e.edescr -in $e -row $curr_row -column 1 -sticky w
    grid configure $e.ldescr $e.edescr  -padx 5 -pady 5
    incr curr_row
#
    label  $e.ldir -text [msgcat::mc "Data directory:"]
    entry  $e.edir -width 48
    grid $e.ldir -in $e -row $curr_row -column 0 -sticky e
    grid $e.edir -in $e -row $curr_row -column 1 -sticky w
    grid configure $e.ldir $e.edir  -padx 5 -pady 5
    incr curr_row    
#    
    label  $e.lbwd -text [msgcat::mc "Bandwidth:"]
    entry  $e.ebwd -width 48 \
        -validatecommand { 
	    regsub -all {[0-9]+} [%W get] {} tmp_bwd 
	    if { $tmp_bwd !="" } { 
	        set ::Interface::err_msg [msgcat::mc "Invalid bandwidth: must be number."] 
		return false 
	    }
	    return true
	}\
	-invalidcommand {
	    set tmp_bwd [%W get]
	    selection clear
	    tk_dialog .nodeProperties.editIf.dlg [msgcat::mc "Error"]  \
		$::Interface::err_msg error 0 [msgcat::mc "Ok" ]
	    focus %W	    
	}
    grid $e.lbwd -in $e -row $curr_row -column 0 -sticky e
    grid $e.ebwd -in $e -row $curr_row -column 1 -sticky w 
    grid configure $e.lbwd $e.ebwd  -padx 5 -pady 5
    incr curr_row    
#
    frame $m -borderwidth 1 -relief sunken   
    pack $m -side top -padx 5 -pady 5 -expand on -fill both -anchor s
    label $m.lb -text [msgcat::mc "Interface methods"] -font $::headerFont
    grid $m.lb -in $m -column 0 -row 0 -sticky w
    grid rowconfig $m 1 -weight 1 -minsize 0
    grid columnconfig $m 0 -weight 1 -minsize 0
    frame $mg
    grid $mg -in $m -column 0 -row 1 -sticky news
    grid configure $mg  -padx 5 -pady 5

    array set ::Interface::mArray [list  {0,0} [msgcat::mc "Type"] {0,1} [msgcat::mc "Name"] {0,2} [msgcat::mc "Parameters"] ]
    
    grid rowconfig $mg 0 -weight 1 -minsize 0
    grid columnconfig $mg 0 -weight 1 -minsize 0
#
	table $m.tbl -rows 1  -cols 3 \
	    -variable ::Interface::mArray \
	    -titlerows 1 -titlecols 0 \
	    -yscrollcommand "$m.sy set "  \
	    -xscrollcommand "$m.sx set "  \
	    -roworigin 0 -colorigin 0 \
	    -width 450 -height 400 \
	    -selectmode browse \
	    -flashmode 0 \
	    -background #f700f700f700 \
	    -borderwidth 2 \
	    -selecttype row	\
	    -state disabled  \
	    -resizeborders both \
	    -anchor w
    scrollbar $m.sy -command "$m.tbl yview"
    scrollbar $m.sx -orient horiz -command "$m.tbl xview"
    grid $m.tbl -in $mg \
	-row 0 -column 0 -rowspan 1 -columnspan 1 -sticky news
    grid $m.sy -in $mg -column 1 -row 0 \
	-columnspan 1 -rowspan 1 -sticky news
    grid $m.sx -in $mg -column 0 -row 1 \
	-columnspan 1 -rowspan 1 -sticky news
    $m.tbl tag config title -bg #0000bf00ff00 -relief groove
    $m.tbl tag config sel -fg #f700f700f700 -bg #48003d008b00 
    $m.tbl width 0 12 1 18 2 40
    bind $m.tbl <Double-1> "LinkMethod $w $m.tbl {}   edit"
#
    frame $mb	
    grid $mb -in $m -column 0 -row 2 \
	-columnspan 1 -rowspan 1 -sticky n
    button	$mb.badd  -text [msgcat::mc "Add"]    -command "LinkMethod $w $m.tbl Save add"
    button	$mb.bedit -text [msgcat::mc "Edit"]   -command "LinkMethod $w $m.tbl {}   edit"
    button	$mb.bdel  -text [msgcat::mc "Delete"] -command "LinkMethod $w $m.tbl {}   delete"
    pack $mb.badd $mb.bedit $mb.bdel -padx 5 -pady 5 -side left 
#	
    frame $b
    pack $b -side bottom -padx 5 -pady 5 -expand off -anchor s
    button $b.ok -text [msgcat::mc "Ok"] -command "EditInterface_OkButton $e"
    button $b.cancel -text [msgcat::mc "Cancel"] -command "set ::Interface::editIf_exitFlag 2"
    pack $b.ok $b.cancel -side left -padx 5 -pady 5

    update idletasks
    set x [expr {[winfo screenwidth $w]/2 - [winfo reqwidth $w]/2 \
    	- [winfo vrootx [winfo parent $w]]}]
    set y [expr {[winfo screenheight $w]/2 - [winfo reqheight $w]/2 \
    	- [winfo vrooty [winfo parent $w]]}]
    set wdt [winfo reqwidth $w]
    set ht  [expr [winfo reqheight $w] +150 ]
    wm geom $w [format "%sx%s+%s+%s" $wdt $ht $x $y]
#	
    bind $w <Destroy> "set ::Interface::editIf_exitFlag 2"
    bind $w <Escape>  "set ::Interface::editIf_exitFlag 2"
}
#
proc EditIf { elbox eip esnmpget mode } {
    set ip [$eip get]
    set snmpget [$esnmpget get]
    set ifind   [ $elbox curselection ]
    set ifname ""
    if { $ifind !="" } { set ifname  [ $elbox get  $ifind ]  }
    set ifkey   [ DLsecondByFirst $::Interface::iflist $ifname ]	
#
    switch $mode {
	delete { 
	    if { $ifname == "" } { return }
	    if { $ifkey != "" } {
	    	unset ::if_methodslist($ifkey)
		unset ::if_name($ifkey)
		unset ::if_owner($ifkey)
		unset ::if_descr($ifkey)
		unset ::if_datadir($ifkey)
		unset ::if_bandwidth($ifkey)
	    }
	    set ::Interface::iflist [ DLdeleteByFirst $::Interface::iflist $ifname ]
	    $elbox delete $ifind
	    return
	}
	edit { 
	    if { $ifname == "" } { return }
	    if { $ifkey == "" } { 
		set ifkey .if$::ifIndex
	    	set ::if_methodslist($ifkey) [list]
		set ::if_name($ifkey)	$ifname
		set ::if_owner($ifkey)	$::Interface::nodeKey
		set ::if_descr($ifkey)		""
		set ::if_datadir($ifkey)  	""
		set ::if_bandwidth($ifkey)	""
		incr ::ifIndex		
	    }
	}
	add  { 
	    set ifkey .if$::ifIndex
	    set ::if_methodslist($ifkey) [list]
	    set ::if_name($ifkey)	""
	    set ::if_owner($ifkey)	$::Interface::nodeKey
	    set ::if_descr($ifkey)	""
	    set ::if_datadir($ifkey)	""
	    set ::if_bandwidth($ifkey)  ""
	    incr ::ifIndex		
	    if { [llength $::Interface::ifs] <=0  } {
		if { ![ LoadIfNames $ip $snmpget ] } {
		    return
		}
	    }
	}
	clone {
	    if { $ifname == "" } { return }
	    if { $ifkey == "" } { return }
	    set src_ifkey $ifkey
	    set ifkey .if$::ifIndex
	    set ::if_methodslist($ifkey) $::if_methodslist($src_ifkey)
	    set ::if_name($ifkey)	""
	    set ::if_owner($ifkey)	$::Interface::nodeKey
	    set ::if_descr($ifkey)	$::if_descr($src_ifkey)
	    set ::if_datadir($ifkey)	$::if_datadir($src_ifkey)	
	    set ::if_bandwidth($ifkey)  $::if_bandwidth($src_ifkey)
	    incr ::ifIndex		
	    if { [llength $::Interface::ifs] <=0  } {
		if { ![LoadIfNames $ip $snmpget ] } {
		    return
		}
	    }
	}
    }
    #    
    set w .nodeProperties.editIf
    set e $w.entryFrame
    set m $w.methodsFrame
    set mg $m.grid
    set mb $m.buttons
    set b $w.buttonFrame
    #
    if { ![winfo exists $w] } {
	EditIf_GUIconstructor
    }
    #
    # Load actiual data
    #
    $e.ename configure -state normal
     $e.ename insert 0 $::if_name($ifkey)	
    $e.ename configure -state disabled
    if { [string equal $mode edit] } {
	$e.bname configure -state disabled
    } else {
	$e.bname configure -state normal
    }
    $e.edescr insert 0 $::if_descr($ifkey)
    $e.edir   insert 0 $::if_datadir($ifkey)
    $e.ebwd   insert 0 $::if_bandwidth($ifkey)
    set idx 1
    foreach i $::if_methodslist($ifkey) {
	set ::Interface::mArray($idx,0) [lindex $i 0]
	set ::Interface::mArray($idx,1) [lindex $i 1]
	set ::Interface::mArray($idx,2) [lindex $i 2]
	incr idx
    }
    $m.tbl configure -rows $idx
    #
    # Start Entry validation
    #
    $e.ebwd configure -validate focusout
    #
    if { [string equal [wm state $w] withdrawn ] } {
        wm deiconify $w
    } 
    raise $w
        
    set ::Interface::editIf_exitFlag 0
    update idletasks
    set oldFocus [focus]
    set oldGrab [grab current $w]
    if { [string compare $oldGrab ""] } {
	set grabStatus [grab status $oldGrab]
    }
    grab $w 
    focus $b.ok
    
    tkwait variable ::Interface::editIf_exitFlag

    if {[winfo exists $w]} {
	wm withdraw $w
	catch {focus $oldFocus}
	grab release $w
	if {$::Interface::editIf_exitFlag==1} {
	    if {[string equal $mode add ] || [string equal $mode clone ]} {
		set ::if_name($ifkey)	[ $e.ename get ]	
		$elbox insert end $::if_name($ifkey)
		lappend ::Interface::iflist [list $::if_name($ifkey) $ifkey ]
	    }
	    set ::if_descr($ifkey)	[ $e.edescr get ]	
	    set ::if_datadir($ifkey)	[ $e.edir get ]	
	    set ::if_bandwidth($ifkey)	[ $e.ebwd get ]	
	    #
	    set nmethods [ expr [array size ::Interface::mArray] / 3 ]
	    set tmp_mlist [list]
	    puts $nmethods
	    for {set i 1} {$i <$nmethods} {incr i} {
		lappend tmp_mlist [list $::Interface::mArray($i,0) $::Interface::mArray($i,1) $::Interface::mArray($i,2)]
	    }
	    set ::if_methodslist($ifkey) $tmp_mlist	    
	}
	#
	# Stop entry validation
	#
	$e.ebwd configure -validate none	
	# cleanup entries
	#
	$e.ename configure -state normal
	$e.ename delete 0 end
	$e.edescr delete 0 end
	$e.edir delete 0 end
	$e.ebwd delete 0 end
	$m.tbl configure -rows 1
    }
    array unset ::Interface::mArray	
    array set ::Interface::mArray [list  {0,0} [msgcat::mc "Type"] {0,1} [msgcat::mc "Name"] {0,2} [msgcat::mc "Parameters"] ]
    #
    if {[string compare $oldGrab ""] && [winfo exists $oldGrab] } {
	if {![string compare $grabStatus "global"]} {
	    grab -global $oldGrab
	} else {
	    grab $oldGrab
	}
    }
    return
}
#
proc LoadIfNames { ip comm } {
    set ::Interface::ifs  [list]
    set p .nodeProperties
    set localcursor [ $p cget -cursor ]
    $p configure -cursor watch
    blt::busy $p
    catch { eval  blt::bgexec ::Interface::snmp_exitFlag     \
	-output ::Interface::tmpSnmp -error Interface::errSnmp \
	$::snmpwalk_command $comm  $ip interfaces.ifTable.ifEntry.ifDescr 
    } rez
    blt::busy release $p
    $p configure -cursor $localcursor

    if { $rez !="" || [string match ::Interface::tmpSnmp "" ] } {
	puts stderr "LoadIfNames: $ip : $rez  $::Interface::errSnmp"
	tk_dialog $p.loadIfNames [msgcat::mc "SNMPget Error"] \
	    "Can't retriev SNMP data from \"$ip\":$rez $::Interface::errSnmp" \
	    error 0 [msgcat::mc "Ok" ]
	return false
    } else {
	foreach str [ split $::Interface::tmpSnmp "\n" ] {
	    if { [regexp -all -- {ifDescr\.[0-9]+ (.+)$} $str dummy ifname ] } {
		puts "$str===$ifname==="
		lappend ::Interface::ifs $ifname
	    }	
	}
    }
    #
    #  Interfaces GROUPS - see ifGroups.tcl
    #
    foreach g $::ifGroupList {
	lappend ::Interface::ifs "Group \"$g\""
    }
    
    return true
} 
#
proc EditInterface_OkButton { e } {    
    if { [string equal [$e.bname cget -state ] normal] } {
	set tmp_name [$e.ename get]
	if { $tmp_name == "" }  {
	    tk_dialog $e.error [msgcat::mc "Error"] \
		[msgcat::mc "Select an interface."] \
		error 0 [msgcat::mc "Ok" ]			
	    focus $e.bname    
	    return
	}
	set tmp_lst [ DLfirstParameterList $::Interface::iflist ]
	if { [lsearch $tmp_lst $tmp_name] >=0  } {
	    tk_dialog $e.error [msgcat::mc "Error"] \
		[msgcat::mc "Such an interface exist."] \
		error 0 [msgcat::mc "Ok"]			
	    focus $e.bname    
	    return	
	}    
    }
    set ::Interface::editIf_exitFlag 1
}
#
lappend ::GUI_pregenerate_list [list {EditIf_GUIconstructor} {Loading Interfaces dialog...}]
#