⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ptprefs.c

📁 PGP8.0源码 请认真阅读您的文件包然后写出其具体功能
💻 C
📖 第 1 页 / 共 2 页
字号:
/*____________________________________________________________________________
	Copyright (C) 2002 PGP Corporation
	All rights reserved.

	PTprefs.c - handle PGP preferences for auto update thread
	
	derived from CLprefs.c

	$Id: PTprefs.c,v 1.3 2002/10/12 06:26:23 pbj Exp $
____________________________________________________________________________*/
#include "pgpPFLConfig.h"
#include "precomp.h"

// project header files
#include "pgpWin32IPC.h"

#include <process.h>

#include "PTprefs.h"
#include "pgpMem.h"
#include "pgpUtilities.h"
#include "pflPrefs.h"
#include "pgpFileSpec.h"
#include "pgpNetPrefs.h"
#include "pgpHashPrefs.h"
#include "pgpRandomPool.h"

#include "pgpClientPrefs.h"
#include "pgpWin32Errors.h"

#define SERVICE_MESSAGE_TIMEOUT		3000

#define PGPPT_WINDOWNAME	("PGPtray AutoUpdate Prefs Communication")

#define FILECHANGE_TIMEOUT			10000

// local globals
static PGPContextRef	s_context				= kInvalidPGPContextRef;
static PGPPrefRef		s_prefref				= kInvalidPGPPrefRef;
static PGPPrefRef		s_prefrefNet			= kInvalidPGPPrefRef;
static BOOL				s_bUsingPrefsService	= FALSE;
static BOOL				s_bNetInstalled			= FALSE;
static BOOL				s_bPGPPrefsModified		= FALSE;
static BOOL				s_bNetPrefsModified		= FALSE;
static HANDLE			s_hContactService		= NULL;
static HWND				s_hwndService			= NULL;
static HWND				s_hwndHidden			= NULL;

static INT				s_iPGPSequenceNumber	= PGP_INVALIDPREFSEQUENCE;
static INT				s_iNetSequenceNumber	= PGP_INVALIDPREFSEQUENCE;

static HANDLE			s_heventKill			= NULL;
static HANDLE			s_hthreadPrefs			= NULL;
static CHAR				s_szPGPPrefFile[MAX_PATH];
static CHAR				s_szNetPrefFile[MAX_PATH];
static FILETIME			s_filetimePGPPrefs;
static FILETIME			s_filetimeNetPrefs;


// external globals
extern HINSTANCE		g_hinst;



//	_______________________________________
//
//  Save and close down client preferences file

static PGPError
sClosePrefs (
		PGPPrefRef	prefref,
		PGPBoolean	bSave)
{
	PGPError		err		= kPGPError_NoErr;

	if (bSave)
	{
		err = PGPSavePrefFile (prefref);
	}

	if (IsPGPError (err))
		PGPFreePrefs (prefref);
	else
	{
		err = PGPFreePrefs (prefref);
	}

	return err;
}


//	____________________________________
//
//  hidden communication window message proc

static LONG APIENTRY
sPrefsServiceWindowProc (
		HWND	hwnd,
		UINT	uMsg,
		WPARAM	wParam,
		LPARAM	lParam)
{
	switch (uMsg) {
	case WM_CREATE :
	{
		LPCREATESTRUCT	lpcs = (LPCREATESTRUCT)lParam;
		s_hwndHidden = hwnd;
		SetEvent ((HANDLE)(lpcs->lpCreateParams));
		return 0;
	}

	case WM_COPYDATA :
	{
		PGPPrefsCopyDataStructure*		ppcds;
		PCOPYDATASTRUCT					pcds;
		PGPError						err;

		pcds = (PCOPYDATASTRUCT)lParam;
		ppcds = pcds->lpData;

		if (pcds->dwData & PGP_PGPPREFSDATA)
		{
			if (PGPPrefRefIsValid (s_prefref))
				PGPFreePrefs (s_prefref);

			err = PGPImportBufferToMemoryPrefs (
					PGPPeekContextMemoryMgr (s_context),
					ppcds->buffer, ppcds->bufferLength,
					clientDefaults, clientDefaultsSize, &s_prefref);
			if (IsntPGPError (err))
			{
				s_iPGPSequenceNumber = ppcds->sequenceNumber;
			}
			else
			{
				s_prefref = kInvalidPGPPrefRef;
				s_iPGPSequenceNumber = PGP_INVALIDPREFSEQUENCE;
			}
		}

		else if (pcds->dwData & PGP_PGPNETPREFSDATA)
		{
			if (PGPPrefRefIsValid (s_prefrefNet))
				PGPFreePrefs (s_prefrefNet);

			err = PGPImportBufferToMemoryPrefs (
					PGPPeekContextMemoryMgr (s_context),
					ppcds->buffer, ppcds->bufferLength,
					netDefaults, netDefaultsSize, &s_prefrefNet);
			if (IsntPGPError (err))
			{
				s_iNetSequenceNumber = ppcds->sequenceNumber;
			}
			else
			{
				s_prefrefNet = kInvalidPGPPrefRef;
				s_iNetSequenceNumber = PGP_INVALIDPREFSEQUENCE;
			}
		}

		return TRUE;
	}

	case WM_CLOSE :
		DestroyWindow (hwnd);
		break;

	case WM_DESTROY :
		PostQuitMessage (0);
		break;

	default :
		return (DefWindowProc (hwnd, uMsg, wParam, lParam));
	}

	return FALSE;
}


