knewstuff Library: provider.cpp Source File (knewstuff Library)

| # knewstuff Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# provider.cpp

    00001 /*
    00002     This file is part of KOrganizer.
    00003     Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org>
    00004
    00005     This library is free software; you can redistribute it and/or
    00006     modify it under the terms of the GNU Library General Public
    00007     License as published by the Free Software Foundation; either
    00008     version 2 of the License, or (at your option) any later version.
    00009
    00010     This library is distributed in the hope that it will be useful,
    00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013     Library General Public License for more details.
    00014
    00015     You should have received a copy of the GNU Library General Public License
    00016     along with this library; see the file COPYING.LIB.  If not, write to
    00017     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00018     Boston, MA 02111-1307, USA.
    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 "provider.h"
    00029 #include "provider.moc"
    00030
    00031 using namespace KNS;
    00032
    00033 Provider::Provider() : mNoUpload( false )
    00034 {
    00035 }
    00036
    00037 Provider::Provider( const QDomElement &e ) : mNoUpload( false )
    00038 {
    00039   parseDomElement( e );
    00040 }
    00041
    00042 Provider::~Provider()
    00043 {
    00044 }
    00045
    00046
    00047 void Provider::setName( const QString &name )
    00048 {
    00049   mName = name;
    00050 }
    00051
    00052 QString Provider::name() const
    00053 {
    00054   return mName;
    00055 }
    00056
    00057
    00058 void Provider::setIcon( const KURL &url )
    00059 {
    00060   mIcon = url;
    00061 }
    00062
    00063 KURL Provider::icon() const
    00064 {
    00065   return mIcon;
    00066 }
    00067
    00068
    00069 void Provider::setDownloadUrl( const KURL &url )
    00070 {
    00071   mDownloadUrl = url;
    00072 }
    00073
    00074 KURL Provider::downloadUrl() const
    00075 {
    00076   return mDownloadUrl;
    00077 }
    00078
    00079
    00080 void Provider::setUploadUrl( const KURL &url )
    00081 {
    00082   mUploadUrl = url;
    00083 }
    00084
    00085 KURL Provider::uploadUrl() const
    00086 {
    00087   return mUploadUrl;
    00088 }
    00089
    00090
    00091 void Provider::setNoUploadUrl( const KURL &url )
    00092 {
    00093   mNoUploadUrl = url;
    00094 }
    00095
    00096 KURL Provider::noUploadUrl() const
    00097 {
    00098   return mNoUploadUrl;
    00099 }
    00100
    00101
    00102 void Provider::setNoUpload( bool enabled )
    00103 {
    00104   mNoUpload = enabled;
    00105 }
    00106
    00107 bool Provider::noUpload() const
    00108 {
    00109   return mNoUpload;
    00110 }
    00111
    00112
    00113 void Provider::parseDomElement( const QDomElement &element )
    00114 {
    00115   if ( element.tagName() != "provider" ) return;
    00116
    00117   setDownloadUrl( KURL( element.attribute("downloadurl") ) );
    00118   setUploadUrl( KURL( element.attribute("uploadurl") ) );
    00119   setNoUploadUrl( KURL( element.attribute("nouploadurl") ) );
    00120   setIcon( KURL( element.attribute("icon") ) );
    00121
    00122   QDomNode n;
    00123   for ( n = element.firstChild(); !n.isNull(); n = n.nextSibling() ) {
    00124     QDomElement p = n.toElement();
    00125
    00126     if ( p.tagName() == "noupload" ) setNoUpload( true );
    00127     if ( p.tagName() == "title" ) setName( p.text().stripWhiteSpace() );
    00128   }
    00129 }
    00130
    00131 QDomElement Provider::createDomElement( QDomDocument &doc, QDomElement &parent )
    00132 {
    00133   QDomElement entry = doc.createElement( "stuff" );
    00134   parent.appendChild( entry );
    00135
    00136   QDomElement n = doc.createElement( "name" );
    00137   n.appendChild( doc.createTextNode( name() ) );
    00138   entry.appendChild( n );
    00139
    00140   return entry;
    00141 }
    00142
    00143
    00144 ProviderLoader::ProviderLoader( QWidget *parentWidget ) :
    00145   mParentWidget( parentWidget )
    00146 {
    00147   mProviders.setAutoDelete( true );
    00148 }
    00149
    00150 void ProviderLoader::load( const QString &type, const QString &providersList )
    00151 {
    00152   kdDebug(5850) << "ProviderLoader::load()" << endl;
    00153
    00154   mProviders.clear();
    00155   mJobData = "";
    00156
    00157   KConfig *cfg = KGlobal::config();
    00158   cfg->setGroup("KNewStuff");
    00159
    00160   QString providersUrl = providersList;
    00161   if( providersUrl.isEmpty() )
    00162     providersUrl = cfg->readEntry( "ProvidersUrl" );
    00163
    00164   if ( providersUrl.isEmpty() ) {
    00165     // TODO: Replace the default by the real one.
    00166     QString server = cfg->readEntry( "MasterServer",
    00167                                      "http://korganizer.kde.org" );
    00168
    00169     providersUrl = server + "/knewstuff/" + type + "/providers.xml";
    00170   }
    00171
    00172   kdDebug(5850) << "ProviderLoader::load(): providersUrl: " << providersUrl << endl;
    00173
    00174   KIO::TransferJob *job = KIO::get( KURL( providersUrl ) );
    00175   connect( job, SIGNAL( result( KIO::Job * ) ),
    00176            SLOT( slotJobResult( KIO::Job * ) ) );
    00177   connect( job, SIGNAL( data( KIO::Job *, const QByteArray & ) ),
    00178            SLOT( slotJobData( KIO::Job *, const QByteArray & ) ) );
    00179
    00180 //  job->dumpObjectInfo();
    00181 }
    00182
    00183 void ProviderLoader::slotJobData( KIO::Job *, const QByteArray &data )
    00184 {
    00185   kdDebug(5850) << "ProviderLoader::slotJobData()" << endl;
    00186
    00187   if ( data.size() == 0 ) return;
    00188
    00189   QCString str( data, data.size() + 1 );
    00190
    00191   mJobData.append( QString::fromUtf8( str ) );
    00192 }
    00193
    00194 void ProviderLoader::slotJobResult( KIO::Job *job )
    00195 {
    00196   if ( job->error() ) {
    00197     job->showErrorDialog( mParentWidget );
    00198   }
    00199
    00200   kdDebug(5850) << "--PROVIDERS-START--" << endl << mJobData << "--PROV_END--"
    00201             << endl;
    00202
    00203   QDomDocument doc;
    00204   if ( !doc.setContent( mJobData ) ) {
    00205     KMessageBox::error( mParentWidget, i18n("Error parsing providers list.") );
    00206     return;
    00207   }
    00208
    00209   QDomElement providers = doc.documentElement();
    00210
    00211   if ( providers.isNull() ) {
    00212     kdDebug(5850) << "No document in Providers.xml." << endl;
    00213   }
    00214
    00215   QDomNode n;
    00216   for ( n = providers.firstChild(); !n.isNull(); n = n.nextSibling() ) {
    00217     QDomElement p = n.toElement();
    00218
    00219     mProviders.append( new Provider( p ) );
    00220   }
    00221
    00222   emit providersLoaded( &mProviders );
    00223 }

This file is part of the documentation for knewstuff Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:28:34 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003