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

// PropPageUpdateDriver.cpp : implementation file
//

#include "stdafx.h"
#include "CustomApp.h"
#include "PropPageUpdateDriver.h"

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

/////////////////////////////////////////////////////////////////////////////
// CPropPageUpdateDriver property page

IMPLEMENT_DYNCREATE(CPropPageUpdateDriver, CPropertyPageEx)

CPropPageUpdateDriver::CPropPageUpdateDriver() : CPropertyPageEx(CPropPageUpdateDriver::IDD,0,IDS_TitleUpdateDriver,IDS_SubTitleUpdateDriver)
{

   // We need a candidate for the target dir...
   TCHAR szTmp[MAX_PATH];
   if ( 0 == GetTempPath(MAX_PATH, &szTmp[0]))
      m_TmpPath = "";
   else
      m_TmpPath = szTmp;

   // We need to know where we are...
   if ( 0 == GetCurrentDirectory(MAX_PATH, &szTmp[0]))
      m_CurrentDir = "";
   else
      m_CurrentDir = szTmp;

   // REMOVED m_DestinationDir.Format(IDS_DestinationDirDefaultForm, m_TmpPath, DXBVER_VERSION_MAJOR, DXBVER_VERSION_MINOR, DXBVER_BUILDNUMBER);
   m_DestinationDir = m_TmpPath; // This one exists...
   //{{AFX_DATA_INIT(CPropPageUpdateDriver)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}

CPropPageUpdateDriver::~CPropPageUpdateDriver()
{
}

void CPropPageUpdateDriver::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPageEx::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPropPageUpdateDriver)
	DDX_Control(pDX, IDC_STATIC_DestinationDirectory, m_ctlDestinationDirectory);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPropPageUpdateDriver, CPropertyPageEx)
	//{{AFX_MSG_MAP(CPropPageUpdateDriver)
	ON_BN_CLICKED(IDC_BUTTON_BROWSE, OnButtonBrowse)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPropPageUpdateDriver message handlers

BOOL CPropPageUpdateDriver::OnSetActive() 
{
   CPropertySheetEx* psheet = (CPropertySheetEx*) GetParent();
   // Only enable [Next], [Back] has no meaning for us
   psheet->SetWizardButtons(PSWIZB_NEXT);

   m_ctlDestinationDirectory.SetWindowText(m_DestinationDir);
	
	return CPropertyPageEx::OnSetActive();
}

LRESULT CPropPageUpdateDriver::OnWizardNext() 
{
   CWaitCursor Wait;

   if (NO_ERROR == ((CCustomAppApp *)(AfxGetApp()))->CopyDistributionFiles(m_DestinationDir))
   {
      // We update the driver version so we don't copy older files to the system
      ((CCustomAppApp *)(AfxGetApp()))->UpdateDriverVersion();
      ((CCustomAppApp *)(AfxGetApp()))->SetUpdatedDriver(TRUE);

      // Install PAM if the software is current, it will then be ready when the user installs hardware
      ((CCustomAppApp *)(AfxGetApp()))->InstallPamA();
   }

	return ((CCustomAppApp *)(AfxGetApp()))->GetPropPageFinishedIdent();
}

void CPropPageUpdateDriver::OnButtonBrowse()
{
   CString Title;
   Title.LoadString(IDS_BROWSE_TITLE);

    LPMALLOC pMalloc;
    /* Gets the Shell's default allocator */
    if (::SHGetMalloc(&pMalloc) == NOERROR)
    {
        BROWSEINFO bi;
        char pszBuffer[MAX_PATH];
        LPITEMIDLIST pidl;
        // Get help on BROWSEINFO struct - it's got all the bit settings.
        bi.hwndOwner = GetSafeHwnd();
        bi.pidlRoot = NULL;
        bi.pszDisplayName = pszBuffer;
        bi.lpszTitle = Title; // _T("Select a Starting Directory");
        bi.ulFlags = BIF_USENEWUI | BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS | BIF_DONTGOBELOWDOMAIN;
        bi.lpfn = NULL;
        bi.lParam = 0;
        // This next call issues the dialog box.
        if ((pidl = ::SHBrowseForFolder(&bi)) != NULL)
        {
            if (::SHGetPathFromIDList(pidl, pszBuffer))
            {
               m_DestinationDir = pszBuffer;
               m_ctlDestinationDirectory.SetWindowText(m_DestinationDir);
            }
            // Free the PIDL allocated by SHBrowseForFolder.
            pMalloc->Free(pidl);
        }
        // Release the shell's allocator.
        pMalloc->Release();
    }
}
