ctxmenu.cpp

来自「PGP8.0源码 请认真阅读您的文件包然后写出其具体功能」· C++ 代码 · 共 1,608 行 · 第 1/3 页

CPP
1,608
字号
/*__________________________________________________________________________
 Copyright (C) 2002 PGP Corporation
 All rights reserved.

 $Id: Ctxmenu.cpp,v 1.25 2002/11/22 02:32:15 wjb Exp $
__________________________________________________________________________*/

//  MODULE:   ctxmenu.cpp
//
//  PURPOSE:   Implements the IContextMenu member
//             functions necessary to support
//             the context menu portions of this
//             shell extension.  Context menu
//             shell extensions are called when
//             the user right clicks on a file
//             (of the type registered for the
//             shell extension--see SHELLEXT.REG
//             for details on the registry entries.
//             In this sample, the relevant
//             files are of type .GAK) in the
//             Explorer, or selects the File menu
//             item.
//
#include "precomp.h"
#include "pgpWin32IPC.h"
#include "pgpClientLib.h"

extern HMENU		hPlaintextMenu;
extern HMENU		hEncryptedMenu;
extern HMENU		hKeyfileMenu;
extern HINSTANCE	g_hmodThisDll; // Handle to this DLL itself.
extern void *PGPsc;
extern void *PGPtls;

char szApp[100];

#include "resource.h"

#define NONCONCURRENT

//HWND g_hwndShell=NULL;

typedef struct _pgpthreadcontrol
{
	UINT ActionCode;
	STGMEDIUM Medium;
#ifdef NONCONCURRENT
	HANDLE hPGPCTXSemaphore;
#endif
}PGPTHREADCONTROL;

#define STACK_SIZE 0x8000
#define PGPCTX_SEMAPHORE  "PGP_50_CONTEXT_MENU_SEMAPHORE"

void CallPGPThread(void *Arg);
HRESULT CallPGP(STGMEDIUM *pMedium, UINT ActionCode);
//HWND ShellWindow(void);

extern HINSTANCE g_hmodThisDll;
extern HANDLE hPGPThreadMutex;

//
//  FUNCTION: CShellExt::QueryContextMenu(HMENU, UINT, UINT, UINT, UINT)
//
//  PURPOSE: Called by the shell just before the context menu is displayed.
//           This is where you add your specific menu items.
//
//  PARAMETERS:
//    hMenu      - Handle to the context menu
//    indexMenu  - Index of where to begin inserting menu items
//    idCmdFirst - Lowest value for new menu ID's
//    idCmtLast  - Highest value for new menu ID's
//    uFlags     - Specifies the context of the menu event
//
//  RETURN VALUE:
//
//
//  COMMENTS:
//
LRESULT CALLBACK InvisiProc(HWND hwnd, UINT msg,
							WPARAM wParam, LPARAM lParam);

PGPBoolean bInstallChecked=FALSE;
PGPBoolean bToolsInstalled=FALSE;
PGPBoolean bDiskInstalled=FALSE;

