summaryrefslogtreecommitdiffstats
path: root/dcoprss/query.cpp
blob: b2c29fdfd4559949484d0425b62c07f90821f4ae (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
/* $Id$ */

/***************************************************************************
                          query.cpp  -  A query interface to select RSS feeds.
                             -------------------
    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 "cache.h"
#include "query.h"

#include <kdebug.h>
#include <krfcdate.h>

#include "service.h"
#include "xmlrpciface.h"

using KXMLRPC::Server;

void SlotCaller::call( QObject *object, const char *slot,
                       const KXMLRPC::Query::Result &result )
{
	SlotCaller caller;
	connect( &caller, SIGNAL( signal( const KXMLRPC::Query::Result &) ),
	         object, slot );
	emit caller.signal( result );
}

QueryService::QueryService( RSSService *service ) : QObject(), DCOPObject( "RSSQuery" ),
	m_service( service )
{
	m_xmlrpcServer = new KXMLRPC::Server( KURL( "http://www.syndic8.com/xmlrpc.php"), this );
}

QStringList QueryService::listActive()
{
	if ( !m_service )
		return QStringList();
	return m_service->list();
}

void QueryService::cachedCall( const QString &method,
                               const QValueList<QVariant> &args,
                               const char *slot )
{
	kdDebug() << "Calling " << method << endl;

	const QString cacheKey = Cache::getCacheKey( m_xmlrpcServer->url().url(),
	                                             method, args );

	CacheEntry *cacheEntry = Cache::self().find( cacheKey );
	if ( cacheEntry != 0 && cacheEntry->isValid() ) {
		kdDebug() << "Using cached result." << endl;
		SlotCaller::call( this, slot, cacheEntry->result() );
	} else {
		kdDebug() << "No cached result found, querying server." << endl;
		m_xmlrpcServer->call( method, args, this, slot );
	}
}

void QueryService::updateCache( const KXMLRPC::Query::Result &result )
{
	const QString cacheKey = Cache::getCacheKey( result.server(),
	                                             result.method(),
	                                             result.args() );

	CacheEntry *cacheEntry = Cache::self().find( cacheKey );
	if ( cacheEntry == 0 ) {
		kdDebug() << "Inserting returned result into cache." << endl;
		Cache::self().insert( cacheKey, result );
	}
}

void QueryService::findFeeds( const QString &query )
{
	kdDebug() << "QueryService::findFeeds()" << endl;

	QStringList args;
	args << query << "headlines_rank";

	cachedCall( "syndic8.FindFeeds", Server::toVariantList( args ),
	            SLOT( slotFoundFeeds( const KXMLRPC::Query::Result & ) ) );
}

void QueryService::findSites( const QString& query )
{
	kdDebug() << "QueryService::findSites()" << endl;

	cachedCall( "syndic8.FindSites", Server::toVariantList( query ),
	            SLOT( slotFoundFeeds( const KXMLRPC::Query::Result & ) ) );
}

void QueryService::getFeedInfo( const QVariant& ids )
{
	kdDebug() << "QueryService::getFeedInfo()" << endl;

	cachedCall( "syndic8.GetFeedInfo", Server::toVariantList( ids ),
	            SLOT( slotGotFeedInfo( const KXMLRPC::Query::Result & ) ) );
}

void QueryService::getCategories( const QString &category )
{
	kdDebug() << "QueryService::getCategories()" << endl;

	if ( category == "Top" ) {
		cachedCall( "syndic8.GetCategoryRoots", Server::toVariantList( QString::fromLatin1( "DMOZ" ) ),
		            SLOT( slotGotCategories( const KXMLRPC::Query::Result & ) ) );
	} else {
		QStringList args;
		args << "DMOZ" << category;
		cachedCall( "syndic8.GetCategoryChildren", Server::toVariantList( args ),
		            SLOT( slotGotCategories( const KXMLRPC::Query::Result & ) ) );
	}
}

void QueryService::getFeedsInCategory( const QString &category )
{
	kdDebug() << "QueryService::getFeedsInCategory()" << endl;

	QStringList args;
	args << "DMOZ" << category;

	cachedCall( "syndic8.GetFeedsInCategory", Server::toVariantList( args ),
	            SLOT( slotGotFeedsInCategory( const KXMLRPC::Query::Result & ) ) );
}

void QueryService::slotFoundFeeds( const KXMLRPC::Query::Result &result )
{
	kdDebug() << "QueryService::slotFoundFeeds()" << endl;
	if ( !result.success() ) {
		kdWarning() << "Failed to query for feeds: " << result.errorString() << endl;
		return;
	}

	updateCache( result );

	QValueList<int> ids;

	const QValueList<QVariant> values = result.data()[ 0 ].toList();
	QValueList<QVariant>::ConstIterator it = values.begin();
	QValueList<QVariant>::ConstIterator end = values.end();
	for ( ; it != end; ++it ) {
		ids << ( *it ).toInt();
		kdDebug() << "Found feed #" << ( *it ).toInt() << endl;
	}
	feedIds( ids );
}

void QueryService::slotGotFeedInfo( const KXMLRPC::Query::Result &result )
{
	kdDebug() << "QueryService::slotGotFeedInfo()" << endl;
	if ( !result.success() ) {
		kdWarning() << "Failed to get feed info: " << result.errorString() << endl;
		return;
	}

	updateCache( result );

	QMap<QString, QString> links;
	QValueList<RSSNewsFeed> feeds;

	const QValueList<QVariant> feedInfos = result.data();
	QValueList<QVariant>::ConstIterator it = feedInfos.begin();
	QValueList<QVariant>::ConstIterator end = feedInfos.end();
	for ( ; it != end; ++it ) {
		const QMap<QString, QVariant> feedInfo = ( *it ).toMap();

		const QString name = feedInfo[ "sitename" ].toString();
		const QString link = feedInfo[ "dataurl" ].toString();
		links[ name ] = link;

		RSSNewsFeed feed;
		feed.m_id = feedInfo[ "feedid" ].toUInt();
		feed.m_name = feedInfo[ "sitename" ].toString();
		feed.m_homePage = feedInfo[ "siteurl" ].toString();
		feed.m_sourceFile = feedInfo[ "dataurl" ].toString();
		feed.m_imageUrl = feedInfo[ "imageurl" ].toString();
		feed.m_webmaster = feedInfo[ "webmaster" ].toString();
		feed.m_editor = feedInfo[ "editor" ].toString();
		feed.m_publisher = feedInfo[ "publisher" ].toString();
		feed.m_creator = feedInfo[ "creator" ].toString();
		QDateTime dateTime;
		dateTime.setTime_t( KRFCDate::parseDate( feedInfo[ "date_created" ].toString() ) );
		feed.m_dateCreated = dateTime;
		dateTime.setTime_t( KRFCDate::parseDate( feedInfo[ "date_approved" ].toString() ) );
		feed.m_dateApproved = dateTime;
		dateTime.setTime_t( KRFCDate::parseDate( feedInfo[ "date_xml_changed" ].toString() ) );
		feed.m_dateXmlChanged = dateTime;
		feed.m_fetchable = feedInfo[ "fetchable" ].toBool();
		feed.m_description = feedInfo[ "description" ].toString();
		feed.m_origin = feedInfo[ "origin" ].toString();
		feed.m_languageCode = feedInfo[ "lang_code" ].toString();
		feed.m_status = feedInfo[ "status" ].toString();
		feed.m_version = feedInfo[ "rss_version" ].toString();
		feed.m_views = feedInfo[ "views" ].toUInt();
		feed.m_headlinesPerDay = feedInfo[ "headlines_per_day" ].toUInt();
		feed.m_headlinesRank = feedInfo[ "headlines_rank" ].toUInt();
		feed.m_toolkit = feedInfo[ "toolkit" ].toString();
		feed.m_toolkitVersion = feedInfo[ "toolkit_version" ].toString();
		feed.m_pollingInterval = feedInfo[ "cur_polling_interval" ].toUInt();
		dateTime.setTime_t( feedInfo[ "last_poll_time" ].toUInt() );
		feed.m_lastPoll = dateTime;
		// ### feed.m_categories missing here!

		feeds << feed;

		kdDebug() << "Retrieved data for newsfeed '" << name << "' <" << link << ">" << endl;
	}

	feedInfo( links );
	feedInfo( feeds );
}

void QueryService::slotGotCategories( const KXMLRPC::Query::Result &result )
{
	kdDebug() << "QueryService::slotGotCategories()" << endl;
	if ( !result.success() ) {
		kdWarning() << "Failed to get the list of categories: " << result.errorString() << endl;
		return;
	}

	updateCache( result );

	QStringList categories;

	const QValueList<QVariant> cats = result.data()[ 0 ].toList();
	QValueList<QVariant>::ConstIterator it = cats.begin();
	QValueList<QVariant>::ConstIterator end = cats.end();
	for (  ; it != end; ++it )
		categories << (  *it ).toString();

	kdDebug() << "Got categories: " << categories.join(  ", " ) << endl;
	gotCategories(  categories );

}

void QueryService::slotGotFeedsInCategory( const KXMLRPC::Query::Result &result )
{
	kdDebug() << "QueryService::slotGotFeedsInCategory()" << endl;
	if ( !result.success() ) {
		kdWarning() << "Failed to get the feeds in the given category: " << result.errorString() << endl;
		return;
	}

	updateCache( result );

	QValueList<int> ids;

	const QValueList<QVariant> values = result.data()[ 0 ].toList();
	QValueList<QVariant>::ConstIterator it = values.begin();
	QValueList<QVariant>::ConstIterator end = values.end();
	for (  ; it != end; ++it ) {
		ids << (  *it ).toInt();
		kdDebug() << "Got feed in category: #" << ( *it ).toInt() << endl;
	}

	gotFeedsInCategory(  ids );
}

#include "query.moc"
// vim:ts=4:sw=4:noet