Add auto-save delay per database (#9100)

Add a new propery autosaveDelay in Metadata of the db.
The property is saved in customData to not affect database structure as this setting is unique to keepasxc.
The propery sets delay to wait since last modification before saving.

Co-authored-by: jNullj <jNullj@users.noreply.github.com>
This commit is contained in:
Jonathan White 2024-04-27 23:50:40 -04:00
parent 775efc65ed
commit bab48b42f7
No known key found for this signature in database
GPG key ID: 440FC65F2E0C6E01
10 changed files with 294 additions and 6 deletions

View file

@ -215,6 +215,10 @@ DatabaseWidget::DatabaseWidget(QSharedPointer<Database> db, QWidget* parent)
m_blockAutoSave = false;
m_autosaveTimer = new QTimer(this);
m_autosaveTimer->setSingleShot(true);
connect(m_autosaveTimer, SIGNAL(timeout()), this, SLOT(onAutosaveDelayTimeout()));
m_searchLimitGroup = config()->get(Config::SearchLimitGroup).toBool();
#ifdef WITH_XC_KEESHARE
@ -1561,13 +1565,42 @@ void DatabaseWidget::onGroupChanged()
void DatabaseWidget::onDatabaseModified()
{
if (!m_blockAutoSave && config()->get(Config::AutoSaveAfterEveryChange).toBool()) {
refreshSearch();
int autosaveDelayMs = m_db->metadata()->autosaveDelayMin() * 60 * 1000; // min to msec for QTimer
bool autosaveAfterEveryChangeConfig = config()->get(Config::AutoSaveAfterEveryChange).toBool();
if (autosaveDelayMs > 0 && autosaveAfterEveryChangeConfig) {
// reset delay when modified
m_autosaveTimer->start(autosaveDelayMs);
return;
}
if (!m_blockAutoSave && autosaveAfterEveryChangeConfig) {
save();
} else {
// Only block once, then reset
m_blockAutoSave = false;
}
refreshSearch();
}
void DatabaseWidget::onAutosaveDelayTimeout()
{
const bool isAutosaveDelayEnabled = m_db->metadata()->autosaveDelayMin() > 0;
const bool autosaveAfterEveryChangeConfig = config()->get(Config::AutoSaveAfterEveryChange).toBool();
if (!(isAutosaveDelayEnabled && autosaveAfterEveryChangeConfig)) {
// User might disable the delay/autosave while the timer is running
return;
}
if (!m_blockAutoSave) {
save();
} else {
// Only block once, then reset
m_blockAutoSave = false;
}
}
void DatabaseWidget::triggerAutosaveTimer()
{
m_autosaveTimer->stop();
QMetaObject::invokeMethod(m_autosaveTimer, "timeout");
}
void DatabaseWidget::onDatabaseNonDataChanged()
@ -2037,6 +2070,7 @@ bool DatabaseWidget::save()
if (performSave(errorMessage)) {
m_saveAttempts = 0;
m_blockAutoSave = false;
m_autosaveTimer->stop(); // stop autosave delay to avoid triggering another save
return true;
}