From 9b74a596c6b7bfb2c73a5626860ca5f5cdc27784 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Fri, 11 May 2018 23:22:33 +0200 Subject: [PATCH] Forbid to encrypt or decrypt a non-empty folder. The specs forbid the encryption or decryption of a non empty folder. so... 1 - check for the sync status, if it's not synced return as there's no way that I can say that there's items on the server right now without waiting for the sync to finish 2 - verify if the folder is empty locally as the user could have send some files to the folder. --- src/gui/accountsettings.cpp | 48 ++++++++++++++++++++++++++++++++----- src/gui/accountsettings.h | 7 +++--- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/src/gui/accountsettings.cpp b/src/gui/accountsettings.cpp index c47c01803..2f8f1bb96 100644 --- a/src/gui/accountsettings.cpp +++ b/src/gui/accountsettings.cpp @@ -34,6 +34,7 @@ #include "tooltipupdater.h" #include "filesystem.h" #include "clientsideencryptionjobs.h" +#include "syncresult.h" #include @@ -335,9 +336,36 @@ void AccountSettings::slotUnlockFolderSuccess(const QByteArray& fileId) { qCInfo(lcAccountSettings()) << "Unlocking success!"; } -void AccountSettings::slotMarkSubfolderEncrpted(const QByteArray& fileId) + +bool AccountSettings::canEncryptOrDecrypt (const FolderStatusModel::SubFolderInfo* info) { + if (info->_folder->syncResult().status() != SyncResult::Status::Success) { + QMessageBox msgBox; + msgBox.setText("Please wait for the folder to sync before trying to encrypt it."); + msgBox.exec(); + return false; + } + + // for some reason the actual folder in disk is info->_folder->path + info->_path. + QDir folderPath(info->_folder->path() + info->_path); + folderPath.setFilter( QDir::AllEntries | QDir::NoDotAndDotDot ); + + if (folderPath.count() != 0) { + QMessageBox msgBox; + msgBox.setText("You cannot encyrpt a folder with contents, please remove the files \n" + "Wait for the new sync, then encrypt it."); + msgBox.exec(); + return false; + } + return true; +} + +void AccountSettings::slotMarkSubfolderEncrpted(const FolderStatusModel::SubFolderInfo* folderInfo) { - auto job = new OCC::SetEncryptionFlagApiJob(accountsState()->account(), fileId); + if (!canEncryptOrDecrypt(folderInfo)) { + return; + } + + auto job = new OCC::SetEncryptionFlagApiJob(accountsState()->account(), folderInfo->_fileId); connect(job, &OCC::SetEncryptionFlagApiJob::success, this, &AccountSettings::slotEncryptionFlagSuccess); connect(job, &OCC::SetEncryptionFlagApiJob::error, this, &AccountSettings::slotEncryptionFlagError); job->start(); @@ -349,11 +377,17 @@ void AccountSettings::slotMarkSubfolderEncrpted(const QByteArray& fileId) // 2 - Delete Metadata, // 3 - Unlock Folder, // 4 - Mark as Decrypted. -void AccountSettings::slotMarkSubfolderDecrypted(const QByteArray& fileId) + + +void AccountSettings::slotMarkSubfolderDecrypted(const FolderStatusModel::SubFolderInfo* folderInfo) { + if (!canEncryptOrDecrypt(folderInfo)) { + return; + } + qDebug() << "Starting to mark as decrypted"; qDebug() << "Locking the folder"; - auto lockJob = new LockEncryptFolderApiJob(accountsState()->account(), fileId); + auto lockJob = new LockEncryptFolderApiJob(accountsState()->account(), folderInfo->_fileId); connect(lockJob, &LockEncryptFolderApiJob::success, this, &AccountSettings::slotLockForDecryptionSuccess); connect(lockJob, &LockEncryptFolderApiJob::error, @@ -444,13 +478,15 @@ void AccountSettings::slotSubfolderContextMenuRequested(const QModelIndex& index auto acc = _accountState->account(); if (acc->capabilities().clientSideEncryptionAvaliable()) { + // Verify if the folder is empty before attempting to encrypt. + bool isEncrypted = acc->e2e()->isFolderEncrypted(info->_path); ac = menu.addAction( isEncrypted ? tr("Decrypt") : tr("Encrypt")); if (not isEncrypted) { - connect(ac, &QAction::triggered, [this, &info] { slotMarkSubfolderEncrpted(info->_fileId); }); + connect(ac, &QAction::triggered, [this, &info] { slotMarkSubfolderEncrpted(info); }); } else { - connect(ac, &QAction::triggered, [this, &info] { slotMarkSubfolderDecrypted(info->_fileId); }); + connect(ac, &QAction::triggered, [this, &info] { slotMarkSubfolderDecrypted(info); }); } } menu.exec(QCursor::pos()); diff --git a/src/gui/accountsettings.h b/src/gui/accountsettings.h index 798b867f0..2345e90bc 100644 --- a/src/gui/accountsettings.h +++ b/src/gui/accountsettings.h @@ -25,6 +25,7 @@ #include "quotainfo.h" #include "progressdispatcher.h" #include "owncloudgui.h" +#include "folderstatusmodel.h" class QModelIndex; class QNetworkReply; @@ -55,7 +56,7 @@ public: explicit AccountSettings(AccountState *accountState, QWidget *parent = 0); ~AccountSettings(); QSize sizeHint() const Q_DECL_OVERRIDE { return ownCloudGui::settingsDialogSize(); } - + bool canEncryptOrDecrypt(const FolderStatusModel::SubFolderInfo* folderInfo); signals: void folderChanged(); @@ -85,8 +86,8 @@ protected slots: void slotOpenAccountWizard(); void slotAccountAdded(AccountState *); void refreshSelectiveSyncStatus(); - void slotMarkSubfolderEncrpted(const QByteArray& fileId); - void slotMarkSubfolderDecrypted(const QByteArray& fileId); + void slotMarkSubfolderEncrpted(const FolderStatusModel::SubFolderInfo* folderInfo); + void slotMarkSubfolderDecrypted(const FolderStatusModel::SubFolderInfo* folderInfo); void slotSubfolderContextMenuRequested(const QModelIndex& idx, const QPoint& point); void slotCustomContextMenuRequested(const QPoint &); void slotFolderListClicked(const QModelIndex &indx);