| 1 | // -*- c++ -*- |
| 2 | /* |
| 3 | This file is part of the KDE libraries |
| 4 | SPDX-FileCopyrightText: 2000 Stephan Kulow <coolo@kde.org> |
| 5 | SPDX-FileCopyrightText: 2000 Waldo Bastian <bastian@kde.org> |
| 6 | |
| 7 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 8 | */ |
| 9 | |
| 10 | #ifndef _kio_scheduler_h |
| 11 | #define _kio_scheduler_h |
| 12 | |
| 13 | #include "simplejob.h" |
| 14 | #include <QMap> |
| 15 | #include <QTimer> |
| 16 | |
| 17 | namespace KIO |
| 18 | { |
| 19 | class Worker; |
| 20 | |
| 21 | class SchedulerPrivate; |
| 22 | /* |
| 23 | * |
| 24 | * The KIO::Scheduler manages KIO workers for the application. |
| 25 | * It also queues jobs and assigns the job to a worker when one |
| 26 | * becomes available. |
| 27 | * |
| 28 | * There are two possible ways for a job to get a worker: |
| 29 | * |
| 30 | * <h3>1. Direct</h3> |
| 31 | * This is the default. When you create a job the |
| 32 | * KIO::Scheduler will be notified and will find either an existing |
| 33 | * worker that is idle or it will create a new worker for the job. |
| 34 | * |
| 35 | * |
| 36 | * <h3>2. Scheduled</h3> |
| 37 | * If you create a lot of jobs, you might want not want to have a |
| 38 | * worker for each job. If you schedule a job, a maximum number |
| 39 | * of workers will be created. When more jobs arrive, they will be |
| 40 | * queued. When a worker is finished with a job, it will be assigned |
| 41 | * a job from the queue. |
| 42 | * |
| 43 | * \sa KIO::Job |
| 44 | */ |
| 45 | class Scheduler : public QObject |
| 46 | { |
| 47 | Q_OBJECT |
| 48 | Q_CLASSINFO("D-Bus Interface" , "org.kde.KIO.Scheduler" ) |
| 49 | public: |
| 50 | /*! |
| 51 | * Register \a job with the scheduler. |
| 52 | * The default is to create a new worker for the job if no worker |
| 53 | * is available. |
| 54 | * |
| 55 | * \a job the job to register |
| 56 | */ |
| 57 | static void doJob(SimpleJob *job); |
| 58 | |
| 59 | /*! |
| 60 | * Stop the execution of a job. |
| 61 | * \a job the job to cancel |
| 62 | */ |
| 63 | static void cancelJob(SimpleJob *job); |
| 64 | |
| 65 | /*! |
| 66 | * Called when a job is done. |
| 67 | * \a job the finished job |
| 68 | * |
| 69 | * \a worker the worker that executed the \a job |
| 70 | */ |
| 71 | static void jobFinished(KIO::SimpleJob *job, KIO::Worker *worker); |
| 72 | |
| 73 | /*! |
| 74 | * Puts a worker on notice. A next job may reuse this worker if it |
| 75 | * requests the same URL. |
| 76 | * |
| 77 | * A job can be put on hold after it has emit'ed its mimetype() signal. |
| 78 | * Based on the MIME type, the program can give control to another |
| 79 | * component in the same process which can then resume the job |
| 80 | * by simply asking for the same URL again. |
| 81 | * |
| 82 | * \a job the job that should be stopped |
| 83 | * |
| 84 | * \a url the URL that is handled by the \a url |
| 85 | * |
| 86 | * \since 5.101 |
| 87 | */ |
| 88 | static void putWorkerOnHold(KIO::SimpleJob *job, const QUrl &url); |
| 89 | |
| 90 | /*! |
| 91 | * Removes any worker that might have been put on hold. If a worker |
| 92 | * was put on hold it will be killed. |
| 93 | * |
| 94 | * \since 5.101 |
| 95 | */ |
| 96 | static void removeWorkerOnHold(); |
| 97 | |
| 98 | static void emitReparseSlaveConfiguration(); |
| 99 | // KF6 TODO: rename to emitReparseWorkerConfiguration. See also T15956. |
| 100 | |
| 101 | /*! |
| 102 | * Returns true if there is a worker on hold for \a url. |
| 103 | * |
| 104 | * \since 5.101 |
| 105 | */ |
| 106 | static bool isWorkerOnHoldFor(const QUrl &url); |
| 107 | |
| 108 | /*! |
| 109 | * Updates the internal metadata from job. |
| 110 | * |
| 111 | * \since 4.6.5 |
| 112 | */ |
| 113 | static void updateInternalMetaData(SimpleJob *job); |
| 114 | |
| 115 | Q_SIGNALS: |
| 116 | |
| 117 | // DBUS |
| 118 | Q_SCRIPTABLE void reparseSlaveConfiguration(const QString &); |
| 119 | // KF6 TODO: rename to reparseWorkerConfiguration. See also T15956. |
| 120 | |
| 121 | private: |
| 122 | Q_DISABLE_COPY(Scheduler) |
| 123 | KIOCORE_NO_EXPORT Scheduler(); |
| 124 | KIOCORE_NO_EXPORT ~Scheduler() override; |
| 125 | |
| 126 | KIOCORE_NO_EXPORT static Scheduler *self(); |
| 127 | |
| 128 | // connected to D-Bus signal: |
| 129 | #ifdef WITH_QTDBUS |
| 130 | Q_PRIVATE_SLOT(d_func(), void slotReparseSlaveConfiguration(const QString &, const QDBusMessage &)) |
| 131 | #endif |
| 132 | |
| 133 | private: |
| 134 | friend class SchedulerPrivate; |
| 135 | KIOCORE_NO_EXPORT SchedulerPrivate *d_func(); |
| 136 | }; |
| 137 | |
| 138 | } |
| 139 | #endif |
| 140 | |