certmanager

storedtransferjob.cpp
1 /*
2  Copyright (C) 2004 David Faure <faure@kde.org>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "storedtransferjob.h"
21 
22 using namespace TDEIOext;
23 
24 #define TDEIO_ARGS TQByteArray packedArgs; TQDataStream stream( packedArgs, IO_WriteOnly ); stream
25 
26 StoredTransferJob::StoredTransferJob(const KURL& url, int command,
27  const TQByteArray &packedArgs,
28  const TQByteArray &_staticData,
29  bool showProgressInfo)
30  : TDEIO::TransferJob( url, command, packedArgs, _staticData, showProgressInfo ),
31  m_uploadOffset( 0 )
32 {
33  connect( this, TQ_SIGNAL( data( TDEIO::Job *, const TQByteArray & ) ),
34  TQ_SLOT( slotData( TDEIO::Job *, const TQByteArray & ) ) );
35  connect( this, TQ_SIGNAL( dataReq( TDEIO::Job *, TQByteArray & ) ),
36  TQ_SLOT( slotDataReq( TDEIO::Job *, TQByteArray & ) ) );
37 }
38 
39 void StoredTransferJob::setData( const TQByteArray& arr )
40 {
41  Q_ASSERT( m_data.isNull() ); // check that we're only called once
42  Q_ASSERT( m_uploadOffset == 0 ); // no upload started yet
43  m_data = arr;
44 }
45 
46 void StoredTransferJob::slotData( TDEIO::Job *, const TQByteArray &data )
47 {
48  // check for end-of-data marker:
49  if ( data.size() == 0 )
50  return;
51  unsigned int oldSize = m_data.size();
52  m_data.resize( oldSize + data.size(), TQGArray::SpeedOptim );
53  memcpy( m_data.data() + oldSize, data.data(), data.size() );
54 }
55 
56 void StoredTransferJob::slotDataReq( TDEIO::Job *, TQByteArray &data )
57 {
58  // Inspired from kmail's KMKernel::byteArrayToRemoteFile
59  // send the data in 64 KB chunks
60  const int MAX_CHUNK_SIZE = 64*1024;
61  int remainingBytes = m_data.size() - m_uploadOffset;
62  if( remainingBytes > MAX_CHUNK_SIZE ) {
63  // send MAX_CHUNK_SIZE bytes to the receiver (deep copy)
64  data.duplicate( m_data.data() + m_uploadOffset, MAX_CHUNK_SIZE );
65  m_uploadOffset += MAX_CHUNK_SIZE;
66  //kdDebug() << "Sending " << MAX_CHUNK_SIZE << " bytes ("
67  // << remainingBytes - MAX_CHUNK_SIZE << " bytes remain)\n";
68  } else {
69  // send the remaining bytes to the receiver (deep copy)
70  data.duplicate( m_data.data() + m_uploadOffset, remainingBytes );
71  m_data = TQByteArray();
72  m_uploadOffset = 0;
73  //kdDebug() << "Sending " << remainingBytes << " bytes\n";
74  }
75 }
76 
78 
79 StoredTransferJob *TDEIOext::storedGet( const KURL& url, bool reload, bool showProgressInfo )
80 {
81  // Send decoded path and encoded query
82  TDEIO_ARGS << url;
83  StoredTransferJob * job = new StoredTransferJob( url, TDEIO::CMD_GET, packedArgs, TQByteArray(), showProgressInfo );
84  if (reload)
85  job->addMetaData("cache", "reload");
86  return job;
87 }
88 
89 StoredTransferJob *TDEIOext::put( const TQByteArray& arr, const KURL& url, int permissions,
90  bool overwrite, bool resume, bool showProgressInfo )
91 {
92  TDEIO_ARGS << url << TQ_INT8( overwrite ? 1 : 0 ) << TQ_INT8( resume ? 1 : 0 ) << permissions;
93  StoredTransferJob * job = new StoredTransferJob( url, TDEIO::CMD_PUT, packedArgs, TQByteArray(), showProgressInfo );
94  job->setData( arr );
95  return job;
96 }
97 
98 #include "storedtransferjob.moc"
StoredTransferJob is a TransferJob (for downloading or uploading data) that also stores a TQByteArray...
StoredTransferJob(const KURL &url, int command, const TQByteArray &packedArgs, const TQByteArray &_staticData, bool showProgressInfo)
Do not create a StoredTransferJob.
TQByteArray data() const
Get hold of the downloaded data.
void setData(const TQByteArray &arr)
Set data to be uploaded.