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.

tdeio_count.cpp 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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_count.h"
  19. #include "tdeio.h"
  20. #include "tdeio_proto.h"
  21. #include "tdeio_single_subject.h"
  22. #include "mailsubject.h"
  23. #include "sortedmailsubject.h"
  24. #include <kdebug.h>
  25. #include <tdeio/jobclasses.h>
  26. #include <tdeio/scheduler.h>
  27. #include <tdeio/slave.h>
  28. #include <tdeio/global.h>
  29. #include <kurl.h>
  30. #include <tqvaluelist.h>
  31. #include <tqstring.h>
  32. TDEIO_Count::TDEIO_Count( TQObject * parent, const char * name )
  33. : TQObject ( parent, name ),
  34. _kurl( 0 ),
  35. _metadata( 0 ),
  36. _protocol( 0 ),
  37. _valid( true ),
  38. _new_mailurls( 0 ),
  39. _subjects_pending( 0 ),
  40. _total_new_messages( 0 ),
  41. _popup_subjects( 0 )
  42. {
  43. }
  44. TDEIO_Count::~TDEIO_Count()
  45. {
  46. // Delete copies of urls.
  47. delete _kurl;
  48. delete _metadata;
  49. }
  50. void TDEIO_Count::count( KKioDrop *drop )
  51. {
  52. if( _new_mailurls )
  53. return; //A counting is pending, so no new one is started.
  54. delete _kurl;
  55. delete _metadata;
  56. _tdeio = drop;
  57. /*
  58. * Saving current settings: all actions are asynchroon, so if someone
  59. * use slow servers, settings could been changed before this class is
  60. * finished with counten. To be able to track back te staring values;
  61. * these are saved in the class.
  62. */
  63. _kurl = new KURL( *_tdeio->_kurl );
  64. _metadata = new TDEIO::MetaData( *_tdeio->_metadata );
  65. _protocol = _tdeio->_protocol;
  66. KURL kurl = *_kurl;
  67. TDEIO::MetaData metadata = *_metadata;
  68. // Serup a connection
  69. if( _protocol->connectionBased( ) )
  70. {
  71. _protocol->recheckConnectKURL( kurl, metadata );
  72. if( kurl.port() == 0 )
  73. kurl.setPort( _protocol->defaultPort( _tdeio->_ssl ) );
  74. if( ! ( _slave = TDEIO::Scheduler::getConnectedSlave( kurl, metadata ) ) ) //Forcing reload
  75. {
  76. kdWarning() << i18n( "Not able to open a tdeio slave for %1." ).arg( _protocol->configName() ) << endl;
  77. _tdeio->emitShowPassivePopup( i18n( "Not able to open a tdeio slave for %1." ).arg( _protocol->configName() ) );
  78. _valid = false;
  79. _tdeio->emitValidChanged();
  80. _slave = 0;
  81. //delete _new_mailurls; _new_mailurls = 0; //No connection pending
  82. return;
  83. }
  84. connect( _slave, TQT_SIGNAL( error( int, const TQString& ) ), _tdeio, TQT_SLOT( slotConnectionError( int, const TQString& ) ) );
  85. connect( _slave, TQT_SIGNAL( warning( const TQString& ) ), _tdeio, TQT_SLOT( slotConnectionWarning( const TQString& ) ) );
  86. connect( _slave, TQT_SIGNAL( infoMessage( const TQString& ) ), _tdeio, TQT_SLOT( slotConnectionInfoMessage( const TQString& ) ) );
  87. /*
  88. * _protocol->recheckConnectKURL could have change kurl and metadata in order to have the right
  89. * settings to connect. But some other functions assumed unmodified settings,
  90. * so the settings are set back to his originals.
  91. */
  92. kurl = *_kurl;
  93. metadata = *_metadata;
  94. }
  95. else
  96. {
  97. _slave = 0; //Prevent disconnecting not-existing slave
  98. }
  99. /* Blocking this function: no new counts can be started from now */
  100. _new_mailurls = new TQValueList< KKioDrop::FileInfo >;
  101. _protocol->recheckKURL( kurl, metadata );
  102. if( kurl.port() == 0 )
  103. kurl.setPort( _protocol->defaultPort( _tdeio->_ssl ) );
  104. //Making job to fetch file-list
  105. _job = TDEIO::listDir( kurl, false );
  106. _job->addMetaData( metadata );
  107. connect( _job, TQT_SIGNAL( result( TDEIO::Job* ) ), this, TQT_SLOT( result( TDEIO::Job* ) ) );
  108. connect( _job, TQT_SIGNAL( entries( TDEIO::Job*, const TDEIO::UDSEntryList& ) ),
  109. this, TQT_SLOT( entries( TDEIO::Job*, const TDEIO::UDSEntryList& ) ) );
  110. if( _protocol->connectionBased() )
  111. TDEIO::Scheduler::assignJobToSlave( _slave, _job );
  112. else
  113. TDEIO::Scheduler::scheduleJob( _job );
  114. }
  115. void TDEIO_Count::stopActiveCount()
  116. {
  117. if( !_new_mailurls )
  118. return;
  119. disconnect( _job, TQT_SIGNAL( result( TDEIO::Job* ) ), this, TQT_SLOT( result( TDEIO::Job* ) ) );
  120. disconnect( _job, TQT_SIGNAL( entries( TDEIO::Job*, const TDEIO::UDSEntryList& ) ),
  121. this, TQT_SLOT( entries( TDEIO::Job*, const TDEIO::UDSEntryList& ) ) );
  122. TDEIO::Scheduler::cancelJob( _job );
  123. if( _slave )
  124. {
  125. //Slave seems to be disconnected by canceling the last job of the slave
  126. //TDEIO::Scheduler::disconnectSlave( _slave );
  127. _slave = 0;
  128. }
  129. //Deletings settings
  130. delete _kurl; _kurl = 0;
  131. delete _metadata; _metadata = 0;
  132. delete _new_mailurls; _new_mailurls = 0;
  133. }
  134. void TDEIO_Count::showPassive( const TQString& id )
  135. {
  136. KURL kurl = *_tdeio->_kurl;
  137. TDEIO::MetaData metadata = *_tdeio->_metadata;
  138. kurl = id;
  139. //TDEIO::Slave *slave = 0;
  140. _tdeio->_protocol->readSubjectKURL( kurl, metadata );
  141. if( kurl.port() == 0 )
  142. kurl.setPort( _tdeio->_protocol->defaultPort( _tdeio->_ssl ) );
  143. TDEIO_Single_Subject *subject = new TDEIO_Single_Subject( this, id.latin1(), kurl, metadata, _tdeio->_protocol, _slave, id, 0 );
  144. _subjects_pending++;
  145. connect( subject, TQT_SIGNAL( readSubject( KornMailSubject* ) ), this, TQT_SLOT( addtoPassivePopup( KornMailSubject* ) ) );
  146. connect( subject, TQT_SIGNAL( finished( TDEIO_Single_Subject* ) ), this, TQT_SLOT( deleteSingleSubject( TDEIO_Single_Subject* ) ) );
  147. }
  148. void TDEIO_Count::disconnectSlave()
  149. {
  150. if( _subjects_pending > 0 )
  151. return; //Still getting data
  152. if( !_protocol->connectionBased() )
  153. return; //Protocol doesn't have a connection
  154. if( !_slave )
  155. return; //Slave doens't exist
  156. //Disconnect slave
  157. TDEIO::Scheduler::disconnectSlave( _slave );
  158. _slave = 0;
  159. _protocol = 0;
  160. }
  161. //This function is called when fetching is over
  162. void TDEIO_Count::result( TDEIO::Job* job )
  163. {
  164. //job should be the latest job; elsewise: print an error.
  165. if( job != _job )
  166. kdError() << i18n( "Got unknown job; something must be wrong..." ) << endl;
  167. //look of an error occurred. If there is, print the error.
  168. //This could be very useful by resolving bugs.
  169. if( job->error() )
  170. {
  171. kdError() << i18n( "The next TDEIO-error occurred by counting: %1" ).arg( job->errorString() ) << endl;
  172. _tdeio->emitShowPassivePopup( i18n( "The next TDEIO-error occurred by counting: %1" ).arg( job->errorString() ) );
  173. _valid = false;
  174. _tdeio->emitValidChanged();
  175. }
  176. disconnect( job, TQT_SIGNAL( result( TDEIO::Job* ) ), this, TQT_SLOT( result( TDEIO::Job* ) ) );
  177. disconnect( job, TQT_SIGNAL( entries( TDEIO::Job*, const TDEIO::UDSEntryList& ) ),
  178. this, TQT_SLOT( entries( TDEIO::Job*, const TDEIO::UDSEntryList& ) ) );
  179. disconnectSlave();
  180. //Deletings settings
  181. delete _kurl; _kurl = 0;
  182. delete _metadata; _metadata = 0;
  183. if( _tdeio->_mailurls->count() != _new_mailurls->count() )
  184. {
  185. *_tdeio->_mailurls = *_new_mailurls;
  186. _tdeio->emitChanged(_tdeio->_mailurls->count());
  187. }
  188. else
  189. {
  190. *_tdeio->_mailurls = *_new_mailurls;
  191. }
  192. delete _new_mailurls; _new_mailurls = 0;
  193. _valid = true;
  194. _tdeio->emitValidChanged();
  195. _tdeio->emitRechecked();
  196. }
  197. //An file list is ready; now save it in _tdeio->_mailurls.
  198. void TDEIO_Count::entries( TDEIO::Job* job, const TDEIO::UDSEntryList &list )
  199. {
  200. TQStringList old_list;
  201. TDEIO::UDSEntryListConstIterator it1 ;
  202. TDEIO::UDSEntry::ConstIterator it2 ;
  203. TDEIO::MetaData metadata;
  204. KURL kurl;
  205. bool isFile;
  206. //job should be the latest job
  207. if( job != _job )
  208. kdError() << i18n( "Got unknown job; something must be wrong..." ) << endl;
  209. for( TQValueListConstIterator<KKioDrop::FileInfo> it = _tdeio->_mailurls->begin(); it != _tdeio->_mailurls->end(); ++it )
  210. old_list.append( (*it).name );
  211. for ( it1 = list.begin() ; it1 != list.end() ; it1++ )
  212. {
  213. /*
  214. * The list contains multiple objects. Each object could be a file.
  215. * Settings about it are saved in this scope until it is added to the list.
  216. */
  217. isFile=false;
  218. KKioDrop::FileInfo fileinfo;
  219. fileinfo.name = TQString();
  220. fileinfo.size = 0;
  221. for ( it2 = (*it1).begin() ; it2 != (*it1).end() ; it2++ )
  222. {
  223. if( (*it2).m_uds == TDEIO::UDS_FILE_TYPE &&
  224. ((long)(*it2).m_long & S_IFREG ) )
  225. isFile=true;
  226. else if( (*it2).m_uds == TDEIO::UDS_URL )
  227. fileinfo.name = (*it2).m_str;
  228. else if( (*it2).m_uds == TDEIO::UDS_NAME )
  229. { //The file tdeioslave doesn't return UDS_URL.
  230. kurl = *_kurl;
  231. metadata = *_metadata;
  232. _protocol->recheckKURL( kurl, metadata );
  233. kurl.setPath ( kurl.path() + '/' + (*it2).m_str );
  234. fileinfo.name = kurl.url();
  235. }
  236. else if( (*it2).m_uds == TDEIO::UDS_SIZE )
  237. {
  238. fileinfo.size = (*it2).m_long;
  239. }
  240. }
  241. //Add the entry.
  242. if( ! fileinfo.name.isNull() && isFile )
  243. {
  244. _new_mailurls->append( fileinfo );
  245. if( ! old_list.contains( fileinfo.name ) && _tdeio->passivePopup() )
  246. showPassive( fileinfo.name );
  247. }
  248. }
  249. }
  250. void TDEIO_Count::addtoPassivePopup( KornMailSubject* subject )
  251. {
  252. if( ! _popup_subjects )
  253. {
  254. _popup_subjects = new SortedMailSubject;
  255. _popup_subjects->setAutoDelete( true );
  256. }
  257. _popup_subjects->inSort( subject );
  258. if( _popup_subjects->count() > 5 )
  259. _popup_subjects->removeFirst(); //Overhead: subject is downloaded
  260. _subjects_pending--;
  261. _total_new_messages++;
  262. if( _subjects_pending == 0 )
  263. {
  264. _tdeio->emitShowPassivePopup( dynamic_cast< TQPtrList<KornMailSubject>* >( _popup_subjects ), _total_new_messages );
  265. delete _popup_subjects; _popup_subjects = 0;
  266. _total_new_messages = 0;
  267. disconnectSlave();
  268. }
  269. }
  270. void TDEIO_Count::deleteSingleSubject( TDEIO_Single_Subject* single_subject )
  271. {
  272. delete single_subject;
  273. }
  274. #include "tdeio_count.moc"