2016-08-25 00:45:32 +02:00
# include "./settingsdialog.h"
# include "../application/settings.h"
# include "../data/syncthingconnection.h"
2016-09-01 16:34:30 +02:00
# include "../data/syncthingconfig.h"
2016-08-25 00:45:32 +02:00
# include "ui_connectionoptionpage.h"
# include "ui_notificationsoptionpage.h"
2016-08-29 20:51:30 +02:00
# include "ui_appearanceoptionpage.h"
2016-08-30 20:01:07 +02:00
# include "ui_autostartoptionpage.h"
2016-08-25 00:45:32 +02:00
# include "ui_webviewoptionpage.h"
2016-09-01 16:34:30 +02:00
# include "resources/config.h"
2016-08-25 00:45:32 +02:00
# include <tagparser/mediafileinfo.h>
# include <tagparser/backuphelper.h>
# include <qtutilities/settingsdialog/optioncategory.h>
# include <qtutilities/settingsdialog/optioncategorymodel.h>
# include <qtutilities/settingsdialog/qtsettings.h>
2016-09-01 16:34:30 +02:00
# include <QFileDialog>
# include <QMessageBox>
# include <QHostAddress>
2016-08-25 00:45:32 +02:00
# include <functional>
using namespace std ;
using namespace Settings ;
using namespace Dialogs ;
using namespace Data ;
namespace QtGui {
// ConnectionOptionPage
ConnectionOptionPage : : ConnectionOptionPage ( Data : : SyncthingConnection * connection , QWidget * parentWidget ) :
ConnectionOptionPageBase ( parentWidget ) ,
m_connection ( connection )
{ }
ConnectionOptionPage : : ~ ConnectionOptionPage ( )
{ }
QWidget * ConnectionOptionPage : : setupWidget ( )
{
auto * w = ConnectionOptionPageBase : : setupWidget ( ) ;
updateConnectionStatus ( ) ;
QObject : : connect ( m_connection , & SyncthingConnection : : statusChanged , bind ( & ConnectionOptionPage : : updateConnectionStatus , this ) ) ;
QObject : : connect ( ui ( ) - > connectPushButton , & QPushButton : : clicked , bind ( & ConnectionOptionPage : : applyAndReconnect , this ) ) ;
2016-09-01 16:34:30 +02:00
QObject : : connect ( ui ( ) - > insertFromConfigFilePushButton , & QPushButton : : clicked , bind ( & ConnectionOptionPage : : insertFromConfigFile , this ) ) ;
2016-08-25 00:45:32 +02:00
return w ;
}
2016-09-01 16:34:30 +02:00
void ConnectionOptionPage : : insertFromConfigFile ( )
{
if ( hasBeenShown ( ) ) {
QString configFile = SyncthingConfig : : locateConfigFile ( ) ;
if ( configFile . isEmpty ( ) ) {
// allow user to select config file manually if it could not be located
configFile = QFileDialog : : getOpenFileName ( widget ( ) , QCoreApplication : : translate ( " QtGui::ConnectionOptionPage " , " Select Syncthing config file " ) + QStringLiteral ( " - " APP_NAME ) ) ;
}
if ( configFile . isEmpty ( ) ) {
return ;
}
SyncthingConfig config ;
if ( ! config . restore ( configFile ) ) {
QMessageBox : : critical ( widget ( ) , widget ( ) - > windowTitle ( ) + QStringLiteral ( " - " APP_NAME ) , QCoreApplication : : translate ( " QtGui::ConnectionOptionPage " , " Unable to parse the Syncthing config file. " ) ) ;
return ;
}
if ( ! config . guiAddress . isEmpty ( ) ) {
ui ( ) - > urlLineEdit - > selectAll ( ) ;
ui ( ) - > urlLineEdit - > insert ( ( ( config . guiEnforcesSecureConnection | | ! QHostAddress ( config . guiAddress . mid ( 0 , config . guiAddress . indexOf ( QChar ( ' : ' ) ) ) ) . isLoopback ( ) ) ? QStringLiteral ( " https:// " ) : QStringLiteral ( " http:// " ) ) + config . guiAddress ) ;
}
if ( ! config . guiUser . isEmpty ( ) | | ! config . guiPasswordHash . isEmpty ( ) ) {
ui ( ) - > authCheckBox - > setChecked ( true ) ;
ui ( ) - > userNameLineEdit - > selectAll ( ) ;
ui ( ) - > userNameLineEdit - > insert ( config . guiUser ) ;
} else {
ui ( ) - > authCheckBox - > setChecked ( false ) ;
}
if ( ! config . guiApiKey . isEmpty ( ) ) {
ui ( ) - > apiKeyLineEdit - > selectAll ( ) ;
ui ( ) - > apiKeyLineEdit - > insert ( config . guiApiKey ) ;
}
}
}
2016-08-25 00:45:32 +02:00
void ConnectionOptionPage : : updateConnectionStatus ( )
{
2016-09-01 16:34:30 +02:00
if ( hasBeenShown ( ) ) {
ui ( ) - > statusLabel - > setText ( m_connection - > statusText ( ) ) ;
}
2016-08-25 00:45:32 +02:00
}
bool ConnectionOptionPage : : apply ( )
{
if ( hasBeenShown ( ) ) {
syncthingUrl ( ) = ui ( ) - > urlLineEdit - > text ( ) ;
authEnabled ( ) = ui ( ) - > authCheckBox - > isChecked ( ) ;
userName ( ) = ui ( ) - > userNameLineEdit - > text ( ) ;
password ( ) = ui ( ) - > passwordLineEdit - > text ( ) ;
apiKey ( ) = ui ( ) - > apiKeyLineEdit - > text ( ) . toUtf8 ( ) ;
}
return true ;
}
void ConnectionOptionPage : : reset ( )
{
if ( hasBeenShown ( ) ) {
ui ( ) - > urlLineEdit - > setText ( syncthingUrl ( ) ) ;
ui ( ) - > authCheckBox - > setChecked ( authEnabled ( ) ) ;
ui ( ) - > userNameLineEdit - > setText ( userName ( ) ) ;
ui ( ) - > passwordLineEdit - > setText ( password ( ) ) ;
ui ( ) - > apiKeyLineEdit - > setText ( apiKey ( ) ) ;
}
}
void ConnectionOptionPage : : applyAndReconnect ( )
{
apply ( ) ;
m_connection - > setSyncthingUrl ( Settings : : syncthingUrl ( ) ) ;
m_connection - > setApiKey ( Settings : : apiKey ( ) ) ;
if ( Settings : : authEnabled ( ) ) {
m_connection - > setCredentials ( Settings : : userName ( ) , Settings : : password ( ) ) ;
} else {
m_connection - > setCredentials ( QString ( ) , QString ( ) ) ;
}
m_connection - > reconnect ( ) ;
}
// NotificationsOptionPage
NotificationsOptionPage : : NotificationsOptionPage ( QWidget * parentWidget ) :
NotificationsOptionPageBase ( parentWidget )
{ }
NotificationsOptionPage : : ~ NotificationsOptionPage ( )
{ }
bool NotificationsOptionPage : : apply ( )
{
if ( hasBeenShown ( ) ) {
notifyOnDisconnect ( ) = ui ( ) - > notifyOnDisconnectCheckBox - > isChecked ( ) ;
2016-09-01 16:34:30 +02:00
notifyOnInternalErrors ( ) = ui ( ) - > notifyOnErrorsCheckBox - > isChecked ( ) ;
2016-08-25 00:45:32 +02:00
notifyOnSyncComplete ( ) = ui ( ) - > notifyOnSyncCompleteCheckBox - > isChecked ( ) ;
showSyncthingNotifications ( ) = ui ( ) - > showSyncthingNotificationsCheckBox - > isChecked ( ) ;
}
return true ;
}
void NotificationsOptionPage : : reset ( )
{
if ( hasBeenShown ( ) ) {
ui ( ) - > notifyOnDisconnectCheckBox - > setChecked ( notifyOnDisconnect ( ) ) ;
2016-09-01 16:34:30 +02:00
ui ( ) - > notifyOnErrorsCheckBox - > setChecked ( notifyOnInternalErrors ( ) ) ;
2016-08-25 00:45:32 +02:00
ui ( ) - > notifyOnSyncCompleteCheckBox - > setChecked ( notifyOnSyncComplete ( ) ) ;
ui ( ) - > showSyncthingNotificationsCheckBox - > setChecked ( showSyncthingNotifications ( ) ) ;
}
}
2016-08-29 20:51:30 +02:00
// AppearanceOptionPage
AppearanceOptionPage : : AppearanceOptionPage ( QWidget * parentWidget ) :
AppearanceOptionPageBase ( parentWidget )
{ }
AppearanceOptionPage : : ~ AppearanceOptionPage ( )
{ }
bool AppearanceOptionPage : : apply ( )
{
if ( hasBeenShown ( ) ) {
2016-09-01 16:34:30 +02:00
trayMenuSize ( ) . setWidth ( ui ( ) - > widthSpinBox - > value ( ) ) ;
trayMenuSize ( ) . setHeight ( ui ( ) - > heightSpinBox - > value ( ) ) ;
2016-08-29 20:51:30 +02:00
showTraffic ( ) = ui ( ) - > showTrafficCheckBox - > isChecked ( ) ;
}
return true ;
}
void AppearanceOptionPage : : reset ( )
{
if ( hasBeenShown ( ) ) {
2016-09-01 16:34:30 +02:00
ui ( ) - > widthSpinBox - > setValue ( trayMenuSize ( ) . width ( ) ) ;
ui ( ) - > heightSpinBox - > setValue ( trayMenuSize ( ) . height ( ) ) ;
2016-08-29 20:51:30 +02:00
ui ( ) - > showTrafficCheckBox - > setChecked ( showTraffic ( ) ) ;
}
}
2016-08-25 00:45:32 +02:00
// LauncherOptionPage
2016-08-30 20:01:07 +02:00
AutostartOptionPage : : AutostartOptionPage ( QWidget * parentWidget ) :
AutostartOptionPageBase ( parentWidget )
2016-08-25 00:45:32 +02:00
{ }
2016-08-30 20:01:07 +02:00
AutostartOptionPage : : ~ AutostartOptionPage ( )
2016-08-25 00:45:32 +02:00
{ }
2016-08-30 20:01:07 +02:00
bool AutostartOptionPage : : apply ( )
2016-08-25 00:45:32 +02:00
{
if ( hasBeenShown ( ) ) {
}
return true ;
}
2016-08-30 20:01:07 +02:00
void AutostartOptionPage : : reset ( )
2016-08-25 00:45:32 +02:00
{
if ( hasBeenShown ( ) ) {
}
}
// WebViewOptionPage
WebViewOptionPage : : WebViewOptionPage ( QWidget * parentWidget ) :
WebViewOptionPageBase ( parentWidget )
{ }
WebViewOptionPage : : ~ WebViewOptionPage ( )
{ }
# if !defined(SYNCTHINGTRAY_USE_WEBENGINE) && !defined(SYNCTHINGTRAY_USE_WEBKIT)
QWidget * WebViewOptionPage : : setupWidget ( )
{
auto * label = new QLabel ;
label - > setWindowTitle ( QCoreApplication : : translate ( " QtGui::WebViewOptionPage " , " General " ) ) ;
label - > setAlignment ( Qt : : AlignCenter ) ;
label - > setText ( QCoreApplication : : translate ( " QtGui::WebViewOptionPage " , " Syncthing Tray has not been built with vieb view support utilizing either Qt WebKit or Qt WebEngine. \n The Web UI will be opened in the default web browser instead. " ) ) ;
return label ;
}
# endif
bool WebViewOptionPage : : apply ( )
{
# if defined(SYNCTHINGTRAY_USE_WEBENGINE) || defined(SYNCTHINGTRAY_USE_WEBKIT)
if ( hasBeenShown ( ) ) {
webViewDisabled ( ) = ui ( ) - > disableCheckBox - > isChecked ( ) ;
webViewZoomFactor ( ) = ui ( ) - > zoomDoubleSpinBox - > value ( ) ;
webViewKeepRunning ( ) = ui ( ) - > keepRunningCheckBox - > isChecked ( ) ;
}
# endif
return true ;
}
void WebViewOptionPage : : reset ( )
{
# if defined(SYNCTHINGTRAY_USE_WEBENGINE) || defined(SYNCTHINGTRAY_USE_WEBKIT)
if ( hasBeenShown ( ) ) {
ui ( ) - > disableCheckBox - > setChecked ( webViewDisabled ( ) ) ;
ui ( ) - > zoomDoubleSpinBox - > setValue ( webViewZoomFactor ( ) ) ;
ui ( ) - > keepRunningCheckBox - > setChecked ( webViewKeepRunning ( ) ) ;
}
# endif
}
SettingsDialog : : SettingsDialog ( Data : : SyncthingConnection * connection , QWidget * parent ) :
Dialogs : : SettingsDialog ( parent )
{
// setup categories
QList < Dialogs : : OptionCategory * > categories ;
Dialogs : : OptionCategory * category ;
category = new OptionCategory ( this ) ;
category - > setDisplayName ( tr ( " Tray " ) ) ;
category - > assignPages ( QList < Dialogs : : OptionPage * > ( )
< < new ConnectionOptionPage ( connection ) < < new NotificationsOptionPage
2016-08-30 20:01:07 +02:00
< < new AppearanceOptionPage < < new AutostartOptionPage ) ;
2016-08-25 00:45:32 +02:00
category - > setIcon ( QIcon ( QStringLiteral ( " :/icons/hicolor/scalable/app/syncthingtray.svg " ) ) ) ;
categories < < category ;
category = new OptionCategory ( this ) ;
category - > setDisplayName ( tr ( " Web view " ) ) ;
category - > assignPages ( QList < Dialogs : : OptionPage * > ( ) < < new WebViewOptionPage ) ;
2016-08-29 20:51:30 +02:00
category - > setIcon ( QIcon : : fromTheme ( QStringLiteral ( " internet-web-browser " ) , QIcon ( QStringLiteral ( " :/icons/hicolor/scalable/apps/internet-web-browser.svg " ) ) ) ) ;
2016-08-25 00:45:32 +02:00
categories < < category ;
categories < < Settings : : qtSettings ( ) . category ( ) ;
categoryModel ( ) - > setCategories ( categories ) ;
setMinimumSize ( 800 , 450 ) ;
2016-08-29 20:51:30 +02:00
setWindowIcon ( QIcon : : fromTheme ( QStringLiteral ( " preferences-other " ) , QIcon ( QStringLiteral ( " :/icons/hicolor/scalable/apps/preferences-other.svg " ) ) ) ) ;
2016-08-25 00:45:32 +02:00
// some settings could be applied without restarting the application, good idea?
//connect(this, &Dialogs::SettingsDialog::applied, bind(&Dialogs::QtSettings::apply, &Settings::qtSettings()));
}
SettingsDialog : : ~ SettingsDialog ( )
{ }
}