summaryrefslogtreecommitdiffstats
path: root/kmrml/kmrml/loader.cpp
blob: ea8803e2ab5fc329bf289e346732085d178cd8d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* This file is part of the KDE project
   Copyright (C) 2001 Carsten Pfeiffer <pfeiffer@kde.org>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation, version 2.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include <kstaticdeleter.h>
#include <kio/scheduler.h>

#include "loader.h"

Loader *Loader::s_self = 0L;

KStaticDeleter<Loader> sd;

Loader * Loader::self()
{
    if ( !s_self )
        s_self = sd.setObject( new Loader() );

    return s_self;
}

Loader::Loader() : TQObject()
{
}

Loader::~Loader()
{
    disconnect( this, TQT_SIGNAL( finished( const KURL&, const TQByteArray& )));

    DownloadIterator it = m_downloads.begin();
    for ( ; it != m_downloads.end(); ++it ) {
        it.key()->kill();
        delete it.data();
    }

    s_self = 0L;
}

void Loader::requestDownload( const KURL& url )
{
    DownloadIterator it = m_downloads.begin();
    for ( ; it != m_downloads.end(); ++it ) {
        if ( it.key()->url() == url )
            return;
    }

    KIO::TransferJob *job = KIO::get( url, false, false );
    KIO::Scheduler::scheduleJob(job);

    connect( job , TQT_SIGNAL( data( KIO::Job *, const TQByteArray& )),
             TQT_SLOT( slotData( KIO::Job *, const TQByteArray& )));
    connect( job , TQT_SIGNAL( result( KIO::Job * )),
             TQT_SLOT( slotResult( KIO::Job * )));

    Download *d = new Download();
    m_downloads.insert( job, d );
}

void Loader::slotData( KIO::Job *job, const TQByteArray& data )
{
    DownloadIterator it = m_downloads.find( static_cast<KIO::TransferJob*>(job) );
    if ( it != m_downloads.end() ) {
        TQBuffer& buffer = it.data()->m_buffer;
        if ( !buffer.isOpen() )
            buffer.open( IO_ReadWrite );
        if ( !buffer.isOpen() ) {
            qDebug("********* EEK, can't open buffer for thumbnail download!");
            return;
        }

        buffer.writeBlock( data.data(), data.size() );
    }
}

void Loader::slotResult( KIO::Job *job )
{
    KIO::TransferJob *tjob = static_cast<KIO::TransferJob*>( job );

    DownloadIterator it = m_downloads.find( tjob );
    if ( it != m_downloads.end() ) {
        Download *d = it.data();

        if ( job->error() != 0 )
            emit finished( tjob->url(), TQByteArray() );
        else
            emit finished( tjob->url(), d->m_buffer.buffer() );

        delete d;
        m_downloads.remove( it );
    }
}


// ### simultaneous downloads with multiple views? reference count downloads!
void Loader::removeDownload( const KURL& url )
{
    DownloadIterator it = m_downloads.begin();
    for ( ; it != m_downloads.end(); ++it ) {
        if ( it.key()->url() == url ) {
            it.key()->kill();
            delete it.data();
            return;
        }
    }
}

#include "loader.moc"