From 3335350a81722660fcb121d864d90a3576fd6aae Mon Sep 17 00:00:00 2001 From: Martchus Date: Thu, 29 Sep 2016 21:19:54 +0200 Subject: [PATCH] Refactor to be able to build data classes as extra lib --- CMakeLists.txt | 2 ++ application/main.cpp | 4 +-- application/settings.cpp | 36 +++++++------------ application/settings.h | 21 +++-------- data/syncthingconnection.cpp | 5 ++- data/syncthingconnection.h | 8 ++--- data/syncthingconnectionsettings.cpp | 22 ++++++++++++ data/syncthingconnectionsettings.h | 26 ++++++++++++++ data/syncthingprocess.cpp | 28 +++++++-------- data/syncthingprocess.h | 6 ++-- data/utils.cpp | 2 +- gui/settingsdialog.cpp | 8 ++--- gui/settingsdialog.h | 4 +-- gui/traywidget.cpp | 2 +- gui/traywidget.h | 2 +- gui/webviewdialog.cpp | 2 +- gui/webviewdialog.h | 8 ++--- translations/syncthingtray_de_DE.ts | 54 ++++++++++++++-------------- translations/syncthingtray_en_US.ts | 54 ++++++++++++++-------------- 19 files changed, 159 insertions(+), 135 deletions(-) create mode 100644 data/syncthingconnectionsettings.cpp create mode 100644 data/syncthingconnectionsettings.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 4909528..1b46236 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,7 @@ set(META_APP_VERSION ${META_VERSION_MAJOR}.${META_VERSION_MINOR}.${META_VERSION_ # add project files set(HEADER_FILES data/syncthingconnection.h + data/syncthingconnectionsettings.h data/syncthingdirectorymodel.h data/syncthingdevicemodel.h data/syncthingdownloadmodel.h @@ -26,6 +27,7 @@ set(HEADER_FILES ) set(SRC_FILES data/syncthingconnection.cpp + data/syncthingconnectionsettings.cpp data/syncthingdirectorymodel.cpp data/syncthingdevicemodel.cpp data/syncthingdownloadmodel.cpp diff --git a/application/main.cpp b/application/main.cpp index c169478..f8ea253 100644 --- a/application/main.cpp +++ b/application/main.cpp @@ -31,7 +31,7 @@ int initSyncthingTray(bool windowed, bool waitForTray) { if(windowed) { if(Settings::launchSynchting()) { - syncthingProcess().startSyncthing(); + syncthingProcess().startSyncthing(Settings::syncthingCmd()); } auto *trayWidget = new TrayWidget; trayWidget->setAttribute(Qt::WA_DeleteOnClose); @@ -40,7 +40,7 @@ int initSyncthingTray(bool windowed, bool waitForTray) #ifndef QT_NO_SYSTEMTRAYICON if(QSystemTrayIcon::isSystemTrayAvailable() || waitForTray) { if(Settings::launchSynchting()) { - syncthingProcess().startSyncthing(); + syncthingProcess().startSyncthing(Settings::syncthingCmd()); } auto *trayIcon = new TrayIcon; trayIcon->show(); diff --git a/application/settings.cpp b/application/settings.cpp index b8a4ec8..061592d 100644 --- a/application/settings.cpp +++ b/application/settings.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -12,7 +13,7 @@ #include using namespace std; -using namespace Media; +using namespace Data; namespace Settings { @@ -23,15 +24,15 @@ bool &firstLaunch() } // connection -ConnectionSettings &primaryConnectionSettings() +SyncthingConnectionSettings &primaryConnectionSettings() { - static ConnectionSettings v; + static SyncthingConnectionSettings v; return v; } -std::vector &secondaryConnectionSettings() +std::vector &secondaryConnectionSettings() { - static vector v; + static vector v; return v; } @@ -94,6 +95,10 @@ QString &syncthingArgs() static QString v; return v; } +QString syncthingCmd() +{ + return syncthingPath() % QChar(' ') % syncthingArgs(); +} // web view #if defined(SYNCTHINGTRAY_USE_WEBENGINE) || defined(SYNCTHINGTRAY_USE_WEBKIT) @@ -137,7 +142,7 @@ void restore() secondaryConnectionSettings().clear(); secondaryConnectionSettings().reserve(static_cast(connectionCount)); for(int i = 0; i < connectionCount; ++i) { - ConnectionSettings *connectionSettings; + SyncthingConnectionSettings *connectionSettings; if(i == 0) { connectionSettings = &primaryConnectionSettings(); } else { @@ -202,7 +207,7 @@ void save() const int connectionCount = static_cast(1 + secondaryConnectionSettings().size()); settings.beginWriteArray(QStringLiteral("connections"), connectionCount); for(int i = 0; i < connectionCount; ++i) { - const ConnectionSettings *connectionSettings = (i == 0 ? &primaryConnectionSettings() : &secondaryConnectionSettings()[static_cast(i - 1)]); + const SyncthingConnectionSettings *connectionSettings = (i == 0 ? &primaryConnectionSettings() : &secondaryConnectionSettings()[static_cast(i - 1)]); settings.setArrayIndex(i); settings.setValue(QStringLiteral("label"), connectionSettings->label); settings.setValue(QStringLiteral("syncthingUrl"), connectionSettings->syncthingUrl); @@ -243,21 +248,4 @@ void save() qtSettings().save(settings); } -bool ConnectionSettings::loadHttpsCert() -{ - if(!httpsCertPath.isEmpty()) { - const QList 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; -} - } diff --git a/application/settings.h b/application/settings.h index dbd0b1d..44dfc59 100644 --- a/application/settings.h +++ b/application/settings.h @@ -1,11 +1,12 @@ #ifndef SETTINGS_H #define SETTINGS_H +#include "../data/syncthingconnectionsettings.h" + #include #include #include -#include #include @@ -25,21 +26,8 @@ namespace Settings { bool &firstLaunch(); // connection -struct ConnectionSettings { - QString label; - QString syncthingUrl; - bool authEnabled = false; - QString userName; - QString password; - QByteArray apiKey; - int trafficPollInterval = 2000; - int devStatsPollInterval = 60000; - QString httpsCertPath; - QList expectedSslErrors; - bool loadHttpsCert(); -}; -ConnectionSettings &primaryConnectionSettings(); -std::vector &secondaryConnectionSettings(); +Data::SyncthingConnectionSettings &primaryConnectionSettings(); +std::vector &secondaryConnectionSettings(); // notifications bool ¬ifyOnDisconnect(); @@ -56,6 +44,7 @@ int &frameStyle(); bool &launchSynchting(); QString &syncthingPath(); QString &syncthingArgs(); +QString syncthingCmd(); // web view #if defined(SYNCTHINGTRAY_USE_WEBENGINE) || defined(SYNCTHINGTRAY_USE_WEBKIT) diff --git a/data/syncthingconnection.cpp b/data/syncthingconnection.cpp index c617b15..9150cdb 100644 --- a/data/syncthingconnection.cpp +++ b/data/syncthingconnection.cpp @@ -1,7 +1,6 @@ #include "./syncthingconnection.h" #include "./syncthingconfig.h" - -#include "../application/settings.h" +#include "./syncthingconnectionsettings.h" #include #include @@ -231,7 +230,7 @@ void SyncthingConnection::reconnect() * \brief Applies the specifies configuration and tries to reconnect via reconnect(). * \remarks The expected SSL errors of the specified configuration are updated accordingly. */ -void SyncthingConnection::reconnect(Settings::ConnectionSettings &connectionSettings) +void SyncthingConnection::reconnect(SyncthingConnectionSettings &connectionSettings) { setSyncthingUrl(connectionSettings.syncthingUrl); setApiKey(connectionSettings.apiKey); diff --git a/data/syncthingconnection.h b/data/syncthingconnection.h index 7a729d2..6cc9f07 100644 --- a/data/syncthingconnection.h +++ b/data/syncthingconnection.h @@ -18,12 +18,10 @@ QT_FORWARD_DECLARE_CLASS(QUrlQuery) QT_FORWARD_DECLARE_CLASS(QJsonObject) QT_FORWARD_DECLARE_CLASS(QJsonArray) -namespace Settings { -struct ConnectionSettings; -} - namespace Data { +struct SyncthingConnectionSettings; + QNetworkAccessManager &networkAccessManager(); enum class SyncthingStatus @@ -200,7 +198,7 @@ public Q_SLOTS: void connect(); void disconnect(); void reconnect(); - void reconnect(Settings::ConnectionSettings &connectionSettings); + void reconnect(SyncthingConnectionSettings &connectionSettings); void pause(const QString &devId); void pauseAllDevs(); void resume(const QString &devId); diff --git a/data/syncthingconnectionsettings.cpp b/data/syncthingconnectionsettings.cpp new file mode 100644 index 0000000..afd3058 --- /dev/null +++ b/data/syncthingconnectionsettings.cpp @@ -0,0 +1,22 @@ +#include "./syncthingconnectionsettings.h" + +namespace Data { + +bool SyncthingConnectionSettings::loadHttpsCert() +{ + if(!httpsCertPath.isEmpty()) { + const QList 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; +} + +} diff --git a/data/syncthingconnectionsettings.h b/data/syncthingconnectionsettings.h new file mode 100644 index 0000000..e98581e --- /dev/null +++ b/data/syncthingconnectionsettings.h @@ -0,0 +1,26 @@ +#ifndef SYNCTHINGCONNECTIONSETTINGS_H +#define SYNCTHINGCONNECTIONSETTINGS_H + +#include +#include +#include + +namespace Data { + +struct SyncthingConnectionSettings { + QString label; + QString syncthingUrl; + bool authEnabled = false; + QString userName; + QString password; + QByteArray apiKey; + int trafficPollInterval = 2000; + int devStatsPollInterval = 60000; + QString httpsCertPath; + QList expectedSslErrors; + bool loadHttpsCert(); +}; + +} + +#endif // SYNCTHINGCONNECTIONSETTINGS_H diff --git a/data/syncthingprocess.cpp b/data/syncthingprocess.cpp index 99f354b..a4a2195 100644 --- a/data/syncthingprocess.cpp +++ b/data/syncthingprocess.cpp @@ -1,36 +1,36 @@ #include "./syncthingprocess.h" -#include "../application/settings.h" - #include -#include namespace Data { SyncthingProcess::SyncthingProcess(QObject *parent) : - QProcess(parent), - m_restarting(false) + QProcess(parent) { setProcessChannelMode(QProcess::MergedChannels); connect(this, static_cast(&SyncthingProcess::finished), this, &SyncthingProcess::handleFinished); } -void SyncthingProcess::restartSyncthing() +void SyncthingProcess::restartSyncthing(const QString &cmd) { if(state() == QProcess::Running) { - m_restarting = true; + m_cmd = cmd; // give Syncthing 5 seconds to terminate, otherwise kill it QTimer::singleShot(5000, this, SLOT(killToRestart())); terminate(); } else { - startSyncthing(); + startSyncthing(cmd); } } -void SyncthingProcess::startSyncthing() +void SyncthingProcess::startSyncthing(const QString &cmd) { if(state() == QProcess::NotRunning) { - start(Settings::syncthingPath() % QChar(' ') % Settings::syncthingArgs(), QProcess::ReadOnly); + if(cmd.isEmpty()) { + start(QProcess::ReadOnly); + } else { + start(cmd, QProcess::ReadOnly); + } } } @@ -38,15 +38,15 @@ void SyncthingProcess::handleFinished(int exitCode, QProcess::ExitStatus exitSta { Q_UNUSED(exitCode) Q_UNUSED(exitStatus) - if(m_restarting) { - m_restarting = false; - startSyncthing(); + if(!m_cmd.isEmpty()) { + startSyncthing(m_cmd); + m_cmd.clear(); } } void SyncthingProcess::killToRestart() { - if(m_restarting) { + if(!m_cmd.isEmpty()) { kill(); } } diff --git a/data/syncthingprocess.h b/data/syncthingprocess.h index 7f25268..a9ab013 100644 --- a/data/syncthingprocess.h +++ b/data/syncthingprocess.h @@ -12,15 +12,15 @@ public: SyncthingProcess(QObject *parent = nullptr); public Q_SLOTS: - void restartSyncthing(); - void startSyncthing(); + void restartSyncthing(const QString &cmd); + void startSyncthing(const QString &cmd); private Q_SLOTS: void handleFinished(int exitCode, QProcess::ExitStatus exitStatus); void killToRestart(); private: - bool m_restarting; + QString m_cmd; }; SyncthingProcess &syncthingProcess(); diff --git a/data/utils.cpp b/data/utils.cpp index 2a64fbe..f0e92f6 100644 --- a/data/utils.cpp +++ b/data/utils.cpp @@ -1,4 +1,4 @@ -#include "utils.h" +#include "./utils.h" #include diff --git a/gui/settingsdialog.cpp b/gui/settingsdialog.cpp index ebc953d..22849e7 100644 --- a/gui/settingsdialog.cpp +++ b/gui/settingsdialog.cpp @@ -110,7 +110,7 @@ bool ConnectionOptionPage::showConnectionSettings(int index) bool ok = true; if(index != m_currentIndex) { if((ok = cacheCurrentSettings(false))) { - const ConnectionSettings &connectionSettings = (index == 0 ? m_primarySettings : m_secondarySettings[static_cast(index - 1)]); + const SyncthingConnectionSettings &connectionSettings = (index == 0 ? m_primarySettings : m_secondarySettings[static_cast(index - 1)]); ui()->urlLineEdit->setText(connectionSettings.syncthingUrl); ui()->authCheckBox->setChecked(connectionSettings.authEnabled); ui()->userNameLineEdit->setText(connectionSettings.userName); @@ -133,7 +133,7 @@ bool ConnectionOptionPage::cacheCurrentSettings(bool applying) { bool ok = true; if(m_currentIndex >= 0) { - ConnectionSettings &connectionSettings = (m_currentIndex == 0 ? m_primarySettings : m_secondarySettings[static_cast(m_currentIndex - 1)]); + SyncthingConnectionSettings &connectionSettings = (m_currentIndex == 0 ? m_primarySettings : m_secondarySettings[static_cast(m_currentIndex - 1)]); connectionSettings.syncthingUrl = ui()->urlLineEdit->text(); connectionSettings.authEnabled = ui()->authCheckBox->isChecked(); connectionSettings.userName = ui()->userNameLineEdit->text(); @@ -204,7 +204,7 @@ void ConnectionOptionPage::reset() QStringList itemTexts; itemTexts.reserve(1 + static_cast(m_secondarySettings.size())); itemTexts << m_primarySettings.label; - for(const ConnectionSettings &settings : m_secondarySettings) { + for(const SyncthingConnectionSettings &settings : m_secondarySettings) { itemTexts << settings.label; } ui()->selectionComboBox->clear(); @@ -505,7 +505,7 @@ void LauncherOptionPage::launch() ui()->launchNowPushButton->hide(); ui()->stopPushButton->show(); m_kill = false; - syncthingProcess().startSyncthing(); + syncthingProcess().startSyncthing(Settings::syncthingCmd()); } } } diff --git a/gui/settingsdialog.h b/gui/settingsdialog.h index fe0b49a..916c0c1 100644 --- a/gui/settingsdialog.h +++ b/gui/settingsdialog.h @@ -30,8 +30,8 @@ private: void addConnectionSettings(); void removeConnectionSettings(); Data::SyncthingConnection *m_connection; - Settings::ConnectionSettings m_primarySettings; - std::vector m_secondarySettings; + Data::SyncthingConnectionSettings m_primarySettings; + std::vector m_secondarySettings; int m_currentIndex; END_DECLARE_OPTION_PAGE diff --git a/gui/traywidget.cpp b/gui/traywidget.cpp index 8e8310e..9148da5 100644 --- a/gui/traywidget.cpp +++ b/gui/traywidget.cpp @@ -297,7 +297,7 @@ void TrayWidget::applySettings() const QList connectionActions = instance->m_connectionsActionGroup->actions(); instance->m_selectedConnection = nullptr; for(; connectionIndex < connectionCount; ++connectionIndex) { - Settings::ConnectionSettings &connectionSettings = (connectionIndex == 0 ? Settings::primaryConnectionSettings() : Settings::secondaryConnectionSettings()[static_cast(connectionIndex - 1)]); + SyncthingConnectionSettings &connectionSettings = (connectionIndex == 0 ? Settings::primaryConnectionSettings() : Settings::secondaryConnectionSettings()[static_cast(connectionIndex - 1)]); if(connectionIndex < connectionActions.size()) { QAction *action = connectionActions.at(connectionIndex); action->setText(connectionSettings.label); diff --git a/gui/traywidget.h b/gui/traywidget.h index a17200d..33d34f5 100644 --- a/gui/traywidget.h +++ b/gui/traywidget.h @@ -88,7 +88,7 @@ private: Data::SyncthingDownloadModel m_dlModel; QMenu *m_connectionsMenu; QActionGroup *m_connectionsActionGroup; - Settings::ConnectionSettings *m_selectedConnection; + Data::SyncthingConnectionSettings *m_selectedConnection; std::vector m_notifications; static std::vector m_instances; }; diff --git a/gui/webviewdialog.cpp b/gui/webviewdialog.cpp index c386f92..877634d 100644 --- a/gui/webviewdialog.cpp +++ b/gui/webviewdialog.cpp @@ -42,7 +42,7 @@ QtGui::WebViewDialog::~WebViewDialog() Settings::webViewGeometry() = saveGeometry(); } -void QtGui::WebViewDialog::applySettings(const Settings::ConnectionSettings &connectionSettings) +void QtGui::WebViewDialog::applySettings(const Data::SyncthingConnectionSettings &connectionSettings) { m_settings = connectionSettings; m_view->setUrl(connectionSettings.syncthingUrl); diff --git a/gui/webviewdialog.h b/gui/webviewdialog.h index c672cf9..61ff81d 100644 --- a/gui/webviewdialog.h +++ b/gui/webviewdialog.h @@ -24,18 +24,18 @@ public: ~WebViewDialog(); public slots: - void applySettings(const Settings::ConnectionSettings &connectionSettings); - const Settings::ConnectionSettings &settings() const; + void applySettings(const Data::SyncthingConnectionSettings &connectionSettings); + const Data::SyncthingConnectionSettings &settings() const; protected: void closeEvent(QCloseEvent *event); private: WEB_VIEW_PROVIDER *m_view; - Settings::ConnectionSettings m_settings; + Data::SyncthingConnectionSettings m_settings; }; -inline const Settings::ConnectionSettings &WebViewDialog::settings() const +inline const Data::SyncthingConnectionSettings &WebViewDialog::settings() const { return m_settings; } diff --git a/translations/syncthingtray_de_DE.ts b/translations/syncthingtray_de_DE.ts index 9eb8456..1eecc7c 100644 --- a/translations/syncthingtray_de_DE.ts +++ b/translations/syncthingtray_de_DE.ts @@ -45,104 +45,104 @@ - + Unable to parse Syncthing log: - + Unable to request system log: - + Unable to locate certificate used by Syncthing GUI. - + Unable to load certificate used by Syncthing GUI. - - + + Unable to parse Syncthing config: - - + + Unable to request Syncthing config: - + Unable to parse connections: - + Unable to request connections: - + Unable to parse directory statistics: - + Unable to request directory statistics: - + Unable to parse device statistics: - + Unable to request device statistics: - + Unable to parse errors: - + Unable to request errors: - + Unable to parse Syncthing events: - + Unable to request Syncthing events: - + Unable to request rescan: - + Unable to request pause/resume: - + Unable to request restart: - + Unable to request QR-Code: @@ -364,12 +364,12 @@ Data::SyncthingDownloadModel - + Dir/item - + Progress @@ -661,12 +661,12 @@ QtGui::DownloadView - + Copy value - + Copy label/ID @@ -1095,7 +1095,7 @@ The Web UI will be opened in the default web browser instead. Settings::restore - + Unable to load certificate "%1" when restoring settings. diff --git a/translations/syncthingtray_en_US.ts b/translations/syncthingtray_en_US.ts index 55be337..fb74e1e 100644 --- a/translations/syncthingtray_en_US.ts +++ b/translations/syncthingtray_en_US.ts @@ -45,104 +45,104 @@ - + Unable to parse Syncthing log: - + Unable to request system log: - + Unable to locate certificate used by Syncthing GUI. - + Unable to load certificate used by Syncthing GUI. - - + + Unable to parse Syncthing config: - - + + Unable to request Syncthing config: - + Unable to parse connections: - + Unable to request connections: - + Unable to parse directory statistics: - + Unable to request directory statistics: - + Unable to parse device statistics: - + Unable to request device statistics: - + Unable to parse errors: - + Unable to request errors: - + Unable to parse Syncthing events: - + Unable to request Syncthing events: - + Unable to request rescan: - + Unable to request pause/resume: - + Unable to request restart: - + Unable to request QR-Code: @@ -364,12 +364,12 @@ Data::SyncthingDownloadModel - + Dir/item - + Progress @@ -661,12 +661,12 @@ QtGui::DownloadView - + Copy value - + Copy label/ID @@ -1095,7 +1095,7 @@ The Web UI will be opened in the default web browser instead. Settings::restore - + Unable to load certificate "%1" when restoring settings.