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.

202 lines
4.8KB

  1. /*
  2. * Copyright (C) 2004, Mart Kelder (mart.kde@hccnet.nl)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program 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
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "tdeio_delete.h"
  19. #include "mailid.h"
  20. #include "stringid.h"
  21. #include "tdeio.h"
  22. #include "tdeio_proto.h"
  23. #include <kdebug.h>
  24. #include <tdelocale.h>
  25. #include <kurl.h>
  26. #include <tdeio/global.h>
  27. #include <tdeio/jobclasses.h>
  28. #include <tdeio/scheduler.h>
  29. #include <tqptrlist.h>
  30. TDEIO_Delete::TDEIO_Delete( TQObject * parent, const char * name ) : TQObject( parent, name ),
  31. _tdeio( 0 ),
  32. _total( 0 ),
  33. _jobs( 0 ),
  34. _slave( 0 ),
  35. _valid( true )
  36. {
  37. _jobs = new TQPtrList< TDEIO::Job >;
  38. }
  39. TDEIO_Delete::~TDEIO_Delete( )
  40. {
  41. disConnect( );
  42. delete _jobs;
  43. }
  44. bool TDEIO_Delete::deleteMails( TQPtrList< const KornMailId > * ids, KKioDrop *drop )
  45. {
  46. KURL kurl = *drop->_kurl;
  47. TDEIO::MetaData metadata = *drop->_metadata;
  48. _tdeio = drop;
  49. _valid = true;
  50. //disConnect earlier operations
  51. disConnect( );
  52. if( _tdeio->_protocol->connectionBased( ) )
  53. {
  54. if( ! setupSlave( kurl, metadata, _tdeio->_protocol ) )
  55. {
  56. _valid = false;
  57. return false;
  58. }
  59. }
  60. _total = ids->count( );
  61. for( const KornMailId * item = ids->first(); item; item = ids->next() )
  62. deleteItem( item, kurl, metadata, _tdeio->_protocol );
  63. if( _jobs->count() == 0 )
  64. {
  65. _tdeio->emitDeleteMailsReady( true );
  66. disConnect( );
  67. return true;
  68. }
  69. if( _tdeio->_protocol->commitDelete() )
  70. commitDelete( kurl, metadata, _tdeio->_protocol );
  71. _tdeio->emitDeleteMailsTotalSteps( _total );
  72. return true;
  73. }
  74. void TDEIO_Delete::disConnect( )
  75. {
  76. _jobs->clear( );
  77. if( _slave )
  78. {
  79. TDEIO::Scheduler::disconnectSlave( _slave );
  80. _slave = 0;
  81. }
  82. }
  83. bool TDEIO_Delete::setupSlave( KURL kurl, TDEIO::MetaData metadata, const TDEIO_Protocol *& protocol )
  84. {
  85. protocol->deleteMailConnectKURL( kurl, metadata );
  86. if( kurl.port() == 0 )
  87. kurl.setPort( protocol->defaultPort( _tdeio->_ssl ) );
  88. if( ! ( _slave = TDEIO::Scheduler::getConnectedSlave( kurl, metadata ) ) )
  89. {
  90. kdWarning() << i18n( "Could not get a connected slave; I cannot delete this way..." ) << endl;
  91. _valid = false;
  92. return false;
  93. }
  94. return true;
  95. }
  96. void TDEIO_Delete::deleteItem( const KornMailId *item, KURL kurl, TDEIO::MetaData metadata, const TDEIO_Protocol *& protocol )
  97. {
  98. TDEIO::Job* job = 0;
  99. kurl = dynamic_cast<const KornStringId*>( item )->getId();
  100. protocol->deleteMailKURL( kurl, metadata );
  101. if( kurl.port() == 0 )
  102. kurl.setPort( protocol->defaultPort( _tdeio->_ssl ) );
  103. if( protocol->deleteFunction() == TDEIO_Protocol::get )
  104. {
  105. job = TDEIO::get( kurl, true, false );
  106. if( protocol->connectionBased() )
  107. TDEIO::Scheduler::assignJobToSlave( _slave, dynamic_cast< TDEIO::SimpleJob* >( job ) );
  108. else
  109. TDEIO::Scheduler::scheduleJob( dynamic_cast< TDEIO::SimpleJob* >( job ) );
  110. }
  111. else if( protocol->deleteFunction() == TDEIO_Protocol::del )
  112. {
  113. job = TDEIO::del( kurl, false, false );
  114. }
  115. else
  116. return; //Unknown deleteFunction
  117. connect( job, TQT_SIGNAL( result( TDEIO::Job* ) ), this, TQT_SLOT( slotResult( TDEIO::Job* ) ) );
  118. job->addMetaData( metadata );
  119. _jobs->append( dynamic_cast< TDEIO::Job* >( job ) );
  120. }
  121. /*
  122. * Some protocols needs to a command to commit protocols.
  123. */
  124. void TDEIO_Delete::commitDelete( KURL kurl, TDEIO::MetaData metadata, const TDEIO_Protocol *& protocol )
  125. {
  126. protocol->deleteCommitKURL( kurl, metadata );
  127. if( kurl.port() == 0 )
  128. kurl.setPort( protocol->defaultPort( _tdeio->_ssl ) );
  129. TDEIO::TransferJob *job = TDEIO::get( kurl, true, false );
  130. job->addMetaData( metadata );
  131. connect( job, TQT_SIGNAL( result( TDEIO::Job* ) ), this, TQT_SLOT( slotResult( TDEIO::Job* ) ) );
  132. _jobs->append( dynamic_cast< TDEIO::Job* >( job ) );
  133. if( protocol->connectionBased() )
  134. TDEIO::Scheduler::assignJobToSlave( _slave, job );
  135. else
  136. TDEIO::Scheduler::scheduleJob( job );
  137. _total++;
  138. }
  139. void TDEIO_Delete::canceled( )
  140. {
  141. disConnect( );
  142. }
  143. void TDEIO_Delete::slotResult( TDEIO::Job* job )
  144. {
  145. if( job->error() )
  146. {
  147. kdWarning() << i18n( "An error occurred when deleting email: %1." ).arg( job->errorString() ) << endl;
  148. _valid = false;
  149. }
  150. _jobs->remove( job );
  151. _tdeio->emitDeleteMailsProgress( _total - _jobs->count() );
  152. if( _jobs->isEmpty() )
  153. {
  154. _tdeio->emitDeleteMailsReady( _valid );
  155. disConnect();
  156. }
  157. }
  158. #include "tdeio_delete.moc"