﻿//------------------------------------------------------------------------------
//
// ADOBE SYSTEMS INCORPORATED
// Copyright 2000-2004 Adobe Systems Incorporated 
// All Rights Reserved
//
// NOTICE: Adobe permits you to use, modify, and distribute 
// this file in accordance with the terms of the Adobe license 
// agreement accompanying it. If you have received this file 
// from a source other than Adobe, then your use, modification, 
// or distribution of it requires the prior written permission 
// of Adobe.
//
//------------------------------------------------------------------------------

///////////////////////////////////////////////////////////////////////////////
//
// UI related event handler
//

//-----------------------------------------------------------------------------
// 
// vcsInitializePanel(...)
// 
// Purpose: initialize UI
// 
//-----------------------------------------------------------------------------

function vcsInitializePanel(e) 
{ 
	dbgwriteln( 'vcsInitializePanel' ); 
	
	//
	// reposition controls
	//
	var offset = e.controlPos - e.target.panel.children['editSpec'].location.x;
	
	e.target.panel.children['staticPort'].location.x = e.labelPos;
	e.target.panel.children['staticSpec'].location.x = e.labelPos;
	e.target.panel.children['editPort'].location.x = e.controlPos;
	e.target.panel.children['editSpec'].location.x = e.controlPos;
	e.target.panel.children['btnSpecs'].location.x += offset;
	
	//
	// set values
	//
	if( typeof( e.settings.port ) == 'string' )
	{
		e.target.panel.children['editPort'].text = e.settings.port;
	}
	else if( typeof( prefs.p4Port ) == 'string' )
	{
		e.target.panel.children['editPort'].text = prefs.p4Port;
	}
	
	if( typeof( e.settings.client ) == 'string' )	
	{
		e.target.panel.children['editSpec'].text = e.settings.client;
	}
	else if( typeof( prefs.p4ClientSpec ) == 'string' )
	{
		e.target.panel.children['editSpec'].text = prefs.p4ClientSpec;
	}
	
	//
	// enable state
	//	
	e.target.panel.children['editSpec'].enabled = false;
	e.target.panel.children['btnSpecs'].enabled = ( parseInt( e.target.panel.children['editPort'].text ) != NaN );
	
	//
	// current settings
	//
	e.target.currentSettings = e.settings;
} 

//-----------------------------------------------------------------------------
// 
// vcsTerminatePanel(...)
// 
// Purpose: terminate UI
// 
//-----------------------------------------------------------------------------

function vcsTerminatePanel(e) 
{ 
	e.settings.port   = e.target.panel.children['editPort'].text;
	e.settings.client = e.target.panel.children['editSpec'].text;
	
	e.target.currentSettings = null;
	
	writeEventLog( e, '', '' );
} 

//-----------------------------------------------------------------------------
// 
// vcsCheckSettings(...)
// 
// Purpose: check settings to en/disable 'next' button of wizard
// 
//-----------------------------------------------------------------------------

function vcsCheckSettings(e)
{
	if( e.target.currentSettings != null )
		e.result = validateSettings( e.target.currentSettings, e.target.panel );
	else
		e.result = true;
}

//-----------------------------------------------------------------------------
// 
// vcsButtonClick(...)
// 
// Purpose: button clicked : choose client specification
// 
//-----------------------------------------------------------------------------

function vcsButtonClick(e) 
{ 
	dialogs['dlgChooseSpec'].children['specPopup'].removeAll();
	dialogs['dlgChooseSpec'].center();
	
	var serverURL = e.currentTarget.currentSettings.url;
	var pos0 = serverURL.indexOf( '/' );
	if( pos0 >= 0 )	
	{
		serverURL = serverURL.slice( 0, pos0 );
		serverURL = serverURL.strippWS();
	}

	//
	// get available clients
	//
	var port = e.currentTarget.panel.children['editPort'].text;
	
	if( isNaN( parseInt( port.strippWS() ) ) )
	{
		alert( module.localize( '$$$/invalidserver=Invalid server' ) );
		return;
	}

	if( PerforceFindApp() == null )
	{
		Window.alert( module.localize( '$$$/nop4=Perforce application not found' ) );
		return;
	}

	var tmp4 = new Perforce( serverURL, port, '', e.currentTarget.currentSettings.user, e.currentTarget.currentSettings.password );
	
	app.startProgress( module.localize( '$$$/connect2server=Connect to server' ), module.localize( '$$$/connect2=Connect to' ) + ' ' + serverURL + ':' + e.currentTarget.panel.children['editPort'].text, true );

	tmp4.clients();
	var result = tmp4.lastResult;
	
	app.stopProgress();
	
	if( result.exit == 0 && result.output.indexOf( 'Perforce client error' ) < 0 )
	{
		var clients = result.output.split( '\r' );
		
		for( var i=0; i<clients.length; i++ )
		{
			var entries = clients[i].toString().split( ' ' );
			
			if( entries.length >= 2 && entries[0].indexOf( 'Client' ) >= 0 )
				dialogs['dlgChooseSpec'].children['specPopup'].addItem( entries[1].toString() );
		}
	}
	else
	{
		alert( module.localize( '$$$/invalidserver=Invalid server' ) );
		return;
	}

	if( dialogs['dlgChooseSpec'].show() == 1 )
	{
		var specName = dialogs['dlgChooseSpec'].children['specPopup'].text;
		
		var validSpec = true;
		
		//
		// find root folder and host name in spec
		//
		var specRoot = '#noroot#';
		
		var p4Con = new Perforce( serverURL, e.currentTarget.panel.children['editPort'].text, specName, e.currentTarget.currentSettings.user, e.currentTarget.currentSettings.password );

		app.startProgress( module.localize( '$$$/connect2server=Connect to server' ), module.localize( '$$$/connect2=Connect to' ) + ' ' + serverURL + ':' + e.currentTarget.panel.children['editPort'].text, true );
		
		if( p4Con.client( '-o' ) )		
		{
			//
			// get root folder
			//
			var pos0 = p4Con.lastResult.output.indexOf( '#  Root:' );
			
			if( pos0 >= 0 )
			{
				var pos1  = p4Con.lastResult.output.indexOf( 'Root:', pos0+8 );
				
				if( pos1 >= 0 )
				{
					var tmp = p4Con.lastResult.output.slice( pos1+5 );
					
					pos1 = tmp.indexOf( '\r' );
					
					if( pos1 >= 0 )
					{
						tmp = tmp.slice( 0, pos1 );
						
						specRoot = tmp.strippWS();
					}
				}
			}
			
			//
			// get host name
			//
			var pos0 = p4Con.lastResult.output.indexOf( '#  Host:' );
			
			if( pos0 >= 0 )
			{
				var pos1  = p4Con.lastResult.output.indexOf( 'Host:', pos0+8 );
				
				if( pos1 >= 0 )
				{
					var tmp = p4Con.lastResult.output.substring( pos1+5 );
					
					pos1 = tmp.indexOf( '\r' );
					
					if( pos1 >= 0 )
					{
						tmp = tmp.slice( 0, pos1 );
						
						e.currentTarget.currentSettings.host = tmp.strippWS();
					}
				}
			}
		}
		
		app.stopProgress();

		if( specRoot != '#noroot#' )
		{
			//
			// TODO: if spec root is not in site root then failed !
		}
		
		if( !validSpec )
		{
			specName = '';
			alert( module.localize( '$$$/badclientspec=Client specifications root not under site root' ) );
		}

		e.target.parent.children['editSpec'].text = specName;
	}

	writeEventLog( e, '', '' );
} 
