summaryrefslogtreecommitdiffstats
path: root/librss/image.cpp
blob: a1fe032c01734420a69a171f7c9bbec2bd55db7a (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 * image.cpp
 *
 * Copyright (c) 2001, 2002, 2003, 2004 Frerich Raabe <raabe@kde.org>
 *
 * 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. For licensing and distribution details, check the
 * accompanying file 'COPYING'.
 */
#include "image.h"
#include "tools_p.h"

#include <kio/job.h>
#include <kurl.h>

#include <tqbuffer.h>
#include <tqdom.h>
#include <tqpixmap.h>

using namespace RSS;

struct Image::Private : public Shared
{
	Private() : height(31), width(88), pixmapBuffer(NULL)
		{ }

	TQString title;
	KURL url;
	KURL link;
	TQString description;
	unsigned int height;
	unsigned int width;
	TQBuffer *pixmapBuffer;
};

Image::Image() : TQObject(), d(new Private)
{
}

Image::Image(const Image &other) : TQObject(), d(0)
{
	*this = other;
}

Image::Image(const TQDomNode &node) : TQObject(), d(new Private)
{
	TQString elemText;

	if (!(elemText = extractNode(node, TQString::tqfromLatin1("title"))).isNull())
		d->title = elemText;
	if (!(elemText = extractNode(node, TQString::tqfromLatin1("url"))).isNull())
		d->url = elemText;
	if (!(elemText = extractNode(node, TQString::tqfromLatin1("link"))).isNull())
		d->link = elemText;
	if (!(elemText = extractNode(node, TQString::tqfromLatin1("description"))).isNull())
		d->description = elemText;
	if (!(elemText = extractNode(node, TQString::tqfromLatin1("height"))).isNull())
		d->height = elemText.toUInt();
	if (!(elemText = extractNode(node, TQString::tqfromLatin1("width"))).isNull())
		d->width = elemText.toUInt();
}

Image::~Image()
{
	if (d->deref())
        {
            delete d->pixmapBuffer;
            d->pixmapBuffer=0L;
            delete d;
        }
}

TQString Image::title() const
{
	return d->title;
}

const KURL &Image::url() const
{
	return d->url;
}

const KURL &Image::link() const
{
	return d->link;
}

TQString Image::description() const
{
	return d->description;
}

unsigned int Image::height() const
{
	return d->height;
}

unsigned int Image::width() const
{
	return d->width;
}

void Image::getPixmap()
{
	// Ignore subsequent calls if we didn't finish the previous download.
	if (d->pixmapBuffer)
		return;

	d->pixmapBuffer = new TQBuffer;
	d->pixmapBuffer->open(IO_WriteOnly);

	KIO::Job *job = KIO::get(d->url, false, false);
	connect(job, TQT_SIGNAL(data(KIO::Job *, const TQByteArray &)),
	        this, TQT_SLOT(slotData(KIO::Job *, const TQByteArray &)));
	connect(job, TQT_SIGNAL(result(KIO::Job *)), this, TQT_SLOT(slotResult(KIO::Job *)));
}

void Image::slotData(KIO::Job *, const TQByteArray &data)
{
	d->pixmapBuffer->writeBlock(data.data(), data.size());
}

void Image::slotResult(KIO::Job *job)
{
	TQPixmap pixmap;
	if (!job->error())
		pixmap = TQPixmap(d->pixmapBuffer->buffer());
	emit gotPixmap(pixmap);

	delete d->pixmapBuffer;
	d->pixmapBuffer = NULL;
}

Image &Image::operator=(const Image &other)
{
	if (this != &other) {
		other.d->ref();
		if (d && d->deref())
			delete d;
		d = other.d;
	}
	return *this;
}

bool Image::operator==(const Image &other) const
{
	return d->title == other.title() &&
	       d->url == other.url() &&
	       d->description == other.description() &&
	       d->height == other.height() &&
	       d->width == other.width() &&
	       d->link == other.link();
}

#include "image.moc"
// vim:noet:ts=4