okteta
abstractfilesystemsynctoremotejob.cpp
Go to the documentation of this file.00001 /* 00002 This file is part of the Kakao Framework, 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 "abstractfilesystemsynctoremotejob.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 AbstractFileSystemSyncToRemoteJob::Private 00038 { 00039 public: 00040 Private( AbstractModelFileSystemSynchronizer* synchronizer ); 00041 00042 public: 00043 void prepareWorkFile(); 00044 void removeWorkFile(); 00045 00046 public: 00047 KUrl url() const; 00048 QString workFilePath() const; 00049 QWidget *widget() const; 00050 AbstractModelFileSystemSynchronizer* synchronizer() const; 00051 00052 protected: 00053 AbstractModelFileSystemSynchronizer* mSynchronizer; 00054 KTemporaryFile *mTemporaryFile; 00055 QString mWorkFilePath; 00056 }; 00057 00058 AbstractFileSystemSyncToRemoteJob::Private::Private( AbstractModelFileSystemSynchronizer* synchronizer ) 00059 : mSynchronizer( synchronizer ), mTemporaryFile( 0 ) 00060 {} 00061 00062 inline KUrl AbstractFileSystemSyncToRemoteJob::Private::url() const { return mSynchronizer->url(); } 00063 inline QString AbstractFileSystemSyncToRemoteJob::Private::workFilePath() const { return mWorkFilePath; } 00064 // TODO: setup a notification system 00065 inline QWidget *AbstractFileSystemSyncToRemoteJob::Private::widget() const { return 0; } 00066 inline AbstractModelFileSystemSynchronizer *AbstractFileSystemSyncToRemoteJob::Private::synchronizer() const 00067 { 00068 return mSynchronizer; 00069 } 00070 00071 inline void AbstractFileSystemSyncToRemoteJob::Private::prepareWorkFile() 00072 { 00073 if( url().isLocalFile() ) 00074 mWorkFilePath = url().path(); 00075 else 00076 { 00077 mTemporaryFile = new KTemporaryFile; 00078 mTemporaryFile->open(); 00079 mWorkFilePath = mTemporaryFile->fileName(); 00080 } 00081 } 00082 inline void AbstractFileSystemSyncToRemoteJob::Private::removeWorkFile() 00083 { 00084 delete mTemporaryFile; 00085 } 00086 00087 00088 AbstractFileSystemSyncToRemoteJob::AbstractFileSystemSyncToRemoteJob( AbstractModelFileSystemSynchronizer* synchronizer ) 00089 : d( new Private(synchronizer) ) 00090 {} 00091 00092 AbstractModelFileSystemSynchronizer* AbstractFileSystemSyncToRemoteJob::synchronizer() const 00093 { 00094 return d->synchronizer(); 00095 } 00096 QString AbstractFileSystemSyncToRemoteJob::workFilePath() const { return d->workFilePath(); } 00097 QWidget *AbstractFileSystemSyncToRemoteJob::widget() const { return d->widget(); } 00098 00099 void AbstractFileSystemSyncToRemoteJob::start() 00100 { 00101 QTimer::singleShot( 0, this, SLOT(syncToRemote()) ); 00102 } 00103 00104 void AbstractFileSystemSyncToRemoteJob::syncToRemote() 00105 { 00106 d->prepareWorkFile(); 00107 00108 startWriteToFile(); 00109 } 00110 00111 void AbstractFileSystemSyncToRemoteJob::completeWrite( bool success ) 00112 { 00113 if( success ) 00114 { 00115 if( !d->url().isLocalFile() ) 00116 { 00117 success = KIO::NetAccess::upload( d->workFilePath(), d->url(), d->widget() ); 00118 if( !success ) 00119 { 00120 setError( KilledJobError ); 00121 setErrorText( KIO::NetAccess::lastErrorString() ); 00122 } 00123 } 00124 } 00125 else 00126 { 00127 setError( KilledJobError ); 00128 setErrorText( i18nc("@info","Problem when saving to local filesystem.") ); 00129 } 00130 00131 d->removeWorkFile(); 00132 00133 emitResult(); 00134 } 00135 00136 00137 AbstractFileSystemSyncToRemoteJob::~AbstractFileSystemSyncToRemoteJob() 00138 { 00139 delete d; 00140 }
KDE 4.2 API Reference