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


// ProtocolPRI.cpp: implementation of the CProtocolPRI class.
//
//////////////////////////////////////////////////////////////////////

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

#include "Resource.h"

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

IMPLEMENT_DYNAMIC(CProtocolPRI, CProtocol)

CProtocolPRI::CProtocolPRI(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_PRI);

      // Set our display name
      CString strDisplayName;
      AffirmThrow(strDisplayName.LoadString(IDS_DisplayName_PRI));
      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]));
      }

      // Init config mode stuff
      m_pwsSheet = NULL;

      m_pSwitchWizPage = NULL;

      AffirmThrow(m_SwitchValueMap.Init(SValueMap::MAP_PRI_SWITCH));

      if ((m_DS1Config.Data.signalling == 0) ||
          ((m_DS1Config.Data.signalling != PRI_NI2) &&
           (m_DS1Config.Data.signalling != PRI_5ESS) &&
           (m_DS1Config.Data.signalling != PRI_DMS100) &&
           (m_DS1Config.Data.signalling != PRI_JAPAN)))
         m_DS1Config.Data.signalling = PRI_5ESS;

      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;
   }
   catch (CAffirmException ae)
   {
   }
}

CProtocolPRI::~CProtocolPRI()
{
   try
   {
      if (m_pSwitchWizPage != NULL) delete m_pSwitchWizPage;
   }
   catch (CAffirmException ae)
   {
   }
}

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

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

   return fSuccess;
}

BOOL CProtocolPRI::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;
}

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

BOOL CProtocolPRI::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 CProtocolPRI::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 CProtocolPRI::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 CProtocolPRI::CopyParameters(CConfigObject const * const pFrom)
{
   BOOL fSuccess = FALSE;

   try
   {
      AffirmThrow(pFrom != NULL);
      CProtocolPRI * pFromProtocol = (CProtocolPRI *) 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 CProtocolPRI::GetConfigData(void)
{
   BOOL fSuccess = FALSE;

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

   return fSuccess;
}

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

BOOL CProtocolPRI::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;

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

      CString strTitle;
      CString strInstruction;

      if (m_pSwitchWizPage == NULL)
      {
         AffirmThrow(strTitle.LoadString(IDS_Title_SwitchType));
         AffirmThrow(strInstruction.LoadString(IDS_Instruction_SwitchType));
         strTitle = GetParent()->GetDisplayName() + TEXT(" - ") + strTitle;

         m_pSwitchWizPage = new CWizPageComboBox(this, WP_SWITCH, strTitle, strInstruction);
         AffirmThrow(m_pSwitchWizPage != NULL);

         AffirmThrow(m_pSwitchWizPage->SetValues(m_SwitchValueMap.astrDisplayNames, m_SwitchValueMap.ToIndex(m_DS1Config.Data.signalling, 0)));
         m_pSwitchWizPage->SetHelpID(IDH_WizPageSwitch);
      }

      AffirmThrow(m_pwsSheet->InsertPage(m_pSwitchWizPage));

#endif

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

   return fSuccess;
}

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

   try
   {
      AffirmThrow(pSheet != NULL);

      if (m_pwsSheet != NULL)
      {
         AffirmThrow(m_pwsSheet == pSheet);
         if (m_pSwitchWizPage != NULL) m_pwsSheet->RemovePage(m_pSwitchWizPage);
      }

      AffirmThrow(CProtocol::RemoveWizardPages(pSheet));

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

   return fSuccess;
}

BOOL CProtocolPRI::HandleWizSwitch(void)
{
   BOOL fSuccess = FALSE;

   try
   {
      AffirmThrow(m_pwsSheet != NULL);

      int iChoice = m_pSwitchWizPage->GetChoice();
      m_DS1Config.Data.signalling = m_SwitchValueMap.FromIndex(iChoice);

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

   return fSuccess;
}

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

   try
   {
      switch (nState)
      {
         case WP_SWITCH:      AffirmThrow(HandleWizSwitch());     break;
         default: AffirmThrow(FALSE); break;
      }

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

   return fSuccess;
}

CString CProtocolPRI::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_SwitchValueMap.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 CProtocolPRI::DoProperties(void)
{
   BOOL fThingsHaveChanged = FALSE;

   try
   {
      CPropertySheet PropSheet(GetDisplayName());

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

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

      PropSheet.AddPage(&PropPRI);
      PropSheet.AddPage(&PropAdv);
      
      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 = PropAdv.GetDChanMode();

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

   return fThingsHaveChanged;
}
