#
#  Module Sql.tcl          06.01.2003   vfom    
#
#  $Log: Sql.tcl,v $
#  Revision 1.2  2006/10/26 09:39:17  vfom
#  Added code for netstate server query
#
#
package require	msgcat
package require sql
#
namespace eval Sql {
variable 	dbmethod	"mysql" \
		dbhost		"" \
		dbuser		"" \
		dbpwd		"" \
		dbname		netmon \
		rc_file		".sqlrc" \
		askForSqlDatabase_exitFlag 0
		
}
#
#  Loading user preferences
#  
#if  [ info exists ::env(HOME)] { } else { set ::env(HOME) {} } 

#if {  ![ file exists [ file join $::env(HOME) ".Tknetmon"] ] } {
#    file mkdir [ file join $::env(HOME) ".Tknetmon" ]
#}

#if  [file readable [file join  $::env(HOME) ".Tknetmon" $::Sql::rc_file] ]  {
#	source [file join $::env(HOME) ".Tknetmon" $::Sql::rc_file ]  
#}
#
#
proc AskForSqlDatabase { } {
    set w .askForSqlDatabase

    if {[ winfo exists $w ]} then  {
	raise $w ;
	return
    }
    toplevel $w -class Dialog
    wm transient $w [winfo toplevel [winfo parent $w]]
    wm title $w [msgcat::mc "SQL Database parameters" ]


    set we $w.entryFrame
    frame $we -relief sunken -borderwidth 2 
    pack $we -side top -ipadx 10 -ipady 10 -padx 3 -pady 3
#    grid $we -in $w \
#	-row 0 -column 0 -rowspan 1 -columnspan 1 -sticky news
    set index 0

    label $we.lmethod -text [msgcat::mc "SQL method:" ]
    grid $we.lmethod -in $we -row $index -column 0 -rowspan 1 -columnspan 1 
    entry $we.emethod -width 16 
    grid $we.emethod -in $we -row $index -column 1 -rowspan 1 -columnspan 1
    incr index

    $we.emethod delete 0 end
    $we.emethod insert 0 $::Sql::dbmethod

    label $we.lhost -text [msgcat::mc "Host:" ]
    grid $we.lhost -in $we -row $index -column 0 -rowspan 1 -columnspan 1 
    entry $we.ehost -width 16 
    grid $we.ehost -in $we -row $index -column 1 -rowspan 1 -columnspan 1
    incr index
    
    $we.ehost delete 0 end
    $we.ehost insert 0 $::Sql::dbhost
    
    label $we.luser -text [msgcat::mc "Username:" ]
    grid $we.luser -in $we -row $index -column 0 -rowspan 1 -columnspan 1 
    entry $we.euser -width 16 
    grid $we.euser -in $we -row $index -column 1 -rowspan 1 -columnspan 1 
    incr index
    
    $we.euser delete 0 end
    $we.euser insert 0 $::Sql::dbuser

    label $we.lpwd -text [msgcat::mc "Password:" ]
    grid $we.lpwd -in $we -row $index -column 0 -rowspan 1 -columnspan 1 
    entry $we.epwd -width 16  -show off
    grid $we.epwd -in $we -row $index -column 1 -rowspan 1 -columnspan 1 
    incr index

    $we.epwd delete 0 end
    $we.epwd insert 0 $::Sql::dbpwd

    label $we.ldbname -text [msgcat::mc "Database:" ]
    grid $we.ldbname -in $we -row $index -column 0 -rowspan 1 -columnspan 1 
    entry $we.edbname -width 16 
    grid $we.edbname -in $we -row $index -column 1 -rowspan 1 -columnspan 1 
    $we.edbname delete 0 end
    $we.edbname insert 0 $::Sql::dbname
             	
    set wb $w.buttonFrame
    frame $wb
    pack $wb -side bottom
    button $wb.ok -text [msgcat::mc "Ok" ] -command {
	set ::Sql::askForSqlDatabase_exitFlag 1
    }
    button $wb.cancel -text [msgcat::mc "Cancel" ] -command "set ::Sql::askForSqlDatabase_exitFlag 2"
    pack $wb.cancel $wb.ok -side right -padx 10 -pady 10
    bind $w <Return> "$wb.ok invoke"
    focus $we.ehost
    
    wm withdraw $w
    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 geometry  $w  +$x+$y
    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 $wb.ok

    tkwait variable ::Sql::askForSqlDatabase_exitFlag

    catch {focus $oldFocus}
    grab release $w

    if { $::Sql::askForSqlDatabase_exitFlag == 1 } {    
	set ::Sql::dbmethod [ string trim [ $we.emethod get ] ]
	set ::Sql::dbhost [ string trim [ $we.ehost get ] ]
	set ::Sql::dbuser [ string trim [ $we.euser get ] ]
	set ::Sql::dbpwd  [ string trim [ $we.epwd get ] ]
	set ::Sql::dbname [ string trim [ $we.edbname get ] ]
    }

    destroy $w
    if {[string compare $oldGrab ""]} {
	if {![string compare $grabStatus "global"]} {
	    grab -global $oldGrab
	} else {
	    grab $oldGrab
	}
    }
    return  
}
#
proc SqlSaveSettings { } {
    #
#    if  [ info exists ::env(HOME)] { } else { set ::env(HOME) {} } 
#    if { [ catch { open [file join $::env(HOME) ".Tknetmon" $::Sql::rc_file ] w } fd ] } {
#	error "$fd"
#	return
#    }
#    set t [clock format [clock seconds]]
#    set u $::tcl_platform(user)
#    if { $::tcl_platform(platform) =="unix" } {
#        set uname [open "|uname -mnrs" r]
#        set h [read -nonewline $uname] ; close $uname
#    } else {
#	set h "localhost"
#    }
#    puts $fd "#\n#  This is user default preferences for $::appName Sql\n#"
#    puts $fd "#  Created by $u at $t with $::appName-$::appVersion"
#    puts $fd "#          on $h\n#"
#    # Sql settings
#    puts $fd "set ::Sql::dbmethod	\"$::Sql::dbmethod\""
#    puts $fd "set ::Sql::dbhost		\"$::Sql::dbhost\""
#    puts $fd "set ::Sql::dbuser		\"$::Sql::dbuser\""
#    puts $fd "set ::Sql::dbpwd		\"$::Sql::dbpwd\""
#    puts $fd "set ::Sql::dbname		\"$::Sql::dbname\""
#    #
#    close $fd
}
#