Directly use accountstate in editlocallyjob
authorClaudio Cambra <claudio.cambra@nextcloud.com>
Mon, 27 May 2024 10:31:11 +0000 (18:31 +0800)
committerClaudio Cambra <claudio.cambra@nextcloud.com>
Mon, 22 Jul 2024 11:51:37 +0000 (19:51 +0800)
Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
src/gui/editlocallyjob.cpp
src/gui/editlocallyjob.h
src/gui/editlocallymanager.cpp

index 8b2e45085a68542e74dbc022ee9ef8fb2d2746d3..3e50e2f1afc8ba1ee3ab0c2f833c0c35c378361c 100644 (file)
@@ -28,12 +28,12 @@ namespace OCC {
 
 Q_LOGGING_CATEGORY(lcEditLocallyJob, "nextcloud.gui.editlocallyjob", QtInfoMsg)
 
-EditLocallyJob::EditLocallyJob(const QString &userId,
-                                       const QString &relPath,
-                                       const QString &token,
-                                       QObject *parent)
+EditLocallyJob::EditLocallyJob(const AccountStatePtr &accountState,
+                               const QString &relPath,
+                               const QString &token,
+                               QObject *parent)
     : QObject{parent}
-    , _userId(userId)
+    , _accountState(accountState)
     , _relPath(relPath)
     , _token(token)
 {
@@ -42,11 +42,11 @@ EditLocallyJob::EditLocallyJob(const QString &userId,
 
 void EditLocallyJob::startSetup()
 {
-    if (_token.isEmpty() || _relPath.isEmpty() || _userId.isEmpty()) {
+    if (_token.isEmpty() || _relPath.isEmpty() || !_accountState) {
         qCWarning(lcEditLocallyJob) << "Could not start setup."
                                     << "token:" << _token
                                     << "relPath:" << _relPath
-                                    << "userId" << _userId;
+                                    << "accountState:" << _accountState;
         showError(tr("Could not start editing locally."), tr("An error occurred during setup."));
         return;
     }
@@ -55,8 +55,6 @@ void EditLocallyJob::startSetup()
     // verified the token
     Systray::instance()->createEditFileLocallyLoadingDialog({});
 
-    _accountState = AccountManager::instance()->accountFromUserId(_userId);
-
     // We now ask the server to verify the token, before we again modify any
     // state or look at local files
     startCheck();
@@ -84,7 +82,7 @@ void EditLocallyJob::proceedWithSetup()
     }
 
     _fileName = relPathSplit.last();
-    _folderForFile = findFolderForFile(_relPath, _userId);
+    _folderForFile = findFolderForFile(_relPath, _accountState->account()->userIdAtHostWithPort());
 
     if (!_folderForFile) {
         showError(tr("Could not find a file for local editing. Make sure it is not excluded via selective sync."), _relPath);
@@ -104,7 +102,7 @@ void EditLocallyJob::proceedWithSetup()
 
 void EditLocallyJob::findAfolderAndConstructPaths()
 {
-    _folderForFile = findFolderForFile(_relPath, _userId);
+    _folderForFile = findFolderForFile(_relPath, _accountState->account()->userIdAtHostWithPort());
 
     if (!_folderForFile) {
         showError(tr("Could not find a file for local editing. Make sure it is not excluded via selective sync."), _relPath);
@@ -287,19 +285,22 @@ OCC::Folder *EditLocallyJob::findFolderForFile(const QString &relPath, const QSt
     }
 
     const auto folderMap = FolderMan::instance()->map();
-
     const auto relPathSplit = relPath.split(QLatin1Char('/'));
 
     // a file is on the first level of remote root, so, we just need a proper folder that points to a remote root
     if (relPathSplit.size() == 1) {
-        const auto foundIt = std::find_if(std::begin(folderMap), std::end(folderMap), [&userId](const OCC::Folder *folder) {
-            return folder->remotePath() == QStringLiteral("/") && folder->accountState()->account()->userIdAtHostWithPort() == userId;
+        const auto foundIt = std::find_if(std::begin(folderMap), 
+                                          std::end(folderMap), 
+                                          [&userId](const OCC::Folder *folder) {
+            const auto folderUserId = folder->accountState()->account()->userIdAtHostWithPort();
+            return folder->remotePath() == QStringLiteral("/") && folderUserId == userId;
         });
 
         return foundIt != std::end(folderMap) ? foundIt.value() : nullptr;
     }
 
-    const auto relPathWithSlash = relPath.startsWith(QStringLiteral("/")) ? relPath : QStringLiteral("/") + relPath;
+    const auto relPathWithSlash = 
+        relPath.startsWith(QStringLiteral("/")) ? relPath : QStringLiteral("/") + relPath;
 
     for (const auto &folder : folderMap) {
         // make sure we properly handle folders with non-root(nested) remote paths
index 32f096ab8f2508acf9101a8e8f040f29fe129323..3ee4a37bad2bdff4531fd4734737b76210754100 100644 (file)
@@ -32,7 +32,7 @@ class EditLocallyJob : public QObject
     Q_OBJECT
 
 public:
-    explicit EditLocallyJob(const QString &userId,
+    explicit EditLocallyJob(const AccountStatePtr &accountState,
                             const QString &relPath,
                             const QString &token,
                             QObject *parent = nullptr);
index 8784c159b963b642b4b6464eccd46ee5532493bf..56bbca5a127af9478e0908b8f140112333daffbc 100644 (file)
@@ -18,6 +18,8 @@
 #include <QUrlQuery>
 #include <QLoggingCategory>
 
+#include "accountmanager.h"
+
 namespace OCC {
 
 Q_LOGGING_CATEGORY(lcEditLocallyManager, "nextcloud.gui.editlocallymanager", QtInfoMsg)
@@ -40,7 +42,8 @@ EditLocallyManager *EditLocallyManager::instance()
 void EditLocallyManager::editLocally(const QUrl &url)
 {
     const auto inputs = parseEditLocallyUrl(url);
-    createJob(inputs.userId, inputs.relPath, inputs.token);
+    const auto accountState = AccountManager::instance()->accountFromUserId(inputs.userId); 
+    createJob(accountState, inputs.relPath, inputs.token);
 }
 
 EditLocallyManager::EditLocallyInputData EditLocallyManager::parseEditLocallyUrl(const QUrl &url)
@@ -68,14 +71,14 @@ EditLocallyManager::EditLocallyInputData EditLocallyManager::parseEditLocallyUrl
     return {userId, fileRemotePath, token};
 }
 
-void EditLocallyManager::createJob(const QString &userId,
                                        const QString &relPath,
                                        const QString &token)
+void EditLocallyManager::createJob(const AccountStatePtr &accountState,
 {
     if (_jobs.contains(token)) {
         return;
     }
-    const EditLocallyJobPtr job(new EditLocallyJob(userId, relPath, token));
+    const EditLocallyJobPtr job(new EditLocallyJob(accountState, relPath, token));
     // We need to make sure the job sticks around until it is finished
     _jobs.insert(token, job);