📄 pkaddid.c
字号:
/*____________________________________________________________________________
Copyright (C) 2000 Networks Associates Technology, Inc.
All rights reserved.
PKAddID.c - add user and/or photo ID to key
$Id: PKAddID.c,v 1.21 2002/11/22 03:15:51 pbj Exp $
____________________________________________________________________________*/
#include "pgpPFLConfig.h"
// project header files
#include "PGPkeysx.h"
#include "UTF8Edit.h"
// system header files
#include <commdlg.h>
// constant definitions
#define MAX_FULL_NAME_LEN 126
#define MAX_EMAIL_LEN 126
// typedefs
typedef struct {
PPGPKEYSSTRUCT ppks;
LPSTR pszUserID;
PGPBoolean bExchangeUser;
PGPBoolean bSyncWithServer;
} ADDNAMESTRUCT, *PADDNAMESTRUCT;
typedef struct {
PPGPKEYSSTRUCT ppks;
WNDPROC wpOrigPhotoIDProc;
HBITMAP hbitmapPhotoID;
HPALETTE hpalettePhotoID;
INT iwidthPhotoID;
INT iheightPhotoID;
LPBYTE pPhotoBuffer;
PGPSize iPhotoBufferLength;
PGPBoolean bSyncWithServer;
} ADDPHOTOSTRUCT, *PADDPHOTOSTRUCT;
// external globals
extern HINSTANCE g_hinst;
extern PGPContextRef g_context;
extern PGPtlsContextRef g_tlscontext;
// local globals
static DWORD aNewUserIds[] = { // Help IDs
IDC_NEWUSERNAME, IDH_PGPPK_NEWUSERID,
IDC_NEWEMAILADDR, IDH_PGPPK_NEWEMAILADDR,
IDC_EXCHANGEID, IDH_PGPPK_USEEXCHANGEID,
0,0
};
static DWORD aNewPhotoIds[] = { // Help IDs
IDC_PHOTOID, IDH_PGPPK_NEWPHOTOID,
IDC_SELECTFILE, IDH_PGPPK_BROWSENEWPHOTOID,
0,0
};
// ____________________________________
//
// try to get the user's MS Exchange name and email address
static VOID
sLookupExchangeUserID (
HWND hwnd)
{
CHAR szName[128];
CHAR szAddr[128];
PGPError err;
err = PGPclGetExchangeServerAddress (
hwnd, TRUE, szName, sizeof(szName), szAddr, sizeof(szAddr));
if (err == kPGPError_FeatureNotAvailable)
{
PKMessageBox (hwnd, IDS_CAPTIONINFO, IDS_EXCHANGEPROMPT,
MB_OK|MB_ICONINFORMATION);
err = PGPclGetExchangeServerAddress (
hwnd, FALSE, szName, sizeof(szName), szAddr, sizeof(szAddr));
}
if (IsPGPError (err))
{
PKMessageBox (hwnd, IDS_CAPTIONALERT, IDS_EXCHANGEERROR,
MB_OK|MB_ICONEXCLAMATION);
}
else
{
UTF8EditSetText (GetDlgItem (hwnd, IDC_NEWUSERNAME), szName);
UTF8EditSetText (GetDlgItem (hwnd, IDC_NEWEMAILADDR), szAddr);
}
}
// ___________________________________________________
//
// Dialog Message procedure
// When user asks to add a userID to a key, a dialog
// appears asking for the new userID to be typed in.
// This is the message processing procedure for that
// dialog.
static BOOL CALLBACK
sAddUserDlgProc (
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
PADDNAMESTRUCT pans;
switch(uMsg) {
case WM_INITDIALOG:
pans = (PADDNAMESTRUCT)lParam;
SetWindowLong (hwnd, GWL_USERDATA, lParam);
UTF8EditInit (GetDlgItem (hwnd, IDC_NEWUSERNAME));
SendMessage (UTF8EditGetHandle (GetDlgItem (hwnd, IDC_NEWUSERNAME)),
EM_SETLIMITTEXT, MAX_FULL_NAME_LEN, 0);
UTF8EditInit (GetDlgItem (hwnd, IDC_NEWEMAILADDR));
SendMessage (UTF8EditGetHandle (GetDlgItem (hwnd, IDC_NEWEMAILADDR)),
EM_SETLIMITTEXT, MAX_EMAIL_LEN, 0);
if (pans->bExchangeUser)
{
EnableWindow (GetDlgItem (hwnd, IDC_EXCHANGEID), TRUE);
ShowWindow (GetDlgItem (hwnd, IDC_EXCHANGEID), SW_SHOW);
}
return TRUE;
case WM_HELP :
case WM_CONTEXTMENU :
return PGPclHtmlHelp (hwnd, uMsg, wParam, lParam,
(char*)kPGPclHelpFile, aNewUserIds);
case WM_DESTROY :
UTF8EditDestroy (GetDlgItem (hwnd, IDC_NEWUSERNAME));
UTF8EditDestroy (GetDlgItem (hwnd, IDC_NEWEMAILADDR));
return 0;
case WM_COMMAND :
switch(LOWORD (wParam)) {
case IDOK:
pans = (PADDNAMESTRUCT)GetWindowLong (hwnd, GWL_USERDATA);
if (PKConstructUserID (hwnd, IDC_NEWUSERNAME, IDC_NEWEMAILADDR,
&(pans->pszUserID)))
{
EndDialog (hwnd, 1);
}
return TRUE;
case IDCANCEL:
EndDialog (hwnd, 0);
return TRUE;
case IDC_EXCHANGEID :
sLookupExchangeUserID (hwnd);
break;
}
return TRUE;
}
return FALSE;
}
// ___________________________________________________
//
// Add User to key
// This routine is called when the user chooses to add
// a userID to an existing secret key.
BOOL
PKAddUserToKey (
PGPKEYSSTRUCT* ppks)
{
PGPPrefRef prefref = kInvalidPGPPrefRef;
PGPByte* pPasskey = NULL;
BOOL bRetVal = TRUE;
PGPError err;
PGPKeyDBObjRef key;
PGPSize sizePasskey;
ADDNAMESTRUCT ans;
CHAR sz[256];
// initialize struct
ans.ppks = ppks;
if (IsPGPError (PGPclPeekClientLibPrefRefs (&prefref, NULL)))
return FALSE;
PGPGetPrefBoolean (prefref,
kPGPPrefKeyServerSyncOnAdd, &(ans.bSyncWithServer));
ans.bExchangeUser = PGPclIsExchangeServerUser ();
// get selected key
PGPclKeyListGetSelectionInfo (ppks->hKL, NULL, &key, NULL, NULL);
// get new userid from user
if (DialogBoxParam (g_hinst, MAKEINTRESOURCE (IDD_NEWUSERID),
ppks->hwndMain, sAddUserDlgProc, (LPARAM)&ans))
{
// get valid passphrase, if required
LoadString (g_hinst, IDS_SELKEYPASSPHRASE, sz, sizeof(sz));
err = PGPclGetKeyPhrase (g_context, g_tlscontext,
ppks->hwndMain, sz, ppks->keydbMain, key,
NULL, &pPasskey, &sizePasskey);
PGPclErrorBox (ppks->hwndMain, err);
// now we have a valid passphrase, if required
if (IsntPGPError (err))
{
// update from server
if (ans.bSyncWithServer)
{
if (!PKGetFromServerInternal (ppks, FALSE, FALSE, FALSE))
{
if (PKMessageBox (ppks->hwndMain, IDS_CAPTIONINFO,
IDS_QUERYCONTINUEADDING,
MB_YESNO|MB_ICONQUESTION) == IDNO)
{
bRetVal = FALSE;
}
}
}
if (bRetVal)
{
// make sure we have enough entropy
PGPclRandom (g_context, ppks->hwndMain, 0);
if (pPasskey)
{
err = PGPAddUserID (key, ans.pszUserID,
PGPOPasskeyBuffer (g_context,
pPasskey, sizePasskey),
PGPOLastOption (g_context));
}
else
{
err = PGPAddUserID (key, ans.pszUserID,
PGPOLastOption (g_context));
}
if (IsntPGPError (PGPclErrorBox (ppks->hwndMain, err)))
{
PKKeyDBModified (ppks, PK_MOD_INDEX_0);
PGPclKeyListUpdateTree (ppks->hKL,
kPGPclKeyListUpdateObject, key, FALSE);
PGPclKeyListSetTree (ppks->hKL,
kPGPclSelectSpecifiedOnly, key);
// send to server
if (ans.bSyncWithServer)
PKSendToServer (ppks, kPGPclDefaultServer);
}
else
bRetVal = FALSE;
}
}
else
bRetVal = FALSE;
pkFree (ans.pszUserID);
}
else
bRetVal = FALSE;
if (pPasskey)
{
PGPclFreePasskey (pPasskey, sizePasskey);
pPasskey = NULL;
}
return bRetVal;
}
// ___________________________________________________
//
// display photo userID with appropriate overwriting
static VOID
sPaintPhotoID (
HWND hwnd,
HBITMAP hbitmapID,
HPALETTE hpaletteID,
INT iwidthBM,
INT iheightBM)
{
HPALETTE hpaletteOld = NULL;
HDC hdc;
HDC hdcMem;
PAINTSTRUCT ps;
RECT rc;
INT icent;
INT ileft, itop, iwidth, iheight;
hdc = BeginPaint (hwnd, &ps);
if (hbitmapID) {
GetWindowRect (GetDlgItem (hwnd, IDC_PHOTOID), &rc);
MapWindowPoints (NULL, hwnd, (LPPOINT)&rc, 2);
// check if bitmap needs shrinking
if ((iheightBM > (rc.bottom-rc.top-2)) ||
(iwidthBM > (rc.right-rc.left-2)))
{
if (iheightBM > (iwidthBM * 1.25))
{
itop = rc.top +1;
iheight = rc.bottom-rc.top -2;
icent = (rc.right+rc.left) / 2;
iwidth = ((rc.bottom-rc.top) * iwidthBM) / iheightBM;
ileft = icent -(iwidth/2);
}
else
{
ileft = rc.left +1;
iwidth = rc.right-rc.left -2;
icent = (rc.bottom+rc.top) / 2;
iheight = ((rc.right-rc.left) * iheightBM) / iwidthBM;
itop = icent - (iheight/2);
}
}
// otherwise draw it at its real size
else
{
iwidth = iwidthBM;
iheight = iheightBM;
icent = (rc.right+rc.left) / 2;
ileft = icent - (iwidth/2);
icent = (rc.bottom+rc.top) / 2;
itop = icent - (iheight/2);
}
hdcMem = CreateCompatibleDC (hdc);
if (hpaletteID)
{
hpaletteOld = SelectPalette (hdc, hpaletteID, FALSE);
RealizePalette (hdc);
}
SetStretchBltMode (hdc, COLORONCOLOR);
SelectObject (hdcMem, hbitmapID);
StretchBlt (hdc, ileft, itop, iwidth, iheight,
hdcMem, 0, 0, iwidthBM, iheightBM, SRCCOPY);
if (hpaletteOld)
{
SelectPalette (hdc, hpaletteOld, TRUE);
RealizePalette (hdc);
}
DeleteDC (hdcMem);
}
EndPaint (hwnd, &ps);
}
// ___________________________________________________
//
// update system palette
static BOOL
sUpdatePalette (
HWND hwnd,
PADDPHOTOSTRUCT paps)
{
BOOL bretval = FALSE;
HDC hdc;
HPALETTE hpaletteOld;
if (paps->hpalettePhotoID == NULL)
return FALSE;
hdc = GetDC (hwnd);
hpaletteOld = SelectPalette (hdc, paps->hpalettePhotoID, FALSE);
if (RealizePalette (hdc))
{
InvalidateRect (hwnd, NULL, TRUE);
bretval = TRUE;
}
SelectPalette (hdc, hpaletteOld, TRUE);
RealizePalette (hdc);
ReleaseDC (hwnd, hdc);
return bretval;
}
// ___________________________________________________
//
// PhotoID subclass procedure
static LRESULT APIENTRY
sPhotoIDSubclassProc (
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
PADDPHOTOSTRUCT paps;
paps = (PADDPHOTOSTRUCT)GetWindowLong (GetParent (hwnd), GWL_USERDATA);
switch (uMsg) {
case WM_CONTEXTMENU :
{
HMENU hMC;
HMENU hMenuTrackPopup;
hMC = LoadMenu (g_hinst, MAKEINTRESOURCE (IDR_MENUNEWPHOTOID));
if (paps->hbitmapPhotoID)
EnableMenuItem (hMC, IDM_COPYBITMAP, MF_BYCOMMAND|MF_ENABLED);
else
EnableMenuItem (hMC, IDM_COPYBITMAP, MF_BYCOMMAND|MF_GRAYED);
if (IsClipboardFormatAvailable (CF_BITMAP) ||
IsClipboardFormatAvailable (CF_HDROP))
{
EnableMenuItem (hMC, IDM_PASTEBITMAP, MF_BYCOMMAND|MF_ENABLED);
}
else
EnableMenuItem (hMC, IDM_PASTEBITMAP, MF_BYCOMMAND|MF_GRAYED);
hMenuTrackPopup = GetSubMenu (hMC, 0);
TrackPopupMenu (hMenuTrackPopup, TPM_LEFTALIGN|TPM_RIGHTBUTTON,
(SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam),
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -