File: CAdvancedOptions.cpp

package info (click to toggle)
synergy 1.3.1-5
  • links: PTS, VCS
  • area: main
  • in suites: lenny, squeeze
  • size: 4,664 kB
  • ctags: 5,482
  • sloc: cpp: 46,292; sh: 3,392; makefile: 938; ansic: 82
file content (269 lines) | stat: -rw-r--r-- 5,885 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 * synergy -- mouse and keyboard sharing utility
 * Copyright (C) 2002 Chris Schoeneman
 * 
 * This package is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * found in the file COPYING that should have accompanied this file.
 * 
 * This package 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.
 */

#include "CConfig.h"
#include "ProtocolTypes.h"
#include "CStringUtil.h"
#include "CArch.h"
#include "CArchMiscWindows.h"
#include "CAdvancedOptions.h"
#include "LaunchUtil.h"
#include "XArch.h"
#include "resource.h"

//
// CAdvancedOptions
//

CAdvancedOptions*		CAdvancedOptions::s_singleton = NULL;

CAdvancedOptions::CAdvancedOptions(HWND parent, CConfig* config) :
	m_parent(parent),
	m_config(config),
	m_isClient(false),
	m_screenName(ARCH->getHostName()),
	m_port(kDefaultPort),
	m_interface()
{
	assert(s_singleton == NULL);
	s_singleton = this;
	init();
}

CAdvancedOptions::~CAdvancedOptions()
{
	s_singleton = NULL;
}

void
CAdvancedOptions::doModal(bool isClient)
{
	// save state
	m_isClient = isClient;

	// do dialog
	DialogBoxParam(s_instance, MAKEINTRESOURCE(IDD_ADVANCED_OPTIONS),
								m_parent, dlgProc, (LPARAM)this);
}

CString
CAdvancedOptions::getScreenName() const
{
	return m_screenName;
}

int
CAdvancedOptions::getPort() const
{
	return m_port;
}

CString
CAdvancedOptions::getInterface() const
{
	return m_interface;
}

CString
CAdvancedOptions::getCommandLine(bool isClient, const CString& serverName) const
{
	CString cmdLine;

	// screen name
	if (!m_screenName.empty()) {
		cmdLine += " --name ";
		cmdLine += m_screenName;
	}

	// port
	char portString[20];
	sprintf(portString, "%d", m_port);
	if (isClient) {
		cmdLine += " ";
		cmdLine += serverName;
		cmdLine += ":";
		cmdLine += portString;
	}
	else {
		cmdLine += " --address ";
		if (!m_interface.empty()) {
			cmdLine += m_interface;
		}
		cmdLine += ":";
		cmdLine += portString;
	}

	return cmdLine;
}

void
CAdvancedOptions::init()
{
	// get values from registry
	HKEY key = CArchMiscWindows::openKey(HKEY_CURRENT_USER, getSettingsPath());
	if (key != NULL) {
		DWORD newPort        = CArchMiscWindows::readValueInt(key, "port");
		CString newName      = CArchMiscWindows::readValueString(key, "name");
		CString newInterface =
			CArchMiscWindows::readValueString(key, "interface");
		if (newPort != 0) {
			m_port = static_cast<int>(newPort);
		}
		if (!newName.empty()) {
			m_screenName = newName;
		}
		if (!newInterface.empty()) {
			m_interface = newInterface;
		}
		CArchMiscWindows::closeKey(key);
	}
}

void
CAdvancedOptions::doInit(HWND hwnd)
{
	// set values in GUI
	HWND child;
	char buffer[20];
	sprintf(buffer, "%d", m_port);
	child = getItem(hwnd, IDC_ADVANCED_PORT_EDIT);
	SendMessage(child, WM_SETTEXT, 0, (LPARAM)buffer);

	child = getItem(hwnd, IDC_ADVANCED_NAME_EDIT);
	SendMessage(child, WM_SETTEXT, 0, (LPARAM)m_screenName.c_str());

	child = getItem(hwnd, IDC_ADVANCED_INTERFACE_EDIT);
	SendMessage(child, WM_SETTEXT, 0, (LPARAM)m_interface.c_str());
}

bool
CAdvancedOptions::save(HWND hwnd)
{
	SetCursor(LoadCursor(NULL, IDC_WAIT));

	HWND child = getItem(hwnd, IDC_ADVANCED_NAME_EDIT);
	CString name = getWindowText(child);
	if (!m_config->isValidScreenName(name)) {
		showError(hwnd, CStringUtil::format(
								getString(IDS_INVALID_SCREEN_NAME).c_str(),
								name.c_str()));
		SetFocus(child);
		return false;
	}
	if (!m_isClient && !m_config->isScreen(name)) {
		showError(hwnd, CStringUtil::format(
								getString(IDS_UNKNOWN_SCREEN_NAME).c_str(),
								name.c_str()));
		SetFocus(child);
		return false;
	}

	child = getItem(hwnd, IDC_ADVANCED_INTERFACE_EDIT);
	CString iface = getWindowText(child);
	if (!m_isClient) {
		try {
			if (!iface.empty()) {
				ARCH->nameToAddr(iface);
			}
		}
		catch (XArchNetworkName& e) {
			showError(hwnd, CStringUtil::format(
								getString(IDS_INVALID_INTERFACE_NAME).c_str(),
								iface.c_str(), e.what().c_str()));
			SetFocus(child);
			return false;
		}
	}

	// get and verify port
	child = getItem(hwnd, IDC_ADVANCED_PORT_EDIT);
	CString portString = getWindowText(child);
	int port = atoi(portString.c_str());
	if (port < 1 || port > 65535) {
		CString defaultPortString = CStringUtil::print("%d", kDefaultPort);
		showError(hwnd, CStringUtil::format(
								getString(IDS_INVALID_PORT).c_str(),
								portString.c_str(),
								defaultPortString.c_str()));
		SetFocus(child);
		return false;
	}

	// save state
	m_screenName = name;
	m_port       = port;
	m_interface  = iface;

	// save values to registry
	HKEY key = CArchMiscWindows::addKey(HKEY_CURRENT_USER, getSettingsPath());
	if (key != NULL) {
		CArchMiscWindows::setValue(key, "port", m_port);
		CArchMiscWindows::setValue(key, "name", m_screenName);
		CArchMiscWindows::setValue(key, "interface", m_interface);
		CArchMiscWindows::closeKey(key);
	}

	return true;
}

void
CAdvancedOptions::setDefaults(HWND hwnd)
{
	// restore defaults
	m_screenName = ARCH->getHostName();
	m_port       = kDefaultPort;
	m_interface  = "";

	// update GUI
	doInit(hwnd);
}

BOOL
CAdvancedOptions::doDlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM)
{
	switch (message) {
	case WM_INITDIALOG:
		doInit(hwnd);
		return TRUE;

	case WM_COMMAND:
		switch (LOWORD(wParam)) {
		case IDOK:
			if (save(hwnd)) {
				EndDialog(hwnd, 0);
			}
			return TRUE;

		case IDCANCEL:
			EndDialog(hwnd, 0);
			return TRUE;

		case IDC_ADVANCED_DEFAULTS:
			setDefaults(hwnd);
			return TRUE;
		}
		break;

	default:
		break;
	}

	return FALSE;
}

BOOL CALLBACK
CAdvancedOptions::dlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	return s_singleton->doDlgProc(hwnd, message, wParam, lParam);
}