From 10747bfdf9aa197efa30d9509f6c8a872b089691 Mon Sep 17 00:00:00 2001 From: Martchus Date: Mon, 24 Dec 2018 17:16:28 +0100 Subject: [PATCH] Handle config issues in Dolphin integration * Check whether URL is present * Allow user to enter API key manually --- .../syncthingfileitemactionstaticdata.cpp | 34 ++++++++++++++++--- .../syncthingfileitemactionstaticdata.h | 2 ++ .../testfiles/config-no-api-key.xml | 9 +++++ .../testfiles/config-no-gui-address.xml | 9 +++++ 4 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 fileitemactionplugin/testfiles/config-no-api-key.xml create mode 100644 fileitemactionplugin/testfiles/config-no-gui-address.xml diff --git a/fileitemactionplugin/syncthingfileitemactionstaticdata.cpp b/fileitemactionplugin/syncthingfileitemactionstaticdata.cpp index b2c7a1a..5f37a6c 100644 --- a/fileitemactionplugin/syncthingfileitemactionstaticdata.cpp +++ b/fileitemactionplugin/syncthingfileitemactionstaticdata.cpp @@ -12,6 +12,7 @@ #include #include +#include #include #include @@ -107,6 +108,14 @@ void SyncthingFileItemActionStaticData::selectSyncthingConfig() } } +void SyncthingFileItemActionStaticData::appendNoteToError(QString &errorMessage, const QString &newSyncthingConfigFilePath) const +{ + if (!m_configFilePath.isEmpty() && m_configFilePath != newSyncthingConfigFilePath) { + errorMessage += QChar('\n'); + errorMessage += tr("(still using config from \"%1\")").arg(m_configFilePath); + } +} + bool SyncthingFileItemActionStaticData::applySyncthingConfiguration(const QString &syncthingConfigFilePath) { clearCurrentError(); @@ -121,15 +130,32 @@ bool SyncthingFileItemActionStaticData::applySyncthingConfiguration(const QStrin SyncthingConfig config; if (!config.restore(syncthingConfigFilePath)) { auto errorMessage = tr("Unable to load Syncthing config from \"%1\"").arg(syncthingConfigFilePath); - if (!m_configFilePath.isEmpty() && m_configFilePath != syncthingConfigFilePath) { - errorMessage += QChar('\n'); - errorMessage += tr("(still using config from \"%1\")").arg(m_configFilePath); - } + appendNoteToError(errorMessage, syncthingConfigFilePath); setCurrentError(errorMessage); return false; } cerr << "Syncthing config loaded from \"" << syncthingConfigFilePath.toLocal8Bit().data() << "\"" << endl; + // check whether the URL is present + if (config.guiAddress.isEmpty()) { + auto errorMessage = tr("Syncthing config from \"%1\" does not contain GUI address.").arg(syncthingConfigFilePath); + appendNoteToError(errorMessage, syncthingConfigFilePath); + setCurrentError(errorMessage); + return false; + } + + // check whether the API key is present + if (config.guiApiKey.isEmpty()) { + config.guiApiKey = QInputDialog::getText( + nullptr, tr("Enter API key"), tr("The selected config file does not contain an API key. Please enter the API key manually:")); + if (config.guiApiKey.isEmpty()) { + auto errorMessage = tr("No API key supplied for \"%1\".").arg(config.guiAddress); + appendNoteToError(errorMessage, syncthingConfigFilePath); + setCurrentError(errorMessage); + return false; + } + } + // make connection settings SyncthingConnectionSettings settings; settings.syncthingUrl = config.syncthingUrl(); diff --git a/fileitemactionplugin/syncthingfileitemactionstaticdata.h b/fileitemactionplugin/syncthingfileitemactionstaticdata.h index 2b5d2dc..2459840 100644 --- a/fileitemactionplugin/syncthingfileitemactionstaticdata.h +++ b/fileitemactionplugin/syncthingfileitemactionstaticdata.h @@ -41,6 +41,8 @@ Q_SIGNALS: void hasErrorChanged(bool hasError); private: + void appendNoteToError(QString &errorMessage, const QString &newSyncthingConfigFilePath) const; + Data::SyncthingConnection m_connection; QString m_configFilePath; QString m_currentError; diff --git a/fileitemactionplugin/testfiles/config-no-api-key.xml b/fileitemactionplugin/testfiles/config-no-api-key.xml new file mode 100644 index 0000000..9ec9232 --- /dev/null +++ b/fileitemactionplugin/testfiles/config-no-api-key.xml @@ -0,0 +1,9 @@ + + +
localhost:4001
+ nobody + $2a$12$35MnbsQgQNn1hzPYK/lWXOaP.U5D2TO0nuuQy2M4gsqJB4ff4q2RK + + default +
+
diff --git a/fileitemactionplugin/testfiles/config-no-gui-address.xml b/fileitemactionplugin/testfiles/config-no-gui-address.xml new file mode 100644 index 0000000..3d80e99 --- /dev/null +++ b/fileitemactionplugin/testfiles/config-no-gui-address.xml @@ -0,0 +1,9 @@ + + +
+ nobody + $2a$12$35MnbsQgQNn1hzPYK/lWXOaP.U5D2TO0nuuQy2M4gsqJB4ff4q2RK + not-secret + default +
+