File indexing completed on 2024-04-28 11:40:52

0001 // -*- c++ -*-
0002 /*
0003     This file is part of the KDE libraries
0004     SPDX-FileCopyrightText: 2003 Leo Savernik <l.savernik@aon.at>
0005     Derived from slave.h
0006 
0007     SPDX-License-Identifier: LGPL-2.0-only
0008 */
0009 
0010 #ifndef KIO_DATAWORKER_P_H
0011 #define KIO_DATAWORKER_P_H
0012 
0013 #include "global.h"
0014 #include "slave.h"
0015 
0016 class QTimer;
0017 
0018 // don't forget to sync DISPATCH_IMPL in dataworker.cpp
0019 #define DISPATCH_DECL(type) void dispatch_##type();
0020 
0021 // don't forget to sync DISPATCH_IMPL1 in dataworker.cpp
0022 #define DISPATCH_DECL1(type, paramtype, param) void dispatch_##type(paramtype param);
0023 
0024 namespace KIO
0025 {
0026 /**
0027  * This class provides a high performance implementation for the data
0028  * url scheme (rfc2397).
0029  *
0030  * @internal
0031  * Do not use this class in external applications. It is an implementation
0032  * detail of KIO and subject to change without notice.
0033  * @author Leo Savernik
0034  */
0035 class DataWorker : public KIO::Slave
0036 {
0037     Q_OBJECT
0038 public:
0039     DataWorker();
0040 
0041     ~DataWorker() override;
0042 
0043     virtual void setHost(const QString &host, quint16 port, const QString &user, const QString &passwd) override;
0044     void setConfig(const MetaData &config) override;
0045 
0046     void suspend() override;
0047     void resume() override;
0048     bool suspended() override;
0049     void send(int cmd, const QByteArray &arr = QByteArray()) override;
0050 
0051     void hold(const QUrl &url) override;
0052 
0053     // pure virtual methods that are defined by the actual protocol
0054     virtual void get(const QUrl &url) = 0;
0055     virtual void mimetype(const QUrl &url) = 0;
0056 
0057 protected:
0058     /**
0059      * Sets metadata
0060      * @internal
0061      */
0062     void setAllMetaData(const MetaData &);
0063     /**
0064      * Sends metadata set with setAllMetaData
0065      * @internal
0066      */
0067     void sendMetaData();
0068 
0069     // queuing methods
0070     /** identifiers of functions to be queued */
0071     enum QueueType {
0072         Queue_mimeType = 1,
0073         Queue_totalSize,
0074         Queue_sendMetaData,
0075         Queue_data,
0076         Queue_finished,
0077     };
0078     /** structure for queuing. It is very primitive, it doesn't
0079      * even try to conserve memory.
0080      */
0081     struct QueueStruct {
0082         QueueType type;
0083         QString s;
0084         KIO::filesize_t size;
0085         QByteArray ba;
0086 
0087         QueueStruct()
0088         {
0089         }
0090         QueueStruct(QueueType type)
0091             : type(type)
0092         {
0093         }
0094     };
0095     typedef QList<QueueStruct> DispatchQueue;
0096     DispatchQueue dispatchQueue;
0097 
0098     DISPATCH_DECL1(mimeType, const QString &, s)
0099     DISPATCH_DECL1(totalSize, KIO::filesize_t, size)
0100     DISPATCH_DECL(sendMetaData)
0101     DISPATCH_DECL1(data, const QByteArray &, ba)
0102     DISPATCH_DECL(finished)
0103 
0104 protected Q_SLOTS:
0105     /** dispatches next queued method. Does nothing if there are no
0106      * queued methods.
0107      */
0108     void dispatchNext();
0109 
0110 private:
0111     MetaData meta_data;
0112     bool _suspended;
0113     QTimer *timer;
0114 };
0115 
0116 }
0117 
0118 #undef DISPATCH_DECL
0119 #undef DISPATCH_DECL1
0120 
0121 #endif