diff --git a/src/librssguard/miscellaneous/autosaver.cpp b/src/librssguard/miscellaneous/autosaver.cpp index 25d4b06fa..78e1a8744 100644 --- a/src/librssguard/miscellaneous/autosaver.cpp +++ b/src/librssguard/miscellaneous/autosaver.cpp @@ -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); } } } diff --git a/src/librssguard/miscellaneous/autosaver.h b/src/librssguard/miscellaneous/autosaver.h index 7eee19a48..1e0bf8fb2 100644 --- a/src/librssguard/miscellaneous/autosaver.h +++ b/src/librssguard/miscellaneous/autosaver.h @@ -3,15 +3,24 @@ #ifndef AUTOSAVER_H #define AUTOSAVER_H -#include -#include #include +#include "definitions/definitions.h" + +#include +#include + +#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 diff --git a/src/librssguard/network-web/cookiejar.cpp b/src/librssguard/network-web/cookiejar.cpp index b819f7bae..fcb412d48 100644 --- a/src/librssguard/network-web/cookiejar.cpp +++ b/src/librssguard/network-web/cookiejar.cpp @@ -18,7 +18,8 @@ #include #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(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) { diff --git a/src/librssguard/network-web/cookiejar.h b/src/librssguard/network-web/cookiejar.h index 59f97e4ad..6531c2ddc 100644 --- a/src/librssguard/network-web/cookiejar.h +++ b/src/librssguard/network-web/cookiejar.h @@ -5,6 +5,8 @@ #include +#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