Extract propfind result data processing into separate method
authorClaudio Cambra <claudio.cambra@nextcloud.com>
Thu, 20 Apr 2023 11:54:35 +0000 (19:54 +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/libsync/networkjobs.cpp
src/libsync/networkjobs.h

index 14334ebf5e53c321f628a85234d0f3bd1f1affa0..00e01d36d8d1adb29b9aacf5758278f5ac1b8832 100644 (file)
@@ -610,7 +610,6 @@ bool PropfindJob::finished()
 
     if (http_result_code == 207) {
         // Parse DAV response
-        auto items = QVariantMap();
         auto domDocument = QDomDocument();
         auto errorMsg = QString();
         auto errorLine = -1;
@@ -619,40 +618,54 @@ bool PropfindJob::finished()
         if (!domDocument.setContent(reply(), true, &errorMsg, &errorLine, &errorColumn)) {
             qCWarning(lcPropfindJob) << "XML parser error: " << errorMsg << errorLine << errorColumn;
             emit finishedWithError(reply());
-            return true;
+
+        } else {
+            const auto parsedItems = processPropfindDomDocument(domDocument);
+            emit result(parsedItems);
         }
 
-        const auto rootElement = domDocument.documentElement();
-        const auto propNodes = rootElement.elementsByTagName(propfindPropElementTagName);
+    } else {
+        qCWarning(lcPropfindJob) << "*not* successful, http result code is" << http_result_code
+                                 << (http_result_code == 302 ? reply()->header(QNetworkRequest::LocationHeader).toString() : QLatin1String(""));
+        emit finishedWithError(reply());
+    }
 
-        for (auto i = 0; i < propNodes.count(); ++i) {
-            const auto propNode = propNodes.at(i);
-            const auto propElement = propNode.toElement();
+    return true;
+}
 
-            if (propElement.isNull() || propElement.tagName() != propfindPropElementTagName) {
-                continue;
-            }
+QVariantMap PropfindJob::processPropfindDomDocument(const QDomDocument &domDocument)
+{
+    if (!domDocument.hasChildNodes()) {
+        return {};
+    }
 
-            auto propChildNode = propElement.firstChild();
+    auto items = QVariantMap();
 
-            while (!propChildNode.isNull()) {
-                const auto propChildElement = propChildNode.toElement();
+    const auto rootElement = domDocument.documentElement();
+    const auto propNodes = rootElement.elementsByTagName(propfindPropElementTagName);
 
-                if (!propChildElement.isNull()) {
-                    items.insert(propChildElement.tagName(), propChildElement.text());
-                }
+    for (auto i = 0; i < propNodes.count(); ++i) {
+        const auto propNode = propNodes.at(i);
+        const auto propElement = propNode.toElement();
+
+        if (propElement.isNull() || propElement.tagName() != propfindPropElementTagName) {
+            continue;
+        }
+
+        auto propChildNode = propElement.firstChild();
 
-                propChildNode = propChildNode.nextSibling();
+        while (!propChildNode.isNull()) {
+            const auto propChildElement = propChildNode.toElement();
+
+            if (!propChildElement.isNull()) {
+                items.insert(propChildElement.tagName(), propChildElement.text());
             }
-    }
 
-        emit result(items);
-    } else {
-        qCWarning(lcPropfindJob) << "*not* successful, http result code is" << http_result_code
-                                 << (http_result_code == 302 ? reply()->header(QNetworkRequest::LocationHeader).toString() : QLatin1String(""));
-        emit finishedWithError(reply());
+            propChildNode = propChildNode.nextSibling();
+        }
     }
-    return true;
+
+    return items;
 }
 
 /*********************************************************************************************/
index 9f1f043b6723de599c07585e5460671fa1d44403..c274b4d3f13e43aaff9e8ebfa2dc057caf9f7213 100644 (file)
@@ -199,6 +199,8 @@ private slots:
     bool finished() override;
 
 private:
+    static QVariantMap processPropfindDomDocument(const QDomDocument &domDocument);
+
     QList<QByteArray> _properties;
 };