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

View File

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

View File

@ -19,7 +19,7 @@
#endif #endif
CookieJar::CookieJar(QObject* parent) 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) #if defined(USE_WEBENGINE)
auto* web_factory = qobject_cast<WebFactory*>(parent); auto* web_factory = qobject_cast<WebFactory*>(parent);