cookie save with auto-saver

This commit is contained in:
Martin Rotter 2023-01-13 11:44:12 +01:00
parent 6e709fceef
commit 08b4d6c6b1
4 changed files with 38 additions and 16 deletions

View File

@ -8,10 +8,8 @@
#include "definitions/definitions.h" #include "definitions/definitions.h"
#define AUTOSAVE_IN (1000 * 3) // seconds AutoSaver::AutoSaver(QObject* parent, const QString& saving_slot, int max_wait_secs, int periodic_save_secs)
#define MAXWAIT (1000 * 15) // seconds : QObject(parent), m_savingSlot(saving_slot), m_maxWaitSecs(max_wait_secs), m_periodicSaveSecs(periodic_save_secs) {
AutoSaver::AutoSaver(QObject* parent) : QObject(parent) {
Q_ASSERT(parent); Q_ASSERT(parent);
} }
@ -30,11 +28,11 @@ void AutoSaver::changeOccurred() {
m_firstChange.start(); m_firstChange.start();
} }
if (m_firstChange.elapsed() > MAXWAIT) { if (m_firstChange.elapsed() > m_maxWaitSecs) {
saveIfNeccessary(); saveIfNeccessary();
} }
else { else {
m_timer.start(AUTOSAVE_IN, this); m_timer.start(m_periodicSaveSecs, this);
} }
} }
@ -52,8 +50,12 @@ void AutoSaver::saveIfNeccessary() {
m_timer.stop(); m_timer.stop();
m_firstChange.invalidate(); m_firstChange.invalidate();
if (!QMetaObject::invokeMethod(parent(), "save", Qt::DirectConnection)) { if (!QMetaObject::invokeMethod(parent(), qPrintable(m_savingSlot), Qt::ConnectionType::DirectConnection)) {
qCriticalNN << LOGSEC_CORE << ("AutoSaver error invoking slot save() on parent."); qCriticalNN << LOGSEC_CORE << "AutoSaver error invoking saving slot on parent.";
}
else {
qDebugNN << LOGSEC_CORE << "Saved data with auto-saver for"
<< QUOTE_W_SPACE(parent()->metaObject()->className()) "and method" << QUOTE_W_SPACE_DOT(m_savingSlot);
} }
} }
} }

View File

@ -3,15 +3,24 @@
#ifndef AUTOSAVER_H #ifndef AUTOSAVER_H
#define AUTOSAVER_H #define AUTOSAVER_H
#include <QBasicTimer>
#include <QElapsedTimer>
#include <QObject> #include <QObject>
#include "definitions/definitions.h"
#include <QBasicTimer>
#include <QElapsedTimer>
#define AUTOSAVE_IN (1000 * 3) // In seconds.
#define MAX_WAIT (1000 * 15) // In seconds.
class AutoSaver : public QObject { class AutoSaver : public QObject {
Q_OBJECT Q_OBJECT
public: public:
explicit AutoSaver(QObject* parent); explicit AutoSaver(QObject* parent,
const QString& saving_slot = QSL("save"),
int max_wait_secs = MAX_WAIT,
int periodic_save_secs = AUTOSAVE_IN);
virtual ~AutoSaver(); virtual ~AutoSaver();
void saveIfNeccessary(); void saveIfNeccessary();
@ -25,6 +34,9 @@ class AutoSaver : public QObject {
private: private:
QBasicTimer m_timer; QBasicTimer m_timer;
QElapsedTimer m_firstChange; QElapsedTimer m_firstChange;
int m_maxWaitSecs;
int m_periodicSaveSecs;
QString m_savingSlot;
}; };
#endif // AUTOSAVER_H #endif // AUTOSAVER_H

View File

@ -18,7 +18,8 @@
#include <QWebEngineCookieStore> #include <QWebEngineCookieStore>
#endif #endif
CookieJar::CookieJar(QObject* parent) : QNetworkCookieJar(parent) { CookieJar::CookieJar(QObject* parent)
: QNetworkCookieJar(parent), m_saver(AutoSaver(this, QSL("saveCookies"), 25, 45)) {
#if defined(USE_WEBENGINE) #if defined(USE_WEBENGINE)
auto* web_factory = qobject_cast<WebFactory*>(parent); auto* web_factory = qobject_cast<WebFactory*>(parent);
@ -132,7 +133,8 @@ bool CookieJar::insertCookieInternal(const QNetworkCookie& cookie, bool notify_o
if (result) { if (result) {
if (should_save) { if (should_save) {
saveCookies(); m_saver.changeOccurred();
// saveCookies();
} }
#if defined(USE_WEBENGINE) #if defined(USE_WEBENGINE)
@ -151,7 +153,8 @@ bool CookieJar::updateCookieInternal(const QNetworkCookie& cookie, bool notify_o
auto result = QNetworkCookieJar::updateCookie(cookie); auto result = QNetworkCookieJar::updateCookie(cookie);
if (result) { if (result) {
saveCookies(); m_saver.changeOccurred();
// saveCookies();
#if defined(USE_WEBENGINE) #if defined(USE_WEBENGINE)
if (notify_others) { if (notify_others) {
@ -169,7 +172,8 @@ bool CookieJar::deleteCookieInternal(const QNetworkCookie& cookie, bool notify_o
auto result = QNetworkCookieJar::deleteCookie(cookie); auto result = QNetworkCookieJar::deleteCookie(cookie);
if (result) { if (result) {
saveCookies(); m_saver.changeOccurred();
// saveCookies();
#if defined(USE_WEBENGINE) #if defined(USE_WEBENGINE)
if (notify_others) { if (notify_others) {

View File

@ -5,6 +5,8 @@
#include <QNetworkCookieJar> #include <QNetworkCookieJar>
#include "miscellaneous/autosaver.h"
#if defined(USE_WEBENGINE) #if defined(USE_WEBENGINE)
class QWebEngineCookieStore; class QWebEngineCookieStore;
#endif #endif
@ -29,6 +31,7 @@ class CookieJar : public QNetworkCookieJar {
bool updateCookieInternal(const QNetworkCookie& cookie, bool notify_others); bool updateCookieInternal(const QNetworkCookie& cookie, bool notify_others);
bool deleteCookieInternal(const QNetworkCookie& cookie, bool notify_others); bool deleteCookieInternal(const QNetworkCookie& cookie, bool notify_others);
private slots:
void loadCookies(); void loadCookies();
void saveCookies(); void saveCookies();
@ -38,6 +41,7 @@ class CookieJar : public QNetworkCookieJar {
#endif #endif
bool m_ignoreAllCookies; bool m_ignoreAllCookies;
AutoSaver m_saver;
}; };
#endif // COOKIEJAR_H #endif // COOKIEJAR_H