###########################################################################
#                                                                         #
#               Copyright (C) 1995, Silicon Graphics, Inc.                #
#                                                                         #
#   These coded instructions, statements, and computer programs  contain  #
#   unpublished  proprietary  information of Silicon Graphics, Inc., and  #
#   are protected by Federal copyright law.  They  may  not be disclosed  #
#   to  third  parties  or copied or duplicated in any form, in whole or  #
#   in part, without the prior written consent of Silicon Graphics, Inc.  #
#                                                                         #
###########################################################################

#%COMMENT_BEGIN
# Filename:	dkSrchDlg
# Version:	$Revision: 1.8 $
# Synopsis:	Encapsulates the dialog that allows the user to search for
#		disks by host name.
# Functions:	dkSrch:realize
#		dkSrch:manage
#		dkSrch:fill
#		dkSrch:setData
#		dkSrch:getHost
#		dkSrch:_create
#		dkSrch:_formQuery
#		dkSrch:_dialogCb
#		dkSrch:_accept
#		dkSrch:_cancel
#
#%COMMENT_END

#########################################
#		Public			#
#########################################
#%COMMENT_BEGIN
# Function:	dkSrch:realize
# Synopsis:	Initializes any class-wide data and creates an instance of the
#		dialog.  If an instance of this dialog already exists for the
#		given handle, no action is taken.
# Arguments:	- handle	An identifier that is used as a key for storing
#				(and later retrieving) the created widgets and
#				any instance specific data.
#		- parent	The parent for the created dialog.
#
#%COMMENT_END
proc dkSrch:realize { handle parent panel } \
{
	global		_GW_srch _GD_srch

	if {! [info exists _GW_srch(initialized)]} {
		set _GW_srch(initialized)	true
		set _GD_srch(class)		DISK
		set _GD_srch(combos)		XFS_HOST
		set _GD_srch(texts)		NAME
		set _GD_srch(buttons)		{accept apply cancel help}
	}

	if {! [info exists _GW_srch($handle,dialog)]} {
		set _GD_srch($handle,hip) " "
		set _GD_srch($handle,hfn) " "
		set _GD_srch($handle,search,hip) " "
		set _GD_srch($handle,search,hfn) " "
		set _GD_srch($handle,needsort) false
		set _GD_srch($handle,FLUSH_CACHE) false

		set _GW_srch($handle,dialog) [dkSrch:_create $handle $parent]
		$_GW_srch($handle,dialog) realizeWidget

		####	This must happen after the dialog is realized
		$_GW_srch($handle,dialog) getValues -height height -width width
		.$_GW_srch($handle,dialog) setValues \
				-minWidth $width \
				-minHeight $height
		$_GW_srch($handle,buttons) getValues -height height
		$_GW_srch($handle,buttons) setValues \
				-paneMinimum $height \
				-paneMaximum $height
	}

	set _GD_srch(iconpanel) $panel
}

#%COMMENT_BEGIN
# Function:	dkSrch:manage
# Synopsis:	Manages an instance of the dialog and sets the "Flush Cache"
#		option to its default value of false.
# Arguments:	- handle	The identifier for the desired instance.
#
#%COMMENT_END
proc dkSrch:manage { handle } \
{
	global	_GW_srch

	if {[info exists _GW_srch($handle,dialog)]} {
		$_GW_srch($handle,dialog) manageChild
		$_GW_srch($handle,XFS_HOST) processTraversal current
		$_GW_srch($handle,FLUSH_CACHE) setValues -set false
		set _GD_srch($handle,FLUSH_CACHE) false
	}
}

#%COMMENT_BEGIN
# Function:	dkSrch:fill
# Synopsis:	If a host name is passed in, stores it as the reference host.
#		Gets the list of available hosts from the reference host and
#		calls dkSrch:setData() to fill the dialog.
# Arguments:	- handle	The identifier for the desired instance.
#		- host		Specifies the host to use when getting the
#				list of available host names.
#
#%COMMENT_END
proc dkSrch:fill { handle host } \
{
	global	_GD_srch _GD_resources

	if {$host == ""} {
		set host $_GD_srch($handle,hfn)
	} elseif {[hu:getIpAddress $host hfn hpd hip $_GD_resources(hostsFile)]} {
		set _GD_srch($handle,hip) $hip
		set _GD_srch($handle,hfn) $hfn
		set h $hip
	} else {
		set _GD_srch($handle,hip) ""
		set _GD_srch($handle,hfn) $host
		set h $hfn
	}

	set data [hu:getHosts $h etc $_GD_resources(hostsFile)]
	dkSrch:setData $handle $_GD_srch($handle,hfn) $data
	if {[cequal $data ""]} {
		return 0
	} else {
		return 1
	}
}

