Improve server error handling in the ShareModel
authorClaudio Cambra <claudio.cambra@nextcloud.com>
Thu, 13 Oct 2022 21:16:10 +0000 (23:16 +0200)
committerClaudio Cambra <claudio.cambra@nextcloud.com>
Mon, 31 Oct 2022 17:06:09 +0000 (18:06 +0100)
Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
src/gui/filedetails/ShareView.qml
src/gui/filedetails/sharemodel.cpp
src/gui/filedetails/sharemodel.h

index 43ada6d7233d3a64d7dfa9de229e53981bd2ab98..e28019c072b375c48cb3b488b762dc1078094c78 100644 (file)
@@ -56,6 +56,7 @@ ColumnLayout {
             }
 
             errorBox.visible = true;
+            root.waitingForSharesToChange = false;
         }
 
         onPasswordSetError: if(root.stopWaitingForSharesToChangeOnPasswordError) {
index c1e38e45bf45f3b0a8ad4479c105997e38edd1e0..cb6d97b3ace0ec9f7bf31aeb2b1444eef809a8c1 100644 (file)
@@ -245,13 +245,13 @@ void ShareModel::updateData()
 
     _numericFileId = fileRecord.numericFileId();
 
+    // Will get added when shares are fetched if no link shares are fetched
     _placeholderLinkShare.reset(new Share(_accountState->account(),
                                           placeholderLinkShareId,
                                           _accountState->account()->id(),
                                           _accountState->account()->davDisplayName(),
                                           _sharePath,
                                           Share::TypePlaceholderLink));
-    slotAddShare(_placeholderLinkShare);
 
     auto job = new PropfindJob(_accountState->account(), _sharePath);
     job->setProperties(
@@ -261,10 +261,11 @@ void ShareModel::updateData()
         << "https://owncloud.org/ns:privatelink");
     job->setTimeout(10 * 1000);
     connect(job, &PropfindJob::result, this, &ShareModel::slotPropfindReceived);
-    connect(job, &PropfindJob::finishedWithError, this, [&]{
+    connect(job, &PropfindJob::finishedWithError, this, [&](const QNetworkReply *reply) {
         qCWarning(lcShareModel) << "Propfind for" << _sharePath << "failed";
         _fetchOngoing = false;
         Q_EMIT fetchOngoingChanged();
+        Q_EMIT serverError(reply->error(), reply->errorString());
     });
 
     _fetchOngoing = true;
@@ -295,11 +296,44 @@ void ShareModel::initShareManager()
         connect(_manager.data(), &ShareManager::shareCreated, this, [&]{ _manager->fetchShares(_sharePath); });
         connect(_manager.data(), &ShareManager::linkShareCreated, this, &ShareModel::slotAddShare);
         connect(_manager.data(), &ShareManager::linkShareRequiresPassword, this, &ShareModel::requestPasswordForLinkShare);
+        connect(_manager.data(), &ShareManager::serverError, this, [this](const int code, const QString &message){
+            _hasInitialShareFetchCompleted = true;
+            Q_EMIT hasInitialShareFetchCompletedChanged();
+            serverError(code, message);
+        });
 
         _manager->fetchShares(_sharePath);
     }
 }
 
+void ShareModel::handlePlaceholderLinkShare()
+{
+    // We want to add the placeholder if there are no link shares and
+    // if we are not already showing the placeholder link share
+    auto linkSharePresent = false;
+    auto placeholderLinkSharePresent = false;
+
+    for (const auto &share : _shares) {
+        const auto shareType = share->getShareType();
+
+        if (!linkSharePresent && shareType == Share::TypeLink) {
+            linkSharePresent = true;
+        } else if (!placeholderLinkSharePresent && shareType == Share::TypePlaceholderLink) {
+            placeholderLinkSharePresent = true;
+        }
+
+        if(linkSharePresent && placeholderLinkSharePresent) {
+            break;
+        }
+    }
+
+    if (linkSharePresent && placeholderLinkSharePresent) {
+        slotRemoveShareWithId(placeholderLinkShareId);
+    } else if (!linkSharePresent && !placeholderLinkSharePresent) {
+        slotAddShare(_placeholderLinkShare);
+    }
+}
+
 void ShareModel::slotPropfindReceived(const QVariantMap &result)
 {
     _fetchOngoing = false;
@@ -341,6 +375,8 @@ void ShareModel::slotSharesFetched(const QList<SharePtr> &shares)
 
         slotAddShare(share);
     }
+
+    handlePlaceholderLinkShare();
 }
 
 void ShareModel::slotAddShare(const SharePtr &share)
@@ -350,12 +386,6 @@ void ShareModel::slotAddShare(const SharePtr &share)
     }
 
     const auto shareId = share->getId();
-
-    // Remove placeholder link share if this is a link share
-    if(share->getShareType() == Share::TypeLink) {
-        slotRemoveShareWithId(placeholderLinkShareId);
-    }
-
     QModelIndex shareModelIndex;
 
     if (_shareIdIndexHash.contains(shareId)) {
@@ -405,6 +435,7 @@ void ShareModel::slotAddShare(const SharePtr &share)
         connect(_manager.data(), &ShareManager::serverError, this, &ShareModel::slotServerError);
     }
 
+    handlePlaceholderLinkShare();
     Q_EMIT sharesChanged();
 }
 
@@ -427,18 +458,7 @@ void ShareModel::slotRemoveShareWithId(const QString &shareId)
     _shares.removeAt(shareIndex.row());
     endRemoveRows();
 
-    // If no link shares then re-add placeholder link share
-    if (shareIndex.data(ShareModel::ShareTypeRole).toInt() == Share::TypeLink) {
-
-        // Early return if we find another link share
-        for(const auto &share : _shares) {
-            if(share->getShareType() == Share::TypeLink) {
-                return;
-            }
-        }
-
-        slotAddShare(_placeholderLinkShare);
-    }
+    handlePlaceholderLinkShare();
 
     Q_EMIT sharesChanged();
 }
index 5262bdf4bcbd52df65623e8f16a31f8376ac0ac9..ca431dc32b9b6f88eb22b87d75d7f30a8ab60593 100644 (file)
@@ -152,6 +152,7 @@ private slots:
     void resetData();
     void updateData();
     void initShareManager();
+    void handlePlaceholderLinkShare();
 
     void slotPropfindReceived(const QVariantMap &result);
     void slotServerError(const int code, const QString &message);