2022-10-26 21:13:38 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Claudio Cambra <claudio.cambra@nextcloud.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
2022-10-29 14:21:56 +03:00
|
|
|
#include "editlocallyjob.h"
|
2022-10-26 21:13:38 +03:00
|
|
|
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QDesktopServices>
|
|
|
|
#include <QtConcurrent>
|
|
|
|
|
2022-10-28 20:40:06 +03:00
|
|
|
#include "editlocallymanager.h"
|
2022-10-26 21:13:38 +03:00
|
|
|
#include "folder.h"
|
|
|
|
#include "folderman.h"
|
|
|
|
#include "syncengine.h"
|
|
|
|
#include "systray.h"
|
|
|
|
|
|
|
|
namespace OCC {
|
|
|
|
|
2022-10-29 14:21:56 +03:00
|
|
|
Q_LOGGING_CATEGORY(lcEditLocallyJob, "nextcloud.gui.editlocallyjob", QtInfoMsg)
|
2022-10-26 21:13:38 +03:00
|
|
|
|
2022-10-29 14:21:56 +03:00
|
|
|
EditLocallyJob::EditLocallyJob(const QString &userId,
|
2022-10-26 21:13:38 +03:00
|
|
|
const QString &relPath,
|
|
|
|
const QString &token,
|
|
|
|
QObject *parent)
|
|
|
|
: QObject{parent}
|
2022-10-28 17:42:57 +03:00
|
|
|
, _userId(userId)
|
2022-10-26 21:13:38 +03:00
|
|
|
, _relPath(relPath)
|
|
|
|
, _token(token)
|
|
|
|
{
|
2022-12-08 12:58:58 +03:00
|
|
|
connect(this, &EditLocallyJob::callShowError, this, &EditLocallyJob::showError, Qt::QueuedConnection);
|
2022-10-28 17:42:57 +03:00
|
|
|
}
|
2022-10-26 21:13:38 +03:00
|
|
|
|
2022-10-29 14:21:56 +03:00
|
|
|
void EditLocallyJob::startSetup()
|
2022-10-28 17:42:57 +03:00
|
|
|
{
|
|
|
|
if (_token.isEmpty() || _relPath.isEmpty() || _userId.isEmpty()) {
|
2022-10-29 14:21:56 +03:00
|
|
|
qCWarning(lcEditLocallyJob) << "Could not start setup."
|
2022-10-28 17:42:57 +03:00
|
|
|
<< "token:" << _token
|
|
|
|
<< "relPath:" << _relPath
|
|
|
|
<< "userId" << _userId;
|
2022-10-26 21:13:38 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-28 21:22:27 +03:00
|
|
|
// Show the loading dialog but don't show the filename until we have
|
|
|
|
// verified the token
|
|
|
|
Systray::instance()->createEditFileLocallyLoadingDialog({});
|
|
|
|
|
2022-10-28 17:42:57 +03:00
|
|
|
// We check the input data locally first, without modifying any state or
|
|
|
|
// showing any potentially misleading data to the user
|
|
|
|
if (!isTokenValid(_token)) {
|
2022-10-29 14:21:56 +03:00
|
|
|
qCWarning(lcEditLocallyJob) << "Edit locally request is missing a valid token, will not open file. "
|
2022-10-28 17:42:57 +03:00
|
|
|
<< "Token received was:" << _token;
|
2022-10-26 21:13:38 +03:00
|
|
|
showError(tr("Invalid token received."), tr("Please try again."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-28 17:42:57 +03:00
|
|
|
if (!isRelPathValid(_relPath)) {
|
2022-10-29 14:21:56 +03:00
|
|
|
qCWarning(lcEditLocallyJob) << "Provided relPath was:" << _relPath << "which is not canonical.";
|
2022-10-26 21:13:38 +03:00
|
|
|
showError(tr("Invalid file path was provided."), tr("Please try again."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-28 17:42:57 +03:00
|
|
|
_accountState = AccountManager::instance()->accountFromUserId(_userId);
|
|
|
|
|
|
|
|
if (!_accountState) {
|
2022-10-29 14:21:56 +03:00
|
|
|
qCWarning(lcEditLocallyJob) << "Could not find an account " << _userId << " to edit file " << _relPath << " locally.";
|
2022-10-28 17:43:30 +03:00
|
|
|
showError(tr("Could not find an account for local editing."), tr("Please try again."));
|
2022-10-28 17:42:57 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We now ask the server to verify the token, before we again modify any
|
|
|
|
// state or look at local files
|
|
|
|
startTokenRemoteCheck();
|
|
|
|
}
|
|
|
|
|
2022-10-29 14:21:56 +03:00
|
|
|
void EditLocallyJob::startTokenRemoteCheck()
|
2022-10-28 17:42:57 +03:00
|
|
|
{
|
|
|
|
if (!_accountState || _relPath.isEmpty() || _token.isEmpty()) {
|
2022-10-29 14:21:56 +03:00
|
|
|
qCWarning(lcEditLocallyJob) << "Could not start token check."
|
2022-10-28 17:42:57 +03:00
|
|
|
<< "accountState:" << _accountState
|
|
|
|
<< "relPath:" << _relPath
|
|
|
|
<< "token:" << _token;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto encodedToken = QString::fromUtf8(QUrl::toPercentEncoding(_token)); // Sanitise the token
|
|
|
|
const auto encodedRelPath = QUrl::toPercentEncoding(_relPath); // Sanitise the relPath
|
2022-10-29 01:19:04 +03:00
|
|
|
|
|
|
|
_checkTokenJob.reset(new SimpleApiJob(_accountState->account(),
|
|
|
|
QStringLiteral("/ocs/v2.php/apps/files/api/v1/openlocaleditor/%1").arg(encodedToken)));
|
2022-10-28 17:42:57 +03:00
|
|
|
|
|
|
|
QUrlQuery params;
|
|
|
|
params.addQueryItem(QStringLiteral("path"), prefixSlashToPath(encodedRelPath));
|
2022-10-29 01:19:04 +03:00
|
|
|
_checkTokenJob->addQueryParams(params);
|
|
|
|
_checkTokenJob->setVerb(SimpleApiJob::Verb::Post);
|
2022-10-29 14:21:56 +03:00
|
|
|
connect(_checkTokenJob.get(), &SimpleApiJob::resultReceived, this, &EditLocallyJob::remoteTokenCheckResultReceived);
|
2022-10-28 17:42:57 +03:00
|
|
|
|
2022-10-29 01:19:04 +03:00
|
|
|
_checkTokenJob->start();
|
2022-10-28 17:42:57 +03:00
|
|
|
}
|
|
|
|
|
2022-10-29 14:21:56 +03:00
|
|
|
void EditLocallyJob::remoteTokenCheckResultReceived(const int statusCode)
|
2022-10-28 17:42:57 +03:00
|
|
|
{
|
2022-10-29 14:21:56 +03:00
|
|
|
qCInfo(lcEditLocallyJob) << "token check result" << statusCode;
|
2022-10-28 17:42:57 +03:00
|
|
|
|
|
|
|
constexpr auto HTTP_OK_CODE = 200;
|
|
|
|
_tokenVerified = statusCode == HTTP_OK_CODE;
|
|
|
|
|
|
|
|
if (!_tokenVerified) {
|
2022-10-28 17:43:30 +03:00
|
|
|
showError(tr("Could not validate the request to open a file from server."), tr("Please try again."));
|
2022-10-28 21:22:27 +03:00
|
|
|
return;
|
2022-10-28 17:42:57 +03:00
|
|
|
}
|
|
|
|
|
2022-11-04 19:38:59 +03:00
|
|
|
findAfolderAndConstructPaths();
|
2022-10-28 17:42:57 +03:00
|
|
|
}
|
|
|
|
|
2022-10-29 14:21:56 +03:00
|
|
|
void EditLocallyJob::proceedWithSetup()
|
2022-10-28 17:42:57 +03:00
|
|
|
{
|
|
|
|
if (!_tokenVerified) {
|
2022-10-29 14:21:56 +03:00
|
|
|
qCWarning(lcEditLocallyJob) << "Could not proceed with setup as token is not verified.";
|
2022-11-04 19:38:59 +03:00
|
|
|
showError(tr("Could not validate the request to open a file from server."), tr("Please try again."));
|
2022-10-28 17:42:57 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-04 19:38:59 +03:00
|
|
|
const auto relPathSplit = _relPath.split(QLatin1Char('/'));
|
|
|
|
if (relPathSplit.isEmpty()) {
|
|
|
|
showError(tr("Could not find a file for local editing. Make sure its path is valid and it is synced locally."), _relPath);
|
2022-10-26 21:13:38 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-04 19:38:59 +03:00
|
|
|
_fileName = relPathSplit.last();
|
|
|
|
_folderForFile = findFolderForFile(_relPath, _userId);
|
2022-10-26 21:13:38 +03:00
|
|
|
|
|
|
|
if (!_folderForFile) {
|
2022-11-04 19:38:59 +03:00
|
|
|
showError(tr("Could not find a file for local editing. Make sure it is not excluded via selective sync."), _relPath);
|
2022-10-26 21:13:38 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-04 19:38:59 +03:00
|
|
|
if (_relPathParent != QStringLiteral("/") && (!_fileParentItem || _fileParentItem->isEmpty())) {
|
2022-10-28 17:42:57 +03:00
|
|
|
showError(tr("Could not find a file for local editing. Make sure its path is valid and it is synced locally."), _relPath);
|
2022-10-26 21:13:38 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-04 19:38:59 +03:00
|
|
|
_localFilePath = _folderForFile->path() + _relativePathToRemoteRoot;
|
2022-10-26 21:13:38 +03:00
|
|
|
|
2022-10-28 21:22:27 +03:00
|
|
|
Systray::instance()->destroyEditFileLocallyLoadingDialog();
|
2022-10-28 17:42:57 +03:00
|
|
|
Q_EMIT setupFinished();
|
2022-10-26 21:13:38 +03:00
|
|
|
}
|
|
|
|
|
2022-11-04 19:38:59 +03:00
|
|
|
void EditLocallyJob::findAfolderAndConstructPaths()
|
|
|
|
{
|
|
|
|
_folderForFile = findFolderForFile(_relPath, _userId);
|
|
|
|
|
|
|
|
if (!_folderForFile) {
|
|
|
|
showError(tr("Could not find a file for local editing. Make sure it is not excluded via selective sync."), _relPath);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_relativePathToRemoteRoot = getRelativePathToRemoteRootForFile();
|
|
|
|
|
|
|
|
if (_relativePathToRemoteRoot.isEmpty()) {
|
|
|
|
qCWarning(lcEditLocallyJob) << "_relativePathToRemoteRoot is empty for" << _relPath;
|
|
|
|
showError(tr("Could not find a file for local editing. Make sure it is not excluded via selective sync."), _relPath);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_relPathParent = getRelativePathParent();
|
|
|
|
|
|
|
|
if (_relPathParent.isEmpty()) {
|
|
|
|
showError(tr("Could not find a file for local editing. Make sure it is not excluded via selective sync."), _relPath);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_relPathParent == QStringLiteral("/")) {
|
|
|
|
proceedWithSetup();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchRemoteFileParentInfo();
|
|
|
|
}
|
|
|
|
|
2022-10-29 14:21:56 +03:00
|
|
|
QString EditLocallyJob::prefixSlashToPath(const QString &path)
|
2022-10-26 21:13:38 +03:00
|
|
|
{
|
2022-10-28 21:22:27 +03:00
|
|
|
return path.startsWith('/') ? path : QChar::fromLatin1('/') + path;
|
2022-10-26 21:13:38 +03:00
|
|
|
}
|
|
|
|
|
2022-11-04 19:38:59 +03:00
|
|
|
void EditLocallyJob::fetchRemoteFileParentInfo()
|
|
|
|
{
|
|
|
|
Q_ASSERT(_relPathParent != QStringLiteral("/"));
|
|
|
|
|
|
|
|
if (_relPathParent == QStringLiteral("/")) {
|
|
|
|
qCWarning(lcEditLocallyJob) << "LsColJob must only be used for nested folders.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto job = new LsColJob(_accountState->account(), QDir::cleanPath(_folderForFile->remotePathTrailingSlash() + _relPathParent), this);
|
|
|
|
const QList<QByteArray> props{QByteArrayLiteral("resourcetype"),
|
|
|
|
QByteArrayLiteral("getlastmodified"),
|
|
|
|
QByteArrayLiteral("getetag"),
|
|
|
|
QByteArrayLiteral("http://owncloud.org/ns:size"),
|
|
|
|
QByteArrayLiteral("http://owncloud.org/ns:id"),
|
|
|
|
QByteArrayLiteral("http://owncloud.org/ns:permissions"),
|
|
|
|
QByteArrayLiteral("http://owncloud.org/ns:checksums")};
|
|
|
|
|
|
|
|
job->setProperties(props);
|
|
|
|
connect(job, &LsColJob::directoryListingIterated, this, &EditLocallyJob::slotDirectoryListingIterated);
|
|
|
|
connect(job, &LsColJob::finishedWithoutError, this, &EditLocallyJob::proceedWithSetup);
|
|
|
|
connect(job, &LsColJob::finishedWithError, this, &EditLocallyJob::slotLsColJobFinishedWithError);
|
|
|
|
job->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EditLocallyJob::checkIfFileParentSyncIsNeeded()
|
|
|
|
{
|
|
|
|
if (_relPathParent == QLatin1String("/")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Q_ASSERT(_fileParentItem && !_fileParentItem->isEmpty());
|
|
|
|
|
|
|
|
if (!_fileParentItem || _fileParentItem->isEmpty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
SyncJournalFileRecord rec;
|
|
|
|
if (!_folderForFile->journalDb()->getFileRecord(_fileParentItem->_file, &rec) || !rec.isValid()) {
|
|
|
|
// we don't have this folder locally, so let's sync it
|
|
|
|
_fileParentItem->_direction = SyncFileItem::Down;
|
|
|
|
_fileParentItem->_instruction = CSYNC_INSTRUCTION_NEW;
|
|
|
|
} else if (rec._etag != _fileParentItem->_etag && rec._modtime != _fileParentItem->_modtime) {
|
|
|
|
// we just need to update metadata as the folder is already present locally
|
|
|
|
_fileParentItem->_direction = rec._modtime < _fileParentItem->_modtime ? SyncFileItem::Down : SyncFileItem::Up;
|
|
|
|
_fileParentItem->_instruction = CSYNC_INSTRUCTION_UPDATE_METADATA;
|
|
|
|
} else {
|
|
|
|
_fileParentItem->_direction = SyncFileItem::Down;
|
|
|
|
_fileParentItem->_instruction = CSYNC_INSTRUCTION_UPDATE_METADATA;
|
|
|
|
SyncJournalFileRecord recFile;
|
|
|
|
if (_folderForFile->journalDb()->getFileRecord(_relativePathToRemoteRoot, &recFile) && recFile.isValid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditLocallyJob::startSyncBeforeOpening()
|
|
|
|
{
|
|
|
|
eraseBlacklistRecordForItem();
|
|
|
|
if (!checkIfFileParentSyncIsNeeded()) {
|
2023-01-09 19:11:33 +03:00
|
|
|
processLocalItem();
|
2022-11-04 19:38:59 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// connect to a SyncEngine::itemDiscovered so we can complete the job as soon as the file in question is discovered
|
|
|
|
QObject::connect(&_folderForFile->syncEngine(), &SyncEngine::itemDiscovered, this, &EditLocallyJob::slotItemDiscovered);
|
|
|
|
_folderForFile->syncEngine().setSingleItemDiscoveryOptions({_relPathParent == QStringLiteral("/") ? QString{} : _relPathParent, _relativePathToRemoteRoot, _fileParentItem});
|
|
|
|
FolderMan::instance()->forceSyncForFolder(_folderForFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditLocallyJob::eraseBlacklistRecordForItem()
|
|
|
|
{
|
|
|
|
if (!_folderForFile || !_fileParentItem) {
|
|
|
|
qCWarning(lcEditLocallyJob) << "_folderForFile or _fileParentItem is invalid!";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Q_ASSERT(!_folderForFile->isSyncRunning());
|
|
|
|
if (_folderForFile->isSyncRunning()) {
|
|
|
|
qCWarning(lcEditLocallyJob) << "_folderForFile is syncing";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (_folderForFile->journalDb()->errorBlacklistEntry(_fileParentItem->_file).isValid()) {
|
|
|
|
_folderForFile->journalDb()->wipeErrorBlacklistEntry(_fileParentItem->_file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString EditLocallyJob::getRelativePathToRemoteRootForFile() const
|
|
|
|
{
|
|
|
|
Q_ASSERT(_folderForFile);
|
|
|
|
if (!_folderForFile) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_folderForFile->remotePathTrailingSlash().size() == 1) {
|
|
|
|
return _relPath;
|
|
|
|
} else {
|
|
|
|
const auto remoteFolderPathWithTrailingSlash = _folderForFile->remotePathTrailingSlash();
|
|
|
|
const auto remoteFolderPathWithoutLeadingSlash =
|
|
|
|
remoteFolderPathWithTrailingSlash.startsWith(QLatin1Char('/')) ? remoteFolderPathWithTrailingSlash.mid(1) : remoteFolderPathWithTrailingSlash;
|
|
|
|
|
|
|
|
return _relPath.startsWith(remoteFolderPathWithoutLeadingSlash) ? _relPath.mid(remoteFolderPathWithoutLeadingSlash.size()) : _relPath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString EditLocallyJob::getRelativePathParent() const
|
|
|
|
{
|
|
|
|
Q_ASSERT(!_relativePathToRemoteRoot.isEmpty());
|
|
|
|
if (_relativePathToRemoteRoot.isEmpty()) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
auto relativePathToRemoteRootSplit = _relativePathToRemoteRoot.split(QLatin1Char('/'));
|
|
|
|
if (relativePathToRemoteRootSplit.size() > 1) {
|
|
|
|
relativePathToRemoteRootSplit.removeLast();
|
|
|
|
return relativePathToRemoteRootSplit.join(QLatin1Char('/'));
|
|
|
|
}
|
|
|
|
return QStringLiteral("/");
|
|
|
|
}
|
|
|
|
|
2022-10-29 14:21:56 +03:00
|
|
|
bool EditLocallyJob::isTokenValid(const QString &token)
|
2022-10-26 21:13:38 +03:00
|
|
|
{
|
|
|
|
if (token.isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Token is an alphanumeric string 128 chars long.
|
|
|
|
// Ensure that is what we received and what we are sending to the server.
|
2022-12-19 19:26:15 +03:00
|
|
|
static const QRegularExpression tokenRegex("^[a-zA-Z0-9]{128}$");
|
2022-10-26 21:13:38 +03:00
|
|
|
const auto regexMatch = tokenRegex.match(token);
|
|
|
|
|
2022-10-28 21:22:27 +03:00
|
|
|
return regexMatch.hasMatch();
|
2022-10-26 21:13:38 +03:00
|
|
|
}
|
|
|
|
|
2022-10-29 14:21:56 +03:00
|
|
|
bool EditLocallyJob::isRelPathValid(const QString &relPath)
|
2022-10-26 21:13:38 +03:00
|
|
|
{
|
|
|
|
if (relPath.isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We want to check that the path is canonical and not relative
|
|
|
|
// (i.e. that it doesn't contain ../../) but we always receive
|
|
|
|
// a relative path, so let's make it absolute by prepending a
|
|
|
|
// slash
|
|
|
|
const auto slashPrefixedPath = prefixSlashToPath(relPath);
|
|
|
|
|
|
|
|
// Let's check that the filepath is canonical, and that the request
|
|
|
|
// contains no funny behaviour regarding paths
|
|
|
|
const auto cleanedPath = QDir::cleanPath(slashPrefixedPath);
|
|
|
|
|
|
|
|
if (cleanedPath != slashPrefixedPath) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-04 19:38:59 +03:00
|
|
|
OCC::Folder *EditLocallyJob::findFolderForFile(const QString &relPath, const QString &userId)
|
2022-10-26 21:13:38 +03:00
|
|
|
{
|
|
|
|
if (relPath.isEmpty()) {
|
2022-11-04 19:38:59 +03:00
|
|
|
return nullptr;
|
2022-10-26 21:13:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto folderMap = FolderMan::instance()->map();
|
2022-11-04 19:38:59 +03:00
|
|
|
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
|
|
|
|
return foundIt != std::end(folderMap) ? foundIt.value() : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto relPathWithSlash = relPath.startsWith(QStringLiteral("/")) ? relPath : QStringLiteral("/") + relPath;
|
|
|
|
|
2022-10-26 21:13:38 +03:00
|
|
|
for (const auto &folder : folderMap) {
|
2022-11-04 19:38:59 +03:00
|
|
|
// make sure we properly handle folders with non-root(nested) remote paths
|
|
|
|
if ((folder->remotePath() != QStringLiteral("/") && !relPathWithSlash.startsWith(folder->remotePath()))
|
|
|
|
|| folder->accountState()->account()->userIdAtHostWithPort() != userId) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto result = false;
|
2022-10-26 21:13:38 +03:00
|
|
|
const auto excludedThroughSelectiveSync = folder->journalDb()->getSelectiveSyncList(SyncJournalDb::SelectiveSyncBlackList, &result);
|
2022-11-04 19:38:59 +03:00
|
|
|
auto isExcluded = false;
|
2022-10-26 21:13:38 +03:00
|
|
|
for (const auto &excludedPath : excludedThroughSelectiveSync) {
|
|
|
|
if (relPath.startsWith(excludedPath)) {
|
2022-11-04 19:38:59 +03:00
|
|
|
isExcluded = true;
|
|
|
|
break;
|
2022-10-26 21:13:38 +03:00
|
|
|
}
|
|
|
|
}
|
2022-11-04 19:38:59 +03:00
|
|
|
if (isExcluded) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return folder;
|
2022-10-26 21:13:38 +03:00
|
|
|
}
|
|
|
|
|
2022-11-04 19:38:59 +03:00
|
|
|
return nullptr;
|
2022-10-26 21:13:38 +03:00
|
|
|
}
|
|
|
|
|
2022-10-29 14:21:56 +03:00
|
|
|
void EditLocallyJob::showError(const QString &message, const QString &informativeText)
|
2022-10-26 21:13:38 +03:00
|
|
|
{
|
2022-10-28 21:22:27 +03:00
|
|
|
Systray::instance()->destroyEditFileLocallyLoadingDialog();
|
2022-10-26 21:13:38 +03:00
|
|
|
showErrorNotification(message, informativeText);
|
|
|
|
// to make sure the error is not missed, show a message box in addition
|
|
|
|
showErrorMessageBox(message, informativeText);
|
2022-10-28 17:42:57 +03:00
|
|
|
Q_EMIT error(message, informativeText);
|
2022-10-26 21:13:38 +03:00
|
|
|
}
|
|
|
|
|
2022-10-29 14:21:56 +03:00
|
|
|
void EditLocallyJob::showErrorNotification(const QString &message, const QString &informativeText) const
|
2022-10-26 21:13:38 +03:00
|
|
|
{
|
|
|
|
if (!_accountState || !_accountState->account()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto folderMap = FolderMan::instance()->map();
|
|
|
|
const auto foundFolder = std::find_if(folderMap.cbegin(), folderMap.cend(), [this](const auto &folder) {
|
|
|
|
return _accountState->account()->davUrl() == folder->remoteUrl();
|
|
|
|
});
|
|
|
|
|
|
|
|
if (foundFolder != folderMap.cend()) {
|
2022-12-19 17:11:18 +03:00
|
|
|
emit (*foundFolder)->syncEngine().addErrorToGui(SyncFileItem::SoftError, message, informativeText);
|
2022-10-26 21:13:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-29 14:21:56 +03:00
|
|
|
void EditLocallyJob::showErrorMessageBox(const QString &message, const QString &informativeText) const
|
2022-10-26 21:13:38 +03:00
|
|
|
{
|
|
|
|
const auto messageBox = new QMessageBox;
|
|
|
|
messageBox->setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
messageBox->setText(message);
|
|
|
|
messageBox->setInformativeText(informativeText);
|
|
|
|
messageBox->setIcon(QMessageBox::Warning);
|
|
|
|
messageBox->addButton(QMessageBox::StandardButton::Ok);
|
|
|
|
messageBox->show();
|
|
|
|
messageBox->activateWindow();
|
|
|
|
messageBox->raise();
|
|
|
|
}
|
|
|
|
|
2022-10-29 14:21:56 +03:00
|
|
|
void EditLocallyJob::startEditLocally()
|
2022-10-26 21:13:38 +03:00
|
|
|
{
|
2022-10-28 17:42:57 +03:00
|
|
|
if (_fileName.isEmpty() || _localFilePath.isEmpty() || !_folderForFile) {
|
2022-10-29 14:21:56 +03:00
|
|
|
qCWarning(lcEditLocallyJob) << "Could not start to edit locally."
|
2022-10-28 17:42:57 +03:00
|
|
|
<< "fileName:" << _fileName
|
|
|
|
<< "localFilePath:" << _localFilePath
|
|
|
|
<< "folderForFile:" << _folderForFile;
|
2022-10-26 21:13:38 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Systray::instance()->createEditFileLocallyLoadingDialog(_fileName);
|
|
|
|
|
2022-11-04 19:38:59 +03:00
|
|
|
if (_folderForFile->isSyncRunning()) {
|
|
|
|
// in case sync is already running - terminate it and start a new one
|
|
|
|
_syncTerminatedConnection = connect(_folderForFile, &Folder::syncFinished, this, [this]() {
|
|
|
|
disconnect(_syncTerminatedConnection);
|
|
|
|
_syncTerminatedConnection = {};
|
|
|
|
startSyncBeforeOpening();
|
|
|
|
});
|
2022-12-09 18:25:29 +03:00
|
|
|
_folderForFile->setSilenceErrorsUntilNextSync(true);
|
2022-11-04 19:38:59 +03:00
|
|
|
_folderForFile->slotTerminateSync();
|
2022-10-28 20:40:06 +03:00
|
|
|
|
2022-11-04 19:38:59 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
startSyncBeforeOpening();
|
2022-10-26 21:13:38 +03:00
|
|
|
}
|
|
|
|
|
2022-11-04 19:38:59 +03:00
|
|
|
void EditLocallyJob::slotItemCompleted(const OCC::SyncFileItemPtr &item)
|
2022-10-26 21:13:38 +03:00
|
|
|
{
|
2022-11-04 19:38:59 +03:00
|
|
|
Q_ASSERT(item && !item->isEmpty());
|
|
|
|
if (!item || item->isEmpty()) {
|
|
|
|
qCWarning(lcEditLocallyJob) << "invalid item";
|
|
|
|
}
|
|
|
|
if (item->_file == _relativePathToRemoteRoot) {
|
|
|
|
disconnect(&_folderForFile->syncEngine(), &SyncEngine::itemCompleted, this, &EditLocallyJob::slotItemCompleted);
|
|
|
|
disconnect(&_folderForFile->syncEngine(), &SyncEngine::itemDiscovered, this, &EditLocallyJob::slotItemDiscovered);
|
2023-01-09 19:11:33 +03:00
|
|
|
processLocalItem();
|
2022-11-04 19:38:59 +03:00
|
|
|
}
|
2022-10-26 21:13:38 +03:00
|
|
|
}
|
|
|
|
|
2022-11-04 19:38:59 +03:00
|
|
|
void EditLocallyJob::slotLsColJobFinishedWithError(QNetworkReply *reply)
|
2022-10-26 21:13:38 +03:00
|
|
|
{
|
2022-11-04 19:38:59 +03:00
|
|
|
const auto contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
|
|
|
|
const auto invalidContentType = !contentType.contains(QStringLiteral("application/xml; charset=utf-8"))
|
|
|
|
&& !contentType.contains(QStringLiteral("application/xml; charset=\"utf-8\"")) && !contentType.contains(QStringLiteral("text/xml; charset=utf-8"))
|
|
|
|
&& !contentType.contains(QStringLiteral("text/xml; charset=\"utf-8\""));
|
|
|
|
const auto httpCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
|
|
|
|
|
|
|
qCWarning(lcEditLocallyJob) << "LSCOL job error" << reply->errorString() << httpCode << reply->error();
|
|
|
|
|
|
|
|
const auto message = reply->error() == QNetworkReply::NoError && invalidContentType
|
|
|
|
? tr("Server error: PROPFIND reply is not XML formatted!") : reply->errorString();
|
|
|
|
qCWarning(lcEditLocallyJob) << "Could not proceed with setup as file PROPFIND job has failed." << httpCode << message;
|
|
|
|
showError(tr("Could not find a remote file info for local editing. Make sure its path is valid."), _relPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditLocallyJob::slotDirectoryListingIterated(const QString &name, const QMap<QString, QString> &properties)
|
|
|
|
{
|
|
|
|
Q_ASSERT(_relPathParent != QStringLiteral("/"));
|
|
|
|
|
|
|
|
if (_relPathParent == QStringLiteral("/")) {
|
|
|
|
qCWarning(lcEditLocallyJob) << "LsColJob must only be used for nested folders.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto job = qobject_cast<LsColJob*>(sender());
|
|
|
|
Q_ASSERT(job);
|
|
|
|
if (!job) {
|
|
|
|
qCWarning(lcEditLocallyJob) << "Must call slotDirectoryListingIterated from a signal.";
|
2022-10-28 17:42:57 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-04 19:38:59 +03:00
|
|
|
if (name.endsWith(_relPathParent)) {
|
|
|
|
// let's remove remote dav path and remote root from the beginning of the name
|
|
|
|
const auto nameWithoutDavPath = name.mid(_accountState->account()->davPath().size());
|
|
|
|
|
|
|
|
const auto remoteFolderPathWithTrailingSlash = _folderForFile->remotePathTrailingSlash();
|
|
|
|
const auto remoteFolderPathWithoutLeadingSlash = remoteFolderPathWithTrailingSlash.startsWith(QLatin1Char('/'))
|
|
|
|
? remoteFolderPathWithTrailingSlash.mid(1) : remoteFolderPathWithTrailingSlash;
|
2022-10-28 20:40:06 +03:00
|
|
|
|
2022-11-04 19:38:59 +03:00
|
|
|
const auto cleanName = nameWithoutDavPath.startsWith(remoteFolderPathWithoutLeadingSlash)
|
|
|
|
? nameWithoutDavPath.mid(remoteFolderPathWithoutLeadingSlash.size()) : nameWithoutDavPath;
|
|
|
|
disconnect(job, &LsColJob::directoryListingIterated, this, &EditLocallyJob::slotDirectoryListingIterated);
|
|
|
|
_fileParentItem = SyncFileItem::fromProperties(cleanName, properties);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditLocallyJob::slotItemDiscovered(const OCC::SyncFileItemPtr &item)
|
|
|
|
{
|
|
|
|
Q_ASSERT(item && !item->isEmpty());
|
|
|
|
if (!item || item->isEmpty()) {
|
|
|
|
qCWarning(lcEditLocallyJob) << "invalid item";
|
|
|
|
}
|
|
|
|
if (item->_file == _relativePathToRemoteRoot) {
|
|
|
|
disconnect(&_folderForFile->syncEngine(), &SyncEngine::itemDiscovered, this, &EditLocallyJob::slotItemDiscovered);
|
|
|
|
if (item->_instruction == CSYNC_INSTRUCTION_NONE) {
|
|
|
|
// return early if the file is already in sync
|
|
|
|
slotItemCompleted(item);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// or connect to the SyncEngine::itemCompleted and wait till the file gets sycned
|
|
|
|
QObject::connect(&_folderForFile->syncEngine(), &SyncEngine::itemCompleted, this, &EditLocallyJob::slotItemCompleted);
|
2022-10-26 21:13:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-29 14:21:56 +03:00
|
|
|
void EditLocallyJob::openFile()
|
2022-10-26 21:13:38 +03:00
|
|
|
{
|
2022-10-28 17:42:57 +03:00
|
|
|
if(_localFilePath.isEmpty()) {
|
2022-10-29 14:21:56 +03:00
|
|
|
qCWarning(lcEditLocallyJob) << "Could not edit locally. Invalid local file path.";
|
2022-10-28 17:42:57 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-29 21:07:00 +03:00
|
|
|
const auto localFilePathUrl = QUrl::fromLocalFile(_localFilePath);
|
2022-10-26 21:13:38 +03:00
|
|
|
// In case the VFS mode is enabled and a file is not yet hydrated, we must call QDesktopServices::openUrl
|
|
|
|
// from a separate thread, or, there will be a freeze. To avoid searching for a specific folder and checking
|
|
|
|
// if the VFS is enabled - we just always call it from a separate thread.
|
2022-11-29 21:07:00 +03:00
|
|
|
QtConcurrent::run([localFilePathUrl, this]() {
|
2022-12-08 12:58:58 +03:00
|
|
|
if (!QDesktopServices::openUrl(localFilePathUrl)) {
|
|
|
|
emit callShowError(tr("Could not open %1").arg(_fileName), tr("Please try again."));
|
2022-11-29 21:07:00 +03:00
|
|
|
}
|
2022-12-07 14:40:48 +03:00
|
|
|
|
2022-10-26 21:13:38 +03:00
|
|
|
Systray::instance()->destroyEditFileLocallyLoadingDialog();
|
2022-12-07 14:40:48 +03:00
|
|
|
emit finished();
|
2022-10-26 21:13:38 +03:00
|
|
|
});
|
2022-11-29 21:07:00 +03:00
|
|
|
}
|
|
|
|
|
2023-01-09 19:11:33 +03:00
|
|
|
void EditLocallyJob::processLocalItem()
|
|
|
|
{
|
|
|
|
Q_ASSERT(_folderForFile);
|
|
|
|
|
|
|
|
SyncJournalFileRecord rec;
|
|
|
|
const auto ok = _folderForFile->journalDb()->getFileRecord(_relativePathToRemoteRoot, &rec);
|
|
|
|
Q_ASSERT(ok);
|
|
|
|
|
|
|
|
if (rec.isDirectory()) { // Directories not lock-able
|
|
|
|
openFile();
|
|
|
|
} else {
|
|
|
|
lockFile();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-29 21:07:00 +03:00
|
|
|
void EditLocallyJob::lockFile()
|
|
|
|
{
|
|
|
|
Q_ASSERT(_accountState);
|
|
|
|
Q_ASSERT(_accountState->account());
|
|
|
|
Q_ASSERT(_folderForFile);
|
|
|
|
|
2022-12-06 14:34:33 +03:00
|
|
|
if (_accountState->account()->fileLockStatus(_folderForFile->journalDb(), _relativePathToRemoteRoot) == SyncFileItem::LockStatus::LockedItem) {
|
2022-12-06 15:52:36 +03:00
|
|
|
fileAlreadyLocked();
|
2022-11-29 21:07:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-06 15:52:36 +03:00
|
|
|
const auto syncEngineFileSlot = [this](const SyncFileItemPtr &item) {
|
2022-12-06 14:34:33 +03:00
|
|
|
if (item->_file == _relativePathToRemoteRoot && item->_locked == SyncFileItem::LockStatus::LockedItem) {
|
2022-12-06 15:52:36 +03:00
|
|
|
fileLockSuccess(item);
|
2022-12-05 23:22:08 +03:00
|
|
|
}
|
2022-12-06 15:52:36 +03:00
|
|
|
};
|
2022-12-05 23:22:08 +03:00
|
|
|
|
2022-12-07 14:40:48 +03:00
|
|
|
const auto runSingleFileDiscovery = [this] {
|
|
|
|
const SyncEngine::SingleItemDiscoveryOptions singleItemDiscoveryOptions = {(_relPathParent == QStringLiteral("/") ? QString{} : _relPathParent),
|
|
|
|
_relativePathToRemoteRoot,
|
|
|
|
_fileParentItem};
|
|
|
|
_folderForFile->syncEngine().setSingleItemDiscoveryOptions(singleItemDiscoveryOptions);
|
|
|
|
FolderMan::instance()->forceSyncForFolder(_folderForFile);
|
|
|
|
};
|
|
|
|
|
2022-12-06 15:52:36 +03:00
|
|
|
_folderConnections.append(connect(&_folderForFile->syncEngine(), &SyncEngine::itemCompleted,
|
|
|
|
this, syncEngineFileSlot));
|
|
|
|
_folderConnections.append(connect(&_folderForFile->syncEngine(), &SyncEngine::itemDiscovered,
|
|
|
|
this, syncEngineFileSlot));
|
2022-12-07 14:40:48 +03:00
|
|
|
_folderConnections.append(connect(_accountState->account().data(), &Account::lockFileSuccess,
|
|
|
|
this, runSingleFileDiscovery));
|
2022-11-29 21:07:00 +03:00
|
|
|
_folderConnections.append(connect(_accountState->account().data(), &Account::lockFileError,
|
|
|
|
this, &EditLocallyJob::fileLockError));
|
|
|
|
|
|
|
|
_folderForFile->accountState()->account()->setLockFileState(_relPath,
|
|
|
|
_folderForFile->journalDb(),
|
|
|
|
SyncFileItem::LockStatus::LockedItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditLocallyJob::disconnectFolderSignals()
|
|
|
|
{
|
|
|
|
for (const auto &connection : qAsConst(_folderConnections)) {
|
|
|
|
disconnect(connection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-06 15:52:36 +03:00
|
|
|
void EditLocallyJob::fileAlreadyLocked()
|
2022-11-29 21:07:00 +03:00
|
|
|
{
|
|
|
|
SyncJournalFileRecord rec;
|
2022-12-06 14:34:33 +03:00
|
|
|
Q_ASSERT(_folderForFile->journalDb()->getFileRecord(_relativePathToRemoteRoot, &rec));
|
2022-11-29 21:07:00 +03:00
|
|
|
Q_ASSERT(rec.isValid());
|
|
|
|
Q_ASSERT(rec._lockstate._locked);
|
|
|
|
|
2022-12-06 15:52:36 +03:00
|
|
|
const auto remainingTimeInMinutes = fileLockTimeRemainingMinutes(rec._lockstate._lockTime, rec._lockstate._lockTimeout);
|
|
|
|
fileLockProcedureComplete(tr("File %1 already locked.").arg(_fileName),
|
|
|
|
tr("Lock will last for %1 minutes. "
|
|
|
|
"You can also unlock this file manually once you are finished editing.").arg(remainingTimeInMinutes),
|
|
|
|
true);
|
|
|
|
}
|
2022-11-29 21:07:00 +03:00
|
|
|
|
2022-12-06 15:52:36 +03:00
|
|
|
void EditLocallyJob::fileLockSuccess(const SyncFileItemPtr &item)
|
|
|
|
{
|
|
|
|
qCDebug(lcEditLocallyJob()) << "File lock succeeded, showing notification" << _relPath;
|
2022-11-29 21:07:00 +03:00
|
|
|
|
2022-12-06 15:52:36 +03:00
|
|
|
const auto remainingTimeInMinutes = fileLockTimeRemainingMinutes(item->_lockTime, item->_lockTimeout);
|
|
|
|
fileLockProcedureComplete(tr("File %1 now locked.").arg(_fileName),
|
|
|
|
tr("Lock will last for %1 minutes. "
|
|
|
|
"You can also unlock this file manually once you are finished editing.").arg(remainingTimeInMinutes),
|
|
|
|
true);
|
2022-11-29 21:07:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditLocallyJob::fileLockError(const QString &errorMessage)
|
|
|
|
{
|
|
|
|
qCWarning(lcEditLocallyJob()) << "File lock failed, showing notification" << _relPath << errorMessage;
|
2022-12-06 15:52:36 +03:00
|
|
|
fileLockProcedureComplete(tr("File %1 could not be locked."), errorMessage, false);
|
|
|
|
}
|
2022-11-29 21:07:00 +03:00
|
|
|
|
2022-12-06 15:52:36 +03:00
|
|
|
void EditLocallyJob::fileLockProcedureComplete(const QString ¬ificationTitle,
|
|
|
|
const QString ¬ificationMessage,
|
|
|
|
const bool success)
|
|
|
|
{
|
|
|
|
Systray::instance()->showMessage(notificationTitle,
|
|
|
|
notificationMessage,
|
|
|
|
success ? QSystemTrayIcon::Information : QSystemTrayIcon::Warning);
|
2022-10-26 21:13:38 +03:00
|
|
|
|
2022-11-29 21:07:00 +03:00
|
|
|
disconnectFolderSignals();
|
2022-12-07 14:40:48 +03:00
|
|
|
openFile();
|
2022-10-26 21:13:38 +03:00
|
|
|
}
|
|
|
|
|
2022-12-07 15:47:48 +03:00
|
|
|
int EditLocallyJob::fileLockTimeRemainingMinutes(const qint64 lockTime, const qint64 lockTimeOut)
|
2022-12-06 15:52:36 +03:00
|
|
|
{
|
|
|
|
const auto lockExpirationTime = lockTime + lockTimeOut;
|
|
|
|
const auto remainingTime = QDateTime::currentDateTime().secsTo(QDateTime::fromSecsSinceEpoch(lockExpirationTime));
|
|
|
|
|
|
|
|
static constexpr auto SECONDS_PER_MINUTE = 60;
|
|
|
|
const auto remainingTimeInMinutes = static_cast<int>(remainingTime > 0 ? remainingTime / SECONDS_PER_MINUTE : 0);
|
|
|
|
|
|
|
|
return remainingTimeInMinutes;
|
|
|
|
}
|
|
|
|
|
2022-10-26 21:13:38 +03:00
|
|
|
}
|