2016-08-25 00:45:32 +02:00
# include "./settings.h"
2016-09-01 16:34:30 +02:00
2016-08-25 00:45:32 +02:00
# include <qtutilities/settingsdialog/qtsettings.h>
# include <QString>
# include <QByteArray>
# include <QApplication>
# include <QSettings>
2016-09-03 19:39:43 +02:00
# include <QFrame>
2016-09-03 20:14:52 +02:00
# include <QSslCertificate>
# include <QSslError>
# include <QMessageBox>
2016-08-25 00:45:32 +02:00
2016-09-03 20:14:52 +02:00
using namespace std ;
2016-08-25 00:45:32 +02:00
using namespace Media ;
namespace Settings {
2016-09-01 16:34:30 +02:00
bool & firstLaunch ( )
{
static bool v = false ;
return v ;
}
// connection
2016-09-03 20:14:52 +02:00
ConnectionSettings & primaryConnectionSettings ( )
2016-08-25 00:45:32 +02:00
{
2016-09-03 20:14:52 +02:00
static ConnectionSettings v ;
2016-08-25 00:45:32 +02:00
return v ;
}
2016-09-03 20:14:52 +02:00
std : : vector < ConnectionSettings > & secondaryConnectionSettings ( )
2016-08-25 00:45:32 +02:00
{
2016-09-03 20:14:52 +02:00
static vector < ConnectionSettings > v ;
2016-08-25 00:45:32 +02:00
return v ;
}
2016-09-01 16:34:30 +02:00
// notifications
2016-08-25 00:45:32 +02:00
bool & notifyOnDisconnect ( )
{
static bool v = true ;
return v ;
}
2016-09-01 16:34:30 +02:00
bool & notifyOnInternalErrors ( )
2016-08-25 00:45:32 +02:00
{
static bool v = true ;
return v ;
}
bool & notifyOnSyncComplete ( )
{
static bool v = true ;
return v ;
}
bool & showSyncthingNotifications ( )
{
static bool v = true ;
return v ;
}
2016-09-01 16:34:30 +02:00
// appearance
2016-08-29 20:51:30 +02:00
bool & showTraffic ( )
{
static bool v = true ;
return v ;
}
2016-09-01 16:34:30 +02:00
QSize & trayMenuSize ( )
{
2016-09-21 21:09:12 +02:00
static QSize v ( 450 , 400 ) ;
2016-09-01 16:34:30 +02:00
return v ;
}
2016-09-03 19:39:43 +02:00
int & frameStyle ( )
{
static int v = QFrame : : StyledPanel | QFrame : : Sunken ;
return v ;
}
2016-09-01 16:34:30 +02:00
// autostart/launcher
2016-08-25 00:45:32 +02:00
bool & launchSynchting ( )
{
static bool v = false ;
return v ;
}
2016-09-03 19:39:43 +02:00
QString & syncthingPath ( )
{
# ifdef PLATFORM_WINDOWS
static QString v ( QStringLiteral ( " syncthing.exe " ) ) ;
# else
static QString v ( QStringLiteral ( " syncthing " ) ) ;
# endif
return v ;
}
QString & syncthingArgs ( )
2016-08-25 00:45:32 +02:00
{
static QString v ;
return v ;
}
// web view
# if defined(SYNCTHINGTRAY_USE_WEBENGINE) || defined(SYNCTHINGTRAY_USE_WEBKIT)
bool & webViewDisabled ( )
{
static bool v = false ;
return v ;
}
double & webViewZoomFactor ( )
{
static double v = 1.0 ;
return v ;
}
QByteArray & webViewGeometry ( )
{
static QByteArray v ;
return v ;
}
bool & webViewKeepRunning ( )
{
static bool v = true ;
return v ;
}
# endif
// Qt settings
Dialogs : : QtSettings & qtSettings ( )
{
static Dialogs : : QtSettings v ;
return v ;
}
void restore ( )
{
QSettings settings ( QSettings : : IniFormat , QSettings : : UserScope , QApplication : : organizationName ( ) , QApplication : : applicationName ( ) ) ;
settings . beginGroup ( QStringLiteral ( " tray " ) ) ;
2016-09-03 20:14:52 +02:00
const int connectionCount = settings . beginReadArray ( QStringLiteral ( " connections " ) ) ;
if ( connectionCount > 0 ) {
secondaryConnectionSettings ( ) . clear ( ) ;
secondaryConnectionSettings ( ) . reserve ( static_cast < size_t > ( connectionCount ) ) ;
for ( int i = 0 ; i < connectionCount ; + + i ) {
ConnectionSettings * connectionSettings ;
if ( i = = 0 ) {
connectionSettings = & primaryConnectionSettings ( ) ;
} else {
secondaryConnectionSettings ( ) . emplace_back ( ) ;
connectionSettings = & secondaryConnectionSettings ( ) . back ( ) ;
}
settings . setArrayIndex ( i ) ;
connectionSettings - > label = settings . value ( QStringLiteral ( " label " ) ) . toString ( ) ;
if ( connectionSettings - > label . isEmpty ( ) ) {
connectionSettings - > label = ( i = = 0 ? QStringLiteral ( " Primary instance " ) : QStringLiteral ( " Secondary instance %1 " ) . arg ( i ) ) ;
}
connectionSettings - > syncthingUrl = settings . value ( QStringLiteral ( " syncthingUrl " ) , connectionSettings - > syncthingUrl ) . toString ( ) ;
connectionSettings - > authEnabled = settings . value ( QStringLiteral ( " authEnabled " ) , connectionSettings - > authEnabled ) . toBool ( ) ;
connectionSettings - > userName = settings . value ( QStringLiteral ( " userName " ) ) . toString ( ) ;
connectionSettings - > password = settings . value ( QStringLiteral ( " password " ) ) . toString ( ) ;
connectionSettings - > apiKey = settings . value ( QStringLiteral ( " apiKey " ) ) . toByteArray ( ) ;
2016-09-25 20:54:09 +02:00
connectionSettings - > trafficPollInterval = settings . value ( QStringLiteral ( " trafficPollInterval " ) , connectionSettings - > trafficPollInterval ) . toInt ( ) ;
connectionSettings - > devStatsPollInterval = settings . value ( QStringLiteral ( " devStatsPollInterval " ) , connectionSettings - > devStatsPollInterval ) . toInt ( ) ;
2016-09-03 20:14:52 +02:00
connectionSettings - > httpsCertPath = settings . value ( QStringLiteral ( " httpsCertPath " ) ) . toString ( ) ;
if ( ! connectionSettings - > loadHttpsCert ( ) ) {
QMessageBox : : critical ( nullptr , QCoreApplication : : applicationName ( ) , QCoreApplication : : translate ( " Settings::restore " , " Unable to load certificate \" %1 \" when restoring settings. " ) . arg ( connectionSettings - > httpsCertPath ) ) ;
}
}
} else {
firstLaunch ( ) = true ;
primaryConnectionSettings ( ) . label = QStringLiteral ( " Primary instance " ) ;
}
settings . endArray ( ) ;
notifyOnDisconnect ( ) = settings . value ( QStringLiteral ( " notifyOnDisconnect " ) , notifyOnDisconnect ( ) ) . toBool ( ) ;
notifyOnInternalErrors ( ) = settings . value ( QStringLiteral ( " notifyOnErrors " ) , notifyOnInternalErrors ( ) ) . toBool ( ) ;
notifyOnSyncComplete ( ) = settings . value ( QStringLiteral ( " notifyOnSyncComplete " ) , notifyOnSyncComplete ( ) ) . toBool ( ) ;
showSyncthingNotifications ( ) = settings . value ( QStringLiteral ( " showSyncthingNotifications " ) , showSyncthingNotifications ( ) ) . toBool ( ) ;
showTraffic ( ) = settings . value ( QStringLiteral ( " showTraffic " ) , showTraffic ( ) ) . toBool ( ) ;
2016-09-01 16:34:30 +02:00
trayMenuSize ( ) = settings . value ( QStringLiteral ( " trayMenuSize " ) , trayMenuSize ( ) ) . toSize ( ) ;
2016-09-03 19:39:43 +02:00
frameStyle ( ) = settings . value ( QStringLiteral ( " frameStyle " ) , frameStyle ( ) ) . toInt ( ) ;
settings . endGroup ( ) ;
settings . beginGroup ( QStringLiteral ( " startup " ) ) ;
2016-08-25 00:45:32 +02:00
launchSynchting ( ) = settings . value ( QStringLiteral ( " launchSynchting " ) , false ) . toBool ( ) ;
2016-09-03 19:39:43 +02:00
syncthingPath ( ) = settings . value ( QStringLiteral ( " syncthingPath " ) , syncthingPath ( ) ) . toString ( ) ;
syncthingArgs ( ) = settings . value ( QStringLiteral ( " syncthingArgs " ) , syncthingArgs ( ) ) . toString ( ) ;
2016-08-25 00:45:32 +02:00
settings . endGroup ( ) ;
# if defined(SYNCTHINGTRAY_USE_WEBENGINE) || defined(SYNCTHINGTRAY_USE_WEBKIT)
settings . beginGroup ( QStringLiteral ( " webview " ) ) ;
webViewDisabled ( ) = settings . value ( QStringLiteral ( " isabled " ) , false ) . toBool ( ) ;
webViewZoomFactor ( ) = settings . value ( QStringLiteral ( " zoomFactor " ) , 1.0 ) . toDouble ( ) ;
webViewGeometry ( ) = settings . value ( QStringLiteral ( " geometry " ) ) . toByteArray ( ) ;
webViewKeepRunning ( ) = settings . value ( QStringLiteral ( " keepRunning " ) , true ) . toBool ( ) ;
settings . endGroup ( ) ;
# endif
qtSettings ( ) . restore ( settings ) ;
}
void save ( )
{
QSettings settings ( QSettings : : IniFormat , QSettings : : UserScope , QApplication : : organizationName ( ) , QApplication : : applicationName ( ) ) ;
settings . beginGroup ( QStringLiteral ( " tray " ) ) ;
2016-09-03 20:14:52 +02:00
const int connectionCount = static_cast < int > ( 1 + secondaryConnectionSettings ( ) . size ( ) ) ;
settings . beginWriteArray ( QStringLiteral ( " connections " ) , connectionCount ) ;
for ( int i = 0 ; i < connectionCount ; + + i ) {
const ConnectionSettings * connectionSettings = ( i = = 0 ? & primaryConnectionSettings ( ) : & secondaryConnectionSettings ( ) [ static_cast < size_t > ( i - 1 ) ] ) ;
settings . setArrayIndex ( i ) ;
settings . setValue ( QStringLiteral ( " label " ) , connectionSettings - > label ) ;
settings . setValue ( QStringLiteral ( " syncthingUrl " ) , connectionSettings - > syncthingUrl ) ;
settings . setValue ( QStringLiteral ( " authEnabled " ) , connectionSettings - > authEnabled ) ;
settings . setValue ( QStringLiteral ( " userName " ) , connectionSettings - > userName ) ;
settings . setValue ( QStringLiteral ( " password " ) , connectionSettings - > password ) ;
settings . setValue ( QStringLiteral ( " apiKey " ) , connectionSettings - > apiKey ) ;
2016-09-25 20:54:09 +02:00
settings . setValue ( QStringLiteral ( " trafficPollInterval " ) , connectionSettings - > trafficPollInterval ) ;
settings . setValue ( QStringLiteral ( " devStatsPollInterval " ) , connectionSettings - > devStatsPollInterval ) ;
2016-09-03 20:14:52 +02:00
settings . setValue ( QStringLiteral ( " httpsCertPath " ) , connectionSettings - > httpsCertPath ) ;
}
settings . endArray ( ) ;
2016-08-25 00:45:32 +02:00
settings . setValue ( QStringLiteral ( " notifyOnDisconnect " ) , notifyOnDisconnect ( ) ) ;
2016-09-01 16:34:30 +02:00
settings . setValue ( QStringLiteral ( " notifyOnErrors " ) , notifyOnInternalErrors ( ) ) ;
2016-08-25 00:45:32 +02:00
settings . setValue ( QStringLiteral ( " notifyOnSyncComplete " ) , notifyOnSyncComplete ( ) ) ;
settings . setValue ( QStringLiteral ( " showSyncthingNotifications " ) , showSyncthingNotifications ( ) ) ;
2016-08-29 20:51:30 +02:00
settings . setValue ( QStringLiteral ( " showTraffic " ) , showTraffic ( ) ) ;
2016-09-01 16:34:30 +02:00
settings . setValue ( QStringLiteral ( " trayMenuSize " ) , trayMenuSize ( ) ) ;
2016-09-03 19:39:43 +02:00
settings . setValue ( QStringLiteral ( " frameStyle " ) , frameStyle ( ) ) ;
settings . endGroup ( ) ;
settings . beginGroup ( QStringLiteral ( " startup " ) ) ;
2016-08-25 00:45:32 +02:00
settings . setValue ( QStringLiteral ( " launchSynchting " ) , launchSynchting ( ) ) ;
2016-09-03 19:39:43 +02:00
settings . setValue ( QStringLiteral ( " syncthingPath " ) , syncthingPath ( ) ) ;
settings . setValue ( QStringLiteral ( " syncthingArgs " ) , syncthingArgs ( ) ) ;
2016-08-25 00:45:32 +02:00
settings . endGroup ( ) ;
# if defined(SYNCTHINGTRAY_USE_WEBENGINE) || defined(SYNCTHINGTRAY_USE_WEBKIT)
settings . beginGroup ( QStringLiteral ( " webview " ) ) ;
settings . setValue ( QStringLiteral ( " disabled " ) , webViewDisabled ( ) ) ;
settings . setValue ( QStringLiteral ( " zoomFactor " ) , webViewZoomFactor ( ) ) ;
settings . setValue ( QStringLiteral ( " geometry " ) , webViewGeometry ( ) ) ;
settings . setValue ( QStringLiteral ( " keepRunning " ) , webViewKeepRunning ( ) ) ;
settings . endGroup ( ) ;
# endif
qtSettings ( ) . save ( settings ) ;
}
2016-09-03 20:14:52 +02:00
bool ConnectionSettings : : loadHttpsCert ( )
{
if ( ! httpsCertPath . isEmpty ( ) ) {
const QList < QSslCertificate > cert = QSslCertificate : : fromPath ( httpsCertPath ) ;
if ( cert . isEmpty ( ) ) {
return false ;
}
expectedSslErrors . clear ( ) ;
expectedSslErrors . reserve ( 4 ) ;
expectedSslErrors < < QSslError ( QSslError : : UnableToGetLocalIssuerCertificate , cert . at ( 0 ) ) ;
expectedSslErrors < < QSslError ( QSslError : : UnableToVerifyFirstCertificate , cert . at ( 0 ) ) ;
expectedSslErrors < < QSslError ( QSslError : : SelfSignedCertificate , cert . at ( 0 ) ) ;
expectedSslErrors < < QSslError ( QSslError : : HostNameMismatch , cert . at ( 0 ) ) ;
}
return true ;
}
2016-08-25 00:45:32 +02:00
}