2012-06-10 16:50:54 +00:00
/*
* synergy - - mouse and keyboard sharing utility
2012-09-04 02:09:56 +00:00
* Copyright ( C ) 2012 Bolton Software Ltd .
2012-06-10 16:50:54 +00:00
* Copyright ( C ) 2008 Volker Lanz ( vl @ fidra . de )
*
* 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 .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
2013-06-05 15:41:14 +00:00
# define DOWNLOAD_URL "http: //synergy-foss.org/?source=gui"
2012-06-10 16:50:54 +00:00
2012-07-03 14:15:05 +00:00
# include <iostream>
2012-06-10 16:50:54 +00:00
# include "MainWindow.h"
# include "AboutDialog.h"
# include "ServerConfigDialog.h"
# include "SettingsDialog.h"
# include "SetupWizard.h"
# include <QtCore>
# include <QtGui>
# include <QtNetwork>
# include <QNetworkAccessManager>
# if defined(Q_OS_MAC)
# include <ApplicationServices/ApplicationServices.h>
# endif
# if defined(Q_OS_WIN)
# define WIN32_LEAN_AND_MEAN
# include <Windows.h>
# endif
# if defined(Q_OS_WIN)
static const char synergyConfigName [ ] = " synergy.sgc " ;
static const QString synergyConfigFilter ( QObject : : tr ( " Synergy Configurations (*.sgc) ; ; All files ( * . * ) " )) ;
# else
static const char synergyConfigName [ ] = " synergy.conf " ;
static const QString synergyConfigFilter ( QObject : : tr ( " Synergy Configurations (*.conf) ; ; All files ( * . * ) " )) ;
# endif
static const char * synergyIconFiles [ ] =
{
" :/res/icons/16x16/synergy-disconnected.png " ,
" :/res/icons/16x16/synergy-disconnected.png " ,
" :/res/icons/16x16/synergy-connected.png "
} ;
MainWindow : : MainWindow ( QSettings & settings , AppConfig & appConfig ) :
m_Settings ( settings ) ,
m_AppConfig ( appConfig ) ,
m_pSynergy ( NULL ) ,
m_SynergyState ( synergyDisconnected ) ,
m_ServerConfig ( & m_Settings , 5 , 3 ) ,
m_pTempConfigFile ( NULL ) ,
m_pTrayIcon ( NULL ) ,
m_pTrayIconMenu ( NULL ) ,
2013-05-29 20:46:24 +00:00
m_AlreadyHidden ( false ) ,
2012-07-28 02:59:20 +00:00
m_ElevateProcess ( false ) ,
2013-05-29 20:46:24 +00:00
m_SuppressElevateWarning ( false ) ,
m_pMenuBar ( NULL ) ,
m_pMenuFile ( NULL ) ,
m_pMenuEdit ( NULL ) ,
m_pMenuWindow ( NULL ) ,
m_pMenuHelp ( NULL )
2012-06-10 16:50:54 +00:00
{
setupUi ( this ) ;
createMenuBar ( ) ;
loadSettings ( ) ;
initConnections ( ) ;
2013-06-05 14:47:42 +00:00
m_pWidgetUpdate - > hide ( ) ;
2013-05-29 20:46:24 +00:00
m_VersionChecker . setApp ( appPath ( appConfig . synergycName ( ) ) ) ;
2012-07-28 20:41:40 +00:00
m_pLabelScreenName - > setText ( getScreenName ( ) ) ;
m_pLabelIpAddresses - > setText ( getIPAddresses ( ) ) ;
2012-06-10 16:50:54 +00:00
2013-07-05 12:05:08 +00:00
updatePremiumInfo ( ) ;
2012-07-14 17:01:29 +00:00
# if defined(Q_OS_WIN)
2012-07-13 14:38:58 +00:00
// ipc must always be enabled, so that we can disable command when switching to desktop mode.
connect ( & m_IpcClient , SIGNAL ( readLogLine ( const QString & ) ) , this , SLOT ( appendLogRaw ( const QString & ) ) ) ;
connect ( & m_IpcClient , SIGNAL ( errorMessage ( const QString & ) ) , this , SLOT ( appendLogError ( const QString & ) ) ) ;
2012-07-14 17:01:29 +00:00
connect ( & m_IpcClient , SIGNAL ( infoMessage ( const QString & ) ) , this , SLOT ( appendLogNote ( const QString & ) ) ) ;
2012-07-13 14:38:58 +00:00
m_IpcClient . connectToHost ( ) ;
2012-07-28 02:59:20 +00:00
# else
// elevate checkbox is only useful on ms windows.
m_pElevateCheckBox - > hide ( ) ;
2012-07-14 17:01:29 +00:00
# endif
2012-07-29 00:10:18 +00:00
2012-07-30 16:11:28 +00:00
// change default size based on os
2012-07-29 00:10:18 +00:00
# if defined(Q_OS_MAC)
2012-07-30 16:11:28 +00:00
resize ( 720 , 550 ) ;
setMinimumSize ( size ( ) ) ;
# elif defined(Q_OS_LINUX)
resize ( 700 , 530 ) ;
setMinimumSize ( size ( ) ) ;
2012-07-29 00:10:18 +00:00
# endif
2012-06-10 16:50:54 +00:00
}
MainWindow : : ~ MainWindow ( )
{
2012-07-28 22:13:14 +00:00
if ( appConfig ( ) . processMode ( ) = = Desktop )
{
stopDesktop ( ) ;
}
2012-06-10 16:50:54 +00:00
saveSettings ( ) ;
}
void MainWindow : : start ( bool firstRun )
2012-07-13 14:38:58 +00:00
{
createTrayIcon ( ) ;
// always show. auto-hide only happens when we have a connection.
2012-07-28 23:27:28 +00:00
showNormal ( ) ;
2012-07-13 14:38:58 +00:00
2013-05-29 20:46:24 +00:00
m_VersionChecker . checkLatest ( ) ;
2012-07-13 14:38:58 +00:00
}
2012-07-28 22:13:14 +00:00
void MainWindow : : onModeChanged ( bool startDesktop , bool applyService )
2012-06-10 16:50:54 +00:00
{
2012-07-13 14:00:42 +00:00
if ( appConfig ( ) . processMode ( ) = = Service )
{
2012-07-28 22:58:51 +00:00
// form is always enabled in service mode.
setFormEnabled ( true ) ;
2012-07-13 18:44:43 +00:00
// ensure that the apply button actually does something, since desktop
// mode screws around with connecting/disconnecting the action.
disconnect ( m_pButtonToggleStart , SIGNAL ( clicked ( ) ) , m_pActionStartSynergy , SLOT ( trigger ( ) ) ) ;
connect ( m_pButtonToggleStart , SIGNAL ( clicked ( ) ) , m_pActionStartSynergy , SLOT ( trigger ( ) ) ) ;
2012-07-28 22:13:14 +00:00
if ( applyService )
2012-07-13 18:44:43 +00:00
{
2012-07-28 22:13:14 +00:00
stopDesktop ( ) ;
2012-07-13 18:44:43 +00:00
startSynergy ( ) ;
}
2012-07-13 14:00:42 +00:00
}
2012-07-28 22:13:14 +00:00
else if ( ( appConfig ( ) . processMode ( ) = = Desktop ) & & startDesktop )
2012-07-13 14:00:42 +00:00
{
2012-07-28 22:13:14 +00:00
stopService ( ) ;
2012-06-10 16:50:54 +00:00
startSynergy ( ) ;
2012-07-13 14:00:42 +00:00
}
2012-07-28 02:59:20 +00:00
m_pElevateCheckBox - > setEnabled ( appConfig ( ) . processMode ( ) = = Service ) ;
2012-06-10 16:50:54 +00:00
}
void MainWindow : : setStatus ( const QString & status )
{
m_pStatusLabel - > setText ( status ) ;
}
void MainWindow : : createTrayIcon ( )
{
m_pTrayIconMenu = new QMenu ( this ) ;
m_pTrayIconMenu - > addAction ( m_pActionStartSynergy ) ;
m_pTrayIconMenu - > addAction ( m_pActionStopSynergy ) ;
m_pTrayIconMenu - > addSeparator ( ) ;
m_pTrayIconMenu - > addAction ( m_pActionMinimize ) ;
m_pTrayIconMenu - > addAction ( m_pActionRestore ) ;
m_pTrayIconMenu - > addSeparator ( ) ;
m_pTrayIconMenu - > addAction ( m_pActionQuit ) ;
m_pTrayIcon = new QSystemTrayIcon ( this ) ;
m_pTrayIcon - > setContextMenu ( m_pTrayIconMenu ) ;
2012-07-28 23:27:28 +00:00
connect ( m_pTrayIcon , SIGNAL ( activated ( QSystemTrayIcon : : ActivationReason ) ) ,
this , SLOT ( trayActivated ( QSystemTrayIcon : : ActivationReason ) ) ) ;
2012-06-10 16:50:54 +00:00
setIcon ( synergyDisconnected ) ;
m_pTrayIcon - > show ( ) ;
}
2013-05-29 20:46:24 +00:00
void MainWindow : : retranslateMenuBar ( )
{
m_pMenuFile - > setTitle ( tr ( " &File " ) ) ;
m_pMenuEdit - > setTitle ( tr ( " &Edit " ) ) ;
m_pMenuWindow - > setTitle ( tr ( " &Window " ) ) ;
m_pMenuHelp - > setTitle ( tr ( " &Help " ) ) ;
}
2012-06-10 16:50:54 +00:00
void MainWindow : : createMenuBar ( )
{
2013-05-29 20:46:24 +00:00
m_pMenuBar = new QMenuBar ( this ) ;
m_pMenuFile = new QMenu ( " " , m_pMenuBar ) ;
m_pMenuEdit = new QMenu ( " " , m_pMenuBar ) ;
m_pMenuWindow = new QMenu ( " " , m_pMenuBar ) ;
m_pMenuHelp = new QMenu ( " " , m_pMenuBar ) ;
retranslateMenuBar ( ) ;
2012-06-10 16:50:54 +00:00
2013-05-29 20:46:24 +00:00
m_pMenuBar - > addAction ( m_pMenuFile - > menuAction ( ) ) ;
m_pMenuBar - > addAction ( m_pMenuEdit - > menuAction ( ) ) ;
2012-06-10 16:50:54 +00:00
# if !defined(Q_OS_MAC)
2013-05-29 20:46:24 +00:00
m_pMenuBar - > addAction ( m_pMenuWindow - > menuAction ( ) ) ;
2012-06-10 16:50:54 +00:00
# endif
2013-05-29 20:46:24 +00:00
m_pMenuBar - > addAction ( m_pMenuHelp - > menuAction ( ) ) ;
2012-06-10 16:50:54 +00:00
2013-05-29 20:46:24 +00:00
m_pMenuFile - > addAction ( m_pActionStartSynergy ) ;
m_pMenuFile - > addAction ( m_pActionStopSynergy ) ;
m_pMenuFile - > addSeparator ( ) ;
m_pMenuFile - > addAction ( m_pActionWizard ) ;
m_pMenuFile - > addAction ( m_pActionSave ) ;
m_pMenuFile - > addSeparator ( ) ;
m_pMenuFile - > addAction ( m_pActionQuit ) ;
m_pMenuEdit - > addAction ( m_pActionSettings ) ;
m_pMenuWindow - > addAction ( m_pActionMinimize ) ;
m_pMenuWindow - > addAction ( m_pActionRestore ) ;
m_pMenuHelp - > addAction ( m_pActionAbout ) ;
2012-06-10 16:50:54 +00:00
2013-05-29 20:46:24 +00:00
setMenuBar ( m_pMenuBar ) ;
2012-06-10 16:50:54 +00:00
}
void MainWindow : : loadSettings ( )
{
// the next two must come BEFORE loading groupServerChecked and groupClientChecked or
// disabling and/or enabling the right widgets won't automatically work
2012-11-15 14:25:46 +00:00
m_pRadioExternalConfig - > setChecked ( settings ( ) . value ( " useExternalConfig " , false ) . toBool ( ) ) ;
m_pRadioInternalConfig - > setChecked ( settings ( ) . value ( " useInternalConfig " , true ) . toBool ( ) ) ;
2012-06-10 16:50:54 +00:00
m_pGroupServer - > setChecked ( settings ( ) . value ( " groupServerChecked " , false ) . toBool ( ) ) ;
m_pLineEditConfigFile - > setText ( settings ( ) . value ( " configFile " , QDir : : homePath ( ) + " / " + synergyConfigName ) . toString ( ) ) ;
m_pGroupClient - > setChecked ( settings ( ) . value ( " groupClientChecked " , true ) . toBool ( ) ) ;
m_pLineEditHostname - > setText ( settings ( ) . value ( " serverHostname " ) . toString ( ) ) ;
2012-07-28 02:59:20 +00:00
m_SuppressElevateWarning = true ;
m_pElevateCheckBox - > setChecked ( settings ( ) . value ( " elevateChecked " , false ) . toBool ( ) ) ;
m_SuppressElevateWarning = false ;
2012-06-10 16:50:54 +00:00
}
void MainWindow : : initConnections ( )
{
connect ( m_pActionMinimize , SIGNAL ( triggered ( ) ) , this , SLOT ( hide ( ) ) ) ;
connect ( m_pActionRestore , SIGNAL ( triggered ( ) ) , this , SLOT ( showNormal ( ) ) ) ;
connect ( m_pActionStartSynergy , SIGNAL ( triggered ( ) ) , this , SLOT ( startSynergy ( ) ) ) ;
connect ( m_pActionStopSynergy , SIGNAL ( triggered ( ) ) , this , SLOT ( stopSynergy ( ) ) ) ;
connect ( m_pActionQuit , SIGNAL ( triggered ( ) ) , qApp , SLOT ( quit ( ) ) ) ;
2013-05-29 20:46:24 +00:00
connect ( & m_VersionChecker , SIGNAL ( updateFound ( const QString & ) ) , this , SLOT ( updateFound ( const QString & ) ) ) ;
2012-06-10 16:50:54 +00:00
}
void MainWindow : : saveSettings ( )
{
// program settings
settings ( ) . setValue ( " groupServerChecked " , m_pGroupServer - > isChecked ( ) ) ;
2012-11-15 14:25:46 +00:00
settings ( ) . setValue ( " useExternalConfig " , m_pRadioExternalConfig - > isChecked ( ) ) ;
2012-06-10 16:50:54 +00:00
settings ( ) . setValue ( " configFile " , m_pLineEditConfigFile - > text ( ) ) ;
2012-11-15 14:25:46 +00:00
settings ( ) . setValue ( " useInternalConfig " , m_pRadioInternalConfig - > isChecked ( ) ) ;
2012-06-10 16:50:54 +00:00
settings ( ) . setValue ( " groupClientChecked " , m_pGroupClient - > isChecked ( ) ) ;
settings ( ) . setValue ( " serverHostname " , m_pLineEditHostname - > text ( ) ) ;
settings ( ) . sync ( ) ;
}
void MainWindow : : setIcon ( qSynergyState state )
{
QIcon icon ;
icon . addFile ( synergyIconFiles [ state ] ) ;
if ( m_pTrayIcon )
m_pTrayIcon - > setIcon ( icon ) ;
}
2012-07-28 23:27:28 +00:00
void MainWindow : : trayActivated ( QSystemTrayIcon : : ActivationReason reason )
2012-06-10 16:50:54 +00:00
{
2013-10-15 15:46:02 +00:00
# ifndef Q_OS_WIN
2012-06-10 16:50:54 +00:00
if ( reason = = QSystemTrayIcon : : DoubleClick )
{
if ( isVisible ( ) )
{
hide ( ) ;
}
else
{
showNormal ( ) ;
activateWindow ( ) ;
}
}
2013-10-15 15:46:02 +00:00
# endif
2012-06-10 16:50:54 +00:00
}
void MainWindow : : logOutput ( )
{
if ( m_pSynergy )
{
QString text ( m_pSynergy - > readAllStandardOutput ( ) ) ;
foreach ( QString line , text . split ( QRegExp ( " \r | \n | \r \n " ) ) )
{
if ( ! line . isEmpty ( ) )
{
2012-07-28 18:54:50 +00:00
appendLogRaw ( line ) ;
2012-06-10 16:50:54 +00:00
}
}
}
}
void MainWindow : : logError ( )
{
if ( m_pSynergy )
{
2012-07-04 23:27:03 +00:00
appendLogRaw ( m_pSynergy - > readAllStandardError ( ) ) ;
2012-06-10 16:50:54 +00:00
}
}
void MainWindow : : updateFound ( const QString & version )
{
2013-06-05 14:47:42 +00:00
m_pWidgetUpdate - > show ( ) ;
m_pLabelUpdate - > setText (
tr ( " <p>Your version of Synergy is out of date. "
" Version <b>%1</b> is now available to "
" <a href= \" %2 \" >download</a>.</p> " )
2013-06-05 15:41:14 +00:00
. arg ( version ) . arg ( DOWNLOAD_URL ) ) ;
2012-06-10 16:50:54 +00:00
}
2012-07-14 17:01:29 +00:00
void MainWindow : : appendLogNote ( const QString & text )
2012-07-04 23:27:03 +00:00
{
2012-07-14 17:01:29 +00:00
appendLogRaw ( " NOTE: " + text ) ;
2012-07-04 23:27:03 +00:00
}
void MainWindow : : appendLogError ( const QString & text )
{
appendLogRaw ( " ERROR: " + text ) ;
}
void MainWindow : : appendLogRaw ( const QString & text )
2012-06-10 16:50:54 +00:00
{
2012-07-28 18:54:50 +00:00
foreach ( QString line , text . split ( QRegExp ( " \r | \n | \r \n " ) ) ) {
if ( ! line . isEmpty ( ) ) {
2012-06-10 16:50:54 +00:00
m_pLogOutput - > append ( line ) ;
2012-07-28 18:54:50 +00:00
updateStateFromLogLine ( line ) ;
}
}
}
void MainWindow : : updateStateFromLogLine ( const QString & line )
{
// TODO: implement ipc connection state messages to replace this hack.
if ( line . contains ( " started server " ) | |
2013-10-16 15:30:42 +00:00
line . contains ( " connected to server " ) | |
line . contains ( " watchdog status: ok " ) )
2012-07-28 18:54:50 +00:00
{
setSynergyState ( synergyConnected ) ;
}
2012-06-10 16:50:54 +00:00
}
void MainWindow : : clearLog ( )
{
m_pLogOutput - > clear ( ) ;
}
void MainWindow : : startSynergy ( )
{
bool desktopMode = appConfig ( ) . processMode ( ) = = Desktop ;
bool serviceMode = appConfig ( ) . processMode ( ) = = Service ;
if ( desktopMode )
{
stopSynergy ( ) ;
}
2012-07-28 22:58:51 +00:00
setSynergyState ( synergyConnecting ) ;
2012-06-10 16:50:54 +00:00
QString app ;
QStringList args ;
args < < " -f " < < " --no-tray " < < " --debug " < < appConfig ( ) . logLevelText ( ) ;
if ( ! appConfig ( ) . screenName ( ) . isEmpty ( ) )
args < < " --name " < < appConfig ( ) . screenName ( ) ;
2013-08-16 18:06:30 +00:00
if ( appConfig ( ) . cryptoEnabled ( ) )
2013-04-11 16:51:17 +00:00
{
args < < " --crypto-pass " < < appConfig ( ) . cryptoPass ( ) ;
}
2012-06-10 16:50:54 +00:00
if ( desktopMode )
{
setSynergyProcess ( new QProcess ( this ) ) ;
}
2012-07-05 18:05:35 +00:00
else
{
// tell client/server to talk to daemon through ipc.
args < < " --ipc " ;
2012-07-28 13:34:35 +00:00
# if defined(Q_OS_WIN)
// tell the client/server to shut down when a ms windows desk
// is switched; this is because we may need to elevate or not
// based on which desk the user is in (login always needs
// elevation, where as default desk does not).
args < < " --stop-on-desk-switch " ;
# endif
2012-07-05 18:05:35 +00:00
}
2012-06-10 16:50:54 +00:00
2013-10-22 18:10:14 +00:00
# ifndef Q_OS_LINUX
2013-09-18 08:34:32 +00:00
if ( appConfig ( ) . isPremium ( ) )
{
args < < " --enable-drag-drop " ;
}
2013-10-22 18:10:14 +00:00
# endif
2013-09-18 08:34:32 +00:00
2012-06-10 16:50:54 +00:00
if ( ( synergyType ( ) = = synergyClient & & ! clientArgs ( args , app ) )
| | ( synergyType ( ) = = synergyServer & & ! serverArgs ( args , app ) ) )
{
if ( desktopMode )
{
stopSynergy ( ) ;
return ;
}
}
if ( desktopMode )
{
connect ( synergyProcess ( ) , SIGNAL ( finished ( int , QProcess : : ExitStatus ) ) , this , SLOT ( synergyFinished ( int , QProcess : : ExitStatus ) ) ) ;
connect ( synergyProcess ( ) , SIGNAL ( readyReadStandardOutput ( ) ) , this , SLOT ( logOutput ( ) ) ) ;
connect ( synergyProcess ( ) , SIGNAL ( readyReadStandardError ( ) ) , this , SLOT ( logError ( ) ) ) ;
}
// put a space between last log output and new instance.
if ( ! m_pLogOutput - > toPlainText ( ) . isEmpty ( ) )
2012-07-04 23:27:03 +00:00
appendLogRaw ( " " ) ;
2012-06-10 16:50:54 +00:00
2012-07-28 18:54:50 +00:00
appendLogNote ( " starting " + QString ( synergyType ( ) = = synergyServer ? " server " : " client " ) ) ;
2012-06-10 16:50:54 +00:00
// show command if debug log level...
if ( appConfig ( ) . logLevel ( ) > = 4 ) {
2012-07-14 17:01:29 +00:00
appendLogNote ( QString ( " command: %1 %2 " ) . arg ( app , args . join ( " " ) ) ) ;
2012-06-10 16:50:54 +00:00
}
2012-07-14 17:01:29 +00:00
appendLogNote ( " config file: " + configFilename ( ) ) ;
appendLogNote ( " log level: " + appConfig ( ) . logLevelText ( ) ) ;
2012-06-10 16:50:54 +00:00
if ( appConfig ( ) . logToFile ( ) )
2012-07-14 17:01:29 +00:00
appendLogNote ( " log file: " + appConfig ( ) . logFilename ( ) ) ;
2012-06-10 16:50:54 +00:00
if ( desktopMode )
{
synergyProcess ( ) - > start ( app , args ) ;
if ( ! synergyProcess ( ) - > waitForStarted ( ) )
{
stopSynergy ( ) ;
show ( ) ;
QMessageBox : : warning ( this , tr ( " Program can not be started " ) , QString ( tr ( " The executable<br><br>%1<br><br>could not be successfully started, although it does exist. Please check if you have sufficient permissions to run this program. " ) . arg ( app ) ) ) ;
return ;
}
}
if ( serviceMode )
{
QString command ( app + " " + args . join ( " " ) ) ;
2012-07-28 02:59:20 +00:00
m_IpcClient . sendCommand ( command , m_ElevateProcess ) ;
2012-06-10 16:50:54 +00:00
}
}
bool MainWindow : : clientArgs ( QStringList & args , QString & app )
{
app = appPath ( appConfig ( ) . synergycName ( ) ) ;
if ( ! QFile : : exists ( app ) )
{
show ( ) ;
QMessageBox : : warning ( this , tr ( " Synergy client not found " ) ,
tr ( " The executable for the synergy client does not exist. " ) ) ;
return false ;
}
2013-10-16 17:20:32 +00:00
# if defined(Q_OS_WIN)
2013-10-03 14:24:58 +00:00
// wrap in quotes so a malicious user can't start \Program.exe as admin.
app = QString ( " \" %1 \" " ) . arg ( app ) ;
2013-10-16 16:52:30 +00:00
# endif
2013-10-03 14:24:58 +00:00
2012-06-10 16:50:54 +00:00
if ( m_pLineEditHostname - > text ( ) . isEmpty ( ) )
{
show ( ) ;
QMessageBox : : warning ( this , tr ( " Hostname is empty " ) ,
tr ( " Please fill in a hostname for the synergy client to connect to. " ) ) ;
return false ;
}
if ( appConfig ( ) . logToFile ( ) )
{
appConfig ( ) . persistLogDir ( ) ;
args < < " --log " < < appConfig ( ) . logFilename ( ) ;
}
args < < m_pLineEditHostname - > text ( ) + " : " + QString : : number ( appConfig ( ) . port ( ) ) ;
return true ;
}
QString MainWindow : : configFilename ( )
{
QString filename ;
if ( m_pRadioInternalConfig - > isChecked ( ) )
{
// TODO: no need to use a temporary file, since we need it to
// be permenant (since it'll be used for Windows services, etc).
m_pTempConfigFile = new QTemporaryFile ( ) ;
if ( ! m_pTempConfigFile - > open ( ) )
{
QMessageBox : : critical ( this , tr ( " Cannot write configuration file " ) , tr ( " The temporary configuration file required to start synergy can not be written. " ) ) ;
return false ;
}
serverConfig ( ) . save ( * m_pTempConfigFile ) ;
filename = m_pTempConfigFile - > fileName ( ) ;
m_pTempConfigFile - > close ( ) ;
}
else
{
if ( ! QFile : : exists ( m_pLineEditConfigFile - > text ( ) ) )
{
if ( QMessageBox : : warning ( this , tr ( " Configuration filename invalid " ) ,
tr ( " You have not filled in a valid configuration file for the synergy server. "
" Do you want to browse for the configuration file now? " ) , QMessageBox : : Yes | QMessageBox : : No ) ! = QMessageBox : : Yes
| | ! on_m_pButtonBrowseConfigFile_clicked ( ) )
return false ;
}
filename = m_pLineEditConfigFile - > text ( ) ;
}
return filename ;
}
QString MainWindow : : address ( )
{
return ( ! appConfig ( ) . interface ( ) . isEmpty ( ) ? appConfig ( ) . interface ( ) : " " ) + " : " + QString : : number ( appConfig ( ) . port ( ) ) ;
}
QString MainWindow : : appPath ( const QString & name )
{
return appConfig ( ) . synergyProgramDir ( ) + name ;
}
bool MainWindow : : serverArgs ( QStringList & args , QString & app )
{
app = appPath ( appConfig ( ) . synergysName ( ) ) ;
if ( ! QFile : : exists ( app ) )
{
QMessageBox : : warning ( this , tr ( " Synergy server not found " ) ,
tr ( " The executable for the synergy server does not exist. " ) ) ;
return false ;
}
2013-10-16 17:20:32 +00:00
# if defined(Q_OS_WIN)
2013-10-03 14:24:58 +00:00
// wrap in quotes so a malicious user can't start \Program.exe as admin.
app = QString ( " \" %1 \" " ) . arg ( app ) ;
2013-10-16 16:52:30 +00:00
# endif
2013-10-03 14:24:58 +00:00
2012-06-10 16:50:54 +00:00
if ( appConfig ( ) . logToFile ( ) )
{
appConfig ( ) . persistLogDir ( ) ;
args < < " --log " < < appConfig ( ) . logFilename ( ) ;
}
args < < " -c " < < configFilename ( ) < < " --address " < < address ( ) ;
return true ;
}
void MainWindow : : stopSynergy ( )
{
2012-07-28 22:13:14 +00:00
if ( appConfig ( ) . processMode ( ) = = Service )
{
stopService ( ) ;
2012-07-28 18:54:50 +00:00
}
2012-07-28 22:13:14 +00:00
else if ( appConfig ( ) . processMode ( ) = = Desktop )
2012-06-10 16:50:54 +00:00
{
2012-07-28 22:13:14 +00:00
stopDesktop ( ) ;
2012-06-10 16:50:54 +00:00
}
2012-07-28 18:54:50 +00:00
setSynergyState ( synergyDisconnected ) ;
2012-06-10 16:50:54 +00:00
// HACK: deleting the object deletes the physical file, which is
// bad, since it could be in use by the Windows service!
//delete m_pTempConfigFile;
m_pTempConfigFile = NULL ;
// reset so that new connects cause auto-hide.
2013-05-29 20:46:24 +00:00
m_AlreadyHidden = false ;
2012-06-10 16:50:54 +00:00
}
2012-07-28 22:13:14 +00:00
void MainWindow : : stopService ( )
{
// send empty command to stop service from laucning anything.
m_IpcClient . sendCommand ( " " , m_ElevateProcess ) ;
}
void MainWindow : : stopDesktop ( )
{
if ( ! synergyProcess ( ) ) {
return ;
}
appendLogNote ( " stopping synergy desktop process " ) ;
if ( synergyProcess ( ) - > isOpen ( ) )
synergyProcess ( ) - > close ( ) ;
delete synergyProcess ( ) ;
setSynergyProcess ( NULL ) ;
}
2012-06-10 16:50:54 +00:00
void MainWindow : : synergyFinished ( int exitCode , QProcess : : ExitStatus )
{
// on Windows, we always seem to have an exit code != 0.
# if !defined(Q_OS_WIN)
if ( exitCode ! = 0 )
{
QMessageBox : : critical ( this , tr ( " Synergy terminated with an error " ) , QString ( tr ( " Synergy terminated unexpectedly with an exit code of %1.<br><br>Please see the log output for details. " ) ) . arg ( exitCode ) ) ;
stopSynergy ( ) ;
}
# else
Q_UNUSED ( exitCode ) ;
# endif
setSynergyState ( synergyDisconnected ) ;
}
void MainWindow : : setSynergyState ( qSynergyState state )
{
if ( synergyState ( ) = = state )
return ;
if ( state = = synergyConnected | | state = = synergyConnecting )
{
disconnect ( m_pButtonToggleStart , SIGNAL ( clicked ( ) ) , m_pActionStartSynergy , SLOT ( trigger ( ) ) ) ;
connect ( m_pButtonToggleStart , SIGNAL ( clicked ( ) ) , m_pActionStopSynergy , SLOT ( trigger ( ) ) ) ;
m_pButtonToggleStart - > setText ( tr ( " &Stop " ) ) ;
2013-10-16 15:30:42 +00:00
m_pButtonApply - > setEnabled ( true ) ;
2012-06-10 16:50:54 +00:00
}
else
{
disconnect ( m_pButtonToggleStart , SIGNAL ( clicked ( ) ) , m_pActionStopSynergy , SLOT ( trigger ( ) ) ) ;
connect ( m_pButtonToggleStart , SIGNAL ( clicked ( ) ) , m_pActionStartSynergy , SLOT ( trigger ( ) ) ) ;
m_pButtonToggleStart - > setText ( tr ( " &Start " ) ) ;
2013-10-15 15:46:02 +00:00
m_pButtonApply - > setEnabled ( false ) ;
2012-06-10 16:50:54 +00:00
}
2012-07-29 00:47:49 +00:00
bool connected = state = = synergyConnected ;
2012-07-28 22:13:14 +00:00
// only disable controls in desktop mode. in service mode, we can use the apply button.
if ( appConfig ( ) . processMode ( ) = = Desktop )
{
2012-07-29 00:47:49 +00:00
setFormEnabled ( ! connected ) ;
2012-07-28 22:13:14 +00:00
}
2012-06-10 16:50:54 +00:00
2012-07-29 00:47:49 +00:00
m_pActionStartSynergy - > setEnabled ( ! connected ) ;
m_pActionStopSynergy - > setEnabled ( connected ) ;
2012-06-10 16:50:54 +00:00
switch ( state )
{
2012-07-28 22:58:51 +00:00
case synergyConnected : {
2013-10-16 15:30:42 +00:00
setStatus ( tr ( " Synergy is running. " ) ) ;
2012-06-10 16:50:54 +00:00
break ;
2012-07-28 22:58:51 +00:00
}
2012-06-10 16:50:54 +00:00
case synergyConnecting :
setStatus ( tr ( " Synergy is starting. " ) ) ;
break ;
case synergyDisconnected :
setStatus ( tr ( " Synergy is not running. " ) ) ;
break ;
}
setIcon ( state ) ;
m_SynergyState = state ;
}
2012-07-28 22:58:51 +00:00
void MainWindow : : setFormEnabled ( bool enabled )
{
m_pGroupClient - > setEnabled ( enabled ) ;
m_pGroupServer - > setEnabled ( enabled ) ;
}
2012-06-10 16:50:54 +00:00
void MainWindow : : setVisible ( bool visible )
{
2013-10-15 15:46:02 +00:00
QMainWindow : : setVisible ( visible ) ;
2012-06-10 16:50:54 +00:00
m_pActionMinimize - > setEnabled ( visible ) ;
m_pActionRestore - > setEnabled ( ! visible ) ;
# if MAC_OS_X_VERSION_10_7
// dock hide only supported on lion :(
ProcessSerialNumber psn = { 0 , kCurrentProcess } ;
GetCurrentProcess ( & psn ) ;
if ( visible )
TransformProcessType ( & psn , kProcessTransformToForegroundApplication ) ;
else
TransformProcessType ( & psn , kProcessTransformToBackgroundApplication ) ;
# endif
}
2012-07-28 20:41:40 +00:00
QString MainWindow : : getIPAddresses ( )
{
QList < QHostAddress > addresses = QNetworkInterface : : allAddresses ( ) ;
bool hinted = false ;
QString result ;
for ( int i = 0 ; i < addresses . size ( ) ; i + + ) {
if ( addresses [ i ] . protocol ( ) = = QAbstractSocket : : IPv4Protocol & &
addresses [ i ] ! = QHostAddress ( QHostAddress : : LocalHost ) ) {
QString address = addresses [ i ] . toString ( ) ;
QString format = " %1, " ;
// usually 192.168.x.x is a useful ip for the user, so indicate
// this by making it bold.
if ( ! hinted & & address . startsWith ( " 192.168 " ) ) {
hinted = true ;
format = " <b>%1</b>, " ;
}
result + = format . arg ( address ) ;
}
}
if ( result = = " " ) {
2012-07-28 22:58:51 +00:00
return tr ( " Unknown " ) ;
2012-07-28 20:41:40 +00:00
}
// remove trailing comma.
result . chop ( 2 ) ;
return result ;
}
QString MainWindow : : getScreenName ( )
{
if ( appConfig ( ) . screenName ( ) = = " " ) {
return QHostInfo : : localHostName ( ) ;
}
else {
return appConfig ( ) . screenName ( ) ;
}
}
2013-05-29 20:46:24 +00:00
void MainWindow : : changeEvent ( QEvent * event )
{
if ( event ! = 0 )
{
switch ( event - > type ( ) )
{
case QEvent : : LanguageChange :
retranslateUi ( this ) ;
retranslateMenuBar ( ) ;
2013-07-05 12:05:08 +00:00
updatePremiumInfo ( ) ;
2013-05-29 20:46:24 +00:00
break ;
default :
QMainWindow : : changeEvent ( event ) ;
}
}
}
2013-07-05 12:05:08 +00:00
void MainWindow : : updatePremiumInfo ( )
{
if ( m_AppConfig . isPremium ( ) )
{
m_pWidgetPremium - > hide ( ) ;
setWindowTitle ( tr ( " Synergy Premium " ) ) ;
}
else
{
m_pWidgetPremium - > show ( ) ;
setWindowTitle ( tr ( " Synergy " ) ) ;
}
}
2012-06-10 16:50:54 +00:00
bool MainWindow : : on_m_pButtonBrowseConfigFile_clicked ( )
{
QString fileName = QFileDialog : : getOpenFileName ( this , tr ( " Browse for a synergys config file " ) , QString ( ) , synergyConfigFilter ) ;
if ( ! fileName . isEmpty ( ) )
{
m_pLineEditConfigFile - > setText ( fileName ) ;
return true ;
}
return false ;
}
bool MainWindow : : on_m_pActionSave_triggered ( )
{
QString fileName = QFileDialog : : getSaveFileName ( this , tr ( " Save configuration as... " ) ) ;
if ( ! fileName . isEmpty ( ) & & ! serverConfig ( ) . save ( fileName ) )
{
QMessageBox : : warning ( this , tr ( " Save failed " ) , tr ( " Could not save configuration to file. " ) ) ;
return true ;
}
return false ;
}
void MainWindow : : on_m_pActionAbout_triggered ( )
{
AboutDialog dlg ( this , appPath ( appConfig ( ) . synergycName ( ) ) ) ;
dlg . exec ( ) ;
}
void MainWindow : : on_m_pActionSettings_triggered ( )
{
2012-07-28 22:58:51 +00:00
ProcessMode lastProcessMode = appConfig ( ) . processMode ( ) ;
2012-06-10 16:50:54 +00:00
SettingsDialog dlg ( this , appConfig ( ) ) ;
dlg . exec ( ) ;
2012-07-28 22:58:51 +00:00
if ( lastProcessMode ! = appConfig ( ) . processMode ( ) )
{
onModeChanged ( true , true ) ;
}
2012-06-10 16:50:54 +00:00
}
void MainWindow : : on_m_pButtonConfigureServer_clicked ( )
{
ServerConfigDialog dlg ( this , serverConfig ( ) , appConfig ( ) . screenName ( ) ) ;
dlg . exec ( ) ;
}
2012-07-28 02:59:20 +00:00
void MainWindow : : on_m_pActionWizard_triggered ( )
2012-06-10 16:50:54 +00:00
{
2013-05-29 20:46:24 +00:00
SetupWizard wizard ( * this , false ) ;
wizard . exec ( ) ;
2013-07-05 12:05:08 +00:00
updatePremiumInfo ( ) ;
2012-06-10 16:50:54 +00:00
}
2012-07-28 02:59:20 +00:00
void MainWindow : : on_m_pElevateCheckBox_toggled ( bool checked )
2012-06-10 16:50:54 +00:00
{
2012-07-28 02:59:20 +00:00
if ( checked & & ! m_SuppressElevateWarning ) {
int r = QMessageBox : : warning (
this , tr ( " Elevate Synergy " ) ,
tr ( " Are you sure you want to elevate Synergy? \n \n "
" This allows Synergy to interact with elevated processes "
" and the UAC dialog, but can cause problems with non-elevated "
" processes. Elevate Synergy only if you really need to. " ) ,
QMessageBox : : Yes | QMessageBox : : No ) ;
if ( r ! = QMessageBox : : Yes ) {
m_pElevateCheckBox - > setChecked ( false ) ;
return ;
}
}
m_ElevateProcess = checked ;
settings ( ) . setValue ( " elevateChecked " , checked ) ;
settings ( ) . sync ( ) ;
2012-06-10 16:50:54 +00:00
}
2012-07-28 18:54:50 +00:00
void MainWindow : : on_m_pButtonApply_clicked ( )
{
startSynergy ( ) ;
}