Skip to content

Commit

Permalink
Implement autosaveDelay in DatabaseWidget
Browse files Browse the repository at this point in the history
Implement a autosave delay functionality to the database widget.
Added timer to track the delay and trigger a save on timeout using the new onAutosaveDelayTimeout function.
Changed the onDatabaseModified to test for the new autosaveDelay property and delay the save if needed or extend the delay on new modifications.
After menual save stop the timer to avoid saving when not needed edited in DatabaseWidget::save().

Part of issue keepassxreboot#8553
  • Loading branch information
jNullj committed May 20, 2023
1 parent 3a3a21d commit 279f7f0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/gui/DatabaseWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,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
Expand Down Expand Up @@ -1528,13 +1532,30 @@ 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 (m_autosaveTimer->isActive() || (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;
}
}

void DatabaseWidget::onAutosaveDelayTimeout()
{
if (!m_blockAutoSave) {
save();
} else {
// Only block once, then reset
m_blockAutoSave = false;
}
refreshSearch();
}

QString DatabaseWidget::getCurrentSearch()
Expand Down Expand Up @@ -1986,6 +2007,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;
}

Expand Down
4 changes: 4 additions & 0 deletions src/gui/DatabaseWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ private slots:
void onEntryChanged(Entry* entry);
void onGroupChanged();
void onDatabaseModified();
void onAutosaveDelayTimeout();
void connectDatabaseSignals();
void loadDatabase(bool accepted);
void unlockDatabase(bool accepted);
Expand Down Expand Up @@ -309,6 +310,9 @@ private slots:
// Autoreload
bool m_blockAutoSave;

// Autosave delay
QPointer<QTimer> m_autosaveTimer;

// Auto-Type related
QString m_searchStringForAutoType;
};
Expand Down

0 comments on commit 279f7f0

Please sign in to comment.