#%COMMENT_BEGIN
# Function:	dkSrch:setData
# Synopsis:	Sets the items for the scrolled list to the given list of
#		host names.
# Arguments:	- handle	The identifier for the desired instance.
#		- host		The item to select in the list.
#		- lst		The list of host names.
#
#%COMMENT_END
proc dkSrch:setData { handle host lst } \
{
	global	_GW_srch _GD_srch

	set _GD_srch($handle,hostlist) $lst
	combo:setItems $_GW_srch($handle,XFS_HOST) $lst
	combo:selectItem $_GW_srch($handle,XFS_HOST) $host
}

#########################################
#	Public: Data Out Routines	#
#########################################
#%COMMENT_BEGIN
# Function:	dkSrch:getHost
# Synopsis:	Returns the currently selected host name.
# Arguments:	- handle	The identifier for the desired instance.
#
#%COMMENT_END
proc dkSrch:getHost { handle } \
{
	global	_GD_srch; return $_GD_srch($handle,hfn)
}


#########################################
#		Private			#
#########################################
#%COMMENT_BEGIN
# Function:	dkSrch:_create
# Synopsis:	Creates an instance of the dialog.
# Arguments:	- handle	The identifier for the new instance.
#		- parent	The parent for the created dialog.
#
#%COMMENT_END
proc dkSrch:_create { handle parent } \
{
	global		_GW_srch _GD_srch
	set name	dkSrch

	set dialog [xmFormDialog $parent.$name]
	set pane [xmPanedWindow $dialog.$handle \
			-sashWidth 1 \
			-sashHeight 1 \
			-topAttachment attach_form \
			-leftAttachment attach_form \
			-rightAttachment attach_form \
			-bottomAttachment attach_none]
	set grid [sgiGrid $pane.grid -numRows 4 -numColumns 2 \
			-defaultSpacing 2]

	set item XFS_HOST; set row 0
	xmLabel $grid.$item-label managed -row $row -column 0
	set _GW_srch($handle,$item) [dtDropDownComboBox $grid.$item managed \
			-row 0 -column 1 \
			-columns 28]

	set item NAME; set row 1
	xmLabel $grid.$item-label managed -row $row -column 0
	set _GW_srch($handle,$item) [xmTextField $grid.$item managed \
			-row 1 -column 1]
	
	set item FLUSH_CACHE; set row 3
	set _GW_srch($handle,$item) [xmToggleButton $grid.$item managed \
			-row $row -column 1 \
			-resizeHorizontal false]
	$_GW_srch($handle,$item) valueChangedCallback \
			"set _GD_srch($handle,$item) %set"
			
	
	set _GW_srch($handle,buttons) \
		[xfs:createDialogButtons $pane $_GD_srch(buttons) 15]
	foreach i $_GD_srch(buttons) {
		$_GW_srch($handle,buttons).$i activateCallback \
				"dkSrch:_dialogCb $handle $i"
	}

	$grid columnResizable 0 false
	$grid manageChild
	$_GW_srch($handle,buttons) manageChild
	$pane manageChild

	$grid rowMargin 2 8

	return $dialog
}

#%COMMENT_BEGIN
# Function:	dkSrch:_formQuery
# Synopsis:	Formulates a query to pass to the server to find the disk
#		objects on a host.
# Arguments:	- handle	The identifier for the desired instance.
#
#%COMMENT_END
proc dkSrch:_formQuery { handle } \
{
	global	_GD_srch

	lappend data "HOST_PATTERN:$_GD_srch($handle,hfn)"
	lappend data "OBJ_TYPE:$_GD_srch(class)"
	lappend data "OBJ_PATTERN:\*"
	return $data
}

#%COMMENT_BEGIN
# Function:	dkSrch:_dialogCb
# Synopsis:	The callback function defined on the dialog buttons.
# Arguments:	- handle	The identifier for the desired instance.
#		- op		Indicates which button the user selected.
#
#%COMMENT_END
proc dkSrch:_dialogCb { handle op } \
{
	global	_GW_srch

	switch $op {
		accept	{ dkSrch:_accept $handle
			  dkSrch:_cancel $handle
			}
		apply	{ dkSrch:_accept $handle }
		cancel	{ dkSrch:_cancel $handle }
		help	{ sgiHelpMsg $_GW_srch($handle,dialog) }
	}
}

