TDE personal information management applications
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

storedtransferjob.cpp 3.8KB

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