From 22a3b19e08f685865a2cee8e3ef847697cc8daee Mon Sep 17 00:00:00 2001 From: Felix Weilbach Date: Thu, 15 Apr 2021 13:06:53 +0200 Subject: [PATCH] Add expire date to user and group share Fixes #3100 Signed-off-by: Felix Weilbach --- src/gui/sharemanager.cpp | 41 ++++++++++++++++++++++++-- src/gui/sharemanager.h | 11 ++++++- src/gui/shareusergroupwidget.cpp | 50 +++++++++++++++++++++++++++++++- src/gui/shareusergroupwidget.h | 5 ++++ src/gui/shareuserline.ui | 46 ++++++++++++++++++++++++++++- 5 files changed, 148 insertions(+), 5 deletions(-) diff --git a/src/gui/sharemanager.cpp b/src/gui/sharemanager.cpp index 0b0ac83c3..5224fc34e 100644 --- a/src/gui/sharemanager.cpp +++ b/src/gui/sharemanager.cpp @@ -285,8 +285,10 @@ UserGroupShare::UserGroupShare(AccountPtr account, const QString &path, const ShareType shareType, const Permissions permissions, - const QSharedPointer shareWith) + const QSharedPointer shareWith, + const QDate &expireDate) : Share(account, id, owner, ownerDisplayName, path, shareType, permissions, shareWith) + , _expireDate(expireDate) { Q_ASSERT(shareType == TypeUser || shareType == TypeGroup); Q_ASSERT(shareWith); @@ -306,6 +308,35 @@ void UserGroupShare::slotNoteSet(const QJsonDocument &, const QVariant ¬e) emit noteSet(); } +QDate UserGroupShare::getExpireDate() const +{ + return _expireDate; +} + +void UserGroupShare::setExpireDate(const QDate &date) +{ + auto *job = new OcsShareJob(_account); + connect(job, &OcsShareJob::shareJobFinished, this, &UserGroupShare::slotExpireDateSet); + connect(job, &OcsJob::ocsError, this, &UserGroupShare::slotOcsError); + job->setExpireDate(getId(), date); +} + +void UserGroupShare::slotExpireDateSet(const QJsonDocument &reply, const QVariant &value) +{ + auto data = reply.object().value("ocs").toObject().value("data").toObject(); + + /* + * If the reply provides a data back (more REST style) + * they use this date. + */ + if (data.value("expiration").isString()) { + _expireDate = QDate::fromString(data.value("expiration").toString(), "yyyy-MM-dd 00:00:00"); + } else { + _expireDate = value.toDate(); + } + emit expireDateSet(); +} + ShareManager::ShareManager(AccountPtr account, QObject *parent) : QObject(parent) , _account(account) @@ -437,6 +468,11 @@ QSharedPointer ShareManager::parseUserGroupShare(const QJsonObje data.value("share_with_displayname").toString(), static_cast(data.value("share_type").toInt()))); + QDate expireDate; + if (data.value("expiration").isString()) { + expireDate = QDate::fromString(data.value("expiration").toString(), "yyyy-MM-dd 00:00:00"); + } + return QSharedPointer(new UserGroupShare(_account, data.value("id").toVariant().toString(), // "id" used to be an integer, support both data.value("uid_owner").toVariant().toString(), @@ -444,7 +480,8 @@ QSharedPointer ShareManager::parseUserGroupShare(const QJsonObje data.value("path").toString(), static_cast(data.value("share_type").toInt()), static_cast(data.value("permissions").toInt()), - sharee)); + sharee, + expireDate)); } QSharedPointer ShareManager::parseLinkShare(const QJsonObject &data) diff --git a/src/gui/sharemanager.h b/src/gui/sharemanager.h index b43d35ef4..c17ed31bf 100644 --- a/src/gui/sharemanager.h +++ b/src/gui/sharemanager.h @@ -270,17 +270,26 @@ public: const QString &path, const ShareType shareType, const Permissions permissions, - const QSharedPointer shareWith); + const QSharedPointer shareWith, + const QDate &expireDate); void setNote(const QString ¬e); void slotNoteSet(const QJsonDocument &, const QVariant ¬e); + void setExpireDate(const QDate &date); + + QDate getExpireDate() const; + + void slotExpireDateSet(const QJsonDocument &reply, const QVariant &value); + signals: void noteSet(); + void expireDateSet(); private: QString _note; + QDate _expireDate; }; /** diff --git a/src/gui/shareusergroupwidget.cpp b/src/gui/shareusergroupwidget.cpp index 51f662130..a233168fa 100644 --- a/src/gui/shareusergroupwidget.cpp +++ b/src/gui/shareusergroupwidget.cpp @@ -445,6 +445,8 @@ ShareUserLine::ShareUserLine(QSharedPointer share, _ui->permissionsEdit->setEnabled(enabled); connect(_ui->permissionsEdit, &QAbstractButton::clicked, this, &ShareUserLine::slotEditPermissionsChanged); connect(_ui->noteConfirmButton, &QAbstractButton::clicked, this, &ShareUserLine::onNoteConfirmButtonClicked); + connect(_ui->confirmExpirationDate, &QAbstractButton::clicked, this, &ShareUserLine::setExpireDate); + connect(_ui->calendar, &QDateTimeEdit::dateChanged, this, &ShareUserLine::setExpireDate); // create menu with checkable permissions auto *menu = new QMenu(this); @@ -454,11 +456,24 @@ ShareUserLine::ShareUserLine(QSharedPointer share, menu->addAction(_permissionReshare); connect(_permissionReshare, &QAction::triggered, this, &ShareUserLine::slotPermissionsChanged); + toggleNoteOptions(false); _noteLinkAction = new QAction(tr("Note to recipient")); _noteLinkAction->setCheckable(true); menu->addAction(_noteLinkAction); connect(_noteLinkAction, &QAction::triggered, this, &ShareUserLine::toggleNoteOptions); + toggleExpireDateOptions(false); + _expirationDateLinkAction = new QAction(tr("Set expiration date")); + _expirationDateLinkAction->setCheckable(true); + menu->addAction(_expirationDateLinkAction); + connect(_expirationDateLinkAction, &QAction::triggered, this, &ShareUserLine::toggleExpireDateOptions); + const auto expireDate = _share.data()->getExpireDate().isValid() ? share.data()->getExpireDate() : QDate(); + if (!expireDate.isNull()) { + _ui->calendar->setDate(expireDate); + _expirationDateLinkAction->setChecked(true); + showExpireDateOptions(true); + } + menu->addSeparator(); // Adds action to delete share widget @@ -516,7 +531,6 @@ ShareUserLine::ShareUserLine(QSharedPointer share, } loadAvatar(); - toggleNoteOptions(false); customizeStyle(); } @@ -727,6 +741,7 @@ void ShareUserLine::customizeStyle() _deleteShareButton->setIcon(deleteicon); _ui->noteConfirmButton->setIcon(Theme::createColorAwareIcon(":/client/theme/confirm.svg")); + _ui->confirmExpirationDate->setIcon(Theme::createColorAwareIcon(":/client/theme/confirm.svg")); } void ShareUserLine::showNoteOptions(bool show) @@ -764,4 +779,37 @@ void ShareUserLine::setNote(const QString ¬e) _share->setNote(note); } } + +void ShareUserLine::toggleExpireDateOptions(bool enable) +{ + showExpireDateOptions(enable); + + if (enable) { + const QDate date = QDate::currentDate().addDays(1); + _ui->calendar->setDate(date); + _ui->calendar->setMinimumDate(date); + _ui->calendar->setFocus(); + } else { + // 'deletes' expire date + if (_share) + _share->setExpireDate(QDate()); + } +} + +void ShareUserLine::showExpireDateOptions(bool show) +{ + _ui->expirationLabel->setVisible(show); + _ui->calendar->setVisible(show); + _ui->confirmExpirationDate->setVisible(show); + emit resizeRequested(); +} + +void ShareUserLine::setExpireDate() +{ + if (!_share) { + return; + } + + _share->setExpireDate(_ui->calendar->date()); +} } diff --git a/src/gui/shareusergroupwidget.h b/src/gui/shareusergroupwidget.h index 79dc3216e..80b70933b 100644 --- a/src/gui/shareusergroupwidget.h +++ b/src/gui/shareusergroupwidget.h @@ -156,6 +156,10 @@ private: void onNoteConfirmButtonClicked(); void setNote(const QString ¬e); + void toggleExpireDateOptions(bool enable); + void showExpireDateOptions(bool show); + void setExpireDate(); + Ui::ShareUserLine *_ui; QSharedPointer _share; bool _isFile; @@ -167,6 +171,7 @@ private: QAction *_permissionChange; QAction *_permissionDelete; QAction *_noteLinkAction; + QAction *_expirationDateLinkAction; }; } diff --git a/src/gui/shareuserline.ui b/src/gui/shareuserline.ui index 6fdebefbb..2bc0f997b 100644 --- a/src/gui/shareuserline.ui +++ b/src/gui/shareuserline.ui @@ -6,7 +6,7 @@ 0 0 - 523 + 486 208 @@ -163,6 +163,50 @@ + + + + + + + 78 + 0 + + + + Expires: + + + Qt::AlignCenter + + + + + + + + 1 + 0 + + + + + + + + ... + + + + :/client/theme/confirm.svg:/client/theme/confirm.svg + + + true + + + + +