PGPBoolean
MNIsComponentInstalled (
		PGPclComponentIndex	index)
{
	PGPBoolean	bInstalled			= FALSE;
	HKEY		hkey;
	LONG		lResult;
	DWORD		dw, dwValueType, dwSize;
	CHAR		sz[128];
	CHAR		szKey[256];

	lstrcpy (szKey,PGP_REGISTRYKEY);
	lstrcat (szKey, PGP_INSTALLEDCOMPONENTSKEY);

	switch (index) {
	case kPGPclUsersManual:		lstrcpy (sz, PGP_MANUALINSTALLED);		break;
	case kPGPclPGPkeys:			lstrcpy (sz, PGP_PGPKEYSINSTALLED);		break;
	case kPGPclPFIDS:			lstrcpy (sz, PGP_PFIDSINSTALLED);		break;
	case kPGPclPGPdisk:			lstrcpy (sz, PGP_PGPDISKINSTALLED);		break;
	case kPGPclCommandLine:		lstrcpy (sz, PGP_COMMANDLINEINSTALLED);	break;
	case kPGPclICQplugin:		lstrcpy (sz, PGP_ICQINSTALLED);			break;
	case kPGPclEudoraPlugin:	lstrcpy (sz, PGP_EUDORAINSTALLED);		break;
	case kPGPclExchangePlugin:	lstrcpy (sz, PGP_EXCHANGEINSTALLED);	break;
	case kPGPclOutlookExpressPlugin:
								lstrcpy (sz, PGP_OEINSTALLED);			break;
	case kPGPclLotusNotesPlugin:lstrcpy (sz, PGP_NOTESINSTALLED);		break;
	case kPGPclPGPlogin:		lstrcpy (sz, PGP_PGPLOGININSTALLED);	break;
	case kPGPclPGPtools:		lstrcpy (sz, PGP_PGPTOOLSINSTALLED);	break;
	case kPGPclVPN:				lstrcpy (sz, PGP_VPNINSTALLED);			break;
	case kPGPclPGPnet:			lstrcpy (sz, PGP_VPNINSTALLED);			break;
	default :					return FALSE;
	}

	lResult = RegOpenKeyEx (HKEY_LOCAL_MACHINE, szKey, 0, KEY_READ, &hkey);

	if (lResult == ERROR_SUCCESS) 
	{
		// see if component is installed
		dwSize = sizeof(DWORD);
		lResult = RegQueryValueEx (
				hkey, sz, 0, &dwValueType, (LPBYTE)&dw, &dwSize);
		if (lResult == ERROR_SUCCESS)
		{
			if (dw == 1)
				bInstalled = TRUE;
		}

		if (!bInstalled && (index == kPGPclPGPnet))
		{
			lstrcpy (sz, PGP_PFIDSINSTALLED);
			dwSize = sizeof(DWORD);
			lResult = RegQueryValueEx (
					hkey, sz, 0, &dwValueType, (LPBYTE)&dw, &dwSize);
			if (lResult == ERROR_SUCCESS)
			{
				if (dw == 1)
					bInstalled = TRUE;
			}
		}

		RegCloseKey (hkey);
	}

	return bInstalled;
}


