okteta
abstractfilesystemloadjob.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "abstractfilesystemloadjob.h"
00024
00025
00026 #include "abstractmodelfilesystemsynchronizer.h"
00027 #include <kabstractdocument.h>
00028
00029 #include <KIO/NetAccess>
00030 #include <KTemporaryFile>
00031 #include <KLocale>
00032 #include <KDirWatch>
00033
00034 #include <QtCore/QTimer>
00035
00036
00037 class AbstractFileSystemLoadJob::Private
00038 {
00039 public:
00040 Private( AbstractModelFileSystemSynchronizer* synchronizer, const KUrl& url );
00041
00042 public:
00043 void setWorkFilePath( const QString &workFilePath );
00044
00045 public:
00046 const KUrl &url() const;
00047 QString workFilePath() const;
00048 QWidget *widget() const;
00049 AbstractModelFileSystemSynchronizer* synchronizer() const;
00050
00051 protected:
00052 AbstractModelFileSystemSynchronizer* mSynchronizer;
00053 const KUrl mUrl;
00054 QString mWorkFilePath;
00055 };
00056
00057 AbstractFileSystemLoadJob::Private::Private( AbstractModelFileSystemSynchronizer* synchronizer, const KUrl &url )
00058 : mSynchronizer( synchronizer ), mUrl( url )
00059 {}
00060
00061 inline AbstractModelFileSystemSynchronizer* AbstractFileSystemLoadJob::Private::synchronizer() const
00062 {
00063 return mSynchronizer;
00064 }
00065 inline const KUrl &AbstractFileSystemLoadJob::Private::url() const { return mUrl; }
00066 inline QString AbstractFileSystemLoadJob::Private::workFilePath() const { return mWorkFilePath; }
00067
00068 inline QWidget *AbstractFileSystemLoadJob::Private::widget() const { return 0; }
00069
00070 inline void AbstractFileSystemLoadJob::Private::setWorkFilePath( const QString &workFilePath )
00071 {
00072 mWorkFilePath = workFilePath;
00073 }
00074
00075
00076
00077 AbstractFileSystemLoadJob::AbstractFileSystemLoadJob( AbstractModelFileSystemSynchronizer* synchronizer,
00078 const KUrl &url )
00079 : d( new Private(synchronizer,url) )
00080 {}
00081
00082 AbstractModelFileSystemSynchronizer* AbstractFileSystemLoadJob::synchronizer() const
00083 {
00084 return d->synchronizer();
00085 }
00086 QString AbstractFileSystemLoadJob::workFilePath() const { return d->workFilePath(); }
00087 QWidget *AbstractFileSystemLoadJob::widget() const { return d->widget(); }
00088
00089 void AbstractFileSystemLoadJob::start()
00090 {
00091 QTimer::singleShot( 0, this, SLOT(load()) );
00092 }
00093
00094 void AbstractFileSystemLoadJob::load()
00095 {
00096 QString workFilePath;
00097
00098 if( KIO::NetAccess::download(d->url().url(),workFilePath,d->widget()) )
00099 {
00100 d->setWorkFilePath( workFilePath );
00101 startLoadFromFile();
00102 }
00103 else
00104 {
00105 setError( KilledJobError );
00106 setErrorText( KIO::NetAccess::lastErrorString() );
00107
00108 AbstractLoadJob::setDocument( 0 );
00109 }
00110 }
00111
00112
00113 void AbstractFileSystemLoadJob::setDocument( KAbstractDocument *document )
00114 {
00115 AbstractModelFileSystemSynchronizer* synchronizer = d->synchronizer();
00116
00117 if( document )
00118 {
00119 synchronizer->setUrl( d->url() );
00120 if( d->url().isLocalFile() )
00121 {
00122 KDirWatch *dirWatch = KDirWatch::self();
00123 connect( dirWatch, SIGNAL(dirty( const QString & )),
00124 synchronizer, SLOT(onFileDirty( const QString & )) );
00125
00126 connect( dirWatch, SIGNAL(created( const QString & )),
00127 synchronizer, SLOT(onFileCreated( const QString & )) );
00128
00129 connect( dirWatch, SIGNAL(deleted( const QString & )),
00130 synchronizer, SLOT(onFileDeleted( const QString & )) );
00131 dirWatch->addFile( d->url().path() );
00132 }
00133 document->setSynchronizer( synchronizer );
00134 }
00135 else
00136 {
00137 delete synchronizer;
00138
00139 setError( KilledJobError );
00140 setErrorText( i18nc("@info","Problem when loading from local filesystem.") );
00141 }
00142
00143 KIO::NetAccess::removeTempFile( d->workFilePath() );
00144
00145 AbstractLoadJob::setDocument( document );
00146 }
00147
00148
00149 AbstractFileSystemLoadJob::~AbstractFileSystemLoadJob()
00150 {
00151 delete d;
00152 }
00153
00154 #include "abstractfilesystemloadjob.moc"