File indexing completed on 2024-04-14 03:52:50

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 worker_p.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 "worker_p.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::Worker
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     // pure virtual methods that are defined by the actual protocol
0052     virtual void get(const QUrl &url) = 0;
0053     virtual void mimetype(const QUrl &url) = 0;
0054 
0055 protected:
0056     /**
0057      * Sets metadata
0058      * @internal
0059      */
0060     void setAllMetaData(const MetaData &);
0061     /**
0062      * Sends metadata set with setAllMetaData
0063      * @internal
0064      */
0065     void sendMetaData();
0066 
0067     // queuing methods
0068     /** identifiers of functions to be queued */
0069     enum QueueType {
0070         Queue_mimeType = 1,
0071         Queue_totalSize,
0072         Queue_sendMetaData,
0073         Queue_data,
0074         Queue_finished,
0075     };
0076     /** structure for queuing. It is very primitive, it doesn't
0077      * even try to conserve memory.
0078      */
0079     struct QueueStruct {
0080         QueueType type;
0081         QString s;
0082         KIO::filesize_t size;
0083         QByteArray ba;
0084 
0085         QueueStruct()
0086         {
0087         }
0088         QueueStruct(QueueType type)
0089             : type(type)
0090         {
0091         }
0092     };
0093     typedef QList<QueueStruct> DispatchQueue;
0094     DispatchQueue dispatchQueue;
0095 
0096     DISPATCH_DECL1(mimeType, const QString &, s)
0097     DISPATCH_DECL1(totalSize, KIO::filesize_t, size)
0098     DISPATCH_DECL(sendMetaData)
0099     DISPATCH_DECL1(data, const QByteArray &, ba)
0100     DISPATCH_DECL(finished)
0101 
0102 protected Q_SLOTS:
0103     /** dispatches next queued method. Does nothing if there are no
0104      * queued methods.
0105      */
0106     void dispatchNext();
0107 
0108 private:
0109     MetaData meta_data;
0110     bool _suspended;
0111     QTimer *timer;
0112 };
0113 
0114 }
0115 
0116 #undef DISPATCH_DECL
0117 #undef DISPATCH_DECL1
0118 
0119 #endif