vncproperties.cpp

来自「Web VNC samples delphi」· C++ 代码 · 共 1,204 行 · 第 1/3 页

CPP
1,204
字号
//  Copyright (C) 2002-2003 Constantin Kaplinsky. All Rights Reserved.
//  Copyright (C) 2002 RealVNC Ltd. All Rights Reserved.
//  Copyright (C) 2000 Tridia Corporation. All Rights Reserved.
//  Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
//
//  This file is part of the VNC system.
//
//  The VNC system is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
//  USA.
//
// TightVNC distribution homepage on the Web: http://www.tightvnc.com/
//
// If the source code for the VNC system is not available from the place 
// whence you received this file, check http://www.uk.research.att.com/vnc or contact
// the authors on vnc@uk.research.att.com for information on obtaining it.


// vncProperties.cpp

// Implementation of the Properties dialog!

#include "stdhdrs.h"
#include "lmcons.h"
#include "vncService.h"

#include "WinVNC.h"
#include "vncProperties.h"
#include "vncServer.h"
#include "vncPasswd.h"
#include "commctrl.h"

const char NO_PASSWORD_WARN [] = "WARNING : Running WinVNC without setting a password is "
								"a dangerous security risk!\n"
								"Until you set a password, WinVNC will not accept incoming connections.";
const char NO_OVERRIDE_ERR [] = "This machine has been preconfigured with WinVNC settings, "
								"which cannot be overridden by individual users.  "
								"The preconfigured settings may be modified only by a System Administrator.";
const char NO_PASSWD_NO_OVERRIDE_ERR [] =
								"No password has been set & this machine has been "
								"preconfigured to prevent users from setting their own.\n"
								"You must contact a System Administrator to configure WinVNC properly.";
const char NO_PASSWD_NO_LOGON_WARN [] =
								"WARNING : This machine has no default password set.  WinVNC will present the "
								"Default Properties dialog now to allow one to be entered.";
const char NO_CURRENT_USER_ERR [] = "The WinVNC settings for the current user are unavailable at present.\n"
								"If you have started the service manually, please run the Service Helper as well.";
const char CANNOT_EDIT_DEFAULT_PREFS [] = "You do not have sufficient priviliges to edit the default local WinVNC settings.";

// Constructor & Destructor
vncProperties::vncProperties()
{
	m_alloweditclients = TRUE;
	m_allowproperties = TRUE;
	m_allowshutdown = TRUE;
	m_dlgvisible = FALSE;
	m_usersettings = TRUE;

	m_pMatchWindow = NULL;

	m_tab_id = 0;
	m_tab_id_restore = false;
}

vncProperties::~vncProperties()
{
}

// Initialisation
BOOL
vncProperties::Init(vncServer *server)
{
	// Save the server pointer
	m_server = server;

	if (m_pMatchWindow == NULL) {
		RECT temp;
		GetWindowRect(GetDesktopWindow(), &temp);
		m_pMatchWindow = new CMatchWindow(m_server,
										  temp.left + 5, temp.top + 5,
										  temp.right/2, temp.bottom/2);
		m_pMatchWindow->CanModify(TRUE);
	}

	// Load the settings from the registry
	Load(TRUE);

	// If incoming connections are enabled but there is no valid password set,
	// then always show a dialog.
	if (m_server->SockConnected() && !m_server->ValidPasswordsSet()) {
		if (!m_allowproperties) {
			MessageBox(NULL, NO_PASSWD_NO_OVERRIDE_ERR,
						"WinVNC Error",
						MB_OK | MB_ICONSTOP);
			PostQuitMessage(0);
		} else {
			char username[UNLEN+1];
			if (!vncService::CurrentUser(username, sizeof(username)))
				return FALSE;
			if (strcmp(username, "") == 0) {
				MessageBox(NULL, NO_PASSWD_NO_LOGON_WARN,
							"WinVNC Error",
							MB_OK | MB_ICONEXCLAMATION);
				Show(TRUE, FALSE, TRUE);
			} else {
				Show(TRUE, TRUE, TRUE);
			}
		}
	}

	return TRUE;
}

