TDE network applications
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

query.cpp 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /* $Id$ */
  2. /***************************************************************************
  3. query.cpp - A query interface to select RSS feeds.
  4. -------------------
  5. begin : Saturday 15 February 2003
  6. copyright : (C) 2003 by Ian Reinhart Geiser
  7. email : geiseri@kde.org
  8. ***************************************************************************/
  9. /***************************************************************************
  10. * *
  11. * This program is free software; you can redistribute it and/or modify *
  12. * it under the terms of the GNU General Public License as published by *
  13. * the Free Software Foundation; either version 2 of the License, or *
  14. * (at your option) any later version. *
  15. * *
  16. ***************************************************************************/
  17. #include "cache.h"
  18. #include "query.h"
  19. #include <kdebug.h>
  20. #include <krfcdate.h>
  21. #include "service.h"
  22. #include "xmlrpciface.h"
  23. using KXMLRPC::Server;
  24. void SlotCaller::call( TQObject *object, const char *slot,
  25. const KXMLRPC::Query::Result &result )
  26. {
  27. SlotCaller caller;
  28. connect( &caller, TQT_SIGNAL( signal( const KXMLRPC::Query::Result &) ),
  29. object, slot );
  30. emit caller.signal( result );
  31. }
  32. QueryService::QueryService( RSSService *service ) : TQObject(), DCOPObject( "RSSQuery" ),
  33. m_service( service )
  34. {
  35. m_xmlrpcServer = new KXMLRPC::Server( KURL( "http://www.syndic8.com/xmlrpc.php"), this );
  36. }
  37. TQStringList QueryService::listActive()
  38. {
  39. if ( !m_service )
  40. return TQStringList();
  41. return m_service->list();
  42. }
  43. void QueryService::cachedCall( const TQString &method,
  44. const TQValueList<TQVariant> &args,
  45. const char *slot )
  46. {
  47. kdDebug() << "Calling " << method << endl;
  48. const TQString cacheKey = Cache::getCacheKey( m_xmlrpcServer->url().url(),
  49. method, args );
  50. CacheEntry *cacheEntry = Cache::self().find( cacheKey );
  51. if ( cacheEntry != 0 && cacheEntry->isValid() ) {
  52. kdDebug() << "Using cached result." << endl;
  53. SlotCaller::call( this, slot, cacheEntry->result() );
  54. } else {
  55. kdDebug() << "No cached result found, querying server." << endl;
  56. m_xmlrpcServer->call( method, args, this, slot );
  57. }
  58. }
  59. void QueryService::updateCache( const KXMLRPC::Query::Result &result )
  60. {
  61. const TQString cacheKey = Cache::getCacheKey( result.server(),
  62. result.method(),
  63. result.args() );
  64. CacheEntry *cacheEntry = Cache::self().find( cacheKey );
  65. if ( cacheEntry == 0 ) {
  66. kdDebug() << "Inserting returned result into cache." << endl;
  67. Cache::self().insert( cacheKey, result );
  68. }
  69. }
  70. void QueryService::findFeeds( const TQString &query )
  71. {
  72. kdDebug() << "QueryService::findFeeds()" << endl;
  73. TQStringList args;
  74. args << query << "headlines_rank";
  75. cachedCall( "syndic8.FindFeeds", Server::toVariantList( args ),
  76. TQT_SLOT( slotFoundFeeds( const KXMLRPC::Query::Result & ) ) );
  77. }
  78. void QueryService::findSites( const TQString& query )
  79. {
  80. kdDebug() << "QueryService::findSites()" << endl;
  81. cachedCall( "syndic8.FindSites", Server::toVariantList( query ),
  82. TQT_SLOT( slotFoundFeeds( const KXMLRPC::Query::Result & ) ) );
  83. }
  84. void QueryService::getFeedInfo( const TQVariant& ids )
  85. {
  86. kdDebug() << "QueryService::getFeedInfo()" << endl;
  87. cachedCall( "syndic8.GetFeedInfo", Server::toVariantList( ids ),
  88. TQT_SLOT( slotGotFeedInfo( const KXMLRPC::Query::Result & ) ) );
  89. }
  90. void QueryService::getCategories( const TQString &category )
  91. {
  92. kdDebug() << "QueryService::getCategories()" << endl;
  93. if ( category == "Top" ) {
  94. cachedCall( "syndic8.GetCategoryRoots", Server::toVariantList( TQString::fromLatin1( "DMOZ" ) ),
  95. TQT_SLOT( slotGotCategories( const KXMLRPC::Query::Result & ) ) );
  96. } else {
  97. TQStringList args;
  98. args << "DMOZ" << category;
  99. cachedCall( "syndic8.GetCategoryChildren", Server::toVariantList( args ),
  100. TQT_SLOT( slotGotCategories( const KXMLRPC::Query::Result & ) ) );
  101. }
  102. }
  103. void QueryService::getFeedsInCategory( const TQString &category )
  104. {
  105. kdDebug() << "QueryService::getFeedsInCategory()" << endl;
  106. TQStringList args;
  107. args << "DMOZ" << category;
  108. cachedCall( "syndic8.GetFeedsInCategory", Server::toVariantList( args ),
  109. TQT_SLOT( slotGotFeedsInCategory( const KXMLRPC::Query::Result & ) ) );
  110. }
  111. void QueryService::slotFoundFeeds( const KXMLRPC::Query::Result &result )
  112. {
  113. kdDebug() << "QueryService::slotFoundFeeds()" << endl;
  114. if ( !result.success() ) {
  115. kdWarning() << "Failed to query for feeds: " << result.errorString() << endl;
  116. return;
  117. }
  118. updateCache( result );
  119. TQValueList<int> ids;
  120. const TQValueList<TQVariant> values = result.data()[ 0 ].toList();
  121. TQValueList<TQVariant>::ConstIterator it = values.begin();
  122. TQValueList<TQVariant>::ConstIterator end = values.end();
  123. for ( ; it != end; ++it ) {
  124. ids << ( *it ).toInt();
  125. kdDebug() << "Found feed #" << ( *it ).toInt() << endl;
  126. }
  127. feedIds( ids );
  128. }
  129. void QueryService::slotGotFeedInfo( const KXMLRPC::Query::Result &result )
  130. {
  131. kdDebug() << "QueryService::slotGotFeedInfo()" << endl;
  132. if ( !result.success() ) {
  133. kdWarning() << "Failed to get feed info: " << result.errorString() << endl;
  134. return;
  135. }
  136. updateCache( result );
  137. TQMap<TQString, TQString> links;
  138. TQValueList<RSSNewsFeed> feeds;
  139. const TQValueList<TQVariant> feedInfos = result.data();
  140. TQValueList<TQVariant>::ConstIterator it = feedInfos.begin();
  141. TQValueList<TQVariant>::ConstIterator end = feedInfos.end();
  142. for ( ; it != end; ++it ) {
  143. const TQStringVariantMap feedInfo = ( *it ).toMap();
  144. const TQString name = feedInfo[ "sitename" ].toString();
  145. const TQString link = feedInfo[ "dataurl" ].toString();
  146. links[ name ] = link;
  147. RSSNewsFeed feed;
  148. feed.m_id = feedInfo[ "feedid" ].toUInt();
  149. feed.m_name = feedInfo[ "sitename" ].toString();
  150. feed.m_homePage = feedInfo[ "siteurl" ].toString();
  151. feed.m_sourceFile = feedInfo[ "dataurl" ].toString();
  152. feed.m_imageUrl = feedInfo[ "imageurl" ].toString();
  153. feed.m_webmaster = feedInfo[ "webmaster" ].toString();
  154. feed.m_editor = feedInfo[ "editor" ].toString();
  155. feed.m_publisher = feedInfo[ "publisher" ].toString();
  156. feed.m_creator = feedInfo[ "creator" ].toString();
  157. TQDateTime dateTime;
  158. dateTime.setTime_t( KRFCDate::parseDate( feedInfo[ "date_created" ].toString() ) );
  159. feed.m_dateCreated = dateTime;
  160. dateTime.setTime_t( KRFCDate::parseDate( feedInfo[ "date_approved" ].toString() ) );
  161. feed.m_dateApproved = dateTime;
  162. dateTime.setTime_t( KRFCDate::parseDate( feedInfo[ "date_xml_changed" ].toString() ) );
  163. feed.m_dateXmlChanged = dateTime;
  164. feed.m_fetchable = feedInfo[ "fetchable" ].toBool();
  165. feed.m_description = feedInfo[ "description" ].toString();
  166. feed.m_origin = feedInfo[ "origin" ].toString();
  167. feed.m_languageCode = feedInfo[ "lang_code" ].toString();
  168. feed.m_status = feedInfo[ "status" ].toString();
  169. feed.m_version = feedInfo[ "rss_version" ].toString();
  170. feed.m_views = feedInfo[ "views" ].toUInt();
  171. feed.m_headlinesPerDay = feedInfo[ "headlines_per_day" ].toUInt();
  172. feed.m_headlinesRank = feedInfo[ "headlines_rank" ].toUInt();
  173. feed.m_toolkit = feedInfo[ "toolkit" ].toString();
  174. feed.m_toolkitVersion = feedInfo[ "toolkit_version" ].toString();
  175. feed.m_pollingInterval = feedInfo[ "cur_polling_interval" ].toUInt();
  176. dateTime.setTime_t( feedInfo[ "last_poll_time" ].toUInt() );
  177. feed.m_lastPoll = dateTime;
  178. // ### feed.m_categories missing here!
  179. feeds << feed;
  180. kdDebug() << "Retrieved data for newsfeed '" << name << "' <" << link << ">" << endl;
  181. }
  182. feedInfo( links );
  183. feedInfo( feeds );
  184. }
  185. void QueryService::slotGotCategories( const KXMLRPC::Query::Result &result )
  186. {
  187. kdDebug() << "QueryService::slotGotCategories()" << endl;
  188. if ( !result.success() ) {
  189. kdWarning() << "Failed to get the list of categories: " << result.errorString() << endl;
  190. return;
  191. }
  192. updateCache( result );
  193. TQStringList categories;
  194. const TQValueList<TQVariant> cats = result.data()[ 0 ].toList();
  195. TQValueList<TQVariant>::ConstIterator it = cats.begin();
  196. TQValueList<TQVariant>::ConstIterator end = cats.end();
  197. for ( ; it != end; ++it )
  198. categories << ( *it ).toString();
  199. kdDebug() << "Got categories: " << categories.join( ", " ) << endl;
  200. gotCategories( categories );
  201. }
  202. void QueryService::slotGotFeedsInCategory( const KXMLRPC::Query::Result &result )
  203. {
  204. kdDebug() << "QueryService::slotGotFeedsInCategory()" << endl;
  205. if ( !result.success() ) {
  206. kdWarning() << "Failed to get the feeds in the given category: " << result.errorString() << endl;
  207. return;
  208. }
  209. updateCache( result );
  210. TQValueList<int> ids;
  211. const TQValueList<TQVariant> values = result.data()[ 0 ].toList();
  212. TQValueList<TQVariant>::ConstIterator it = values.begin();
  213. TQValueList<TQVariant>::ConstIterator end = values.end();
  214. for ( ; it != end; ++it ) {
  215. ids << ( *it ).toInt();
  216. kdDebug() << "Got feed in category: #" << ( *it ).toInt() << endl;
  217. }
  218. gotFeedsInCategory( ids );
  219. }
  220. #include "query.moc"
  221. // vim:ts=4:sw=4:noet