Debian Codegraph @

src/librssguard/miscellaneous/autosaver.cpp

raw symbols SCIP
1 // For license of this file, see <project-root-folder>/LICENSE.md.
2
3 #include "miscellaneous/autosaver.h"
4
5 #include "definitions/definitions.h"
6
7 #include <QCoreApplication>
8 #include <QDir>
9 #include <QMetaObject>
10
11 AutoSaver::AutoSaver(QObject* parent, const QString& saving_slot, int max_wait_secs, int periodic_save_secs)
12 : QObject(parent), m_maxWaitMsecs(max_wait_secs * 1000), m_periodicSaveMsecs(periodic_save_secs * 1000),
13 m_savingSlot(saving_slot) {
14 Q_ASSERT(parent);
15
16 connect(&m_timer, &QTimer::timeout, this, &AutoSaver::saveIfNeccessary);
17 }
18
19 AutoSaver::~AutoSaver() {
20 if (m_timer.isActive()) {
21 qWarningNN << LOGSEC_CORE << "AutoSaver still active when destroyed, changes not saved.";
22
23 if (parent() != nullptr && parent()->metaObject() != nullptr) {
24 qDebugNN << LOGSEC_CORE << "Should call saveIfNeccessary.";
25 }
26 }
27 }
28
29 void AutoSaver::changeOccurred() {
30 if (!m_firstChange.isValid()) {
31 m_firstChange.start();
32 }
33
34 if (m_firstChange.elapsed() > m_maxWaitMsecs) {
35 saveIfNeccessary();
36 }
37 else {
38 QMetaObject::invokeMethod(&m_timer, "start", Qt::ConnectionType::AutoConnection, Q_ARG(int, m_periodicSaveMsecs));
39 }
40 }
41
42 void AutoSaver::saveIfNeccessary() {
43 if (m_timer.isActive()) {
44 QMetaObject::invokeMethod(&m_timer, "stop", Qt::ConnectionType::AutoConnection);
45 m_firstChange.invalidate();
46
47 if (!QMetaObject::invokeMethod(parent(), qPrintable(m_savingSlot), Qt::ConnectionType::DirectConnection)) {
48 qCriticalNN << LOGSEC_CORE << "AutoSaver error invoking saving slot on parent.";
49 }
50 else {
51 qDebugNN << LOGSEC_CORE << "Saved data with auto-saver for"
52 << QUOTE_W_SPACE(parent()->metaObject()->className()) "and method" << QUOTE_W_SPACE_DOT(m_savingSlot);
53 }
54 }
55 }