Skip to content

Commit

Permalink
introduce new jobs to handle lock/unlock of files
Browse files Browse the repository at this point in the history
close #4382

Signed-off-by: Matthieu Gallien <[email protected]>
  • Loading branch information
mgallien committed Apr 14, 2022
1 parent f040776 commit 93b76b5
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ set(client_SRCS
ocssharejob.cpp
ocsshareejob.h
ocsshareejob.cpp
ocslockfilejob.h
ocslockfilejob.cpp
openfilemanager.h
openfilemanager.cpp
owncloudgui.h
Expand Down
43 changes: 43 additions & 0 deletions src/gui/ocslockfilejob.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (C) by Matthieu Gallien <[email protected]>
*
* 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.
*/

#include "ocslockfilejob.h"

#include "account.h"

namespace OCC {

OcsLockFileJob::OcsLockFileJob(const AccountPtr account)
: OcsJob(account)
{
setPath("ocs/v2.php/apps/files_lock/lock/");
}

void OCC::OcsLockFileJob::acquireLock(const int fileId)
{
appendPath(QString::number(fileId));
setVerb("PUT");

start();
}

void OcsLockFileJob::releaseLock(const int fileId)
{
appendPath(QString::number(fileId));
setVerb("DELETE");

start();
}

}
48 changes: 48 additions & 0 deletions src/gui/ocslockfilejob.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (C) by Matthieu Gallien <[email protected]>
*
* 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.
*/

#ifndef OCSLOCKFILEJOB_H
#define OCSLOCKFILEJOB_H

#include "ocsjob.h"

#include "accountfwd.h"

#include <QObject>

class QJsonDocument;

namespace OCC {

/**
* @brief The OcsShareJob class
* @ingroup gui
*
* Handle talking to the OCS Share API.
* For creation, deletion and modification of shares.
*/
class OcsLockFileJob : public OcsJob
{
Q_OBJECT
public:
explicit OcsLockFileJob(const AccountPtr account);

void acquireLock(const int fileId);

void releaseLock(const int fileId);
};

}

#endif // OCSLOCKFILEJOB_H
2 changes: 2 additions & 0 deletions src/libsync/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ set(libsync_SRCS
userstatusconnector.cpp
ocsprofileconnector.h
ocsprofileconnector.cpp
lockfilejobs.h
lockfilejobs.cpp
creds/dummycredentials.h
creds/dummycredentials.cpp
creds/abstractcredentials.h
Expand Down
92 changes: 92 additions & 0 deletions src/libsync/lockfilejobs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright (C) by Matthieu Gallien <[email protected]>
*
* 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.
*/

#include "lockfilejobs.h"

#include "account.h"
#include "common/syncjournaldb.h"

#include <QLoggingCategory>

namespace OCC {

Q_LOGGING_CATEGORY(lcLockFileJob, "nextcloud.sync.networkjob.lockfile", QtInfoMsg)

LockFileJob::LockFileJob(const AccountPtr account,
SyncJournalDb* const journal,
const QString &path,
const SyncFileItem::LockStatus requestedLockState,
QObject *parent)
: AbstractNetworkJob(account, path, parent)
, _journal(journal)
, _requestedLockState(requestedLockState)
{
}

void LockFileJob::start()
{
qCInfo(lcLockFileJob()) << "start" << path() << _requestedLockState;

QNetworkRequest request;
request.setRawHeader("X-User-Lock", "1");

QByteArray verb;
switch(_requestedLockState)
{
case SyncFileItem::LockStatus::LockedItem:
verb = "LOCK";
break;
case SyncFileItem::LockStatus::UnlockedItem:
verb = "UNLOCK";
break;
}
sendRequest(verb, makeDavUrl(path()), request);

AbstractNetworkJob::start();
}

bool LockFileJob::finished()
{
if (reply()->error() != QNetworkReply::NoError) {
qCInfo(lcLockFileJob()) << "finished with error" << reply()->error() << reply()->errorString();
Q_EMIT finishedWithError(reply());
} else {
qCInfo(lcLockFileJob()) << "success" << path();
SyncJournalFileRecord record;
const auto relativePath = path().mid(1);
if (_journal->getFileRecord(relativePath, &record) && record.isValid()) {
switch(_requestedLockState)
{
case SyncFileItem::LockStatus::LockedItem:
record._locked = true;
record._lockOwnerType = static_cast<int>(SyncFileItem::LockOwnerType::UserLock);
record._lockOwnerDisplayName = account()->davDisplayName();
record._lockOwnerId = account()->davUser();
record._lockTime = QDateTime::currentSecsSinceEpoch();
break;
case SyncFileItem::LockStatus::UnlockedItem:
record._locked = false;
break;
}
_journal->setFileRecord(record);
_journal->commit("lock file job");
} else {
qCInfo(lcLockFileJob()) << "failed to update database record of" << path();
}
Q_EMIT finishedWithoutError();
}
return true;
}

}
37 changes: 37 additions & 0 deletions src/libsync/lockfilejobs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef LOCKFILEJOBS_H
#define LOCKFILEJOBS_H

#include "abstractnetworkjob.h"

#include "syncfileitem.h"

namespace OCC {

class SyncJournalDb;

class OWNCLOUDSYNC_EXPORT LockFileJob : public AbstractNetworkJob
{
Q_OBJECT

public:
explicit LockFileJob(const AccountPtr account,
SyncJournalDb* const journal,
const QString &path,
const SyncFileItem::LockStatus requestedLockState,
QObject *parent = nullptr);
void start() override;

signals:
void finishedWithError(QNetworkReply *reply);
void finishedWithoutError();

private:
bool finished() override;

SyncJournalDb* _journal = nullptr;
SyncFileItem::LockStatus _requestedLockState = SyncFileItem::LockStatus::LockedItem;
};

}

#endif // LOCKFILEJOBS_H

0 comments on commit 93b76b5

Please sign in to comment.