STDMETHODIMP CShellExt::QueryContextMenu(HMENU hMenu,
                                         UINT indexMenu,
                                         UINT idCmdFirst,
                                         UINT idCmdLast,
                                         UINT uFlags)
{
    ODS("CShellExt::QueryContextMenu()\r\n");

	DWORD FileAttr;
    char szMenuText[64] = "";
    char szExt[64];
	char * pExt;
	char * pSlash;
    BOOL bAppendItems=TRUE;
	int i = 0;
	BOOL bDirectory=FALSE;
	UINT nDriveType;
	char szDrive[5];
	char szDir[MAX_PATH];
	char szFname[MAX_PATH];

	if(!bInstallChecked)
	{
		bInstallChecked=TRUE;
		bToolsInstalled=MNIsComponentInstalled (kPGPclPGPtools);
		bDiskInstalled=MNIsComponentInstalled (kPGPclPGPdisk);
	}

	hParentMenu = hMenu;	// save for DeleteMenu() in Release()

	// return if we don't have a file name
	if (m_szFileUserClickedOn[0] == '\0')
        return ResultFromShort(0); //return zero items added
	// no context menu entry for folders and system files

	_splitpath(m_szFileUserClickedOn, szDrive, szDir, szFname, szExt);
	nDriveType = GetDriveType(szDrive);

	// We'll allow network drives now...(nDriveType == DRIVE_REMOTE)
//	if (nDriveType == DRIVE_CDROM) // took out CDROM filter too.... -wjb
//		return ResultFromShort(0);

	m_NumFiles = DragQueryFile((HDROP) medium.hGlobal, (UINT)-1, 0, 0);

	// We'll allow network drives now...
//	if (!strncmp(m_szFileUserClickedOn, "\\\\", 2))
//		return ResultFromShort(0);

	FileAttr = GetFileAttributes(m_szFileUserClickedOn);

	// don't fail on FileAttr == -1, PGPdisk needs to see these
	if ((FileAttr != 0xFFFFFFFF) && (FileAttr & FILE_ATTRIBUTE_DIRECTORY))
		bDirectory=TRUE;

//	if (FileAttr & FILE_ATTRIBUTE_SYSTEM)
//        return ResultFromShort(0); //return zero items added

	// get file's extension
	pExt = strrchr(m_szFileUserClickedOn, '.');
	// get last period in file name
	pSlash = strrchr(m_szFileUserClickedOn, '\\');
	// get last backslash in file name
	if (!pExt || pSlash > pExt)
	// if no period or slash follows period
		*szExt = '\0';
	else
		strcpy(szExt, &pExt[1]);		// copy file's extension w/o the period
	ODS(szExt);
	ODS("\r\n");

	if ((uFlags & 0x000F) == CMF_NORMAL)  //Check == here, since CMF_NORMAL=0
    {
        ODS("CMF_NORMAL...\r\n");
        strcpy(szMenuText, "P&GP");
    }
    else
        if (uFlags & CMF_VERBSONLY)
        {
            ODS("CMF_VERBSONLY...\r\n");
            strcpy(szMenuText, "P&GP");
        }
    else
        if (uFlags & CMF_EXPLORE)
        {
            ODS("CMF_EXPLORE...\r\n");
            strcpy(szMenuText, "P&GP");
        }
    else
        if (uFlags & CMF_DEFAULTONLY)
        {
            ODS("CMF_DEFAULTONLY...\r\n");
            bAppendItems = FALSE;
        }
    else
        {
            char szTemp[32];

            wsprintf(szTemp, "uFlags==>%d\r\n", uFlags);
            ODS("CMF_default...\r\n");
            ODS(szTemp);
            bAppendItems = FALSE;
        }

    if (bAppendItems)
    {
		int iPosition;

        if (_strnicmp(szExt, "pgp", 3) == 0
			|| _strnicmp(szExt, "asc", 3) == 0)
		{
			if(!bToolsInstalled)
				return ResultFromShort(0);

			CreateEncryptedMenu(idCmdFirst);
		}
		else
		{
			if(_strnicmp(szExt, "sig", 3) == 0)
			{
				if(!bToolsInstalled)
					return ResultFromShort(0);
				
				CreateVerifyMenu(idCmdFirst);
			}
			else
			{
				if(_strnicmp(szExt, "bexpk", 5) == 0 ||
				   _strnicmp(szExt, "aexpk", 5) == 0 ||
				   _strnicmp(szExt, "pubkr", 5) == 0 ||
				   _strnicmp(szExt, "prvkr", 5) == 0 ||
				   _strnicmp(szExt, "pkr", 3) == 0 ||
				   _strnicmp(szExt, "skr", 3) == 0)
				{
					if(!bToolsInstalled)
						return ResultFromShort(0);

					CreateKeyfileMenu(idCmdFirst);
				}
				else
				{
					BOOL	isPGPdisk;

					MNdiskIsPathPGPdisk(m_szFileUserClickedOn, &isPGPdisk,
						&m_IsMountedDisk);

					if(isPGPdisk)
					{
						if(!bDiskInstalled)
							return ResultFromShort(0);

						CreatePGPdiskMenu(idCmdFirst);
					}
					else
					{
						if(strlen(m_szFileUserClickedOn)<=3)
						{
							if(!bToolsInstalled)
								return ResultFromShort(0);

							CreateVolumeMenu(idCmdFirst);
						}
						else if(bDirectory)
						{
							if(!bToolsInstalled)
								return ResultFromShort(0);

							CreateDirectoryMenu(idCmdFirst);
						}
						else
						{
							if(!bToolsInstalled)
								return ResultFromShort(0);

							CreatePlaintextMenu(idCmdFirst);
						}
					}
				}
			}
		}

		iPosition = FindMenuItem(hMenu, "Se&nd To");

		if (iPosition == -1)
			iPosition = FindMenuItem(hMenu, "Send &To");

		if (iPosition == -1)
			iPosition = indexMenu;

		if (!InsertSubMenu(hMenu, szMenuText, &iPosition))
		{
			char EC[64];
			DWORD ErrorCode = GetLastError();
			_ltoa(ErrorCode, EC, 16);
			ODS("InsertMenu failed!\r\n");
			ODS(EC);
			ODS("\r\n");
			SubMenuItems = 0;
		}
		else
		{
			ODS("InsertMenu succeeded!\r\n");
			AddMenuSeparator(hMenu, iPosition+1);
        }

        return ResultFromShort(SubMenuItems);
   }

   return NOERROR;
}

//
//  FUNCTION: CShellExt::InvokeCommand(LPCMINVOKECOMMANDINFO)
//
//  PURPOSE: Called by the shell after the user has selected one of the
//           menu items that was added in QueryContextMenu().
//
//  PARAMETERS:
//    lpcmi - Pointer to an CMINVOKECOMMANDINFO structure
//
//  RETURN VALUE:
//
//
//  COMMENTS:
//