//	______________________________________________
//
//	thread function that handles communication with prefs service

static DWORD WINAPI
sPrefServiceCommThread (
		LPVOID		hEvent)
{
	WNDCLASS	wc;
	MSG			msg;

	// Register the Window Class
	wc.style			= 0;
	wc.lpfnWndProc		= sPrefsServiceWindowProc;
	wc.cbClsExtra		= 0;
	wc.cbWndExtra		= 0;
	wc.hInstance		= g_hinst;
	wc.hIcon			= 0;
	wc.hCursor			= 0;
	wc.hbrBackground	= 0;
	wc.lpszMenuName		= 0;
	wc.lpszClassName	= PGPPT_WINDOWNAME;

	RegisterClass (&wc);

	CreateWindow (
			PGPPT_WINDOWNAME, PGPPT_WINDOWNAME, 0,
			0, 0, 0, 0, NULL, NULL, g_hinst, hEvent);

	if (!s_hwndHidden)
		return 0;

	while (GetMessage (&msg, s_hwndHidden, 0, 0))
	{
		DispatchMessage (&msg);
	}

	return 0;
}


//	______________________________________________
//
//	determine if prefs service is running by trying to communicate

static BOOL
sLogonToPrefsService (VOID)
{
	DWORD			dw;
	COPYDATASTRUCT	cds;

	s_hwndService = FindWindow (PGP_PREFSSERVICEWINDOWNAME,
			PGP_PREFSSERVICEWINDOWNAME);

	if (s_hwndService)
	{
		HANDLE	hEvent;

		hEvent = CreateEvent (
				NULL, FALSE, FALSE, NULL);
		s_hthreadPrefs = (HANDLE)_beginthreadex (
				NULL, 0, sPrefServiceCommThread, hEvent, 0, &dw);
		if (IsntNull (s_hthreadPrefs))
		{
			WaitForSingleObject (hEvent, 10000);
			CloseHandle (hEvent);

			if (s_hwndHidden)
			{
				cds.dwData = PGP_PGPPREFSLOGON;
				cds.lpData = s_szPGPPrefFile;
				cds.cbData = sizeof(s_szPGPPrefFile);

				if (SendMessageTimeout (s_hwndService, WM_COPYDATA,
						(WPARAM)s_hwndHidden, (LPARAM)&cds,
						SMTO_NORMAL, SERVICE_MESSAGE_TIMEOUT, &dw) != 0)
				{
					if (dw == 1)
					{
						//success
						return TRUE;
					}
				}
				// if here, then there was an error.  Kill thread.
				SendMessage (s_hwndHidden, WM_CLOSE, 0, 0);
				s_hthreadPrefs = NULL;
			}
		}
	}
	return FALSE;
}


//	______________________________________________
//
//	determine if the prefs file has changed by querying the timestamp

