akregator/src/librss

image.cpp

00001 /*
00002  * image.cpp
00003  *
00004  * Copyright (c) 2001, 2002, 2003 Frerich Raabe <raabe@kde.org>
00005  *
00006  * This program is distributed in the hope that it will be useful, but WITHOUT
00007  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00008  * FOR A PARTICULAR PURPOSE. For licensing and distribution details, check the
00009  * accompanying file 'COPYING'.
00010  */
00011 #include "image.h"
00012 #include "tools_p.h"
00013 
00014 #include <kio/job.h>
00015 #include <kurl.h>
00016 
00017 #include <qbuffer.h>
00018 #include <qdom.h>
00019 #include <qpixmap.h>
00020 
00021 using namespace RSS;
00022 
00023 struct Image::Private : public Shared
00024 {
00025     Private() : height(31), width(88), pixmapBuffer(NULL), job(NULL)
00026         { }
00027 
00028     QString title;
00029     KURL url;
00030     KURL link;
00031     QString description;
00032     unsigned int height;
00033     unsigned int width;
00034     QBuffer *pixmapBuffer;
00035     KIO::Job *job;
00036 };
00037 
00038 Image::Image() : QObject(), d(new Private)
00039 {
00040 }
00041 
00042 Image::Image(const Image &other) : QObject(), d(0)
00043 {
00044     *this = other;
00045 }
00046 
00047 Image::Image(const QDomNode &node) : QObject(), d(new Private)
00048 {
00049     QString elemText;
00050 
00051     if (!(elemText = extractNode(node, QString::fromLatin1("title"))).isNull())
00052         d->title = elemText;
00053     if (!(elemText = extractNode(node, QString::fromLatin1("url"))).isNull())
00054         d->url = elemText;
00055     if (!(elemText = extractNode(node, QString::fromLatin1("link"))).isNull())
00056         d->link = elemText;
00057     if (!(elemText = extractNode(node, QString::fromLatin1("description"))).isNull())
00058         d->description = elemText;
00059     if (!(elemText = extractNode(node, QString::fromLatin1("height"))).isNull())
00060         d->height = elemText.toUInt();
00061     if (!(elemText = extractNode(node, QString::fromLatin1("width"))).isNull())
00062         d->width = elemText.toUInt();
00063 }
00064 
00065 Image::~Image()
00066 {
00067     if (d->deref())
00068         {
00069             delete d->pixmapBuffer;
00070             d->pixmapBuffer=0L;
00071             delete d;
00072         }
00073 }
00074 
00075 QString Image::title() const
00076 {
00077     return d->title;
00078 }
00079 
00080 const KURL &Image::url() const
00081 {
00082     return d->url;
00083 }
00084 
00085 const KURL &Image::link() const
00086 {
00087     return d->link;
00088 }
00089 
00090 QString Image::description() const
00091 {
00092     return d->description;
00093 }
00094 
00095 unsigned int Image::height() const
00096 {
00097     return d->height;
00098 }
00099 
00100 unsigned int Image::width() const
00101 {
00102     return d->width;
00103 }
00104 
00105 void Image::getPixmap()
00106 {
00107     // Ignore subsequent calls if we didn't finish the previous download.
00108     if (d->pixmapBuffer)
00109         return;
00110 
00111     d->pixmapBuffer = new QBuffer;
00112     d->pixmapBuffer->open(IO_WriteOnly);
00113 
00114     d->job = KIO::get(d->url, false, false);
00115     connect(d->job, SIGNAL(data(KIO::Job *, const QByteArray &)),
00116             this, SLOT(slotData(KIO::Job *, const QByteArray &)));
00117     connect(d->job, SIGNAL(result(KIO::Job *)), this, SLOT(slotResult(KIO::Job *)));
00118 }
00119 
00120 void Image::slotData(KIO::Job *, const QByteArray &data)
00121 {
00122     d->pixmapBuffer->writeBlock(data.data(), data.size());
00123 }
00124 
00125 void Image::slotResult(KIO::Job *job)
00126 {
00127     QPixmap pixmap;
00128     if (!job->error())
00129         pixmap = QPixmap(d->pixmapBuffer->buffer());
00130     emit gotPixmap(pixmap);
00131 
00132     delete d->pixmapBuffer;
00133     d->pixmapBuffer = NULL;
00134 }
00135 
00136 void Image::abort()
00137 {
00138     if (d->job)
00139     {
00140         d->job->kill(true);
00141         d->job = NULL;
00142     }
00143 }
00144 
00145 Image &Image::operator=(const Image &other)
00146 {
00147     if (this != &other) {
00148         other.d->ref();
00149         if (d && d->deref())
00150             delete d;
00151         d = other.d;
00152     }
00153     return *this;
00154 }
00155 
00156 bool Image::operator==(const Image &other) const
00157 {
00158     return d->title == other.title() &&
00159            d->url == other.url() &&
00160            d->description == other.description() &&
00161            d->height == other.height() &&
00162            d->width == other.width() &&
00163            d->link == other.link();
00164 }
00165 
00166 #include "image.moc"
00167 // vim:noet:ts=4
KDE Home | KDE Accessibility Home | Description of Access Keys