Skip to content

Commit

Permalink
Rename EditLocallyHandler to EditLocallyJob
Browse files Browse the repository at this point in the history
Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
  • Loading branch information
claucambra committed Oct 29, 2022
1 parent 542a568 commit 76c4e06
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 59 deletions.
4 changes: 2 additions & 2 deletions src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ set(client_SRCS
conflictsolver.cpp
connectionvalidator.h
connectionvalidator.cpp
editlocallyhandler.h
editlocallyhandler.cpp
editlocallyjob.h
editlocallyjob.cpp
editlocallymanager.h
editlocallymanager.cpp
folder.h
Expand Down
58 changes: 29 additions & 29 deletions src/gui/editlocallyhandler.cpp → src/gui/editlocallyjob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* for more details.
*/

#include "editlocallyhandler.h"
#include "editlocallyjob.h"

#include <QMessageBox>
#include <QDesktopServices>
Expand All @@ -26,9 +26,9 @@

namespace OCC {

Q_LOGGING_CATEGORY(lcEditLocallyHandler, "nextcloud.gui.editlocallyhandler", QtInfoMsg)
Q_LOGGING_CATEGORY(lcEditLocallyJob, "nextcloud.gui.editlocallyjob", QtInfoMsg)

EditLocallyHandler::EditLocallyHandler(const QString &userId,
EditLocallyJob::EditLocallyJob(const QString &userId,
const QString &relPath,
const QString &token,
QObject *parent)
Expand All @@ -39,10 +39,10 @@ EditLocallyHandler::EditLocallyHandler(const QString &userId,
{
}

void EditLocallyHandler::startSetup()
void EditLocallyJob::startSetup()
{
if (_token.isEmpty() || _relPath.isEmpty() || _userId.isEmpty()) {
qCWarning(lcEditLocallyHandler) << "Could not start setup."
qCWarning(lcEditLocallyJob) << "Could not start setup."
<< "token:" << _token
<< "relPath:" << _relPath
<< "userId" << _userId;
Expand All @@ -56,22 +56,22 @@ void EditLocallyHandler::startSetup()
// We check the input data locally first, without modifying any state or
// showing any potentially misleading data to the user
if (!isTokenValid(_token)) {
qCWarning(lcEditLocallyHandler) << "Edit locally request is missing a valid token, will not open file. "
qCWarning(lcEditLocallyJob) << "Edit locally request is missing a valid token, will not open file. "
<< "Token received was:" << _token;
showError(tr("Invalid token received."), tr("Please try again."));
return;
}

if (!isRelPathValid(_relPath)) {
qCWarning(lcEditLocallyHandler) << "Provided relPath was:" << _relPath << "which is not canonical.";
qCWarning(lcEditLocallyJob) << "Provided relPath was:" << _relPath << "which is not canonical.";
showError(tr("Invalid file path was provided."), tr("Please try again."));
return;
}

_accountState = AccountManager::instance()->accountFromUserId(_userId);

if (!_accountState) {
qCWarning(lcEditLocallyHandler) << "Could not find an account " << _userId << " to edit file " << _relPath << " locally.";
qCWarning(lcEditLocallyJob) << "Could not find an account " << _userId << " to edit file " << _relPath << " locally.";
showError(tr("Could not find an account for local editing."), tr("Please try again."));
return;
}
Expand All @@ -81,10 +81,10 @@ void EditLocallyHandler::startSetup()
startTokenRemoteCheck();
}

void EditLocallyHandler::startTokenRemoteCheck()
void EditLocallyJob::startTokenRemoteCheck()
{
if (!_accountState || _relPath.isEmpty() || _token.isEmpty()) {
qCWarning(lcEditLocallyHandler) << "Could not start token check."
qCWarning(lcEditLocallyJob) << "Could not start token check."
<< "accountState:" << _accountState
<< "relPath:" << _relPath
<< "token:" << _token;
Expand All @@ -101,14 +101,14 @@ void EditLocallyHandler::startTokenRemoteCheck()
params.addQueryItem(QStringLiteral("path"), prefixSlashToPath(encodedRelPath));
_checkTokenJob->addQueryParams(params);
_checkTokenJob->setVerb(SimpleApiJob::Verb::Post);
connect(_checkTokenJob.get(), &SimpleApiJob::resultReceived, this, &EditLocallyHandler::remoteTokenCheckResultReceived);
connect(_checkTokenJob.get(), &SimpleApiJob::resultReceived, this, &EditLocallyJob::remoteTokenCheckResultReceived);

_checkTokenJob->start();
}

void EditLocallyHandler::remoteTokenCheckResultReceived(const int statusCode)
void EditLocallyJob::remoteTokenCheckResultReceived(const int statusCode)
{
qCInfo(lcEditLocallyHandler) << "token check result" << statusCode;
qCInfo(lcEditLocallyJob) << "token check result" << statusCode;

constexpr auto HTTP_OK_CODE = 200;
_tokenVerified = statusCode == HTTP_OK_CODE;
Expand All @@ -121,10 +121,10 @@ void EditLocallyHandler::remoteTokenCheckResultReceived(const int statusCode)
proceedWithSetup();
}

void EditLocallyHandler::proceedWithSetup()
void EditLocallyJob::proceedWithSetup()
{
if (!_tokenVerified) {
qCWarning(lcEditLocallyHandler) << "Could not proceed with setup as token is not verified.";
qCWarning(lcEditLocallyJob) << "Could not proceed with setup as token is not verified.";
return;
}

Expand Down Expand Up @@ -159,12 +159,12 @@ void EditLocallyHandler::proceedWithSetup()
Q_EMIT setupFinished();
}

QString EditLocallyHandler::prefixSlashToPath(const QString &path)
QString EditLocallyJob::prefixSlashToPath(const QString &path)
{
return path.startsWith('/') ? path : QChar::fromLatin1('/') + path;
}

bool EditLocallyHandler::isTokenValid(const QString &token)
bool EditLocallyJob::isTokenValid(const QString &token)
{
if (token.isEmpty()) {
return false;
Expand All @@ -178,7 +178,7 @@ bool EditLocallyHandler::isTokenValid(const QString &token)
return regexMatch.hasMatch();
}

bool EditLocallyHandler::isRelPathValid(const QString &relPath)
bool EditLocallyJob::isRelPathValid(const QString &relPath)
{
if (relPath.isEmpty()) {
return false;
Expand All @@ -201,7 +201,7 @@ bool EditLocallyHandler::isRelPathValid(const QString &relPath)
return true;
}

bool EditLocallyHandler::isRelPathExcluded(const QString &relPath)
bool EditLocallyJob::isRelPathExcluded(const QString &relPath)
{
if (relPath.isEmpty()) {
return false;
Expand All @@ -221,7 +221,7 @@ bool EditLocallyHandler::isRelPathExcluded(const QString &relPath)
return false;
}

void EditLocallyHandler::showError(const QString &message, const QString &informativeText)
void EditLocallyJob::showError(const QString &message, const QString &informativeText)
{
Systray::instance()->destroyEditFileLocallyLoadingDialog();
showErrorNotification(message, informativeText);
Expand All @@ -230,7 +230,7 @@ void EditLocallyHandler::showError(const QString &message, const QString &inform
Q_EMIT error(message, informativeText);
}

void EditLocallyHandler::showErrorNotification(const QString &message, const QString &informativeText) const
void EditLocallyJob::showErrorNotification(const QString &message, const QString &informativeText) const
{
if (!_accountState || !_accountState->account()) {
return;
Expand All @@ -246,7 +246,7 @@ void EditLocallyHandler::showErrorNotification(const QString &message, const QSt
}
}

void EditLocallyHandler::showErrorMessageBox(const QString &message, const QString &informativeText) const
void EditLocallyJob::showErrorMessageBox(const QString &message, const QString &informativeText) const
{
const auto messageBox = new QMessageBox;
messageBox->setAttribute(Qt::WA_DeleteOnClose);
Expand All @@ -259,10 +259,10 @@ void EditLocallyHandler::showErrorMessageBox(const QString &message, const QStri
messageBox->raise();
}

void EditLocallyHandler::startEditLocally()
void EditLocallyJob::startEditLocally()
{
if (_fileName.isEmpty() || _localFilePath.isEmpty() || !_folderForFile) {
qCWarning(lcEditLocallyHandler) << "Could not start to edit locally."
qCWarning(lcEditLocallyJob) << "Could not start to edit locally."
<< "fileName:" << _fileName
<< "localFilePath:" << _localFilePath
<< "folderForFile:" << _folderForFile;
Expand All @@ -273,20 +273,20 @@ void EditLocallyHandler::startEditLocally()

_folderForFile->startSync();
const auto syncFinishedConnection = connect(_folderForFile, &Folder::syncFinished,
this, &EditLocallyHandler::folderSyncFinished);
this, &EditLocallyJob::folderSyncFinished);

EditLocallyManager::instance()->folderSyncFinishedConnections.insert(_localFilePath,
syncFinishedConnection);
}

void EditLocallyHandler::folderSyncFinished(const OCC::SyncResult &result)
void EditLocallyJob::folderSyncFinished(const OCC::SyncResult &result)
{
Q_UNUSED(result)
disconnectSyncFinished();
openFile();
}

void EditLocallyHandler::disconnectSyncFinished() const
void EditLocallyJob::disconnectSyncFinished() const
{
if(_localFilePath.isEmpty()) {
return;
Expand All @@ -300,10 +300,10 @@ void EditLocallyHandler::disconnectSyncFinished() const
}
}

void EditLocallyHandler::openFile()
void EditLocallyJob::openFile()
{
if(_localFilePath.isEmpty()) {
qCWarning(lcEditLocallyHandler) << "Could not edit locally. Invalid local file path.";
qCWarning(lcEditLocallyJob) << "Could not edit locally. Invalid local file path.";
return;
}

Expand Down
14 changes: 7 additions & 7 deletions src/gui/editlocallyhandler.h → src/gui/editlocallyjob.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@

namespace OCC {

class EditLocallyHandler;
using EditLocallyHandlerPtr = QSharedPointer<EditLocallyHandler>;
class EditLocallyJob;
using EditLocallyJobPtr = QSharedPointer<EditLocallyJob>;

class Folder;
class SyncResult;

class EditLocallyHandler : public QObject
class EditLocallyJob : public QObject
{
Q_OBJECT

public:
explicit EditLocallyHandler(const QString &userId,
const QString &relPath,
const QString &token,
QObject *parent = nullptr);
explicit EditLocallyJob(const QString &userId,
const QString &relPath,
const QString &token,
QObject *parent = nullptr);

[[nodiscard]] static bool isTokenValid(const QString &token);
[[nodiscard]] static bool isRelPathValid(const QString &relPath);
Expand Down
30 changes: 14 additions & 16 deletions src/gui/editlocallymanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
#include <QUrl>
#include <QLoggingCategory>

#include "editlocallyhandler.h"

namespace OCC {

Q_LOGGING_CATEGORY(lcEditLocallyManager, "nextcloud.gui.editlocallymanager", QtInfoMsg)
Expand All @@ -41,7 +39,7 @@ EditLocallyManager *EditLocallyManager::instance()
void EditLocallyManager::editLocally(const QUrl &url)
{
const auto inputs = parseEditLocallyUrl(url);
createHandler(inputs.userId, inputs.relPath, inputs.token);
createJob(inputs.userId, inputs.relPath, inputs.token);
}

EditLocallyManager::EditLocallyInputData EditLocallyManager::parseEditLocallyUrl(const QUrl &url)
Expand Down Expand Up @@ -69,25 +67,25 @@ EditLocallyManager::EditLocallyInputData EditLocallyManager::parseEditLocallyUrl
return {userId, fileRemotePath, token};
}

void EditLocallyManager::createHandler(const QString &userId,
void EditLocallyManager::createJob(const QString &userId,
const QString &relPath,
const QString &token)
{
const EditLocallyHandlerPtr handler(new EditLocallyHandler(userId, relPath, token));
// We need to make sure the handler sticks around until it is finished
_handlers.insert(token, handler);
const EditLocallyJobPtr job(new EditLocallyJob(userId, relPath, token));
// We need to make sure the job sticks around until it is finished
_jobs.insert(token, job);

const auto removeHandler = [this, token] { _handlers.remove(token); };
const auto setupHandler = [handler] { handler->startEditLocally(); };
const auto removeJob = [this, token] { _jobs.remove(token); };
const auto setupJob = [job] { job->startEditLocally(); };

connect(handler.data(), &EditLocallyHandler::error,
this, removeHandler);
connect(handler.data(), &EditLocallyHandler::fileOpened,
this, removeHandler);
connect(handler.data(), &EditLocallyHandler::setupFinished,
handler.data(), setupHandler);
connect(job.data(), &EditLocallyJob::error,
this, removeJob);
connect(job.data(), &EditLocallyJob::fileOpened,
this, removeJob);
connect(job.data(), &EditLocallyJob::setupFinished,
job.data(), setupJob);

handler->startSetup();
job->startSetup();
}

}
10 changes: 5 additions & 5 deletions src/gui/editlocallymanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <QObject>
#include <QHash>

#include "editlocallyhandler.h"
#include "editlocallyjob.h"

namespace OCC {

Expand All @@ -34,9 +34,9 @@ public slots:
void editLocally(const QUrl &url);

private slots:
void createHandler(const QString &userId,
const QString &relPath,
const QString &token);
void createJob(const QString &userId,
const QString &relPath,
const QString &token);

private:
explicit EditLocallyManager(QObject *parent = nullptr);
Expand All @@ -50,7 +50,7 @@ private slots:

[[nodiscard]] static EditLocallyInputData parseEditLocallyUrl(const QUrl &url);

QHash<QString, EditLocallyHandlerPtr> _handlers;
QHash<QString, EditLocallyJobPtr> _jobs;
};

}

0 comments on commit 76c4e06

Please sign in to comment.