📄 cpassphrasedialog.cpp
字号:
/*____________________________________________________________________________
Copyright (C) 2002 PGP Corporation
All rights reserved.
$Id: CPassphraseDialog.cpp,v 1.23 2002/11/05 02:10:53 wjb Exp $
____________________________________________________________________________*/
#include "pgpClassesConfig.h"
#include "CArray.h"
#include "CFile.h"
#include "UMath.h"
#include "CBrush.h"
#include "CDC.h"
#include "CMessageBox.h"
#include "CPath.h"
#include "CRect.h"
#include "CWaitCursor.h"
#include "UModuleInstance.h"
#include "USecurity.h"
#include "UWinVersion.h"
#include "pgpClientErrors.h"
#include "UCommonStrings.h"
#include "CPGPclPrefs.h"
#include "CPGPdiskEngine.h"
#include "CPGPKeyDB.h"
#include "CDriveLetterCombo.h"
#include "CHelpShower.h"
#include "CPassphraseDialog.h"
#include "CPGPKeyJoiner.h"
#include "CUISubsystems.h"
#include "PGPdiskHelp.h"
#include "Resource.h"
#include "UMountPoint.h"
#include "CUTF8Edit.h"
_USING_PGP
_UNNAMED_BEGIN
// Constants
enum
{
kDLUMarginControl = 4,
kDLUMarginDialog = 7,
kBadPassStringId = 0,
kCapsLockStringId = 1,
kNumWiggles = 2,
kWiggleDelayMs = 40
};
DWORD SinglePassDlgHelpIDs[] =
{
IDC_MESSAGE_TEXT, -1,
IDC_HIDETYPE_CHECK, IDH_PGPDISKUI_SINGLEPASSDLG_HIDETYPING,
IDC_PASSPHRASE_CAPTION, IDH_PGPDISKUI_SINGLEPASSDLG_PASSPHRASE,
IDC_PASSPHRASE_EDIT, IDH_PGPDISKUI_SINGLEPASSDLG_PASSPHRASE,
IDC_JOIN_BUTTON, IDH_PGPDISKUI_SINGLEPASSDLG_JOINKEY,
IDC_OPTIONS_BUTTON, IDH_PGPDISKUI_SINGLEPASSDLG_OPTIONS,
IDOK, IDH_PGPDISKUI_SINGLEPASSDLG_OK,
IDCANCEL, IDH_PGPDISKUI_SINGLEPASSDLG_CANCEL,
0, 0
};
DWORD MountOptionsDlgHelpIDs[] =
{
IDC_DISKSTATIC1, -1,
IDC_DRIVE_RADIO, IDH_PGPDISKUI_MOUNTOPTSDLG_DRIVE,
IDC_DRIVE_COMBO, IDH_PGPDISKUI_MOUNTOPTSDLG_DRIVE,
IDC_DIR_RADIO, IDH_PGPDISKUI_MOUNTOPTSDLG_DIR,
IDC_DIR_EDIT, IDH_PGPDISKUI_MOUNTOPTSDLG_DIR,
IDC_BROWSE_BUTTON, IDH_PGPDISKUI_MOUNTOPTSDLG_BROWSEDIR,
IDC_RO_CHECK, IDH_PGPDISKUI_MOUNTOPTSDLG_ROCHECK,
IDOK, IDH_PGPDISKUI_SINGLEPASSDLG_OK,
IDCANCEL, IDH_PGPDISKUI_SINGLEPASSDLG_CANCEL,
0, 0
};
// Class CMountOptionsDialog
class CMountOptionsDialog : public CDialog
{
public:
CMountOptionsDialog() { }
~CMountOptionsDialog() { }
PGPUInt16 GetDialogId() const
{
return IDD_MOUNTOPTIONS_DLG;
}
void Display(HWND parentWnd, PGPUInt8& drive, CPath& mountPoint,
PGPBoolean& choseDrive, PGPBoolean& choseRO,
PGPBoolean forceRO = FALSE);
private:
CButton mBrowseButton;
CButton mDriveRadio;
CButton mDirRadio;
CButton mROCheck;
CDriveLetterCombo mDriveCombo;
CUTF8Edit mDirEdit;
PGPUInt8 mDrive;
CPath mMountPoint;
PGPBoolean mChoseDrive;
PGPBoolean mChoseRO;
PGPBoolean mForceRO;
void InitControls();
void OnBrowseButton();
void OnDirCheck();
void OnDriveCheck();
void OnOk();
BOOL OnCommand(PGPUInt16 notifyCode, PGPUInt16 itemId, HWND ctrl);
void OnContextMenu(HWND menuWnd, CPoint coords);
void OnHelp(HELPINFO *pInfo);
BOOL OnInitDialog(HWND focusCtrl);
};
// Class CMountOptionsDialog member functions
void
CMountOptionsDialog::Display(
HWND parentWnd,
PGPUInt8& drive,
CPath& mountPoint,
PGPBoolean& choseDrive,
PGPBoolean& choseRO,
PGPBoolean forceRO)
{
mDrive = drive;
mMountPoint = mountPoint;
mChoseDrive = choseDrive;
mChoseRO = choseRO;
mForceRO = forceRO;
CreateModal(parentWnd);
drive = mDrive;
mountPoint = mMountPoint;
choseDrive = mChoseDrive;
choseRO = mChoseRO;
}
void
CMountOptionsDialog::InitControls()
{
mDriveCombo.Fill();
mDriveCombo.Set(mDrive);
mROCheck.SetCheck(mChoseRO ? BST_CHECKED : BST_UNCHECKED);
if (mForceRO)
{
mROCheck.SetCheck(BST_CHECKED);
mROCheck.EnableWindow(FALSE);
}
if (mChoseDrive)
OnDriveCheck();
else
OnDirCheck();
if (UWinVersion::IsWin2000Compatible())
{
if (USecurity::IsRemoteSession())
{
mDriveRadio.EnableWindow(FALSE);
mDriveCombo.EnableWindow(FALSE);
mChoseDrive = FALSE;
OnDirCheck();
}
mDirEdit.SetWindowText(mMountPoint);
}
else
{
mDirRadio.EnableWindow(FALSE);
mDirEdit.EnableWindow(FALSE);
mBrowseButton.EnableWindow(FALSE);
}
}
void
CMountOptionsDialog::OnDirCheck()
{
mDriveRadio.SetCheck(BST_UNCHECKED);
mDirRadio.SetCheck(BST_CHECKED);
mDriveCombo.EnableWindow(FALSE);
mDirEdit.EnableWindow(TRUE);
mBrowseButton.EnableWindow(TRUE);
mDirEdit.SetFocus();
mDirEdit.SetSel(0, -1);
}
void
CMountOptionsDialog::OnDriveCheck()
{
mDriveRadio.SetCheck(BST_CHECKED);
mDirRadio.SetCheck(BST_UNCHECKED);
mDriveCombo.EnableWindow(TRUE);
mDirEdit.EnableWindow(FALSE);
mBrowseButton.EnableWindow(FALSE);
mDriveCombo.SetFocus();
}
void
CMountOptionsDialog::OnBrowseButton()
{
using namespace UCommonStrings;
try
{
UMountPoint::AskChooseMountPoint(Handle(),
Get(kMountOptionsMsgBoxTitle), mMountPoint);
mMountPoint.EndInSlash();
mDirEdit.SetWindowText(mMountPoint);
}
catch (CComboError& error)
{
if (error.pgpErr != kPGPError_UserAbort)
throw;
}
}
void
CMountOptionsDialog::OnOk()
{
mDrive = mDriveCombo.Get();
mDirEdit.GetWindowText(mMountPoint);
mChoseDrive = (mDriveRadio.GetCheck() == BST_CHECKED);
mChoseRO = (mROCheck.GetCheck() == BST_CHECKED);
if (!mChoseDrive &&
UMountPoint::FixWarnRoot(mMountPoint, TRUE, FALSE, Handle(),
UCommonStrings::Get(UCommonStrings::kSinglePassMsgBoxTitle)))
{
mDirEdit.SetFocus();
mDirEdit.SetSel(0, -1);
}
else
{
CDialog::OnOk();
}
}
BOOL
CMountOptionsDialog::OnCommand(
PGPUInt16 notifyCode,
PGPUInt16 itemId,
HWND ctrl)
{
switch (notifyCode)
{
case BN_CLICKED:
switch (itemId)
{
case IDC_DRIVE_RADIO:
OnDriveCheck();
break;
case IDC_DIR_RADIO:
OnDirCheck();
break;
case IDC_BROWSE_BUTTON:
OnBrowseButton();
return TRUE;
}
break;
}
return CDialog::OnCommand(notifyCode, itemId, ctrl);
}
void
CMountOptionsDialog::OnContextMenu(HWND menuWnd, CPoint coords)
{
CHelpShower::ShowContextMenu(menuWnd, MountOptionsDlgHelpIDs);
}
void
CMountOptionsDialog::OnHelp(HELPINFO *pInfo)
{
CHelpShower::ShowForControl(static_cast<HWND>(pInfo->hItemHandle),
MountOptionsDlgHelpIDs);
}
BOOL
CMountOptionsDialog::OnInitDialog(HWND focusCtrl)
{
CDialog::OnInitDialog(focusCtrl);
mBrowseButton.Subclass(GetDlgItem(IDC_BROWSE_BUTTON));
mDriveCombo.Subclass(GetDlgItem(IDC_DRIVE_COMBO));
mDriveRadio.Subclass(GetDlgItem(IDC_DRIVE_RADIO));
mDirRadio.Subclass(GetDlgItem(IDC_DIR_RADIO));
mROCheck.Subclass(GetDlgItem(IDC_RO_CHECK));
mDirEdit.Subclass(GetDlgItem(IDC_DIR_EDIT));
InitControls();
SetForegroundWindow();
return FALSE;
}
_UNNAMED_END
// Class CPassphraseDialog member functions
CPassphraseDialog::CPassphraseDialog() :
mPDisk(NULL), mPContext(NULL), mPDiskUsers(NULL), mPUser(NULL),
mPSplitKeys(NULL)
{
}
PGPUInt16
CPassphraseDialog::GetDialogId() const
{
return IDD_SINGLEPASS_DLG;
}
const CSecureString&
CPassphraseDialog::Passphrase() const
{
return mPassphraseEdit.Contents();
}
PGPBoolean
CPassphraseDialog::ReadOnly() const
{
return mChoseRO;
}
const char *
CPassphraseDialog::Root() const
{
return mRoot;
}
void
CPassphraseDialog::AskForPassphrase(
const CPGPdiskDisk& disk,
const CPGPdiskUserSet& userSet,
HWND parent,
CPGPdiskUser& matchingUser,
const char *formatText,
const char *formatSubString,
PGPBoolean showOptions,
PGPBoolean defaultRO,
PGPBoolean forceRO)
{
char localSub[kPGPMaxUserIDSize];
PGPSize usedLength;
pgpAssert(disk.IsAttached());
pgpAssert(disk.IsOpened());
pgpAssert(CWindow::IsWindow(parent));
pgpAssertStrValid(formatText);
pgpAssertStrValid(formatSubString);
CPGPdiskContext context(disk.Context());
mPDisk = &disk;
mPContext = &context;
mPDiskUsers = &userSet;
mPUser = &matchingUser;
mFormatText = formatText;
pgpUTF8StringToLocal (
0, 0, formatSubString, strlen(formatSubString),
localSub,
kPGPMaxUserIDSize, &usedLength);
mFormatSubString = localSub;
mShowOptions = showOptions;
mChoseRO = defaultRO;
mForceRO = forceRO;
if (mShowOptions)
{
mPDisk->GetDefaultRoot(mRoot);
UMountPoint::FixWarnRoot(mRoot, FALSE, TRUE);
if (mRoot.IsPlainLocalRoot() ||
!UWinVersion::IsWin2000Compatible())
{
mDrive = mRoot.GetDriveLet();
mMountPoint.Empty();
mChoseDrive = TRUE;
}
else
{
mDrive = CPath::kMaxDrives + 1;
mMountPoint = mRoot;
mChoseDrive = FALSE;
}
}
CreateModal(parent);
}
void
CPassphraseDialog::PrepareInfoBars()
{
// Create info bar 1.
CRect messageRect;
mMessageText.GetClientRect(messageRect);
CRect passRect;
mPassphraseEdit.GetWindowRect(passRect);
ScreenToClient(passRect);
CPoint origin(passRect.Left(), VertDLUToPixel(kDLUMarginDialog) +
messageRect.Height() + VertDLUToPixel(kDLUMarginControl));
PGPUInt32 width = passRect.Width();
mInfoBar1.Create(UModuleInstance::Get(), Handle(), origin, width);
mCurHeightInfoBar1 = 0;
// Create info bar 2.
CRect okRect;
mOkButton.GetWindowRect(okRect);
ScreenToClient(okRect);
origin = CPoint(okRect.Left(), passRect.Bottom() +
VertDLUToPixel(kDLUMarginControl));
width = passRect.Right() - okRect.Left();
mInfoBar2.Create(UModuleInstance::Get(), Handle(), origin, width);
}
void
CPassphraseDialog::PrepareJoinButton()
{
// Look for split keys.
CPGPdiskUserIter userIter(*mPDiskUsers);
CPGPdiskUser user;
while (userIter.Next(user))
{
if (user.IsDisabled() || user.HasSymmetricKey())
continue;
try
{
CPGPKeyDB pgpKeyDB(mPContext->PGPKeyDB());
CPGPKeyID keyID;
user.GetPGPKeyID(keyID);
CPGPKey key(pgpKeyDB, keyID);
if (key.GetBooleanProp(kPGPKeyProperty_CanDecrypt) &&
key.GetBooleanProp(kPGPKeyProperty_IsSecretShared))
{
if (IsNull(mPSplitKeys))
{
mPSplitKeys = new CPGPKeySet;
mPSplitKeys->CreateEmpty(pgpKeyDB);
}
mPSplitKeys->Add(key);
}
}
catch (CComboError&) { }
}
if (IsNull(mPSplitKeys))
mJoinButton.ShowWindow(SW_HIDE);
else if ((mPDiskUsers->Count() == 1) && (mPSplitKeys->Count() == 1))
JoinAKey();
}
void
CPassphraseDialog::PrepareDialogText()
{
mMessageText.FitFormattedTextToWindow(mFormatText, mFormatSubString);
SetWindowText(UCommonStrings::Get(
UCommonStrings::kSinglePassDialogTitle));
}
void
CPassphraseDialog::Resize()
{
PGPUInt32 heightInfoBar = mInfoBar1.Height();
if (heightInfoBar == mCurHeightInfoBar1)
return;
if (mCurHeightInfoBar1 == 0)
heightInfoBar += VertDLUToPixel(kDLUMarginControl);
PGPInt32 delta = heightInfoBar - mCurHeightInfoBar1;
CRect rect;
GetWindowRect(rect);
SetWindowPos(HWND_TOP, 0, 0, rect.Width(),
rect.Height() + delta, SWP_NOMOVE | SWP_NOZORDER);
mOkButton.GetWindowRect(rect);
ScreenToClient(rect);
mOkButton.SetWindowPos(HWND_TOP, rect.Left(),
rect.Top() + delta, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
mCancelButton.GetWindowRect(rect);
ScreenToClient(rect);
mCancelButton.SetWindowPos(HWND_TOP, rect.Left(),
rect.Top() + delta, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
mJoinButton.GetWindowRect(rect);
ScreenToClient(rect);
mJoinButton.SetWindowPos(HWND_TOP, rect.Left(),
rect.Top() + delta, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
mHideTypingCheck.GetWindowRect(rect);
ScreenToClient(rect);
mHideTypingCheck.SetWindowPos(HWND_TOP, rect.Left(),
rect.Top() + delta, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
mPassphraseEdit.GetWindowRect(rect);
ScreenToClient(rect);
mPassphraseEdit.SetWindowPos(HWND_TOP, rect.Left(),
rect.Top() + delta, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
mCaptionText.GetWindowRect(rect);
ScreenToClient(rect);
mCaptionText.SetWindowPos(HWND_TOP, rect.Left(),
rect.Top() + delta, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
mInfoBar2.GetWindowRect(rect);
ScreenToClient(rect);
mInfoBar2.SetWindowPos(HWND_TOP, rect.Left(),
rect.Top() + delta, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
mOptionsButton.GetWindowRect(rect);
ScreenToClient(rect);
mOptionsButton.SetWindowPos(HWND_TOP, rect.Left(),
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -