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