#		
#  Module Service.tcl
#
# $Log: Service.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 ::Service {
variable	editSrv_exitFlag	0 \
		nodeKey		"" \
		err_msg		{} \
		srvlist		[list] 
array set 	mArray		[list]
array set 	vArray		[list]
}
#             
proc EditSrv_GUIconstructor {  } {
    set w .nodeProperties.editSrv
    set e $w.entryFrame
    set m $w.methodsFrame
    set mg $m.grid
    set mb $m.buttons
    set v $w.variablesFrame
    set vg $v.grid
    set vb $v.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 "Service name:"]
    entry  $e.ename -width 48 \
	-validatecommand {  return [EditSrv_validateName [%W get] ] } \
	-invalidcommand  "EditSrv_invalidName %W"
    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 configure  $e.lname $e.ename  -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    
#
    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 "Service 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 ::Service::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 ::Service::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 42
    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.bsadd  -text [msgcat::mc "New Save"]   -command "LinkMethod $w $m.tbl Save add"
    button	$mb.bpadd  -text [msgcat::mc "New Poll"]   -command "LinkMethod $w $m.tbl Method 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.bsadd $mb.bpadd $mb.bedit $mb.bdel -padx 5 -pady 5 -side left 
#	
    frame $v -borderwidth 1 -relief sunken
    pack $v -side top -padx 5 -pady 5 -expand on -fill both -anchor s
    label $v.lb -text [msgcat::mc "Service SNMP variables"] -font $::headerFont
    grid $v.lb -in $v -column 0 -row 0 -sticky w
    grid rowconfig $v 1 -weight 1 -minsize 0
    grid columnconfig $v 0 -weight 1 -minsize 0
    frame $vg
    grid $vg -in $v -column 0 -row 1 -sticky news
    grid configure $vg -padx 5 -pady 5

    array set ::Service::vArray [list {0,0} [msgcat::mc "Name"] {0,1} [msgcat::mc "OID"] {0,2} [msgcat::mc "Value"] ]
    
    grid rowconfig $vg 0 -weight 1 -minsize 0
    grid columnconfig $vg 0 -weight 1 -minsize 0
#
	table $v.tbl -rows 1  -cols 3 \
	    -variable ::Service::vArray \
	    -titlerows 1 -titlecols 0 \
	    -yscrollcommand "$v.sy set "  \
	    -xscrollcommand "$v.sx set "  \
	    -roworigin 0 -colorigin 0 \
	    -maxwidth 400 -maxheight 100 \
	    -selectmode browse \
	    -flashmode 0 \
	    -background #f700f700f700 \
	    -borderwidth 2 \
	    -selecttype row	\
	    -state disabled  \
	    -resizeborders both \
	    -anchor w
    scrollbar $v.sy -command "$v.tbl yview"
    scrollbar $v.sx -orient horiz -command "$v.tbl xview"
    grid $v.tbl -in $vg \
	-row 0 -column 0 -rowspan 1 -columnspan 1 -sticky news
    grid $v.sy -in $vg -column 1 -row 0 \
	-columnspan 1 -rowspan 1 -sticky news
    grid $v.sx -in $vg -column 0 -row 1 \
	-columnspan 1 -rowspan 1 -sticky news
    $v.tbl tag config title -bg #0000bf00ff00 -relief groove
    $v.tbl tag config sel -fg #f700f700f700 -bg #48003d008b00 
    $v.tbl width 0 24 1 32 2 16
    bind $v.tbl <Double-1> "LinkVariable $v.tbl edit"
