| 1 | /* | 
|---|
| 2 | * synergy -- mouse and keyboard sharing utility | 
|---|
| 3 | * Copyright (C) 2002 Chris Schoeneman | 
|---|
| 4 | * | 
|---|
| 5 | * This package is free software; you can redistribute it and/or | 
|---|
| 6 | * modify it under the terms of the GNU General Public License | 
|---|
| 7 | * found in the file COPYING that should have accompanied this file. | 
|---|
| 8 | * | 
|---|
| 9 | * This package is distributed in the hope that it will be useful, | 
|---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 12 | * GNU General Public License for more details. | 
|---|
| 13 | */ | 
|---|
| 14 |  | 
|---|
| 15 | #include "CConfig.h" | 
|---|
| 16 | #include "ProtocolTypes.h" | 
|---|
| 17 | #include "CStringUtil.h" | 
|---|
| 18 | #include "CArch.h" | 
|---|
| 19 | #include "CArchMiscWindows.h" | 
|---|
| 20 | #include "CAdvancedOptions.h" | 
|---|
| 21 | #include "LaunchUtil.h" | 
|---|
| 22 | #include "XArch.h" | 
|---|
| 23 | #include "resource.h" | 
|---|
| 24 |  | 
|---|
| 25 | // | 
|---|
| 26 | // CAdvancedOptions | 
|---|
| 27 | // | 
|---|
| 28 |  | 
|---|
| 29 | CAdvancedOptions*               CAdvancedOptions::s_singleton = NULL; | 
|---|
| 30 |  | 
|---|
| 31 | CAdvancedOptions::CAdvancedOptions(HWND parent, CConfig* config) : | 
|---|
| 32 | m_parent(parent), | 
|---|
| 33 | m_config(config), | 
|---|
| 34 | m_isClient(false), | 
|---|
| 35 | m_screenName(ARCH->getHostName()), | 
|---|
| 36 | m_port(kDefaultPort), | 
|---|
| 37 | m_interface() | 
|---|
| 38 | { | 
|---|
| 39 | assert(s_singleton == NULL); | 
|---|
| 40 | s_singleton = this; | 
|---|
| 41 | init(); | 
|---|
| 42 | } | 
|---|
| 43 |  | 
|---|
| 44 | CAdvancedOptions::~CAdvancedOptions() | 
|---|
| 45 | { | 
|---|
| 46 | s_singleton = NULL; | 
|---|
| 47 | } | 
|---|
| 48 |  | 
|---|
| 49 | void | 
|---|
| 50 | CAdvancedOptions::doModal(bool isClient) | 
|---|
| 51 | { | 
|---|
| 52 | // save state | 
|---|
| 53 | m_isClient = isClient; | 
|---|
| 54 |  | 
|---|
| 55 | // do dialog | 
|---|
| 56 | DialogBoxParam(s_instance, MAKEINTRESOURCE(IDD_ADVANCED_OPTIONS), | 
|---|
| 57 | m_parent, dlgProc, (LPARAM)this); | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 | CString | 
|---|
| 61 | CAdvancedOptions::getScreenName() const | 
|---|
| 62 | { | 
|---|
| 63 | return m_screenName; | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 | int | 
|---|
| 67 | CAdvancedOptions::getPort() const | 
|---|
| 68 | { | 
|---|
| 69 | return m_port; | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | CString | 
|---|
| 73 | CAdvancedOptions::getInterface() const | 
|---|
| 74 | { | 
|---|
| 75 | return m_interface; | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | CString | 
|---|
| 79 | CAdvancedOptions::getCommandLine(bool isClient, const CString& serverName) const | 
|---|
| 80 | { | 
|---|
| 81 | CString cmdLine; | 
|---|
| 82 |  | 
|---|
| 83 | // screen name | 
|---|
| 84 | if (!m_screenName.empty()) { | 
|---|
| 85 | cmdLine += " --name "; | 
|---|
| 86 | cmdLine += m_screenName; | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | // port | 
|---|
| 90 | char portString[20]; | 
|---|
| 91 | sprintf(portString, "%d", m_port); | 
|---|
| 92 | if (isClient) { | 
|---|
| 93 | cmdLine += " "; | 
|---|
| 94 | cmdLine += serverName; | 
|---|
| 95 | cmdLine += ":"; | 
|---|
| 96 | cmdLine += portString; | 
|---|
| 97 | } | 
|---|
| 98 | else { | 
|---|
| 99 | cmdLine += " --address "; | 
|---|
| 100 | if (!m_interface.empty()) { | 
|---|
| 101 | cmdLine += m_interface; | 
|---|
| 102 | } | 
|---|
| 103 | cmdLine += ":"; | 
|---|
| 104 | cmdLine += portString; | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | return cmdLine; | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 | void | 
|---|
| 111 | CAdvancedOptions::init() | 
|---|
| 112 | { | 
|---|
| 113 | // get values from registry | 
|---|
| 114 | HKEY key = CArchMiscWindows::openKey(HKEY_CURRENT_USER, getSettingsPath()); | 
|---|
| 115 | if (key != NULL) { | 
|---|
| 116 | DWORD newPort        = CArchMiscWindows::readValueInt(key, "port"); | 
|---|
| 117 | CString newName      = CArchMiscWindows::readValueString(key, "name"); | 
|---|
| 118 | CString newInterface = | 
|---|
| 119 | CArchMiscWindows::readValueString(key, "interface"); | 
|---|
| 120 | if (newPort != 0) { | 
|---|
| 121 | m_port = static_cast<int>(newPort); | 
|---|
| 122 | } | 
|---|
| 123 | if (!newName.empty()) { | 
|---|
| 124 | m_screenName = newName; | 
|---|
| 125 | } | 
|---|
| 126 | if (!newInterface.empty()) { | 
|---|
| 127 | m_interface = newInterface; | 
|---|
| 128 | } | 
|---|
| 129 | CArchMiscWindows::closeKey(key); | 
|---|
| 130 | } | 
|---|
| 131 | } | 
|---|
| 132 |  | 
|---|
| 133 | void | 
|---|
| 134 | CAdvancedOptions::doInit(HWND hwnd) | 
|---|
| 135 | { | 
|---|
| 136 | // set values in GUI | 
|---|
| 137 | HWND child; | 
|---|
| 138 | char buffer[20]; | 
|---|
| 139 | sprintf(buffer, "%d", m_port); | 
|---|
| 140 | child = getItem(hwnd, IDC_ADVANCED_PORT_EDIT); | 
|---|
| 141 | SendMessage(child, WM_SETTEXT, 0, (LPARAM)buffer); | 
|---|
| 142 |  | 
|---|
| 143 | child = getItem(hwnd, IDC_ADVANCED_NAME_EDIT); | 
|---|
| 144 | SendMessage(child, WM_SETTEXT, 0, (LPARAM)m_screenName.c_str()); | 
|---|
| 145 |  | 
|---|
| 146 | child = getItem(hwnd, IDC_ADVANCED_INTERFACE_EDIT); | 
|---|
| 147 | SendMessage(child, WM_SETTEXT, 0, (LPARAM)m_interface.c_str()); | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | bool | 
|---|
| 151 | CAdvancedOptions::save(HWND hwnd) | 
|---|
| 152 | { | 
|---|
| 153 | SetCursor(LoadCursor(NULL, IDC_WAIT)); | 
|---|
| 154 |  | 
|---|
| 155 | HWND child = getItem(hwnd, IDC_ADVANCED_NAME_EDIT); | 
|---|
| 156 | CString name = getWindowText(child); | 
|---|
| 157 | if (!m_config->isValidScreenName(name)) { | 
|---|
| 158 | showError(hwnd, CStringUtil::format( | 
|---|
| 159 | getString(IDS_INVALID_SCREEN_NAME).c_str(), | 
|---|
| 160 | name.c_str())); | 
|---|
| 161 | SetFocus(child); | 
|---|
| 162 | return false; | 
|---|
| 163 | } | 
|---|
| 164 | if (!m_isClient && !m_config->isScreen(name)) { | 
|---|
| 165 | showError(hwnd, CStringUtil::format( | 
|---|
| 166 | getString(IDS_UNKNOWN_SCREEN_NAME).c_str(), | 
|---|
| 167 | name.c_str())); | 
|---|
| 168 | SetFocus(child); | 
|---|
| 169 | return false; | 
|---|
| 170 | } | 
|---|
| 171 |  | 
|---|
| 172 | child = getItem(hwnd, IDC_ADVANCED_INTERFACE_EDIT); | 
|---|
| 173 | CString iface = getWindowText(child); | 
|---|
| 174 | if (!m_isClient) { | 
|---|
| 175 | try { | 
|---|
| 176 | if (!iface.empty()) { | 
|---|
| 177 | ARCH->nameToAddr(iface); | 
|---|
| 178 | } | 
|---|
| 179 | } | 
|---|
| 180 | catch (XArchNetworkName& e) { | 
|---|
| 181 | showError(hwnd, CStringUtil::format( | 
|---|
| 182 | getString(IDS_INVALID_INTERFACE_NAME).c_str(), | 
|---|
| 183 | iface.c_str(), e.what().c_str())); | 
|---|
| 184 | SetFocus(child); | 
|---|
| 185 | return false; | 
|---|
| 186 | } | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 | // get and verify port | 
|---|
| 190 | child = getItem(hwnd, IDC_ADVANCED_PORT_EDIT); | 
|---|
| 191 | CString portString = getWindowText(child); | 
|---|
| 192 | int port = atoi(portString.c_str()); | 
|---|
| 193 | if (port < 1 || port > 65535) { | 
|---|
| 194 | CString defaultPortString = CStringUtil::print("%d", kDefaultPort); | 
|---|
| 195 | showError(hwnd, CStringUtil::format( | 
|---|
| 196 | getString(IDS_INVALID_PORT).c_str(), | 
|---|
| 197 | portString.c_str(), | 
|---|
| 198 | defaultPortString.c_str())); | 
|---|
| 199 | SetFocus(child); | 
|---|
| 200 | return false; | 
|---|
| 201 | } | 
|---|
| 202 |  | 
|---|
| 203 | // save state | 
|---|
| 204 | m_screenName = name; | 
|---|
| 205 | m_port       = port; | 
|---|
| 206 | m_interface  = iface; | 
|---|
| 207 |  | 
|---|
| 208 | // save values to registry | 
|---|
| 209 | HKEY key = CArchMiscWindows::addKey(HKEY_CURRENT_USER, getSettingsPath()); | 
|---|
| 210 | if (key != NULL) { | 
|---|
| 211 | CArchMiscWindows::setValue(key, "port", m_port); | 
|---|
| 212 | CArchMiscWindows::setValue(key, "name", m_screenName); | 
|---|
| 213 | CArchMiscWindows::setValue(key, "interface", m_interface); | 
|---|
| 214 | CArchMiscWindows::closeKey(key); | 
|---|
| 215 | } | 
|---|
| 216 |  | 
|---|
| 217 | return true; | 
|---|
| 218 | } | 
|---|
| 219 |  | 
|---|
| 220 | void | 
|---|
| 221 | CAdvancedOptions::setDefaults(HWND hwnd) | 
|---|
| 222 | { | 
|---|
| 223 | // restore defaults | 
|---|
| 224 | m_screenName = ARCH->getHostName(); | 
|---|
| 225 | m_port       = kDefaultPort; | 
|---|
| 226 | m_interface  = ""; | 
|---|
| 227 |  | 
|---|
| 228 | // update GUI | 
|---|
| 229 | doInit(hwnd); | 
|---|
| 230 | } | 
|---|
| 231 |  | 
|---|
| 232 | BOOL | 
|---|
| 233 | CAdvancedOptions::doDlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM) | 
|---|
| 234 | { | 
|---|
| 235 | switch (message) { | 
|---|
| 236 | case WM_INITDIALOG: | 
|---|
| 237 | doInit(hwnd); | 
|---|
| 238 | return TRUE; | 
|---|
| 239 |  | 
|---|
| 240 | case WM_COMMAND: | 
|---|
| 241 | switch (LOWORD(wParam)) { | 
|---|
| 242 | case IDOK: | 
|---|
| 243 | if (save(hwnd)) { | 
|---|
| 244 | EndDialog(hwnd, 0); | 
|---|
| 245 | } | 
|---|
| 246 | return TRUE; | 
|---|
| 247 |  | 
|---|
| 248 | case IDCANCEL: | 
|---|
| 249 | EndDialog(hwnd, 0); | 
|---|
| 250 | return TRUE; | 
|---|
| 251 |  | 
|---|
| 252 | case IDC_ADVANCED_DEFAULTS: | 
|---|
| 253 | setDefaults(hwnd); | 
|---|
| 254 | return TRUE; | 
|---|
| 255 | } | 
|---|
| 256 | break; | 
|---|
| 257 |  | 
|---|
| 258 | default: | 
|---|
| 259 | break; | 
|---|
| 260 | } | 
|---|
| 261 |  | 
|---|
| 262 | return FALSE; | 
|---|
| 263 | } | 
|---|
| 264 |  | 
|---|
| 265 | BOOL CALLBACK | 
|---|
| 266 | CAdvancedOptions::dlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) | 
|---|
| 267 | { | 
|---|
| 268 | return s_singleton->doDlgProc(hwnd, message, wParam, lParam); | 
|---|
| 269 | } | 
|---|