diff --git a/syncthingconnector/syncthingconnection.h b/syncthingconnector/syncthingconnection.h index e0ff031..8a1fed5 100644 --- a/syncthingconnector/syncthingconnection.h +++ b/syncthingconnector/syncthingconnection.h @@ -262,6 +262,7 @@ public: QMetaObject::Connection browse(const QString &dirId, const QString &prefix, int level, std::function> &&, QString &&)> &&callback); QMetaObject::Connection ignores(const QString &dirId, std::function &&callback); + QMetaObject::Connection setIgnores(const QString &dirId, const SyncthingIgnores &ignores, std::function &&callback); Q_SIGNALS: void newConfig(const QJsonObject &rawConfig); @@ -378,6 +379,7 @@ private: // handler to evaluate results from request...() methods void readBrowse(const QString &dirId, int levels, std::function> &&, QString &&)> &&callback); void readIgnores(const QString &dirId, std::function &&callback); + void readSetIgnores(const QString &dirId, std::function &&callback); // internal helper methods struct Reply { diff --git a/syncthingconnector/syncthingconnection_requests.cpp b/syncthingconnector/syncthingconnection_requests.cpp index 5578740..3589594 100644 --- a/syncthingconnector/syncthingconnection_requests.cpp +++ b/syncthingconnector/syncthingconnection_requests.cpp @@ -1623,6 +1623,32 @@ QMetaObject::Connection SyncthingConnection::ignores(const QString &dirId, std:: [this, id = dirId, cb = std::move(callback)]() mutable { readIgnores(id, std::move(cb)); }, Qt::QueuedConnection); } +/*! + * \brief Sets the contents of ".stignore" of the directory with the specified \a dirId. + * \sa https://docs.syncthing.net/rest/db-ignores-post.html + * \remarks + * In contrast to most other functions, this one uses a \a callback to return results (instead of a signal). This makes it easier + * to consume results of a specific request. Errors are still reported via the error() signal so there's no extra error handling + * required. Note that in case of an error \a callback is invoked with a non-empty string containing the error message. + */ +QMetaObject::Connection SyncthingConnection::setIgnores( + const QString &dirId, const SyncthingIgnores &ignores, std::function &&callback) +{ + auto query = QUrlQuery(); + query.addQueryItem(QStringLiteral("folder"), formatQueryItem(dirId)); + auto ignoreArray = QJsonArray(); + for (const auto &ignore : ignores.ignore) { + ignoreArray.append(ignore); + } + auto jsonObj = QJsonObject(); + jsonObj.insert(QLatin1String("ignore"), ignoreArray); + auto jsonDoc = QJsonDocument(); + jsonDoc.setObject(jsonObj); + return QObject::connect( + postData(QStringLiteral("db/ignores"), query, jsonDoc.toJson(QJsonDocument::Compact)), &QNetworkReply::finished, this, + [this, id = dirId, cb = std::move(callback)]() mutable { readSetIgnores(id, std::move(cb)); }, Qt::QueuedConnection); +} + /// \cond static void readSyncthingItems(const QJsonArray &array, std::vector> &into, int level, int levels) { @@ -1748,6 +1774,28 @@ void SyncthingConnection::readIgnores(const QString &dirId, std::function &&callback) +{ + auto const [reply, response] = prepareReply(); + if (!reply) { + return; + } + switch (reply->error()) { + case QNetworkReply::NoError: { + if (callback) { + callback(QString()); + } + break; + } + default: + auto errorMessage = tr("Unable to change ignore patterns of \"%1\": ").arg(dirId); + emitError(errorMessage, SyncthingErrorCategory::SpecificRequest, reply); + if (callback) { + callback(std::move(errorMessage)); + } + } +} + // post config /*!