File indexing completed on 2024-04-28 03:55:19

0001 /*
0002     This file is part of the KDE project
0003     SPDX-FileCopyrightText: 2000 David Faure <faure@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef KIO_WORKERINTERFACE_P_H
0009 #define KIO_WORKERINTERFACE_P_H
0010 
0011 #include <qplatformdefs.h>
0012 
0013 #include <QHostInfo>
0014 #include <QObject>
0015 #include <QTimer>
0016 
0017 #include "connection_p.h"
0018 #include "global.h"
0019 #include "metadata.h"
0020 #include "udsentry.h"
0021 
0022 class QUrl;
0023 
0024 namespace KIO
0025 {
0026 
0027 // Definition of enum Command has been moved to global.h
0028 
0029 /**
0030  * Identifiers for KIO informational messages.
0031  */
0032 enum Info {
0033     INF_TOTAL_SIZE = 10,
0034     INF_PROCESSED_SIZE = 11,
0035     INF_SPEED,
0036     INF_REDIRECTION = 20,
0037     INF_MIME_TYPE = 21,
0038     INF_ERROR_PAGE = 22,
0039     INF_WARNING = 23,
0040     INF_UNUSED = 25, ///< now unused
0041     INF_INFOMESSAGE,
0042     INF_META_DATA,
0043     INF_MESSAGEBOX,
0044     INF_POSITION,
0045     INF_TRUNCATED,
0046     INF_SSLERROR,
0047     // add new ones here once a release is done, to avoid breaking binary compatibility
0048 };
0049 
0050 /**
0051  * Identifiers for KIO data messages.
0052  */
0053 enum Message {
0054     MSG_DATA = 100,
0055     MSG_DATA_REQ,
0056     MSG_ERROR,
0057     MSG_CONNECTED,
0058     MSG_FINISHED,
0059     MSG_STAT_ENTRY, // 105
0060     MSG_LIST_ENTRIES,
0061     MSG_RENAMED, ///< unused
0062     MSG_RESUME,
0063     MSG_CANRESUME,
0064     MSG_OPENED,
0065     MSG_WRITTEN,
0066     MSG_HOST_INFO_REQ,
0067     MSG_PRIVILEGE_EXEC,
0068     MSG_WORKER_STATUS,
0069     // add new ones here once a release is done, to avoid breaking binary compatibility
0070 };
0071 
0072 /**
0073  * @class KIO::WorkerInterface workerinterface_p.h <KIO/WorkerInterface>
0074  *
0075  * There are two classes that specifies the protocol between application
0076  * ( KIO::Job) and kioworker. WorkerInterface is the class to use on the application
0077  * end, WorkerBase is the one to use on the worker end.
0078  *
0079  * A call to foo() results in a call to slotFoo() on the other end.
0080  */
0081 class WorkerInterface : public QObject
0082 {
0083     Q_OBJECT
0084 
0085 protected:
0086     explicit WorkerInterface(QObject *parent = nullptr);
0087 
0088 public:
0089     ~WorkerInterface() override;
0090 
0091     // Send our answer to the MSG_RESUME (canResume) request
0092     // (to tell the "put" job whether to resume or not)
0093     void sendResumeAnswer(bool resume);
0094 
0095     /**
0096      * Sends our answer for the INF_MESSAGEBOX request.
0097      *
0098      */
0099     void sendMessageBoxAnswer(int result);
0100 
0101     void sendSslErrorAnswer(int result);
0102 
0103     void setOffset(KIO::filesize_t offset);
0104     KIO::filesize_t offset() const;
0105 
0106 Q_SIGNALS:
0107     ///////////
0108     // Messages sent by the worker
0109     ///////////
0110 
0111     void data(const QByteArray &);
0112     void dataReq();
0113     void error(int, const QString &);
0114     void connected();
0115     void finished();
0116     void workerStatus(qint64, const QByteArray &, const QString &, bool);
0117     void listEntries(const KIO::UDSEntryList &);
0118     void statEntry(const KIO::UDSEntry &);
0119 
0120     void canResume(KIO::filesize_t);
0121 
0122     void open();
0123     void written(KIO::filesize_t);
0124     void close();
0125 
0126     void privilegeOperationRequested();
0127 
0128     ///////////
0129     // Info sent by the worker
0130     //////////
0131     void metaData(const KIO::MetaData &);
0132     void totalSize(KIO::filesize_t);
0133     void processedSize(KIO::filesize_t);
0134     void redirection(const QUrl &);
0135     void position(KIO::filesize_t);
0136     void truncated(KIO::filesize_t);
0137 
0138     void speed(unsigned long);
0139     void errorPage();
0140     void mimeType(const QString &);
0141     void warning(const QString &);
0142     void infoMessage(const QString &);
0143     // void connectFinished(); //it does not get emitted anywhere
0144 
0145 protected:
0146     /////////////////
0147     // Dispatching
0148     ////////////////
0149 
0150     virtual bool dispatch();
0151     virtual bool dispatch(int _cmd, const QByteArray &data);
0152 
0153     void messageBox(int type, const QString &text, const QString &title, const QString &primaryActionText, const QString &secondaryActionText);
0154 
0155     void messageBox(int type,
0156                     const QString &text,
0157                     const QString &title,
0158                     const QString &primaryActionText,
0159                     const QString &secondaryActionText,
0160                     const QString &dontAskAgainName);
0161 
0162 protected Q_SLOTS:
0163     void calcSpeed();
0164 
0165 private Q_SLOTS:
0166     void slotHostInfo(const QHostInfo &info);
0167 
0168 protected:
0169     Connection *m_connection = nullptr;
0170 
0171     // We need some metadata here for our SSL code in messageBox() and for sslMetaData().
0172     MetaData m_sslMetaData;
0173 
0174 private:
0175     QTimer m_speed_timer;
0176 
0177     // Used to cache privilege operation details passed from the worker by the metadata hack
0178     // See WORKER_MESSAGEBOX_DETAILS_HACK
0179     QString m_messageBoxDetails;
0180 
0181     static const unsigned int max_nums = 8;
0182     KIO::filesize_t m_sizes[max_nums];
0183     qint64 m_times[max_nums];
0184 
0185     KIO::filesize_t m_filesize = 0;
0186     KIO::filesize_t m_offset = 0;
0187     size_t m_last_time = 0;
0188     qint64 m_start_time = 0;
0189     uint m_nums = 0;
0190     bool m_worker_calcs_speed = false;
0191 };
0192 
0193 }
0194 
0195 #endif