KNewStuff
provider.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <kconfig.h>
00022 #include <kdebug.h>
00023 #include <kio/job.h>
00024 #include <kglobal.h>
00025 #include <kmessagebox.h>
00026 #include <klocale.h>
00027
00028 #include <qptrdict.h>
00029 #include <qwindowdefs.h>
00030
00031 #include "provider.h"
00032 #include "provider.moc"
00033
00034 using namespace KNS;
00035
00036
00037
00038 class ProviderPrivate
00039 {
00040 public:
00041 ProviderPrivate(){}
00042 KURL mDownloadUrlLatest;
00043 KURL mDownloadUrlScore;
00044 KURL mDownloadUrlDownloads;
00045 };
00046
00047 static QPtrDict<ProviderPrivate> *d_ptr_prov = 0;
00048
00049 static ProviderPrivate *d_prov(const Provider *p)
00050 {
00051 if(!d_ptr_prov)
00052 {
00053 d_ptr_prov = new QPtrDict<ProviderPrivate>();
00054 d_ptr_prov->setAutoDelete(true);
00055 }
00056 ProviderPrivate *ret = d_ptr_prov->find((void*)p);
00057 if(!ret)
00058 {
00059 ret = new ProviderPrivate();
00060 d_ptr_prov->replace((void*)p, ret);
00061 }
00062 return ret;
00063 }
00064
00065 KURL Provider::downloadUrlVariant( QString variant ) const
00066 {
00067 if((variant == "latest") && (d_prov(this)->mDownloadUrlLatest.isValid()))
00068 return d_prov(this)->mDownloadUrlLatest;
00069 if((variant == "score") && (d_prov(this)->mDownloadUrlScore.isValid()))
00070 return d_prov(this)->mDownloadUrlScore;
00071 if((variant == "downloads") && (d_prov(this)->mDownloadUrlDownloads.isValid()))
00072 return d_prov(this)->mDownloadUrlDownloads;
00073
00074 return mDownloadUrl;
00075 }
00076
00077
00078
00079 Provider::Provider() : mNoUpload( false )
00080 {
00081 }
00082
00083 Provider::Provider( const QDomElement &e ) : mNoUpload( false )
00084 {
00085 parseDomElement( e );
00086 }
00087
00088 Provider::~Provider()
00089 {
00090 if (d_ptr_prov)
00091 {
00092 ProviderPrivate *p = d_ptr_prov->find(this);
00093 if (p)
00094 d_ptr_prov->remove(p);
00095
00096 if (d_ptr_prov->isEmpty())
00097 {
00098 delete d_ptr_prov;
00099 d_ptr_prov = 0L;
00100 }
00101 }
00102 }
00103
00104
00105 void Provider::setName( const QString &name )
00106 {
00107 mName = name;
00108 }
00109
00110 QString Provider::name() const
00111 {
00112 return mName;
00113 }
00114
00115
00116 void Provider::setIcon( const KURL &url )
00117 {
00118 mIcon = url;
00119 }
00120
00121 KURL Provider::icon() const
00122 {
00123 return mIcon;
00124 }
00125
00126
00127 void Provider::setDownloadUrl( const KURL &url )
00128 {
00129 mDownloadUrl = url;
00130 }
00131
00132 KURL Provider::downloadUrl() const
00133 {
00134 return mDownloadUrl;
00135 }
00136
00137
00138 void Provider::setUploadUrl( const KURL &url )
00139 {
00140 mUploadUrl = url;
00141 }
00142
00143 KURL Provider::uploadUrl() const
00144 {
00145 return mUploadUrl;
00146 }
00147
00148
00149 void Provider::setNoUploadUrl( const KURL &url )
00150 {
00151 mNoUploadUrl = url;
00152 }
00153
00154 KURL Provider::noUploadUrl() const
00155 {
00156 return mNoUploadUrl;
00157 }
00158
00159
00160 void Provider::setNoUpload( bool enabled )
00161 {
00162 mNoUpload = enabled;
00163 }
00164
00165 bool Provider::noUpload() const
00166 {
00167 return mNoUpload;
00168 }
00169
00170
00171 void Provider::parseDomElement( const QDomElement &element )
00172 {
00173 if ( element.tagName() != "provider" ) return;
00174
00175 setDownloadUrl( KURL( element.attribute("downloadurl") ) );
00176 setUploadUrl( KURL( element.attribute("uploadurl") ) );
00177 setNoUploadUrl( KURL( element.attribute("nouploadurl") ) );
00178
00179 d_prov(this)->mDownloadUrlLatest = KURL( element.attribute("downloadurl-latest") );
00180 d_prov(this)->mDownloadUrlScore = KURL( element.attribute("downloadurl-score") );
00181 d_prov(this)->mDownloadUrlDownloads = KURL( element.attribute("downloadurl-downloads") );
00182
00183 KURL iconurl( element.attribute("icon") );
00184 if(!iconurl.isValid()) iconurl.setPath( element.attribute("icon") );
00185 setIcon( iconurl );
00186
00187 QDomNode n;
00188 for ( n = element.firstChild(); !n.isNull(); n = n.nextSibling() ) {
00189 QDomElement p = n.toElement();
00190
00191 if ( p.tagName() == "noupload" ) setNoUpload( true );
00192 if ( p.tagName() == "title" ) setName( p.text().stripWhiteSpace() );
00193 }
00194 }
00195
00196 QDomElement Provider::createDomElement( QDomDocument &doc, QDomElement &parent )
00197 {
00198 QDomElement entry = doc.createElement( "stuff" );
00199 parent.appendChild( entry );
00200
00201 QDomElement n = doc.createElement( "name" );
00202 n.appendChild( doc.createTextNode( name() ) );
00203 entry.appendChild( n );
00204
00205 return entry;
00206 }
00207
00208
00209 ProviderLoader::ProviderLoader( QWidget *parentWidget ) :
00210 QObject( parentWidget )
00211 {
00212 mProviders.setAutoDelete( true );
00213 }
00214
00215 void ProviderLoader::load( const QString &type, const QString &providersList )
00216 {
00217 kdDebug() << "ProviderLoader::load()" << endl;
00218
00219 mProviders.clear();
00220 mJobData = "";
00221
00222 KConfig *cfg = KGlobal::config();
00223 cfg->setGroup("KNewStuff");
00224
00225 QString providersUrl = providersList;
00226 if( providersUrl.isEmpty() )
00227 providersUrl = cfg->readEntry( "ProvidersUrl" );
00228
00229 if ( providersUrl.isEmpty() ) {
00230
00231 QString server = cfg->readEntry( "MasterServer",
00232 "http://korganizer.kde.org" );
00233
00234 providersUrl = server + "/knewstuff/" + type + "/providers.xml";
00235 }
00236
00237 kdDebug() << "ProviderLoader::load(): providersUrl: " << providersUrl << endl;
00238
00239 KIO::TransferJob *job = KIO::get( KURL( providersUrl ), false, false );
00240 connect( job, SIGNAL( result( KIO::Job * ) ),
00241 SLOT( slotJobResult( KIO::Job * ) ) );
00242 connect( job, SIGNAL( data( KIO::Job *, const QByteArray & ) ),
00243 SLOT( slotJobData( KIO::Job *, const QByteArray & ) ) );
00244
00245
00246 }
00247
00248 void ProviderLoader::slotJobData( KIO::Job *, const QByteArray &data )
00249 {
00250 kdDebug() << "ProviderLoader::slotJobData()" << endl;
00251
00252 if ( data.size() == 0 ) return;
00253
00254 QCString str( data, data.size() + 1 );
00255
00256 mJobData.append( QString::fromUtf8( str ) );
00257 }
00258
00259 void ProviderLoader::slotJobResult( KIO::Job *job )
00260 {
00261 if ( job->error() ) {
00262 job->showErrorDialog( static_cast<QWidget *>(parent()) );
00263 }
00264
00265 kdDebug() << "--PROVIDERS-START--" << endl << mJobData << "--PROV_END--"
00266 << endl;
00267
00268 QDomDocument doc;
00269 if ( !doc.setContent( mJobData ) ) {
00270 KMessageBox::error( static_cast<QWidget *>(parent()), i18n("Error parsing providers list.") );
00271 return;
00272 }
00273
00274 QDomElement providers = doc.documentElement();
00275
00276 if ( providers.isNull() ) {
00277 kdDebug() << "No document in Providers.xml." << endl;
00278 }
00279
00280 QDomNode n;
00281 for ( n = providers.firstChild(); !n.isNull(); n = n.nextSibling() ) {
00282 QDomElement p = n.toElement();
00283
00284 if ( p.tagName() == "provider" ) {
00285 mProviders.append( new Provider( p ) );
00286 }
00287 }
00288
00289 emit providersLoaded( &mProviders );
00290 }