STDMETHODIMP CShellExt::InvokeCommand(LPCMINVOKECOMMANDINFO lpcmi)
{
	PGPTHREADCONTROL *pTC = NULL;
    ODS("CShellExt::InvokeCommand()\r\n");


	HRESULT hr = E_INVALIDARG;

    //If HIWORD(lpcmi->lpVerb) then we have been called programmatically
    //and lpVerb is a command that should be invoked.  Otherwise, the shell
    //has called us, and LOWORD(lpcmi->lpVerb) is the menu ID the user has
    //selected.  Actually, it's (menu ID - idCmdFirst) from
	//QueryContextMenu().
	if (!HIWORD(lpcmi->lpVerb))
    {
        UINT idCmd = LOWORD(lpcmi->lpVerb);

		if((pTC = (PGPTHREADCONTROL *) malloc(sizeof(PGPTHREADCONTROL))))
		{
			pTC->Medium = medium;
			pTC->ActionCode = 0;

#ifdef NONCONCURRENT
			HANDLE hPGPCTXSemaphore = NULL;
			BOOL DidCreate = FALSE;

			if(!(hPGPCTXSemaphore = OpenSemaphore(SEMAPHORE_ALL_ACCESS,
				FALSE, PGPCTX_SEMAPHORE)))
			{
				hPGPCTXSemaphore = CreateSemaphore(NULL, 0, 1,
					PGPCTX_SEMAPHORE);
				DidCreate = TRUE;
			}

			if(hPGPCTXSemaphore)
			{
				if(DidCreate || WaitForSingleObject(hPGPCTXSemaphore, 0) !=
					WAIT_TIMEOUT)
				{
					pTC->hPGPCTXSemaphore = hPGPCTXSemaphore;
#endif
					if(!strcmp(SubMenuType, "Directory"))
					{
						switch (idCmd)
						{
							case 0:
								break;

							case 1:
								pTC->ActionCode = PGP_CODE_ENCRYPT;
								hr = NOERROR;
								break;

							case 2:
								pTC->ActionCode = PGP_CODE_SIGN;
								hr = NOERROR;
								break;

							case 3:
								pTC->ActionCode = PGP_CODE_ENCRYPT_SIGN;
								hr = NOERROR;
								break;

							case 4:
								pTC->ActionCode = PGP_CODE_DECRYPT;
								hr = NOERROR;
								break;

							case 5:
								pTC->ActionCode = PGP_CODE_WIPE;
								hr = NOERROR;
								break;

							case 6:
								pTC->ActionCode = PGP_CODE_CREATE_SDA;
								hr = NOERROR;
								break;
						}
					}
					else if(!strcmp(SubMenuType, "Volume"))
					{
						switch (idCmd)
						{
							case 0:
								break;

							case 1:
								pTC->ActionCode = PGP_CODE_ENCRYPT;
								hr = NOERROR;
								break;

							case 2:
								pTC->ActionCode = PGP_CODE_SIGN;
								hr = NOERROR;
								break;

							case 3:
								pTC->ActionCode = PGP_CODE_ENCRYPT_SIGN;
								hr = NOERROR;
								break;

							case 4:
								pTC->ActionCode = PGP_CODE_DECRYPT;
								hr = NOERROR;
								break;

							case 5:
								pTC->ActionCode = PGP_CODE_WIPEFREESPACE;
								hr = NOERROR;
								break;

							case 6:
								pTC->ActionCode = PGP_CODE_CREATE_SDA;
								hr = NOERROR;
								break;
						}
					}
					else
					{
					switch (idCmd)
					{
						case 0:
							break;

						case 1:
						if (!strcmp(SubMenuType, "Encrypted"))
						{
							pTC->ActionCode = PGP_CODE_DECRYPT;
						}
						else if (!strcmp(SubMenuType, "Keyfile"))
						{
							pTC->ActionCode = PGP_CODE_ADD_KEYS;
						}
						else if (!strcmp(SubMenuType, "AddKeys"))
						{
							pTC->ActionCode = PGP_CODE_VIEW_KEYS;
						}
						else if(!strcmp(SubMenuType, "Verify"))
						{
							pTC->ActionCode = PGP_CODE_VERIFY_SIG;
						}
						else if(!strcmp(SubMenuType, "PGPdisk"))
						{
							pTC->ActionCode = PGP_CODE_MNTUNMNT_DISK;
						}
						else
						{
							pTC->ActionCode = PGP_CODE_ENCRYPT;
						}
						hr = NOERROR;
						break;

					case 2:
						if (!strcmp(SubMenuType, "Plaintext"))
						{
							pTC->ActionCode = PGP_CODE_SIGN;
						}
						else if(!strcmp(SubMenuType, "PGPdisk"))

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?