📄 kmaddusr.c
字号:
/*____________________________________________________________________________
Copyright (C) 1998 Network Associates, Inc.
All rights reserved.
KMAddUsr.c - add userID to key
$Id: KMAddUsr.c,v 1.26 1998/08/12 18:31:21 pbj Exp $
____________________________________________________________________________*/
#include "pgpPFLConfig.h"
// project header files
#include "pgpkmx.h"
// pgp header files
#include "PGPClientPrefs.h"
// system header files
#include <commdlg.h>
// constant definitions
#define MAX_FULL_NAME_LEN 126
#define MAX_EMAIL_LEN 126
// typedefs
typedef struct {
PKEYMAN pKM;
LPSTR pszUserID;
PGPBoolean bSyncWithServer;
} ADDNAMESTRUCT, *PADDNAMESTRUCT;
typedef struct {
PKEYMAN pKM;
WNDPROC wpOrigPhotoIDProc;
HBITMAP hbitmapPhotoID;
HPALETTE hpalettePhotoID;
INT iwidthPhotoID;
INT iheightPhotoID;
LPBYTE pPhotoBuffer;
PGPSize iPhotoBufferLength;
PGPBoolean bSyncWithServer;
} ADDPHOTOSTRUCT, *PADDPHOTOSTRUCT;
// external globals
extern HINSTANCE g_hInst;
// local globals
static DWORD aNewUserIds[] = { // Help IDs
IDC_NEWUSERNAME, IDH_PGPKM_NEWUSERID,
IDC_NEWEMAILADDR, IDH_PGPKM_NEWEMAILADDR,
0,0
};
static DWORD aNewPhotoIds[] = { // Help IDs
IDC_PHOTOID, IDH_PGPKM_NEWPHOTOID,
IDC_SELECTFILE, IDH_PGPKM_BROWSENEWPHOTOID,
0,0
};
// ___________________________________________________
//
// 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 hDlg,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
PADDNAMESTRUCT pans;
switch(uMsg) {
case WM_INITDIALOG:
pans = (PADDNAMESTRUCT)lParam;
SetWindowLong (hDlg, GWL_USERDATA, lParam);
SendDlgItemMessage (hDlg, IDC_NEWUSERNAME, EM_SETLIMITTEXT,
MAX_FULL_NAME_LEN, 0);
SendDlgItemMessage (hDlg, IDC_NEWEMAILADDR, EM_SETLIMITTEXT,
MAX_EMAIL_LEN, 0);
return TRUE;
case WM_HELP:
pans = (PADDNAMESTRUCT)GetWindowLong (hDlg, GWL_USERDATA);
WinHelp (((LPHELPINFO) lParam)->hItemHandle, pans->pKM->szHelpFile,
HELP_WM_HELP, (DWORD) (LPSTR) aNewUserIds);
break;
case WM_CONTEXTMENU:
pans = (PADDNAMESTRUCT)GetWindowLong (hDlg, GWL_USERDATA);
WinHelp ((HWND) wParam, pans->pKM->szHelpFile, HELP_CONTEXTMENU,
(DWORD) (LPVOID) aNewUserIds);
break;
case WM_COMMAND:
switch(LOWORD (wParam)) {
case IDOK:
pans = (PADDNAMESTRUCT)GetWindowLong (hDlg, GWL_USERDATA);
if (KMConstructUserID (hDlg, IDC_NEWUSERNAME, IDC_NEWEMAILADDR,
&(pans->pszUserID))) {
EndDialog (hDlg, 1);
}
return TRUE;
case IDCANCEL:
EndDialog (hDlg, 0);
return TRUE;
}
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
KMAddUserToKey (PKEYMAN pKM)
{
PGPByte* pPasskey = NULL;
BOOL bRetVal = TRUE;
PGPError err;
PGPKeyRef key;
PGPSize sizePasskey;
ADDNAMESTRUCT ans;
CHAR sz[256];
PGPPrefRef prefref;
// get selected key
key = (PGPKeyRef)KMFocusedObject (pKM);
// initialize struct
ans.pKM = pKM;
PGPclOpenClientPrefs (PGPGetContextMemoryMgr (pKM->Context), &prefref);
PGPGetPrefBoolean (prefref, kPGPPrefKeyServerSyncOnAdd,
&(ans.bSyncWithServer));
PGPclCloseClientPrefs (prefref, FALSE);
// get new userid from user
if (DialogBoxParam (g_hInst, MAKEINTRESOURCE (IDD_NEWUSERID),
pKM->hWndParent, sAddUserDlgProc, (LPARAM)&ans)) {
// get valid passphrase, if required
LoadString (g_hInst, IDS_SELKEYPASSPHRASE, sz, sizeof(sz));
err = KMGetKeyPhrase (pKM->Context, pKM->tlsContext,
pKM->hWndParent, sz,
pKM->KeySetMain, key,
NULL, &pPasskey, &sizePasskey);
PGPclErrorBox (NULL, err);
// now we have a valid passphrase, if required
if (IsntPGPError (err)) {
// update from server
if (ans.bSyncWithServer) {
if (!KMGetFromServerInternal (pKM, FALSE, FALSE, FALSE)) {
if (KMMessageBox (pKM->hWndParent, IDS_CAPTION,
IDS_QUERYCONTINUEADDING,
MB_YESNO|MB_ICONEXCLAMATION) == IDNO) {
bRetVal = FALSE;
}
}
}
if (bRetVal) {
// make sure we have enough entropy
PGPclRandom (pKM->Context, pKM->hWndParent, 0);
if (pPasskey) {
err = PGPAddUserID (key, ans.pszUserID,
PGPOPasskeyBuffer (pKM->Context,
pPasskey, sizePasskey),
PGPOLastOption (pKM->Context));
}
else {
err = PGPAddUserID (key, ans.pszUserID,
PGPOLastOption (pKM->Context));
}
if (IsntPGPError (PGPclErrorBox (NULL, err))) {
KMCommitKeyRingChanges (pKM);
KMUpdateKeyInTree (pKM, key, FALSE);
InvalidateRect (pKM->hWndTree, NULL, TRUE);
// send to server
if (ans.bSyncWithServer) {
KMSendToServer (pKM, PGPCL_DEFAULTSERVER);
}
}
else bRetVal = FALSE;
}
}
else bRetVal = FALSE;
KMFree (ans.pszUserID);
}
else bRetVal = FALSE;
if (pPasskey) {
KMFreePasskey (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,
LOWORD(lParam), HIWORD(lParam), 0, GetParent (hWnd), NULL);
DestroyMenu (hMC);
}
break;
case WM_DROPFILES :
{
CHAR szFile[MAX_PATH];
INT iLen;
LPBITMAPINFO lpbmi;
LPBYTE pPhoto;
INT isize;
PGPError err;
HWND hwndParent;
iLen = DragQueryFile ((HDROP)wParam, 0, szFile, sizeof(szFile));
hwndParent = GetParent (hWnd);
err = KMReadPhotoFromFile (szFile, &pPhoto, &isize);
if (IsntPGPError (PGPclErrorBox (hWnd, err))) {
SetActiveWindow (GetParent (hwndParent));
if (paps->pPhotoBuffer)
KMFree (paps->pPhotoBuffer);
if (paps->hbitmapPhotoID)
DeleteObject (paps->hbitmapPhotoID);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -