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>
2016-12-11 17:58:58 +01:00
# ifdef QT_UTILITIES_SUPPORT_DBUS_NOTIFICATIONS
# include <qtutilities / misc / dbusnotification.h>
# endif
2016-08-25 00:45:32 +02:00
2016-09-29 21:19:54 +02:00
# include <QStringBuilder>
2016-08-25 00:45:32 +02:00
# include <QApplication>
# include <QSettings>
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-09-29 21:19:54 +02:00
using namespace Data ;
2016-12-11 17:58:58 +01:00
# ifdef QT_UTILITIES_SUPPORT_DBUS_NOTIFICATIONS
using namespace MiscUtils ;
# endif
2016-08-25 00:45:32 +02:00
namespace Settings {
2016-11-02 20:03:38 +01:00
QString Launcher : : syncthingCmd ( ) const
2016-09-01 16:34:30 +02:00
{
2016-11-02 20:03:38 +01:00
return syncthingPath % QChar ( ' ' ) % syncthingArgs ;
2016-09-01 16:34:30 +02:00
}
2016-11-02 20:03:38 +01:00
Settings & values ( )
2016-08-25 00:45:32 +02:00
{
2016-11-02 20:03:38 +01:00
static Settings settings ;
return settings ;
2016-08-25 00:45:32 +02:00
}
void restore ( )
{
QSettings settings ( QSettings : : IniFormat , QSettings : : UserScope , QApplication : : organizationName ( ) , QApplication : : applicationName ( ) ) ;
2016-11-02 20:03:38 +01:00
Settings & v = values ( ) ;
2016-08-25 00:45:32 +02:00
settings . beginGroup ( QStringLiteral ( " tray " ) ) ;
2016-09-03 20:14:52 +02:00
const int connectionCount = settings . beginReadArray ( QStringLiteral ( " connections " ) ) ;
2016-11-02 20:03:38 +01:00
auto & primaryConnectionSettings = v . connection . primary ;
2016-09-03 20:14:52 +02:00
if ( connectionCount > 0 ) {
2016-11-02 20:03:38 +01:00
auto & secondaryConnectionSettings = v . connection . secondary ;
secondaryConnectionSettings . clear ( ) ;
secondaryConnectionSettings . reserve ( static_cast < size_t > ( connectionCount ) ) ;
2016-09-03 20:14:52 +02:00
for ( int i = 0 ; i < connectionCount ; + + i ) {
2016-09-29 21:19:54 +02:00
SyncthingConnectionSettings * connectionSettings ;
2016-09-03 20:14:52 +02:00
if ( i = = 0 ) {
2016-11-02 20:03:38 +01:00
connectionSettings = & primaryConnectionSettings ;
2016-09-03 20:14:52 +02:00
} else {
2016-11-02 20:03:38 +01:00
secondaryConnectionSettings . emplace_back ( ) ;
connectionSettings = & secondaryConnectionSettings . back ( ) ;
2016-09-03 20:14:52 +02:00
}
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 ( ) ;
2017-01-12 22:38:36 +01:00
connectionSettings - > errorsPollInterval = settings . value ( QStringLiteral ( " errorsPollInterval " ) , connectionSettings - > errorsPollInterval ) . toInt ( ) ;
2016-10-07 15:11:25 +02:00
connectionSettings - > reconnectInterval = settings . value ( QStringLiteral ( " reconnectInterval " ) , connectionSettings - > reconnectInterval ) . 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 {
2016-11-02 20:03:38 +01:00
v . firstLaunch = true ;
primaryConnectionSettings . label = QStringLiteral ( " Primary instance " ) ;
2016-09-03 20:14:52 +02:00
}
settings . endArray ( ) ;
2016-11-02 20:03:38 +01:00
auto & notifyOn = v . notifyOn ;
notifyOn . disconnect = settings . value ( QStringLiteral ( " notifyOnDisconnect " ) , notifyOn . disconnect ) . toBool ( ) ;
notifyOn . internalErrors = settings . value ( QStringLiteral ( " notifyOnErrors " ) , notifyOn . internalErrors ) . toBool ( ) ;
notifyOn . syncComplete = settings . value ( QStringLiteral ( " notifyOnSyncComplete " ) , notifyOn . syncComplete ) . toBool ( ) ;
notifyOn . syncthingErrors = settings . value ( QStringLiteral ( " showSyncthingNotifications " ) , notifyOn . syncthingErrors ) . toBool ( ) ;
2016-12-11 17:58:58 +01:00
# ifdef QT_UTILITIES_SUPPORT_DBUS_NOTIFICATIONS
v . dbusNotifications = settings . value ( QStringLiteral ( " dbusNotifications " ) , DBusNotification : : isAvailable ( ) ) . toBool ( ) ;
# endif
2016-11-02 20:03:38 +01:00
auto & appearance = v . appearance ;
appearance . showTraffic = settings . value ( QStringLiteral ( " showTraffic " ) , appearance . showTraffic ) . toBool ( ) ;
appearance . trayMenuSize = settings . value ( QStringLiteral ( " trayMenuSize " ) , appearance . trayMenuSize ) . toSize ( ) ;
appearance . frameStyle = settings . value ( QStringLiteral ( " frameStyle " ) , appearance . frameStyle ) . toInt ( ) ;
appearance . tabPosition = settings . value ( QStringLiteral ( " tabPos " ) , appearance . tabPosition ) . toInt ( ) ;
2016-11-02 21:05:32 +01:00
appearance . brightTextColors = settings . value ( QStringLiteral ( " brightTextColors " ) , appearance . brightTextColors ) . toBool ( ) ;
2016-09-03 19:39:43 +02:00
settings . endGroup ( ) ;
settings . beginGroup ( QStringLiteral ( " startup " ) ) ;
2016-11-02 20:03:38 +01:00
auto & launcher = v . launcher ;
launcher . enabled = settings . value ( QStringLiteral ( " launchSynchting " ) , launcher . enabled ) . toBool ( ) ;
launcher . syncthingPath = settings . value ( QStringLiteral ( " syncthingPath " ) , launcher . syncthingPath ) . toString ( ) ;
launcher . syncthingArgs = settings . value ( QStringLiteral ( " syncthingArgs " ) , launcher . syncthingArgs ) . toString ( ) ;
2016-12-18 16:50:35 +01:00
# ifdef LIB_SYNCTHING_CONNECTOR_SUPPORT_SYSTEMD
auto & systemd = v . systemd ;
systemd . syncthingUnit = settings . value ( QStringLiteral ( " syncthingUnit " ) , systemd . syncthingUnit ) . toString ( ) ;
systemd . showButton = settings . value ( QStringLiteral ( " showButton " ) , systemd . showButton ) . toBool ( ) ;
2016-12-27 16:16:12 +01:00
systemd . considerForReconnect = settings . value ( QStringLiteral ( " considerForReconnect " ) , systemd . considerForReconnect ) . toBool ( ) ;
2016-12-18 16:50:35 +01:00
# endif
2016-08-25 00:45:32 +02:00
settings . endGroup ( ) ;
# if defined(SYNCTHINGTRAY_USE_WEBENGINE) || defined(SYNCTHINGTRAY_USE_WEBKIT)
settings . beginGroup ( QStringLiteral ( " webview " ) ) ;
2016-11-02 20:03:38 +01:00
auto & webView = v . webView ;
2017-01-07 16:15:13 +01:00
webView . disabled = settings . value ( QStringLiteral ( " disabled " ) , webView . disabled ) . toBool ( ) ;
2016-11-02 20:03:38 +01:00
webView . zoomFactor = settings . value ( QStringLiteral ( " zoomFactor " ) , webView . zoomFactor ) . toDouble ( ) ;
webView . geometry = settings . value ( QStringLiteral ( " geometry " ) ) . toByteArray ( ) ;
webView . keepRunning = settings . value ( QStringLiteral ( " keepRunning " ) , webView . keepRunning ) . toBool ( ) ;
2016-08-25 00:45:32 +02:00
settings . endGroup ( ) ;
# endif
2016-11-02 20:03:38 +01:00
v . qt . restore ( settings ) ;
2016-08-25 00:45:32 +02:00
}
void save ( )
{
QSettings settings ( QSettings : : IniFormat , QSettings : : UserScope , QApplication : : organizationName ( ) , QApplication : : applicationName ( ) ) ;
2016-11-02 20:03:38 +01:00
const Settings & v = values ( ) ;
2016-08-25 00:45:32 +02:00
settings . beginGroup ( QStringLiteral ( " tray " ) ) ;
2016-11-02 20:03:38 +01:00
const auto & primaryConnectionSettings = v . connection . primary ;
const auto & secondaryConnectionSettings = v . connection . secondary ;
const int connectionCount = static_cast < int > ( 1 + secondaryConnectionSettings . size ( ) ) ;
2016-09-03 20:14:52 +02:00
settings . beginWriteArray ( QStringLiteral ( " connections " ) , connectionCount ) ;
for ( int i = 0 ; i < connectionCount ; + + i ) {
2016-11-02 20:03:38 +01:00
const SyncthingConnectionSettings * connectionSettings = ( i = = 0 ? & primaryConnectionSettings : & secondaryConnectionSettings [ static_cast < size_t > ( i - 1 ) ] ) ;
2016-09-03 20:14:52 +02:00
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 ) ;
2017-01-12 22:38:36 +01:00
settings . setValue ( QStringLiteral ( " errorsPollInterval " ) , connectionSettings - > errorsPollInterval ) ;
2016-10-07 15:11:25 +02:00
settings . setValue ( QStringLiteral ( " reconnectInterval " ) , connectionSettings - > reconnectInterval ) ;
2016-09-03 20:14:52 +02:00
settings . setValue ( QStringLiteral ( " httpsCertPath " ) , connectionSettings - > httpsCertPath ) ;
}
settings . endArray ( ) ;
2016-11-02 20:03:38 +01:00
const auto & notifyOn = v . notifyOn ;
settings . setValue ( QStringLiteral ( " notifyOnDisconnect " ) , notifyOn . disconnect ) ;
settings . setValue ( QStringLiteral ( " notifyOnErrors " ) , notifyOn . internalErrors ) ;
settings . setValue ( QStringLiteral ( " notifyOnSyncComplete " ) , notifyOn . syncComplete ) ;
settings . setValue ( QStringLiteral ( " showSyncthingNotifications " ) , notifyOn . syncthingErrors ) ;
2016-12-11 17:58:58 +01:00
# ifdef QT_UTILITIES_SUPPORT_DBUS_NOTIFICATIONS
settings . setValue ( QStringLiteral ( " dbusNotifications " ) , v . dbusNotifications ) ;
# endif
2016-11-02 20:03:38 +01:00
const auto & appearance = v . appearance ;
settings . setValue ( QStringLiteral ( " showTraffic " ) , appearance . showTraffic ) ;
settings . setValue ( QStringLiteral ( " trayMenuSize " ) , appearance . trayMenuSize ) ;
settings . setValue ( QStringLiteral ( " frameStyle " ) , appearance . frameStyle ) ;
settings . setValue ( QStringLiteral ( " tabPos " ) , appearance . tabPosition ) ;
2016-11-02 21:05:32 +01:00
settings . setValue ( QStringLiteral ( " brightTextColors " ) , appearance . brightTextColors ) ;
2016-09-03 19:39:43 +02:00
settings . endGroup ( ) ;
settings . beginGroup ( QStringLiteral ( " startup " ) ) ;
2016-11-02 20:03:38 +01:00
const auto & launcher = v . launcher ;
settings . setValue ( QStringLiteral ( " launchSynchting " ) , launcher . enabled ) ;
settings . setValue ( QStringLiteral ( " syncthingPath " ) , launcher . syncthingPath ) ;
settings . setValue ( QStringLiteral ( " syncthingArgs " ) , launcher . syncthingArgs ) ;
2016-12-18 16:50:35 +01:00
# ifdef LIB_SYNCTHING_CONNECTOR_SUPPORT_SYSTEMD
const auto & systemd = v . systemd ;
settings . setValue ( QStringLiteral ( " syncthingUnit " ) , systemd . syncthingUnit ) ;
settings . setValue ( QStringLiteral ( " showButton " ) , systemd . showButton ) ;
2016-12-27 16:16:12 +01:00
settings . setValue ( QStringLiteral ( " considerForReconnect " ) , systemd . considerForReconnect ) ;
2016-12-18 16:50:35 +01:00
# endif
2016-08-25 00:45:32 +02:00
settings . endGroup ( ) ;
# if defined(SYNCTHINGTRAY_USE_WEBENGINE) || defined(SYNCTHINGTRAY_USE_WEBKIT)
settings . beginGroup ( QStringLiteral ( " webview " ) ) ;
2016-11-02 20:03:38 +01:00
const auto & webView = v . webView ;
settings . setValue ( QStringLiteral ( " disabled " ) , webView . disabled ) ;
settings . setValue ( QStringLiteral ( " zoomFactor " ) , webView . zoomFactor ) ;
settings . setValue ( QStringLiteral ( " geometry " ) , webView . geometry ) ;
settings . setValue ( QStringLiteral ( " keepRunning " ) , webView . keepRunning ) ;
2016-08-25 00:45:32 +02:00
settings . endGroup ( ) ;
# endif
2016-11-02 20:03:38 +01:00
v . qt . save ( settings ) ;
2016-08-25 00:45:32 +02:00
}
}