/*<<<***************************************************************************
The MIT License (MIT)

Copyright(C) 2013 - 2015 Marc Mirti Mancinelli, Christophe Lardau and Philippe Pamart (xdcube.org)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
////////////////////////////////////////////////////////////////////////////////
#Title: xbHotPlugItf
#Project: xdcBase
////////////////////////////////////////////////////////////////////////////////
#Author: Marc MIRTI MANCINELLI
#History:
<04/09/2013: creation.
12/02/2014: this notice inserted.
***************************************************************************>>>*/
#include "stdafx.h"

#include "xbHotPlugItf.h"
#include "xbConfig.h"
#include "xbFileTools.h"
#include "xbLibTools.h"
extern xbXdcLib g_xdcBaseDLL;

xbEimpl(xbEhotPlug,xbEadv);

int xbHotPlugTraceLevel()
{
	xbCfgKey key(xbCfgCommon,"hotplug");
	if (!key.Exists() || !key.ValueExists("TraceLevel")) return -1;
	return key.Value["TraceLevel"].AsLong - 1;
}


xbString xbLabel(xbPlugStatus status)
{
	switch(status)
	{
	case xbPlugNoop :
		break;
	case xbPlugVerif :
		return "Verifying components...";
	case xbPlugDownloading :
		return "Downloading components...";
	case xbPlugDownloadDone :
		return "Components downloaded...";
	case xbPlugInstalling :
		return "Installing components...";
	case xbPlugInstallDone :
		return "Installation terminated.";
	case xbPlugUninstalling :
		return "Uninstalling components...";
	case xbPlugUninstallDone :
		return "Uninstallation terminated.";
	default:
		break;
	}
	return "";
}


void xbHotPlugCb::Create()
{
	if (m_prCBobj.IsValid()) 
		return;
	
	CBobj *pPlugObj = xbDnew CBobj(this,m_bStartNotif,m_caps);
	try
	{
		pPlugObj->Create();
		
		m_prCBobj = pPlugObj;
		
		pPlugObj->Release();
	}
	catch(...)
	{
		pPlugObj->Release();
		throw;
	}

}

void xbHotPlugCb::Destroy()
{
	if (m_prCBobj.IsEmpty()) 
		return;
	m_prCBobj->Destroy();
	m_prCBobj.Empty();
}

xbHotPlugCb::operator IxbHotPlugTypeCb*()
{
	if (!HasPlugTypeCb())
		xbThrow xbEsys("xbHotPlugCb::operator IxbHotPlugTypeCb*() called with no plugtype cb defined");
	Create();
	return m_prCBobj;
}

//TODO ? en fonction du comportement de loadlibrary
bool xbHotPlugPathOk(const xbString &path)
{
	xbString tmp(path);
	return xbHotPlugPathOk(&tmp);
}
bool xbHotPlugPathOk(xbString *pPath)
{
	if (::xbGetFileDir(*pPath).IsEmpty())
	{
		*pPath = ::xbGetFileDir(g_xdcBaseDLL.GetInstance())+*pPath;
		return ::xbFileExists(*pPath);
	}
	return ::xbFileExists(*pPath);
}
xbString xbHotPlugPathCheck(const xbString &path)
{
	xbString fdirExe(::xbGetFileDir(g_xdcBaseDLL.GetInstance()));
	xbString fdirPlug(::xbGetFileDir(path));
	xbRelOutput("xbHotPlugPathCheck: dir exe:"+fdirExe+", dir plugin:"+fdirPlug);
	if (fdirExe == fdirPlug)
		return ::xbGetFileNameExt(path);

	return path;
}
