TDE graphics utilities
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.

122 lines
3.3KB

  1. /* This file is part of the KDE project
  2. Copyright (C) 2001 Carsten Pfeiffer <pfeiffer@kde.org>
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public
  5. License as published by the Free Software Foundation, version 2.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  9. General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; see the file COPYING. If not, write to
  12. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  13. Boston, MA 02110-1301, USA.
  14. */
  15. #include <kstaticdeleter.h>
  16. #include <kio/scheduler.h>
  17. #include "loader.h"
  18. Loader *Loader::s_self = 0L;
  19. KStaticDeleter<Loader> sd;
  20. Loader * Loader::self()
  21. {
  22. if ( !s_self )
  23. s_self = sd.setObject( new Loader() );
  24. return s_self;
  25. }
  26. Loader::Loader() : TQObject()
  27. {
  28. }
  29. Loader::~Loader()
  30. {
  31. disconnect( this, TQT_SIGNAL( finished( const KURL&, const TQByteArray& )));
  32. DownloadIterator it = m_downloads.begin();
  33. for ( ; it != m_downloads.end(); ++it ) {
  34. it.key()->kill();
  35. delete it.data();
  36. }
  37. s_self = 0L;
  38. }
  39. void Loader::requestDownload( const KURL& url )
  40. {
  41. DownloadIterator it = m_downloads.begin();
  42. for ( ; it != m_downloads.end(); ++it ) {
  43. if ( it.key()->url() == url )
  44. return;
  45. }
  46. TDEIO::TransferJob *job = TDEIO::get( url, false, false );
  47. TDEIO::Scheduler::scheduleJob(job);
  48. connect( job , TQT_SIGNAL( data( TDEIO::Job *, const TQByteArray& )),
  49. TQT_SLOT( slotData( TDEIO::Job *, const TQByteArray& )));
  50. connect( job , TQT_SIGNAL( result( TDEIO::Job * )),
  51. TQT_SLOT( slotResult( TDEIO::Job * )));
  52. Download *d = new Download();
  53. m_downloads.insert( job, d );
  54. }
  55. void Loader::slotData( TDEIO::Job *job, const TQByteArray& data )
  56. {
  57. DownloadIterator it = m_downloads.find( static_cast<TDEIO::TransferJob*>(job) );
  58. if ( it != m_downloads.end() ) {
  59. TQBuffer& buffer = it.data()->m_buffer;
  60. if ( !buffer.isOpen() )
  61. buffer.open( IO_ReadWrite );
  62. if ( !buffer.isOpen() ) {
  63. tqDebug("********* EEK, can't open buffer for thumbnail download!");
  64. return;
  65. }
  66. buffer.writeBlock( data.data(), data.size() );
  67. }
  68. }
  69. void Loader::slotResult( TDEIO::Job *job )
  70. {
  71. TDEIO::TransferJob *tjob = static_cast<TDEIO::TransferJob*>( job );
  72. DownloadIterator it = m_downloads.find( tjob );
  73. if ( it != m_downloads.end() ) {
  74. Download *d = it.data();
  75. if ( job->error() != 0 )
  76. emit finished( tjob->url(), TQByteArray() );
  77. else
  78. emit finished( tjob->url(), d->m_buffer.buffer() );
  79. delete d;
  80. m_downloads.remove( it );
  81. }
  82. }
  83. // ### simultaneous downloads with multiple views? reference count downloads!
  84. void Loader::removeDownload( const KURL& url )
  85. {
  86. DownloadIterator it = m_downloads.begin();
  87. for ( ; it != m_downloads.end(); ++it ) {
  88. if ( it.key()->url() == url ) {
  89. it.key()->kill();
  90. delete it.data();
  91. return;
  92. }
  93. }
  94. }
  95. #include "loader.moc"