/*****************************************************************************
* Copyright (c) 1990-2000 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
*
*****************************************************************************/


// PortBChannel.cpp: implementation of the CPortBChannel class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "PortBChannel.h"
#include "PropChannelInfo.h"

#include "Resource.h"

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

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

IMPLEMENT_DYNAMIC(CPortBChannel, CPort)

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

      // Solve our identity crisis
      SetIdentity(DXB_REGVAL_PORT_TYPE_BCHANNEL);

      // Set what we can do
      EnableProperties(FALSE);
      m_fNeedSpid = TRUE;

      // Set our display name
      CString strDisplayName;
      strDisplayName.Format(IDS_DisplayName_ChannelFormat, (GetPortNumber() + 1));
      SetDisplayName(strDisplayName);

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

      // Wiz mode stuff
      m_pwsSheet = NULL;
      m_WizChanInfo.SetOwner(this);
      m_WizChanInfo.SetState(WP_CHANINFO);

      CConfigObject * pService = GetParent();
      AffirmThrow(pService != NULL);
      CConfigObject * pProtocol = pService->GetParent();
      AffirmThrow(pProtocol != NULL);
      CConfigObject * pLine = pProtocol->GetParent();
      AffirmThrow(pLine != NULL);

      CString strTitle;
      strTitle = pLine->GetDisplayName() + TEXT(" - ") + GetDisplayName();
      m_WizChanInfo.SetTitle(strTitle);
   }
   catch (CAffirmException ae)
   {
   }
}

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

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

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

   try
   {
      AffirmThrow(CPort::GetParameters());
      fSuccess = TRUE;
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

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

   try
   {
      AffirmThrow(CPort::SetParameters());
      fSuccess = TRUE;
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

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

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

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

   return fSuccess;
}

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

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

      if (m_fDoProps)
      {
         m_WizChanInfo.SetChannelInfo(m_strAddress, m_strSpid, m_fNeedSpid, m_strSubAddress, m_fNeedSubAddress);
         AffirmThrow(m_pwsSheet->InsertPage(&m_WizChanInfo, FALSE, TRUE));
      }

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

   return fSuccess;
}

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

   try
   {
      AffirmThrow(pSheet != NULL);

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

      AffirmThrow(CPort::RemoveWizardPages(pSheet));

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

   return fSuccess;
}

BOOL CPortBChannel::HandleWizChanInfo(void)
{
   BOOL fSuccess = FALSE;

   try
   {
      AffirmThrow(m_pwsSheet != NULL);

      m_WizChanInfo.GetChannelInfo(m_strAddress, m_strSpid, m_strSubAddress);

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

   return fSuccess;
}

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

   try
   {
      switch (nState)
      {
         case WP_CHANINFO: AffirmThrow(HandleWizChanInfo()); break;
         default: AffirmThrow(FALSE); break;
      }

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

   return fSuccess;
}

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

   try
   {
      if (m_fDoProps)
      {
         CString str;

         str.Format(IDS_Information_PhoneNumberFormat, m_strAddress);
         strInfo += str;

         if (m_fNeedSpid)
         {
            str.Format(IDS_Information_SpidFormat, m_strSpid);
            strInfo += str;
         }
         if (m_fNeedSubAddress)
         {
            str.Format(IDS_Information_SubAddressFor, m_strSubAddress);
            strInfo += str;
         }
      }
      else
      {
         strInfo = CPort::GetInformationText();
      }
   }
   catch (CAffirmException ae)
   {
   }

   return strInfo;
}

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

   try
   {
      AffirmThrow(m_fDoProps);

      CPropChannelInfo ChannelInfo;
      ChannelInfo.SetChannelInfo(m_strAddress, m_strSpid, m_fNeedSpid, m_strSubAddress, m_fNeedSubAddress);

      CPropertySheet PropSheet(GetDisplayName());
      PropSheet.AddPage(&ChannelInfo);

      PropSheet.m_psh.dwFlags |= PSH_NOAPPLYNOW;
      if (PropSheet.DoModal() == IDOK)
      {
         ChannelInfo.GetChannelInfo(m_strAddress, m_strSpid, m_strSubAddress);
         fThingsHaveChanged = TRUE;
      }
   }
   catch (CAffirmException ae)
   {
   }

   return fThingsHaveChanged;
}
