/* ADOBE SYSTEMS INCORPORATED
* Copyright 2008 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.
*/
const kFTP_OK 							= 0;
const kFTP_URL_MALFORMAT		= 4;
const kFTP_ACCESS_DENIED 		= 6;
const kFTP_COULDNT_CONNECT	= 5;
const kFTP_LOGIN_DENIED 			= 18;

var gCanceled = false;
var gStream = null;

// ------------------------------------------
// toArray
// ------------------------------------------

function toArray(o)
{
	var a = new Array();
	for(i = 0; i < o.length; i++)
		a.push(o[i]);
	
	return a;
}

// ------------------------------------------
// inArray
// ------------------------------------------

function inArray(list, elem)
{
	var found = false;
	for(var i = 0; (i < list.length) && ! found; i++)
	{
		if(elem.path == list[i].path)
			found = true;
	}
	return found;
}

// ------------------------------------------
// doAddFile
// ------------------------------------------

function doAddFile()
{
	var file = project.openResourcesDialog();
	if(file != null)
	{
		var files = task.getElement("files").list;
		files = toArray(files);
		if(file.length > 0)
			task.getElement("btnRemove").enabled = true;
		for(var i = 0; i < file.length; i++)
		{
			var f = new File(file[i]);
			var item = new ListItem;
			item.filename = f.displayName;
			item.path = f.fsName;
			
			if(! inArray(files, item))
				files.push(item);
		}
		task.getElement("files").list = files;
	}
}

// ------------------------------------------
// doRemoveFile
// ------------------------------------------

function doRemoveFile()
{
	var selectedFiles = task.getElement("files").selectedItems;
	var all = task.getElement("files").list;
	var tmpFiles = new Array();
	for(i = 0; i < all.length; i++)
	{
			var found = false;
			for(j = 0; (j < selectedFiles.length) && ! found; j++)
			{
				if(all[i].path == selectedFiles[j].path)
					found = true;
			}
			if(! found)
				tmpFiles.push(all[i]);
	}
	if(tmpFiles.length <= 0)
		task.getElement("btnRemove").enabled = false;
	task.getElement("files").list = tmpFiles;
}

// ------------------------------------------
// doCancel
// ------------------------------------------

function doCancel()
{
	gCanceled = true;
	gStream.cancel();
	task.statusDialog.end();
}

// ------------------------------------------
// doDone
// ------------------------------------------

function doDone()
{
	gCanceled = true;
	task.statusDialog.end();
}

// ------------------------------------------
// install
// ------------------------------------------

function install()
{
	task.getElement("btnRemove").enabled = false;
}

// ------------------------------------------
// configure
// ------------------------------------------

function configure(proTask)
{
	if(proTask)
	{
		task.getElement("btnRemove").enabled = true;
		var selectedFiles = project.selectedResources;
		var resources = new Array;
		for(var i = 0; i < selectedFiles.length; i++)
		{
			var file = new File(selectedFiles[i]);
			
			var item = new ListItem;
			item.filename = file.displayName;
			item.path = file.fsName;
			resources.push(item);
		}
		task.getElement("files").list = resources;
	}
	
}

// ------------------------------------------
// execute
// ------------------------------------------

function execute()
{

	var files = task.getElement("files");
	var txtUrl  = task.getElement("url").text;
	var port = task.getElement("port").text;
	var username  = task.getElement("username").text;
	var password  = task.getElement("password").text;
		
	//obtain files
	var allFiles = files.list;
	allFiles = toArray(allFiles);
	var msg = "";
	
	var error = false;
	var pwd = password;
	if(password.length <= 0)
	{
		pwd = task.promptDialog(
			localize("$$$/FTP/EnterPassword/Title=Empty Password"),
			localize("$$$/FTP/EnterPassword/Label=Please enter a password:"),
			true);
	}
	
	task.statusDialog.start("doCancel", "doDone");
	task.statusDialog.title = localize("$$$/FTP/SendTo=Send to FTP Server");
	task.statusDialog.text = localize("$$$/FTP/EstablishConnection=Establishing connection...");
	
	var header = "$$$/FTP/OperationStopped=The operation stopped with file ^Q%1^Q.\n%2\nFiles following in the queue will not be transmitted.";
	
	if((password.length <= 0) && (pwd == undefined))
	{
		task.statusDialog.text = localize("$$$/FTP/Canceled=Sending canceled");
		error = true;
	}
	else
		password = pwd;
	
	task.statusDialog.enableDoneButton(false);
	task.statusDialog.enableCancelButton(true);
	
	if(allFiles.length == 0)
	{
		task.statusDialog.text = localize("$$$/FTP/NoSourceFiles=No source files selected.");
		error = true;
	}
	else if(port.length <= 0)
	{
		task.statusDialog.text = localize("$$$/FTP/NoPortDefined=No port defined.");
		error = true;
	}

	for(var i = 0; (i < allFiles.length) && ! gCanceled && ! error; i++)
	{
		var url = txtUrl;
		var file = allFiles[i].path;
		var fileObject = new File(file); 
		if(fileObject.exists)
		{
			var fileName = fileObject.name;
			
			// check for ftp in host string
			if(! (url.indexOf("ftp://") >= 0))
				url = "ftp://" + url;
			//check for ending slash
			if(! (url.charAt(url.length - 1) == "/"))
				url = url + "/";
				
			url = url + fileName;
			
			task.statusDialog.text = localize("$$$/FTP/Uploading=Processing file %1 of %2\nUploading ^Q%3^Q...", i +1 , allFiles.length, fileObject.displayName);
			
			gStream = new URLStream;
			gStream.url = url;
			gStream.port = port;
			gStream.username = username;
			gStream.password = password;
			gStream.authentication = "basic";
			gStream.sendFile(file);
			
			var running = true;
			var status = -1;
			var handled = false;
			while(! gCanceled && gStream.isConnected) { task.statusDialog.modalDuring(); }
			
			status = gStream.error;
			switch(status)
			{
				case kFTP_OK:
					task.statusDialog.text = localize("$$$/FTP/UploadSuccess=File(s) successfully uploaded.");
					break;
				case kFTP_ACCESS_DENIED:
					msg = localize("$$$/FTP/AccessDenied=Could not connect. FTP access denied.");
					task.statusDialog.text = localize(header, fileObject.displayName, msg);
					break;
					
				case kFTP_LOGIN_DENIED:
					msg = localize("$$$/FTP/LoginDenied=Could not connect. Invalid user ID or password.");
					task.statusDialog.text = localize(header, fileObject.displayName, msg);
					break;
				case kFTP_COULDNT_CONNECT:
				case kFTP_URL_MALFORMAT:
					msg = localize("$$$/FTP/UrlMalformed=Could not connect. Server not found.");
					task.statusDialog.text = localize(header, fileObject.displayName, msg);
					break;
				default:
					task.statusDialog.text = localize("$$$/FTP/UndefinedError=Undefined error occurred. Code %1", status);
			}
			if(status != kFTP_OK)
				error = true;
			
		}
		else
		{
			msg = localize("$$$/FTP/FileNotExisting=Source file doesn't exist.");
			var displayName = decodeURI(fileObject.name);
			task.statusDialog.text = localize(header, displayName, msg);
			error = true;
		}
	}
	if(! error)
		task.getElement("password").text = password;
		
	task.statusDialog.enableDoneButton(true);
	task.statusDialog.enableCancelButton(false);
	while(! gCanceled) { task.statusDialog.modalDuring(); }
}



