/*****************************************************************************
* 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
*
*****************************************************************************/


// ProtocolE1.cpp: implementation of the CProtocolE1 class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ProtocolE1.h"
#include "PropPRI.h"
#include "PropAdvanced.h"
#include "ObjectFactory.h"

#include "Resource.h"

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

IMPLEMENT_DYNAMIC(CProtocolE1, CProtocol)

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

      // Solve our identity crisis
      SetIdentity(DXB_REGVAL_PROTOCOL_ENUM_E1);

      // Set our display name
      CString strDisplayName;
      AffirmThrow(strDisplayName.LoadString(IDS_DisplayName_E1));
      SetDisplayName(strDisplayName);

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

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

      m_astrServices.Copy(m_astrServicesAllowed);

      // Give birth to our children
      for (int i = 0; i < m_astrServices.GetSize(); i++)
      {
         AffirmThrow(AddChild(m_astrServices[i]));
      }

      // Hard coded defaults
      m_DS1Config.Data.signalling = PRI_ETSI;
      m_DS1Config.Data.wink_digits = 0;

      if (m_DChanExtConfig.Data.mode == 0) m_DChanExtConfig.Data.mode = DCHAN_NORMAL;
      if (m_DChanExtConfig.Data.address_type == 0) m_DChanExtConfig.Data.address_type = DCHAN_ADDRESS_TYPE_UNKNOWN;
      if (m_DChanExtConfig.Data.numbering_plan == 0) m_DChanExtConfig.Data.numbering_plan = DCHAN_NUMBERING_PLAN_UNKNOWN;
      if (m_DChanExtConfig.Data.subaddress_type == 0) m_DChanExtConfig.Data.subaddress_type = DCHAN_SUBADDRESS_TYPE_NSAP;

      AffirmThrow(m_E1SwitchValueMap.Init(SValueMap::MAP_E1_SWITCH));
   }
   catch (CAffirmException ae)
   {
   }
}

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

