// ServiceBase.cpp: implementation of the CServiceBase class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ServiceBase.h"
#include "resource.h"
#include "defs.h"
#include "ObjectFactory.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
IMPLEMENT_DYNAMIC(CServiceBase, CService)

CServiceBase::CServiceBase(HKEY const hKeyRoot, CString const strRegPath, CConfigObject const * const pParent)
			 :CService(hKeyRoot, strRegPath, pParent)
{
	try
	{
		m_pWizPvc = NULL;
		AffirmThrow(GetParameters());
	}
	catch(CAffirmException ae)
	{
	}

}

CServiceBase::~CServiceBase()
{
	delete m_pWizPvc;

}

//////////////////////////////////////////////////////////////////////
// Get/Set parameters
//////////////////////////////////////////////////////////////////////

BOOL CServiceBase::GetParameters(void)
{
   BOOL fSuccess = FALSE;

   try
   {
      AffirmThrow(CService::GetParameters());

      fSuccess = TRUE;
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

BOOL CServiceBase::SetParameters(void)
{
   BOOL fSuccess = FALSE;

   try
   {
	   // free up the registry resources taken by PVC's on the
	   // deleted list and remove the object
	   for(int i=0;i<m_apTrash.GetSize();i++)
	   {
			AffirmThrow(m_apTrash[i] != NULL);
			m_apTrash[i]->RemoveParameters(TRUE);
			DeleteConfigObject(&m_apTrash[i]);
	   }
	   m_apTrash.RemoveAll();
      AffirmThrow(CService::SetParameters());

      // We need to look at our parent line...
      // Port<-Service<-Protocol<-Line<-Adapter
      CLine * pLine = (CLine *)GetParent()->GetParent();
      AffirmThrow(pLine != NULL);
      AffirmThrow(RegWriteDWordValue(m_hKey, DXB_REGVAL_LINESPEED, pLine->GetLineSpeed()));

      fSuccess = TRUE;
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

BOOL CServiceBase::RemoveParameters(BOOL fRemoveKey)
{
	BOOL fSuccess = FALSE;
	try
	{
		AffirmThrow(CService::RemoveParameters(fRemoveKey));
		// remove ourselves from the registry
		AffirmThrow(RegDelKey(m_hKeyRoot,m_strRegPath));
		fSuccess = TRUE;
	}
	catch (CAffirmException ae)
	{
	}
	return fSuccess;
}
//////////////////////////////////////////////////////////////////////
// UI config methods
//////////////////////////////////////////////////////////////////////
// we provide no wizard page for configuring PVC's
BOOL CServiceBase::AddWizardPages(CConfigWizSheet * const pSheet)
{

    BOOL fSuccess = FALSE;

    try
    {
		AffirmThrow(pSheet != NULL);

		if (m_pWizPvc == NULL)
		{
			m_pWizPvc = new CWizPVCCfg(this,WP_WIZPVCCFG);
			AffirmThrow(m_pWizPvc != NULL);
		}
		// some tight coupling. Should really define a base class with a
		// virtual function about getting a list of children. But since
		// the UI is only concerned with PVC's we'll let it slide
		m_pWizPvc->SetPVCList(&m_apChildren);
		pSheet->InsertPage(m_pWizPvc);
		fSuccess = TRUE;
	}
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}
BOOL CServiceBase::NextWizardPage(int const nState)
{
   BOOL fSuccess = FALSE;

   try
   {
      if (nState == WP_WIZPVCCFG)
	  {
		  // we have a list of PVC's. make sure they get written to the
		  // registry.
		fSuccess = TRUE;
	  }
	  else
		  AffirmThrow(FALSE);
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

// pop up a dialog box to manage PVC entries. More of a convienience
BOOL CServiceBase::DoProperties(void)
{
   BOOL fSuccess = FALSE;

   try
   {
      AffirmThrow(FALSE);
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

// inform the protocol that a child is being freed up
BOOL CServiceBase::DeleteChild(CConfigObject const * const pChild)
{
	CService::DeleteChild(pChild);
/*
	// let the child remove its parameters from the registry
	// Call the base class to acutally delete the child.
	// delibaretly overwriting the const nature of ptr.
	CConfigObject* pObj = (CConfigObject*)pChild;
	pObj->RemoveParameters();
	return(CService::DeleteChild(pChild));
*/
	return TRUE;
}

