From 8d7cfbe0ad7cd51f53ac00c3ef85b232ecf012b3 Mon Sep 17 00:00:00 2001 From: Martchus Date: Fri, 3 Feb 2023 12:40:12 +0100 Subject: [PATCH] Add functions to request overriding/reverting folders --- connector/syncthingconnection.h | 6 ++ connector/syncthingconnection_requests.cpp | 66 ++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/connector/syncthingconnection.h b/connector/syncthingconnection.h index cb97589..62461d7 100644 --- a/connector/syncthingconnection.h +++ b/connector/syncthingconnection.h @@ -240,6 +240,8 @@ public Q_SLOTS: void requestDiskEvents(int limit = 25); void requestQrCode(const QString &text); void requestLog(); + void requestOverride(const QString &dirId); + void requestRevert(const QString &dirId); void postConfigFromJsonObject(const QJsonObject &rawConfig); void postConfigFromByteArray(const QByteArray &rawConfig); @@ -276,6 +278,8 @@ Q_SIGNALS: void shutdownTriggered(); void logAvailable(const std::vector &logEntries); void qrCodeAvailable(const QString &text, const QByteArray &qrCodeData); + void overrideTriggered(const QString &dirId); + void revertTriggered(const QString &dirId); private Q_SLOTS: // handler to evaluate results from request...() methods @@ -327,6 +331,8 @@ private Q_SLOTS: void readChangeEvent(CppUtilities::DateTime eventTime, const QString &eventType, const QJsonObject &eventData); void readLog(); void readQrCode(); + void readOverride(); + void readRevert(); // internal helper methods void continueConnecting(); diff --git a/connector/syncthingconnection_requests.cpp b/connector/syncthingconnection_requests.cpp index 8bd5e99..3213079 100644 --- a/connector/syncthingconnection_requests.cpp +++ b/connector/syncthingconnection_requests.cpp @@ -1426,6 +1426,72 @@ void SyncthingConnection::readLog() } } +/*! + * \brief Request the override of the send only folder with the specified \a dirId. + * \remarks + * - Override means to make the local version latest, overriding changes made on other devices. + * - This call does nothing if the folder is not a send only folder. + */ +void SyncthingConnection::requestOverride(const QString &dirId) +{ + auto query = QUrlQuery(); + query.addQueryItem(QStringLiteral("folder"), dirId); + auto *const reply = postData(QStringLiteral("db/override"), query); + reply->setProperty("dirId", dirId); + QObject::connect(reply, &QNetworkReply::finished, this, &SyncthingConnection::readOverride, Qt::QueuedConnection); +} + +/*! + * \brief Reads data from requestOverride(). + */ +void SyncthingConnection::readOverride() +{ + auto const [reply, response] = prepareReply(false); + if (!reply) { + return; + } + switch (reply->error()) { + case QNetworkReply::NoError: + emit overrideTriggered(reply->property("dirId").toString()); + break; + default: + emitError(tr("Unable to request directory override: "), SyncthingErrorCategory::SpecificRequest, reply); + } +} + +/*! + * \brief Request the revert of the receive only folder with the specified \a dirId. + * \remarks + * - Reverting a folder means to undo all local changes. + * - This call does nothing if the folder is not a receive only folder. + */ +void SyncthingConnection::requestRevert(const QString &dirId) +{ + auto query = QUrlQuery(); + query.addQueryItem(QStringLiteral("folder"), dirId); + auto *const reply = postData(QStringLiteral("db/revert"), query); + reply->setProperty("dirId", dirId); + QObject::connect(reply, &QNetworkReply::finished, this, &SyncthingConnection::readRevert, Qt::QueuedConnection); +} + +/*! + * \brief Reads data from requestOverride(). + */ +void SyncthingConnection::readRevert() +{ + auto const [reply, response] = prepareReply(false); + if (!reply) { + return; + } + switch (reply->error()) { + case QNetworkReply::NoError: + emit revertTriggered(reply->property("dirId").toString()); + break; + default: + emitError(tr("Unable to request directory revert: "), SyncthingErrorCategory::SpecificRequest, reply); + } +} + // post config /*!