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 <[email protected]>
This commit is contained in:
Jonathan WhiteandjNullj committed 2024-04-28 23:41:45 -04:00
1 parent 775efc65ed
commit bab48b42f7
10 files changed
+294 -6

No files matched your search

+36 -2
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;
}