summaryrefslogtreecommitdiffstats
path: root/dcoprss/query.cpp
blob: 0c6dd2f54cc32f1f5c3fb1053c04fd1731e7322d (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                : geiseri@kde.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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( TQObject *object, const char *slot,
                       const KXMLRPC::Query::Result &result )
{
	SlotCaller caller;
	connect( &caller, TQT_SIGNAL( signal( const KXMLRPC::Query::Result &) ),
	         object, slot );
	emit caller.signal( result );
}

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

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

void QueryService::cachedCall( const TQString &method,
                               const TQValueList<TQVariant> &args,
                               const char *slot )
{
	kdDebug() << "Calling " << method << endl;

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

	CacheEntry *cacheEntry = Cache::self().tqfind( 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 TQString cacheKey = Cache::getCacheKey( result.server(),
	                                             result.method(),
	                                             result.args() );

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

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

	TQStringList args;
	args << query << "headlines_rank";

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

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

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

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

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

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

	if ( category == "Top" ) {
		cachedCall( "syndic8.GetCategoryRoots", Server::toVariantList( TQString::tqfromLatin1( "DMOZ" ) ),
		            TQT_SLOT( slotGotCategories( const KXMLRPC::Query::Result & ) ) );
	} else {
		TQStringList args;
		args << "DMOZ" << category;
		cachedCall( "syndic8.GetCategoryChildren", Server::toVariantList( args ),
		            TQT_SLOT( slotGotCategories( const KXMLRPC::Query::Result & ) ) );
	}
}

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

	TQStringList args;
	args << "DMOZ" << category;

	cachedCall( "syndic8.GetFeedsInCategory", Server::toVariantList( args ),
	            TQT_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 );

	TQValueList<int> ids;

	const TQValueList<TQVariant> values = result.data()[ 0 ].toList();
	TQValueList<TQVariant>::ConstIterator it = values.begin();
	TQValueList<TQVariant>::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 );

	TQMap<TQString, TQString> links;
	TQValueList<RSSNewsFeed> feeds;

	const TQValueList<TQVariant> feedInfos = result.data();
	TQValueList<TQVariant>::ConstIterator it = feedInfos.begin();
	TQValueList<TQVariant>::ConstIterator end = feedInfos.end();
	for ( ; it != end; ++it ) {
		const TQMap<TQString, TQVariant> feedInfo = ( *it ).toMap();

		const TQString name = feedInfo[ "sitename" ].toString();
		const TQString 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();
		TQDateTime 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 );

	TQStringList categories;

	const TQValueList<TQVariant> cats = result.data()[ 0 ].toList();
	TQValueList<TQVariant>::ConstIterator it = cats.begin();
	TQValueList<TQVariant>::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 );

	TQValueList<int> ids;

	const TQValueList<TQVariant> values = result.data()[ 0 ].toList();
	TQValueList<TQVariant>::ConstIterator it = values.begin();
	TQValueList<TQVariant>::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