// Dialog box handling functions
void
vncProperties::Show(BOOL show, BOOL usersettings, BOOL passwordfocused)
{
	if (show)
	{
		if (!m_allowproperties)
		{
			// If the user isn't allowed to override the settings then tell them
			MessageBox(NULL, NO_OVERRIDE_ERR, "WinVNC Error", MB_OK | MB_ICONEXCLAMATION);
			return;
		}

		// Verify that we know who is logged on
		if (usersettings) {
			char username[UNLEN+1];
			if (!vncService::CurrentUser(username, sizeof(username)))
				return;
			if (strcmp(username, "") == 0) {
				MessageBox(NULL, NO_CURRENT_USER_ERR, "WinVNC Error", MB_OK | MB_ICONEXCLAMATION);
				return;
			}
		} else {
			// We're trying to edit the default local settings - verify that we can
			HKEY hkLocal, hkDefault;
			BOOL canEditDefaultPrefs = 1;
			DWORD dw;
			if (RegCreateKeyEx(HKEY_LOCAL_MACHINE,
				WINVNC_REGISTRY_KEY,
				0, REG_NONE, REG_OPTION_NON_VOLATILE,
				KEY_READ, NULL, &hkLocal, &dw) != ERROR_SUCCESS)
				canEditDefaultPrefs = 0;
			else if (RegCreateKeyEx(hkLocal,
				"Default",
				0, REG_NONE, REG_OPTION_NON_VOLATILE,
				KEY_WRITE | KEY_READ, NULL, &hkDefault, &dw) != ERROR_SUCCESS)
				canEditDefaultPrefs = 0;
			if (hkLocal) RegCloseKey(hkLocal);
			if (hkDefault) RegCloseKey(hkDefault);

			if (!canEditDefaultPrefs) {
				MessageBox(NULL, CANNOT_EDIT_DEFAULT_PREFS, "WinVNC Error", MB_OK | MB_ICONEXCLAMATION);
				return;
			}
		}

		// Now, if the dialog is not already displayed, show it!
		if (!m_dlgvisible)
		{
			if (usersettings)
				vnclog.Print(LL_INTINFO, VNCLOG("show per-user Properties\n"));
			else
				vnclog.Print(LL_INTINFO, VNCLOG("show default system Properties\n"));

			// Load in the settings relevant to the user or system
			Load(usersettings);

			m_returncode_valid = FALSE;

			m_tab_id_restore = !passwordfocused && usersettings;

			// Do the dialog box
			int result = DialogBoxParam(hAppInstance,
										MAKEINTRESOURCE(IDD_PROPERTIES_PARENT), 
										NULL, (DLGPROC)ParentDlgProc, (LONG)this);
			if (!m_returncode_valid)
			    result = IDCANCEL;

			vnclog.Print(LL_INTINFO, VNCLOG("dialog result = %d\n"), result);

			if (result == -1) {
				// Dialog box failed, so quit
				PostQuitMessage(0);
				return;
			}

			// Show a warning if incoming connections are enabled but there
			// are no valid passwords. Show a warning message in that case.
			if (m_server->SockConnected() && !m_server->ValidPasswordsSet()) {
				vnclog.Print(LL_INTERR, VNCLOG("warning - no valid passwords set\n"));
				MessageBox(NULL, NO_PASSWORD_WARN,
						   "WinVNC Warning",
						   MB_OK | MB_ICONEXCLAMATION);
			}

			// Load in all the settings
			Load(TRUE);
		}
		else
		{
			// The dialog is already displayed, just raise it to foreground.
			SetForegroundWindow(m_hDialog);
		}
	}
}

