TDE core libraries
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.

provider.cpp 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. This file is part of KOrganizer.
  3. Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org>
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public License
  13. along with this library; see the file COPYING.LIB. If not, write to
  14. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. #include <tdeconfig.h>
  18. #include <kdebug.h>
  19. #include <tdeio/job.h>
  20. #include <tdeglobal.h>
  21. #include <tdemessagebox.h>
  22. #include <tdelocale.h>
  23. #include <tqptrdict.h>
  24. #include <tqwindowdefs.h>
  25. #include "provider.h"
  26. #include "provider.moc"
  27. using namespace KNS;
  28. // BCI for KDE 3.5 only
  29. class ProviderPrivate
  30. {
  31. public:
  32. ProviderPrivate(){}
  33. KURL mDownloadUrlLatest;
  34. KURL mDownloadUrlScore;
  35. KURL mDownloadUrlDownloads;
  36. };
  37. static TQPtrDict<ProviderPrivate> *d_ptr_prov = 0;
  38. static ProviderPrivate *d_prov(const Provider *p)
  39. {
  40. if(!d_ptr_prov)
  41. {
  42. d_ptr_prov = new TQPtrDict<ProviderPrivate>();
  43. d_ptr_prov->setAutoDelete(true);
  44. }
  45. ProviderPrivate *ret = d_ptr_prov->find((void*)p);
  46. if(!ret)
  47. {
  48. ret = new ProviderPrivate();
  49. d_ptr_prov->replace((void*)p, ret);
  50. }
  51. return ret;
  52. }
  53. KURL Provider::downloadUrlVariant( TQString variant ) const
  54. {
  55. if((variant == "latest") && (d_prov(this)->mDownloadUrlLatest.isValid()))
  56. return d_prov(this)->mDownloadUrlLatest;
  57. if((variant == "score") && (d_prov(this)->mDownloadUrlScore.isValid()))
  58. return d_prov(this)->mDownloadUrlScore;
  59. if((variant == "downloads") && (d_prov(this)->mDownloadUrlDownloads.isValid()))
  60. return d_prov(this)->mDownloadUrlDownloads;
  61. return mDownloadUrl;
  62. }
  63. // BCI part ends here
  64. Provider::Provider() : mNoUpload( false )
  65. {
  66. }
  67. Provider::Provider( const TQDomElement &e ) : mNoUpload( false )
  68. {
  69. parseDomElement( e );
  70. }
  71. Provider::~Provider()
  72. {
  73. if (d_ptr_prov)
  74. {
  75. ProviderPrivate *p = d_ptr_prov->find(this);
  76. if (p)
  77. d_ptr_prov->remove(p);
  78. if (d_ptr_prov->isEmpty())
  79. {
  80. delete d_ptr_prov;
  81. d_ptr_prov = 0L;
  82. }
  83. }
  84. }
  85. void Provider::setName( const TQString &name )
  86. {
  87. mName = name;
  88. }
  89. TQString Provider::name() const
  90. {
  91. return mName;
  92. }
  93. void Provider::setIcon( const KURL &url )
  94. {
  95. mIcon = url;
  96. }
  97. KURL Provider::icon() const
  98. {
  99. return mIcon;
  100. }
  101. void Provider::setDownloadUrl( const KURL &url )
  102. {
  103. mDownloadUrl = url;
  104. }
  105. KURL Provider::downloadUrl() const
  106. {
  107. return mDownloadUrl;
  108. }
  109. void Provider::setUploadUrl( const KURL &url )
  110. {
  111. mUploadUrl = url;
  112. }
  113. KURL Provider::uploadUrl() const
  114. {
  115. return mUploadUrl;
  116. }
  117. void Provider::setNoUploadUrl( const KURL &url )
  118. {
  119. mNoUploadUrl = url;
  120. }
  121. KURL Provider::noUploadUrl() const
  122. {
  123. return mNoUploadUrl;
  124. }
  125. void Provider::setNoUpload( bool enabled )
  126. {
  127. mNoUpload = enabled;
  128. }
  129. bool Provider::noUpload() const
  130. {
  131. return mNoUpload;
  132. }
  133. void Provider::parseDomElement( const TQDomElement &element )
  134. {
  135. if ( element.tagName() != "provider" ) return;
  136. setDownloadUrl( KURL( element.attribute("downloadurl") ) );
  137. setUploadUrl( KURL( element.attribute("uploadurl") ) );
  138. setNoUploadUrl( KURL( element.attribute("nouploadurl") ) );
  139. d_prov(this)->mDownloadUrlLatest = KURL( element.attribute("downloadurl-latest") );
  140. d_prov(this)->mDownloadUrlScore = KURL( element.attribute("downloadurl-score") );
  141. d_prov(this)->mDownloadUrlDownloads = KURL( element.attribute("downloadurl-downloads") );
  142. KURL iconurl( element.attribute("icon") );
  143. if(!iconurl.isValid()) iconurl.setPath( element.attribute("icon") );
  144. setIcon( iconurl );
  145. TQDomNode n;
  146. for ( n = element.firstChild(); !n.isNull(); n = n.nextSibling() ) {
  147. TQDomElement p = n.toElement();
  148. if ( p.tagName() == "noupload" ) setNoUpload( true );
  149. if ( p.tagName() == "title" ) setName( p.text().stripWhiteSpace() );
  150. }
  151. }
  152. TQDomElement Provider::createDomElement( TQDomDocument &doc, TQDomElement &parent )
  153. {
  154. TQDomElement entry = doc.createElement( "stuff" );
  155. parent.appendChild( entry );
  156. TQDomElement n = doc.createElement( "name" );
  157. n.appendChild( doc.createTextNode( name() ) );
  158. entry.appendChild( n );
  159. return entry;
  160. }
  161. ProviderLoader::ProviderLoader( TQWidget *parentWidget ) :
  162. TQObject( parentWidget )
  163. {
  164. mProviders.setAutoDelete( true );
  165. }
  166. void ProviderLoader::load( const TQString &type, const TQString &providersList )
  167. {
  168. kdDebug() << "ProviderLoader::load()" << endl;
  169. mProviders.clear();
  170. mJobData = "";
  171. TDEConfig *cfg = TDEGlobal::config();
  172. cfg->setGroup("KNewStuff");
  173. TQString providersUrl = providersList;
  174. if( providersUrl.isEmpty() )
  175. providersUrl = cfg->readEntry( "ProvidersUrl" );
  176. if ( providersUrl.isEmpty() ) {
  177. // TODO: Replace the default by the real one.
  178. TQString server = cfg->readEntry( "MasterServer",
  179. "http://korganizer.kde.org" );
  180. providersUrl = server + "/knewstuff/" + type + "/providers.xml";
  181. }
  182. kdDebug() << "ProviderLoader::load(): providersUrl: " << providersUrl << endl;
  183. TDEIO::TransferJob *job = TDEIO::get( KURL( providersUrl ), false, false );
  184. connect( job, TQT_SIGNAL( result( TDEIO::Job * ) ),
  185. TQT_SLOT( slotJobResult( TDEIO::Job * ) ) );
  186. connect( job, TQT_SIGNAL( data( TDEIO::Job *, const TQByteArray & ) ),
  187. TQT_SLOT( slotJobData( TDEIO::Job *, const TQByteArray & ) ) );
  188. // job->dumpObjectInfo();
  189. }
  190. void ProviderLoader::slotJobData( TDEIO::Job *, const TQByteArray &data )
  191. {
  192. kdDebug() << "ProviderLoader::slotJobData()" << endl;
  193. if ( data.size() == 0 ) return;
  194. TQCString str( data, data.size() + 1 );
  195. mJobData.append( TQString::fromUtf8( str ) );
  196. }
  197. void ProviderLoader::slotJobResult( TDEIO::Job *job )
  198. {
  199. if ( job->error() ) {
  200. job->showErrorDialog( TQT_TQWIDGET(parent()) );
  201. }
  202. kdDebug() << "--PROVIDERS-START--" << endl << mJobData << "--PROV_END--"
  203. << endl;
  204. TQDomDocument doc;
  205. if ( !doc.setContent( mJobData ) ) {
  206. KMessageBox::error( TQT_TQWIDGET(parent()), i18n("Error parsing providers list.") );
  207. return;
  208. }
  209. TQDomElement providers = doc.documentElement();
  210. if ( providers.isNull() ) {
  211. kdDebug() << "No document in Providers.xml." << endl;
  212. }
  213. TQDomNode n;
  214. for ( n = providers.firstChild(); !n.isNull(); n = n.nextSibling() ) {
  215. TQDomElement p = n.toElement();
  216. if ( p.tagName() == "provider" ) {
  217. mProviders.append( new Provider( p ) );
  218. }
  219. }
  220. emit providersLoaded( &mProviders );
  221. }