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"
#define AUTOSAVE_IN (1000 * 3) // seconds
#define MAXWAIT (1000 * 15) // seconds
AutoSaver::AutoSaver(QObject* parent) : QObject(parent) {
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) {
Q_ASSERT(parent);
}
@ -30,11 +28,11 @@ void AutoSaver::changeOccurred() {
m_firstChange.start();
}
if (m_firstChange.elapsed() > MAXWAIT) {
if (m_firstChange.elapsed() > m_maxWaitSecs) {
saveIfNeccessary();
}
else {
m_timer.start(AUTOSAVE_IN, this);
m_timer.start(m_periodicSaveSecs, this);
}
}
@ -52,8 +50,12 @@ void AutoSaver::saveIfNeccessary() {
m_timer.stop();
m_firstChange.invalidate();
if (!QMetaObject::invokeMethod(parent(), "save", Qt::DirectConnection)) {
qCriticalNN << LOGSEC_CORE << ("AutoSaver error invoking slot save() on parent.");
if (!QMetaObject::invokeMethod(parent(), qPrintable(m_savingSlot), Qt::ConnectionType::DirectConnection)) {
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
#define AUTOSAVER_H
#include <QBasicTimer>
#include <QElapsedTimer>
#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 {
Q_OBJECT
Q_OBJECT
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();
void saveIfNeccessary();
@ -25,6 +34,9 @@ class AutoSaver : public QObject {
private:
QBasicTimer m_timer;
QElapsedTimer m_firstChange;
int m_maxWaitSecs;
int m_periodicSaveSecs;
QString m_savingSlot;
};
#endif // AUTOSAVER_H

View File

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

View File

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