File indexing completed on 2025-01-26 05:07:43

0001 /*
0002     SPDX-FileCopyrightText: 2013 Sebastian Kügler <sebas@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "pumpjob.h"
0008 
0009 #include <QDebug>
0010 
0011 #include <KIO/Global>
0012 #include <KIO/JobTracker>
0013 #include <KJobTrackerInterface>
0014 #include <KLocalizedString>
0015 #include <QStringList>
0016 #include <QTimer>
0017 
0018 static QTextStream cout(stdout);
0019 
0020 class PumpJobPrivate
0021 {
0022 public:
0023     QString name;
0024     QString error;
0025 
0026     QTimer *timer;
0027     int interval = 200;
0028 
0029     int counter = 0;
0030 
0031     bool suspended = false;
0032 };
0033 
0034 PumpJob::PumpJob(int interval)
0035     : KIO::Job()
0036 {
0037     d = new PumpJobPrivate;
0038 
0039     if (interval) {
0040         d->interval = d->interval * interval;
0041     }
0042     KIO::getJobTracker()->registerJob(this);
0043 
0044     d->timer = new QTimer(this);
0045     d->timer->setInterval(d->interval);
0046     qDebug() << "Starting job with interval: " << d->interval;
0047 
0048     connect(d->timer, &QTimer::timeout, this, &PumpJob::timeout);
0049 
0050     init();
0051 }
0052 
0053 void PumpJob::init()
0054 {
0055     Q_EMIT description(this,
0056                        i18n("Pump Job"),
0057                        qMakePair(i18n("Source"), QStringLiteral("this is the source")),
0058                        qMakePair(i18n("Destination"), QStringLiteral("destination, baby")));
0059     d->timer->start();
0060 }
0061 
0062 PumpJob::~PumpJob()
0063 {
0064     KIO::getJobTracker()->unregisterJob(this);
0065     qDebug() << "Bye bye";
0066     delete d;
0067 }
0068 
0069 void PumpJob::start()
0070 {
0071     qDebug() << "Starting job / timer";
0072     d->timer->start();
0073 }
0074 
0075 bool PumpJob::doKill()
0076 {
0077     qDebug() << "kill";
0078     emitResult();
0079     d->timer->stop();
0080     setError(KIO::ERR_USER_CANCELED);
0081     setErrorText(QStringLiteral("You killed the job."));
0082     return KJob::doKill();
0083 }
0084 
0085 bool PumpJob::doResume()
0086 {
0087     d->suspended = false;
0088     qDebug() << "resume";
0089     d->timer->start();
0090     Q_EMIT resumed(this);
0091     return KJob::doResume();
0092 }
0093 
0094 bool PumpJob::isSuspended() const
0095 {
0096     return d->suspended;
0097 }
0098 
0099 bool PumpJob::doSuspend()
0100 {
0101     d->suspended = true;
0102     qDebug() << "suspend";
0103     d->timer->stop();
0104     Q_EMIT suspended(this);
0105     return KJob::doSuspend();
0106 }
0107 
0108 void PumpJob::timeout()
0109 {
0110     d->counter++;
0111     setPercent(d->counter);
0112     emitSpeed(1024 * 1024 * d->counter / 70); // just something randomly changing
0113     int seconds = (int)((d->interval * 100) - (d->interval * percent())) / 1000;
0114     Q_EMIT infoMessage(this, i18n("Testing kuiserver (%1 seconds remaining)", seconds));
0115 
0116     qDebug() << "percent: " << percent() << "  Seconds: " << seconds;
0117     if (d->counter % 20 == 0) {
0118         // qDebug() << "percent: " << percent() << "  Seconds: " << seconds;
0119     }
0120 
0121     if (d->counter >= 100) {
0122         qDebug() << "Job done";
0123         emitResult();
0124     }
0125 }
0126 
0127 #include "moc_pumpjob.cpp"