22 #include <tdeconfig.h>
24 #include <tdeio/job.h>
25 #include <tdeglobal.h>
26 #include <tdemessagebox.h>
27 #include <tdelocale.h>
29 #include <tqptrdict.h>
30 #include <tqwindowdefs.h>
33 #include "provider.moc"
43 KURL mDownloadUrlLatest;
44 KURL mDownloadUrlScore;
45 KURL mDownloadUrlDownloads;
48 static TQPtrDict<ProviderPrivate> *d_ptr_prov = 0;
50 static ProviderPrivate *d_prov(
const Provider *p)
54 d_ptr_prov =
new TQPtrDict<ProviderPrivate>();
55 d_ptr_prov->setAutoDelete(
true);
57 ProviderPrivate *ret = d_ptr_prov->find((
void*)p);
60 ret =
new ProviderPrivate();
61 d_ptr_prov->replace((
void*)p, ret);
68 if((variant ==
"latest") && (d_prov(
this)->mDownloadUrlLatest.isValid()))
69 return d_prov(
this)->mDownloadUrlLatest;
70 if((variant ==
"score") && (d_prov(
this)->mDownloadUrlScore.isValid()))
71 return d_prov(
this)->mDownloadUrlScore;
72 if((variant ==
"downloads") && (d_prov(
this)->mDownloadUrlDownloads.isValid()))
73 return d_prov(
this)->mDownloadUrlDownloads;
80 Provider::Provider( TQString type, TQWidget* parent ) : mNoUpload( false ), mParent( parent ), mLoaded( false ), mContentType( type )
84 Provider::Provider(
const TQDomElement &e, TQString type, TQWidget* parent ) : mNoUpload( false ), mParent( parent ), mLoaded( false ), mContentType( type )
93 ProviderPrivate *p = d_ptr_prov->find(
this);
95 d_ptr_prov->remove(p);
97 if (d_ptr_prov->isEmpty())
175 void Provider::parseDomElement(
const TQDomElement &element )
177 bool contentAvailable =
false;
179 if ( element.tagName() !=
"provider" )
return;
182 for ( n = element.firstChild(); !n.isNull(); n = n.nextSibling() ) {
183 TQDomElement p = n.toElement();
185 if ( p.tagName() ==
"location" ) mBaseURL = p.text();
186 if ( p.tagName() ==
"icon" ) {
187 KURL iconurl( p.text() );
188 if(!iconurl.isValid()) iconurl.setPath( p.text() );
192 if ( p.tagName() ==
"noupload" )
setNoUpload(
true );
193 if ( p.tagName() ==
"name" )
setName( p.text().stripWhiteSpace() );
195 if ( p.tagName() ==
"services" ) {
197 for ( n2 = p.firstChild(); !n2.isNull(); n2 = n2.nextSibling() ) {
198 TQDomElement p = n2.toElement();
200 if ( p.tagName() ==
"content" ) contentAvailable =
true;
205 if (!mBaseURL.endsWith(
"/")) {
206 mBaseURL.append(
"/");
209 if (contentAvailable) {
211 KURL contentTypeUrl( mBaseURL +
"content/categories" );
213 kdDebug() <<
"Provider::parseDomElement(): contentTypeUrl: " << contentTypeUrl << endl;
215 TDEIO::TransferJob *job = TDEIO::get( KURL( contentTypeUrl ),
false,
false );
216 connect( job, TQ_SIGNAL( result( TDEIO::Job * ) ),
217 TQ_SLOT( slotJobResult( TDEIO::Job * ) ) );
218 connect( job, TQ_SIGNAL( data( TDEIO::Job *,
const TQByteArray & ) ),
219 TQ_SLOT( slotJobData( TDEIO::Job *,
const TQByteArray & ) ) );
223 TQDomElement Provider::createDomElement( TQDomDocument &doc, TQDomElement &parent )
225 TQDomElement entry = doc.createElement(
"stuff" );
226 parent.appendChild( entry );
228 TQDomElement n = doc.createElement(
"name" );
229 n.appendChild( doc.createTextNode(
name() ) );
230 entry.appendChild( n );
235 void Provider::slotJobData( TDEIO::Job *,
const TQByteArray &data )
237 kdDebug() <<
"Provider::slotJobData()" << endl;
239 if ( data.size() == 0 )
return;
241 TQCString str( data, data.size() + 1 );
243 mJobData.append( TQString::fromUtf8( str ) );
246 void Provider::slotJobResult( TDEIO::Job *job )
248 if ( job->error() ) {
250 job->showErrorDialog( mParent );
255 kdDebug() <<
"--CONTENT-START--" << endl << mJobData <<
"--CONT_END--"
259 if ( !doc.setContent( mJobData ) ) {
261 KMessageBox::error( mParent, i18n(
"Error parsing category list.") );
266 TQDomElement categories = doc.documentElement();
268 if ( categories.isNull() ) {
269 kdDebug() <<
"No document in Content.xml." << endl;
272 TQStringList desiredCategoryList;
273 TQString desiredCategories;
276 for ( n = categories.firstChild(); !n.isNull(); n = n.nextSibling() ) {
277 TQDomElement p = n.toElement();
279 if ( p.tagName() ==
"data" ) {
281 for ( n2 = p.firstChild(); !n2.isNull(); n2 = n2.nextSibling() ) {
282 TQDomElement p = n2.toElement();
284 if ( p.tagName() ==
"category" ) {
288 for ( n3 = p.firstChild(); !n3.isNull(); n3 = n3.nextSibling() ) {
289 TQDomElement p = n3.toElement();
291 if ( p.tagName() ==
"id" ) {
295 if ( p.tagName() ==
"name" ) {
300 if (mContentType ==
"") {
301 desiredCategoryList.append(
id);
304 if (
name.lower().contains(mContentType.lower())) {
305 desiredCategoryList.append(
id);
313 desiredCategories = desiredCategoryList.join(
"x");
317 int maxEntries = 100;
322 setDownloadUrl( KURL( mBaseURL +
"content/data?categories=" + desiredCategories +
"&search=&sortmode=alpha&page=0&pagesize=" + TQString(
"%1").arg(maxEntries) ) );
328 d_prov(
this)->mDownloadUrlLatest = KURL( mBaseURL +
"content/data?categories=" + desiredCategories +
"&search=&sortmode=new&page=0&pagesize=" + TQString(
"%1").arg(maxEntries) );
329 d_prov(
this)->mDownloadUrlScore = KURL( mBaseURL +
"content/data?categories=" + desiredCategories +
"&search=&sortmode=high&page=0&pagesize=" + TQString(
"%1").arg(maxEntries) );
330 d_prov(
this)->mDownloadUrlDownloads = KURL( mBaseURL +
"content/data?categories=" + desiredCategories +
"&search=&sortmode=down&page=0&pagesize=" + TQString(
"%1").arg(maxEntries) );
333 emit providerLoaded();
338 TQObject( parentWidget )
340 mProviders.setAutoDelete(
true );
345 kdDebug() <<
"ProviderLoader::load()" << endl;
351 TDEConfig *cfg = TDEGlobal::config();
352 cfg->setGroup(
"TDENewStuff");
354 TQString providersUrl = providersList;
355 if( providersUrl.isEmpty() ) {
356 providersUrl = cfg->readEntry(
"ProvidersUrl" );
359 if ( providersUrl.isEmpty() ) {
360 TQString server = cfg->readEntry(
"MasterServer",
361 "https://www.trinitydesktop.org" );
362 providersUrl = server +
"/ocs/providers.xml";
365 kdDebug() <<
"ProviderLoader::load(): providersUrl: " << providersUrl << endl;
367 TDEIO::TransferJob *job = TDEIO::get( KURL( providersUrl ),
false,
false );
368 connect( job, TQ_SIGNAL( result( TDEIO::Job * ) ),
369 TQ_SLOT( slotJobResult( TDEIO::Job * ) ) );
370 connect( job, TQ_SIGNAL( data( TDEIO::Job *,
const TQByteArray & ) ),
371 TQ_SLOT( slotJobData( TDEIO::Job *,
const TQByteArray & ) ) );
372 connect( job, TQ_SIGNAL( percent (TDEIO::Job *,
unsigned long) ),
373 TQ_SIGNAL( percent (TDEIO::Job *,
unsigned long) ) );
378 void ProviderLoader::slotJobData( TDEIO::Job *,
const TQByteArray &data )
380 kdDebug() <<
"ProviderLoader::slotJobData()" << endl;
382 if ( data.size() == 0 )
return;
384 TQCString str( data, data.size() + 1 );
386 mJobData.append( TQString::fromUtf8( str ) );
389 void ProviderLoader::slotJobResult( TDEIO::Job *job )
391 if ( job->error() ) {
392 job->showErrorDialog(
static_cast<TQWidget*
>(parent()) );
397 kdDebug() <<
"--PROVIDERS-START--" << endl << mJobData <<
"--PROV_END--"
401 if ( !doc.setContent( mJobData ) ) {
402 KMessageBox::error(
static_cast<TQWidget*
>(parent()), i18n(
"Error parsing providers list.") );
407 TQDomElement providers = doc.documentElement();
409 if ( providers.isNull() ) {
410 kdDebug() <<
"No document in Providers.xml." << endl;
416 for ( n = providers.firstChild(); !n.isNull(); n = n.nextSibling() ) {
417 TQDomElement p = n.toElement();
419 if ( p.tagName() ==
"provider" ) {
420 Provider* prov =
new Provider( p, mContentType,
static_cast<TQWidget*
>(parent()) );
421 mProviders.append( prov );
422 connect( prov, TQ_SIGNAL( providerLoaded() ),
this, TQ_SLOT( providerLoaded() ) );
427 void ProviderLoader::providerLoaded() {
429 bool allLoaded =
true;
430 for ( prov = mProviders.first(); prov; prov = mProviders.next() ) {
ProviderLoader(TQWidget *parentWidget)
Constructor.
void providersLoaded(Provider::List *)
Indicates that the list of providers has been successfully loaded.
void load(const TQString &type, const TQString &providerList=TQString::null)
Starts asynchronously loading the list of providers of the specified type.
TDENewStuff provider container.
KURL downloadUrl() const
Retrieves the download URL.
TQString name() const
Retrieves the common name of the provider.
void setNoUpload(bool)
Indicate whether provider supports uploads.
KURL uploadUrl() const
Retrieves the upload URL.
void setIcon(const KURL &)
Sets the URL for an icon for this provider.
Provider(TQString type=TQString::null, TQWidget *parent=0)
Constructor.
bool noUpload() const
Query whether provider supports uploads.
KURL icon() const
Retrieves the icon URL for this provider.
KURL noUploadUrl() const
Retrieves the URL where a user is led if the provider does not support uploads.
virtual ~Provider()
Destructor.
void setNoUploadUrl(const KURL &)
Sets the URL where a user is led if the provider does not support uploads.
KURL downloadUrlVariant(TQString variant) const
Variant to retrieve 'tagged' download URLs.
void setName(const TQString &)
Sets the common name of the provider.
void setUploadUrl(const KURL &)
Sets the upload URL.
void setDownloadUrl(const KURL &)
Sets the download URL.
Handles security releated issues, like signing, verifying.