CAdvancedOptions.cpp

00001 /*
00002  * synergy -- mouse and keyboard sharing utility
00003  * Copyright (C) 2002 Chris Schoeneman
00004  * 
00005  * This package is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * found in the file COPYING that should have accompanied this file.
00008  * 
00009  * This package is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  */
00014 
00015 #include "CConfig.h"
00016 #include "ProtocolTypes.h"
00017 #include "CStringUtil.h"
00018 #include "CArch.h"
00019 #include "CArchMiscWindows.h"
00020 #include "CAdvancedOptions.h"
00021 #include "LaunchUtil.h"
00022 #include "XArch.h"
00023 #include "resource.h"
00024 
00025 //
00026 // CAdvancedOptions
00027 //
00028 
00029 CAdvancedOptions*       CAdvancedOptions::s_singleton = NULL;
00030 
00031 CAdvancedOptions::CAdvancedOptions(HWND parent, CConfig* config) :
00032     m_parent(parent),
00033     m_config(config),
00034     m_isClient(false),
00035     m_screenName(ARCH->getHostName()),
00036     m_port(kDefaultPort),
00037     m_interface()
00038 {
00039     assert(s_singleton == NULL);
00040     s_singleton = this;
00041     init();
00042 }
00043 
00044 CAdvancedOptions::~CAdvancedOptions()
00045 {
00046     s_singleton = NULL;
00047 }
00048 
00049 void
00050 CAdvancedOptions::doModal(bool isClient)
00051 {
00052     // save state
00053     m_isClient = isClient;
00054 
00055     // do dialog
00056     DialogBoxParam(s_instance, MAKEINTRESOURCE(IDD_ADVANCED_OPTIONS),
00057                                 m_parent, (DLGPROC)dlgProc, (LPARAM)this);
00058 }
00059 
00060 CString
00061 CAdvancedOptions::getScreenName() const
00062 {
00063     return m_screenName;
00064 }
00065 
00066 int
00067 CAdvancedOptions::getPort() const
00068 {
00069     return m_port;
00070 }
00071 
00072 CString
00073 CAdvancedOptions::getInterface() const
00074 {
00075     return m_interface;
00076 }
00077 
00078 CString
00079 CAdvancedOptions::getCommandLine(bool isClient, const CString& serverName) const
00080 {
00081     CString cmdLine;
00082 
00083     // screen name
00084     if (!m_screenName.empty()) {
00085         cmdLine += " --name ";
00086         cmdLine += m_screenName;
00087     }
00088 
00089     // port
00090     char portString[20];
00091     sprintf(portString, "%d", m_port);
00092     if (isClient) {
00093         cmdLine += " ";
00094         cmdLine += serverName;
00095         cmdLine += ":";
00096         cmdLine += portString;
00097     }
00098     else {
00099         cmdLine += " --address ";
00100         if (!m_interface.empty()) {
00101             cmdLine += m_interface;
00102         }
00103         cmdLine += ":";
00104         cmdLine += portString;
00105     }
00106 
00107     return cmdLine;
00108 }
00109 
00110 void
00111 CAdvancedOptions::init()
00112 {
00113     // get values from registry
00114     HKEY key = CArchMiscWindows::openKey(HKEY_CURRENT_USER, getSettingsPath());
00115     if (key != NULL) {
00116         DWORD newPort        = CArchMiscWindows::readValueInt(key, "port");
00117         CString newName      = CArchMiscWindows::readValueString(key, "name");
00118         CString newInterface =
00119             CArchMiscWindows::readValueString(key, "interface");
00120         if (newPort != 0) {
00121             m_port = static_cast<int>(newPort);
00122         }
00123         if (!newName.empty()) {
00124             m_screenName = newName;
00125         }
00126         if (!newInterface.empty()) {
00127             m_interface = newInterface;
00128         }
00129         CArchMiscWindows::closeKey(key);
00130     }
00131 }
00132 
00133 void
00134 CAdvancedOptions::doInit(HWND hwnd)
00135 {
00136     // set values in GUI
00137     HWND child;
00138     char buffer[20];
00139     sprintf(buffer, "%d", m_port);
00140     child = getItem(hwnd, IDC_ADVANCED_PORT_EDIT);
00141     SendMessage(child, WM_SETTEXT, 0, (LPARAM)buffer);
00142 
00143     child = getItem(hwnd, IDC_ADVANCED_NAME_EDIT);
00144     SendMessage(child, WM_SETTEXT, 0, (LPARAM)m_screenName.c_str());
00145 
00146     child = getItem(hwnd, IDC_ADVANCED_INTERFACE_EDIT);
00147     SendMessage(child, WM_SETTEXT, 0, (LPARAM)m_interface.c_str());
00148 }
00149 
00150 bool
00151 CAdvancedOptions::save(HWND hwnd)
00152 {
00153     SetCursor(LoadCursor(NULL, IDC_WAIT));
00154 
00155     HWND child = getItem(hwnd, IDC_ADVANCED_NAME_EDIT);
00156     CString name = getWindowText(child);
00157     if (!m_config->isValidScreenName(name)) {
00158         showError(hwnd, CStringUtil::format(
00159                                 getString(IDS_INVALID_SCREEN_NAME).c_str(),
00160                                 name.c_str()));
00161         SetFocus(child);
00162         return false;
00163     }
00164     if (!m_isClient && !m_config->isScreen(name)) {
00165         showError(hwnd, CStringUtil::format(
00166                                 getString(IDS_UNKNOWN_SCREEN_NAME).c_str(),
00167                                 name.c_str()));
00168         SetFocus(child);
00169         return false;
00170     }
00171 
00172     child = getItem(hwnd, IDC_ADVANCED_INTERFACE_EDIT);
00173     CString iface = getWindowText(child);
00174     if (!m_isClient) {
00175         try {
00176             if (!iface.empty()) {
00177                 ARCH->nameToAddr(iface);
00178             }
00179         }
00180         catch (XArchNetworkName& e) {
00181             showError(hwnd, CStringUtil::format(
00182                                 getString(IDS_INVALID_INTERFACE_NAME).c_str(),
00183                                 iface.c_str(), e.what().c_str()));
00184             SetFocus(child);
00185             return false;
00186         }
00187     }
00188 
00189     // get and verify port
00190     child = getItem(hwnd, IDC_ADVANCED_PORT_EDIT);
00191     CString portString = getWindowText(child);
00192     int port = atoi(portString.c_str());
00193     if (port < 1 || port > 65535) {
00194         CString defaultPortString = CStringUtil::print("%d", kDefaultPort);
00195         showError(hwnd, CStringUtil::format(
00196                                 getString(IDS_INVALID_PORT).c_str(),
00197                                 portString.c_str(),
00198                                 defaultPortString.c_str()));
00199         SetFocus(child);
00200         return false;
00201     }
00202 
00203     // save state
00204     m_screenName = name;
00205     m_port       = port;
00206     m_interface  = iface;
00207 
00208     // save values to registry
00209     HKEY key = CArchMiscWindows::addKey(HKEY_CURRENT_USER, getSettingsPath());
00210     if (key != NULL) {
00211         CArchMiscWindows::setValue(key, "port", m_port);
00212         CArchMiscWindows::setValue(key, "name", m_screenName);
00213         CArchMiscWindows::setValue(key, "interface", m_interface);
00214         CArchMiscWindows::closeKey(key);
00215     }
00216 
00217     return true;
00218 }
00219 
00220 void
00221 CAdvancedOptions::setDefaults(HWND hwnd)
00222 {
00223     // restore defaults
00224     m_screenName = ARCH->getHostName();
00225     m_port       = kDefaultPort;
00226     m_interface  = "";
00227 
00228     // update GUI
00229     doInit(hwnd);
00230 }
00231 
00232 BOOL
00233 CAdvancedOptions::doDlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM)
00234 {
00235     switch (message) {
00236     case WM_INITDIALOG:
00237         doInit(hwnd);
00238         return TRUE;
00239 
00240     case WM_COMMAND:
00241         switch (LOWORD(wParam)) {
00242         case IDOK:
00243             if (save(hwnd)) {
00244                 EndDialog(hwnd, 0);
00245             }
00246             return TRUE;
00247 
00248         case IDCANCEL:
00249             EndDialog(hwnd, 0);
00250             return TRUE;
00251 
00252         case IDC_ADVANCED_DEFAULTS:
00253             setDefaults(hwnd);
00254             return TRUE;
00255         }
00256         break;
00257 
00258     default:
00259         break;
00260     }
00261 
00262     return FALSE;
00263 }
00264 
00265 BOOL CALLBACK
00266 CAdvancedOptions::dlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
00267 {
00268     return s_singleton->doDlgProc(hwnd, message, wParam, lParam);
00269 }

Generated on Fri Nov 6 00:18:44 2009 for synergy-plus by  doxygen 1.4.7