fix autosaver

This commit is contained in:
Martin Rotter 2023-01-18 10:02:43 +01:00
parent b9cbc1ad5b
commit bf208fc9dd
3 changed files with 12 additions and 22 deletions

View File

@ -9,8 +9,11 @@
#include "definitions/definitions.h"
AutoSaver::AutoSaver(QObject* parent, const QString& saving_slot, int max_wait_secs, int periodic_save_secs)
: QObject(parent), m_savingSlot(saving_slot), m_maxWaitSecs(max_wait_secs), m_periodicSaveSecs(periodic_save_secs) {
: QObject(parent), m_savingSlot(saving_slot), m_maxWaitMsecs(max_wait_secs * 1000),
m_periodicSaveMsecs(periodic_save_secs * 1000) {
Q_ASSERT(parent);
connect(&m_timer, &QTimer::timeout, this, &AutoSaver::saveIfNeccessary);
}
AutoSaver::~AutoSaver() {
@ -28,20 +31,11 @@ void AutoSaver::changeOccurred() {
m_firstChange.start();
}
if (m_firstChange.elapsed() > m_maxWaitSecs) {
if (m_firstChange.elapsed() > m_maxWaitMsecs) {
saveIfNeccessary();
}
else {
m_timer.start(m_periodicSaveSecs, this);
}
}
void AutoSaver::timerEvent(QTimerEvent* event) {
if (event->timerId() == m_timer.timerId()) {
saveIfNeccessary();
}
else {
QObject::timerEvent(event);
m_timer.start(m_periodicSaveMsecs);
}
}

View File

@ -7,8 +7,8 @@
#include "definitions/definitions.h"
#include <QBasicTimer>
#include <QElapsedTimer>
#include <QTimer>
#define AUTOSAVE_IN (1000 * 3) // In seconds.
#define MAX_WAIT (1000 * 15) // In seconds.
@ -23,19 +23,15 @@ class AutoSaver : public QObject {
int periodic_save_secs = AUTOSAVE_IN);
virtual ~AutoSaver();
void saveIfNeccessary();
public slots:
void changeOccurred();
protected:
void timerEvent(QTimerEvent* event);
void saveIfNeccessary();
private:
QBasicTimer m_timer;
QTimer m_timer;
QElapsedTimer m_firstChange;
int m_maxWaitSecs;
int m_periodicSaveSecs;
int m_maxWaitMsecs;
int m_periodicSaveMsecs;
QString m_savingSlot;
};

View File

@ -19,7 +19,7 @@
#endif
CookieJar::CookieJar(QObject* parent)
: QNetworkCookieJar(parent), m_saver(AutoSaver(this, QSL("saveCookies"), 25, 45)) {
: QNetworkCookieJar(parent), m_saver(AutoSaver(this, QSL("saveCookies"), 30, 45)) {
#if defined(USE_WEBENGINE)
auto* web_factory = qobject_cast<WebFactory*>(parent);