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


// LineHouston.cpp: implementation of the CLineHouston class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "LineHouston.h"
#include "ObjectFactory.h"
#include "ProtocolDisplayNameMap.h"
#include "PropProtocol.h"

#include "Resource.h"

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

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

IMPLEMENT_DYNAMIC(CLineHouston, CLineCSU)

CLineHouston::CLineHouston(HKEY const hKeyRoot, CString const & strRegPath, CConfigObject const * const pParent) : CLineCSU(hKeyRoot, strRegPath, pParent)
{
   try
   {
      // Solve our identity crisis
      // NOTE: Lines do not have identities!

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

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

      // Set default protocol if one isn't already.
      AffirmThrow(m_astrProtocolsAllowed.GetSize() > 0);
      if (!IsValidProtocol(m_strProtocol)) m_strProtocol = m_astrProtocolsAllowed[0];

      // Give birth to our children
      AffirmThrow(AddChild());

      // Init wiz mode stuff
      m_pwsSheet = NULL;
      m_pwpProtocol = NULL;
   }
   catch (CAffirmException ae)
   {
   }
}

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

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

   try
   {
      Affirm(m_apChildren.GetSize() == 0);

      INT_PTR iNewChild = m_apChildren.Add(NULL);
      AffirmThrow(iNewChild == 0);

      // 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 protocol parameters reside at the same level.
      //    So, pass in the hKey and path we were given.

      AffirmThrow(NewConfigObject(&m_apChildren[iNewChild], CConfigObject::PROTOCOL, m_strProtocol, m_hKeyRoot, m_strRegPath, this));
      AffirmThrow(m_apChildren[iNewChild] != NULL);

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

   return fSuccess;
}

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

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

      BOOL fDoProtocolWizPage = 
#ifdef NT5
         // For NT5, the protocol is at the adapter level, all lines must be configured the same
         FALSE;
#else
         (m_astrProtocolsAllowed.GetSize() > 1);
#endif

      if (fDoProtocolWizPage)
      {
         if (m_pwpProtocol == NULL)
         {
            CString strTitle;
            CString strInstruction;

            AffirmThrow(strTitle.LoadString(IDS_Title_LineProtocol));
            AffirmThrow(strInstruction.LoadString(IDS_Instruction_LineProtocol));

            strTitle = GetDisplayName() + TEXT(" - ") + strTitle;

            m_pwpProtocol = new CWizPageComboBox(this, WP_PROTOCOL, strTitle, strInstruction);
            AffirmThrow(m_pwpProtocol != NULL);
            m_pwpProtocol->SetHelpID(IDH_WizPageLineProtocol);

            int iDefaultProtocol = 0;
            CProtocolDisplayNameMap mapProtocolDisplayNames;
            CStringArray astrProtocolDisplayNames;

            for (int iAllowed = 0; iAllowed < m_astrProtocolsAllowed.GetSize(); iAllowed++)
            {
               if (m_strProtocol == m_astrProtocolsAllowed[iAllowed]) iDefaultProtocol = iAllowed;

               astrProtocolDisplayNames.SetAtGrow(iAllowed, mapProtocolDisplayNames[m_astrProtocolsAllowed[iAllowed]]);
               AffirmThrow(!astrProtocolDisplayNames[iAllowed].IsEmpty());
            }

            AffirmThrow(m_pwpProtocol->SetValues(astrProtocolDisplayNames, iDefaultProtocol));
         }

         AffirmThrow(m_pwsSheet->InsertPage(m_pwpProtocol, FALSE, TRUE));
      }

      AffirmThrow(CLineCSU::AddWizardPages(pSheet));

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

   return fSuccess;
}

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

   try
   {
      AffirmThrow(pSheet != NULL);

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

      AffirmThrow(CLineCSU::RemoveWizardPages(pSheet));

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

   return fSuccess;
}

BOOL CLineHouston::HandleWizProtocol(void)
{
   BOOL fSuccess = FALSE;

   try
   {
      AffirmThrow(m_pwsSheet != NULL);
      AffirmThrow(m_pwpProtocol != NULL);
      AffirmThrow(m_astrProtocolsAllowed.GetSize() > 1);

      int iChoice = m_pwpProtocol->GetChoice();
      CString strNewProtocol = m_astrProtocolsAllowed[iChoice];

      AffirmThrow(SwitchProtocol(strNewProtocol));

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

   return fSuccess;
}

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

   try
   {
      switch (nState)
      {
         case WP_PROTOCOL: AffirmThrow(HandleWizProtocol());      break;
         default: AffirmThrow(CLineCSU::NextWizardPage(nState));  break;
      }

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

   return fSuccess;
}

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

   try
   {
      AffirmThrow(m_apChildren[0] != NULL);
      strInfo.Format(IDS_Information_LineFormat, m_apChildren[0]->GetDisplayName());
      strInfo += CLineCSU::GetInformationText();
   }
   catch (CAffirmException ae)
   {
   }

   return strInfo;
}

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

   try
   {
      CPropertySheet PropSheet(GetDisplayName());

      // For NT5, the protocol is at the adapter level, all lines must be configured the same
#ifndef NT5

      CPropProtocol PropProtocol;

      int iDefaultProtocol = 0;
      CProtocolDisplayNameMap mapProtocolDisplayNames;
      CStringArray astrProtocolDisplayNames;

      for (int iAllowed = 0; iAllowed < m_astrProtocolsAllowed.GetSize(); iAllowed++)
      {
         if (m_strProtocol == m_astrProtocolsAllowed[iAllowed]) iDefaultProtocol = iAllowed;

         astrProtocolDisplayNames.SetAtGrow(iAllowed, mapProtocolDisplayNames[m_astrProtocolsAllowed[iAllowed]]);
         AffirmThrow(!astrProtocolDisplayNames[iAllowed].IsEmpty());
      }

      AffirmThrow(PropProtocol.SetValues(astrProtocolDisplayNames, iDefaultProtocol));

      PropSheet.AddPage(&PropProtocol);

#endif

      AffirmThrow(CLineCSU::AddPropertyPages(&PropSheet));

      PropSheet.m_psh.dwFlags |= PSH_NOAPPLYNOW;
      if (PropSheet.DoModal() == IDOK)
      {
#ifndef NT5

         int iChoice = PropProtocol.GetChoice();
         CString strNewProtocol = m_astrProtocolsAllowed[iChoice];
         AffirmThrow(SwitchProtocol(strNewProtocol));

#endif

         AffirmThrow(CLineCSU::GetPropertiesFromPages());

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

   return fThingsHaveChanged;
}
