Add capability for FileTagModel to fetch tags
authorClaudio Cambra <claudio.cambra@nextcloud.com>
Tue, 11 Apr 2023 15:21:17 +0000 (23:21 +0800)
committerClaudio Cambra <claudio.cambra@nextcloud.com>
Tue, 16 May 2023 10:23:33 +0000 (18:23 +0800)
Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
src/gui/filetagmodel.cpp
src/gui/filetagmodel.h

index eaa11f197acae95ae14cee9b31696f97078294b6..c94f1cfef8d6d4798bd06ef54b3111bb23358eb7 100644 (file)
@@ -16,6 +16,8 @@
 
 #include "libsync/networkjobs.h"
 
+Q_LOGGING_CATEGORY(lcFileTagModel, "nextcloud.gui.filetagmodel", QtInfoMsg)
+
 namespace OCC {
 
 FileTagModel::FileTagModel(const QString &serverRelativePath,
@@ -25,6 +27,7 @@ FileTagModel::FileTagModel(const QString &serverRelativePath,
     , _serverRelativePath(serverRelativePath)
     , _account(account)
 {
+    fetchFileTags();
 }
 
 int FileTagModel::rowCount(const QModelIndex &parent) const
@@ -45,4 +48,44 @@ QVariant FileTagModel::data(const QModelIndex &index, int role) const
     // FIXME: Implement me!
     return QVariant();
 }
+
+void FileTagModel::fetchFileTags()
+{
+    if (!_account || _serverRelativePath.isEmpty()) {
+        qCDebug(lcFileTagModel) << "Cannot fetch filetags as account is null, or server relative path is empty";
+        return;
+    }
+
+    qCDebug(lcFileTagModel) << "Starting fetch of filetags for file at:" << _serverRelativePath;
+
+    const auto propfindJob = new PropfindJob(_account, _serverRelativePath, this);
+    propfindJob->setProperties({ QByteArrayLiteral("http://nextcloud.org/ns:tags") });
+
+    connect(propfindJob, &PropfindJob::result, this, &FileTagModel::processFileTagRequestFinished);
+    connect(propfindJob, &PropfindJob::finishedWithError, this, &FileTagModel::processFileTagRequestFinishedWithError);
+
+    propfindJob->start();
+}
+
+void FileTagModel::processFileTagRequestFinished(const QVariantMap &result)
+{
+    if (result.empty()) {
+        qCDebug(lcFileTagModel) << "File tag fetch request finished successfully, but received empty results..."
+                                << _serverRelativePath;
+        return;
+    }
+
+    qCDebug(lcFileTagModel) << "File tag fetch request finished successfully, processing results..."
+                            << _serverRelativePath;
+
+    beginResetModel();
+    _tags = result.value(QStringLiteral("tags")).toStringList();
+    endResetModel();
+}
+
+void FileTagModel::processFileTagRequestFinishedWithError()
+{
+    qCWarning(lcFileTagModel) << "File tag fetch request job ended with error.";
+}
+
 }
index a18022eb923822d624b7f51f830c3b2a79960527..3808ba65d6f44d4dbdf2e7578e65ccc5711bf3e1 100644 (file)
@@ -32,9 +32,15 @@ public:
 
     [[nodiscard]] QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
 
+private slots:
+    void fetchFileTags();
+    void processFileTagRequestFinished(const QVariantMap &result);
+    void processFileTagRequestFinishedWithError();
+
 private:
     QString _serverRelativePath;
     AccountPtr _account;
+    QStringList _tags;
 };
 
 }