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 <tdeio/job.h> 00015 #include <kurl.h> 00016 00017 #include <tqbuffer.h> 00018 #include <tqdom.h> 00019 #include <tqpixmap.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 TQString title; 00029 KURL url; 00030 KURL link; 00031 TQString description; 00032 unsigned int height; 00033 unsigned int width; 00034 TQBuffer *pixmapBuffer; 00035 TDEIO::Job *job; 00036 }; 00037 00038 Image::Image() : TQObject(), d(new Private) 00039 { 00040 } 00041 00042 Image::Image(const Image &other) : TQObject(), d(0) 00043 { 00044 *this = other; 00045 } 00046 00047 Image::Image(const TQDomNode &node) : TQObject(), d(new Private) 00048 { 00049 TQString elemText; 00050 00051 if (!(elemText = extractNode(node, TQString::fromLatin1("title"))).isNull()) 00052 d->title = elemText; 00053 if (!(elemText = extractNode(node, TQString::fromLatin1("url"))).isNull()) 00054 d->url = elemText; 00055 if (!(elemText = extractNode(node, TQString::fromLatin1("link"))).isNull()) 00056 d->link = elemText; 00057 if (!(elemText = extractNode(node, TQString::fromLatin1("description"))).isNull()) 00058 d->description = elemText; 00059 if (!(elemText = extractNode(node, TQString::fromLatin1("height"))).isNull()) 00060 d->height = elemText.toUInt(); 00061 if (!(elemText = extractNode(node, TQString::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 TQString 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 TQString 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 TQBuffer; 00112 d->pixmapBuffer->open(IO_WriteOnly); 00113 00114 d->job = TDEIO::get(d->url, false, false); 00115 connect(d->job, TQT_SIGNAL(data(TDEIO::Job *, const TQByteArray &)), 00116 this, TQT_SLOT(slotData(TDEIO::Job *, const TQByteArray &))); 00117 connect(d->job, TQT_SIGNAL(result(TDEIO::Job *)), this, TQT_SLOT(slotResult(TDEIO::Job *))); 00118 } 00119 00120 void Image::slotData(TDEIO::Job *, const TQByteArray &data) 00121 { 00122 d->pixmapBuffer->writeBlock(data.data(), data.size()); 00123 } 00124 00125 void Image::slotResult(TDEIO::Job *job) 00126 { 00127 TQPixmap pixmap; 00128 if (!job->error()) 00129 pixmap = TQPixmap(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