File indexing completed on 2024-04-28 05:42:11

0001 /***************************************************************************
0002  *   Copyright (C) 2006-2009 by Rajko Albrecht                             *
0003  *   ral@alwins-world.de                                                   *
0004  *                                                                         *
0005  * This program is free software; you can redistribute it and/or           *
0006  * modify it under the terms of the GNU Lesser General Public              *
0007  * License as published by the Free Software Foundation; either            *
0008  * version 2.1 of the License, or (at your option) any later version.      *
0009  *                                                                         *
0010  * This program is distributed in the hope that it will be useful,         *
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU       *
0013  * Lesser General Public License for more details.                         *
0014  *                                                                         *
0015  * You should have received a copy of the GNU Lesser General Public        *
0016  * License along with this program (in the file LGPL.txt); if not,         *
0017  * write to the Free Software Foundation, Inc., 51 Franklin St,            *
0018  * Fifth Floor, Boston, MA  02110-1301  USA                                *
0019  *                                                                         *
0020  * This software consists of voluntary contributions made by many          *
0021  * individuals.  For exact contribution history, see the revision          *
0022  * history and logs, available at https://commits.kde.org/kdesvn.          *
0023  ***************************************************************************/
0024 #ifndef SVNSVNSTREAM_HPP
0025 #define SVNSVNSTREAM_HPP
0026 
0027 #include "svnqt/svnqt_defines.h"
0028 
0029 #include <QString>
0030 
0031 #include <svn_io.h>
0032 struct svn_client_ctx_t;
0033 class QBuffer;
0034 
0035 namespace svn
0036 {
0037 
0038 namespace stream
0039 {
0040 class SvnStream_private;
0041 
0042 /**
0043     @author Rajko Albrecht <ral@alwins-world.de>
0044     @short wrapper class around the svn_stream_t structure
0045 */
0046 class SVNQT_EXPORT SvnStream
0047 {
0048     friend class SvnStream_private;
0049 
0050 public:
0051     /* disable default contructor */
0052     SvnStream() = delete;
0053     Q_DISABLE_COPY(SvnStream)
0054 
0055     //! Constructor
0056     /*!
0057      * Setup a svn_stream_t and holds a required pool. The stream will freed
0058      * when deleting this object.
0059      * \param readit set readable
0060      * \param writeit set writable
0061      * \param ctx a client context for calls to cancel_func inside. you should this only set with functions not using it itself
0062      * like svn_client_cat2:
0063      */
0064     explicit SvnStream(bool readit, bool writeit, svn_client_ctx_t *ctx = nullptr);
0065     //! frees all structures and releases memory pool.
0066     virtual ~SvnStream();
0067 
0068     //! operator returning transparent a svn_stream_t structure
0069     /*!
0070         \return a svn_stream_t structure for use with subversion api.
0071      */
0072     operator svn_stream_t *() const;
0073 
0074     //! write operation
0075     /*!
0076         Write data FROM subversion to the class behind. Eg., data comes from
0077         subversion-api and this method has to do something with it (printing on a window, writing to a file)
0078         This implementation always returns -1 (eg, error), must reimplemented for real usage.
0079         \param data the data to written
0080         \param max maximum data to write
0081         \return should return the amount of data real written, in case of error must return -1
0082         \sa setError(int ioError), setError(const QString&error), read(char*data,const unsigned long max)
0083      */
0084     virtual long write(const char *data, const unsigned long max);
0085     //! read operation
0086     /*! implements the wrapper for svn_stream_read, eg. data are read FROM class (eg, file, string or whatever)
0087         into subversion-api. This implementation always returns -1 (eg, error), must reimplemented for real usage.
0088         \param data target array where to store the read
0089         \param max maximum byte count to read
0090         \return amount of data read or -1 in case of error
0091         \sa setError(int ioError), setError(const QString&error), write(const char*data,const unsigned long max)
0092     */
0093     virtual long read(char *data, const unsigned long max);
0094 
0095     //! returns the error set
0096     /*!
0097         \return a human readable message about the reason the last operation failed.
0098      */
0099     virtual const QString &lastError() const;
0100     //! is that stream usable
0101     /*!
0102         Gives information about if the stream object is usable. May if the file is real open or such.
0103         \return true if stream is usable, false if not.
0104      */
0105     virtual bool isOk() const = 0;
0106 
0107     svn_client_ctx_t *context();
0108 
0109 protected:
0110     //! set a human readable errormessage
0111     /*!
0112         This message may printed to the user and will checked if one of the stream-operations failed. So should set from
0113         write and/or read if them will return -1 (for error)
0114         \param error the errormessage assigned.
0115      */
0116     virtual void setError(const QString &error) const;
0117 
0118 protected:
0119     int cancelElapsed() const;
0120     void cancelTimeReset();
0121 
0122 private:
0123     SvnStream_private *m_Data;
0124 };
0125 
0126 //! a class let subversion print into a QByteArray
0127 class SVNQT_EXPORT SvnByteStream : public SvnStream
0128 {
0129 public:
0130     //! constructor
0131     /*!
0132         creates internal buffer
0133      * \param ctx a client context for calls to cancel_func inside. you should this only set with functions not using it itself
0134      * like svn_client_cat2:
0135      */
0136     explicit SvnByteStream(svn_client_ctx_t *ctx = nullptr);
0137     //! release internal buffer
0138     ~SvnByteStream();
0139     //! fill internal buffer with data
0140     /*!
0141         stores the data written into the internal buffer.
0142         \param data data to store
0143         \param max length of data to store
0144         \return data real stored or -1 if error.
0145     */
0146     long write(const char *data, const unsigned long max) override final;
0147 
0148     //! return the data stored
0149     /*!
0150         \return the internal stored data
0151     */
0152     QByteArray content() const;
0153     //! checks if the buffer is usable.
0154     /*!
0155      * \return true if data may written, false if not, in that case a errormessage will set.
0156      */
0157     bool isOk() const override final;
0158 
0159 private:
0160     QBuffer *m_ByteData;
0161 };
0162 
0163 } // namespace stream
0164 
0165 } // namespace svn
0166 
0167 #endif