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 #if !defined(APPCONFIG_H)
20 
21 #define APPCONFIG_H
22 
23 #include <QObject>
24 #include <QString>
25 #include <QVariant>
26 #include "ElevateMode.h"
27 #include <shared/EditionType.h>
28 #include <mutex>
29 #include "ConfigBase.h"
30 #include "ConfigWriter.h"
31 #include "CoreInterface.h"
32 
33 // this should be incremented each time a new page is added. this is
34 // saved to settings when the user finishes running the wizard. if
35 // the saved wizard version is lower than this number, the wizard
36 // will be displayed. each version incrememnt should be described
37 // here...
38 //
39 //   1: first version
40 //   2: added language page
41 //   3: added premium page and removed
42 //   4: ssl plugin 'ns' v1.0
43 //   5: ssl plugin 'ns' v1.1
44 //   6: ssl plugin 'ns' v1.2
45 //   7: serial key activation
46 //   8: Visual Studio 2015 support
47 //
48 const int kWizardVersion = 8;
49 
50 class QSettings;
51 class SettingsDialog;
52 class ServerConfig;
53 
54 enum ProcessMode {
55     Service,
56     Desktop
57 };
58 
59 class AppConfig: public QObject, public GUI::Config::ConfigBase
60 {
61     Q_OBJECT
62 
63     friend class SettingsDialog;
64     friend class MainWindow;
65     friend class SetupWizard;
66     friend class ServerConfig;
67 
68     public:
69         AppConfig();
70 
71     public:
72         bool isWritable() const;
73         bool isSystemScoped() const;
74 
75         const QString& screenName() const;
76         int port() const;
77         const QString& networkInterface() const;
78         int logLevel() const;
79         bool logToFile() const;
80         const QString& logFilename() const;
81         const QString logFilenameCmd() const;
82         QString logLevelText() const;
83         ProcessMode processMode() const;
84         bool wizardShouldRun() const;
85         const QString& language() const;
86         bool startedBefore() const;
87         bool autoConfig() const;
88         void setAutoConfig(bool autoConfig);
89         QString autoConfigServer() const;
90         void setAutoConfigServer(const QString& autoConfigServer);
91 #ifndef SYNERGY_ENTERPRISE
92         void setEdition(Edition);
93         Edition edition() const;
94         void setSerialKey(const QString& serial);
95         void clearSerialKey();
96         QString serialKey();
97         int lastExpiringWarningTime() const;
98         void setLastExpiringWarningTime(int t);
99 #endif
100 
101         QString synergysName() const;
102         QString synergycName() const;
103         QString synergyProgramDir() const;
104         QString synergyLogDir() const;
105 
106         void persistLogDir();
107         ElevateMode elevateMode();
108 
109         bool isCryptoAvailable() const;
110         void setCryptoEnabled(bool e);
111         bool getCryptoEnabled() const;
112 
113         void setAutoHide(bool b);
114         bool getAutoHide();
115         void setLanguageSync(bool b);
116         bool getLanguageSync() const;
117 #ifndef SYNERGY_ENTERPRISE
118         bool activationHasRun() const;
119         AppConfig& activationHasRun(bool value);
120 #endif
121         /// @brief Sets the user preference to load from SystemScope.
122         /// @param [in] value
123         ///             True - This will set the variable and load the global scope settings.
124         ///             False - This will set the variable and load the user scope settings.
125         void setLoadFromSystemScope(bool value);
126 
127 
128         bool    getServerGroupChecked() const;
129         bool    getUseExternalConfig() const;
130         const QString& getConfigFile() const;
131         bool    getUseInternalConfig() const;
132         bool    getClientGroupChecked() const;
133         QString getServerHostname() const;
134 
135         /// @brief Gets the current TLS certificate path
136         /// @return QString The path to the cert
137         QString getTLSCertPath() const;
138 
139         /// @brief Get the key length to be used for the private key of a TLS cert
140         /// @return QString The key length in bits
141         QString     getTLSKeyLength() const;
142 
143         void setServerGroupChecked(bool);
144         void setUseExternalConfig(bool) ;
145         void setConfigFile(const QString&);
146         void setUseInternalConfig(bool) ;
147         void setClientGroupChecked(bool) ;
148         void setServerHostname(const QString&);
149 
150         /// @brief Set the path to the TLS/SSL certificate file that will be used
151         /// @param [in] path The path to the Certificate
152         void setTLSCertPath(const QString& path);
153 
154         /// @brief Sets the key length of the private key to use in a TLS connection
155         /// @param [in] QString length The key length eg: 1024, 2048, 4096
156         void setTLSKeyLength(const QString& length);
157 
158 
159         QString lastVersion() const;
160 
161         void setMinimizeToTray(bool b);
162         bool getMinimizeToTray();
163 
164         void saveSettings() override;
165         void setLastVersion(const QString& version);
166 
167         /// @brief Generates TLS certificate
168         /// @param [in] bool forceGeneration Generate certificate even if it's exists.
169         void generateCertificate(bool forceGeneration=false) const;
170 
171 protected:
172     /// @brief The enumeration to easily access the names of the setting inside m_SynergySettingsName
173     enum Setting {
174         kScreenName,
175         kPort,
176         kInterfaceSetting,
177         kLogLevel,
178         kLogToFile,
179         kLogFilename,
180         kWizardLastRun,
181         kLanguage,
182         kStartedBefore,
183         kAutoConfig,
184         kAutoConfigServer,
185         kElevateModeSetting,
186         kElevateModeEnum,
187         kEditionSetting,
188         kCryptoEnabled,
189         kAutoHide,
190         kSerialKey,
191         kLastVersion,
192         kLastExpireWarningTime,
193         kActivationHasRun,
194         kMinimizeToTray,
195         kActivateEmail,
196         kLoadSystemSettings,
197         kGroupServerCheck,
198         kUseExternalConfig,
199         kConfigFile,
200         kUseInternalConfig,
201         kGroupClientCheck,
202         kServerHostname,
203         kTLSCertPath,
204         kTLSKeyLength,
205     };
206 
207         void setScreenName(const QString& s);
208         void setPort(int i);
209         void setNetworkInterface(const QString& s);
210         void setLogLevel(int i);
211         void setLogToFile(bool b);
212         void setLogFilename(const QString& s);
213         void setWizardHasRun();
214         void setLanguage(const QString& language);
215         void setStartedBefore(bool b);
216         void setElevateMode(ElevateMode em);
217 
218         /// @brief loads the setting from the current scope
219         /// @param ignoreSystem should the load feature ignore the globalScope setting that was saved
220         void loadSettings() override;
221         static QString settingName(AppConfig::Setting name);
222 
223     private:
224 
225         QString m_ScreenName;
226         int m_Port;
227         QString m_Interface;
228         int m_LogLevel;
229         bool m_LogToFile;
230         QString m_LogFilename;
231         int m_WizardLastRun;
232         ProcessMode m_ProcessMode;
233         QString m_Language;
234         bool m_StartedBefore;
235         bool m_AutoConfig;
236         QString m_AutoConfigServer;
237         ElevateMode m_ElevateMode;
238         Edition m_Edition;
239         QString m_ActivateEmail;
240         bool m_CryptoEnabled;
241         bool m_AutoHide;
242         QString m_Serialkey;
243         QString m_lastVersion;
244         int m_LastExpiringWarningTime;
245         bool m_ActivationHasRun;
246         bool m_MinimizeToTray;
247         bool m_LanguageSync           = true;
248 
249         bool m_ServerGroupChecked;
250         bool m_UseExternalConfig;
251         QString m_ConfigFile;
252         bool m_UseInternalConfig;
253         bool m_ClientGroupChecked;
254         QString m_ServerHostname;
255 
256         QString m_TLSCertificatePath;   /// @brief The path to the TLS certificate file
257         QString m_TLSKeyLength;         /// @brief The key length of the TLS cert to make
258 
259         bool m_LoadFromSystemScope;     /// @brief should the setting be loaded from SystemScope
260                                         ///         If the user has settings but this is true then
261                                         ///         system settings will be loaded instead of the users
262 
263         CoreInterface m_CoreInterface;
264 
265         static const char m_SynergysName[];
266         static const char m_SynergycName[];
267         static const char m_SynergyLogDir[];
268 
269         /// @brief Contains the string values of the settings names that will be saved
270         static const char* m_SynergySettingsName[];
271 
272         /// @brief Contains the name of the default configuration filename
273         static const char synergyConfigName[];
274 
275         /// @brief Sets the value of a setting
276         /// @param [in] name The Setting to be saved
277         /// @param [in] value The Value to be saved
278         template <typename T>
279         void setSetting(AppConfig::Setting name, T value);
280 
281         /// @brief Sets the value of a common setting
282         /// which should have the same value for all scopes
283         /// @param [in] name The Setting to be saved
284         /// @param [in] value The Value to be saved
285         template <typename T>
286         void setCommonSetting(AppConfig::Setting name, T value);
287 
288         /// @brief Loads a setting
289         /// @param [in] name The setting to be loaded
290         /// @param [in] defaultValue The default value of the setting
291         QVariant loadSetting(AppConfig::Setting name, const QVariant& defaultValue = QVariant());
292 
293         /// @brief Loads a common setting
294         /// @param [in] name The setting to be loaded
295         /// @param [in] defaultValue The default value of the setting
296         QVariant loadCommonSetting(AppConfig::Setting name, const QVariant& defaultValue = QVariant()) const;
297 
298         /// @brief As the settings will be accessible by multiple objects this lock will ensure that
299         ///         it cant be modified by more that one object at a time if the setting is being switched
300         ///         from system to user.
301         std::mutex m_settings_lock;
302 
303         /// @brief Sets the setting in the config checking if it has changed and flagging that settings
304         ///         needs to be saved if the setting was different
305         /// @param [in] variable the setting that will be changed
306         /// @param [in] newValue The new value of the setting
307         template <typename T>
308         void setSettingModified(T& variable,const T& newValue);
309 
310         /// @brief This method loads config from specified scope
311         /// @param [in] scope which should be loaded.
312         void loadScope(GUI::Config::ConfigWriter::Scope scope) const;
313 
314     signals:
315         void sslToggled() const;
316         void zeroConfToggled();
317         void screenNameChanged() const;
318 };
319 
320 #endif
321