File indexing completed on 2024-05-12 04:57:51

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2010-2016  David Rosca <nowrep@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "autosaver.h"
0019 
0020 #include <QTimerEvent>
0021 #include <QCoreApplication>
0022 
0023 #define SAVE_DELAY 1000 * 10 // 10 seconds
0024 
0025 AutoSaver::AutoSaver(QObject* parent)
0026     : QObject(parent)
0027 {
0028     connect(qApp, &QCoreApplication::aboutToQuit, this, &AutoSaver::saveIfNecessary);
0029 }
0030 
0031 void AutoSaver::saveIfNecessary()
0032 {
0033     if (m_timer.isActive()) {
0034         m_timer.stop();
0035         Q_EMIT save();
0036     }
0037 }
0038 
0039 void AutoSaver::changeOccurred()
0040 {
0041     if (!m_timer.isActive()) {
0042         m_timer.start(SAVE_DELAY, this);
0043     }
0044 }
0045 
0046 void AutoSaver::timerEvent(QTimerEvent* event)
0047 {
0048     if (event->timerId() == m_timer.timerId()) {
0049         m_timer.stop();
0050         Q_EMIT save();
0051     }
0052 
0053     QObject::timerEvent(event);
0054 }