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.
tdewebdev/lib/compatibility/knewstuff/provider.cpp

224 lines
5.1 KiB

/*
This file is part of KOrganizer.
Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include <tdeconfig.h>
#include <kdebug.h>
#include <tdeio/job.h>
#include <kglobal.h>
#include <kmessagebox.h>
#include <klocale.h>
#include "provider.h"
#include "provider.moc"
using namespace KNS;
Provider::Provider() : mNoUpload( false )
{
}
Provider::Provider( const TQDomElement &e ) : mNoUpload( false )
{
parseDomElement( e );
}
Provider::~Provider()
{
}
void Provider::setName( const TQString &name )
{
mName = name;
}
TQString Provider::name() const
{
return mName;
}
void Provider::setIcon( const KURL &url )
{
mIcon = url;
}
KURL Provider::icon() const
{
return mIcon;
}
void Provider::setDownloadUrl( const KURL &url )
{
mDownloadUrl = url;
}
KURL Provider::downloadUrl() const
{
return mDownloadUrl;
}
void Provider::setUploadUrl( const KURL &url )
{
mUploadUrl = url;
}
KURL Provider::uploadUrl() const
{
return mUploadUrl;
}
void Provider::setNoUploadUrl( const KURL &url )
{
mNoUploadUrl = url;
}
KURL Provider::noUploadUrl() const
{
return mNoUploadUrl;
}
void Provider::setNoUpload( bool enabled )
{
mNoUpload = enabled;
}
bool Provider::noUpload() const
{
return mNoUpload;
}
void Provider::parseDomElement( const TQDomElement &element )
{
if ( element.tagName() != "provider" ) return;
setDownloadUrl( KURL( element.attribute("downloadurl") ) );
setUploadUrl( KURL( element.attribute("uploadurl") ) );
setNoUploadUrl( KURL( element.attribute("nouploadurl") ) );
setIcon( KURL( element.attribute("icon") ) );
TQDomNode n;
for ( n = element.firstChild(); !n.isNull(); n = n.nextSibling() ) {
TQDomElement p = n.toElement();
if ( p.tagName() == "noupload" ) setNoUpload( true );
if ( p.tagName() == "title" ) setName( p.text().stripWhiteSpace() );
}
}
TQDomElement Provider::createDomElement( TQDomDocument &doc, TQDomElement &parent )
{
TQDomElement entry = doc.createElement( "stuff" );
parent.appendChild( entry );
TQDomElement n = doc.createElement( "name" );
n.appendChild( doc.createTextNode( name() ) );
entry.appendChild( n );
return entry;
}
ProviderLoader::ProviderLoader( TQWidget *parentWidget ) :
mParentWidget( parentWidget )
{
mProviders.setAutoDelete( true );
}
void ProviderLoader::load( const TQString &type, const TQString &providersList )
{
kdDebug(5850) << "ProviderLoader::load()" << endl;
mProviders.clear();
mJobData = "";
TDEConfig *cfg = TDEGlobal::config();
cfg->setGroup("KNewStuff");
TQString providersUrl = providersList;
if( providersUrl.isEmpty() )
providersUrl = cfg->readEntry( "ProvidersUrl" );
if ( providersUrl.isEmpty() ) {
// TODO: Replace the default by the real one.
TQString server = cfg->readEntry( "MasterServer",
"http://korganizer.kde.org" );
providersUrl = server + "/knewstuff/" + type + "/providers.xml";
}
kdDebug(5850) << "ProviderLoader::load(): providersUrl: " << providersUrl << endl;
TDEIO::TransferJob *job = TDEIO::get( KURL( providersUrl ) );
connect( job, TQT_SIGNAL( result( TDEIO::Job * ) ),
TQT_SLOT( slotJobResult( TDEIO::Job * ) ) );
connect( job, TQT_SIGNAL( data( TDEIO::Job *, const TQByteArray & ) ),
TQT_SLOT( slotJobData( TDEIO::Job *, const TQByteArray & ) ) );
// job->dumpObjectInfo();
}
void ProviderLoader::slotJobData( TDEIO::Job *, const TQByteArray &data )
{
kdDebug(5850) << "ProviderLoader::slotJobData()" << endl;
if ( data.size() == 0 ) return;
TQCString str( data, data.size() + 1 );
mJobData.append( TQString::fromUtf8( str ) );
}
void ProviderLoader::slotJobResult( TDEIO::Job *job )
{
if ( job->error() ) {
job->showErrorDialog( mParentWidget );
}
kdDebug(5850) << "--PROVIDERS-START--" << endl << mJobData << "--PROV_END--"
<< endl;
TQDomDocument doc;
if ( !doc.setContent( mJobData ) ) {
KMessageBox::error( mParentWidget, i18n("Error parsing providers list.") );
return;
}
TQDomElement providers = doc.documentElement();
if ( providers.isNull() ) {
kdDebug(5850) << "No document in Providers.xml." << endl;
}
TQDomNode n;
for ( n = providers.firstChild(); !n.isNull(); n = n.nextSibling() ) {
TQDomElement p = n.toElement();
mProviders.append( new Provider( p ) );
}
emit providersLoaded( &mProviders );
}