summaryrefslogtreecommitdiffstats
path: root/dcoprss/document.cpp
blob: b43f600e81ffb4531eb10c111b07b875a2ee9b51 (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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/* $Id$ */
/***************************************************************************
                          document.cpp  -  A DCOP Service to provide RSS data
                             -------------------
    begin                : Saturday 15 February 2003
    copyright            : (C) 2003 by Ian Reinhart Geiser
    email                : [email protected]
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
#include <kdebug.h>
#include <qdatetime.h>
#include <kurl.h>
#include "service.h"

RSSDocument::RSSDocument(const QString& url) :
	QObject(), DCOPObject(), m_Url(url)
{

	m_list.setAutoDelete( true );
	m_Doc = 0L;
	m_pix = QPixmap();
	m_isLoading = false;
	m_maxAge = 60;
	m_Timeout = QDateTime::currentDateTime();
	m_state.clear();
}

RSSDocument::~RSSDocument()
{
	kdDebug() << "Document going away..." << endl;

	delete m_Doc;
}

void RSSDocument::loadingComplete(Loader *ldr, Document doc, Status stat)
{


	if( m_Doc != 0L)
	{
		delete m_Doc;

	}

	if (stat != RSS::Success)
	{
		kdDebug() << "Document error! Loader:" << ldr->errorCode() << " Parser:" << stat << endl;

		m_isLoading = false;
		m_Doc = 0L;
		if( stat == RSS::ParseError )
			documentUpdateError(DCOPRef(this), 1);
		else if( stat == RSS::RetrieveError )
			documentUpdateError(DCOPRef(this), 2);
		else
			documentUpdateError(DCOPRef(this), 3);
	}
	else
	{
		kdDebug() << "New Document is done..." << endl;
		m_Doc = new Document(doc);
		m_list.clear();
		Article::List list = doc.articles();
		for(Article::List::ConstIterator it = list.begin(); it != list.end(); ++it)
		{
			int state = m_state[(*it).title()];
			if( state == 0 ) m_state[(*it).title()] = 1; // new
			else if( state == 1 ) m_state[(*it).title()] = 2; // old message now
			m_list.append( new RSSArticle( new Article(*it)));
		}
		Image *img = m_Doc->image();
		if ( img )
		{
			connect(img, SIGNAL(gotPixmap(const QPixmap &)),
				SLOT(pixmapLoaded(const QPixmap &)));
			img->getPixmap();
			pixmapUpdating(DCOPRef(this));
		}
		m_isLoading = false;
		documentUpdated(DCOPRef(this));

		kdDebug() << "Old Mod time " << m_Timeout.toString() << endl;
		m_Timeout = m_Timeout.addSecs(m_maxAge * 60 );
		kdDebug() << "New Mod time " << m_Timeout.toString() << endl;

	}
}

void RSSDocument::pixmapLoaded(const QPixmap &pix )
{
	m_pix = pix;
	pixmapUpdated(DCOPRef(this));
}

QString RSSDocument::webMaster()
{
	if( m_Doc != 0L)
		return m_Doc->webMaster();
	else
		return "";
}

QString RSSDocument::managingEditor()
{
	if( m_Doc != 0L)
			return m_Doc->managingEditor();
	else
		return "";
}

QString RSSDocument::rating()
{
	if( m_Doc != 0L)
		return m_Doc->rating();
	else
		return "";
}

QDateTime RSSDocument::lastBuildDate()
{
	if( m_Doc != 0L)
		return m_Doc->lastBuildDate();
	else
		return QDateTime::currentDateTime();
}

QDateTime RSSDocument::pubDate()
{
	if( m_Doc != 0L)
		return m_Doc->pubDate();
	else
		return QDateTime::currentDateTime();
}

QString RSSDocument::copyright()
{
	if( m_Doc != 0L)
		return m_Doc->copyright();
	else
		return "";
}

QStringList RSSDocument::articles()
{
	if( m_Doc != 0L)
	{
		kdDebug() << "Document giving articles..." << endl;
		Article::List list = m_Doc->articles();
		QStringList stringList;

		for(Article::List::ConstIterator it = list.begin(); it != list.end(); ++it)
				stringList.append((*it).title());
		return stringList;
	}
	else
		return QStringList();
}

DCOPRef RSSDocument::article(int idx)
{
	if(m_list.at(idx))
		return DCOPRef(m_list.at(idx));
	else
		return DCOPRef();
}

int RSSDocument::count()
{
	if( m_Doc != 0L)
		return m_Doc->articles().count();
	return 0;
}

QString RSSDocument::link()
{
	if( m_Doc != 0L)
		return m_Doc->link().prettyURL();
	else
		return "";
}

QString RSSDocument::description()
{
	if( m_Doc != 0L)
		return m_Doc->description();
	else
		return "";
}

QString RSSDocument::title()
{
	if( m_Doc != 0L)
		return m_Doc->title();
	else
		return "";
}

QString RSSDocument::verbVersion()
{
	if( m_Doc != 0L)
		return m_Doc->verbVersion();
	else
		return "";
}

QString RSSDocument::pixmapURL()
{
	if( m_Doc != 0L)
		if( m_Doc->image() )
			return m_Doc->image()->url().prettyURL();
		else
			return "";
	else
		return "";
}

QPixmap RSSDocument::pixmap()
{
	return m_pix;
}

bool RSSDocument::documentValid()
{
	if (m_Doc != 0L)
		return true;
	else
		return false;
}

bool RSSDocument::pixmapValid()
{
	return !m_pix.isNull();
}

void RSSDocument::refresh()
{
	kdDebug() << "Mod time " << m_Timeout.toString() << endl;
	kdDebug() << "Current time " << QDateTime::currentDateTime().toString() << endl;

	if(!m_isLoading  && (QDateTime::currentDateTime() >= m_Timeout))
	{
		kdDebug() << "Document going to refresh" << endl;
		m_isLoading = true;
		Loader *loader = Loader::create(this,
			SLOT(loadingComplete(Loader *, Document, Status)));
		loader->loadFrom(KURL( m_Url ),  new FileRetriever());
		documentUpdating(DCOPRef(this));
	}
	else
	{
		documentUpdated(DCOPRef(this));
		if(pixmapValid())
			pixmapUpdated(DCOPRef(this));
		/*
		else
		{
			// Refactor this!
			Image *img = m_Doc->image();
			if ( img )
			{
				connect(img, SIGNAL(gotPixmap(const QPixmap &)),
					SLOT(pixmapLoaded(const QPixmap &)));
				img->getPixmap();
				pixmapUpdating(DCOPRef(this));
			}
		}
		*/
	}

}

int RSSDocument::maxAge()
{
	return m_maxAge;
}

void RSSDocument::setMaxAge(int _min)
{
	m_Timeout.addSecs(-m_maxAge);
	m_maxAge = _min;
	m_Timeout.addSecs(m_maxAge);
}

int RSSDocument::state( const QString &title) const
{
	return m_state[title];
}

void RSSDocument::setState( const QString &title, int s )
{
	m_state[title] = s;
}

void RSSDocument::read( const QString &title)
{
	m_state[title] = 3;
}

#include "service.moc"