Port KProgressBar's to QProgressBar's.
[kdenetwork.git] / dcoprss / document.cpp
bloba4bf96d9581a17f0a8fe6cddff09d33d9c44e53c
1 /* $Id$ */
2 /***************************************************************************
3 document.cpp - A DCOP Service to provide RSS data
4 -------------------
5 begin : Saturday 15 February 2003
6 copyright : (C) 2003 by Ian Reinhart Geiser
7 email : geiseri@kde.org
8 ***************************************************************************/
10 /***************************************************************************
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 ***************************************************************************/
18 #include <kdebug.h>
19 #include <qdatetime.h>
20 //Added by qt3to4:
21 #include <QPixmap>
22 #include <kurl.h>
23 #include "service.h"
25 RSSDocument::RSSDocument(const QString& url) :
26 QObject(), DCOPObject(), m_Url(url)
29 m_Doc = 0L;
30 m_pix = QPixmap();
31 m_isLoading = false;
32 m_maxAge = 60;
33 m_Timeout = QDateTime::currentDateTime();
34 m_state.clear();
37 RSSDocument::~RSSDocument()
39 qDeleteAll(m_list);
40 m_list.clear();
41 kDebug() << "Document going away..." << endl;
43 delete m_Doc;
46 void RSSDocument::loadingComplete(Loader *ldr, Document doc, Status stat)
50 if( m_Doc != 0L)
52 delete m_Doc;
56 if (stat != RSS::Success)
58 kDebug() << "Document error! Loader:" << ldr->errorCode() << " Parser:" << stat << endl;
60 m_isLoading = false;
61 m_Doc = 0L;
62 if( stat == RSS::ParseError )
63 documentUpdateError(DCOPRef(this), 1);
64 else if( stat == RSS::RetrieveError )
65 documentUpdateError(DCOPRef(this), 2);
66 else
67 documentUpdateError(DCOPRef(this), 3);
69 else
71 kDebug() << "New Document is done..." << endl;
72 m_Doc = new Document(doc);
73 m_list.clear();
74 Article::List list = doc.articles();
75 for(Article::List::ConstIterator it = list.begin(); it != list.end(); ++it)
77 int state = m_state[(*it).title()];
78 if( state == 0 ) m_state[(*it).title()] = 1; // new
79 else if( state == 1 ) m_state[(*it).title()] = 2; // old message now
80 m_list.append( new RSSArticle( new Article(*it)));
82 Image *img = m_Doc->image();
83 if ( img )
85 connect(img, SIGNAL(gotPixmap(const QPixmap &)),
86 SLOT(pixmapLoaded(const QPixmap &)));
87 img->getPixmap();
88 pixmapUpdating(DCOPRef(this));
90 m_isLoading = false;
91 documentUpdated(DCOPRef(this));
93 kDebug() << "Old Mod time " << m_Timeout.toString() << endl;
94 m_Timeout = m_Timeout.addSecs(m_maxAge * 60 );
95 kDebug() << "New Mod time " << m_Timeout.toString() << endl;
100 void RSSDocument::pixmapLoaded(const QPixmap &pix )
102 m_pix = pix;
103 pixmapUpdated(DCOPRef(this));
106 QString RSSDocument::webMaster()
108 if( m_Doc != 0L)
109 return m_Doc->webMaster();
110 else
111 return "";
114 QString RSSDocument::managingEditor()
116 if( m_Doc != 0L)
117 return m_Doc->managingEditor();
118 else
119 return "";
122 QString RSSDocument::rating()
124 if( m_Doc != 0L)
125 return m_Doc->rating();
126 else
127 return "";
130 QDateTime RSSDocument::lastBuildDate()
132 if( m_Doc != 0L)
133 return m_Doc->lastBuildDate();
134 else
135 return QDateTime::currentDateTime();
138 QDateTime RSSDocument::pubDate()
140 if( m_Doc != 0L)
141 return m_Doc->pubDate();
142 else
143 return QDateTime::currentDateTime();
146 QString RSSDocument::copyright()
148 if( m_Doc != 0L)
149 return m_Doc->copyright();
150 else
151 return "";
154 QStringList RSSDocument::articles()
156 if( m_Doc != 0L)
158 kDebug() << "Document giving articles..." << endl;
159 Article::List list = m_Doc->articles();
160 QStringList stringList;
162 for(Article::List::ConstIterator it = list.begin(); it != list.end(); ++it)
163 stringList.append((*it).title());
164 return stringList;
166 else
167 return QStringList();
170 DCOPRef RSSDocument::article(int idx)
172 if(m_list.at(idx))
173 return DCOPRef(m_list.at(idx));
174 else
175 return DCOPRef();
178 int RSSDocument::count()
180 if( m_Doc != 0L)
181 return m_Doc->articles().count();
182 return 0;
185 QString RSSDocument::link()
187 if( m_Doc != 0L)
188 return m_Doc->link().prettyUrl();
189 else
190 return "";
193 QString RSSDocument::description()
195 if( m_Doc != 0L)
196 return m_Doc->description();
197 else
198 return "";
201 QString RSSDocument::title()
203 if( m_Doc != 0L)
204 return m_Doc->title();
205 else
206 return "";
209 QString RSSDocument::verbVersion()
211 if( m_Doc != 0L)
212 return m_Doc->verbVersion();
213 else
214 return "";
217 QString RSSDocument::pixmapURL()
219 if( m_Doc != 0L)
220 if( m_Doc->image() )
221 return m_Doc->image()->url().prettyUrl();
222 else
223 return "";
224 else
225 return "";
228 QPixmap RSSDocument::pixmap()
230 return m_pix;
233 bool RSSDocument::documentValid()
235 if (m_Doc != 0L)
236 return true;
237 else
238 return false;
241 bool RSSDocument::pixmapValid()
243 return !m_pix.isNull();
246 void RSSDocument::refresh()
248 kDebug() << "Mod time " << m_Timeout.toString() << endl;
249 kDebug() << "Current time " << QDateTime::currentDateTime().toString() << endl;
251 if(!m_isLoading && (QDateTime::currentDateTime() >= m_Timeout))
253 kDebug() << "Document going to refresh" << endl;
254 m_isLoading = true;
255 Loader *loader = Loader::create(this,
256 SLOT(loadingComplete(Loader *, Document, Status)));
257 loader->loadFrom(KUrl( m_Url ), new FileRetriever());
258 documentUpdating(DCOPRef(this));
260 else
262 documentUpdated(DCOPRef(this));
263 if(pixmapValid())
264 pixmapUpdated(DCOPRef(this));
266 else
268 // Refactor this!
269 Image *img = m_Doc->image();
270 if ( img )
272 connect(img, SIGNAL(gotPixmap(const QPixmap &)),
273 SLOT(pixmapLoaded(const QPixmap &)));
274 img->getPixmap();
275 pixmapUpdating(DCOPRef(this));
283 int RSSDocument::maxAge()
285 return m_maxAge;
288 void RSSDocument::setMaxAge(int _min)
290 m_Timeout.addSecs(-m_maxAge);
291 m_maxAge = _min;
292 m_Timeout.addSecs(m_maxAge);
295 int RSSDocument::state( const QString &title) const
297 return m_state[title];
300 void RSSDocument::setState( const QString &title, int s )
302 m_state[title] = s;
305 void RSSDocument::read( const QString &title)
307 m_state[title] = 3;
310 #include "service.moc"