#
#   Module Variables.tcl
#
#   $Log: Variables.tcl,v $
#   Revision 1.2  2006/10/26 09:39:17  vfom
#   Added code for netstate server query
#
#   Revision 1.1.1.1  2006/09/21 10:57:02  vfom
#   Loading TkNetmon to CVS
#
#
namespace eval ::Variables {
variable	linkVar_exitFlag	0 \
		viewVars_exitFlag	0 \
		editVar_exitFlag	0 \
		varlist		[list]	\
		editMode		"" \
		err_msg		{}
array set vArray	[list]
}
#
proc EditVariable_GUIconstructor { } {
    set w .editVar
    set e $w.entryFrame
    set b $w.buttonsFrame

    toplevel $w  -class Dialog
    wm title $w [msgcat::mc "Edit Variables"]
    wm withdraw $w

    frame $e -relief sunken -border 2
    pack $e -side top -fill both -expand on -ipadx 10 -ipady 10 -padx 5 -pady 5
    
    label $e.lname -text [msgcat::mc "Variable Name:"]
    grid $e.lname -in $e -row 0 -column 0 -rowspan 1 -columnspan 1 -sticky e
    entry $e.ename -width 48
    grid $e.ename -in $e -row 0 -column 1 -rowspan 1 -columnspan 1 -sticky w

    label $e.loid -text [msgcat::mc "SNMP OID:"]
    grid $e.loid -in $e -row 1 -column 0 -rowspan 1 -columnspan 1 -sticky e
    entry $e.eoid -width 48
    grid $e.eoid -in $e -row 1 -column 1 -rowspan 1 -columnspan 1 -sticky w

    frame $b 
    pack $b -side bottom -padx 5 -pady 5 -anchor s
    button $b.ok -text [msgcat::mc "Ok"]         -command {
	set w .editVar
	set e $w.entryFrame
	set b $w.buttonsFrame
	set tmp_nm [string trim [$e.ename get]]
	if {$tmp_nm == "" } {
	    tk_dialog $w.error [msgcat::mc "Error"] \
		[msgcat::mc "Enter non-empty variable name."] \
		error 0 [msgcat::mc "Ok"]
		focus $e.ename
	    return
	}	
	regsub -all -- {[a-zA-Z0-9_]+} $tmp_nm {} rez
	if {$rez != ""} {
	    tk_dialog $w.error [msgcat::mc "Error"] \
		[msgcat::mc "Invalid character in Variable name."] \
		error 0 [msgcat::mc "Ok"]
	    focus $e.ename
	    return
	}
	if {[string equal $::Variables::editMode "add" ] } {
	    set nvars [expr [array size ::Variables::vArray]/2 ]
	    for {set i 1} { $i<$nvars} { incr i } {
		if { [ string equal $tmp_nm $::Variables::vArray($i,0) ]  } {
		    tk_dialog $w.error [msgcat::mc "Error"] \
			[msgcat::mc "Such a name exists."] \
			error 0 [msgcat::mc "Ok"]
		    focus $e.ename
		    return
		}		
	    }
	}
	set tmp_oid [string trim [$e.eoid get]]
	if {$tmp_oid == "" } {
	    tk_dialog $w.error [msgcat::mc "Error"] \
		[msgcat::mc "Enter non-empty OID."] \
		error 0 [msgcat::mc "Ok"]
	    focus $e.eoid
	    return
	}	
	if { ![ IsValidOID $tmp_oid ] } {
	    tk_dialog $w.error [msgcat::mc "Error"] \
		[msgcat::mc "Enter valid OID.(Example - 1.3.6.1.4.0) "] \
		error 0 [msgcat::mc "Ok"]		
	    focus $e.eoid
	    return
	}
    
	set ::Variables::editVar_exitFlag 1
    }
    button $b.cancel -text [msgcat::mc "Cancel"] -command "set ::Variables::editVar_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]]}]
    wm geom $w +$x+$y
        
    bind $w <Destroy> "set ::Variables::editVar_exitFlag 2"
    bind $w <Escape>  "set ::Variables::editVar_exitFlag 2"    
}
#
proc EditVariable { tbl mode } {
    set w .editVar
    set e $w.entryFrame
    set b $w.buttonsFrame
    set ::Variables::editMode $mode
    if {[ string equal $mode "delete" ] } {
	set idxs [ $tbl tag row sel ] 
	if { $idxs=="" } { return }
	array set tmpArr [ list ] 
	set idx 0
	set nvars [expr [array size ::Variables::vArray]/2 ]
	for {set i 0} { $i<$nvars} { incr i } {
	    if { [ lsearch $idxs $i ] < 0 } {
		set tmpArr($idx,0) $::Variables::vArray($i,0)
		set tmpArr($idx,1) $::Variables::vArray($i,1)
		incr idx
	    }
	}
	array unset ::Variables::vArray
	array set ::Variables::vArray [ array get tmpArr ] 
	$tbl configure -rows $idx
	return
    }
    if { ![winfo exists $w] } {
	EditVariable_GUIconstructor
    }    
#   
    if {[ string equal $mode "edit" ] } {
	set idx [ lindex [ $tbl tag row sel ]  0 ]
	if { $idx == "" } { return }
	$e.ename insert 0 $::Variables::vArray($idx,0)
	$e.eoid insert 0 $::Variables::vArray($idx,1)
    } else {
	set idx [expr [array size ::Variables::vArray]/2 ]
    }
#
    set ::Variables::editVar_exitFlag 0
    update idletasks
    wm deiconify $w
    raise $w
    set oldFocus [focus]
    set oldGrab [grab current $w]
    if { [string compare $oldGrab ""] } {
	set grabStatus [grab status $oldGrab]
    }
    grab $w 
    focus $b.ok
    
    tkwait variable ::Variables::editVar_exitFlag

    if {[winfo exists $w]} {
	wm withdraw $w
	wm withdraw $w
	catch {focus $oldFocus}
	grab release $w
	if {$::Variables::editVar_exitFlag==1} {
	    # Ok button pressed
	    set ::Variables::vArray($idx,0) [string trim [$e.ename get ] ]
	    set ::Variables::vArray($idx,1) [string trim [$e.eoid get ] ]
	    if { [string equal $mode "add" ] } {
		$tbl configure -rows [expr $idx + 1 ]	
	    }
	}
	$e.ename delete 0  end
	$e.eoid  delete 0  end
    }
    # cleanup entries
    #
    if {[string compare $oldGrab ""]} {
	if {![string compare $grabStatus "global"]} {
	    grab -global $oldGrab
	} else {
	    grab $oldGrab
	}
    }
    return        
}
#
proc ViewVariables_GUIconstructor { }  {
    set w .viewVar
    set e $w.subFrame
    set t $e.tFrame
    set d $e.dFrame
    set b $w.bFrame
    
    toplevel $w 
    wm title $w [msgcat::mc "Edit Variables"]
    wm withdraw $w

    frame $e -relief sunken -border 2
    pack $e -side top -fill both -expand on -padx 5 -pady 5
    
    frame $t
    pack $t -side top -fill both -expand on -padx 5 -pady 5
    
    grid rowconfig	$t 0 -weight 1 -minsize 0
    grid columnconfig   $t 0 -weight 1 -minsize 0

    scrollbar $t.sy -command " $t.tbl yview "
    scrollbar $t.sx -orient horiz -command " $t.tbl xview "

    array set ::Variables::vArray [ list {0,0} [msgcat::mc "Name"] {0,1} [msgcat::mc "SNMP OID"] ]

    table $t.tbl -rows 1 -cols 2 \
	-variable ::Variables::vArray \
	-titlerows 1 -titlecols 0 \
	-width 600 -height 800 \
	-yscrollcommand " $t.sy set "  \
	-xscrollcommand " $t.sx set "  \
	-maxwidth 300 -maxheight 400  \
	-roworigin 0 -colorigin 0 \
	-selectmode extended \
	-selecttype row \
	-rowstretch none -colstretch last \
	-drawmode single \
	-resizeborders both \
	-flashmode 0 \
	-state disabled \
	-borderwidth 1 \
	-anchor nw

    grid $t.tbl -in $t \
	-row 0 -column 0 -rowspan 1 -columnspan 1 -sticky news
    grid $t.sy -in $t -column 1 -row 0 \
	-columnspan 1 -rowspan 1 -sticky news
    grid $t.sx -in $t -column 0 -row 1 \
	-columnspan 1 -rowspan 1 -sticky news

    $t.tbl tag config title -bg #0000bf00ff00 -relief groove
    $t.tbl tag config sel -fg #f700f700f700 -bg #48003d008b00 
    $t.tbl width   0  32 
    bind $t.tbl <Double-1> "EditVariable $t.tbl edit"
    
    frame $d
    pack $d -side bottom -padx 5 -pady 5 -anchor s

    button $d.add -text  [msgcat::mc "Add"]    -command "EditVariable $t.tbl add"
    button $d.edit -text [msgcat::mc "Edit"]   -command "EditVariable $t.tbl edit"
    button $d.del -text  [msgcat::mc "Delete"] -command "EditVariable $t.tbl delete"
    pack $d.add $d.edit $d.del -side left -padx 5
            
    frame $b
    pack $b -side bottom -padx 5 -pady 5 -expand off -anchor s
    button $b.ok -text [msgcat::mc "Ok"]         -command "set ::Variables::viewVars_exitFlag 1"
    button $b.cancel -text [msgcat::mc "Cancel"] -command "set ::Variables::viewVars_exitFlag 2"
    pack $b.ok $b.cancel -side left -padx 5 -pady 5
#
    update idletasks
    set x [expr {[winfo screenwidth $w]/2 - 400  \
	- [winfo vrootx [winfo parent $w]]}]
    set y [expr {[winfo screenheight $w]/2 - 300 \
	- [winfo vrooty [winfo parent $w]]}]
    wm geom $w [format "%sx%s+%s+%s" 800 600 $x $y]
    
    
    bind $w <Destroy> "set ::Variables::viewVars_exitFlag 2"
    bind $w <Escape>  "set ::Variables::viewVars_exitFlag 2"
    
}
#
proc ViewVariables { } {
    set w .viewVar
    set e $w.subFrame
    set t $e.tFrame
    set d $e.dFrame
    set b $w.bFrame
    if { ![winfo exists $w] } {
	ViewVariables_GUIconstructor
    }
#
    set idx 1
    foreach sublist $::snmpVarsList {
	set ::Variables::vArray($idx,0) [ lindex $sublist 0 ] 
	set ::Variables::vArray($idx,1) [ lindex $sublist 1 ] 
	incr idx
    }
    $t.tbl configure -rows $idx
#
    set ::Variables::viewVars_exitFlag 0
    update idletasks
    wm deiconify $w
    raise $w
    set oldFocus [focus]
    set oldGrab [grab current $w]
    if { [string compare $oldGrab ""] } {
	set grabStatus [grab status $oldGrab]
    }
    grab $w 
    focus $b.ok
    
    tkwait variable ::Variables::viewVars_exitFlag

    if {[winfo exists $w]} {
	wm withdraw $w
	wm withdraw $w
	catch {focus $oldFocus}
	grab release $w
	if {$::Variables::viewVars_exitFlag==1} {
	    # Ok button pressed
	    set nvars [ expr [array size ::Variables::vArray] / 2 ]
	    set tmp_vlist [list]
	    for {set i 1} {$i <$nvars} {incr i} {
		lappend tmp_vlist [list $::Variables::vArray($i,0) $::Variables::vArray($i,1) ]
		}
	    set ::snmpVarsList $tmp_vlist
	    $t.tbl configure -rows 1
	}
    }
    # cleanup entries
    array set ::Variables::vArray [ list {0,0} [msgcat::mc "Name"] {0,1} [msgcat::mc "SNMP OID"] ]    
    if {[string compare $oldGrab ""]} {
	if {![string compare $grabStatus "global"]} {
	    grab -global $oldGrab
	} else {
	    grab $oldGrab
	}
    }
    return    
}
#
proc LinkVariable_GUIconstructor {  }  {
    set w .linkVar
    set e $w.entryFrame
    set b $w.buttonFrame
#
    toplevel $w -class Dialog
    wm withdraw $w
#
    frame $e
    pack $e -side top -padx 5 -pady 5 -expand on -fill both
    set curr_row 0
    label  $e.lname -text [msgcat::mc "Variable name:"] 
    entry  $e.ename -width 48
    grid $e.lname -in $e -row $curr_row -column 0 -sticky e
    grid $e.ename -in $e -row $curr_row -column 1 -sticky w
    incr curr_row
#
    label  $e.loid -text [msgcat::mc "OID:"]
    entry  $e.eoid -width 48
    grid $e.loid -in $e -row $curr_row -column 0 -sticky e
    grid $e.eoid -in $e -row $curr_row -column 1 -sticky w
    incr curr_row
#
    label  $e.lval -text [msgcat::mc "Value:"]
    entry  $e.eval -width 54 
    grid $e.lval -in $e -row $curr_row -column 0 -sticky e
    grid $e.eval -in $e -row $curr_row -column 1 -sticky w
    incr curr_row
#	
    frame $b
    pack $b -side bottom -padx 5 -pady 5 -expand off -anchor s
    button $b.ok -text [msgcat::mc "Ok"] -command {
	set w .linkVar
	set e $w.entryFrame
	set b $w.buttonFrame
	set tmp_nm  [string trim [$e.ename get]]
	if { $tmp_nm=="" } {
	    tk_dialog $w.error [msgcat::mc "Error"] \
		[msgcat::mc "Enter non-null Variable name."] \
		error 0 [msgcat::mc "Ok"]
	    focus $e.ename
	    return
        }
	regsub -all -- {[a-zA-Z0-9_]+} $tmp_nm {} rez
	if {$rez != ""} {
	    tk_dialog $w.error [msgcat::mc "Error"]  \
		[msgcat::mc "Invalid character in Variable name."] \
	        error 0 [msgcat::mc "Ok"]
	    focus $e.ename
	    return
	}	
	if { [lsearch $::Variables::varlist $tmp_nm] >= 0 } { 
	    tk_dialog $w.error [msgcat::mc "Error"]  \
		[msgcat::mc "This name was defined early."] \
		error 0 [msgcat::mc "Ok"]
	    focus $e.ename
	    return
	}
	set tmp_oid [string trim [$e.eoid get] ]
	if { $tmp_oid != "" } {
	    if { ![IsValidOID $tmp_oid] } {
		tk_dialog $w.error [msgcat::mc "Error"]  \
		    [msgcat::mc "Invalid OID syntax."] \
		    error 0 [msgcat::mc "Ok"]
		focus $e.eoid
		return
	    } 
	}
	set ::Variables::linkVar_exitFlag 1    
    }
    button $b.cancel -text [msgcat::mc "Cancel"] -command "set ::Variables::linkVar_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]]}]
    wm geom $w +$x+$y
    #	
    bind $w <Destroy> "set ::Variables::linkVar_exitFlag 2"
    bind $w <Escape>  "set ::Variables::linkVar_exitFlag 2"    
}
#
proc LinkVariable { tbl mode }  {
    set w .linkVar
    set e $w.entryFrame
    set b $w.buttonFrame
    #
    if { ![winfo exists $w] } {
	LinkVariable_GUIconstructor 
    }
#    
    set xtArr [ $tbl cget -variable ]
    upvar $xtArr tArr
    set idx  [ $tbl tag row sel ]
#    
    switch $mode {
	add {
	    wm title $w [msgcat::mc "SNMP variable::Add"]
	    $e.ename configure -state normal
	    set ::Variables::varlist [array get tArr "*,0" ]
	}
	edit {
	    if { $idx == "" } { return }  else {
		wm title $w [msgcat::mc "SNMP variable::Edit"]	     
		$e.ename configure -state normal
		$e.ename insert 0 $tArr($idx,0)	    
		$e.ename configure -state disabled
		$e.eoid  insert 0 $tArr($idx,1)	    
		$e.eval  insert 0 $tArr($idx,2)	    
	    }
	}
	delete  {
	    if { $idx == "" } { return }  else {
		set ncols [$tbl cget -cols ]
		set nrows [expr [ array size tArr  ] / $ncols]
		set prev_idx $idx
		set next_idx [expr $prev_idx+1]
		while { $next_idx < $nrows }  {
		    for { set j 0} {$j<$ncols} {incr j} { 
			set tArr($prev_idx,$j) $tArr($next_idx,$j) 
		    }
		    set prev_idx $next_idx
		    set next_idx [expr $prev_idx+1]
		}		    
		for { set j 0} {$j<$ncols} {incr j} { 
		    unset tArr($prev_idx,$j) 
		}
		$tbl configure -rows [expr $nrows -1]
		return
	    }
	}
    }
        
    set ::Variables::linkVar_exitFlag 0
    update idletasks
    wm deiconify $w
    raise $w

    set oldFocus [focus]
    set oldGrab [grab current $w]
    if { [string compare $oldGrab ""] } {
	set grabStatus [grab status $oldGrab]
    }
    grab $w 
    focus $b.ok
    
    tkwait variable ::Variables::linkVar_exitFlag

    if {[winfo exists $w]} {
	wm withdraw $w
	catch {focus $oldFocus}
	grab release $w
	if { $::Variables::linkVar_exitFlag==1 } {
	    # Ok button pressed	    
	    switch $mode {
		add {
		    set idx [expr [ array size tArr  ] / [$tbl cget -cols ]]		    
		    set tArr($idx,0) [string trim [$e.ename get] ]
		    set tArr($idx,1) [string trim [$e.eoid  get] ]
		    set tArr($idx,2) [string trim [$e.eval  get] ]
		    $tbl configure -rows [expr $idx + 1 ]
		} 
		edit { 	
		    set tArr($idx,1) [string trim [$e.eoid  get] ]
		    set tArr($idx,2) [string trim [$e.eval  get] ]
		}
	    }
	}
    # cleanup entries
    $e.ename configure -state normal
    $e.ename delete  0 end
    $e.eoid delete  0 end
    $e.eval delete  0 end
    set ::Variables::varlist [list]
    }
    if {[string compare $oldGrab ""] && [winfo exists $oldGrab] } {
	if {![string compare $grabStatus "global"]} {
	    grab -global $oldGrab
	} else {
	    grab $oldGrab
	}
    }
    return 
}
#
proc IsValidOID { oid } {
    regsub -all -- {[0-9]+\.} $oid {} rez1
    if { [string equal $rez1 $oid ] } { 
	return false 
    }    
    if { [string equal $rez1 "" ] } { 
	return false 
    }    
    regsub -all -- {[0-9]+} $rez1 {} rez	
    if { ![string equal $rez "" ] } { 
	return false 
    }
    return true
}
#
lappend ::GUI_pregenerate_list [list { ViewVariables_GUIconstructor } {Loading Variables dialog...}]
lappend ::GUI_pregenerate_list [list { EditVariable_GUIconstructor } {Loading Variables dialog...}]
#