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

// HwWizDigiWizPage.cpp : implementation file
//

#include "stdafx.h"
#include "digiinf.h"
#include "DriverServices.h"
#include "HwWizDigiWizPage.h"
#include "ObjectFactory.h"

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

/////////////////////////////////////////////////////////////////////////////
// HwWizDigiWizPage property page

IMPLEMENT_DYNCREATE(CHwWizDigiWizPage, CPropertyPageEx)

CHwWizDigiWizPage::CHwWizDigiWizPage() 
   : CPropertyPageEx(CHwWizDigiWizPage::IDD, 0, IDS_HW_WIZ_DIGI_WIZ_HEADER, IDS_HW_WIZ_DIGI_WIZ_SUB_HEADER)
{
	//{{AFX_DATA_INIT(CHwWizDigiWizPage)
	//}}AFX_DATA_INIT
}

CHwWizDigiWizPage::~CHwWizDigiWizPage()
{

}

void CHwWizDigiWizPage::SetPropPageParams(HDEVINFO const DeviceInfoSet,
                                               PSP_DEVINFO_DATA const DeviceInfoData,
                                               CDevPrivateData const * const pPrivateBackPtr)
{
   // NT5 class installer provided data
   ASSERT(DeviceInfoSet != NULL);
   ASSERT(DeviceInfoData != NULL);
   ASSERT(pPrivateBackPtr != NULL);
   m_PropPageParams.DeviceInfoSet = DeviceInfoSet;
   m_PropPageParams.DeviceInfoData = DeviceInfoData;
   m_pPrivateBackPtr = pPrivateBackPtr;

   // Set up the prop sheet page's styles for use when the page is created
   CString Title;
   m_psp.dwFlags |= PSP_USETITLE;
   Title.LoadString(IDS_HWW_DIGI_TITLE);
   SetTitle(Title);

   // Turn the wiz help button off, we don't have any help for this now
   m_psp.dwFlags &= ~PSP_HASHELP;
   // Turn on the wiz page header
   m_psp.dwFlags &= ~PSP_HIDEHEADER;
   m_psp.dwFlags |= PSP_USEHEADERSUBTITLE;
   m_psp.dwFlags |= PSP_USEHEADERTITLE;

   // OK, now for the kludge, fill in what we have and then set the superset members
   memcpy(&m_kludge_psp, &m_psp, sizeof m_psp);
   m_kludge_psp.dwSize = sizeof(PROPSHEETPAGE);
   m_kludge_psp.pszHeaderTitle = MAKEINTRESOURCE(IDS_HW_WIZ_DIGI_WIZ_HEADER);
   m_kludge_psp.pszHeaderSubTitle = MAKEINTRESOURCE(IDS_HW_WIZ_DIGI_WIZ_SUB_HEADER);
}

void CHwWizDigiWizPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPageEx::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CHwWizDigiWizPage)
	DDX_Control(pDX, IDC_STATIC_DIGI_WIZ_INSTRUCTION, m_csInstruction);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CHwWizDigiWizPage, CPropertyPageEx)
	//{{AFX_MSG_MAP(CHwWizDigiWizPage)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHwWizDigiWizPage message handlers

BOOL CHwWizDigiWizPage::OnInitDialog() 
{
	CPropertyPageEx::OnInitDialog();

   // Fill the combo box with the specified instruction text
   try
   {
      CString IdsString;
      IdsString.LoadString(IDS_HW_WIZ_DIGI_WIZ_INSTRUCTION);
      m_csInstruction.SetWindowText(IdsString);
   }
   catch (CAffirmException ae)
   {
   }

   return TRUE;
}

LRESULT CHwWizDigiWizPage::OnWizardNext() 
{
   TRACE("CHwWizDigiWizPage::OnWizardNext()\n");

   BOOL fRestart = TRUE;
   CString strProtocol;
   CAdapter * pAdapter = NULL;
   CString strAdapterType = m_pPrivateBackPtr->m_strAdapterType;
   CString strAdapterRegPath = m_pPrivateBackPtr->m_strAdapterRegPath;

   try
   {
      AffirmThrow(NewConfigObject((CConfigObject **)(&pAdapter), CConfigObject::ADAPTER, strAdapterType, HKEY_LOCAL_MACHINE, strAdapterRegPath, NULL));
      AffirmThrow(NULL != pAdapter);
      CConfigWizSheet ws(TEXT(""), this);
      AffirmThrow(pAdapter->AddWizardPages(&ws));
      switch (ws.DoModal())
      {
         case ID_WIZFINISH:
         {
            TRACE("The wizard finished properly\n");
            AffirmThrow(pAdapter->SetParameters());
            DeleteConfigObject((CConfigObject **)(&pAdapter));

            // If we're not one of the ISDN protocols, issue a prop change to restart the driver with the new config
            if (AdapterDefaultRequiresPropChange(m_PropPageParams.DeviceInfoSet,m_PropPageParams.DeviceInfoData))
            {
               TRACE("DigiDxbCoInstaller(): About to issue DIF_PROPERTYCHANGE\n");
               SP_PROPCHANGE_PARAMS pcp = { 0 };
               pcp.ClassInstallHeader.cbSize = sizeof pcp.ClassInstallHeader;
               pcp.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
               pcp.StateChange = DICS_PROPCHANGE;
               pcp.Scope = DICS_FLAG_GLOBAL;
               if (SetupDiSetClassInstallParams(m_PropPageParams.DeviceInfoSet, m_PropPageParams.DeviceInfoData, &pcp.ClassInstallHeader, sizeof pcp))
               {
                  SetupDiCallClassInstaller( DIF_PROPERTYCHANGE, m_PropPageParams.DeviceInfoSet, m_PropPageParams.DeviceInfoData);
               }
            }

//             nResponse = IDOK;
            break;
         }

         case IDCANCEL:
         {
            DeleteConfigObject((CConfigObject **)(&pAdapter));
            TRACE("The user canceled out of the wizard!\n");
//             nResponse = IDCANCEL;
            break;
         }

         default:
            DeleteConfigObject((CConfigObject **)(&pAdapter));
            TRACE("Install wizard failed!\n");
            break;
      }

   }
   catch (CAffirmException ae)
   {
      if (NULL != pAdapter)
      {
         DeleteConfigObject((CConfigObject **)(&pAdapter));
      }
   }

   return CPropertyPageEx::OnWizardNext();
}

BOOL CHwWizDigiWizPage::OnSetActive() 
{

   CPropertySheet* psheet = (CPropertySheet*) GetParent();
   // Only enable [Next], [Back] has no meaning for us
   psheet->SetWizardButtons(PSWIZB_NEXT);
   	
	return CPropertyPageEx::OnSetActive();
}

BOOL CHwWizDigiWizPage::OnWizardFinish() 
{
   TRACE("************ CHwWizDigiWizPage::OnWizardFinish() ************\n");
   return CPropertyPageEx::OnWizardFinish();
}
