From e4b6289c0b2e4456ac1f7856668e6374e549934c Mon Sep 17 00:00:00 2001 From: Florian Geyer Date: Sat, 12 May 2012 00:01:58 +0200 Subject: [PATCH] Prevent deletion of custom icon if it is still used by other entries. Refs #22 --- src/core/Group.cpp | 2 +- src/core/Group.h | 2 +- src/core/Metadata.cpp | 5 +++++ src/core/Metadata.h | 1 + src/gui/EditEntryWidget.cpp | 24 +++++++++++++++++++++--- 5 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/core/Group.cpp b/src/core/Group.cpp index 2a5815c7e..460480250 100644 --- a/src/core/Group.cpp +++ b/src/core/Group.cpp @@ -359,7 +359,7 @@ const QList& Group::entries() const return m_entries; } -QList Group::entriesRecursive(bool includeHistoryItems) +QList Group::entriesRecursive(bool includeHistoryItems) const { QList entryList; diff --git a/src/core/Group.h b/src/core/Group.h index a725a672f..593426eda 100644 --- a/src/core/Group.h +++ b/src/core/Group.h @@ -75,7 +75,7 @@ public: const QList& children() const; QList entries(); const QList& entries() const; - QList entriesRecursive(bool includeHistoryItems = false); + QList entriesRecursive(bool includeHistoryItems = false) const; Q_SIGNALS: diff --git a/src/core/Metadata.cpp b/src/core/Metadata.cpp index a5a35b2d4..3a500b837 100644 --- a/src/core/Metadata.cpp +++ b/src/core/Metadata.cpp @@ -415,3 +415,8 @@ void Metadata::removeCustomField(const QString& key) m_customFields.remove(key); Q_EMIT modified(); } + +const Database* Metadata::database() const +{ + return m_parent; +} diff --git a/src/core/Metadata.h b/src/core/Metadata.h index 62f01b15f..6bb85f953 100644 --- a/src/core/Metadata.h +++ b/src/core/Metadata.h @@ -68,6 +68,7 @@ public: int historyMaxItems() const; int historyMaxSize() const; QHash customFields() const; + const Database* database() const; static const int defaultHistoryMaxItems; static const int defaultHistoryMaxSize; diff --git a/src/gui/EditEntryWidget.cpp b/src/gui/EditEntryWidget.cpp index 8fbb98d90..79f6cdb36 100644 --- a/src/gui/EditEntryWidget.cpp +++ b/src/gui/EditEntryWidget.cpp @@ -450,9 +450,27 @@ void EditEntryWidget::removeCustomIcon() if (m_metadata) { QModelIndex index = m_iconsUi->customIconsView->currentIndex(); if (index.isValid()) { - // TODO: check if the icon is used in history items or other entries - m_metadata->removeCustomIcon(m_customIconModel->uuidFromIndex(index)); - m_customIconModel->setIcons(m_metadata->customIcons()); + QList allEntries = m_metadata->database()->rootGroup()->entriesRecursive(true); + Uuid uuid = m_customIconModel->uuidFromIndex(index); + + int iconUsedCount = 0; + QListIterator i(allEntries); + while (i.hasNext()) { + Entry* entry = i.next(); + if (uuid == entry->iconUuid() && entry != m_entry) { + iconUsedCount++; + } + } + + if (iconUsedCount == 0) { + m_metadata->removeCustomIcon(uuid); + m_customIconModel->setIcons(m_metadata->customIcons()); + } + else { + QMessageBox::information(this, tr("Icon still used!"), + tr("Can't delete icon. Still used by %1 entries.") + .arg(iconUsedCount)); + } } } }