Improve auto save handling.
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
#include "Database.h"
|
||||
|
||||
#include <QtCore/QFile>
|
||||
#include <QtCore/QTimer>
|
||||
#include <QtCore/QXmlStreamReader>
|
||||
|
||||
#include "core/Group.h"
|
||||
@@ -30,19 +31,24 @@ QHash<Uuid, Database*> Database::m_uuidMap;
|
||||
|
||||
Database::Database()
|
||||
: m_metadata(new Metadata(this))
|
||||
, m_timer(new QTimer(this))
|
||||
, m_cipher(KeePass2::CIPHER_AES)
|
||||
, m_compressionAlgo(CompressionGZip)
|
||||
, m_transformRounds(50000)
|
||||
, m_hasKey(false)
|
||||
, m_emitModified(false)
|
||||
, m_uuid(Uuid::random())
|
||||
{
|
||||
setRootGroup(new Group());
|
||||
rootGroup()->setUuid(Uuid::random());
|
||||
m_timer->setSingleShot(true);
|
||||
|
||||
m_uuidMap.insert(m_uuid, this);
|
||||
|
||||
connect(m_metadata, SIGNAL(modified()), this, SIGNAL(modified()));
|
||||
connect(m_metadata, SIGNAL(modified()), this, SIGNAL(modifiedImmediate()));
|
||||
connect(m_metadata, SIGNAL(nameTextChanged()), this, SIGNAL(nameTextChanged()));
|
||||
connect(this, SIGNAL(modifiedImmediate()), this, SLOT(startModifiedTimer()));
|
||||
connect(m_timer, SIGNAL(timeout()), SIGNAL(modified()));
|
||||
}
|
||||
|
||||
Database::~Database()
|
||||
@@ -195,7 +201,7 @@ void Database::setKey(const CompositeKey& key, const QByteArray& transformSeed,
|
||||
if (updateChangedTime) {
|
||||
m_metadata->setMasterKeyChanged(Tools::currentDateTimeUtc());
|
||||
}
|
||||
Q_EMIT modified();
|
||||
Q_EMIT modifiedImmediate();
|
||||
}
|
||||
|
||||
void Database::setKey(const CompositeKey& key)
|
||||
@@ -209,7 +215,7 @@ void Database::updateKey(quint64 rounds)
|
||||
m_transformRounds = rounds;
|
||||
m_transformedMasterKey = m_key.transform(m_transformSeed, transformRounds());
|
||||
m_metadata->setMasterKeyChanged(Tools::currentDateTimeUtc());
|
||||
Q_EMIT modified();
|
||||
Q_EMIT modifiedImmediate();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -253,7 +259,16 @@ void Database::recycleGroup(Group* group)
|
||||
}
|
||||
else {
|
||||
delete group;
|
||||
}
|
||||
}
|
||||
|
||||
void Database::setEmitModified(bool value)
|
||||
{
|
||||
if (m_emitModified && !value) {
|
||||
m_timer->stop();
|
||||
}
|
||||
|
||||
m_emitModified = value;
|
||||
}
|
||||
|
||||
Uuid Database::uuid()
|
||||
@@ -265,3 +280,15 @@ Database* Database::databaseByUuid(const Uuid& uuid)
|
||||
{
|
||||
return m_uuidMap.value(uuid, 0);
|
||||
}
|
||||
|
||||
void Database::startModifiedTimer()
|
||||
{
|
||||
if (!m_emitModified) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_timer->isActive()) {
|
||||
m_timer->stop();
|
||||
}
|
||||
m_timer->start(150);
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
class Entry;
|
||||
class Group;
|
||||
class Metadata;
|
||||
class QTimer;
|
||||
|
||||
struct DeletedObject
|
||||
{
|
||||
@@ -89,6 +90,7 @@ public:
|
||||
bool hasKey();
|
||||
void recycleEntry(Entry* entry);
|
||||
void recycleGroup(Group* group);
|
||||
void setEmitModified(bool value);
|
||||
|
||||
/**
|
||||
* Returns a unique id that is only valid as long as the Database exists.
|
||||
@@ -107,6 +109,10 @@ Q_SIGNALS:
|
||||
void groupMoved();
|
||||
void nameTextChanged();
|
||||
void modified();
|
||||
void modifiedImmediate();
|
||||
|
||||
private Q_SLOTS:
|
||||
void startModifiedTimer();
|
||||
|
||||
private:
|
||||
Entry* recFindEntry(const Uuid& uuid, Group* group);
|
||||
@@ -117,6 +123,7 @@ private:
|
||||
Metadata* const m_metadata;
|
||||
Group* m_rootGroup;
|
||||
QList<DeletedObject> m_deletedObjects;
|
||||
QTimer* m_timer;
|
||||
|
||||
Uuid m_cipher;
|
||||
CompressionAlgorithm m_compressionAlgo;
|
||||
@@ -126,6 +133,7 @@ private:
|
||||
|
||||
CompositeKey m_key;
|
||||
bool m_hasKey;
|
||||
bool m_emitModified;
|
||||
|
||||
Uuid m_uuid;
|
||||
static QHash<Uuid, Database*> m_uuidMap;
|
||||
|
||||
@@ -434,7 +434,7 @@ void Group::addEntry(Entry* entry)
|
||||
m_entries << entry;
|
||||
connect(entry, SIGNAL(dataChanged(Entry*)), SIGNAL(entryDataChanged(Entry*)));
|
||||
if (m_db) {
|
||||
connect(entry, SIGNAL(modified()), m_db, SIGNAL(modified()));
|
||||
connect(entry, SIGNAL(modified()), m_db, SIGNAL(modifiedImmediate()));
|
||||
}
|
||||
|
||||
Q_EMIT modified();
|
||||
@@ -474,7 +474,7 @@ void Group::recSetDatabase(Database* db)
|
||||
entry->disconnect(m_db);
|
||||
}
|
||||
if (db) {
|
||||
connect(entry, SIGNAL(modified()), db, SIGNAL(modified()));
|
||||
connect(entry, SIGNAL(modified()), db, SIGNAL(modifiedImmediate()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -486,7 +486,7 @@ void Group::recSetDatabase(Database* db)
|
||||
connect(this, SIGNAL(added()), db, SIGNAL(groupAdded()));
|
||||
connect(this, SIGNAL(aboutToMove(Group*,Group*,int)), db, SIGNAL(groupAboutToMove(Group*,Group*,int)));
|
||||
connect(this, SIGNAL(moved()), db, SIGNAL(groupMoved()));
|
||||
connect(this, SIGNAL(modified()), db, SIGNAL(modified()));
|
||||
connect(this, SIGNAL(modified()), db, SIGNAL(modifiedImmediate()));
|
||||
}
|
||||
|
||||
m_db = db;
|
||||
|
||||
Reference in New Issue
Block a user