From a876b3b72fb5fca454f5025aec7e4fac376ab6ab Mon Sep 17 00:00:00 2001 From: Mark Hakansson Date: Sat, 12 Oct 2019 15:27:35 +0200 Subject: [PATCH] Prompt to delete recycle bin when disabling it Fixes #3365 Add prompt to delete the recycle bin when disabling it. If the user chooses not to delete it, the recycle bin will be suffixed with "(old)" and the icon changed to the default group icon. Also moved recycle bin creation within the database class where it belongs. --- src/core/Database.cpp | 9 ++++++- src/core/Group.cpp | 16 ++++-------- src/core/Group.h | 3 +-- .../DatabaseSettingsWidgetGeneral.cpp | 25 ++++++++++++++++++- tests/TestGroup.cpp | 2 +- 5 files changed, 39 insertions(+), 16 deletions(-) diff --git a/src/core/Database.cpp b/src/core/Database.cpp index 2a0499f5..fed8fb12 100644 --- a/src/core/Database.cpp +++ b/src/core/Database.cpp @@ -702,8 +702,15 @@ void Database::setPublicCustomData(const QVariantMap& customData) void Database::createRecycleBin() { Q_ASSERT(!m_data.isReadOnly); - Group* recycleBin = Group::createRecycleBin(); + + auto recycleBin = new Group(); + recycleBin->setUuid(QUuid::createUuid()); recycleBin->setParent(rootGroup()); + recycleBin->setName(tr("Recycle Bin")); + recycleBin->setIcon(Group::RecycleBinIconNumber); + recycleBin->setSearchingEnabled(Group::Disable); + recycleBin->setAutoTypeEnabled(Group::Disable); + m_metadata->setRecycleBin(recycleBin); } diff --git a/src/core/Group.cpp b/src/core/Group.cpp index adbd5147..7fd403ab 100644 --- a/src/core/Group.cpp +++ b/src/core/Group.cpp @@ -75,17 +75,6 @@ Group::~Group() cleanupParent(); } -Group* Group::createRecycleBin() -{ - Group* recycleBin = new Group(); - recycleBin->setUuid(QUuid::createUuid()); - recycleBin->setName(tr("Recycle Bin")); - recycleBin->setIcon(RecycleBinIconNumber); - recycleBin->setSearchingEnabled(Group::Disable); - recycleBin->setAutoTypeEnabled(Group::Disable); - return recycleBin; -} - template inline bool Group::set(P& property, const V& value) { if (property != value) { @@ -281,6 +270,11 @@ bool Group::isExpired() const return m_data.timeInfo.expires() && m_data.timeInfo.expiryTime() < Clock::currentDateTimeUtc(); } +bool Group::isEmpty() const +{ + return !hasChildren() && m_entries.isEmpty(); +} + CustomData* Group::customData() { return m_customData; diff --git a/src/core/Group.h b/src/core/Group.h index 2df00ef5..2e6da887 100644 --- a/src/core/Group.h +++ b/src/core/Group.h @@ -80,8 +80,6 @@ public: Group(); ~Group(); - static Group* createRecycleBin(); - const QUuid& uuid() const; const QString uuidToHex() const; QString name() const; @@ -103,6 +101,7 @@ public: Entry* lastTopVisibleEntry() const; bool isExpired() const; bool isRecycled() const; + bool isEmpty() const; CustomData* customData(); const CustomData* customData() const; diff --git a/src/gui/dbsettings/DatabaseSettingsWidgetGeneral.cpp b/src/gui/dbsettings/DatabaseSettingsWidgetGeneral.cpp index b96016fd..65d7f52b 100644 --- a/src/gui/dbsettings/DatabaseSettingsWidgetGeneral.cpp +++ b/src/gui/dbsettings/DatabaseSettingsWidgetGeneral.cpp @@ -23,6 +23,7 @@ #include "core/Entry.h" #include "core/Group.h" #include "core/Metadata.h" +#include "gui/MessageBox.h" DatabaseSettingsWidgetGeneral::DatabaseSettingsWidgetGeneral(QWidget* parent) : DatabaseSettingsWidget(parent) @@ -77,9 +78,31 @@ void DatabaseSettingsWidgetGeneral::showEvent(QShowEvent* event) bool DatabaseSettingsWidgetGeneral::save() { + auto* meta = m_db->metadata(); + + if (!m_ui->recycleBinEnabledCheckBox->isChecked() && meta->recycleBinEnabled()) { + auto recycleBin = meta->recycleBin(); + if (recycleBin && !recycleBin->isEmpty()) { + auto result = MessageBox::question(this, + tr("Delete Recycle Bin"), + tr("Do you want to delete the current recycle bin and all its " + "contents?\nThis action is not reversible."), + MessageBox::Delete | MessageBox::No, + MessageBox::No); + + if (result == MessageBox::Delete) { + recycleBin->deleteLater(); + } else { + recycleBin->setName(recycleBin->name().append(tr(" (old)"))); + recycleBin->setIcon(Group::DefaultIconNumber); + } + } + + meta->setRecycleBin(nullptr); + } + m_db->setCompressionAlgorithm(m_ui->compressionCheckbox->isChecked() ? Database::CompressionGZip : Database::CompressionNone); - Metadata* meta = m_db->metadata(); meta->setName(m_ui->dbNameEdit->text()); meta->setDescription(m_ui->dbDescriptionEdit->text()); diff --git a/tests/TestGroup.cpp b/tests/TestGroup.cpp index bd3d3608..ae9c5989 100644 --- a/tests/TestGroup.cpp +++ b/tests/TestGroup.cpp @@ -799,7 +799,7 @@ void TestGroup::testAddEntryWithPath() void TestGroup::testIsRecycled() { Database* db = new Database(); - db->rootGroup()->createRecycleBin(); + db->metadata()->setRecycleBinEnabled(true); Group* group1 = new Group(); group1->setName("group1");