diff --git a/src/core/Entry.cpp b/src/core/Entry.cpp index c6d897e2..178c5aae 100644 --- a/src/core/Entry.cpp +++ b/src/core/Entry.cpp @@ -404,6 +404,13 @@ void Entry::setGroup(Group* group) m_group->removeEntry(this); if (m_group->database() && m_group->database() != group->database()) { m_group->database()->addDeletedObject(m_uuid); + + // copy custom icon to the new database + if (!iconUuid().isNull() && group->database() + && m_group->database()->metadata()->containsCustomIcon(iconUuid()) + && !group->database()->metadata()->containsCustomIcon(iconUuid())) { + group->database()->metadata()->addCustomIcon(iconUuid(), icon()); + } } } diff --git a/src/core/Group.cpp b/src/core/Group.cpp index c4f851fd..e4a19b31 100644 --- a/src/core/Group.cpp +++ b/src/core/Group.cpp @@ -276,6 +276,13 @@ void Group::setParent(Group* parent, int index) m_parent = parent; if (m_db) { recCreateDelObjects(); + + // copy custom icon to the new database + if (!iconUuid().isNull() && parent->m_db + && m_db->metadata()->containsCustomIcon(iconUuid()) + && !parent->m_db->metadata()->containsCustomIcon(iconUuid())) { + parent->m_db->metadata()->addCustomIcon(iconUuid(), icon()); + } } if (m_db != parent->m_db) { recSetDatabase(parent->m_db); diff --git a/src/core/Metadata.cpp b/src/core/Metadata.cpp index a759366d..266cf554 100644 --- a/src/core/Metadata.cpp +++ b/src/core/Metadata.cpp @@ -161,6 +161,11 @@ QImage Metadata::customIcon(const Uuid& uuid) const return m_customIcons.value(uuid); } +bool Metadata::containsCustomIcon(const Uuid& uuid) const +{ + return m_customIcons.contains(uuid); +} + QHash Metadata::customIcons() const { return m_customIcons; diff --git a/src/core/Metadata.h b/src/core/Metadata.h index 78fadc72..f00e07ab 100644 --- a/src/core/Metadata.h +++ b/src/core/Metadata.h @@ -52,6 +52,7 @@ public: bool protectNotes() const; // bool autoEnableVisualHiding() const; QImage customIcon(const Uuid& uuid) const; + bool containsCustomIcon(const Uuid& uuid) const; QHash customIcons() const; bool recycleBinEnabled() const; Group* recycleBin(); diff --git a/tests/TestGroup.cpp b/tests/TestGroup.cpp index 017ba3d9..79d974c5 100644 --- a/tests/TestGroup.cpp +++ b/tests/TestGroup.cpp @@ -24,6 +24,7 @@ #include "tests.h" #include "core/Database.h" #include "core/Group.h" +#include "core/Metadata.h" #include "crypto/Crypto.h" void TestGroup::initTestCase() @@ -293,4 +294,44 @@ void TestGroup::testDeleteSignals() delete db2; } +void TestGroup::testCopyCustomIcon() +{ + Database* dbSource = new Database(); + + Uuid groupIconUuid = Uuid::random(); + QImage groupIcon(16, 16, QImage::Format_RGB32); + groupIcon.setPixel(0, 0, qRgb(255, 0, 0)); + dbSource->metadata()->addCustomIcon(groupIconUuid, groupIcon); + + Uuid entryIconUuid = Uuid::random(); + QImage entryIcon(16, 16, QImage::Format_RGB32); + entryIcon.setPixel(0, 0, qRgb(255, 0, 0)); + dbSource->metadata()->addCustomIcon(entryIconUuid, entryIcon); + + Group* group = new Group(); + group->setParent(dbSource->rootGroup()); + group->setIcon(groupIconUuid); + QCOMPARE(group->icon(), groupIcon); + + Entry* entry = new Entry(); + entry->setGroup(dbSource->rootGroup()); + entry->setIcon(entryIconUuid); + QCOMPARE(entry->icon(), entryIcon); + + Database* dbTarget = new Database(); + + group->setParent(dbTarget->rootGroup()); + QVERIFY(dbTarget->metadata()->containsCustomIcon(groupIconUuid)); + QCOMPARE(dbTarget->metadata()->customIcon(groupIconUuid), groupIcon); + QCOMPARE(group->icon(), groupIcon); + + entry->setGroup(dbTarget->rootGroup()); + QVERIFY(dbTarget->metadata()->containsCustomIcon(entryIconUuid)); + QCOMPARE(dbTarget->metadata()->customIcon(entryIconUuid), entryIcon); + QCOMPARE(entry->icon(), entryIcon); + + delete dbSource; + delete dbTarget; +} + KEEPASSX_QTEST_CORE_MAIN(TestGroup) diff --git a/tests/TestGroup.h b/tests/TestGroup.h index 7edd3577..5c2cd2c4 100644 --- a/tests/TestGroup.h +++ b/tests/TestGroup.h @@ -30,6 +30,7 @@ private Q_SLOTS: void testSignals(); void testEntries(); void testDeleteSignals(); + void testCopyCustomIcon(); }; #endif // KEEPASSX_TESTGROUP_H