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::fromPathOrURL( 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   if (!mPreviewFile.isEmpty()) {
00268     text.append( mPreviewFile + "\n" );
00269   }
00270   text.append( mUploadMetaFile + "\n" );
00271 
00272   QString caption = i18n("Upload Files");
00273 
00274   if ( mUploadProvider->noUpload() ) {
00275     KURL noUploadUrl = mUploadProvider->noUploadUrl();
00276     if ( noUploadUrl.isEmpty() ) {
00277       text.append( i18n("Please upload the files manually.") );
00278       KMessageBox::information( mParentWidget, text, caption );
00279     } else {
00280       int result = KMessageBox::questionYesNo( mParentWidget, text, caption,
00281                                                i18n("Upload Info..."),
00282                                                KStdGuiItem::close() );
00283       if ( result == KMessageBox::Yes ) {
00284         kapp->invokeBrowser( noUploadUrl.url() );
00285       }
00286     }
00287   } else {
00288     int result = KMessageBox::questionYesNo( mParentWidget, text, caption,
00289                                              i18n("Upload"), KStdGuiItem::cancel() );
00290     if ( result == KMessageBox::Yes ) {
00291       KURL destination = mUploadProvider->uploadUrl();
00292       destination.setFileName( fi.fileName() );
00293 
00294       KIO::FileCopyJob *job = KIO::file_copy( KURL::fromPathOrURL( mUploadFile ), destination );
00295       connect( job, SIGNAL( result( KIO::Job * ) ),
00296                SLOT( slotUploadPayloadJobResult( KIO::Job * ) ) );
00297     }
00298   }
00299 }
00300 
00301 bool Engine::createMetaFile( Entry *entry )
00302 {
00303   QDomDocument doc("knewstuff");
00304   doc.appendChild( doc.createProcessingInstruction(
00305                    "xml", "version=\"1.0\" encoding=\"UTF-8\"" ) );
00306   QDomElement de = doc.createElement("knewstuff");
00307   doc.appendChild( de );
00308 
00309   entry->setType(type());
00310   de.appendChild( entry->createDomElement( doc, de ) );
00311 
00312   kdDebug(5850) << "--DOM START--" << endl << doc.toString()
00313             << "--DOM_END--" << endl;
00314 
00315   if ( mUploadMetaFile.isNull() ) {
00316     mUploadMetaFile = entry->fullName() + ".meta";
00317     mUploadMetaFile = locateLocal( "data", "korganizer/upload/" + mUploadMetaFile );
00318   }
00319 
00320   QFile f( mUploadMetaFile );
00321   if ( !f.open( IO_WriteOnly ) ) {
00322     mUploadMetaFile = QString::null;
00323     return false;
00324   }
00325 
00326   QTextStream ts( &f );
00327   ts.setEncoding( QTextStream::UnicodeUTF8 );
00328   ts << doc.toString();
00329 
00330   f.close();
00331 
00332   return true;
00333 }
00334 
00335 void Engine::slotUploadPayloadJobResult( KIO::Job *job )
00336 {
00337   if ( job->error() ) {
00338     kdDebug(5850) << "Error uploading new stuff payload." << endl;
00339     job->showErrorDialog( mParentWidget );
00340     return;
00341   }
00342 
00343   if (mPreviewFile.isEmpty()) {
00344     slotUploadPreviewJobResult(job);
00345     return;
00346   }
00347 
00348   QFileInfo fi( mPreviewFile );
00349 
00350   KURL previewDestination = mUploadProvider->uploadUrl();
00351   previewDestination.setFileName( fi.fileName() );
00352 
00353   KIO::FileCopyJob *newJob = KIO::file_copy( KURL::fromPathOrURL( mPreviewFile ), previewDestination );
00354   connect( newJob, SIGNAL( result( KIO::Job * ) ),
00355            SLOT( slotUploadPreviewJobResult( KIO::Job * ) ) );
00356 }
00357 
00358 void Engine::slotUploadPreviewJobResult( KIO::Job *job )
00359 {
00360   if ( job->error() ) {
00361     kdDebug(5850) << "Error uploading new stuff preview." << endl;
00362     job->showErrorDialog( mParentWidget );
00363     return;
00364   }
00365 
00366   QFileInfo fi( mUploadMetaFile );
00367 
00368   KURL metaDestination = mUploadProvider->uploadUrl();
00369   metaDestination.setFileName( fi.fileName() );
00370 
00371   KIO::FileCopyJob *newJob = KIO::file_copy( KURL::fromPathOrURL( mUploadMetaFile ), metaDestination );
00372   connect( newJob, SIGNAL( result( KIO::Job * ) ),
00373            SLOT( slotUploadMetaJobResult( KIO::Job * ) ) );
00374 }
00375 
00376 void Engine::slotUploadMetaJobResult( KIO::Job *job )
00377 {
00378   if ( job->error() ) {
00379     kdDebug(5850) << "Error uploading new stuff payload." << endl;
00380     job->showErrorDialog( mParentWidget );
00381     return;
00382   }
00383 
00384   KMessageBox::information( mParentWidget,
00385                             i18n("Successfully uploaded new stuff.") );
00386 }
KDE Logo
This file is part of the documentation for korganizer Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Oct 17 09:56:23 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003