summaryrefslogtreecommitdiffstats
path: root/librss/article.cpp
blob: fbc240f430ffa08bfd68ade5fa68b7d139487b36 (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
/*
 * article.cpp
 *
 * Copyright (c) 2001, 2002, 2003, 2004 Frerich Raabe <[email protected]>
 *
 * 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 "article.h"
#include "tools_p.h"

#include <krfcdate.h>
#include <kurl.h>
#include <kurllabel.h>

#include <tqdatetime.h>
#include <tqdom.h>

using namespace RSS;

struct Article::Private : public Shared
{
	TQString title;
	KURL link;
	TQString description;
	TQDateTime pubDate;
	TQString guid;
	bool guidIsPermaLink;
};

Article::Article() : d(new Private)
{
}

Article::Article(const Article &other) : d(0)
{
	*this = other;
}

Article::Article(const TQDomNode &node) : d(new Private)
{
	TQString elemText;
	
	if (!(elemText = extractNode(node, TQString::fromLatin1("title"))).isNull())
		d->title = elemText;
	if (!(elemText = extractNode(node, TQString::fromLatin1("link"))).isNull())
		d->link = elemText;
	if (!(elemText = extractNode(node, TQString::fromLatin1("description"))).isNull())
		d->description = elemText;
	if (!(elemText = extractNode(node, TQString::fromLatin1("pubDate"))).isNull()) {
		time_t _time = KRFCDate::parseDate(elemText);
		/* \bug This isn't really the right way since it will set the date to
		 * Jan 1 1970, 1:00:00 if the passed date was invalid; this means that
		 * we cannot distinguish between that date, and invalid values. :-/
		 */
		d->pubDate.setTime_t(_time);
	}
	if (!(elemText = extractNode(node, TQString::fromLatin1("dc:date"))).isNull()) {
        time_t _time = KRFCDate::parseDateISO8601(elemText);
        /* \bug This isn't really the right way since it will set the date to
         * Jan 1 1970, 1:00:00 if the passed date was invalid; this means that
         * we cannot distinguish between that date, and invalid values. :-/
         */
        d->pubDate.setTime_t(_time);
    }

	TQDomNode n = node.namedItem(TQString::fromLatin1("guid"));
	if (!n.isNull()) {
		d->guidIsPermaLink = true;
		if (n.toElement().attribute(TQString::fromLatin1("isPermaLink"), "true") == "false") d->guidIsPermaLink = false;

		if (!(elemText = extractNode(node, TQString::fromLatin1("guid"))).isNull())
			d->guid = elemText;
	}
}

Article::~Article()
{
	if (d->deref())
		delete d;
}

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

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

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

TQString Article::guid() const
{
	return d->guid;
}

bool Article::guidIsPermaLink() const
{
	return d->guidIsPermaLink;
}

const TQDateTime &Article::pubDate() const
{
	return d->pubDate;
}

KURLLabel *Article::widget(TQWidget *parent, const char *name) const
{
	KURLLabel *label = new KURLLabel(d->link.url(), d->title, parent, name);
	label->setUseTips(true);
	if (!d->description.isNull())
		label->setTipText(d->description);
	
	return label;
}

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

bool Article::operator==(const Article &other) const
{
	return d->title == other.title() &&
		d->link == other.link() &&
		d->description == other.description() &&
		d->pubDate == other.pubDate() &&
		d->guid == other.guid() &&
		d->guidIsPermaLink == other.guidIsPermaLink();
}