akregator/src/librss

image.cpp
1/*
2 * image.cpp
3 *
4 * Copyright (c) 2001, 2002, 2003 Frerich Raabe <raabe@kde.org>
5 *
6 * This program is distributed in the hope that it will be useful, but WITHOUT
7 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8 * FOR A PARTICULAR PURPOSE. For licensing and distribution details, check the
9 * accompanying file 'COPYING'.
10 */
11#include "image.h"
12#include "tools_p.h"
13
14#include <tdeio/job.h>
15#include <kurl.h>
16
17#include <tqbuffer.h>
18#include <tqdom.h>
19#include <tqpixmap.h>
20
21using namespace RSS;
22
23struct Image::Private : public Shared
24{
25 Private() : height(31), width(88), pixmapBuffer(NULL), job(NULL)
26 { }
27
28 TQString title;
29 KURL url;
30 KURL link;
31 TQString description;
32 unsigned int height;
33 unsigned int width;
34 TQBuffer *pixmapBuffer;
35 TDEIO::Job *job;
36};
37
38Image::Image() : TQObject(), d(new Private)
39{
40}
41
42Image::Image(const Image &other) : TQObject(), d(0)
43{
44 *this = other;
45}
46
47Image::Image(const TQDomNode &node) : TQObject(), d(new Private)
48{
49 TQString elemText;
50
51 if (!(elemText = extractNode(node, TQString::fromLatin1("title"))).isNull())
52 d->title = elemText;
53 if (!(elemText = extractNode(node, TQString::fromLatin1("url"))).isNull())
54 d->url = elemText;
55 if (!(elemText = extractNode(node, TQString::fromLatin1("link"))).isNull())
56 d->link = elemText;
57 if (!(elemText = extractNode(node, TQString::fromLatin1("description"))).isNull())
58 d->description = elemText;
59 if (!(elemText = extractNode(node, TQString::fromLatin1("height"))).isNull())
60 d->height = elemText.toUInt();
61 if (!(elemText = extractNode(node, TQString::fromLatin1("width"))).isNull())
62 d->width = elemText.toUInt();
63}
64
66{
67 if (d->deref())
68 {
69 delete d->pixmapBuffer;
70 d->pixmapBuffer=0L;
71 delete d;
72 }
73}
74
75TQString Image::title() const
76{
77 return d->title;
78}
79
80const KURL &Image::url() const
81{
82 return d->url;
83}
84
85const KURL &Image::link() const
86{
87 return d->link;
88}
89
90TQString Image::description() const
91{
92 return d->description;
93}
94
95unsigned int Image::height() const
96{
97 return d->height;
98}
99
100unsigned int Image::width() const
101{
102 return d->width;
103}
104
106{
107 // Ignore subsequent calls if we didn't finish the previous download.
108 if (d->pixmapBuffer)
109 return;
110
111 d->pixmapBuffer = new TQBuffer;
112 d->pixmapBuffer->open(IO_WriteOnly);
113
114 d->job = TDEIO::get(d->url, false, false);
115 connect(d->job, TQ_SIGNAL(data(TDEIO::Job *, const TQByteArray &)),
116 this, TQ_SLOT(slotData(TDEIO::Job *, const TQByteArray &)));
117 connect(d->job, TQ_SIGNAL(result(TDEIO::Job *)), this, TQ_SLOT(slotResult(TDEIO::Job *)));
118}
119
120void Image::slotData(TDEIO::Job *, const TQByteArray &data)
121{
122 d->pixmapBuffer->writeBlock(data.data(), data.size());
123}
124
125void Image::slotResult(TDEIO::Job *job)
126{
127 TQPixmap pixmap;
128 if (!job->error())
129 pixmap = TQPixmap(d->pixmapBuffer->buffer());
130 emit gotPixmap(pixmap);
131
132 delete d->pixmapBuffer;
133 d->pixmapBuffer = NULL;
134}
135
136void Image::abort()
137{
138 if (d->job)
139 {
140 d->job->kill(true);
141 d->job = NULL;
142 }
143}
144
146{
147 if (this != &other) {
148 other.d->ref();
149 if (d && d->deref())
150 delete d;
151 d = other.d;
152 }
153 return *this;
154}
155
156bool Image::operator==(const Image &other) const
157{
158 return d->title == other.title() &&
159 d->url == other.url() &&
160 d->description == other.description() &&
161 d->height == other.height() &&
162 d->width == other.width() &&
163 d->link == other.link();
164}
165
166#include "image.moc"
Represents an image as stored in a RSS file.
Definition: image.h:35
virtual ~Image()
Destructor.
Definition: image.cpp:65
TQString title() const
RSS 0.90 and upwards.
Definition: image.cpp:75
void gotPixmap(const TQPixmap &pixmap)
Emitted when this Image is done downloading the actual graphics data as referenced by the URL returne...
Image & operator=(const Image &other)
Assignment operator.
Definition: image.cpp:145
unsigned int height() const
RSS 0.91 and upwards.
Definition: image.cpp:95
Image()
Default constructor.
Definition: image.cpp:38
bool operator==(const Image &other) const
Compares two images.
Definition: image.cpp:156
TQString description() const
RSS 0.91 and upwards.
Definition: image.cpp:90
unsigned int width() const
RSS 0.91 and upwards.
Definition: image.cpp:100
const KURL & link() const
RSS 0.90 and upwards.
Definition: image.cpp:85
void getPixmap()
Makes the image download the image data as referenced by the URL returned by url().
Definition: image.cpp:105
const KURL & url() const
RSS 0.90 and upwards.
Definition: image.cpp:80