BOOL CProtocolE1::AddChild(void)
{
   BOOL fSuccess = FALSE;

   try
   {
      // Currently unimplemented
      AffirmThrow(FALSE);
      fSuccess = TRUE;
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

BOOL CProtocolE1::AddChild(CString const & strChild)
{
   BOOL fSuccess = FALSE;

   try
   {
      INT_PTR iNewChild = m_apChildren.Add(NULL);

      // This registry path thingie needs a little explanation!
      //    Here's the layout so far:
      //       ...\DigiDxb?\Parameters\N           (where ? = the adapter instance, and
      //                                                  N = 0, 1, ... # of lines)
      //    The line service parameters reside at:
      //       ...\DigiDxb?\Parameters\XXXX\N      (where XXXX = the service name)
      //    So, pass in the hKey we were told to start at, and the XXXX\N reg path.

      CString strServiceRegPath = strChild + TEXT("\\") + m_strRegPath;
      AffirmThrow(NewConfigObject(&m_apChildren[iNewChild], CConfigObject::SERVICE, strChild, m_hKeyRoot, strServiceRegPath, this));
      AffirmThrow(m_apChildren[iNewChild] != NULL);

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

   return fSuccess;
}

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

   try
   {
      AffirmThrow(CProtocol::GetParameters());
      AffirmThrow(m_DS1Config.Get(m_hKey));
      AffirmThrow(m_DChanExtConfig.Get(m_hKey));
      fSuccess = TRUE;
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

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

   try
   {
      AffirmThrow(CProtocol::SetParameters());
      AffirmThrow(m_DS1Config.Set(m_hKey));
      AffirmThrow(m_DChanExtConfig.Set(m_hKey));
      fSuccess = TRUE;
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

BOOL CProtocolE1::RemoveParameters(BOOL fRemoveKey /* = FALSE */)
{
   BOOL fSuccess = FALSE;

   try
   {
      AffirmThrow(m_DS1Config.Remove(m_hKey));
      AffirmThrow(m_DChanExtConfig.Remove(m_hKey));
      AffirmThrow(CProtocol::RemoveParameters(fRemoveKey));
      fSuccess = TRUE;
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

BOOL CProtocolE1::CopyParameters(CConfigObject const * const pFrom)
{
   BOOL fSuccess = FALSE;

   try
   {
      AffirmThrow(pFrom != NULL);
      CProtocolE1 * pFromProtocol = (CProtocolE1 *) pFrom;

      m_DS1Config = pFromProtocol->m_DS1Config;
      m_DChanExtConfig = pFromProtocol->m_DChanExtConfig;

      AffirmThrow(CProtocol::CopyParameters(pFrom));

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

   return fSuccess;
}

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

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

   try
   {
      AffirmThrow(CProtocol::GetConfigData());
      fSuccess = TRUE;
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

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

BOOL CProtocolE1::AddWizardPages(CConfigWizSheet * const pSheet)
{
   return TRUE;
}

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

   try
   {
      AffirmThrow(CProtocol::RemoveWizardPages(pSheet));
      fSuccess = TRUE;
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

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

   try
   {
      AffirmThrow(FALSE);
      fSuccess = TRUE;
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

CString CProtocolE1::GetInformationText(void)
{
   CString strInfo = TEXT("");

   try
   {
      CString str;

	  // FOR NT5, the switch type is maintained by the ISDN wizard. Currently not
	  // displaying it. If needed, info can be extracted from the registry
#ifndef NT5
      str.Format(IDS_Information_SwitchFormat,
                 m_E1SwitchValueMap.DisplayNameFromValue(m_DS1Config.Data.signalling));
      strInfo += str;
#endif

      // Address Type
      SValueMap AddrMap;
      AffirmThrow(AddrMap.Init(SValueMap::MAP_DCHAN_ADDRTYPE));
      str.Format(IDS_Information_AddressTypeFormat,
                 AddrMap.DisplayNameFromValue(m_DChanExtConfig.Data.address_type));
      strInfo += str;

      // Numbering Plan
      SValueMap NumPlanMap;
      AffirmThrow(NumPlanMap.Init(SValueMap::MAP_DCHAN_NUMPLAN));
      str.Format(IDS_Information_NumberingPlanFormat,
                 NumPlanMap.DisplayNameFromValue(m_DChanExtConfig.Data.numbering_plan));
      strInfo += str;

      // Subaddress Type
      SValueMap SubAddrMap;
      AffirmThrow(SubAddrMap.Init(SValueMap::MAP_DCHAN_SUBADDRTYPE));
      str.Format(IDS_Information_SubaddressTypeFormat,
                 SubAddrMap.DisplayNameFromValue(m_DChanExtConfig.Data.subaddress_type));
      strInfo += str;
   }
   catch (CAffirmException ae)
   {
   }

   return strInfo;
}

BOOL CProtocolE1::DoProperties(void)
{
   BOOL fThingsHaveChanged = FALSE;

   try
   {
      CPropertySheet PropSheet(GetDisplayName());

      CPropPRI PropPRI(IDS_DisplayName_E1);
      PropPRI.SetSwitchType(SValueMap::MAP_E1_SWITCH, m_DS1Config.Data.signalling);
      PropPRI.SetDChanExtConfig(&m_DChanExtConfig.Data);

      CPropAdvPRI PropAdvPRI;
      PropAdvPRI.SetDChanMode(m_DChanExtConfig.Data.mode);
      PropAdvPRI.ShowDChanMode(TRUE);

      PropSheet.AddPage(&PropPRI);
      PropSheet.AddPage(&PropAdvPRI);
      
      PropSheet.m_psh.dwFlags |= PSH_NOAPPLYNOW;
      if (PropSheet.DoModal() == IDOK)
      {
         m_DS1Config.Data.signalling = PropPRI.GetSwitchType();
         PropPRI.GetDChanExtConfig(&m_DChanExtConfig.Data);

         m_DChanExtConfig.Data.mode = PropAdvPRI.GetDChanMode();

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

   return fThingsHaveChanged;
}
