File indexing completed on 2024-06-16 07:42:24

0001 /*
0002     SPDX-FileCopyrightText: 1998-2008 Sebastian Trueg <trueg@k3b.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #ifndef _K3B_ACTIVE_PIPE_H_
0007 #define _K3B_ACTIVE_PIPE_H_
0008 
0009 #include "k3b_export.h"
0010 
0011 #include <QIODevice>
0012 
0013 
0014 namespace K3b {
0015     /**
0016      * The active pipe pumps data from a source to a sink using an
0017      * additional thread.
0018      *
0019      * The active pumping is only performed if both the source and sink
0020      * QIODevices are set. Otherwise the pipe only serves as a conduit for
0021      * data streams. The latter is mostly interesting when using the ChecksumPipe
0022      * in combination with a Job that can only push data (like the DataTrackReader).
0023      */
0024     class LIBK3B_EXPORT ActivePipe : public QIODevice
0025     {
0026         Q_OBJECT
0027 
0028     public:
0029         ActivePipe();
0030         ~ActivePipe() override;
0031 
0032         /**
0033          * Opens the pipe and thus starts the
0034          * pumping.
0035          *
0036          * \param closeWhenDone If true the pipes will be closed
0037          *        once all data has been read.
0038          */
0039         virtual bool open( bool closeWhenDone = false );
0040 
0041         /**
0042          * Close the pipe
0043          */
0044         void close() override;
0045 
0046         /**
0047          * Read from a QIODevice instead of a file descriptor.
0048          * The device will be opened QIODevice::ReadOnly and closed
0049          * afterwards.
0050          *
0051          * \param close If true the device will be closed once close() is called.
0052          */
0053         void readFrom( QIODevice* dev, bool close = false );
0054 
0055         /**
0056          * Write to a QIODevice instead of using the readyRead signal.
0057          * The device will be opened QIODevice::WriteOnly and closed
0058          * afterwards.
0059          *
0060          * \param close If true the device will be closed once close() is called.
0061          */
0062         void writeTo( QIODevice* dev, bool close = false );
0063 
0064         /**
0065          * The number of bytes that have been read.
0066          */
0067         quint64 bytesRead() const;
0068 
0069         /**
0070          * The number of bytes that have been written.
0071          */
0072         quint64 bytesWritten() const;
0073 
0074     protected:
0075         /**
0076          * Reads the data from the source.
0077          * The default implementation reads from the file desc
0078          * set via readFromFd or from in()
0079          */
0080         qint64 readData( char* data, qint64 max ) override;
0081 
0082         /**
0083          * Write the data to the sink.
0084          * The default implementation writes to the file desc
0085          * set via writeToFd or out()
0086          *
0087          * Can be reimplememented to further process the data.
0088          */
0089         qint64 writeData( const char* data, qint64 max ) override;
0090 
0091         /**
0092          * Hidden open method. Use open(bool).
0093          */
0094         bool open( OpenMode mode ) override;
0095 
0096     private:
0097         class Private;
0098         Private* d;
0099 
0100         Q_PRIVATE_SLOT( d, void _k3b_close() )
0101     };
0102 }
0103 
0104 #endif