File indexing completed on 2024-10-06 03:40:32

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2007 Kevin Ottens <ervin@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only
0006 */
0007 
0008 #include "kjobtrackerstest.h"
0009 #include "kdialogjobuidelegate.h"
0010 
0011 #include <QApplication>
0012 #include <QDebug>
0013 #include <QMainWindow>
0014 #include <QStatusBar>
0015 
0016 #include <KJobWidgets>
0017 #include <kstatusbarjobtracker.h>
0018 #include <kuiserverjobtracker.h>
0019 #include <kwidgetjobtracker.h>
0020 
0021 KTestJob::KTestJob(int numberOfDirs)
0022     : KJob()
0023     , m_numberOfDirs(numberOfDirs)
0024     , m_currentSpeed(1000)
0025     , m_state(Stopped)
0026 {
0027     setCapabilities(KJob::Killable | KJob::Suspendable);
0028 }
0029 
0030 KTestJob::~KTestJob()
0031 {
0032 }
0033 
0034 void KTestJob::start()
0035 {
0036     connect(&m_timer, &QTimer::timeout, this, &KTestJob::nextStep);
0037     m_state = StatingDirs;
0038     m_timer.start(50);
0039     Q_EMIT description(this,
0040                        QStringLiteral("Copying"),
0041                        qMakePair(QStringLiteral("Source"), QStringLiteral("file:/src")),
0042                        qMakePair(QStringLiteral("Destination"), QStringLiteral("file:/dest")));
0043 }
0044 
0045 void KTestJob::nextStep()
0046 {
0047     switch (m_state) {
0048     case StatingDirs:
0049         Q_EMIT infoMessage(this, QStringLiteral("Initial listing"));
0050         stateNextDir();
0051         break;
0052     case CreatingDirs:
0053         Q_EMIT infoMessage(this, QStringLiteral("Folder creation"));
0054         createNextDir();
0055         break;
0056     case CopyingFiles:
0057         Q_EMIT infoMessage(this, QStringLiteral("Actual file copying"));
0058         copyNextFile();
0059         break;
0060     case Stopped:
0061         qDebug() << "Do nothing, we stopped";
0062     }
0063 }
0064 
0065 void KTestJob::stateNextDir()
0066 {
0067     if (totalAmount(KJob::Directories) == m_numberOfDirs) {
0068         m_state = CreatingDirs;
0069         return;
0070     }
0071 
0072     QString directory_name = QStringLiteral("dir") + QString::number(totalAmount(KJob::Directories));
0073 
0074     qDebug() << "Stating " << directory_name;
0075     setTotalAmount(KJob::Directories, totalAmount(KJob::Directories) + 1);
0076     setTotalAmount(KJob::Files, totalAmount(KJob::Directories) * 10);
0077     setTotalAmount(KJob::Bytes, totalAmount(KJob::Files) * 1000);
0078 
0079     Q_EMIT warning(this, directory_name);
0080     Q_EMIT description(this, QStringLiteral("Stating"), qMakePair(QStringLiteral("Stating"), QString(QStringLiteral("file:/src/") + directory_name)));
0081 }
0082 
0083 void KTestJob::createNextDir()
0084 {
0085     if (processedAmount(KJob::Directories) == totalAmount(KJob::Directories)) {
0086         m_state = CopyingFiles;
0087         return;
0088     }
0089 
0090     QString directory_name = QStringLiteral("dir") + QString::number(processedAmount(KJob::Directories));
0091 
0092     qDebug() << "Creating " << directory_name;
0093     setProcessedAmount(KJob::Directories, processedAmount(KJob::Directories) + 1);
0094 
0095     Q_EMIT description(this, QStringLiteral("Creating Dir"), qMakePair(QStringLiteral("Creating"), QString(QStringLiteral("file:/dest/") + directory_name)));
0096 }
0097 
0098 void KTestJob::copyNextFile()
0099 {
0100     if (processedAmount(KJob::Files) == totalAmount(KJob::Files)) {
0101         m_state = Stopped;
0102         m_timer.stop();
0103         emitResult();
0104         return;
0105     }
0106 
0107     QString file_name =
0108         QLatin1String("dir%1/file%2").arg(QString::number(processedAmount(KJob::Files) / 10), QString::number(processedAmount(KJob::Files) % 10));
0109 
0110     qDebug() << "Copying " << file_name;
0111     setProcessedAmount(KJob::Files, processedAmount(KJob::Files) + 1);
0112     setProcessedAmount(KJob::Bytes, processedAmount(KJob::Bytes) + 1000);
0113 
0114     Q_EMIT description(this,
0115                        QStringLiteral("Copying"),
0116                        qMakePair(QStringLiteral("Source"), QString(QStringLiteral("file:/src/") + file_name)),
0117                        qMakePair(QStringLiteral("Destination"), QString(QStringLiteral("file:/dest/") + file_name)));
0118 
0119     emitSpeed(m_currentSpeed);
0120 }
0121 
0122 bool KTestJob::doSuspend()
0123 {
0124     m_timer.stop();
0125     return true;
0126 }
0127 
0128 bool KTestJob::doResume()
0129 {
0130     m_timer.start(50);
0131     return true;
0132 }
0133 
0134 bool KTestJob::doKill()
0135 {
0136     m_timer.stop();
0137     m_state = Stopped;
0138     return true;
0139 }
0140 
0141 int main(int argc, char **argv)
0142 {
0143     QApplication::setApplicationName(QStringLiteral("kjobtrackerstest"));
0144 
0145     QApplication app(argc, argv);
0146 
0147     KTestJob *testJob = new KTestJob(10 /* 100000 bytes to process */);
0148 
0149     KWidgetJobTracker *tracker1 = new KWidgetJobTracker();
0150     tracker1->registerJob(testJob);
0151 
0152     QMainWindow *main = new QMainWindow;
0153     main->setWindowTitle(QStringLiteral("Mainwindow with statusbar-job-tracker"));
0154     main->show();
0155 
0156     QStatusBar *statusBar = new QStatusBar(main);
0157     KStatusBarJobTracker *tracker2 = new KStatusBarJobTracker(main, true);
0158     tracker2->registerJob(testJob);
0159     tracker2->setStatusBarMode(KStatusBarJobTracker::ProgressOnly);
0160     statusBar->addWidget(tracker2->widget(testJob));
0161 
0162     main->setStatusBar(statusBar);
0163 
0164     KUiServerJobTracker *tracker3 = new KUiServerJobTracker(main);
0165     tracker3->registerJob(testJob);
0166 
0167     KJobWidgets::setWindow(testJob, main);
0168     testJob->setUiDelegate(new KDialogJobUiDelegate());
0169 
0170     testJob->start();
0171 
0172     tracker1->widget(testJob)->show();
0173     tracker2->widget(testJob)->show();
0174 
0175     return app.exec();
0176 }
0177 
0178 #include "moc_kjobtrackerstest.cpp"