# knewstuff Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# engine.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 <qcstring.h>
    00022 #include <qdom.h>
    00023 #include <qfileinfo.h>
    00024
    00025 #include <kapplication.h>
    00026 #include <kdebug.h>
    00027 #include <kio/job.h>
    00028 #include <klocale.h>
    00029 #include <kmessagebox.h>
    00030 #include <kstandarddirs.h>
    00031
    00032 #include "knewstuff.h"
    00033 #include "downloaddialog.h"
    00034 #include "uploaddialog.h"
    00035 #include "providerdialog.h"
    00036
    00037 #include "engine.h"
    00038 #include "engine.moc"
    00039
    00040 using namespace KNS;
    00041
    00042 struct Engine::Private
    00043 {
    00044     bool mIgnoreInstallResult;
    00045     KNewStuff *mNewStuff;
    00046 };
    00047
    00048 Engine::Engine( KNewStuff *newStuff, const QString &type,
    00049                 QWidget *parentWidget ) :
    00050   mParentWidget( parentWidget ), mDownloadDialog( 0 ),
    00051   mUploadDialog( 0 ), mProviderDialog( 0 ), mUploadProvider( 0 ),
    00052   d(new Private), mType( type )
    00053 {
    00054   d->mNewStuff = newStuff;
    00055   mProviderLoader = new ProviderLoader( mParentWidget );
    00056
    00057   mNewStuffList.setAutoDelete( true );
    00058 }
    00059
    00060 Engine::Engine( KNewStuff *newStuff, const QString &type,
    00061                 const QString &providerList, QWidget *parentWidget ) :
    00062                 mParentWidget( parentWidget ),
    00063         mDownloadDialog( 0 ), mUploadDialog( 0 ),
    00064         mProviderDialog( 0 ), mUploadProvider( 0 ),
    00065                 mProviderList( providerList ), d(new Private),
    00066         mType( type )
    00067 {
    00068   d->mNewStuff = newStuff;
    00069   d->mIgnoreInstallResult = false;
    00070   mProviderLoader = new ProviderLoader( mParentWidget );
    00071   mNewStuffList.setAutoDelete( true );
    00072 }
    00073
    00074 Engine::~Engine()
    00075 {
    00076   delete d;
    00077   delete mProviderLoader;
    00078
    00079   delete mUploadDialog;
    00080   delete mDownloadDialog;
    00081 }
    00082
    00083 void Engine::download()
    00084 {
    00085   kdDebug(5850) << "Engine::download()" << endl;
    00086
    00087   connect( mProviderLoader,
    00088            SIGNAL( providersLoaded( Provider::List * ) ),
    00089            SLOT( getMetaInformation( Provider::List * ) ) );
    00090   mProviderLoader->load( mType, mProviderList );
    00091 }
    00092
    00093 void Engine::getMetaInformation( Provider::List *providers )
    00094 {
    00095   mProviderLoader->disconnect();
    00096
    00097   mNewStuffJobData.clear();
    00098
    00099   if ( !mDownloadDialog ) {
    00100     mDownloadDialog = new DownloadDialog( this, mParentWidget );
    00101     mDownloadDialog->show();
    00102   }
    00103   mDownloadDialog->clear();
    00104
    00105   Provider *p;
    00106   for ( p = providers->first(); p; p = providers->next() ) {
    00107     if ( p->downloadUrl().isEmpty() ) continue;
    00108
    00109     KIO::TransferJob *job = KIO::get( p->downloadUrl() );
    00110     connect( job, SIGNAL( result( KIO::Job * ) ),
    00111              SLOT( slotNewStuffJobResult( KIO::Job * ) ) );
    00112     connect( job, SIGNAL( data( KIO::Job *, const QByteArray & ) ),
    00113              SLOT( slotNewStuffJobData( KIO::Job *, const QByteArray & ) ) );
    00114
    00115     mNewStuffJobData.insert( job, "" );
    00116     mProviderJobs[ job ] = p;
    00117   }
    00118 }
    00119
    00120 void Engine::slotNewStuffJobData( KIO::Job *job, const QByteArray &data )
    00121 {
    00122   if ( data.isEmpty() ) return;
    00123
    00124   kdDebug(5850) << "Engine:slotNewStuffJobData()" << endl;
    00125
    00126   QCString str( data, data.size() + 1 );
    00127
    00128   mNewStuffJobData[ job ].append( QString::fromUtf8( str ) );
    00129 }
    00130
    00131 void Engine::slotNewStuffJobResult( KIO::Job *job )
    00132 {
    00133   if ( job->error() ) {
    00134     kdDebug(5850) << "Error downloading new stuff descriptions." << endl;
    00135     job->showErrorDialog( mParentWidget );
    00136   } else {
    00137     QString knewstuffDoc = mNewStuffJobData[ job ];
    00138
    00139     kdDebug(5850) << "---START---" << endl << knewstuffDoc << "---END---" << endl;
    00140
    00141     mDownloadDialog->addProvider( mProviderJobs[ job ] );
    00142
    00143     QDomDocument doc;
    00144     if ( !doc.setContent( knewstuffDoc ) ) {
    00145       kdDebug(5850) << "Error parsing knewstuff.xml." << endl;
    00146       return;
    00147     } else {
    00148       QDomElement knewstuff = doc.documentElement();
    00149
    00150       if ( knewstuff.isNull() ) {
    00151         kdDebug(5850) << "No document in knewstuffproviders.xml." << endl;
    00152       } else {
    00153         QDomNode p;
    00154         for ( p = knewstuff.firstChild(); !p.isNull(); p = p.nextSibling() ) {
    00155           QDomElement stuff = p.toElement();
    00156           if ( stuff.tagName() != "stuff" ) continue;
    00157           if ( stuff.attribute("type", mType) != mType ) continue;
    00158
    00159           Entry *entry = new Entry( stuff );
    00160           mNewStuffList.append( entry );
    00161
    00162           mDownloadDialog->show();
    00163
    00164           mDownloadDialog->addEntry( entry );
    00165
    00166           kdDebug(5850) << "KNEWSTUFF: " << entry->name() << endl;
    00167
    00168           kdDebug(5850) << "  SUMMARY: " << entry->summary() << endl;
    00169           kdDebug(5850) << "  VERSION: " << entry->version() << endl;
    00170           kdDebug(5850) << "  RELEASEDATE: " << entry->releaseDate().toString() << endl;
    00171           kdDebug(5850) << "  RATING: " << entry->rating() << endl;
    00172
    00173           kdDebug(5850) << "  LANGS: " << entry->langs().join(", ") << endl;
    00174         }
    00175       }
    00176     }
    00177   }
    00178
    00179   mNewStuffJobData.remove( job );
    00180   mProviderJobs.remove( job );
    00181
    00182   if ( mNewStuffJobData.count() == 0 ) {
    00183     mDownloadDialog->show();
    00184     mDownloadDialog->raise();
    00185   }
    00186 }
    00187
    00188 void Engine::download( Entry *entry )
    00189 {
    00190   kdDebug(5850) << "Engine::download(entry)" << endl;
    00191
    00192   KURL source = entry->payload();
    00193   mDownloadDestination = d->mNewStuff->downloadDestination( entry );
    00194
    00195   if ( mDownloadDestination.isEmpty() ) {
    00196     kdDebug(5850) << "Empty downloadDestination. Cancelling download." << endl;
    00197     return;
    00198   }
    00199
    00200   KURL destination = KURL( mDownloadDestination );
    00201
    00202   kdDebug(5850) << "  SOURCE: " << source.url() << endl;
    00203   kdDebug(5850) << "  DESTINATION: " << destination.url() << endl;
    00204
    00205   KIO::FileCopyJob *job = KIO::file_copy( source, destination, -1, true );
    00206   connect( job, SIGNAL( result( KIO::Job * ) ),
    00207            SLOT( slotDownloadJobResult( KIO::Job * ) ) );
    00208 }
    00209
    00210 void Engine::slotDownloadJobResult( KIO::Job *job )
    00211 {
    00212   if ( job->error() ) {
    00213     kdDebug(5850) << "Error downloading new stuff payload." << endl;
    00214     job->showErrorDialog( mParentWidget );
    00215     return;
    00216   }
    00217
    00218   if ( d->mNewStuff->install( mDownloadDestination ) ) {
    00219     if ( !d->mIgnoreInstallResult ) {
    00220       KMessageBox::information( mParentWidget,
    00221                                 i18n("Successfully installed hot new stuff.") );
    00222     }
    00223   } else
    00224     if ( !d->mIgnoreInstallResult ){
    00225       KMessageBox::error( mParentWidget,
    00226                           i18n("Failed to install hot new stuff.") );
    00227   }
    00228 }
    00229
    00230 void Engine::upload(const QString &fileName, const QString &previewName )
    00231 {
    00232   mUploadFile = fileName;
    00233   mPreviewFile = previewName;
    00234
    00235   connect( mProviderLoader,
    00236            SIGNAL( providersLoaded( Provider::List * ) ),
    00237            SLOT( selectUploadProvider( Provider::List * ) ) );
    00238   mProviderLoader->load( mType );
    00239 }
    00240
    00241 void Engine::selectUploadProvider( Provider::List *providers )
    00242 {
    00243   kdDebug(5850) << "Engine:selectUploadProvider()" << endl;
    00244
    00245   mProviderLoader->disconnect();
    00246
    00247   if ( !mProviderDialog ) {
    00248     mProviderDialog = new ProviderDialog( this, mParentWidget );
    00249   }
    00250
    00251   mProviderDialog->clear();
    00252
    00253   mProviderDialog->show();
    00254   mProviderDialog->raise();
    00255
    00256   for( Provider *p = providers->first(); p; p = providers->next() ) {
    00257     mProviderDialog->addProvider( p );
    00258   }
    00259 }
    00260
    00261 void Engine::requestMetaInformation( Provider *provider )
    00262 {
    00263   mUploadProvider = provider;
    00264
    00265   if ( !mUploadDialog ) {
    00266     mUploadDialog = new UploadDialog( this, mParentWidget );
    00267   }
    00268   mUploadDialog->setPreviewFile( mPreviewFile );
    00269   mUploadDialog->show();
    00270   mUploadDialog->raise();
    00271 }
    00272
    00273 void Engine::upload( Entry *entry )
    00274 {
    00275   if ( mUploadFile.isNull()) {
    00276     mUploadFile = entry->fullName();
    00277     mUploadFile = locateLocal( "data", QString(kapp->instanceName()) + "/upload/" + mUploadFile );
    00278
    00279     if ( !d->mNewStuff->createUploadFile( mUploadFile ) ) {
    00280       KMessageBox::error( mParentWidget, i18n("Unable to create file to upload.") );
    00281       emit uploadFinished( false );
    00282       return;
    00283     }
    00284   }
    00285
    00286   QString lang = entry->langs().first();
    00287   QFileInfo fi( mUploadFile );
    00288   entry->setPayload( KURL::fromPathOrURL( fi.fileName() ), lang );
    00289
    00290   if ( !createMetaFile( entry ) ) {
    00291     emit uploadFinished( false );
    00292     return;
    00293   }
    00294
    00295   QString text = i18n("The files to be uploaded have been created at:\n");
    00296   text.append( i18n("Data file: %1\n").arg( mUploadFile) );
    00297   if (!mPreviewFile.isEmpty()) {
    00298     text.append( i18n("Preview image: %1\n").arg( mPreviewFile) );
    00299   }
    00300   text.append( i18n("Content information: %1\n").arg( mUploadMetaFile) );
    00301   text.append( i18n("Those files can now be uploaded.\n") );
    00302   text.append( i18n("Beware that any people might have access to them at any time.") );
    00303
    00304   QString caption = i18n("Upload Files");
    00305
    00306   if ( mUploadProvider->noUpload() ) {
    00307     KURL noUploadUrl = mUploadProvider->noUploadUrl();
    00308     if ( noUploadUrl.isEmpty() ) {
    00309       text.append( i18n("Please upload the files manually.") );
    00310       KMessageBox::information( mParentWidget, text, caption );
    00311     } else {
    00312       int result = KMessageBox::questionYesNo( mParentWidget, text, caption,
    00313                                                i18n("Upload Info"),
    00314                                                i18n("&Close") );
    00315       if ( result == KMessageBox::Yes ) {
    00316         kapp->invokeBrowser( noUploadUrl.url() );
    00317       }
    00318     }
    00319   } else {
    00320     int result = KMessageBox::questionYesNo( mParentWidget, text, caption,
    00321                                              i18n("&Upload"), i18n("&Cancel") );
    00322     if ( result == KMessageBox::Yes ) {
    00323       KURL destination = mUploadProvider->uploadUrl();
    00324       destination.setFileName( fi.fileName() );
    00325
    00326       KIO::FileCopyJob *job = KIO::file_copy( KURL::fromPathOrURL( mUploadFile ), destination );
    00327       connect( job, SIGNAL( result( KIO::Job * ) ),
    00328                SLOT( slotUploadPayloadJobResult( KIO::Job * ) ) );
    00329     } else {
    00330       emit uploadFinished( false );
    00331     }
    00332   }
    00333 }
    00334
    00335 bool Engine::createMetaFile( Entry *entry )
    00336 {
    00337   QDomDocument doc("knewstuff");
    00338   doc.appendChild( doc.createProcessingInstruction(
    00339                    "xml", "version=\"1.0\" encoding=\"UTF-8\"" ) );
    00340   QDomElement de = doc.createElement("knewstuff");
    00341   doc.appendChild( de );
    00342
    00343   entry->setType(type());
    00344   de.appendChild( entry->createDomElement( doc, de ) );
    00345
    00346   kdDebug(5850) << "--DOM START--" << endl << doc.toString()
    00347             << "--DOM_END--" << endl;
    00348
    00349   if ( mUploadMetaFile.isNull() ) {
    00350     mUploadMetaFile = entry->fullName() + ".meta";
    00351     mUploadMetaFile = locateLocal( "data", QString(kapp->instanceName()) + "/upload/" + mUploadMetaFile );
    00352   }
    00353
    00354   QFile f( mUploadMetaFile );
    00355   if ( !f.open( IO_WriteOnly ) ) {
    00356     mUploadMetaFile = QString::null;
    00357     return false;
    00358   }
    00359
    00360   QTextStream ts( &f );
    00361   ts.setEncoding( QTextStream::UnicodeUTF8 );
    00362   ts << doc.toString();
    00363
    00364   f.close();
    00365
    00366   return true;
    00367 }
    00368
    00369 void Engine::slotUploadPayloadJobResult( KIO::Job *job )
    00370 {
    00371   if ( job->error() ) {
    00372     kdDebug(5850) << "Error uploading new stuff payload." << endl;
    00373     job->showErrorDialog( mParentWidget );
    00374     emit uploadFinished( false );
    00375     return;
    00376   }
    00377
    00378   if (mPreviewFile.isEmpty()) {
    00379     slotUploadPreviewJobResult(job);
    00380     return;
    00381   }
    00382
    00383   QFileInfo fi( mPreviewFile );
    00384
    00385   KURL previewDestination = mUploadProvider->uploadUrl();
    00386   previewDestination.setFileName( fi.fileName() );
    00387
    00388   KIO::FileCopyJob *newJob = KIO::file_copy( KURL::fromPathOrURL( mPreviewFile ), previewDestination );
    00389   connect( newJob, SIGNAL( result( KIO::Job * ) ),
    00390            SLOT( slotUploadPreviewJobResult( KIO::Job * ) ) );
    00391 }
    00392
    00393 void Engine::slotUploadPreviewJobResult( KIO::Job *job )
    00394 {
    00395   if ( job->error() ) {
    00396     kdDebug(5850) << "Error uploading new stuff preview." << endl;
    00397     job->showErrorDialog( mParentWidget );
    00398     emit uploadFinished( true );
    00399     return;
    00400   }
    00401
    00402   QFileInfo fi( mUploadMetaFile );
    00403
    00404   KURL metaDestination = mUploadProvider->uploadUrl();
    00405   metaDestination.setFileName( fi.fileName() );
    00406
    00407   KIO::FileCopyJob *newJob = KIO::file_copy( KURL::fromPathOrURL( mUploadMetaFile ), metaDestination );
    00408   connect( newJob, SIGNAL( result( KIO::Job * ) ),
    00409            SLOT( slotUploadMetaJobResult( KIO::Job * ) ) );
    00410 }
    00411
    00412 void Engine::slotUploadMetaJobResult( KIO::Job *job )
    00413 {
    00414   mUploadMetaFile = QString::null;
    00415   if ( job->error() ) {
    00416     kdDebug(5850) << "Error uploading new stuff metadata." << endl;
    00417     job->showErrorDialog( mParentWidget );
    00418     emit uploadFinished( false );
    00419     return;
    00420   }
    00421
    00422   KMessageBox::information( mParentWidget,
    00423                             i18n("Successfully uploaded new stuff.") );
    00424   emit uploadFinished( true );
    00425 }
    00426
    00427 void Engine::ignoreInstallResult(bool ignore)
    00428 {
    00429   d->mIgnoreInstallResult = ignore;
    00430 }

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