static BOOL
sPGPPrefsFileTimeChanged (VOID)
{
	BOOL		bChanged		= FALSE;
	FILETIME	filetime;
	HANDLE		hfile;

	hfile = CreateFile (s_szPGPPrefFile, 0,
			FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
			OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if (hfile == INVALID_HANDLE_VALUE)
		memset (&s_filetimePGPPrefs, 0, sizeof(FILETIME));
	else
	{
		GetFileTime (hfile, NULL, NULL, &filetime);
		CloseHandle (hfile);
	}

	if (CompareFileTime (&filetime, &s_filetimePGPPrefs))
	{
		s_filetimePGPPrefs = filetime;
		bChanged = TRUE;
	}

	return bChanged;
}


//	______________________________________________
//
//	determine if the prefs file has changed by querying the timestamp

static BOOL
sNetPrefsFileTimeChanged (VOID)
{
	BOOL		bChanged		= FALSE;
	FILETIME	filetime;
	HANDLE		hfile;

	hfile = CreateFile (s_szNetPrefFile, 0,
			FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
			OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if (hfile == INVALID_HANDLE_VALUE)
		memset (&s_filetimeNetPrefs, 0, sizeof(FILETIME));
	else
	{
		GetFileTime (hfile, NULL, NULL, &filetime);
		CloseHandle (hfile);
	}

	if (CompareFileTime (&filetime, &s_filetimeNetPrefs))
	{
		s_filetimeNetPrefs = filetime;
		bChanged = TRUE;
	}

	return bChanged;
}


//	______________________________________________
//
//	thread function that waits for file change notification or kill
//  notification and acts accordingly

static DWORD WINAPI
sFileChangeThread (
		LPVOID		lpVoid)
{
	BOOL		bKilled		= FALSE;
	DWORD		dw;

	while (!bKilled)
	{
		dw = WaitForSingleObject (s_heventKill, FILECHANGE_TIMEOUT);

		if (dw != WAIT_TIMEOUT)
		{
			bKilled = TRUE;
		}
		else
		{
			if (s_hContactService != NULL)
			{
				if (WaitForSingleObject(s_hContactService, FILECHANGE_TIMEOUT)
					== WAIT_TIMEOUT)
				{
					bKilled = TRUE;
					break;
				}
			}

			if (sLogonToPrefsService ())
			{
				s_bUsingPrefsService = TRUE;

				s_iPGPSequenceNumber = PGP_INVALIDPREFSEQUENCE;
				s_iNetSequenceNumber = PGP_INVALIDPREFSEQUENCE;

				if (PGPPrefRefIsValid (s_prefref))
				{
					PGPFreePrefs (s_prefref);
					s_prefref = kInvalidPGPPrefRef;
				}

				if (PGPPrefRefIsValid (s_prefrefNet))
				{
					PGPFreePrefs (s_prefrefNet);
					s_prefrefNet = kInvalidPGPPrefRef;
				}

				if (s_hContactService)
					ReleaseMutex(s_hContactService);

				break;
			}

			if (s_hContactService)
				ReleaseMutex(s_hContactService);

			if (sPGPPrefsFileTimeChanged ())
			{
				s_bPGPPrefsModified = TRUE;
///				PGPclNotifyPrefsChanges (0);
			}

			if (sNetPrefsFileTimeChanged ())
			{
				s_bNetPrefsModified = TRUE;
///				PGPclNotifyNetPrefsChanges (0);
			}
		}
	}

	return 0;
}


//	______________________________________________
//
//	initialize and open up the prefs system

PGPError
PTInitPrefs (
		PGPContextRef	context)
{
	PGPError		err			= kPGPError_NoErr;

	DWORD			dwID;
	HANDLE			hfile;

	s_context			= context;
	s_prefref			= kInvalidPGPPrefRef;
	s_prefrefNet		= kInvalidPGPPrefRef;
	s_bNetInstalled		= FALSE;
	s_hthreadPrefs		= NULL;
	s_szPGPPrefFile[0]	= '\0';

	// load the file names
	PGPclGetPath (kPGPclPrefsFile, s_szPGPPrefFile, sizeof(s_szPGPPrefFile));
	PGPclGetPath (kPGPclNetPrefsFile, s_szNetPrefFile, sizeof(s_szNetPrefFile));

	if (sLogonToPrefsService ())
	{
		s_bUsingPrefsService = TRUE;
		s_bNetInstalled = PGPclIsComponentInstalled (kPGPclPGPnet);

		s_iPGPSequenceNumber = PGP_INVALIDPREFSEQUENCE;
		s_iNetSequenceNumber = PGP_INVALIDPREFSEQUENCE;

		s_prefref		= kInvalidPGPPrefRef;
		s_prefrefNet	= kInvalidPGPPrefRef;
	}
	else
	{
		s_bUsingPrefsService = FALSE;
		s_hContactService = CreateMutex (NULL, FALSE,
				"PGPtray Contact Service Mutex");

⌨️ 快捷键说明

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