Directly use accountstate in editlocallyjob

Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
This commit is contained in:
Claudio Cambra 2024-05-27 18:31:11 +08:00
parent a5c7bfe584
commit 7761182474
No known key found for this signature in database
GPG key ID: C839200C384636B0
3 changed files with 23 additions and 19 deletions

View 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

View 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);

View 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);