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

//------------ Modification Log -----------------
//CPY 9/24/09 QA70-14275 MODULE_LICENSE_DIALOG_MOVED_TO_RC
/// Iris 2/09/2010 FIX_NOT_RECOMPILE_OC_FILES_AFTER_INSTALL_DEV_KIT_LIC
//-----------------------------------------------


#define LIC_FILE_PATH		okutil_get_origin_path(ORIGIN_PATH_LIC)	
#define LIC_FILE_EXTENSION	"lic"
//---- CPY 9/24/09 QA70-14275 MODULE_LICENSE_DIALOG_MOVED_TO_RC
// return empty if something wrong
/// Iris 2/09/2010 FIX_NOT_RECOMPILE_OC_FILES_AFTER_INSTALL_DEV_KIT_LIC
//static string _get_lic_file_name_from_lic_str(const string& strLic)
static string _get_lic_file_name_from_lic_str(const string& strLic, bool bRemoveDot = true)
///End FIX_NOT_RECOMPILE_OC_FILES_AFTER_INSTALL_DEV_KIT_LIC
{
	vector<string> vs;
	int nn = strLic.GetTokens(vs);
	if(nn < 5) return "";
	if(vs[0].CompareNoCase("FEATURE") != 0) return "";
	//FEATURE DeveloperKit8.1 orglab 8.1 
	string str = vs[1];
	if( bRemoveDot ) /// Iris 2/09/2010 FIX_NOT_RECOMPILE_OC_FILES_AFTER_INSTALL_DEV_KIT_LIC
		str.Replace(".", "");
	
	return str;
}
//----


void lic(const string& hint, const string& lic, const string& name)
{
	
	//---- CPY 9/24/09 QA70-14275 MODULE_LICENSE_DIALOG_MOVED_TO_RC
	//if( lic.IsEmpty() || name.IsEmpty() )
	//	return;
	if( lic.IsEmpty() )
	{
		///-----Kit 10/22/2010 CLEANUP_XFS_ERROR_CODE
		//warning_msg_box("license string empty, nothing to do.", false, 'E');
		string strErrMsg = _L("license string empty, nothing to do.");
		XF_THROW(strErrMsg);
		///-----End CLEANUP_XFS_ERROR_CODE
		return;
	}
	string strFileName = name;
	if(strFileName.IsEmpty())
		strFileName = _get_lic_file_name_from_lic_str(lic);
	if( strFileName.IsEmpty() )
	{
		///-----Kit 10/22/2010 CLEANUP_XFS_ERROR_CODE
		//warning_msg_box("license string error.", false, 'E');
		string strMsgErr = _L("license string error.");
		XF_THROW(strMsgErr);
		///-----End CLEANUP_XFS_ERROR_CODE
		return;
	}
	//----
	string 	strFile = LIC_FILE_PATH + strFileName + "." + LIC_FILE_EXTENSION;
	if( strFile.IsFile() )
		DeleteFile(strFile);
	
	stdioFile ff(strFile, file::modeCreate | file::modeReadWrite);
	ff.WriteString(lic);
	
	/// Iris 2/09/2010 FIX_NOT_RECOMPILE_OC_FILES_AFTER_INSTALL_DEV_KIT_LIC
	if( strFile.IsFile() )
	{
		string strModuleName;
		strModuleName = _get_lic_file_name_from_lic_str(lic, false);
		on_lic_module_add(strModuleName);
	}
	///End FIX_NOT_RECOMPILE_OC_FILES_AFTER_INSTALL_DEV_KIT_LIC
	
	warning_msg_box(XFERR_LIC_UPDATED, "", true);
	
}

//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 lic_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return lic_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	
	bool bHasErr = false;
	/* //---- CPY 9/24/09 QA70-14275 MODULE_LICENSE_DIALOG_MOVED_TO_RC better not show err if empty
	if( trGetN.lic.strVal.IsEmpty() || PDS_UNASSIGNED == okutil_cvt_str_to_predefined_type(trGetN.lic.strVal))
	{
		bHasErr = true;
		bOKEnable = false;
		strErrMsg = CER_EMPTY_VARIABLE;
		strErrMsg += ":License string";
	}
	if( !bHasErr && (trGetN.name.strVal.IsEmpty() || PDS_UNASSIGNED == okutil_cvt_str_to_predefined_type(trGetN.name.strVal)) ) 
	{
		bHasErr = true;
		bOKEnable = false;
		strErrMsg = CER_EMPTY_VARIABLE;
		strErrMsg += ":License Name";
	}
	*/
	if( !bHasErr && 0 == lstrcmpi(trGetN.name.strVal, "orglab") )
	{
		bHasErr = true;
		bOKEnable = false;
		strErrMsg = XFERR_RESERVED_LIC_FILE_NAME;		
	}		
	
	//---- Iris not allow hint wrapped since want keep large width for dialog
	int nVal;
	if( trGetN.hint.GetAttribute(STR_ATTRIB_DISPFMT, nVal) && (nVal & DISPLAY_EDITOR_WRAPPED) )
	{
		nVal = nVal & ~DISPLAY_EDITOR_WRAPPED;
		trGetN.hint.SetAttribute(STR_ATTRIB_DISPFMT, nVal);
	}
	//---- 
	
	return false;

}

int lic_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 lic_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	
	//string 	strFile = LIC_FILE_PATH;	
	//if( strFile.IsFile() )
	//{		
		//stdioFile ff;
		//bool bRet = ff.Open(strFile, file::modeRead);
		//if( bRet )
		//{
			//trGetN.lic.strVal = "";
			//string strTemp;
		    //while(ff.ReadString(strTemp))
		    //{
		    	//trGetN.lic.strVal += strTemp + "\r\n";
		    //}		
		    //trGetN.lic.strVal.TrimRight("\r\n");
		//}
	//}	
	
}
//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 lic_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