#%COMMENT_BEGIN
# Function:	dkSrch:_accept
# Synopsis:	This is called when the user presses the "Accept" dialog
#		button.  It gets the data from the dialog and then calls
#		the server to get the objects from the given host.
# Arguments:	- handle	The identifier for the desired instance.
#
#%COMMENT_END
proc dkSrch:_accept { handle } \
{
	global		_GW_srch _GD_srch _GD_resources
	set new_host	true

	$_GW_srch($handle,dialog) defineCursor watch; . defineCursor watch
	set host [combo:getValue $_GW_srch($handle,XFS_HOST)]

	set sv_hip $_GD_srch($handle,hip)
	set sv_hfn $_GD_srch($handle,hfn)
	set sv_ship $_GD_srch($handle,search,hip)
	set sv_shfn $_GD_srch($handle,search,hfn)

	if {$host == $_GD_srch($handle,search,hfn)} {
		set new_host false
	} elseif {[hu:getIpAddress $host hfn hpd hip $_GD_resources(hostsFile)]} {
		if {$hip == $_GD_srch($handle,search,hip)} {
			set new_host false
		}
		set _GD_srch($handle,search,hip) $hip
		set _GD_srch($handle,search,hfn) $hfn
	} else {
		if {$host != $_GD_srch($handle,search,hfn)} {
			set _GD_srch($handle,search,hip) " "
			set _GD_srch($handle,search,hfn) $host
		} else {
			set new_host false
		}
	}

	if {$new_host == "true" || $_GD_srch($handle,FLUSH_CACHE) == "true"} {
		set _GD_srch($handle,hip) $_GD_srch($handle,search,hip)
		set _GD_srch($handle,hfn) $_GD_srch($handle,search,hfn)

		####	Format the query and get the objects
		set query [dkSrch:_formQuery $handle]
		if {[catch {set objects [xfsObjects [join $query \n]]} err]} {
			set _GD_srch($handle,hip) $sv_hip
			set _GD_srch($handle,hfn) $sv_hfn
			set _GD_srch($handle,search,hip) $sv_ship
			set _GD_srch($handle,search,hfn) $sv_shfn

			em:setMessageString $handle \
				"Unable to perform search on $host."
			regsub -all -- "\n" [string trim $err] "\n\t" nerr
			em:storeMsg $handle error \
				"Unable to get list of objects.\n\t$nerr"

			$_GW_srch($handle,dialog) defineCursor ""
			. defineCursor ""
			return
		}

		####	Use the full host name so that it is consistent
		####	with what is used throughout the Tcl/Tm code
		set nobjs {}
		foreach obj $objects {
			lvarpop obj 0 $_GD_srch($handle,hfn)
			lappend nobjs $obj
		}
		set _GD_srch($handle,objects) [xfsmSort diskObjs $nobjs]

	} elseif {$_GD_srch($handle,needsort)} {
		####	Sort the objects
		set _GD_srch($handle,objects) \
			[xfsmSort diskObjs $_GD_srch($handle,objects)]
		set _GD_srch($handle,needsort) false
	}

	$_GD_srch(iconpanel) clear
	dk:searchCompleted
	ip:setHostName dk [hu:getHostPlusDom $_GD_srch($handle,hfn)]

	if {$_GD_srch($handle,FLUSH_CACHE)} {
		$_GW_srch($handle,FLUSH_CACHE) setValues -set false
		set _GD_srch($handle,FLUSH_CACHE) false
	}

	if {[llength $_GD_srch($handle,objects)] == 0} {
		$_GD_srch(iconpanel) render
		$_GW_srch($handle,dialog) defineCursor ""; . defineCursor ""
		return
	}

	####	Collect the filter data
	set pattern [string trim [$_GW_srch($handle,NAME) getString]]
	if {$pattern != "" && $pattern != "*"} {
		foreach item $_GD_srch($handle,objects) {
			obj:parse $item o_class o_host o_nm o_type
			if {[string match $pattern $o_nm]} {
				lappend filtered $item
			}
		}
	} else {
		set no_filter true
	}

	if {[info exists no_filter]} {
		ip:fillPanel dk $_GD_srch($handle,objects)
	} elseif {[info exists filtered]} {
		ip:fillPanel dk $filtered
	} else {
		$_GD_srch(iconpanel) render
	}

	$_GW_srch($handle,dialog) defineCursor ""; . defineCursor ""
}

#%COMMENT_BEGIN
# Function:	dkSrch:_cancel
# Synopsis:	This is called when the user presses the "Cancel" dialog
#		button.  It unmanages the dialog.
# Arguments:	- handle	The identifier for the desired instance.
#
#%COMMENT_END
proc dkSrch:_cancel { handle } \
{
	global	_GW_srch; $_GW_srch($handle,dialog) unmanageChild
}
