certmanager

storedtransferjob.cpp

00001 /*
00002     Copyright (C) 2004 David Faure <faure@kde.org>
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to
00016     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017     Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "storedtransferjob.h"
00021 
00022 using namespace KIOext;
00023 
00024 #define KIO_ARGS QByteArray packedArgs; QDataStream stream( packedArgs, IO_WriteOnly ); stream
00025 
00026 StoredTransferJob::StoredTransferJob(const KURL& url, int command,
00027                                      const QByteArray &packedArgs,
00028                                      const QByteArray &_staticData,
00029                                      bool showProgressInfo)
00030     : KIO::TransferJob( url, command, packedArgs, _staticData, showProgressInfo ),
00031       m_uploadOffset( 0 )
00032 {
00033     connect( this, SIGNAL( data( KIO::Job *, const QByteArray & ) ),
00034              SLOT( slotData( KIO::Job *, const QByteArray & ) ) );
00035     connect( this, SIGNAL( dataReq( KIO::Job *, QByteArray & ) ),
00036              SLOT( slotDataReq( KIO::Job *, QByteArray & ) ) );
00037 }
00038 
00039 void StoredTransferJob::setData( const QByteArray& arr )
00040 {
00041     Q_ASSERT( m_data.isNull() ); // check that we're only called once
00042     Q_ASSERT( m_uploadOffset == 0 ); // no upload started yet
00043     m_data = arr;
00044 }
00045 
00046 void StoredTransferJob::slotData( KIO::Job *, const QByteArray &data )
00047 {
00048   // check for end-of-data marker:
00049   if ( data.size() == 0 )
00050     return;
00051   unsigned int oldSize = m_data.size();
00052   m_data.resize( oldSize + data.size(), QGArray::SpeedOptim );
00053   memcpy( m_data.data() + oldSize, data.data(), data.size() );
00054 }
00055 
00056 void StoredTransferJob::slotDataReq( KIO::Job *, QByteArray &data )
00057 {
00058   // Inspired from kmail's KMKernel::byteArrayToRemoteFile
00059   // send the data in 64 KB chunks
00060   const int MAX_CHUNK_SIZE = 64*1024;
00061   int remainingBytes = m_data.size() - m_uploadOffset;
00062   if( remainingBytes > MAX_CHUNK_SIZE ) {
00063     // send MAX_CHUNK_SIZE bytes to the receiver (deep copy)
00064     data.duplicate( m_data.data() + m_uploadOffset, MAX_CHUNK_SIZE );
00065     m_uploadOffset += MAX_CHUNK_SIZE;
00066     //kdDebug() << "Sending " << MAX_CHUNK_SIZE << " bytes ("
00067     //                << remainingBytes - MAX_CHUNK_SIZE << " bytes remain)\n";
00068   } else {
00069     // send the remaining bytes to the receiver (deep copy)
00070     data.duplicate( m_data.data() + m_uploadOffset, remainingBytes );
00071     m_data = QByteArray();
00072     m_uploadOffset = 0;
00073     //kdDebug() << "Sending " << remainingBytes << " bytes\n";
00074   }
00075 }
00076 
00078 
00079 StoredTransferJob *KIOext::storedGet( const KURL& url, bool reload, bool showProgressInfo )
00080 {
00081     // Send decoded path and encoded query
00082     KIO_ARGS << url;
00083     StoredTransferJob * job = new StoredTransferJob( url, KIO::CMD_GET, packedArgs, QByteArray(), showProgressInfo );
00084     if (reload)
00085        job->addMetaData("cache", "reload");
00086     return job;
00087 }
00088 
00089 StoredTransferJob *KIOext::put( const QByteArray& arr, const KURL& url, int permissions,
00090                                 bool overwrite, bool resume, bool showProgressInfo )
00091 {
00092     KIO_ARGS << url << Q_INT8( overwrite ? 1 : 0 ) << Q_INT8( resume ? 1 : 0 ) << permissions;
00093     StoredTransferJob * job = new StoredTransferJob( url, KIO::CMD_PUT, packedArgs, QByteArray(), showProgressInfo );
00094     job->setData( arr );
00095     return job;
00096 }
00097 
00098 #include "storedtransferjob.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys