diff options
author | Camila San <hello@camila.codes> | 2019-04-06 21:15:24 +0300 |
---|---|---|
committer | Camila San <hello@camila.codes> | 2019-04-12 15:17:58 +0300 |
commit | e07c472057544fd2a59116970da99ac87fd1f6be (patch) | |
tree | 62c28b50b2977e43ea4cda36a195d08010d75418 /src/gui/sharelinkwidget.cpp | |
parent | 0fed1cc54df815a2839ecae13ddf3535ac35290c (diff) |
Refactores sharing link to support multiple share links.
Signed-off-by: Camila San <hello@camila.codes>
Diffstat (limited to 'src/gui/sharelinkwidget.cpp')
-rw-r--r-- | src/gui/sharelinkwidget.cpp | 429 |
1 files changed, 187 insertions, 242 deletions
diff --git a/src/gui/sharelinkwidget.cpp b/src/gui/sharelinkwidget.cpp index 51c437965..a09e2c4e3 100644 --- a/src/gui/sharelinkwidget.cpp +++ b/src/gui/sharelinkwidget.cpp @@ -17,9 +17,8 @@ #include "sharelinkwidget.h" #include "account.h" #include "capabilities.h" - -#include "sharemanager.h" #include "guiutility.h" +#include "sharemanager.h" #include "QProgressIndicator.h" #include <QBuffer> @@ -29,9 +28,12 @@ #include <QMessageBox> #include <QMenu> #include <QToolButton> +#include <QPropertyAnimation> namespace OCC { +Q_LOGGING_CATEGORY(lcShareLink, "nextcloud.gui.sharelink", QtInfoMsg) + ShareLinkWidget::ShareLinkWidget(AccountPtr account, const QString &sharePath, const QString &localPath, @@ -42,13 +44,11 @@ ShareLinkWidget::ShareLinkWidget(AccountPtr account, , _account(account) , _sharePath(sharePath) , _localPath(localPath) - , _manager(nullptr) , _linkShare(nullptr) , _passwordRequired(false) , _expiryRequired(false) , _namesSupported(true) , _linkContextMenu(nullptr) - , _copyLinkAction(nullptr) , _readOnlyLinkAction(nullptr) , _allowEditingLinkAction(nullptr) , _allowUploadEditingLinkAction(nullptr) @@ -68,26 +68,26 @@ ShareLinkWidget::ShareLinkWidget(AccountPtr account, QFileInfo fi(localPath); _isFile = fi.isFile(); - connect(_ui->enableShareLink, &QCheckBox::clicked, this, &ShareLinkWidget::slotCreateOrDeleteShareLink); + connect(_ui->enableShareLink, &QPushButton::clicked, this, &ShareLinkWidget::slotCreateShareLink); connect(_ui->lineEdit_password, &QLineEdit::returnPressed, this, &ShareLinkWidget::slotCreatePassword); connect(_ui->confirmPassword, &QAbstractButton::clicked, this, &ShareLinkWidget::slotCreatePassword); connect(_ui->confirmExpirationDate, &QAbstractButton::clicked, this, &ShareLinkWidget::slotSetExpireDate); - connect(_ui->calendar, &QDateTimeEdit::dateChanged, this, &ShareLinkWidget::slotExpireDateChanged); + connect(_ui->calendar, &QDateTimeEdit::dateChanged, this, &ShareLinkWidget::slotSetExpireDate); _ui->errorLabel->hide(); bool sharingPossible = true; if (!_account->capabilities().sharePublicLink()) { - qCWarning(lcSharing) << "Link shares have been disabled"; + qCWarning(lcShareLink) << "Link shares have been disabled"; sharingPossible = false; } else if (!(maxSharingPermissions & SharePermissionShare)) { - qCWarning(lcSharing) << "The file can not be shared because it was shared without sharing permission."; + qCWarning(lcShareLink) << "The file can not be shared because it was shared without sharing permission."; sharingPossible = false; } - _ui->createShareButton->setVisible(sharingPossible); - _ui->enableShareLink->setVisible(sharingPossible); - _ui->shareLinkToolButton->setVisible(sharingPossible); + _ui->enableShareLink->setChecked(false); + _ui->shareLinkToolButton->setEnabled(false); + _ui->shareLinkToolButton->hide(); // Older servers don't support multiple public link shares if (!_account->capabilities().sharePublicLinkMultiple()) { @@ -100,34 +100,9 @@ ShareLinkWidget::ShareLinkWidget(AccountPtr account, // check if the file is already inside of a synced folder if (sharePath.isEmpty()) { - // The file is not yet in an ownCloud synced folder. We could automatically - // copy it over, but that is skipped as not all questions can be answered that - // are involved in that, see https://github.com/owncloud/client/issues/2732 - // - // _ui->checkBox_shareLink->setEnabled(false); - // uploadExternalFile(); - qCWarning(lcSharing) << "Unable to share files not in a sync folder."; + qCWarning(lcShareLink) << "Unable to share files not in a sync folder."; return; } - - - // TODO File Drop - // File can't have public upload set; we also hide it if the capability isn't there -// _ui->widget_editing->setVisible( -// !_isFile && _account->capabilities().sharePublicLinkAllowUpload()); - //_ui->radio_uploadOnly->setVisible( - //_account->capabilities().sharePublicLinkSupportsUploadOnly()); - - /* - * Create the share manager and connect it properly - */ - if (sharingPossible) { - _manager = new ShareManager(_account, this); - connect(_manager, &ShareManager::sharesFetched, this, &ShareLinkWidget::slotSharesFetched); - connect(_manager, &ShareManager::linkShareCreated, this, &ShareLinkWidget::slotCreateShareFetched); - connect(_manager, &ShareManager::linkShareRequiresPassword, this, &ShareLinkWidget::slotCreateShareRequiresPassword); - connect(_manager, &ShareManager::serverError, this, &ShareLinkWidget::slotServerError); - } } ShareLinkWidget::~ShareLinkWidget() @@ -135,7 +110,7 @@ ShareLinkWidget::~ShareLinkWidget() delete _ui; } -void ShareLinkWidget::toggleAnimation(bool start){ +void ShareLinkWidget::slotToggleAnimation(bool start){ if (start) { if (!_ui->progressIndicator->isAnimated()) _ui->progressIndicator->startAnimation(); @@ -144,218 +119,170 @@ void ShareLinkWidget::toggleAnimation(bool start){ } } -void ShareLinkWidget::getShares() -{ - if (_manager) { - toggleAnimation(true); - _manager->fetchShares(_sharePath); - } +void ShareLinkWidget::setLinkShare(QSharedPointer<LinkShare> linkShare){ + _linkShare = linkShare; } -void ShareLinkWidget::slotSharesFetched(const QList<QSharedPointer<Share>> &shares) -{ - const QString versionString = _account->serverVersion(); - qCInfo(lcSharing) << versionString << "Fetched" << shares.count() << "shares"; - - foreach (auto share, shares) { - if (share->getShareType() != Share::TypeLink) { - continue; - } - _linkShare = qSharedPointerDynamicCast<LinkShare>(share); - - // Connect all shares signals to gui slots - connect(share.data(), &Share::serverError, this, &ShareLinkWidget::slotServerError); - connect(share.data(), &Share::shareDeleted, this, &ShareLinkWidget::slotDeleteShareFetched); - connect(_linkShare.data(), &LinkShare::expireDateSet, this, &ShareLinkWidget::slotExpireDateSet); - connect(_linkShare.data(), &LinkShare::passwordSet, this, &ShareLinkWidget::slotPasswordSet); - connect(_linkShare.data(), &LinkShare::passwordSetError, this, &ShareLinkWidget::slotPasswordSetError); - - // Prepare permissions check and create group action - bool checked = false; - SharePermissions perm = _linkShare->getPermissions(); - QActionGroup *permissionsGroup = new QActionGroup(this); - - // Prepare sharing menu - _linkContextMenu = new QMenu(this); - - // radio button style - permissionsGroup->setExclusive(true); - - if(_isFile){ - checked = perm & (SharePermissionRead & SharePermissionUpdate); - _allowEditingLinkAction = _linkContextMenu->addAction(tr("Allow Editing")); - _allowEditingLinkAction->setCheckable(true); - _allowEditingLinkAction->setChecked(checked); - - } else { - checked = perm & SharePermissionRead; - _readOnlyLinkAction = permissionsGroup->addAction(tr("Read only")); - _readOnlyLinkAction->setCheckable(true); - _readOnlyLinkAction->setChecked(checked); - - checked = perm & (SharePermissionRead & - SharePermissionCreate & - SharePermissionUpdate & - SharePermissionDelete); - _allowUploadEditingLinkAction = permissionsGroup->addAction(tr("Allow Upload && Editing")); - _allowUploadEditingLinkAction->setCheckable(true); - _allowUploadEditingLinkAction->setChecked(checked); - - checked = perm & SharePermissionCreate; - _allowUploadLinkAction = permissionsGroup->addAction(tr("File Drop (Upload Only)")); - _allowUploadLinkAction->setCheckable(true); - _allowUploadLinkAction->setChecked(checked); - } - - // Add copy action (icon only) - _copyLinkAction = _linkContextMenu->addAction(QIcon(":/client/resources/copy.svg"), - tr("Copy link")); - - // Adds permissions actions (radio button style) - if(_isFile){ - _linkContextMenu->addAction(_allowEditingLinkAction); - } else { - _linkContextMenu->addAction(_readOnlyLinkAction); - _linkContextMenu->addAction(_allowUploadEditingLinkAction); - _linkContextMenu->addAction(_allowUploadLinkAction); - } - - - // Adds action to display password widget (check box) - _passwordProtectLinkAction = _linkContextMenu->addAction(tr("Password Protect")); - _passwordProtectLinkAction->setCheckable(true); - - if(_linkShare->isPasswordSet()){ - _passwordProtectLinkAction->setChecked(true); - _ui->lineEdit_password->setPlaceholderText("********"); - showPasswordOptions(true); - } - - // If password is enforced then don't allow users to disable it - if (_account->capabilities().sharePublicLinkEnforcePassword()) { - _passwordProtectLinkAction->setChecked(true); - _passwordProtectLinkAction->setEnabled(false); - _passwordRequired = true; - } - - // Adds action to display expiration date widget (check box) - _expirationDateLinkAction = _linkContextMenu->addAction(tr("Expiration Date")); - _expirationDateLinkAction->setCheckable(true); - if(_linkShare->getExpireDate().isValid()){ - _ui->calendar->setDate(_linkShare->getExpireDate()); - _expirationDateLinkAction->setChecked(true); - showExpireDateOptions(true); - } - - - // If expiredate is enforced do not allow disable and set max days - if (_account->capabilities().sharePublicLinkEnforceExpireDate()) { - _ui->calendar->setMaximumDate(QDate::currentDate().addDays( - _account->capabilities().sharePublicLinkExpireDateDays())); - _expirationDateLinkAction->setChecked(true); - _expirationDateLinkAction->setEnabled(false); - _expiryRequired = true; - } - - // Adds action to unshare widget (check box) - _unshareLinkAction = _linkContextMenu->addAction(QIcon(":/client/resources/delete.png"), - tr("Unshare")); - - connect(_linkContextMenu, &QMenu::triggered, - this, &ShareLinkWidget::slotLinkContextMenuActionTriggered); - - _ui->shareLinkToolButton->setMenu(_linkContextMenu); - _ui->shareLinkToolButton->setEnabled(true); - _ui->enableShareLink->setEnabled(true); - _ui->enableShareLink->setChecked(true); - - // show sharing options - _ui->shareLinkToolButton->show(); +QSharedPointer<LinkShare> ShareLinkWidget::getLinkShare(){ + return _linkShare; +} + +void ShareLinkWidget::setupUiOptions(){ + connect(_linkShare.data(), &LinkShare::expireDateSet, this, &ShareLinkWidget::slotExpireDateSet); + connect(_linkShare.data(), &LinkShare::passwordSet, this, &ShareLinkWidget::slotPasswordSet); + connect(_linkShare.data(), &LinkShare::passwordSetError, this, &ShareLinkWidget::slotPasswordSetError); + + // Prepare permissions check and create group action + const QDate expireDate = _linkShare.data()->getExpireDate().isValid()? _linkShare.data()->getExpireDate() : QDate(); + const SharePermissions perm = _linkShare.data()->getPermissions(); + bool checked = false; + QActionGroup *permissionsGroup = new QActionGroup(this); + + // Prepare sharing menu + _linkContextMenu = new QMenu(this); + + // radio button style + permissionsGroup->setExclusive(true); + + if(_isFile){ + checked = perm & (SharePermissionRead & SharePermissionUpdate); + _allowEditingLinkAction = _linkContextMenu->addAction(tr("Allow Editing")); + _allowEditingLinkAction->setCheckable(true); + _allowEditingLinkAction->setChecked(checked); + + } else { + checked = perm & SharePermissionRead; + _readOnlyLinkAction = permissionsGroup->addAction(tr("Read only")); + _readOnlyLinkAction->setCheckable(true); + _readOnlyLinkAction->setChecked(checked); + + checked = perm & (SharePermissionRead & + SharePermissionCreate & + SharePermissionUpdate & + SharePermissionDelete); + _allowUploadEditingLinkAction = permissionsGroup->addAction(tr("Allow Upload && Editing")); + _allowUploadEditingLinkAction->setCheckable(true); + _allowUploadEditingLinkAction->setChecked(checked); + + checked = perm & SharePermissionCreate; + _allowUploadLinkAction = permissionsGroup->addAction(tr("File Drop (Upload Only)")); + _allowUploadLinkAction->setCheckable(true); + _allowUploadLinkAction->setChecked(checked); } - toggleAnimation(false); -} + // Adds permissions actions (radio button style) + if(_isFile){ + _linkContextMenu->addAction(_allowEditingLinkAction); + } else { + _linkContextMenu->addAction(_readOnlyLinkAction); + _linkContextMenu->addAction(_allowUploadEditingLinkAction); + _linkContextMenu->addAction(_allowUploadLinkAction); + } -void ShareLinkWidget::setExpireDate(const QDate &date) -{ - if (_linkShare) { - toggleAnimation(true); - _ui->errorLabel->hide(); - _linkShare->setExpireDate(date); + // Adds action to display password widget (check box) + _passwordProtectLinkAction = _linkContextMenu->addAction(tr("Password Protect")); + _passwordProtectLinkAction->setCheckable(true); + + if(_linkShare.data()->isPasswordSet()){ + _passwordProtectLinkAction->setChecked(true); + _ui->lineEdit_password->setPlaceholderText("********"); + showPasswordOptions(true); } -} -void ShareLinkWidget::slotExpireDateSet() -{ - toggleAnimation(false); + // If password is enforced then don't allow users to disable it + if (_account->capabilities().sharePublicLinkEnforcePassword()) { + _passwordProtectLinkAction->setChecked(true); + _passwordProtectLinkAction->setEnabled(false); + _passwordRequired = true; + } + + // Adds action to display expiration date widget (check box) + _expirationDateLinkAction = _linkContextMenu->addAction(tr("Expiration Date")); + _expirationDateLinkAction->setCheckable(true); + if(!expireDate.isNull()){ + _ui->calendar->setDate(expireDate); + _expirationDateLinkAction->setChecked(true); + showExpireDateOptions(true); + } + + // If expiredate is enforced do not allow disable and set max days + if (_account->capabilities().sharePublicLinkEnforceExpireDate()) { + _ui->calendar->setMaximumDate(QDate::currentDate().addDays( + _account->capabilities().sharePublicLinkExpireDateDays())); + _expirationDateLinkAction->setChecked(true); + _expirationDateLinkAction->setEnabled(false); + _expiryRequired = true; + } + + // Adds action to unshare widget (check box) + _unshareLinkAction = _linkContextMenu->addAction(QIcon(":/client/resources/delete.png"), + tr("Unshare")); + + _linkContextMenu->addSeparator(); + + _addAnotherLinkAction = _linkContextMenu->addAction(QIcon(":/client/resources/add.png"), + tr("Add another link")); + + _ui->enableShareLink->setIcon(QIcon(":/client/resources/copy.svg")); + disconnect(_ui->enableShareLink, &QPushButton::clicked, this, &ShareLinkWidget::slotCreateShareLink); + connect(_ui->enableShareLink, &QPushButton::clicked, this, &ShareLinkWidget::slotCopyLinkShare); + + connect(_linkContextMenu, &QMenu::triggered, + this, &ShareLinkWidget::slotLinkContextMenuActionTriggered); + + _ui->shareLinkToolButton->setMenu(_linkContextMenu); + _ui->shareLinkToolButton->setEnabled(true); + _ui->enableShareLink->setEnabled(true); + _ui->enableShareLink->setChecked(true); + + // show sharing options + _ui->shareLinkToolButton->show(); + + //TO DO + //startAnimation(0, height()); } -void ShareLinkWidget::slotExpireDateChanged(const QDate &date) -{ - setExpireDate(date); +void ShareLinkWidget::slotCopyLinkShare(bool clicked){ + Q_UNUSED(clicked); + + QApplication::clipboard()->setText(_linkShare->getLink().toString()); } -void ShareLinkWidget::slotSetExpireDate() +void ShareLinkWidget::slotExpireDateSet() { - slotExpireDateChanged(_ui->calendar->date()); + slotToggleAnimation(false); } -void ShareLinkWidget::slotCreatePassword() +void ShareLinkWidget::slotSetExpireDate() { - if (!_manager) { + if(!_linkShare){ return; } - toggleAnimation(true); - - if (!_linkShare) { - // If share creation requires a password, we'll be in this case - if (_ui->lineEdit_password->text().isEmpty()) { - _ui->lineEdit_password->setFocus(); - return; - } - - _manager->createLinkShare(_sharePath, QString(), _ui->lineEdit_password->text()); - } else { - setPassword(_ui->lineEdit_password->text()); - } + slotToggleAnimation(true); + _ui->errorLabel->hide(); + _linkShare->setExpireDate(_ui->calendar->date()); } -void ShareLinkWidget::slotCreateOrDeleteShareLink(bool checked) +void ShareLinkWidget::slotCreatePassword() { - if (!_manager) { - qCWarning(lcSharing) << "No share manager set."; + if (!_linkShare) { return; } - toggleAnimation(true); - - if(checked){ - _manager->createLinkShare(_sharePath, QString(), QString()); - } else { - if (!_linkShare) { - qCWarning(lcSharing) << "No public link set."; - return; - } - confirmAndDeleteShare(); - } + slotToggleAnimation(true); + _ui->errorLabel->hide(); + _linkShare->setPassword(_ui->lineEdit_password->text()); } -void ShareLinkWidget::setPassword(const QString &password) +void ShareLinkWidget::slotCreateShareLink(bool clicked) { - if (_linkShare) { - toggleAnimation(true); - - _ui->errorLabel->hide(); - _linkShare->setPassword(password); - } + slotToggleAnimation(true); + emit createLinkShare(); } void ShareLinkWidget::slotPasswordSet() { - if (!_linkShare) - return; - _ui->lineEdit_password->setText(QString()); if (_linkShare->isPasswordSet()) { _ui->lineEdit_password->setPlaceholderText("********"); @@ -364,39 +291,55 @@ void ShareLinkWidget::slotPasswordSet() _ui->lineEdit_password->setPlaceholderText(QString()); } - toggleAnimation(false); + slotToggleAnimation(false); +} + +void ShareLinkWidget::startAnimation(const int start, const int end){ + + QPropertyAnimation *animation = new QPropertyAnimation(this, "maximumHeight", this); + + animation->setDuration(500); + animation->setStartValue(start); + animation->setEndValue(end); - /* - * When setting/deleting a password from a share the old share is - * deleted and a new one is created. So we need to refetch the shares - * at this point. - * - * NOTE: I don't see this happening with oC > 10 - */ - getShares(); + connect(animation, &QAbstractAnimation::finished, this, &ShareLinkWidget::slotAnimationFinished); + if(end < start) // that is to remove the widget, not to show it + connect(animation, &QAbstractAnimation::finished, this, &ShareLinkWidget::slotDeleteAnimationFinished); + connect(animation, &QVariantAnimation::valueChanged, this, &ShareLinkWidget::resizeRequested); + + animation->start(); } void ShareLinkWidget::slotDeleteShareFetched() { - toggleAnimation(true); + slotToggleAnimation(false); + + // TODO + //startAnimation(height(), 0); + _linkShare.clear(); - _ui->enableShareLink->setChecked(false); - _ui->shareLinkToolButton->setEnabled(false); - _ui->shareLinkToolButton->hide(); togglePasswordOptions(false); toggleExpireDateOptions(false); - getShares(); + emit deleteLinkShare(); } -void ShareLinkWidget::slotCreateShareFetched() +void ShareLinkWidget::slotAnimationFinished() { - toggleAnimation(true); - getShares(); + emit resizeRequested(); + deleteLater(); +} + +void ShareLinkWidget::slotDeleteAnimationFinished() +{ + // There is a painting bug where a small line of this widget isn't + // properly cleared. This explicit repaint() call makes sure any trace of + // the share widget is removed once it's destroyed. #4189 + connect(this, SIGNAL(destroyed(QObject *)), parentWidget(), SLOT(repaint())); } void ShareLinkWidget::slotCreateShareRequiresPassword(const QString &message) { - toggleAnimation(true); + slotToggleAnimation(true); showPasswordOptions(true); if (!message.isEmpty()) { @@ -468,8 +411,10 @@ void ShareLinkWidget::confirmAndDeleteShare() connect(messageBox, &QMessageBox::finished, this, [messageBox, yesButton, this]() { - if (messageBox->clickedButton() == yesButton) + if (messageBox->clickedButton() == yesButton) { + this->slotToggleAnimation(true); this->_linkShare->deleteShare(); + } }); messageBox->open(); } @@ -495,8 +440,8 @@ void ShareLinkWidget::slotLinkContextMenuActionTriggered(QAction *action) bool state = action->isChecked(); SharePermissions perm = SharePermissionRead; - if (action == _copyLinkAction) { - QApplication::clipboard()->setText(_linkShare->getLink().toString()); + if(action == _addAnotherLinkAction){ + emit createLinkShare(); } else if (action == _readOnlyLinkAction && state) { _linkShare->setPermissions(perm); @@ -520,13 +465,13 @@ void ShareLinkWidget::slotLinkContextMenuActionTriggered(QAction *action) toggleExpireDateOptions(state); } else if (action == _unshareLinkAction) { - slotCreateOrDeleteShareLink(state); + confirmAndDeleteShare(); } } void ShareLinkWidget::slotServerError(int code, const QString &message) { - toggleAnimation(false); + slotToggleAnimation(false); qCWarning(lcSharing) << "Error from server" << code << message; displayError(message); |