00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <signal.h>
00023 #include <sys/types.h>
00024 #include <sys/stat.h>
00025 #include <unistd.h>
00026
00027 #include <qfile.h>
00028 #include <qfileinfo.h>
00029 #include <qregexp.h>
00030 #include <qtimer.h>
00031
00032 #include <kapplication.h>
00033 #include <kconfig.h>
00034 #include <kdebug.h>
00035 #include <kio/scheduler.h>
00036 #include <klocale.h>
00037 #include <ksavefile.h>
00038 #include <kstandarddirs.h>
00039 #include <ktempfile.h>
00040
00041 #include "formatfactory.h"
00042 #include "resourcefileconfig.h"
00043 #include "stdaddressbook.h"
00044 #include "lock.h"
00045
00046 #include "resourcefile.h"
00047
00048 using namespace KABC;
00049
00050 class ResourceFile::ResourceFilePrivate
00051 {
00052 public:
00053 KIO::Job *mLoadJob;
00054 bool mIsLoading;
00055
00056 KIO::Job *mSaveJob;
00057 bool mIsSaving;
00058 };
00059
00060 ResourceFile::ResourceFile( const KConfig *config )
00061 : Resource( config ), mFormat( 0 ), mLocalTempFile( 0 ),
00062 mAsynchronous( false ), d( new ResourceFilePrivate )
00063 {
00064 QString fileName, formatName;
00065
00066 if ( config ) {
00067 fileName = config->readPathEntry( "FileName", StdAddressBook::fileName() );
00068 formatName = config->readEntry( "FileFormat", "vcard" );
00069 } else {
00070 fileName = StdAddressBook::fileName();
00071 formatName = "vcard";
00072 }
00073
00074 init( fileName, formatName );
00075 }
00076
00077 ResourceFile::ResourceFile( const QString &fileName,
00078 const QString &formatName )
00079 : Resource( 0 ), mFormat( 0 ), mLocalTempFile( 0 ),
00080 mAsynchronous( false ), d( new ResourceFilePrivate )
00081 {
00082 init( fileName, formatName );
00083 }
00084
00085 void ResourceFile::init( const QString &fileName, const QString &formatName )
00086 {
00087 d->mLoadJob = 0;
00088 d->mIsLoading = false;
00089 d->mSaveJob = 0;
00090 d->mIsSaving = false;
00091
00092 mFormatName = formatName;
00093
00094 FormatFactory *factory = FormatFactory::self();
00095 mFormat = factory->format( mFormatName );
00096
00097 if ( !mFormat ) {
00098 mFormatName = "vcard";
00099 mFormat = factory->format( mFormatName );
00100 }
00101
00102 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) );
00103 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) );
00104 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) );
00105
00106 setFileName( fileName );
00107
00108 mLock = 0;
00109 }
00110
00111 ResourceFile::~ResourceFile()
00112 {
00113 if ( d->mIsLoading )
00114 d->mLoadJob->kill();
00115 if ( d->mIsSaving )
00116 d->mSaveJob->kill();
00117
00118 delete d;
00119 d = 0;
00120 delete mFormat;
00121 mFormat = 0;
00122 delete mLocalTempFile;
00123 mLocalTempFile = 0;
00124 }
00125
00126 void ResourceFile::writeConfig( KConfig *config )
00127 {
00128 Resource::writeConfig( config );
00129
00130 if ( mFileName == StdAddressBook::fileName() )
00131 config->deleteEntry( "FileName" );
00132 else
00133 config->writePathEntry( "FileName", mFileName );
00134
00135 config->writeEntry( "FileFormat", mFormatName );
00136 }
00137
00138 Ticket *ResourceFile::requestSaveTicket()
00139 {
00140 kdDebug(5700) << "ResourceFile::requestSaveTicket()" << endl;
00141
00142 if ( !addressBook() ) return 0;
00143
00144 delete mLock;
00145 mLock = new Lock( mFileName );
00146
00147 if ( mLock->lock() ) {
00148 addressBook()->emitAddressBookLocked();
00149 } else {
00150 addressBook()->error( mLock->error() );
00151 kdDebug(5700) << "ResourceFile::requestSaveTicket(): Unable to lock file '"
00152 << mFileName << "': " << mLock->error() << endl;
00153 return 0;
00154 }
00155
00156 return createTicket( this );
00157 }
00158
00159 void ResourceFile::releaseSaveTicket( Ticket *ticket )
00160 {
00161 delete ticket;
00162
00163 delete mLock;
00164 mLock = 0;
00165
00166 addressBook()->emitAddressBookUnlocked();
00167 }
00168
00169 bool ResourceFile::doOpen()
00170 {
00171 QFile file( mFileName );
00172
00173 if ( !file.exists() ) {
00174
00175 bool ok = file.open( IO_WriteOnly );
00176 if ( ok )
00177 file.close();
00178
00179 return ok;
00180 } else {
00181 QFileInfo fileInfo( mFileName );
00182 if ( readOnly() || !fileInfo.isWritable() ) {
00183 if ( !file.open( IO_ReadOnly ) )
00184 return false;
00185 } else {
00186 if ( !file.open( IO_ReadWrite ) )
00187 return false;
00188 }
00189
00190 if ( file.size() == 0 ) {
00191 file.close();
00192 return true;
00193 }
00194
00195 bool ok = mFormat->checkFormat( &file );
00196 file.close();
00197
00198 return ok;
00199 }
00200 }
00201
00202 void ResourceFile::doClose()
00203 {
00204 }
00205
00206 bool ResourceFile::load()
00207 {
00208 kdDebug(5700) << "ResourceFile::load(): '" << mFileName << "'" << endl;
00209
00210 mAsynchronous = false;
00211
00212 QFile file( mFileName );
00213 if ( !file.open( IO_ReadOnly ) ) {
00214 addressBook()->error( i18n( "Unable to open file '%1'." ).arg( mFileName ) );
00215 return false;
00216 }
00217
00218 return mFormat->loadAll( addressBook(), this, &file );
00219 }
00220
00221 bool ResourceFile::asyncLoad()
00222 {
00223 mAsynchronous = true;
00224
00225 if ( mLocalTempFile ) {
00226 kdDebug(5700) << "stale temp file detected " << mLocalTempFile->name() << endl;
00227 delete mLocalTempFile;
00228 }
00229
00230 mLocalTempFile = new KTempFile();
00231 mLocalTempFile->setAutoDelete( true );
00232 mTempFile = mLocalTempFile->name();
00233
00234 KURL dest, src;
00235 dest.setPath( mTempFile );
00236 src.setPath( mFileName );
00237
00238 KIO::Scheduler::checkSlaveOnHold( true );
00239 d->mLoadJob = KIO::file_copy( src, dest, -1, true, false, false );
00240 d->mIsLoading = true;
00241 connect( d->mLoadJob, SIGNAL( result( KIO::Job* ) ),
00242 this, SLOT( downloadFinished( KIO::Job* ) ) );
00243
00244 return true;
00245 }
00246
00247 bool ResourceFile::save( Ticket * )
00248 {
00249 kdDebug(5700) << "ResourceFile::save()" << endl;
00250
00251
00252 QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() );
00253 (void) KSaveFile::backupFile( mFileName, QString::null ,
00254 extension );
00255
00256 mDirWatch.stopScan();
00257 KSaveFile saveFile( mFileName );
00258 bool ok = false;
00259 if ( saveFile.status() == 0 && saveFile.file() )
00260 {
00261 mFormat->saveAll( addressBook(), this, saveFile.file() );
00262 ok = saveFile.close();
00263 }
00264
00265 if ( !ok )
00266 addressBook()->error( i18n( "Unable to save file '%1'." ).arg( mFileName ) );
00267 mDirWatch.startScan();
00268
00269 return ok;
00270 }
00271
00272 bool ResourceFile::asyncSave( Ticket * )
00273 {
00274 QFile file( mTempFile );
00275
00276 if ( !file.open( IO_WriteOnly ) ) {
00277 emit savingError( this, i18n( "Unable to open file '%1'." ).arg( mTempFile ) );
00278 return false;
00279 }
00280
00281 mDirWatch.stopScan();
00282 mFormat->saveAll( addressBook(), this, &file );
00283 file.close();
00284
00285 KURL src, dest;
00286 src.setPath( mTempFile );
00287 dest.setPath( mFileName );
00288
00289 KIO::Scheduler::checkSlaveOnHold( true );
00290 d->mSaveJob = KIO::file_copy( src, dest, -1, true, false, false );
00291 d->mIsSaving = true;
00292 connect( d->mSaveJob, SIGNAL( result( KIO::Job* ) ),
00293 this, SLOT( uploadFinished( KIO::Job* ) ) );
00294
00295 return true;
00296 }
00297
00298 void ResourceFile::setFileName( const QString &fileName )
00299 {
00300 mDirWatch.stopScan();
00301 if ( mDirWatch.contains( mFileName ) )
00302 mDirWatch.removeFile( mFileName );
00303
00304 mFileName = fileName;
00305
00306 mDirWatch.addFile( mFileName );
00307 mDirWatch.startScan();
00308 }
00309
00310 QString ResourceFile::fileName() const
00311 {
00312 return mFileName;
00313 }
00314
00315 void ResourceFile::setFormat( const QString &format )
00316 {
00317 mFormatName = format;
00318 delete mFormat;
00319
00320 FormatFactory *factory = FormatFactory::self();
00321 mFormat = factory->format( mFormatName );
00322 }
00323
00324 QString ResourceFile::format() const
00325 {
00326 return mFormatName;
00327 }
00328
00329 void ResourceFile::fileChanged()
00330 {
00331 if ( !addressBook() )
00332 return;
00333
00334 clear();
00335 if ( mAsynchronous )
00336 asyncLoad();
00337 else {
00338 load();
00339 kdDebug() << "addressBookChanged() " << endl;
00340 addressBook()->emitAddressBookChanged();
00341 }
00342 }
00343
00344 void ResourceFile::removeAddressee( const Addressee &addr )
00345 {
00346 QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/photos/" ) + addr.uid() ) );
00347 QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/logos/" ) + addr.uid() ) );
00348 QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/sounds/" ) + addr.uid() ) );
00349
00350 mAddrMap.erase( addr.uid() );
00351 }
00352
00353 void ResourceFile::downloadFinished( KIO::Job* )
00354 {
00355 d->mIsLoading = false;
00356
00357 if ( !mLocalTempFile )
00358 emit loadingError( this, i18n( "Download failed in some way!" ) );
00359
00360 QFile file( mTempFile );
00361 if ( !file.open( IO_ReadOnly ) ) {
00362 emit loadingError( this, i18n( "Unable to open file '%1'." ).arg( mTempFile ) );
00363 return;
00364 }
00365
00366 if ( !mFormat->loadAll( addressBook(), this, &file ) )
00367 emit loadingError( this, i18n( "Problems during parsing file '%1'." ).arg( mTempFile ) );
00368 else
00369 emit loadingFinished( this );
00370 }
00371
00372 void ResourceFile::uploadFinished( KIO::Job *job )
00373 {
00374 d->mIsSaving = false;
00375
00376 if ( job->error() )
00377 emit savingError( this, job->errorString() );
00378 else
00379 emit savingFinished( this );
00380 mDirWatch.startScan();
00381 }
00382
00383 #include "resourcefile.moc"