summaryrefslogtreecommitdiffstats
path: root/plugins/rssfeed/rss/article.cpp
blob: 47413b32754340dcf826ab60f084ea250cc22fc9 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
 * 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 <kdebug.h>
#include <krfcdate.h>
#include <kurl.h>
#include <kurllabel.h>
#include <kmdcodec.h> 

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

using namespace RSS;
namespace RSS
{
    KMD5 md5Machine;
}

struct Article::Private : public Shared
{
	TQString title;
	KURL link;
	TQString description;
	TQDateTime pubDate;
	TQString guid;
	bool guidIsPermaLink;
    MetaInfoMap meta;
	KURL commentsLink;
	int numComments;
};

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

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

Article::Article(const TQDomNode &node, Format format) : d(new Private)
{
	TQString elemText;

	d->numComments=0;

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

	TQDomNode n;
	bool foundTorrentEnclosure = false;
	for (n = node.firstChild(); !n.isNull(); n = n.nextSibling()) {
		const TQDomElement e = n.toElement();
		if ( (e.tagName()==TQString::fromLatin1("enclosure") ) )
			{
			TQString enclosureAttr = e.attribute(TQString::fromLatin1("type"));
			if (!enclosureAttr.isNull() )
				{
				if (enclosureAttr == "application/x-bittorrent")
					{
					enclosureAttr = e.attribute(TQString::fromLatin1("url"));
					if (!enclosureAttr.isNull() )
						{
						d->link=enclosureAttr;
						foundTorrentEnclosure = true;
						break;
						}
					}
				}
			}
		}

	if (!foundTorrentEnclosure)
		{
		if (format==AtomFeed)
		{
			TQDomNode n;
			for (n = node.firstChild(); !n.isNull(); n = n.nextSibling()) {
				const TQDomElement e = n.toElement();
				if ( (e.tagName()==TQString::fromLatin1("link")) &&
					(e.attribute(TQString::fromLatin1("rel"))==TQString::fromLatin1("alternate")))
					{   
						d->link=n.toElement().attribute(TQString::fromLatin1("href"));
						break;
					}
			}
		}
		else
		{
			if (!(elemText = extractNode(node, TQString::fromLatin1("link"))).isNull())
				d->link = elemText;
		}
	}


    // prefer content/content:encoded over summary/description for feeds that provide it
    TQString tagName=(format==AtomFeed)? TQString::fromLatin1("content"): TQString::fromLatin1("content:encoded");
    
    if (!(elemText = extractNode(node, tagName, false)).isNull())
        d->description = elemText;
    
    if (d->description.isEmpty())
    {
		if (!(elemText = extractNode(node, TQString::fromLatin1("body"), false)).isNull())
	    	d->description = elemText;
    
		if (d->description.isEmpty())  // 3rd try: see http://www.intertwingly.net/blog/1299.html
		{
			if (!(elemText = extractNode(node, TQString::fromLatin1((format==AtomFeed)? "summary" : "description"), false)).isNull())
				d->description = elemText;
		}
    }
    
	if (!(elemText = extractNode(node, TQString::fromLatin1((format==AtomFeed)? "created": "pubDate"))).isNull())
    {
		time_t _time;
		if (format==AtomFeed)
		   _time = parseISO8601Date(elemText); 
		else
		   _time = KRFCDate::parseDate(elemText);

        // 0 means invalid, not epoch (it returns epoch+1 when it parsed epoch, see the KRFCDate::parseDate() docs)
        if (_time != 0)
		  d->pubDate.setTime_t(_time);
	}
	if (!(elemText = extractNode(node, TQString::fromLatin1("dc:date"))).isNull())
    {
		time_t _time = parseISO8601Date(elemText);

        // 0 means invalid, not epoch (it returns epoch+1 when it parsed epoch, see the KRFCDate::parseDate() docs)
        if (_time != 0)
		  d->pubDate.setTime_t(_time);
	}

	//no luck so far - so let's set it to the current time
	if (!d->pubDate.isValid())
	{
		d->pubDate = TQDateTime::currentDateTime();
	}
	

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

    if (!(elemText = extractNode(node, TQString::fromLatin1("slash:comments"))).isNull()) {
        d->numComments = elemText.toInt();
    }

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

		if (!(elemText = extractNode(node, tagName)).isNull())
			d->guid = elemText;
	}

	if(d->guid.isEmpty()) {
		d->guidIsPermaLink = false;
        
		md5Machine.reset();
		TQDomNode n(node);
		md5Machine.update(d->title.utf8());
		md5Machine.update(d->description.utf8());
		d->guid = TQString(md5Machine.hexDigest().data());
        d->meta[TQString::fromLatin1("guidIsHash")] = TQString::fromLatin1("true");
	}

    for (TQDomNode i = node.firstChild(); !i.isNull(); i = i.nextSibling())
    {
        if (i.isElement() && i.toElement().tagName() == TQString::fromLatin1("metaInfo:meta"))
        {
            TQString type = i.toElement().attribute(TQString::fromLatin1("type"));
            d->meta[type] = i.toElement().text();
        }
    }
}

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;
}

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

int Article::comments() const
{
	return d->numComments;
}


TQString Article::meta(const TQString &key) const
{
    return d->meta[key];
}

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->guid == other.guid();
}

// vim:noet:ts=4