Blokkal
an Extendable KDE Blogging Client
SourceForge.net Logo

blokkalimagefetcher.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2006 by Martin Müller                                   *
00003  *   orvio@orvio.de                                                        *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #include "blokkalimagefetcher.h"
00021 #include "blokkalimagefetcher.moc"
00022 
00023 #include <kio/job.h>
00024 #include <kdebug.h>
00025 
00026 #include <iostream>
00027 
00028 class Blokkal::ImageFetcher::Private
00029 {
00030 public:
00031         Private( void ) :
00032         autoDelete( TRUE )
00033         {}
00034 
00035         bool autoDelete;
00036 
00037         KJob * job;
00038         QByteArray buffer;
00039         QString url;
00040 };
00041 
00042 Blokkal::ImageFetcher::ImageFetcher( const QString & imageUrl, QObject * parent ):
00043 QObject( parent ),
00044 d( new Private() )
00045 {
00046         d->url = imageUrl;
00047         d->job = KIO::get( imageUrl,
00048                            KIO::Reload, //may take data from cache
00049                            KIO::HideProgressInfo //don't show progress info
00050                          );
00051 
00052         connect( d->job, SIGNAL( data( KIO::Job *, const QByteArray & ) ), SLOT( bufferData( KIO::Job*, const QByteArray & ) ) );
00053         connect( d->job, SIGNAL( result( KJob * ) ), SLOT( emitImage( KJob * ) ) );
00054 }
00055 
00056 Blokkal::ImageFetcher::~ImageFetcher( void )
00057 {
00058         if( d->job ) {
00059                 d->job->kill();
00060         }
00061 
00062         delete d;
00063 }
00064 
00065 void Blokkal::ImageFetcher::bufferData( KIO::Job * job, const QByteArray & data )
00066 {
00067         if( data.size() > 0 && job == d->job ) {
00068                 d->buffer.append( data );
00069         }
00070 }
00071 
00072 void Blokkal::ImageFetcher::emitImage( KJob * job )
00073 {
00074         if( job == d->job ) {
00075                 QImage image = QImage::fromData( d->buffer );
00076 
00077                 emit imageReceived( image, d->url );
00078         
00079                 d->job = 0;
00080                 if( isAutoDelete( ) ) {
00081                         deleteLater();
00082                 }
00083         }
00084 }
00085 
00086 bool Blokkal::ImageFetcher::isAutoDelete( void )
00087 {
00088         return d->autoDelete;
00089 }
00090 
00091 void Blokkal::ImageFetcher::setAutoDelete( bool enable )
00092 {
00093         d->autoDelete = enable;
00094 }