/*****************************************************************************
* Copyright (c) 1990-98 Digi International Inc., All Rights Reserved
* 
* This software contains proprietary and confidential information of Digi
* International Inc.  By accepting transfer of this copy, Recipient agrees
* to retain this software in confidence, to prevent disclosure to others,
* and to make no use of this software other than that for which it was
* delivered.  This is an unpublished copyrighted work of Digi International
* Inc.  Except as permitted by federal law, 17 USC 117, copying is strictly
* prohibited.
* 
* Restricted Rights Legend
*
* Use, duplication, or disclosure by the Government is subject to
* restrictions set forth in sub-paragraph (c)(1)(ii) of The Rights in
* Technical Data and Computer Software clause at DFARS 252.227-7031 or
* subparagraphs (c)(1) and (2) of the Commercial Computer Software -
* Restricted Rights at 48 CFR 52.227-19, as applicable.
*
* Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
*
*****************************************************************************/


// AdapterLondon.cpp: implementation of the CAdapterLondon class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "AdapterLondon.h"
#include "ObjectFactory.h"
#include "ModemCCDisplayNameMap.h"

#include "Resource.h"

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

IMPLEMENT_DYNAMIC(CAdapterLondon, CAdapter)

CAdapterLondon::CAdapterLondon(HKEY const hKeyRoot, CString const & strRegPath, CConfigObject const * const pParent) : CAdapter(hKeyRoot, strRegPath, pParent)
{
   try
   {
      // Get parameters
      AffirmThrow(GetParameters());

      if (m_dwModemCC == 0) m_dwModemCC = COUNTRY_CODE_UNITED_STATES;

      // Set what we can do
      SetAbility(CConfigObject::DOPROPERTIES);

      // Get our static config data
      AffirmThrow(GetConfigData());

      // Init wiz mode stuff
      m_WizModemCC.Construct(CPageModemCC::WIZARD, this, WP_MODEMCC);
      m_WizModemCC.Init(m_adwModemCCsAllowed);
   }
   catch (CAffirmException ae)
   {
   }
}

CAdapterLondon::~CAdapterLondon()
{
   try
   {
   }
   catch (CAffirmException ae)
   {
   }
}

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

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

   try
   {
      AffirmThrow(RegReadDWordValue(m_hKey, DXB_REGVAL_MODEM_NATIONALITY, &m_dwModemCC));
      fSuccess = TRUE;
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

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

   try
   {
      AffirmThrow(CAdapter::SetParameters());
      AffirmThrow(RegWriteDWordValue(m_hKey, DXB_REGVAL_MODEM_NATIONALITY, m_dwModemCC));
      fSuccess = TRUE;
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

//////////////////////////////////////////////////////////////////////
// Config data
//////////////////////////////////////////////////////////////////////

BOOL CAdapterLondon::GetConfigData(void)
{
   BOOL fSuccess = FALSE;

   try
   {
      static DWORD const adwModemCCsU[] = LONDON_U_DRV_CC_LIST_INIT;
      static DWORD const adwModemCCsST[] = LONDON_ST_DRV_CC_LIST_INIT;

      DWORD const * adwModemCCs = NULL;
      int cModemCCs = 0;

      if (GetIdentity() == DXB_REGVAL_ADAPTER_ENUM_LONDON_ST)
      {
         adwModemCCs = adwModemCCsST;
         cModemCCs = ElementCount(adwModemCCsST);
      }
      else
      {
         adwModemCCs = adwModemCCsU;
         cModemCCs = ElementCount(adwModemCCsU);
      }

      AffirmThrow(adwModemCCs != NULL);
      AffirmThrow(cModemCCs > 0);

      m_adwModemCCsAllowed.RemoveAll();

      for (int i = 0; i < cModemCCs; i++)
      {
         m_adwModemCCsAllowed.Add(adwModemCCs[i]);
      }

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

   return fSuccess;
}

//////////////////////////////////////////////////////////////////////
// Wizard config methods
//////////////////////////////////////////////////////////////////////

BOOL CAdapterLondon::AddWizardPages(CConfigWizSheet * const pSheet)
{
   BOOL fSuccess = FALSE;

   try
   {
      AffirmThrow(pSheet != NULL);

#if defined(_DEBUG)
      if ((m_pwsSheet != NULL) && (m_pwsSheet != pSheet))
         TRACE("AddWizardPages() called for different sheet!\n");
#endif

      m_pwsSheet = pSheet;

      AffirmThrow(CAdapter::AddWizardPages(pSheet));

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

   return fSuccess;
}

BOOL CAdapterLondon::RemoveWizardPages(CConfigWizSheet * const pSheet)
{
   BOOL fSuccess = FALSE;

   try
   {
      AffirmThrow(pSheet != NULL);
      pSheet->RemovePage(&m_WizModemCC);
      AffirmThrow(CAdapter::RemoveWizardPages(pSheet));
      fSuccess = TRUE;
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

BOOL CAdapterLondon::HandleWizWelcome(void)
{
   BOOL fSuccess = FALSE;

   try
   {
      AffirmThrow(m_pwsSheet != NULL);
      m_WizModemCC.SetModemCC(m_dwModemCC);
      AffirmThrow(m_pwsSheet->InsertPage(&m_WizModemCC));
      AffirmThrow(CAdapter::HandleWizWelcome());
      fSuccess = TRUE;
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

BOOL CAdapterLondon::HandleWizModemCC(void)
{
   BOOL fSuccess = FALSE;

   try
   {
      AffirmThrow(m_pwsSheet != NULL);

      m_dwModemCC = m_WizModemCC.GetModemCC();

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

   return fSuccess;
}

BOOL CAdapterLondon::NextWizardPage(int const nState)
{
   BOOL fSuccess = FALSE;

   try
   {
      switch (nState)
      {
         case WP_WELCOME: AffirmThrow(HandleWizWelcome());                break;
         case WP_MODEMCC: AffirmThrow(HandleWizModemCC());                break;
         default:         AffirmThrow(CAdapter::NextWizardPage(nState));  break;
      }

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

   return fSuccess;
}

//////////////////////////////////////////////////////////////////////
// UI config methods
//////////////////////////////////////////////////////////////////////

CString CAdapterLondon::GetInformationText(void)
{
   CString strInfo = TEXT("");
   try
   {
      CModemCCDisplayNameMap mapModemCCDisplayNames;
      strInfo.Format(IDS_Information_ModemCC, mapModemCCDisplayNames[m_dwModemCC]);
   }
   catch (CAffirmException ae)
   {
   }
   return strInfo;
}

BOOL CAdapterLondon::DoProperties(void)
{
   BOOL fThingsHaveChanged = FALSE;
   try
   {
      CPageModemCC PropModemCC(CPageModemCC::PROPPAGE);
      PropModemCC.Init(m_adwModemCCsAllowed);
      PropModemCC.SetModemCC(m_dwModemCC);

      CPropertySheet PropSheet(GetDisplayName());
      PropSheet.AddPage(&PropModemCC);
      
      PropSheet.m_psh.dwFlags |= PSH_NOAPPLYNOW;
      if (PropSheet.DoModal() == IDOK)
      {
         m_dwModemCC = PropModemCC.GetModemCC();
         fThingsHaveChanged = TRUE;
      }
   }
   catch (CAffirmException ae)
   {
   }
   return fThingsHaveChanged;
}
