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

0001 /*
0002  *   SPDX-FileCopyrightText: 2011 Matthias Fuchs <mat69@gmx.net>
0003  *   SPDX-FileCopyrightText: 2022 Alexander Lohnau <alexander.lohnau@gmx.de>
0004  *
0005  *   SPDX-License-Identifier: GPL-2.0-or-later
0006  */
0007 
0008 #include "checknewstrips.h"
0009 
0010 #include <QTimer>
0011 
0012 CheckNewStrips::CheckNewStrips(const QStringList &identifiers, ComicEngine *engine, int minutes, QObject *parent)
0013     : QObject(parent)
0014     , mMinutes(minutes)
0015     , mIndex(0)
0016     , mEngine(engine)
0017     , mIdentifiers(identifiers)
0018 {
0019     QTimer *timer = new QTimer(this);
0020     timer->setInterval(minutes * 60 * 1000);
0021     connect(timer, &QTimer::timeout, this, &CheckNewStrips::start);
0022     timer->start();
0023 
0024     // start at once, that way the user does not have to wait for minutes to get the initial result
0025     start();
0026     connect(mEngine, &ComicEngine::requestFinished, this, &CheckNewStrips::dataUpdated);
0027 }
0028 
0029 void CheckNewStrips::dataUpdated(const ComicMetaData &data)
0030 {
0031     const QString source = data.identifier;
0032     QString lastIdentifierSuffix;
0033 
0034     if (!data.error) {
0035         lastIdentifierSuffix = data.identifier;
0036         lastIdentifierSuffix.remove(source);
0037     }
0038 
0039     if (!lastIdentifierSuffix.isEmpty()) {
0040         QString temp = source;
0041         temp.remove(QLatin1Char(':'));
0042         Q_EMIT lastStrip(mIndex, temp, lastIdentifierSuffix);
0043     }
0044     ++mIndex;
0045 
0046     if (mIndex < mIdentifiers.count()) {
0047         const QString newSource = mIdentifiers[mIndex] + QLatin1Char(':');
0048         mEngine->requestSource(newSource);
0049     } else {
0050         mIndex = 0;
0051     }
0052 }
0053 
0054 void CheckNewStrips::start()
0055 {
0056     // already running, do nothing
0057     if (mIndex) {
0058         return;
0059     }
0060 
0061     if (mIndex < mIdentifiers.count()) {
0062         const QString newSource = mIdentifiers[mIndex] + QLatin1Char(':');
0063         mEngine->requestSource(newSource);
0064     }
0065 }