File indexing completed on 2024-05-12 17:16:25

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 http://kdesvn.alwins-world.de.           *
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 public:
0050     /* disable default contructor */
0051     SvnStream() = delete;
0052     Q_DISABLE_COPY(SvnStream)
0053 
0054     //! Constructor
0055     /*!
0056      * Setup a svn_stream_t and holds a required pool. The stream will freed
0057      * when deleting this object.
0058      * \param readit set readable
0059      * \param writeit set writable
0060      * \param ctx a client context for calls to cancel_func inside. you should this only set with functions not using it itself
0061      * like svn_client_cat2:
0062      */
0063     explicit SvnStream(bool readit, bool writeit, svn_client_ctx_t *ctx = nullptr);
0064     //! frees all structures and releases memory pool.
0065     virtual ~SvnStream();
0066 
0067     //! operator returning transparent a svn_stream_t structure
0068     /*!
0069         \return a svn_stream_t structure for use with subversion api.
0070      */
0071     operator svn_stream_t *()const;
0072 
0073     //! write operation
0074     /*!
0075         Write data FROM subversion to the class behind. Eg., data comes from
0076         subversion-api and this method has to do something with it (printing on a window, writing to a file)
0077         This implementation always returns -1 (eg, error), must reimplemented for real usage.
0078         \param data the data to written
0079         \param max maximum data to write
0080         \return should return the amount of data real written, in case of error must return -1
0081         \sa setError(int ioError), setError(const QString&error), read(char*data,const unsigned long max)
0082      */
0083     virtual long write(const char *data, const unsigned long max);
0084     //! read operation
0085     /*! implements the wrapper for svn_stream_read, eg. data are read FROM class (eg, file, string or whatever)
0086         into subversion-api. This implementation always returns -1 (eg, error), must reimplemented for real usage.
0087         \param data target array where to store the read
0088         \param max maximum byte count to read
0089         \return amount of data read or -1 in case of error
0090         \sa setError(int ioError), setError(const QString&error), write(const char*data,const unsigned long max)
0091     */
0092     virtual long read(char *data, const unsigned long max);
0093 
0094     //! returns the error set
0095     /*!
0096         \return a human readable message about the reason the last operation failed.
0097      */
0098     virtual const QString &lastError()const;
0099     //! is that stream usable
0100     /*!
0101         Gives information about if the stream object is usable. May if the file is real open or such.
0102         \return true if stream is usable, false if not.
0103      */
0104     virtual bool isOk()const = 0;
0105 
0106     svn_client_ctx_t *context();
0107 
0108 protected:
0109     //! set a human readable errormessage
0110     /*!
0111         This message may printed to the user and will checked if one of the stream-operations failed. So should set from
0112         write and/or read if them will return -1 (for error)
0113         \param error the errormessage assigned.
0114      */
0115     virtual void setError(const QString &error)const;
0116 
0117 protected:
0118     int cancelElapsed()const;
0119     void cancelTimeReset();
0120 
0121 private:
0122     SvnStream_private *m_Data;
0123 };
0124 
0125 //! a class let subversion print into a QByteArray
0126 class SVNQT_EXPORT SvnByteStream: public SvnStream
0127 {
0128 public:
0129     //! constructor
0130     /*!
0131         creates internal buffer
0132      * \param ctx a client context for calls to cancel_func inside. you should this only set with functions not using it itself
0133      * like svn_client_cat2:
0134      */
0135     explicit SvnByteStream(svn_client_ctx_t *ctx = nullptr);
0136     //! release internal buffer
0137     ~SvnByteStream();
0138     //! fill internal buffer with data
0139     /*!
0140         stores the data written into the internal buffer.
0141         \param data data to store
0142         \param max length of data to store
0143         \return data real stored or -1 if error.
0144     */
0145     long write(const char *data, const unsigned long max) override final;
0146 
0147     //! return the data stored
0148     /*!
0149         \return the internal stored data
0150     */
0151     QByteArray content() const;
0152     //! checks if the buffer is usable.
0153     /*!
0154      * \return true if data may written, false if not, in that case a errormessage will set.
0155      */
0156     bool isOk() const override final;
0157 
0158 private:
0159     QBuffer *m_ByteData;
0160 };
0161 
0162 } // namespace stream
0163 
0164 } // namespace svn
0165 
0166 #endif