korganizer Library API Documentation

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 Engine::Engine( KNewStuff *newStuff, const QString &type,
00043                 QWidget *parentWidget ) :
00044   mParentWidget( parentWidget ), mDownloadDialog( 0 ),
00045   mUploadDialog( 0 ), mProviderDialog( 0 ), mUploadProvider( 0 ),
00046   mNewStuff( newStuff ), mType( type )
00047 {
00048   mProviderLoader = new ProviderLoader( mParentWidget );
00049 
00050   mNewStuffList.setAutoDelete( true );
00051 }
00052 
00053 Engine::~Engine()
00054 {
00055   delete mProviderLoader;
00056 
00057   delete mUploadDialog;
00058   delete mDownloadDialog;
00059 }
00060 
00061 void Engine::download()
00062 {
00063   kdDebug() << "Engine::download()" << endl;
00064 
00065   connect( mProviderLoader,
00066            SIGNAL( providersLoaded( Provider::List * ) ),
00067            SLOT( getMetaInformation( Provider::List * ) ) );
00068   mProviderLoader->load( mType );
00069 }
00070 
00071 void Engine::getMetaInformation( Provider::List *providers )
00072 {
00073   mProviderLoader->disconnect();
00074 
00075   mNewStuffJobData.clear();
00076 
00077   if ( !mDownloadDialog ) {
00078     mDownloadDialog = new DownloadDialog( this, mParentWidget );
00079     mDownloadDialog->show();
00080   }
00081   mDownloadDialog->clear();
00082 
00083   Provider *p;
00084   for ( p = providers->first(); p; p = providers->next() ) {
00085     if ( p->downloadUrl().isEmpty() ) continue;
00086 
00087     KIO::TransferJob *job = KIO::get( p->downloadUrl() );
00088     connect( job, SIGNAL( result( KIO::Job * ) ),
00089              SLOT( slotNewStuffJobResult( KIO::Job * ) ) );
00090     connect( job, SIGNAL( data( KIO::Job *, const QByteArray & ) ),
00091              SLOT( slotNewStuffJobData( KIO::Job *, const QByteArray & ) ) );
00092 
00093     mNewStuffJobData.insert( job, "" );
00094   }
00095 }
00096 
00097 void Engine::slotNewStuffJobData( KIO::Job *job, const QByteArray &data )
00098 {
00099   if ( data.isEmpty() ) return;
00100 
00101   kdDebug() << "Engine:slotNewStuffJobData()" << endl;
00102 
00103   kdDebug() << "===START===" << endl << data.data() << "===END===" << endl;
00104 
00105   QString s = data;
00106   mNewStuffJobData[ job ].append( s );
00107 }
00108 
00109 void Engine::slotNewStuffJobResult( KIO::Job *job )
00110 {
00111   if ( job->error() ) {
00112     kdDebug() << "Error downloading new stuff descriptions." << endl;
00113     job->showErrorDialog( mParentWidget );
00114   } else {
00115     QString knewstuffDoc = QString::fromUtf8( mNewStuffJobData[ job ] );
00116 
00117     kdDebug() << "---START---" << endl << knewstuffDoc << "---END---" << endl;
00118 
00119     QDomDocument doc;
00120     if ( !doc.setContent( knewstuffDoc ) ) {
00121       kdDebug() << "Error parsing knewstuff.xml." << endl;
00122       return;
00123     } else {
00124       QDomElement knewstuff = doc.documentElement();
00125 
00126       if ( knewstuff.isNull() ) {
00127         kdDebug() << "No document in knewstuffproviders.xml." << endl;
00128       } else {
00129         QDomNode p;
00130         for ( p = knewstuff.firstChild(); !p.isNull(); p = p.nextSibling() ) {
00131           QDomElement stuff = p.toElement();
00132           if ( stuff.tagName() != "stuff" ) continue;
00133 
00134           Entry *entry = new Entry( stuff );
00135           mNewStuffList.append( entry );
00136 
00137           mDownloadDialog->show();
00138 
00139           mDownloadDialog->addEntry( entry );
00140     
00141           kdDebug() << "KNEWSTUFF: " << entry->name() << endl;
00142 
00143           kdDebug() << "  SUMMARY: " << entry->summary() << endl;
00144           kdDebug() << "  VERSION: " << entry->version() << endl;
00145           kdDebug() << "  RELEASEDATE: " << entry->releaseDate().toString() << endl;
00146           kdDebug() << "  RATING: " << entry->rating() << endl;
00147 
00148           kdDebug() << "  LANGS: " << entry->langs().join(", ") << endl;
00149         }
00150       }
00151     }
00152   }
00153   
00154   mNewStuffJobData.remove( job );
00155 
00156   if ( mNewStuffJobData.count() == 0 ) {
00157     mDownloadDialog->show();
00158     mDownloadDialog->raise();
00159   }
00160 }
00161 
00162 void Engine::download( Entry *entry )
00163 {
00164   kdDebug() << "Engine::download(entry)" << endl;
00165 
00166   KURL source = entry->payload();
00167   mDownloadDestination = mNewStuff->downloadDestination( entry );
00168   KURL destination = KURL( mDownloadDestination );
00169 
00170   kdDebug() << "  SOURCE: " << source.url() << endl;
00171   kdDebug() << "  DESTINATION: " << destination.url() << endl;
00172 
00173   KIO::FileCopyJob *job = KIO::file_copy( source, destination );
00174   connect( job, SIGNAL( result( KIO::Job * ) ),
00175            SLOT( slotDownloadJobResult( KIO::Job * ) ) );
00176 }
00177 
00178 void Engine::slotDownloadJobResult( KIO::Job *job )
00179 {
00180   if ( job->error() ) {
00181     kdDebug() << "Error downloading new stuff payload." << endl;
00182     job->showErrorDialog( mParentWidget );
00183     return;
00184   }
00185 
00186   if ( mNewStuff->install( mDownloadDestination ) ) {
00187     KMessageBox::information( mParentWidget,
00188                               i18n("Successfully installed hot new stuff.") );
00189   } else {
00190     KMessageBox::error( mParentWidget,
00191                         i18n("Failed to install hot new stuff.") );
00192   }
00193 }
00194 
00195 void Engine::upload()
00196 {
00197   connect( mProviderLoader,
00198            SIGNAL( providersLoaded( Provider::List * ) ),
00199            SLOT( selectUploadProvider( Provider::List * ) ) );
00200   mProviderLoader->load( mType );
00201 }
00202 
00203 void Engine::selectUploadProvider( Provider::List *providers )
00204 {
00205   kdDebug() << "Engine:selectUploadProvider()" << endl;
00206 
00207   mProviderLoader->disconnect();
00208 
00209   if ( !mProviderDialog ) {
00210     mProviderDialog = new ProviderDialog( this, mParentWidget );
00211   }
00212 
00213   mProviderDialog->clear();
00214   
00215   mProviderDialog->show();
00216   mProviderDialog->raise();
00217 
00218   for( Provider *p = providers->first(); p; p = providers->next() ) {
00219     mProviderDialog->addProvider( p );
00220   }
00221 }
00222 
00223 void Engine::requestMetaInformation( Provider *provider )
00224 {
00225   mUploadProvider = provider;
00226 
00227   if ( !mUploadDialog ) {
00228     mUploadDialog = new UploadDialog( this, mParentWidget );
00229   }
00230   mUploadDialog->show();
00231   mUploadDialog->raise();
00232 }
00233 
00234 void Engine::upload( Entry *entry )
00235 {
00236   QString uploadFile = entry->fullName();
00237   uploadFile = locateLocal( "appdata", "upload/" + uploadFile );
00238 
00239   if ( !mNewStuff->createUploadFile( uploadFile ) ) {
00240     KMessageBox::error( mParentWidget, i18n("Unable to create file to upload") );
00241     return;
00242   }
00243 
00244   QString lang = entry->langs().first();
00245   QFileInfo fi( uploadFile );
00246   entry->setPayload( fi.fileName(), lang );
00247 
00248   if ( !createMetaFile( entry ) ) return;
00249 
00250   QString text = i18n("The files to be uploaded have been created at:\n");
00251   text.append( uploadFile + "\n" );
00252   text.append( mUploadMetaFile + "\n" );
00253 
00254   QString caption = i18n("Upload files");
00255 
00256   if ( mUploadProvider->noUpload() ) {
00257     KURL noUploadUrl = mUploadProvider->noUploadUrl();
00258     if ( noUploadUrl.isEmpty() ) {
00259       text.append( i18n("Please upload the files manually.") ); 
00260       KMessageBox::information( mParentWidget, text, caption );
00261     } else {
00262       int result = KMessageBox::questionYesNo( mParentWidget, text, caption,
00263                                                i18n("Upload Info..."),
00264                                                i18n("Close") );
00265       if ( result == KMessageBox::Yes ) {
00266         kapp->invokeBrowser( noUploadUrl.url() );
00267       }
00268     }
00269   } else {
00270     int result = KMessageBox::questionYesNo( mParentWidget, text, caption,
00271                                              i18n("Upload"), i18n("Cancel") );
00272     if ( result == KMessageBox::Yes ) {
00273       KURL destination = mUploadProvider->uploadUrl();
00274       destination.setFileName( fi.fileName() );
00275 
00276       KIO::FileCopyJob *job = KIO::file_copy( uploadFile, destination );
00277       connect( job, SIGNAL( result( KIO::Job * ) ),
00278                SLOT( slotUploadPayloadJobResult( KIO::Job * ) ) );
00279     }
00280   }
00281 }
00282 
00283 bool Engine::createMetaFile( Entry *entry )
00284 {
00285   QDomDocument doc("knewstuff");
00286   doc.appendChild( doc.createProcessingInstruction(
00287                    "xml", "version=\"1.0\" encoding=\"UTF-8\"" ) );
00288   QDomElement de = doc.createElement("knewstuff");
00289   doc.appendChild( de );
00290 
00291   de.appendChild( entry->createDomElement( doc, de ) );
00292   
00293   kdDebug() << "--DOM START--" << endl << doc.toString()
00294             << "--DOM_END--" << endl;
00295 
00296   mUploadMetaFile = entry->fullName() + ".meta";
00297   mUploadMetaFile = locateLocal( "appdata", "upload/" + mUploadMetaFile );
00298 
00299   QFile f( mUploadMetaFile );
00300   if ( !f.open( IO_WriteOnly ) ) {
00301     mUploadMetaFile = QString::null;
00302     return false;
00303   }
00304   
00305   QTextStream ts( &f );
00306   ts.setEncoding( QTextStream::UnicodeUTF8 );
00307   ts << doc.toString();
00308   
00309   f.close();
00310   
00311   return true;
00312 }
00313 
00314 void Engine::slotUploadPayloadJobResult( KIO::Job *job )
00315 {
00316   if ( job->error() ) {
00317     kdDebug() << "Error uploading new stuff payload." << endl;
00318     job->showErrorDialog( mParentWidget );
00319     return;
00320   }
00321 
00322   QFileInfo fi( mUploadMetaFile );
00323 
00324   KURL metaDestination = mUploadProvider->uploadUrl();
00325   metaDestination.setFileName( fi.fileName() );
00326 
00327   KIO::FileCopyJob *newJob = KIO::file_copy( mUploadMetaFile, metaDestination );
00328   connect( newJob, SIGNAL( result( KIO::Job * ) ),
00329            SLOT( slotUploadMetaJobResult( KIO::Job * ) ) );
00330 }
00331 
00332 void Engine::slotUploadMetaJobResult( KIO::Job *job )
00333 {
00334   if ( job->error() ) {
00335     kdDebug() << "Error uploading new stuff payload." << endl;
00336     job->showErrorDialog( mParentWidget );
00337     return;
00338   }
00339 
00340   KMessageBox::information( mParentWidget,
00341                             i18n("Successfully uploaded new stuff.") );
00342 }
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sat Oct 18 02:47:30 2003 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001