• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdeutils
  • Sitemap
  • Contact Us
 

okteta

abstractfilesystemsyncwithremotejob.cpp

Go to the documentation of this file.
00001 /*
00002     This file is part of the Okteta Kakao module, part of the KDE project.
00003 
00004     Copyright 2008 Friedrich W. H. Kossebau <kossebau@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Lesser General Public
00008     License as published by the Free Software Foundation; either
00009     version 2.1 of the License, or (at your option) version 3, or any
00010     later version accepted by the membership of KDE e.V. (or its
00011     successor approved by the membership of KDE e.V.), which shall
00012     act as a proxy defined in Section 6 of version 3 of the license.
00013 
00014     This library is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017     Lesser General Public License for more details.
00018 
00019     You should have received a copy of the GNU Lesser General Public
00020     License along with this library. If not, see <http://www.gnu.org/licenses/>.
00021 */
00022 
00023 #include "abstractfilesystemsyncwithremotejob.h"
00024 
00025 // library
00026 #include "abstractmodelfilesystemsynchronizer.h"
00027 #include <kabstractdocument.h>
00028 // KDE
00029 #include <KIO/NetAccess>
00030 #include <KTemporaryFile>
00031 #include <KLocale>
00032 #include <KDirWatch>
00033 // Qt
00034 #include <QtCore/QTimer>
00035 
00036 
00037 class AbstractFileSystemSyncWithRemoteJob::Private
00038 {
00039   public:
00040     Private( AbstractModelFileSystemSynchronizer* synchronizer,
00041              const KUrl& url, AbstractModelSynchronizer::ConnectOption option );
00042 
00043   public:
00044     void setTemporaryFile( KTemporaryFile *temporaryFile );
00045     void setWorkFilePath( const QString &workFilePath );
00046 
00047   public:
00048     KUrl url() const;
00049     AbstractModelSynchronizer::ConnectOption option() const;
00050     KTemporaryFile *temporaryFile() const;
00051     QString workFilePath() const;
00052     QWidget *widget() const;
00053     AbstractModelFileSystemSynchronizer* synchronizer() const;
00054 
00055   protected:
00056     AbstractModelFileSystemSynchronizer* mSynchronizer;
00057     const KUrl mUrl;
00058     const AbstractModelSynchronizer::ConnectOption mOption;
00059     KTemporaryFile *mTemporaryFile;
00060     QString mWorkFilePath;
00061 };
00062 
00063 AbstractFileSystemSyncWithRemoteJob::Private::Private( AbstractModelFileSystemSynchronizer* synchronizer,
00064                                          const KUrl &url, AbstractModelSynchronizer::ConnectOption option )
00065  : mSynchronizer( synchronizer ), mUrl( url ), mOption( option ), mTemporaryFile( 0 )
00066 {}
00067 
00068 inline KUrl AbstractFileSystemSyncWithRemoteJob::Private::url()             const { return mUrl; }
00069 inline KTemporaryFile *AbstractFileSystemSyncWithRemoteJob::Private::temporaryFile() const { return mTemporaryFile; }
00070 inline QString AbstractFileSystemSyncWithRemoteJob::Private::workFilePath() const { return mWorkFilePath; }
00071 // TODO: setup a notification system
00072 inline QWidget *AbstractFileSystemSyncWithRemoteJob::Private::widget()      const { return 0; }
00073 inline AbstractModelFileSystemSynchronizer* AbstractFileSystemSyncWithRemoteJob::Private::synchronizer() const
00074 {
00075     return mSynchronizer;
00076 }
00077 inline AbstractModelSynchronizer::ConnectOption AbstractFileSystemSyncWithRemoteJob::Private::option() const
00078 {
00079     return mOption;
00080 }
00081 
00082 inline void AbstractFileSystemSyncWithRemoteJob::Private::setTemporaryFile( KTemporaryFile *temporaryFile )
00083 {
00084     mTemporaryFile = temporaryFile;
00085 }
00086 inline void AbstractFileSystemSyncWithRemoteJob::Private::setWorkFilePath( const QString &workFilePath )
00087 {
00088     mWorkFilePath = workFilePath;
00089 }
00090 
00091 
00092 
00093 AbstractFileSystemSyncWithRemoteJob::AbstractFileSystemSyncWithRemoteJob( AbstractModelFileSystemSynchronizer* synchronizer,
00094                                          const KUrl &url, AbstractModelSynchronizer::ConnectOption option )
00095  : d( new Private(synchronizer,url,option) )
00096 {}
00097 
00098 AbstractModelFileSystemSynchronizer* AbstractFileSystemSyncWithRemoteJob::synchronizer() const
00099 {
00100     return d->synchronizer();
00101 }
00102 QString AbstractFileSystemSyncWithRemoteJob::workFilePath() const { return d->workFilePath(); }
00103 QWidget *AbstractFileSystemSyncWithRemoteJob::widget() const { return d->widget(); }
00104 
00105 void AbstractFileSystemSyncWithRemoteJob::start()
00106 {
00107     QTimer::singleShot( 0, this, SLOT(syncWithRemote()) );
00108 }
00109 
00110 void AbstractFileSystemSyncWithRemoteJob::syncWithRemote()
00111 {
00112 // Comment: here we play tricks to reuse the temporary file
00113 // KIO::NetAccess::removeTempFile only removes tempfiles created by KIO::NetAccess::download
00114 // So if replaceRemote and workFilePath is temporaryFile both don't conflict -> no problem (now)
00115 
00116     QString workFilePath;
00117 
00118     const KUrl newUrl = d->url();
00119     bool isWorkFileOk;
00120     if( d->option() == AbstractModelSynchronizer::ReplaceRemote )
00121     {
00122         if( newUrl.isLocalFile() )
00123             workFilePath = newUrl.path();
00124         else
00125         {
00126             KTemporaryFile *temporaryFile = new KTemporaryFile;
00127             temporaryFile->open();
00128             workFilePath = temporaryFile->fileName();
00129             d->setTemporaryFile( temporaryFile );
00130         }
00131         isWorkFileOk = true;
00132     }
00133     else
00134         isWorkFileOk = KIO::NetAccess::download( newUrl.url(), workFilePath, d->widget() );
00135 
00136     if( isWorkFileOk )
00137     {
00138         d->setWorkFilePath( workFilePath );
00139         startSyncWithRemote();
00140     }
00141     else
00142     {
00143         setError( KilledJobError );
00144         setErrorText( KIO::NetAccess::lastErrorString() );
00145         // TODO: should we rather skip completeSync in successthe API?
00146         emitResult();
00147     }
00148 }
00149 
00150 
00151 void AbstractFileSystemSyncWithRemoteJob::completeSync( bool success )
00152 {
00153     AbstractModelFileSystemSynchronizer* synchronizer = d->synchronizer();
00154     if( success )
00155     {
00156         KDirWatch *dirWatch = KDirWatch::self();
00157         const KUrl oldUrl = synchronizer->url();
00158         // care for old url
00159         if( oldUrl.isLocalFile() )
00160         {
00161             dirWatch->disconnect( synchronizer );
00162             dirWatch->removeFile( oldUrl.path() );
00163         }
00164         const KUrl newUrl = d->url();
00165         if( !newUrl.isLocalFile() )
00166         {
00167             const bool uploaded = KIO::NetAccess::upload( workFilePath(), newUrl, d->widget() );
00168             if( !uploaded )
00169             {
00170                 setError( KilledJobError );
00171                 setErrorText( KIO::NetAccess::lastErrorString() );
00172             }
00173         }
00174         else
00175         {
00176             connect( dirWatch, SIGNAL(dirty( const QString & )),
00177                      synchronizer, SLOT(onFileDirty( const QString & )) );
00178 
00179             connect( dirWatch, SIGNAL(created( const QString & )),
00180                      synchronizer, SLOT(onFileCreated( const QString & )) );
00181 
00182             connect( dirWatch, SIGNAL(deleted( const QString & )),
00183                      synchronizer, SLOT(onFileDeleted( const QString & )) );
00184             dirWatch->addFile( workFilePath() );
00185         }
00186         synchronizer->setUrl( newUrl );
00187     }
00188     else
00189     {
00190         setError( KilledJobError );
00191         setErrorText( i18nc("@info","Problem when synching with local filesystem.") );
00192     }
00193 
00194     KIO::NetAccess::removeTempFile( d->workFilePath() );
00195     delete d->temporaryFile();
00196 
00197     emitResult();
00198 }
00199 
00200 
00201 AbstractFileSystemSyncWithRemoteJob::~AbstractFileSystemSyncWithRemoteJob()
00202 {
00203     delete d;
00204 }

okteta

Skip menu "okteta"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdeutils

Skip menu "kdeutils"
  • ark
  • kcalc
  • kcharselect
  • kdessh
  • kdf
  • kfloppy
  • kgpg
  • ktimer
  • kwallet
  • okteta
  • printer-applet
  • superkaramba
  • sweeper
Generated for kdeutils by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal