File indexing completed on 2024-05-05 17:33:57

0001 /*
0002  *   SPDX-FileCopyrightText: 2007 Tobias Koenig <tokoe@kde.org>
0003  *   SPDX-FileCopyrightText: 2008-2010 Matthias Fuchs <mat69@gmx.net>
0004  *
0005  *   SPDX-License-Identifier: GPL-2.0-or-later
0006  */
0007 
0008 #include "comicupdater.h"
0009 #include "comicmodel.h"
0010 
0011 #include <QSortFilterProxyModel>
0012 #include <QTimer>
0013 
0014 #include <KConfigDialog>
0015 #include <KNSCore/Engine>
0016 
0017 ComicUpdater::ComicUpdater(QObject *parent)
0018     : QObject(parent)
0019     , mEngine(nullptr)
0020     , mUpdateIntervall(3)
0021     , m_updateTimer(nullptr)
0022     , mProvidersLoaded(false)
0023 {
0024 }
0025 
0026 ComicUpdater::~ComicUpdater()
0027 {
0028 }
0029 
0030 void ComicUpdater::init(const KConfigGroup &group)
0031 {
0032     mGroup = group;
0033 }
0034 
0035 void ComicUpdater::load()
0036 {
0037     // check when the last update happened and update if necessary
0038     mUpdateIntervall = mGroup.readEntry("updateInterval", 3);
0039     if (mUpdateIntervall) {
0040         mLastUpdate = mGroup.readEntry("lastUpdate", QDateTime());
0041         checkForUpdate();
0042     }
0043 }
0044 
0045 void ComicUpdater::save()
0046 {
0047     mGroup.writeEntry("updateInterval", mUpdateIntervall);
0048 }
0049 
0050 void ComicUpdater::setInterval(int interval)
0051 {
0052     mUpdateIntervall = interval;
0053 }
0054 
0055 int ComicUpdater::interval() const
0056 {
0057     return mUpdateIntervall;
0058 }
0059 
0060 void ComicUpdater::checkForUpdate()
0061 {
0062     // start a timer to check each hour, if KNS3 should look for updates
0063     if (!m_updateTimer) {
0064         m_updateTimer = new QTimer(this);
0065         connect(m_updateTimer, &QTimer::timeout, this, &ComicUpdater::checkForUpdate);
0066         m_updateTimer->start(1 * 60 * 60 * 1000);
0067     }
0068 
0069     if (!mLastUpdate.isValid() || (mLastUpdate.addDays(mUpdateIntervall) < QDateTime::currentDateTime())) {
0070         mLastUpdate = QDateTime::currentDateTime();
0071         mGroup.writeEntry("lastUpdate", mLastUpdate);
0072         if (mProvidersLoaded) {
0073             engine()->checkForUpdates();
0074         } else {
0075             connect(engine(), &KNSCore::Engine::signalProvidersLoaded, this, [this]() {
0076                 engine()->checkForUpdates();
0077             });
0078         }
0079     }
0080 }
0081 
0082 void ComicUpdater::slotUpdatesFound(const KNSCore::EntryInternal::List &entries)
0083 {
0084     for (int i = 0; i < entries.count(); ++i) {
0085         engine()->install(entries[i]);
0086     }
0087 }
0088 
0089 KNSCore::Engine *ComicUpdater::engine()
0090 {
0091     if (!mEngine) {
0092         mEngine = new KNSCore::Engine(this);
0093         if (mEngine->init(QStringLiteral("comic.knsrc"))) {
0094             connect(mEngine, &KNSCore::Engine::signalUpdateableEntriesLoaded, this, &ComicUpdater::slotUpdatesFound);
0095             connect(mEngine, &KNSCore::Engine::signalProvidersLoaded, this, [this]() {
0096                 mProvidersLoaded = true;
0097             });
0098         }
0099     }
0100     return mEngine;
0101 }