resourceremote.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <typeinfo>
00023 #include <stdlib.h>
00024
00025 #include <qdatetime.h>
00026 #include <qstring.h>
00027 #include <qptrlist.h>
00028
00029 #include <kdebug.h>
00030 #include <kurl.h>
00031 #include <kio/job.h>
00032 #include <klocale.h>
00033 #include <kstandarddirs.h>
00034
00035 #include "vcaldrag.h"
00036 #include "vcalformat.h"
00037 #include "icalformat.h"
00038 #include "exceptions.h"
00039 #include "incidence.h"
00040 #include "event.h"
00041 #include "todo.h"
00042 #include "journal.h"
00043 #include "filestorage.h"
00044
00045 #include <kabc/locknull.h>
00046
00047 #include <kresources/configwidget.h>
00048
00049 #include "resourceremoteconfig.h"
00050
00051 #include "resourceremote.h"
00052
00053 using namespace KCal;
00054
00055 ResourceRemote::ResourceRemote( const KConfig* config )
00056 : ResourceCached( config ), mLock( 0 )
00057 {
00058 if ( config ) {
00059 readConfig( config );
00060 }
00061
00062 init();
00063 }
00064
00065 ResourceRemote::ResourceRemote( const KURL &downloadUrl, const KURL &uploadUrl )
00066 : ResourceCached( 0 )
00067 {
00068 mDownloadUrl = downloadUrl;
00069
00070 if ( uploadUrl.isEmpty() ) {
00071 mUploadUrl = mDownloadUrl;
00072 } else {
00073 mUploadUrl = uploadUrl;
00074 }
00075
00076 init();
00077 }
00078
00079 ResourceRemote::~ResourceRemote()
00080 {
00081 close();
00082
00083 if ( mDownloadJob ) mDownloadJob->kill();
00084 if ( mUploadJob ) mUploadJob->kill();
00085
00086 delete mLock;
00087 }
00088
00089 void ResourceRemote::init()
00090 {
00091 mDownloadJob = 0;
00092 mUploadJob = 0;
00093
00094 setType( "remote" );
00095
00096 mOpen = false;
00097
00098 mLock = new KABC::LockNull( true );
00099 }
00100
00101 void ResourceRemote::readConfig( const KConfig *config )
00102 {
00103 QString url = config->readEntry( "DownloadUrl" );
00104 mDownloadUrl = KURL( url );
00105
00106 url = config->readEntry( "UploadUrl" );
00107 mUploadUrl = KURL( url );
00108
00109 mReloadPolicy = config->readNumEntry( "ReloadPolicy", ReloadNever );
00110 }
00111
00112 void ResourceRemote::writeConfig( KConfig *config )
00113 {
00114 kdDebug() << "ResourceRemote::writeConfig()" << endl;
00115
00116 ResourceCalendar::writeConfig( config );
00117
00118 config->writeEntry( "DownloadUrl", mDownloadUrl.url() );
00119 config->writeEntry( "UploadUrl", mUploadUrl.url() );
00120
00121 config->writeEntry( "ReloadPolicy", mReloadPolicy );
00122 }
00123
00124 void ResourceRemote::setDownloadUrl( const KURL &url )
00125 {
00126 mDownloadUrl = url;
00127 }
00128
00129 KURL ResourceRemote::downloadUrl() const
00130 {
00131 return mDownloadUrl;
00132 }
00133
00134 void ResourceRemote::setUploadUrl( const KURL &url )
00135 {
00136 mUploadUrl = url;
00137 }
00138
00139 KURL ResourceRemote::uploadUrl() const
00140 {
00141 return mUploadUrl;
00142 }
00143
00144 void ResourceRemote::setReloadPolicy( int i )
00145 {
00146 mReloadPolicy = i;
00147 }
00148
00149 int ResourceRemote::reloadPolicy() const
00150 {
00151 return mReloadPolicy;
00152 }
00153
00154 QString ResourceRemote::cacheFile()
00155 {
00156 QString file = locateLocal( "cache", "kcal/kresources/" + identifier() );
00157 kdDebug() << "ResourceRemote::cacheFile(): " << file << endl;
00158 return file;
00159 }
00160
00161 bool ResourceRemote::doOpen()
00162 {
00163 kdDebug(5800) << "ResourceRemote::doOpen()" << endl;
00164
00165 mOpen = true;
00166
00167 return true;
00168 }
00169
00170 bool ResourceRemote::load()
00171 {
00172 kdDebug() << "ResourceRemote::load()" << endl;
00173
00174 if ( !mOpen ) return true;
00175
00176 if ( mDownloadJob ) {
00177 kdWarning() << "ResourceRemote::load(): download still in progress."
00178 << endl;
00179 return false;
00180 }
00181 if ( mUploadJob ) {
00182 kdWarning() << "ResourceRemote::load(): upload still in progress."
00183 << endl;
00184 return false;
00185 }
00186
00187 mCalendar.close();
00188
00189 mCalendar.load( cacheFile() );
00190
00191 mDownloadJob = KIO::file_copy( mDownloadUrl, KURL( cacheFile() ), -1, true );
00192 connect( mDownloadJob, SIGNAL( result( KIO::Job * ) ),
00193 SLOT( slotLoadJobResult( KIO::Job * ) ) );
00194
00195 return true;
00196 }
00197
00198 void ResourceRemote::slotLoadJobResult( KIO::Job *job )
00199 {
00200 if ( job->error() ) {
00201 job->showErrorDialog( 0 );
00202 } else {
00203 kdDebug() << "ResourceRemote::slotLoadJobResult() success" << endl;
00204
00205 mCalendar.close();
00206 mCalendar.load( cacheFile() );
00207
00208 emit resourceChanged( this );
00209 }
00210
00211 mDownloadJob = 0;
00212
00213 emit resourceLoaded( this );
00214 }
00215
00216 bool ResourceRemote::save()
00217 {
00218 kdDebug() << "ResourceRemote::save()" << endl;
00219
00220 if ( !mOpen ) return true;
00221
00222 if ( readOnly() ) {
00223 emit resourceSaved( this );
00224 return true;
00225 }
00226
00227 if ( mDownloadJob ) {
00228 kdWarning() << "ResourceRemote::save(): download still in progress."
00229 << endl;
00230 return false;
00231 }
00232 if ( mUploadJob ) {
00233 kdWarning() << "ResourceRemote::save(): upload still in progress."
00234 << endl;
00235 return false;
00236 }
00237
00238 mCalendar.save( cacheFile() );
00239
00240 mUploadJob = KIO::file_copy( KURL( cacheFile() ), mUploadUrl, -1, true );
00241 connect( mUploadJob, SIGNAL( result( KIO::Job * ) ),
00242 SLOT( slotSaveJobResult( KIO::Job * ) ) );
00243
00244 return true;
00245 }
00246
00247 bool ResourceRemote::isSaving()
00248 {
00249 return mUploadJob;
00250 }
00251
00252 void ResourceRemote::slotSaveJobResult( KIO::Job *job )
00253 {
00254 if ( job->error() ) {
00255 job->showErrorDialog( 0 );
00256 } else {
00257 kdDebug() << "ResourceRemote::slotSaveJobResult() success" << endl;
00258 }
00259
00260 mUploadJob = 0;
00261
00262 emit resourceSaved( this );
00263 }
00264
00265 void ResourceRemote::doClose()
00266 {
00267 if ( !mOpen ) return;
00268
00269 mCalendar.close();
00270 mOpen = false;
00271 }
00272
00273 KABC::Lock *ResourceRemote::lock()
00274 {
00275 return mLock;
00276 }
00277
00278 void ResourceRemote::update(IncidenceBase *)
00279 {
00280 }
00281
00282 void ResourceRemote::dump() const
00283 {
00284 ResourceCalendar::dump();
00285 kdDebug(5800) << " DownloadUrl: " << mDownloadUrl.url() << endl;
00286 kdDebug(5800) << " UploadUrl: " << mUploadUrl.url() << endl;
00287 kdDebug(5800) << " ReloadPolicy: " << mReloadPolicy << endl;
00288 }
00289
00290 #include "resourceremote.moc"
This file is part of the documentation for libkcal Library Version 3.2.2.