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.

document.cpp 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /* $Id$ */
  2. /***************************************************************************
  3. document.cpp - A DCOP Service to provide RSS data
  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 <kdebug.h>
  18. #include <tqdatetime.h>
  19. #include <kurl.h>
  20. #include "service.h"
  21. RSSDocument::RSSDocument(const TQString& url) :
  22. TQObject(), DCOPObject(), m_Url(url)
  23. {
  24. m_list.setAutoDelete( true );
  25. m_Doc = 0L;
  26. m_pix = TQPixmap();
  27. m_isLoading = false;
  28. m_maxAge = 60;
  29. m_Timeout = TQDateTime::currentDateTime();
  30. m_state.clear();
  31. }
  32. RSSDocument::~RSSDocument()
  33. {
  34. kdDebug() << "Document going away..." << endl;
  35. delete m_Doc;
  36. }
  37. void RSSDocument::loadingComplete(Loader *ldr, Document doc, Status stat)
  38. {
  39. if( m_Doc != 0L)
  40. {
  41. delete m_Doc;
  42. }
  43. if (stat != RSS::Success)
  44. {
  45. kdDebug() << "Document error! Loader:" << ldr->errorCode() << " Parser:" << stat << endl;
  46. m_isLoading = false;
  47. m_Doc = 0L;
  48. if( stat == RSS::ParseError )
  49. documentUpdateError(DCOPRef(this), 1);
  50. else if( stat == RSS::RetrieveError )
  51. documentUpdateError(DCOPRef(this), 2);
  52. else
  53. documentUpdateError(DCOPRef(this), 3);
  54. }
  55. else
  56. {
  57. kdDebug() << "New Document is done..." << endl;
  58. m_Doc = new Document(doc);
  59. m_list.clear();
  60. Article::List list = doc.articles();
  61. for(Article::List::ConstIterator it = list.begin(); it != list.end(); ++it)
  62. {
  63. int state = m_state[(*it).title()];
  64. if( state == 0 ) m_state[(*it).title()] = 1; // new
  65. else if( state == 1 ) m_state[(*it).title()] = 2; // old message now
  66. m_list.append( new RSSArticle( new Article(*it)));
  67. }
  68. Image *img = m_Doc->image();
  69. if ( img )
  70. {
  71. connect(img, TQT_SIGNAL(gotPixmap(const TQPixmap &)),
  72. TQT_SLOT(pixmapLoaded(const TQPixmap &)));
  73. img->getPixmap();
  74. pixmapUpdating(DCOPRef(this));
  75. }
  76. m_isLoading = false;
  77. documentUpdated(DCOPRef(this));
  78. kdDebug() << "Old Mod time " << m_Timeout.toString() << endl;
  79. m_Timeout = m_Timeout.addSecs(m_maxAge * 60 );
  80. kdDebug() << "New Mod time " << m_Timeout.toString() << endl;
  81. }
  82. }
  83. void RSSDocument::pixmapLoaded(const TQPixmap &pix )
  84. {
  85. m_pix = pix;
  86. pixmapUpdated(DCOPRef(this));
  87. }
  88. TQString RSSDocument::webMaster()
  89. {
  90. if( m_Doc != 0L)
  91. return m_Doc->webMaster();
  92. else
  93. return "";
  94. }
  95. TQString RSSDocument::managingEditor()
  96. {
  97. if( m_Doc != 0L)
  98. return m_Doc->managingEditor();
  99. else
  100. return "";
  101. }
  102. TQString RSSDocument::rating()
  103. {
  104. if( m_Doc != 0L)
  105. return m_Doc->rating();
  106. else
  107. return "";
  108. }
  109. TQDateTime RSSDocument::lastBuildDate()
  110. {
  111. if( m_Doc != 0L)
  112. return m_Doc->lastBuildDate();
  113. else
  114. return TQDateTime::currentDateTime();
  115. }
  116. TQDateTime RSSDocument::pubDate()
  117. {
  118. if( m_Doc != 0L)
  119. return m_Doc->pubDate();
  120. else
  121. return TQDateTime::currentDateTime();
  122. }
  123. TQString RSSDocument::copyright()
  124. {
  125. if( m_Doc != 0L)
  126. return m_Doc->copyright();
  127. else
  128. return "";
  129. }
  130. TQStringList RSSDocument::articles()
  131. {
  132. if( m_Doc != 0L)
  133. {
  134. kdDebug() << "Document giving articles..." << endl;
  135. Article::List list = m_Doc->articles();
  136. TQStringList stringList;
  137. for(Article::List::ConstIterator it = list.begin(); it != list.end(); ++it)
  138. stringList.append((*it).title());
  139. return stringList;
  140. }
  141. else
  142. return TQStringList();
  143. }
  144. DCOPRef RSSDocument::article(int idx)
  145. {
  146. if(m_list.at(idx))
  147. return DCOPRef(m_list.at(idx));
  148. else
  149. return DCOPRef();
  150. }
  151. int RSSDocument::count()
  152. {
  153. if( m_Doc != 0L)
  154. return m_Doc->articles().count();
  155. return 0;
  156. }
  157. TQString RSSDocument::link()
  158. {
  159. if( m_Doc != 0L)
  160. return m_Doc->link().prettyURL();
  161. else
  162. return "";
  163. }
  164. TQString RSSDocument::description()
  165. {
  166. if( m_Doc != 0L)
  167. return m_Doc->description();
  168. else
  169. return "";
  170. }
  171. TQString RSSDocument::title()
  172. {
  173. if( m_Doc != 0L)
  174. return m_Doc->title();
  175. else
  176. return "";
  177. }
  178. TQString RSSDocument::verbVersion()
  179. {
  180. if( m_Doc != 0L)
  181. return m_Doc->verbVersion();
  182. else
  183. return "";
  184. }
  185. TQString RSSDocument::pixmapURL()
  186. {
  187. if( m_Doc != 0L)
  188. if( m_Doc->image() )
  189. return m_Doc->image()->url().prettyURL();
  190. else
  191. return "";
  192. else
  193. return "";
  194. }
  195. TQPixmap RSSDocument::pixmap()
  196. {
  197. return m_pix;
  198. }
  199. bool RSSDocument::documentValid()
  200. {
  201. if (m_Doc != 0L)
  202. return true;
  203. else
  204. return false;
  205. }
  206. bool RSSDocument::pixmapValid()
  207. {
  208. return !m_pix.isNull();
  209. }
  210. void RSSDocument::refresh()
  211. {
  212. kdDebug() << "Mod time " << m_Timeout.toString() << endl;
  213. kdDebug() << "Current time " << TQDateTime::currentDateTime().toString() << endl;
  214. if(!m_isLoading && (TQDateTime::currentDateTime() >= m_Timeout))
  215. {
  216. kdDebug() << "Document going to refresh" << endl;
  217. m_isLoading = true;
  218. Loader *loader = Loader::create(this,
  219. TQT_SLOT(loadingComplete(Loader *, Document, Status)));
  220. loader->loadFrom(KURL( m_Url ), new FileRetriever());
  221. documentUpdating(DCOPRef(this));
  222. }
  223. else
  224. {
  225. documentUpdated(DCOPRef(this));
  226. if(pixmapValid())
  227. pixmapUpdated(DCOPRef(this));
  228. /*
  229. else
  230. {
  231. // Refactor this!
  232. Image *img = m_Doc->image();
  233. if ( img )
  234. {
  235. connect(img, TQT_SIGNAL(gotPixmap(const TQPixmap &)),
  236. TQT_SLOT(pixmapLoaded(const TQPixmap &)));
  237. img->getPixmap();
  238. pixmapUpdating(DCOPRef(this));
  239. }
  240. }
  241. */
  242. }
  243. }
  244. int RSSDocument::maxAge()
  245. {
  246. return m_maxAge;
  247. }
  248. void RSSDocument::setMaxAge(int _min)
  249. {
  250. m_Timeout.addSecs(-m_maxAge);
  251. m_maxAge = _min;
  252. m_Timeout.addSecs(m_maxAge);
  253. }
  254. int RSSDocument::state( const TQString &title) const
  255. {
  256. return m_state[title];
  257. }
  258. void RSSDocument::setState( const TQString &title, int s )
  259. {
  260. m_state[title] = s;
  261. }
  262. void RSSDocument::read( const TQString &title)
  263. {
  264. m_state[title] = 3;
  265. }
  266. #include "service.moc"