1 /*
2  * synergy -- mouse and keyboard sharing utility
3  * Copyright (C) 2012-2016 Symless Ltd.
4  * Copyright (C) 2008 Volker Lanz (vl@fidra.de)
5  *
6  * This package is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * found in the file LICENSE that should have accompanied this file.
9  *
10  * This package is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #pragma once
20 
21 #include <QMainWindow>
22 #include <QSystemTrayIcon>
23 #include <QSettings>
24 #include <QProcess>
25 #include <QThread>
26 #include <QMutex>
27 
28 #include "ui_MainWindowBase.h"
29 
30 #include "ServerConfig.h"
31 #include "ServerConnection.h"
32 #include "ClientConnection.h"
33 #include "AppConfig.h"
34 #include "VersionChecker.h"
35 #include "IpcClient.h"
36 #include "Ipc.h"
37 #include "ActivationDialog.h"
38 #include "ConfigWriter.h"
39 
40 #include "TrayIcon.h"
41 
42 class QAction;
43 class QMenu;
44 class QLineEdit;
45 class QGroupBox;
46 class QPushButton;
47 class QTextEdit;
48 class QComboBox;
49 class QTabWidget;
50 class QCheckBox;
51 class QRadioButton;
52 class QMessageBox;
53 class QAbstractButton;
54 
55 class LogDialog;
56 class QSynergyApplication;
57 class SetupWizard;
58 class DataDownloader;
59 class CommandProcess;
60 class SslCertificate;
61 class LicenseManager;
62 class Zeroconf;
63 
64 class MainWindow : public QMainWindow, public Ui::MainWindowBase
65 {
66     Q_OBJECT
67 
68     friend class QSynergyApplication;
69     friend class SetupWizard;
70     friend class ActivationDialog;
71     friend class SettingsDialog;
72     friend class ServerConnection;
73     friend class ClientConnection;
74 
75     public:
76         enum qSynergyState
77         {
78             synergyDisconnected,
79             synergyConnecting,
80             synergyConnected,
81             synergyListening,
82             synergyPendingRetry
83         };
84 
85         enum qSynergyType
86         {
87             synergyClient,
88             synergyServer
89         };
90 
91         enum qLevel {
92             Error,
93             Info
94         };
95 
96         enum qRuningState {
97             kStarted,
98             kStopped
99         };
100 
101     public:
102 #ifdef SYNERGY_ENTERPRISE
103         MainWindow(AppConfig& appConfig);
104 #else
105         MainWindow(AppConfig& appConfig,
106                    LicenseManager& licenseManager);
107 #endif
108         ~MainWindow();
109 
110     public:
111         void setVisible(bool visible);
synergyType()112         int synergyType() const { return m_pRadioGroupClient->isChecked() ? synergyClient : synergyServer; }
synergyState()113         int synergyState() const { return m_SynergyState; }
hostname()114         QString hostname() const { return m_pLineEditHostname->text(); }
115         QString configFilename();
116         QString address() const;
117         QString appPath(const QString& name);
118         void open();
119         void clearLog();
versionChecker()120         VersionChecker& versionChecker() { return m_VersionChecker; }
serverConfig()121         ServerConfig& serverConfig() { return m_ServerConfig; }
122         void showConfigureServer(const QString& message);
showConfigureServer()123         void showConfigureServer() { showConfigureServer(""); }
124         void autoAddScreen(const QString name);
125         void addZeroconfServer(const QString name);
zeroconf()126         Zeroconf& zeroconf() { return *m_pZeroconf; }
127 #ifndef SYNERGY_ENTERPRISE
128         LicenseManager& licenseManager() const;
129         int raiseActivationDialog();
130 #endif
131 
132         void updateZeroconfService();
133 
134 public slots:
135         void setEdition(Edition edition);
136 #ifndef SYNERGY_ENTERPRISE
137 		void InvalidLicense();
138         void showLicenseNotice(const QString& message);
139 #endif
140         void appendLogRaw(const QString& text);
141         void appendLogInfo(const QString& text);
142         void appendLogDebug(const QString& text);
143         void appendLogError(const QString& text);
144         void startSynergy();
145         void retryStart(); // If the connection failed this will retry a startSynergy
146         void actionStart();
147 
148     protected slots:
149         void updateLocalFingerprint();
150         void updateScreenName();
151         void on_m_pRadioGroupServer_clicked(bool);
152         void on_m_pRadioGroupClient_clicked(bool);
153         void on_m_pButtonConfigureServer_clicked();
154         bool on_m_pActionSave_triggered();
155         void on_m_pActionAbout_triggered();
156         void on_m_pActionHelp_triggered();
157         void on_m_pActionSettings_triggered();
158         void on_m_pActivate_triggered();
159         void synergyFinished(int exitCode, QProcess::ExitStatus);
160         void trayActivated(QSystemTrayIcon::ActivationReason reason);
161         void stopSynergy();
162         void logOutput();
163         void logError();
164         void updateFound(const QString& version);
165         void saveSettings();
166 
167         /// @brief Receives the signal that the auto config option has changed
168         void zeroConfToggled();
169 
170     protected:
171         // TODO This should be properly using the ConfigWriter system.
settings()172         QSettings& settings() { return GUI::Config::ConfigWriter::make()->settings(); }
appConfig()173         AppConfig& appConfig() { return *m_AppConfig; }
appConfig()174         AppConfig const& appConfig() const { return *m_AppConfig; }
synergyProcess()175         QProcess* synergyProcess() { return m_pSynergy; }
setSynergyProcess(QProcess * p)176         void setSynergyProcess(QProcess* p) { m_pSynergy = p; }
177         void initConnections();
178         void createMenuBar();
179         void createStatusBar();
180         void createTrayIcon();
181         void loadSettings();
182         void setIcon(qSynergyState state) const;
183         void setSynergyState(qSynergyState state);
184         bool checkForApp(int which, QString& app);
185         bool clientArgs(QStringList& args, QString& app);
186         bool serverArgs(QStringList& args, QString& app);
187         void setStatus(const QString& status);
188         void sendIpcMessage(qIpcMessageType type, const char* buffer, bool showErrors);
189         void updateFromLogLine(const QString& line);
190         QString getIPAddresses();
191         void stopService();
192         void stopDesktop();
193         void changeEvent(QEvent* event);
194         void retranslateMenuBar();
195         void enableServer(bool enable);
196         void enableClient(bool enable);
197 
198 #if defined(Q_OS_WIN)
199         bool isServiceRunning(QString name);
200 #else
201         bool isServiceRunning();
202 #endif
203 
204         QString getProfileRootForArg();
205         void checkConnected(const QString& line);
206         void checkFingerprint(const QString& line);
207         void checkSecureSocket(const QString& line);
208 #ifndef SYNERGY_ENTERPRISE
209         void checkLicense(const QString& line);
210 #endif
211         QString getTimeStamp();
212         void restartSynergy();
213         void proofreadInfo();
214 
215         void showEvent (QShowEvent*);
216         void secureSocket(bool secureSocket);
217 
218         void windowStateChanged();
219 
220 
221     private:
222 #ifndef SYNERGY_ENTERPRISE
223         LicenseManager*     m_LicenseManager;
224         bool                m_ActivationDialogRunning;
225         QStringList         m_PendingClientNames;
226 #endif
227         Zeroconf*           m_pZeroconf;
228         AppConfig*          m_AppConfig;
229         QProcess*           m_pSynergy;
230         int                 m_SynergyState;
231         ServerConfig        m_ServerConfig;
232         bool                m_AlreadyHidden;
233         VersionChecker      m_VersionChecker;
234         IpcClient           m_IpcClient;
235         QMenuBar*           m_pMenuBar;
236         QMenu*              m_pMenuFile;
237         QMenu*              m_pMenuEdit;
238         QMenu*              m_pMenuWindow;
239         QMenu*              m_pMenuHelp;
240         QAbstractButton*    m_pCancelButton;
241         TrayIcon            m_trayIcon;
242         qRuningState        m_ExpectedRunningState;
243         QMutex              m_StopDesktopMutex;
244         bool                m_SecureSocket;             // brief Is the program running a secure socket protocol (SSL/TLS)
245         QString             m_SecureSocketVersion;      // brief Contains the version of the Secure Socket currently active
246         ServerConnection    m_serverConnection;
247         ClientConnection    m_clientConnection;
248 
249         void                updateAutoConfigWidgets();
250 
251 private slots:
252     void on_m_pButtonApply_clicked();
253     void on_windowShown();
254 
255     void on_m_pLabelComputerName_linkActivated(const QString &link);
256     void on_m_pLabelFingerprint_linkActivated(const QString& link);
257     void on_m_pComboServerList_currentIndexChanged(const QString &arg1);
258 
259     void on_m_pButtonConnect_clicked();
260 
261 signals:
262     void windowShown();
263 };
264