File indexing completed on 2024-04-28 15:26:31

0001 // -*- c++ -*-
0002 /*
0003     This file is part of the KDE libraries
0004     SPDX-FileCopyrightText: 2000 Stephan Kulow <coolo@kde.org>
0005     SPDX-FileCopyrightText: 2000 Waldo Bastian <bastian@kde.org>
0006 
0007     SPDX-License-Identifier: LGPL-2.0-or-later
0008 */
0009 
0010 #ifndef _kio_scheduler_h
0011 #define _kio_scheduler_h
0012 
0013 #include "simplejob.h"
0014 #include <QMap>
0015 #include <QTimer>
0016 
0017 namespace KIO
0018 {
0019 class Slave;
0020 #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 102)
0021 class SlaveConfig;
0022 #endif
0023 
0024 class SchedulerPrivate;
0025 /**
0026  * @class KIO::Scheduler scheduler.h <KIO/Scheduler>
0027  *
0028  * The KIO::Scheduler manages KIO workers for the application.
0029  * It also queues jobs and assigns the job to a worker when one
0030  * becomes available.
0031  *
0032  * There are 3 possible ways for a job to get a worker:
0033  *
0034  * <h3>1. Direct</h3>
0035  * This is the default. When you create a job the
0036  * KIO::Scheduler will be notified and will find either an existing
0037  * worker that is idle or it will create a new worker for the job.
0038  *
0039  * Example:
0040  * \code
0041  *    TransferJob *job = KIO::get(QUrl("https://www.kde.org"));
0042  * \endcode
0043  *
0044  *
0045  * <h3>2. Scheduled</h3>
0046  * If you create a lot of jobs, you might want not want to have a
0047  * worker for each job. If you schedule a job, a maximum number
0048  * of workers will be created. When more jobs arrive, they will be
0049  * queued. When a worker is finished with a job, it will be assigned
0050  * a job from the queue.
0051  *
0052  * Example:
0053  * \code
0054  *    TransferJob *job = KIO::get(QUrl("https://www.kde.org"));
0055  *    KIO::Scheduler::setJobPriority(job, 1);
0056  * \endcode
0057  *
0058  * <h3>3. Connection Oriented (TODO KF6 remove this section)</h3>
0059  * For some operations it is important that multiple jobs use
0060  * the same connection. This can only be ensured if all these jobs
0061  * use the same slave.
0062  *
0063  * You can ask the scheduler to open a slave for connection oriented
0064  * operations. You can then use the scheduler to assign jobs to this
0065  * slave. The jobs will be queued and the slave will handle these jobs
0066  * one after the other.
0067  *
0068  * Example:
0069  * \code
0070  *    Slave *slave = KIO::Scheduler::getConnectedSlave(
0071  *            QUrl("pop3://bastian:password@mail.kde.org"));
0072  *    TransferJob *job1 = KIO::get(
0073  *            QUrl("pop3://bastian:password@mail.kde.org/msg1"));
0074  *    KIO::Scheduler::assignJobToSlave(slave, job1);
0075  *    TransferJob *job2 = KIO::get(
0076  *            QUrl("pop3://bastian:password@mail.kde.org/msg2"));
0077  *    KIO::Scheduler::assignJobToSlave(slave, job2);
0078  *    TransferJob *job3 = KIO::get(
0079  *            QUrl("pop3://bastian:password@mail.kde.org/msg3"));
0080  *    KIO::Scheduler::assignJobToSlave(slave, job3);
0081  *
0082  *    // ... Wait for jobs to finish...
0083  *
0084  *    KIO::Scheduler::disconnectSlave(slave);
0085  * \endcode
0086  *
0087  * Note that you need to explicitly disconnect the slave when the
0088  * connection goes down, so your error handler should contain:
0089  * \code
0090  *    if (error == KIO::ERR_CONNECTION_BROKEN)
0091  *        KIO::Scheduler::disconnectSlave(slave);
0092  * \endcode
0093  *
0094  * @see KIO::Slave
0095  * @see KIO::Job
0096  **/
0097 
0098 class KIOCORE_EXPORT Scheduler : public QObject
0099 {
0100     Q_OBJECT
0101     Q_CLASSINFO("D-Bus Interface", "org.kde.KIO.Scheduler")
0102 public:
0103     /**
0104      * Register @p job with the scheduler.
0105      * The default is to create a new worker for the job if no worker
0106      * is available. This can be changed by calling setJobPriority.
0107      * @param job the job to register
0108      */
0109     static void doJob(SimpleJob *job);
0110 
0111 #if KIOCORE_ENABLE_DEPRECATED_SINCE(4, 5)
0112     /**
0113      * Schedules @p job scheduled for later execution.
0114      * This method is deprecated and just sets the job's priority to 1. It is
0115      * recommended to replace calls to scheduleJob(job) with setJobPriority(job, 1).
0116      * @param job the job to schedule
0117      * @deprecated Since 4.5, use setJobPriority(SimpleJob *job, int priority)
0118      */
0119     KIOCORE_DEPRECATED_VERSION(4, 5, "Use Scheduler::setJobPriority(SimpleJob *, int )")
0120     static void scheduleJob(SimpleJob *job);
0121 #endif
0122 
0123 #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 90)
0124     /**
0125      * Changes the priority of @p job; jobs of the same priority run in the order in which
0126      * they were created. Jobs of lower numeric priority always run before any
0127      * waiting jobs of higher numeric priority. The range of priority is -10 to 10,
0128      * the default priority of jobs is 0.
0129      * @param job the job to change
0130      * @param priority new priority of @p job, lower runs earlier
0131      * @deprecated Since 5.90. Changing priorities was only used by KHTML. If you need this, please contact kde-frameworks-devel to request the feature back,
0132      * but as better API like Job::setPriority.
0133      */
0134     KIOCORE_DEPRECATED_VERSION(5,
0135                                90,
0136                                "Changing priorities was only used by KHTML. If you need this, please contact kde-frameworks-devel to request the feature back, "
0137                                "but as better API like Job::setPriority.")
0138     static void setJobPriority(SimpleJob *job, int priority);
0139 #endif
0140 
0141     /**
0142      * Stop the execution of a job.
0143      * @param job the job to cancel
0144      */
0145     static void cancelJob(SimpleJob *job);
0146 
0147     /**
0148      * Called when a job is done.
0149      * @param job the finished job
0150      * @param slave the slave that executed the @p job
0151      */
0152     static void jobFinished(KIO::SimpleJob *job, KIO::Slave *slave);
0153 
0154 #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 101)
0155     /**
0156      * Puts a slave on notice. A next job may reuse this slave if it
0157      * requests the same URL.
0158      *
0159      * A job can be put on hold after it has emit'ed its mimetype() signal.
0160      * Based on the MIME type, the program can give control to another
0161      * component in the same process which can then resume the job
0162      * by simply asking for the same URL again.
0163      * @param job the job that should be stopped
0164      * @param url the URL that is handled by the @p url
0165      *
0166      * @deprecated Since 5.101, use putWorkerOnHold(KIO::SimpleJob *, const QUrl &)
0167      */
0168     static KIOCORE_DEPRECATED_VERSION(5, 101, "Use putWorkerOnHold(KIO::SimpleJob *, const QUrl &)") void putSlaveOnHold(KIO::SimpleJob *job, const QUrl &url);
0169 #endif
0170 
0171 #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 101)
0172     /**
0173      * Removes any slave that might have been put on hold. If a slave
0174      * was put on hold it will be killed.
0175      *
0176      * @deprecated Since 5.101, use removeWorkerOnHold()
0177      */
0178     static KIOCORE_DEPRECATED_VERSION(5, 101, "Use removeWorkerOnHold()") void removeSlaveOnHold();
0179 #endif
0180 
0181     /**
0182      * Puts a worker on notice. A next job may reuse this worker if it
0183      * requests the same URL.
0184      *
0185      * A job can be put on hold after it has emit'ed its mimetype() signal.
0186      * Based on the MIME type, the program can give control to another
0187      * component in the same process which can then resume the job
0188      * by simply asking for the same URL again.
0189      * @param job the job that should be stopped
0190      * @param url the URL that is handled by the @p url
0191      *
0192      * @since 5.101
0193      */
0194     static void putWorkerOnHold(KIO::SimpleJob *job, const QUrl &url);
0195 
0196     /**
0197      * Removes any worker that might have been put on hold. If a worker
0198      * was put on hold it will be killed.
0199      *
0200      * @since 5.101
0201      */
0202     static void removeWorkerOnHold();
0203 
0204 #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 88)
0205     /**
0206      * Send the slave that was put on hold back to KLauncher. This
0207      * allows another process to take over the slave and resume the job
0208      * that was started.
0209      * @deprecated since 5.88, the feature of holding slaves between processes is gone, just remove the call
0210      */
0211     KIOCORE_DEPRECATED_VERSION(5, 88, "Remove this call. The feature is gone")
0212     static void publishSlaveOnHold();
0213 #endif
0214 
0215 #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 91)
0216     /**
0217      * Requests a slave for use in connection-oriented mode.
0218      *
0219      * @param url This defines the username,password,host & port to
0220      *            connect with.
0221      * @param config Configuration data for the slave.
0222      *
0223      * @return A pointer to a connected slave or @c nullptr if an error occurred.
0224      * @see assignJobToSlave()
0225      * @see disconnectSlave()
0226      * @deprecated since 5.91 Port away from the connected slave feature, e.g. making a library out of the slave code.
0227      */
0228     KIOCORE_DEPRECATED_VERSION(5, 91, "Port away from the connected slave feature, e.g. making a library out of the slave code.")
0229     static KIO::Slave *getConnectedSlave(const QUrl &url, const KIO::MetaData &config = MetaData());
0230 #endif
0231 
0232 #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 91)
0233     /**
0234      * Uses @p slave to do @p job.
0235      * This function should be called immediately after creating a Job.
0236      *
0237      * @param slave The slave to use. The slave must have been obtained
0238      *              with a call to getConnectedSlave and must not
0239      *              be currently assigned to any other job.
0240      * @param job The job to do.
0241      *
0242      * @return true is successful, false otherwise.
0243      *
0244      * @see getConnectedSlave()
0245      * @see disconnectSlave()
0246      * @see slaveConnected()
0247      * @see slaveError()
0248      * @deprecated since 5.91 Port away from the connected slave feature, e.g. making a library out of the slave code.
0249      */
0250     KIOCORE_DEPRECATED_VERSION(5, 91, "Port away from the connected slave feature, e.g. making a library out of the slave code.")
0251     static bool assignJobToSlave(KIO::Slave *slave, KIO::SimpleJob *job);
0252 #endif
0253 
0254 #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 91)
0255     /**
0256      * Disconnects @p slave.
0257      *
0258      * @param slave The slave to disconnect. The slave must have been
0259      *              obtained with a call to getConnectedSlave
0260      *              and must not be assigned to any job.
0261      *
0262      * @return true is successful, false otherwise.
0263      *
0264      * @see getConnectedSlave
0265      * @see assignJobToSlave
0266      * @deprecated since 5.91 Port away from the connected slave feature, e.g. making a library out of the slave code.
0267      */
0268     KIOCORE_DEPRECATED_VERSION(5, 91, "Port away from the connected slave feature, e.g. making a library out of the slave code.")
0269     static bool disconnectSlave(KIO::Slave *slave);
0270 #endif
0271 
0272 #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 103)
0273     /**
0274      * Function to connect signals emitted by the scheduler.
0275      *
0276      * @see slaveConnected()
0277      * @see slaveError()
0278      * @deprecated Since 5.103, due to no known users.
0279      */
0280     KIOCORE_DEPRECATED_VERSION(5, 103, "No known users")
0281     static bool connect(const char *signal, const QObject *receiver, const char *member);
0282 
0283     KIOCORE_DEPRECATED_VERSION(5, 103, "No known users")
0284     static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member);
0285 
0286     KIOCORE_DEPRECATED_VERSION(5, 103, "No known users")
0287     static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member);
0288 
0289     KIOCORE_DEPRECATED_VERSION(5, 103, "No known users")
0290     bool connect(const QObject *sender, const char *signal, const char *member);
0291 #endif
0292 
0293 #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 88)
0294     /**
0295      * When true, the next job will check whether KLauncher has a slave
0296      * on hold that is suitable for the job.
0297      * @param b true when KLauncher has a job on hold
0298      * @deprecated since 5.88, the feature of holding slaves between processes is gone, just remove the call
0299      */
0300     KIOCORE_DEPRECATED_VERSION(5, 88, "Remove this call. The feature is gone")
0301     static void checkSlaveOnHold(bool b);
0302 #endif
0303 
0304     static void emitReparseSlaveConfiguration();
0305     // KF6 TODO: rename to emitReparseWorkerConfiguration. See also T15956.
0306 
0307 #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 101)
0308     /**
0309      * Returns true if there is a slave on hold for @p url.
0310      *
0311      * @since 4.7
0312      * @deprecated Since 5.101, use isWorkerOnHoldFor(const QUrl &)
0313      */
0314     static KIOCORE_DEPRECATED_VERSION(5, 101, "Use isWorkerOnHoldFor(const QUrl &)") bool isSlaveOnHoldFor(const QUrl &url);
0315 #endif
0316 
0317     /**
0318      * Returns true if there is a worker on hold for @p url.
0319      *
0320      * @since 5.101
0321      */
0322     static bool isWorkerOnHoldFor(const QUrl &url);
0323 
0324     /**
0325      * Updates the internal metadata from job.
0326      *
0327      * @since 4.6.5
0328      */
0329     static void updateInternalMetaData(SimpleJob *job);
0330 
0331 Q_SIGNALS:
0332 #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 91)
0333     /**
0334      * @deprecated since 5.91 Port away from the connected slave feature, e.g. making a library out of the slave code.
0335      */
0336     KIOCORE_DEPRECATED_VERSION(5, 91, "Port away from the connected slave feature, e.g. making a library out of the slave code.")
0337     QT_MOC_COMPAT void slaveConnected(KIO::Slave *slave);
0338 
0339     /**
0340      * @deprecated since 5.91 Port away from the connected slave feature, e.g. making a library out of the slave code.
0341      */
0342     KIOCORE_DEPRECATED_VERSION(5, 91, "Port away from the connected slave feature, e.g. making a library out of the slave code.")
0343     QT_MOC_COMPAT void slaveError(KIO::Slave *slave, int error, const QString &errorMsg);
0344 #endif
0345 
0346     // DBUS
0347     Q_SCRIPTABLE void reparseSlaveConfiguration(const QString &);
0348     // KF6 TODO: rename to reparseWorkerConfiguration. See also T15956.
0349 
0350 #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 91)
0351     /**
0352      * @deprecated since 5.91 Port away from the connected slave feature, e.g. making a library out of the slave code.
0353      */
0354     KIOCORE_DEPRECATED_VERSION(5, 91, "Removed for lack of usage, and due to feature removal.")
0355     Q_SCRIPTABLE void slaveOnHoldListChanged();
0356 #endif
0357 
0358 private:
0359     Q_DISABLE_COPY(Scheduler)
0360     KIOCORE_NO_EXPORT Scheduler();
0361     KIOCORE_NO_EXPORT ~Scheduler() override;
0362 
0363     KIOCORE_NO_EXPORT static Scheduler *self();
0364 
0365     friend class AccessManager;
0366     // For internal use from KIOWidgets' KIO::AccessManager, since 5.90
0367     static void setSimpleJobPriority(SimpleJob *job, int priority);
0368 
0369     // connected to D-Bus signal:
0370 #ifndef KIO_ANDROID_STUB
0371     Q_PRIVATE_SLOT(d_func(), void slotReparseSlaveConfiguration(const QString &, const QDBusMessage &))
0372 #endif
0373 
0374 #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 91)
0375     Q_PRIVATE_SLOT(d_func(), void slotSlaveConnected())
0376     Q_PRIVATE_SLOT(d_func(), void slotSlaveError(int error, const QString &errorMsg))
0377 #endif
0378 private:
0379     friend class SchedulerPrivate;
0380     KIOCORE_NO_EXPORT SchedulerPrivate *d_func();
0381 };
0382 
0383 }
0384 #endif