BOOL CALLBACK
vncProperties::ParentDlgProc(HWND hwnd,
						  UINT uMsg,
						  WPARAM wParam,
						  LPARAM lParam )
{
	// We use the dialog-box's USERDATA to store a _this pointer
	// This is set only once WM_INITDIALOG has been recieved, though!
	vncProperties *_this = (vncProperties *) GetWindowLong(hwnd, GWL_USERDATA);

	switch (uMsg)
	{

	case WM_INITDIALOG:
		{
			// Retrieve the Dialog box parameter and use it as a pointer
			// to the calling vncProperties object
			SetWindowLong(hwnd, GWL_USERDATA, lParam);
			vncProperties *_this = (vncProperties *) lParam;
			_this->m_hDialog = hwnd;
			_this->m_dlgvisible = TRUE;

			InitCommonControls();

			_this->m_hTab = GetDlgItem(hwnd, IDC_TAB);

			TCITEM item;
			item.mask = TCIF_TEXT; 
			item.pszText="Server";
			TabCtrl_InsertItem(_this->m_hTab, 0, &item);
			item.pszText = "Hooks";
			TabCtrl_InsertItem(_this->m_hTab, 1, &item);
			item.pszText = "Display";
			TabCtrl_InsertItem(_this->m_hTab, 2, &item);
			item.pszText = "Query";
			TabCtrl_InsertItem(_this->m_hTab, 3, &item);
			item.pszText = "Administration";
			TabCtrl_InsertItem(_this->m_hTab, 4, &item);
			int tab_id = (_this->m_tab_id_restore) ? _this->m_tab_id : 0;
			TabCtrl_SetCurSel(_this->m_hTab, tab_id);

			_this->m_hShared = CreateDialogParam(hAppInstance, 
				MAKEINTRESOURCE(IDD_SHARED_DESKTOP_AREA),
				hwnd,
				(DLGPROC)_this->SharedDlgProc,
				(LONG)_this);

			_this->m_hIncoming = CreateDialogParam(hAppInstance,
				MAKEINTRESOURCE(IDD_INCOMING),
				hwnd,
				(DLGPROC)_this->IncomingDlgProc,
				(LONG)_this);

			_this->m_hPoll = CreateDialogParam(hAppInstance, 
				MAKEINTRESOURCE(IDD_UPDATE_HANDLING),
				hwnd,
				(DLGPROC)_this->PollDlgProc,
				(LONG)_this);

			_this->m_hQuerySettings = CreateDialogParam(hAppInstance, 
				MAKEINTRESOURCE(IDD_QUERY_SETTINGS),
				hwnd,
				(DLGPROC)_this->QuerySettingsDlgProc,
				(LONG)_this);

			_this->m_hAdministration = CreateDialogParam(hAppInstance, 
				MAKEINTRESOURCE(IDD_ADMINISTRATION),
				hwnd,
				(DLGPROC)_this->AdministrationDlgProc,
				(LONG)_this);

			// Position child dialogs, to fit the Tab control's display area
			RECT rc;
			GetWindowRect(_this->m_hTab, &rc);
			MapWindowPoints(NULL, hwnd, (POINT *)&rc, 2);
			TabCtrl_AdjustRect(_this->m_hTab, FALSE, &rc);
			SetWindowPos(_this->m_hIncoming, HWND_TOP, rc.left, rc.top,
						 rc.right - rc.left, rc.bottom - rc.top,
						 (tab_id == 0) ? SWP_SHOWWINDOW : SWP_HIDEWINDOW);
			SetWindowPos(_this->m_hPoll, HWND_TOP, rc.left, rc.top,
						 rc.right - rc.left, rc.bottom - rc.top,
						 (tab_id == 1) ? SWP_SHOWWINDOW : SWP_HIDEWINDOW);
			SetWindowPos(_this->m_hShared, HWND_TOP, rc.left, rc.top,
						 rc.right - rc.left, rc.bottom - rc.top,
						 (tab_id == 2) ? SWP_SHOWWINDOW : SWP_HIDEWINDOW);
			SetWindowPos(_this->m_hQuerySettings, HWND_TOP, rc.left, rc.top,
						 rc.right - rc.left, rc.bottom - rc.top,
						 (tab_id == 3) ? SWP_SHOWWINDOW : SWP_HIDEWINDOW);
			SetWindowPos(_this->m_hAdministration, HWND_TOP, rc.left, rc.top,
						 rc.right - rc.left, rc.bottom - rc.top,
						 (tab_id == 4) ? SWP_SHOWWINDOW : SWP_HIDEWINDOW);

			// Set the dialog box's title to indicate which Properties we're editting
			if (_this->m_usersettings) {
				SetWindowText(hwnd, "TightVNC Server: Current User Properties");
			} else {
				SetWindowText(hwnd, "TightVNC Server: Default Local System Properties");
			}						

			SetForegroundWindow(hwnd);

			// If the first tab is selected, then return FALSE because in that case
			// we set the keyboard focus explicitly (on the password field).
			return (tab_id != 0);
		}
	case WM_HELP:	
		VNCHelp::Popup(lParam);
		return 0;
    case WM_NOTIFY:
		{
			LPNMHDR pn = (LPNMHDR)lParam;			
			switch (pn->idFrom) {
			case IDC_TAB:
				{
					int id = TabCtrl_GetCurSel(_this->m_hTab);
					DWORD style;
					if (pn->code == TCN_SELCHANGE) {
						style = SW_SHOW;
					} else if (pn->code == TCN_SELCHANGING) {
						style = SW_HIDE;
					} else {
						return 0;
					}
					// FIXME: Map between tab IDs and subdialogs in one place.
					const HWND subDialogList[5] = {
						_this->m_hIncoming,
						_this->m_hPoll,
						_this->m_hShared,
						_this->m_hQuerySettings,
						_this->m_hAdministration
					};
					if (id >= 5) {
						// Invalid tab ID.
						return 0;
					}
					HWND subDialog = subDialogList[id];
					ShowWindow(subDialog, style);						
					SetFocus(subDialog);
					return 0;
				}
			}
			return 0;
		}
	case WM_COMMAND:
		switch (LOWORD(wParam))
		{
		case IDOK:
		case IDC_APPLY:
			SendMessage(_this->m_hIncoming, WM_COMMAND, IDC_APPLY,0);
			SendMessage(_this->m_hPoll, WM_COMMAND, IDC_APPLY,0);
			SendMessage(_this->m_hShared, WM_COMMAND, IDC_APPLY,0);
			SendMessage(_this->m_hQuerySettings, WM_COMMAND, IDC_APPLY,0);
			SendMessage(_this->m_hAdministration, WM_COMMAND, IDC_APPLY,0);

			// Remember selected tab, except when in default settings.
			if (_this->m_usersettings)
				_this->m_tab_id = TabCtrl_GetCurFocus(_this->m_hTab);

			_this->Save();
        
			// Was ok pressed?
			if (LOWORD(wParam) == IDOK) {
        
				// Yes, so close the dialog
				vnclog.Print(LL_INTINFO, VNCLOG("enddialog (OK)\n"));

				_this->m_returncode_valid = TRUE;

				EndDialog(hwnd, IDOK);
				_this->m_dlgvisible = FALSE;
				_this->m_hTab = NULL;
			}
			return TRUE;
		case IDCANCEL:
			vnclog.Print(LL_INTINFO, VNCLOG("enddialog (CANCEL)\n"));
			_this->m_returncode_valid = TRUE;
			EndDialog(hwnd, IDCANCEL);
			_this->m_dlgvisible = FALSE;
			_this->m_hTab = NULL;
			return TRUE;		
		}
		return 0;

⌨️ 快捷键说明

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