#
    frame $vb	
    grid $vb -in $v -column 0 -row 2 \
	-columnspan 1 -rowspan 1 -sticky n
    button	$vb.badd  -text [msgcat::mc "Add"]     -command "LinkVariable  $v.tbl add"
    button	$vb.bedit  -text [msgcat::mc "Edit"]   -command "LinkVariable  $v.tbl edit"
    button	$vb.bdel   -text [msgcat::mc "Delete"] -command "LinkVariable  $v.tbl delete"
    pack $vb.badd $vb.bedit $vb.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 "EditSrv_OkButton $e"
    button $b.cancel -text [msgcat::mc "Cancel"] -command "set ::Service::editSrv_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
    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 ::Service::editSrv_exitFlag 2"
    bind $w <Escape>  "set ::Service::editSrv_exitFlag 2"
}
#
proc EditSrv_getGUIgeometry { } {
    set w .nodeProperties.editSrv
    set e $w.entryFrame
    set m $w.methodsFrame
    set mg $m.grid
    set mb $m.buttons
    set v $w.variablesFrame
    set vg $v.grid
    set vb $v.buttons    
    set b $w.buttonFrame
    #
    if { [winfo exists $w] } {

    }
}
#
proc EditSrv { elbox mode } {
    set w .nodeProperties.editSrv
    set e $w.entryFrame
    set m $w.methodsFrame
    set mg $m.grid
    set mb $m.buttons
    set v $w.variablesFrame
    set vg $v.grid
    set vb $v.buttons    
    set b $w.buttonFrame
    #
    if { ![winfo exists $w] } {
	EditSrv_GUIconstructor
    }
    set srvind   [ $elbox curselection ]
    set srvname ""
    if { $srvind !="" } { set srvname  [ $elbox get  $srvind ]  }
    set srvkey   [ DLsecondByFirst $::Service::srvlist $srvname ]	
#
    switch $mode {
	delete { 
	    if { $srvname == "" } { return }
	    if { $srvkey != "" } {
	    	unset ::srv_methodslist($srvkey)
	    	unset ::srv_snmpvarslist($srvkey)
		unset ::srv_name($srvkey)
		unset ::srv_owner($srvkey)
		unset ::srv_descr($srvkey)
		unset ::srv_datadir($srvkey)
	    }
	    set ::Service::srvlist [ DLdeleteByFirst $::Service::srvlist $srvname ]
	    $elbox delete $srvind
	    return
	}
	edit { 
	    if { $srvname == "" } { return }
	    if { $srvkey == "" } { 
		set srvkey .srv$::srvIndex
	    	set ::srv_methodslist($srvkey) [list]
	    	set ::srv_snmpvarslist($srvkey) [list]
		set ::srv_name($srvkey)	$srvname
		set ::srv_owner($srvkey)	$::Service::nodeKey
		set ::srv_descr($srvkey)	""
		set ::srv_datadir($srvkey)  	""
		incr ::srvIndex		
	    }
	    $e.ename  configure -state normal	
	    $e.ename  insert 0 $::srv_name($srvkey)	
	    $e.ename  configure -validate none
	    $e.ename  configure -state disabled
	}
	add  { 
	    set srvkey .srv$::srvIndex
	    set ::srv_methodslist($srvkey) [list]
	    set ::srv_snmpvarslist($srvkey) [list]
	    set ::srv_name($srvkey)	""
	    set ::srv_owner($srvkey)	$::Service::nodeKey
	    set ::srv_descr($srvkey)	""
	    set ::srv_datadir($srvkey)	""
	    incr ::srvIndex		
	    $e.ename  configure -state normal
	    $e.ename  configure -validate focusout
	}
    }
    #
    # Load actiual data
    #
    $e.edescr insert 0 $::srv_descr($srvkey)
    $e.edir   insert 0 $::srv_datadir($srvkey)
    set idx 1
    foreach i $::srv_methodslist($srvkey) {
	set ::Service::mArray($idx,0) [lindex $i 0]
	set ::Service::mArray($idx,1) [lindex $i 1]
	set ::Service::mArray($idx,2) [lindex $i 2]
	incr idx
    }
    $m.tbl configure -rows $idx
    set idx 1
    foreach i $::srv_snmpvarslist($srvkey) {
	set ::Service::vArray($idx,0) [lindex $i 0]
	set ::Service::vArray($idx,1) [lindex $i 1]
	set ::Service::vArray($idx,2) [lindex $i 2]
	incr idx
    }
    $v.tbl configure -rows $idx
    #
    if { [string equal [wm state $w] withdrawn ] } {
        wm deiconify $w
    }
    raise $w
        
    set ::Service::editSrv_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 ::Service::editSrv_exitFlag

    if {[winfo exists $w]} {
	wm withdraw $w
	catch {focus $oldFocus}
	grab release $w
	if {$::Service::editSrv_exitFlag==1} {
	    if {[string equal $mode add ]} {
		set ::srv_name($srvkey)	[ $e.ename get ]	
		$elbox insert end $::srv_name($srvkey)
		lappend ::Service::srvlist [list $::srv_name($srvkey) $srvkey ]
	    }
	    set ::srv_descr($srvkey)	[ $e.edescr get ]	
	    set ::srv_datadir($srvkey)	[ $e.edir get ]	
	    #
	    set nmethods [ expr [array size ::Service::mArray] / 3 ]
	    set tmp_mlist [list]
	    for {set i 1} {$i <$nmethods} {incr i} {
		lappend tmp_mlist [list $::Service::mArray($i,0) $::Service::mArray($i,1) $::Service::mArray($i,2)]
	    }
	    set ::srv_methodslist($srvkey) $tmp_mlist	    
	    set nvars [ expr [array size ::Service::vArray] / 3 ]
	    set tmp_vlist [list]
	    for {set i 1} {$i <$nvars} {incr i} {
		lappend tmp_vlist [list $::Service::vArray($i,0) $::Service::vArray($i,1) $::Service::vArray($i,2) ]
	    }
	    set ::srv_snmpvarslist($srvkey) $tmp_vlist	    
	}
	#
	$e.ename  configure -state normal
	$e.ename configure -validate none 	
	$e.ename delete 0 end
	$e.edescr delete 0 end
	$e.edir delete 0 end
	$m.tbl configure -rows 1
	$v.tbl configure -rows 1
    }	
    array unset ::Service::mArray
    array set ::Service::mArray [list  {0,0} [msgcat::mc "Type"] {0,1} [msgcat::mc "Name"] {0,2} [msgcat::mc "Parameters"] ]
    array unset ::Service::vArray
    array set ::Service::vArray [list {0,0} [msgcat::mc "Name"] {0,1} [msgcat::mc "OID"] {0,2} [msgcat::mc "Value"] ]
    #
    if {[string compare $oldGrab ""] && [winfo exists $oldGrab] } {
	if {![string compare $grabStatus "global"]} {
	    grab -global $oldGrab
	} else {
	    grab $oldGrab
	}
    }
    return
}
#
proc EditSrv_validateName { name } {
    if { $name == "" } { return true }
    set names [ DLfirstParameterList $::Service::srvlist ]
    if { [lsearch $names $name ] >= 0 } { 
	set ::Service::err_msg [msgcat::mc "Such a service exist."]
	return false
    }
    return true 
}
#
proc EditSrv_invalidName { e } {
    selection clear
    tk_dialog $e.error [msgcat::mc "Error"] \
	$::Service::err_msg  error 0 [msgcat::mc "Ok"] 
    focus $e
}
#
proc EditSrv_OkButton { e } {
    if { [string equal [$e.ename cget -state ] normal ] } {
	set tmp_name [$e.ename get]
	if {$tmp_name =="" } {
	    selection clear
	    tk_dialog $e.error [msgcat::mc "Error"] \
		[msgcat::mc "Enter non-null service name."]  \
		error 0 [msgcat::mc "Ok"] 
	    focus $e
	    return
	}
	if { ![$e.ename validate ]  } { return }
    }
    set ::Service::editSrv_exitFlag 1
}
#
lappend ::GUI_pregenerate_list [list {EditSrv_GUIconstructor} {Loading Services dialog...}]
#