summaryrefslogtreecommitdiffstats
path: root/src/svnqt/svnstream.cpp
blob: 53d115e4fe347d0352fc914cf7435b9817fa74f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/***************************************************************************
 *   Copyright (C) 2006-2007 by Rajko Albrecht                             *
 *   ral@alwins-world.de                                                   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 ***************************************************************************/
#include "svnqt/svnstream.hpp"
#include "svnqt/pool.hpp"
#include "svnqt/apr.hpp"

// Subversion api
#include "svn_client.h"

#include <tqbuffer.h>
#include <tqdatetime.h>
#include <tqfile.h>

#define MAX_TIME 300

namespace svn {

namespace stream {
class SVNTQT_NOEXPORT SvnStream_private
{
public:
    SvnStream_private(){m_Stream=0;m_LastError="";_context=0;cancel_timeout.start();}
    ~SvnStream_private(){tqDebug("Time elapsed: %i ",cancel_timeout.elapsed());}

    static svn_error_t * stream_write(void*baton,const char*data,apr_size_t*len);
    static svn_error_t * stream_read(void*baton,char*data,apr_size_t*len);

    Pool m_Pool;
    svn_stream_t * m_Stream;
    TQString m_LastError;

    svn_client_ctx_t* _context;
    TQTime cancel_timeout;
};

svn_error_t * SvnStream_private::stream_read(void*baton,char*data,apr_size_t*len)
{
    SvnStream*b = (SvnStream*)baton;
    svn_client_ctx_t*ctx = b->context();

    if (ctx&&ctx->cancel_func) {
        SVN_ERR(ctx->cancel_func(ctx->cancel_baton));
    }

    long res = b->isOk()?b->read(data,*len):-1;

    if (res<0) {
        *len = 0;
        return svn_error_create(SVN_ERR_MALFORMED_FILE,0L,b->lastError().TOUTF8());
    }
    *len = res;
    return SVN_NO_ERROR;
}

svn_error_t * SvnStream_private::stream_write(void*baton,const char*data,apr_size_t*len)
{
    SvnStream*b = (SvnStream*)baton;
    svn_client_ctx_t*ctx = b->context();

    if (ctx&&ctx->cancel_func&&b->cancelElapsed()>50) {
        tqDebug("Check cancel");
        SVN_ERR(ctx->cancel_func(ctx->cancel_baton));
        b->cancelTimeReset();
    }

    long res = b->isOk()?b->write(data,*len):-1;
    if (res<0) {
        *len = 0;
        return svn_error_create(SVN_ERR_MALFORMED_FILE,0L,b->lastError().TOUTF8());
    }
    *len = res;
    return SVN_NO_ERROR;
}

SvnStream::SvnStream(bool read, bool write,svn_client_ctx_t * ctx)
{
    m_Data = new SvnStream_private;
    m_Data->m_Stream = svn_stream_create(this,m_Data->m_Pool);
    m_Data->_context = ctx;
    if (read) {
        svn_stream_set_read(m_Data->m_Stream,SvnStream_private::stream_read);
    }
    if (write) {
        svn_stream_set_write(m_Data->m_Stream,SvnStream_private::stream_write);
    }
}

SvnStream::SvnStream()
{
}

SvnStream::~SvnStream()
{
    delete m_Data;
}

int SvnStream::cancelElapsed()const
{
    return m_Data->cancel_timeout.elapsed();
}

void SvnStream::cancelTimeReset()
{
    m_Data->cancel_timeout.restart();
}

SvnStream::operator svn_stream_t* ()const
{
    return m_Data->m_Stream;
}

svn_client_ctx_t * SvnStream::context()
{
    return m_Data->_context;
}

long SvnStream::write(const char*,const unsigned long)
{
    m_Data->m_LastError = "Write not supported with that stream";
    return -1;
}

long SvnStream::read(char*,const unsigned long )
{
    m_Data->m_LastError = "Read not supported with that stream";
    return -1;
}

const TQString&SvnStream::lastError()const
{
    return m_Data->m_LastError;
}

void SvnStream::setError(const TQString&aError)const
{
    m_Data->m_LastError = aError;
}

void SvnStream::setError(int ioError)const
{
   switch (ioError) {
       case IO_Ok:
            setError("Operation was successfull.");
            break;
        case IO_ReadError:
            setError("Could not read from device");
            break;
        case IO_WriteError:
            setError("Could not write to device");
            break;
        case IO_FatalError:
            setError("A fatal unrecoverable error occurred.");
            break;
        case IO_OpenError:
            setError("Could not open device or stream.");
            break;
        case IO_AbortError:
            setError("The operation was unexpectedly aborted.");
            break;
        case IO_TimeOutError:
            setError("The operation timed out.");
            break;
        case IO_UnspecifiedError:
            setError("An unspecified error happened on close.");
            break;
        default:
            setError("Unknown error happend.");
            break;
    }
}

class SvnByteStream_private {
public:
    SvnByteStream_private();
    virtual ~SvnByteStream_private(){}

    TQByteArray m_Content;
    TQBuffer mBuf;
};

SvnByteStream_private::SvnByteStream_private()
    :mBuf(m_Content)
{
    mBuf.open(IO_WriteOnly);
}

/* ByteStream implementation start */
SvnByteStream::SvnByteStream(svn_client_ctx_t * ctx)
    : SvnStream(false,true,ctx)
{
    m_ByteData = new SvnByteStream_private;
    if (!m_ByteData->mBuf.isOpen()) {
        setError(TQT_TQIODEVICE_OBJECT(m_ByteData->mBuf).status());
    }
}

SvnByteStream::~SvnByteStream()
{
    delete m_ByteData;
}

long SvnByteStream::write(const char*aData,const unsigned long max)
{
    long i = m_ByteData->mBuf.writeBlock(aData,max);
    if (i<0) {
        setError(TQT_TQIODEVICE_OBJECT(m_ByteData->mBuf).status());
    }
    return i;
}

TQByteArray SvnByteStream::content()const
{
    return m_ByteData->mBuf.buffer();
}

bool SvnByteStream::isOk()const
{
    return m_ByteData->mBuf.isOpen();
}

/* ByteStream implementation end */

} // namespace stream

} // namespace svn