Add a method to get databases by uuid.

Each database gets a random uuid on construction which is not saved by
KeePass2XmlWriter and only valid until the database object is deleted.
This commit is contained in:
Felix Geyer 2012-04-25 00:12:23 +02:00
parent 0d20955920
commit d5cd0dcd14
2 changed files with 31 additions and 0 deletions

View File

@ -25,19 +25,29 @@
#include "crypto/Random.h"
#include "format/KeePass2.h"
QHash<Uuid, Database*> Database::m_uuidMap;
Database::Database()
: m_metadata(new Metadata(this))
, m_cipher(KeePass2::CIPHER_AES)
, m_compressionAlgo(CompressionGZip)
, m_transformRounds(50000)
, m_hasKey(false)
, m_uuid(Uuid::random())
{
setRootGroup(new Group());
rootGroup()->setUuid(Uuid::random());
m_uuidMap.insert(m_uuid, this);
connect(m_metadata, SIGNAL(modified()), this, SIGNAL(modified()));
}
Database::~Database()
{
m_uuidMap.remove(m_uuid);
}
Group* Database::rootGroup()
{
return m_rootGroup;
@ -240,3 +250,13 @@ void Database::recycleGroup(Group* group)
delete group;
}
}
Uuid Database::uuid()
{
return m_uuid;
}
Database* Database::databaseByUuid(const Uuid& uuid)
{
return m_uuidMap.value(uuid, 0);
}

View File

@ -49,6 +49,7 @@ public:
static const quint32 CompressionAlgorithmMax = CompressionGZip;
Database();
~Database();
Group* rootGroup();
const Group* rootGroup() const;
@ -89,6 +90,13 @@ public:
void recycleEntry(Entry* entry);
void recycleGroup(Group* group);
/**
* Returns a unique id that is only valid as long as the Database exists.
*/
Uuid uuid();
static Database* databaseByUuid(const Uuid& uuid);
Q_SIGNALS:
void groupDataChanged(Group* group);
void groupAboutToAdd(Group* group, int index);
@ -117,6 +125,9 @@ private:
CompositeKey m_key;
bool m_hasKey;
Uuid m_uuid;
static QHash<Uuid, Database*> m_uuidMap;
};
#endif // KEEPASSX_DATABASE_H