#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
//put additional include files here

//put your own support static functions here

#define STR_REGISTRY_KEY 			"Software\\National Instruments\\LabView\\"
#define STR_INSTALL_PATH_ATTRIB 	"Path"
#define STR_SOURCE_PATH				GetAppPath(true) + "Samples\\Automation Server\\LabVIEW\\Palette\\"



void lvvicopy(const TreeNode& version, const int& del)
{
	string strSrcPath = STR_SOURCE_PATH;
	if(!strSrcPath.IsPath())
		return;
	
	vector<string> vsFiles;
	if(!FindFiles(vsFiles, strSrcPath, "llb"))
		return;
	
	string strResPath, strMsg = "lvvicopy";
	int nChecked = 0;
	foreach(TreeNode tr in version.Children)
	{
		if(!tr.nVal || !tr.GetAttribute(STR_INSTALL_PATH_ATTRIB, strResPath))
			continue;
		nChecked ++;
		strResPath.TrimRight('\\');
		strResPath += "\\user.lib\\";
		if(strResPath.IsPath())
		{
			for(int ii=0; ii<vsFiles.GetSize(); ii++)
				CopyFile(strSrcPath + vsFiles[ii], strResPath + vsFiles[ii]);
			
			if(del)
			{
				string strOldFile = strResPath + "OriginAutomationServer_LV7.llb";
				if(strOldFile.IsFile())
					DeleteFile(strOldFile);
			}
			
			string strFilesCopied;
			strFilesCopied.Format(_L("%d files copied to: %s"), vsFiles.GetSize(), strResPath);
			strMsg += "\n    " + strFilesCopied;
		}
		else
		{
			string strPathNoFound;
			strPathNoFound.Format(_L("Path not Found: %s"), strResPath);
			strMsg += "\n    " + strPathNoFound;
		}
	}
	if(nChecked == 0)
		strMsg += "\n    " + _L("No files copied");
	Project.OutStringToResultsLog(strMsg)
}

//nEventID < 0 on event, like GETNE_ON_INIT, GETNE_ON_OK etc, >0 on change from control
//return 0 to do nothing, return PEVENT_GETN_RET_TO_CLOSE to close dialog
//dwEnables = bOKEnable, bBTN1Enable, bBTN2Enable, bBTN3Enable, , can set to false to disable OK/Apply buttons
//lpcszNodeName = the trGetN TreeNode tag name that generates the event, or is  if GETNE_ON_OK etc
//strAux = optional string for XF Wizard Bar, like DLG_NEED_CHANGE_GRID etc, it will be invalid if from internal node value change event
//strErrMsg = allowing messages to be shown
int lvvicopy_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return lvvicopy_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	if(trGetN.version.GetNodeCount()<=0)
	{
		strErrMsg = XFERR_NO_LABVIEW_INSTALLED;
		bOKEnable = false;
	}
	return false;

}

int lvvicopy_make_tree(TreeNode& tr, LPCSTR lpcszVarName)
{
	return false;
}
//nGetNDialog = 0,1,2, corresponding to the XF GetN dropdown, 0 = dialog is not used
//you can change nRet to be one of XFEVT_PROCEED (defualt), XFEVT_ABORT, XFEVT_PROCEED_NO_DLG or XFEVT_PROCEED_WITH_DLG(when nGetNDialog = 0)
//nGetNDialog = -1 when Ok on GetN dialog to allow handler to preocess <auto> etc variables
//dwCntrl is combination of bits
//LTXF_FROM_GUI_MENU, LTXF_FROM_GUI_PROMPT, LTXF_FROM_AUTO_UPDATE, LTXF_FROM_FUNCTION, LTXF_THEME_USED, LTXF_CHANGE_PARAM
void lvvicopy_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	if(is_xf_startup(nGetNDialog, dwCntrl))
	{
		TreeNode trVersions = trGetN.version;
		trVersions.SetAttribute(STR_ATTRIB_BRANCH, GETNBRANCH_OPEN);
		
		Registry reg(HKEY_LOCAL_MACHINE);
		string strMainKey = STR_REGISTRY_KEY;
		vector<string> vsSubKey;
	
		if(!reg.EnumKey( vsSubKey, strMainKey))
			return;
	
		string strKey, strPath, strVersion;
		TreeNode trOneVersion;
	
		for(int ii=0; ii<vsSubKey.GetSize(); ii++)
		{
			if(vsSubKey[ii].CompareNoCase("CurrentVersion") == 0)
				continue;
	
			strKey = strMainKey + vsSubKey[ii] + "\\";
			if(reg.GetValue(strKey, "Path", strPath) && reg.GetValue(strKey, "Version", strVersion) )
			{
				string strTagName = "Version" + ii;
				trOneVersion = trVersions.AddNumericNode( 0, strTagName, TRGP_CHECK);
				trOneVersion.SetAttribute(STR_LABEL_ATTRIB, strVersion); 
				trOneVersion.SetAttribute(STR_INSTALL_PATH_ATTRIB, strPath);
			}
		}
	}
}
//nEventID < 0 on event, like GETNE_ON_INIT, GETNE_ON_OK etc, >0 on change from control
//return 0 to do nothing, return PEVENT_GETN_RET_TO_CLOSE to close dialog
//dwEnables = bOKEnable, bBTN1Enable, bBTN2Enable, bBTN3Enable, , can set to false to disable OK/Apply buttons
//lpcszNodeName = the trGetN TreeNode tag name that generates the event, or is  if GETNE_ON_OK etc
//strAux = optional string for XF Wizard Bar, like DLG_NEED_CHANGE_GRID etc, it will be invalid if from internal node value change event
//strErrMsg = allowing messages to be shown
int lvvicopy_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
