3 /***************************************************************************
4 query.cpp - A query interface to select RSS feeds.
6 begin : Saturday 15 February 2003
7 copyright : (C) 2003 by Ian Reinhart Geiser
8 email : geiseri@kde.org
9 ***************************************************************************/
11 /***************************************************************************
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. *
18 ***************************************************************************/
28 #include "xmlrpciface.h"
30 using KXMLRPC::Server
;
32 void SlotCaller::call( QObject
*object
, const char *slot
,
33 const KXMLRPC::Query::Result
&result
)
36 connect( &caller
, SIGNAL( signal( const KXMLRPC::Query::Result
&) ),
38 emit caller
.signal( result
);
41 QueryService::QueryService( RSSService
*service
) : QObject(), DCOPObject( "RSSQuery" ),
44 m_xmlrpcServer
= new KXMLRPC::Server( KURL( "http://www.syndic8.com/xmlrpc.php"), this );
47 QStringList
QueryService::listActive()
51 return m_service
->list();
54 void QueryService::cachedCall( const QString
&method
,
55 const QList
<QVariant
> &args
,
58 kdDebug() << "Calling " << method
<< endl
;
60 const QString cacheKey
= Cache::getCacheKey( m_xmlrpcServer
->url().url(),
63 CacheEntry
*cacheEntry
= Cache::self().find( cacheKey
);
64 if ( cacheEntry
!= 0 && cacheEntry
->isValid() ) {
65 kdDebug() << "Using cached result." << endl
;
66 SlotCaller::call( this, slot
, cacheEntry
->result() );
68 kdDebug() << "No cached result found, querying server." << endl
;
69 m_xmlrpcServer
->call( method
, args
, this, slot
);
73 void QueryService::updateCache( const KXMLRPC::Query::Result
&result
)
75 const QString cacheKey
= Cache::getCacheKey( result
.server(),
79 CacheEntry
*cacheEntry
= Cache::self().find( cacheKey
);
80 if ( cacheEntry
== 0 ) {
81 kdDebug() << "Inserting returned result into cache." << endl
;
82 Cache::self().insert( cacheKey
, result
);
86 void QueryService::findFeeds( const QString
&query
)
88 kdDebug() << "QueryService::findFeeds()" << endl
;
91 args
<< query
<< "headlines_rank";
93 cachedCall( "syndic8.FindFeeds", Server::toVariantList( args
),
94 SLOT( slotFoundFeeds( const KXMLRPC::Query::Result
& ) ) );
97 void QueryService::findSites( const QString
& query
)
99 kdDebug() << "QueryService::findSites()" << endl
;
101 cachedCall( "syndic8.FindSites", Server::toVariantList( query
),
102 SLOT( slotFoundFeeds( const KXMLRPC::Query::Result
& ) ) );
105 void QueryService::getFeedInfo( const QVariant
& ids
)
107 kdDebug() << "QueryService::getFeedInfo()" << endl
;
109 cachedCall( "syndic8.GetFeedInfo", Server::toVariantList( ids
),
110 SLOT( slotGotFeedInfo( const KXMLRPC::Query::Result
& ) ) );
113 void QueryService::getCategories( const QString
&category
)
115 kdDebug() << "QueryService::getCategories()" << endl
;
117 if ( category
== "Top" ) {
118 cachedCall( "syndic8.GetCategoryRoots", Server::toVariantList( QString::fromLatin1( "DMOZ" ) ),
119 SLOT( slotGotCategories( const KXMLRPC::Query::Result
& ) ) );
122 args
<< "DMOZ" << category
;
123 cachedCall( "syndic8.GetCategoryChildren", Server::toVariantList( args
),
124 SLOT( slotGotCategories( const KXMLRPC::Query::Result
& ) ) );
128 void QueryService::getFeedsInCategory( const QString
&category
)
130 kdDebug() << "QueryService::getFeedsInCategory()" << endl
;
133 args
<< "DMOZ" << category
;
135 cachedCall( "syndic8.GetFeedsInCategory", Server::toVariantList( args
),
136 SLOT( slotGotFeedsInCategory( const KXMLRPC::Query::Result
& ) ) );
139 void QueryService::slotFoundFeeds( const KXMLRPC::Query::Result
&result
)
141 kdDebug() << "QueryService::slotFoundFeeds()" << endl
;
142 if ( !result
.success() ) {
143 kdWarning() << "Failed to query for feeds: " << result
.errorString() << endl
;
147 updateCache( result
);
151 const QList
<QVariant
> values
= result
.data()[ 0 ].toList();
152 QList
<QVariant
>::ConstIterator it
= values
.begin();
153 QList
<QVariant
>::ConstIterator end
= values
.end();
154 for ( ; it
!= end
; ++it
) {
155 ids
<< ( *it
).toInt();
156 kdDebug() << "Found feed #" << ( *it
).toInt() << endl
;
161 void QueryService::slotGotFeedInfo( const KXMLRPC::Query::Result
&result
)
163 kdDebug() << "QueryService::slotGotFeedInfo()" << endl
;
164 if ( !result
.success() ) {
165 kdWarning() << "Failed to get feed info: " << result
.errorString() << endl
;
169 updateCache( result
);
171 QMap
<QString
, QString
> links
;
172 QList
<RSSNewsFeed
> feeds
;
174 const QList
<QVariant
> feedInfos
= result
.data();
175 QList
<QVariant
>::ConstIterator it
= feedInfos
.begin();
176 QList
<QVariant
>::ConstIterator end
= feedInfos
.end();
177 for ( ; it
!= end
; ++it
) {
178 const QMap
<QString
, QVariant
> feedInfo
= ( *it
).toMap();
180 const QString name
= feedInfo
[ "sitename" ].toString();
181 const QString link
= feedInfo
[ "dataurl" ].toString();
182 links
[ name
] = link
;
185 feed
.m_id
= feedInfo
[ "feedid" ].toUInt();
186 feed
.m_name
= feedInfo
[ "sitename" ].toString();
187 feed
.m_homePage
= feedInfo
[ "siteurl" ].toString();
188 feed
.m_sourceFile
= feedInfo
[ "dataurl" ].toString();
189 feed
.m_imageUrl
= feedInfo
[ "imageurl" ].toString();
190 feed
.m_webmaster
= feedInfo
[ "webmaster" ].toString();
191 feed
.m_editor
= feedInfo
[ "editor" ].toString();
192 feed
.m_publisher
= feedInfo
[ "publisher" ].toString();
193 feed
.m_creator
= feedInfo
[ "creator" ].toString();
195 dateTime
.setTime_t( KRFCDate::parseDate( feedInfo
[ "date_created" ].toString() ) );
196 feed
.m_dateCreated
= dateTime
;
197 dateTime
.setTime_t( KRFCDate::parseDate( feedInfo
[ "date_approved" ].toString() ) );
198 feed
.m_dateApproved
= dateTime
;
199 dateTime
.setTime_t( KRFCDate::parseDate( feedInfo
[ "date_xml_changed" ].toString() ) );
200 feed
.m_dateXmlChanged
= dateTime
;
201 feed
.m_fetchable
= feedInfo
[ "fetchable" ].toBool();
202 feed
.m_description
= feedInfo
[ "description" ].toString();
203 feed
.m_origin
= feedInfo
[ "origin" ].toString();
204 feed
.m_languageCode
= feedInfo
[ "lang_code" ].toString();
205 feed
.m_status
= feedInfo
[ "status" ].toString();
206 feed
.m_version
= feedInfo
[ "rss_version" ].toString();
207 feed
.m_views
= feedInfo
[ "views" ].toUInt();
208 feed
.m_headlinesPerDay
= feedInfo
[ "headlines_per_day" ].toUInt();
209 feed
.m_headlinesRank
= feedInfo
[ "headlines_rank" ].toUInt();
210 feed
.m_toolkit
= feedInfo
[ "toolkit" ].toString();
211 feed
.m_toolkitVersion
= feedInfo
[ "toolkit_version" ].toString();
212 feed
.m_pollingInterval
= feedInfo
[ "cur_polling_interval" ].toUInt();
213 dateTime
.setTime_t( feedInfo
[ "last_poll_time" ].toUInt() );
214 feed
.m_lastPoll
= dateTime
;
215 // ### feed.m_categories missing here!
219 kdDebug() << "Retrieved data for newsfeed '" << name
<< "' <" << link
<< ">" << endl
;
226 void QueryService::slotGotCategories( const KXMLRPC::Query::Result
&result
)
228 kdDebug() << "QueryService::slotGotCategories()" << endl
;
229 if ( !result
.success() ) {
230 kdWarning() << "Failed to get the list of categories: " << result
.errorString() << endl
;
234 updateCache( result
);
236 QStringList categories
;
238 const QList
<QVariant
> cats
= result
.data()[ 0 ].toList();
239 QList
<QVariant
>::ConstIterator it
= cats
.begin();
240 QList
<QVariant
>::ConstIterator end
= cats
.end();
241 for ( ; it
!= end
; ++it
)
242 categories
<< ( *it
).toString();
244 kdDebug() << "Got categories: " << categories
.join( ", " ) << endl
;
245 gotCategories( categories
);
249 void QueryService::slotGotFeedsInCategory( const KXMLRPC::Query::Result
&result
)
251 kdDebug() << "QueryService::slotGotFeedsInCategory()" << endl
;
252 if ( !result
.success() ) {
253 kdWarning() << "Failed to get the feeds in the given category: " << result
.errorString() << endl
;
257 updateCache( result
);
261 const QList
<QVariant
> values
= result
.data()[ 0 ].toList();
262 QList
<QVariant
>::ConstIterator it
= values
.begin();
263 QList
<QVariant
>::ConstIterator end
= values
.end();
264 for ( ; it
!= end
; ++it
) {
265 ids
<< ( *it
).toInt();
266 kdDebug() << "Got feed in category: #" << ( *it
).toInt() << endl
;
269 gotFeedsInCategory( ids
);
273 // vim:ts=4:sw=4:noet