The regexp was not correct.
[kdenetwork.git] / dcoprss / query.cpp
blobb2c29fdfd4559949484d0425b62c07f90821f4ae
1 /* $Id$ */
3 /***************************************************************************
4 query.cpp - A query interface to select RSS feeds.
5 -------------------
6 begin : Saturday 15 February 2003
7 copyright : (C) 2003 by Ian Reinhart Geiser
8 email : geiseri@kde.org
9 ***************************************************************************/
11 /***************************************************************************
12 * *
13 * This program is free software; you can redistribute it and/or modify *
14 * it under the terms of the GNU General Public License as published by *
15 * the Free Software Foundation; either version 2 of the License, or *
16 * (at your option) any later version. *
17 * *
18 ***************************************************************************/
19 #include "cache.h"
20 #include "query.h"
22 #include <kdebug.h>
23 #include <krfcdate.h>
25 #include "service.h"
26 #include "xmlrpciface.h"
28 using KXMLRPC::Server;
30 void SlotCaller::call( QObject *object, const char *slot,
31 const KXMLRPC::Query::Result &result )
33 SlotCaller caller;
34 connect( &caller, SIGNAL( signal( const KXMLRPC::Query::Result &) ),
35 object, slot );
36 emit caller.signal( result );
39 QueryService::QueryService( RSSService *service ) : QObject(), DCOPObject( "RSSQuery" ),
40 m_service( service )
42 m_xmlrpcServer = new KXMLRPC::Server( KURL( "http://www.syndic8.com/xmlrpc.php"), this );
45 QStringList QueryService::listActive()
47 if ( !m_service )
48 return QStringList();
49 return m_service->list();
52 void QueryService::cachedCall( const QString &method,
53 const QValueList<QVariant> &args,
54 const char *slot )
56 kdDebug() << "Calling " << method << endl;
58 const QString cacheKey = Cache::getCacheKey( m_xmlrpcServer->url().url(),
59 method, args );
61 CacheEntry *cacheEntry = Cache::self().find( cacheKey );
62 if ( cacheEntry != 0 && cacheEntry->isValid() ) {
63 kdDebug() << "Using cached result." << endl;
64 SlotCaller::call( this, slot, cacheEntry->result() );
65 } else {
66 kdDebug() << "No cached result found, querying server." << endl;
67 m_xmlrpcServer->call( method, args, this, slot );
71 void QueryService::updateCache( const KXMLRPC::Query::Result &result )
73 const QString cacheKey = Cache::getCacheKey( result.server(),
74 result.method(),
75 result.args() );
77 CacheEntry *cacheEntry = Cache::self().find( cacheKey );
78 if ( cacheEntry == 0 ) {
79 kdDebug() << "Inserting returned result into cache." << endl;
80 Cache::self().insert( cacheKey, result );
84 void QueryService::findFeeds( const QString &query )
86 kdDebug() << "QueryService::findFeeds()" << endl;
88 QStringList args;
89 args << query << "headlines_rank";
91 cachedCall( "syndic8.FindFeeds", Server::toVariantList( args ),
92 SLOT( slotFoundFeeds( const KXMLRPC::Query::Result & ) ) );
95 void QueryService::findSites( const QString& query )
97 kdDebug() << "QueryService::findSites()" << endl;
99 cachedCall( "syndic8.FindSites", Server::toVariantList( query ),
100 SLOT( slotFoundFeeds( const KXMLRPC::Query::Result & ) ) );
103 void QueryService::getFeedInfo( const QVariant& ids )
105 kdDebug() << "QueryService::getFeedInfo()" << endl;
107 cachedCall( "syndic8.GetFeedInfo", Server::toVariantList( ids ),
108 SLOT( slotGotFeedInfo( const KXMLRPC::Query::Result & ) ) );
111 void QueryService::getCategories( const QString &category )
113 kdDebug() << "QueryService::getCategories()" << endl;
115 if ( category == "Top" ) {
116 cachedCall( "syndic8.GetCategoryRoots", Server::toVariantList( QString::fromLatin1( "DMOZ" ) ),
117 SLOT( slotGotCategories( const KXMLRPC::Query::Result & ) ) );
118 } else {
119 QStringList args;
120 args << "DMOZ" << category;
121 cachedCall( "syndic8.GetCategoryChildren", Server::toVariantList( args ),
122 SLOT( slotGotCategories( const KXMLRPC::Query::Result & ) ) );
126 void QueryService::getFeedsInCategory( const QString &category )
128 kdDebug() << "QueryService::getFeedsInCategory()" << endl;
130 QStringList args;
131 args << "DMOZ" << category;
133 cachedCall( "syndic8.GetFeedsInCategory", Server::toVariantList( args ),
134 SLOT( slotGotFeedsInCategory( const KXMLRPC::Query::Result & ) ) );
137 void QueryService::slotFoundFeeds( const KXMLRPC::Query::Result &result )
139 kdDebug() << "QueryService::slotFoundFeeds()" << endl;
140 if ( !result.success() ) {
141 kdWarning() << "Failed to query for feeds: " << result.errorString() << endl;
142 return;
145 updateCache( result );
147 QValueList<int> ids;
149 const QValueList<QVariant> values = result.data()[ 0 ].toList();
150 QValueList<QVariant>::ConstIterator it = values.begin();
151 QValueList<QVariant>::ConstIterator end = values.end();
152 for ( ; it != end; ++it ) {
153 ids << ( *it ).toInt();
154 kdDebug() << "Found feed #" << ( *it ).toInt() << endl;
156 feedIds( ids );
159 void QueryService::slotGotFeedInfo( const KXMLRPC::Query::Result &result )
161 kdDebug() << "QueryService::slotGotFeedInfo()" << endl;
162 if ( !result.success() ) {
163 kdWarning() << "Failed to get feed info: " << result.errorString() << endl;
164 return;
167 updateCache( result );
169 QMap<QString, QString> links;
170 QValueList<RSSNewsFeed> feeds;
172 const QValueList<QVariant> feedInfos = result.data();
173 QValueList<QVariant>::ConstIterator it = feedInfos.begin();
174 QValueList<QVariant>::ConstIterator end = feedInfos.end();
175 for ( ; it != end; ++it ) {
176 const QMap<QString, QVariant> feedInfo = ( *it ).toMap();
178 const QString name = feedInfo[ "sitename" ].toString();
179 const QString link = feedInfo[ "dataurl" ].toString();
180 links[ name ] = link;
182 RSSNewsFeed feed;
183 feed.m_id = feedInfo[ "feedid" ].toUInt();
184 feed.m_name = feedInfo[ "sitename" ].toString();
185 feed.m_homePage = feedInfo[ "siteurl" ].toString();
186 feed.m_sourceFile = feedInfo[ "dataurl" ].toString();
187 feed.m_imageUrl = feedInfo[ "imageurl" ].toString();
188 feed.m_webmaster = feedInfo[ "webmaster" ].toString();
189 feed.m_editor = feedInfo[ "editor" ].toString();
190 feed.m_publisher = feedInfo[ "publisher" ].toString();
191 feed.m_creator = feedInfo[ "creator" ].toString();
192 QDateTime dateTime;
193 dateTime.setTime_t( KRFCDate::parseDate( feedInfo[ "date_created" ].toString() ) );
194 feed.m_dateCreated = dateTime;
195 dateTime.setTime_t( KRFCDate::parseDate( feedInfo[ "date_approved" ].toString() ) );
196 feed.m_dateApproved = dateTime;
197 dateTime.setTime_t( KRFCDate::parseDate( feedInfo[ "date_xml_changed" ].toString() ) );
198 feed.m_dateXmlChanged = dateTime;
199 feed.m_fetchable = feedInfo[ "fetchable" ].toBool();
200 feed.m_description = feedInfo[ "description" ].toString();
201 feed.m_origin = feedInfo[ "origin" ].toString();
202 feed.m_languageCode = feedInfo[ "lang_code" ].toString();
203 feed.m_status = feedInfo[ "status" ].toString();
204 feed.m_version = feedInfo[ "rss_version" ].toString();
205 feed.m_views = feedInfo[ "views" ].toUInt();
206 feed.m_headlinesPerDay = feedInfo[ "headlines_per_day" ].toUInt();
207 feed.m_headlinesRank = feedInfo[ "headlines_rank" ].toUInt();
208 feed.m_toolkit = feedInfo[ "toolkit" ].toString();
209 feed.m_toolkitVersion = feedInfo[ "toolkit_version" ].toString();
210 feed.m_pollingInterval = feedInfo[ "cur_polling_interval" ].toUInt();
211 dateTime.setTime_t( feedInfo[ "last_poll_time" ].toUInt() );
212 feed.m_lastPoll = dateTime;
213 // ### feed.m_categories missing here!
215 feeds << feed;
217 kdDebug() << "Retrieved data for newsfeed '" << name << "' <" << link << ">" << endl;
220 feedInfo( links );
221 feedInfo( feeds );
224 void QueryService::slotGotCategories( const KXMLRPC::Query::Result &result )
226 kdDebug() << "QueryService::slotGotCategories()" << endl;
227 if ( !result.success() ) {
228 kdWarning() << "Failed to get the list of categories: " << result.errorString() << endl;
229 return;
232 updateCache( result );
234 QStringList categories;
236 const QValueList<QVariant> cats = result.data()[ 0 ].toList();
237 QValueList<QVariant>::ConstIterator it = cats.begin();
238 QValueList<QVariant>::ConstIterator end = cats.end();
239 for ( ; it != end; ++it )
240 categories << ( *it ).toString();
242 kdDebug() << "Got categories: " << categories.join( ", " ) << endl;
243 gotCategories( categories );
247 void QueryService::slotGotFeedsInCategory( const KXMLRPC::Query::Result &result )
249 kdDebug() << "QueryService::slotGotFeedsInCategory()" << endl;
250 if ( !result.success() ) {
251 kdWarning() << "Failed to get the feeds in the given category: " << result.errorString() << endl;
252 return;
255 updateCache( result );
257 QValueList<int> ids;
259 const QValueList<QVariant> values = result.data()[ 0 ].toList();
260 QValueList<QVariant>::ConstIterator it = values.begin();
261 QValueList<QVariant>::ConstIterator end = values.end();
262 for ( ; it != end; ++it ) {
263 ids << ( *it ).toInt();
264 kdDebug() << "Got feed in category: #" << ( *it ).toInt() << endl;
267 gotFeedsInCategory( ids );
270 #include "query.moc"
271 // vim:ts=4:sw=4:noet