00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "resourcefile.h"
00023 #include "resourcefileconfig.h"
00024
00025 #include "kabc/formatfactory.h"
00026 #include "kabc/stdaddressbook.h"
00027 #include "kabc/lock.h"
00028
00029 #include <kio/scheduler.h>
00030 #include <kconfiggroup.h>
00031 #include <kdebug.h>
00032 #include <klocale.h>
00033 #include <ksavefile.h>
00034 #include <kstandarddirs.h>
00035 #include <ktemporaryfile.h>
00036
00037 #include <QtCore/QFile>
00038 #include <QtCore/QFileInfo>
00039
00040 #include <sys/types.h>
00041 #include <sys/stat.h>
00042 #include <signal.h>
00043 #include <unistd.h>
00044
00045 using namespace KABC;
00046
00047 typedef QList< QPair<QString, QString> > MissingEntryList;
00048
00049 class ResourceFile::ResourceFilePrivate
00050 {
00051 public:
00052 QMap< QString, MissingEntryList > mMissingEntries;
00053 };
00054
00055 ResourceFile::ResourceFile()
00056 : Resource(), mFormat( 0 ), mTempFile( 0 ),
00057 mAsynchronous( false ), d( new ResourceFilePrivate )
00058 {
00059 QString fileName, formatName;
00060
00061 fileName = StdAddressBook::fileName();
00062 formatName = "vcard";
00063
00064 init( fileName, formatName );
00065 }
00066
00067 ResourceFile::ResourceFile( const KConfigGroup &group )
00068 : Resource( group ), mFormat( 0 ), mTempFile( 0 ),
00069 mAsynchronous( false ), d( new ResourceFilePrivate )
00070 {
00071 QString fileName, formatName;
00072
00073 fileName = group.readPathEntry( "FileName", StdAddressBook::fileName() );
00074 formatName = group.readEntry( "FileFormat", "vcard" );
00075
00076 init( fileName, formatName );
00077 }
00078
00079 ResourceFile::ResourceFile( const QString &fileName,
00080 const QString &formatName )
00081 : Resource(), mFormat( 0 ), mTempFile( 0 ),
00082 mAsynchronous( false ), d( new ResourceFilePrivate )
00083 {
00084 init( fileName, formatName );
00085 }
00086
00087 void ResourceFile::init( const QString &fileName, const QString &formatName )
00088 {
00089 mFormatName = formatName;
00090
00091 FormatFactory *factory = FormatFactory::self();
00092 mFormat = factory->format( mFormatName );
00093
00094 if ( !mFormat ) {
00095 mFormatName = "vcard";
00096 mFormat = factory->format( mFormatName );
00097 }
00098
00099 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged(const QString&) ) );
00100 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged(const QString&) ) );
00101 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged(const QString&) ) );
00102
00103 setFileName( fileName );
00104
00105 mDirWatch.addFile( KStandardDirs::locateLocal( "data", "kabc/distlists" ) );
00106
00107 mLock = 0;
00108 }
00109
00110 ResourceFile::~ResourceFile()
00111 {
00112 delete d;
00113 d = 0;
00114 delete mFormat;
00115 mFormat = 0;
00116 }
00117
00118 void ResourceFile::writeConfig( KConfigGroup &group )
00119 {
00120 Resource::writeConfig( group );
00121
00122 if ( mFileName == StdAddressBook::fileName() ) {
00123 group.deleteEntry( "FileName" );
00124 } else {
00125 group.writePathEntry( "FileName", mFileName );
00126 }
00127
00128 group.writeEntry( "FileFormat", mFormatName );
00129 }
00130
00131 Ticket *ResourceFile::requestSaveTicket()
00132 {
00133 kDebug(5700) << "ResourceFile::requestSaveTicket()";
00134
00135 if ( !addressBook() ) {
00136 return 0;
00137 }
00138
00139 delete mLock;
00140 mLock = new Lock( mFileName );
00141
00142 if ( mLock->lock() ) {
00143 addressBook()->emitAddressBookLocked();
00144 } else {
00145 addressBook()->error( mLock->error() );
00146 kDebug(5700) << "ResourceFile::requestSaveTicket(): Unable to lock file '"
00147 << mFileName << "':" << mLock->error();
00148 return 0;
00149 }
00150
00151 return createTicket( this );
00152 }
00153
00154 void ResourceFile::releaseSaveTicket( Ticket *ticket )
00155 {
00156 delete ticket;
00157
00158 delete mLock;
00159 mLock = 0;
00160
00161 addressBook()->emitAddressBookUnlocked();
00162 }
00163
00164 bool ResourceFile::doOpen()
00165 {
00166 QFile file( mFileName );
00167
00168 if ( !file.exists() ) {
00169
00170 bool ok = file.open( QIODevice::WriteOnly );
00171 if ( ok ) {
00172 file.close();
00173 }
00174 return ok;
00175 } else {
00176 QFileInfo fileInfo( mFileName );
00177 if ( readOnly() || !fileInfo.isWritable() ) {
00178 if ( !file.open( QIODevice::ReadOnly ) ) {
00179 return false;
00180 }
00181 } else {
00182 if ( !file.open( QIODevice::ReadWrite ) ) {
00183 return false;
00184 }
00185 }
00186
00187 if ( file.size() == 0 ) {
00188 file.close();
00189 return true;
00190 }
00191
00192 bool ok = mFormat->checkFormat( &file );
00193 file.close();
00194
00195 return ok;
00196 }
00197 }
00198
00199 void ResourceFile::doClose()
00200 {
00201 }
00202
00203 bool ResourceFile::load()
00204 {
00205 kDebug(5700) << "ResourceFile::load(): '" << mFileName << "'";
00206
00207 mAsynchronous = false;
00208
00209 QFile file( mFileName );
00210 if ( !file.open( QIODevice::ReadOnly ) ) {
00211 addressBook()->error( i18n( "Unable to open file '%1'.", mFileName ) );
00212 return false;
00213 }
00214
00215 if ( !clearAndLoad( &file ) ) {
00216 addressBook()->error( i18n( "Problems during parsing file '%1'.", mFileName ) );
00217 return false;
00218 }
00219
00220 return true;
00221 }
00222
00223 bool ResourceFile::clearAndLoad( QFile *file )
00224 {
00225 clear();
00226
00227 bool addresseesOk = mFormat->loadAll( addressBook(), this, file );
00228
00229 bool listsOk = loadDistributionLists();
00230
00231 return addresseesOk && listsOk;
00232 }
00233
00234 bool ResourceFile::asyncLoad()
00235 {
00236 mAsynchronous = true;
00237
00238 load();
00239
00240 QTimer::singleShot( 0, this, SLOT( emitLoadingFinished() ) );
00241
00242 return true;
00243 }
00244
00245 bool ResourceFile::save( Ticket *ticket )
00246 {
00247 Q_UNUSED( ticket );
00248 kDebug(5700) << "ResourceFile::save()";
00249
00250
00251 QString extension = '_' + QString::number( QDate::currentDate().dayOfWeek() );
00252 (void) KSaveFile::simpleBackupFile( mFileName, QString(), extension );
00253
00254 mDirWatch.stopScan();
00255
00256 KSaveFile saveFile( mFileName );
00257 bool ok = false;
00258
00259 if ( saveFile.open() ) {
00260 saveToFile( &saveFile );
00261 ok = saveFile.finalize();
00262 }
00263
00264 if ( !ok ) {
00265 addressBook()->error( i18n( "Unable to save file '%1'.", mFileName ) );
00266 }
00267
00268 mDirWatch.startScan();
00269
00270 return ok;
00271 }
00272
00273 bool ResourceFile::asyncSave( Ticket *ticket )
00274 {
00275 kDebug(5700) << "ResourceFile::asyncSave()";
00276
00277 save( ticket );
00278
00279 QTimer::singleShot( 0, this, SLOT( emitSavingFinished() ) );
00280
00281 return true;
00282 }
00283
00284 void ResourceFile::emitLoadingFinished()
00285 {
00286 emit loadingFinished( this );
00287 }
00288
00289 void ResourceFile::emitSavingFinished()
00290 {
00291 emit savingFinished( this );
00292 }
00293
00294 bool ResourceFile::loadDistributionLists()
00295 {
00296 KConfig cfg( KStandardDirs::locateLocal( "data", "kabc/distlists" ) );
00297
00298 KConfigGroup cg( &cfg, "DistributionLists" );
00299 const QStringList entryList = cg.keyList();
00300
00301 d->mMissingEntries.clear();
00302
00303 QStringList::ConstIterator it;
00304 for ( it = entryList.constBegin(); it != entryList.constEnd(); ++it ) {
00305 const QString name = *it;
00306 const QStringList value = cg.readEntry( name, QStringList() );
00307
00308 kDebug(5700) << "ResourceFile::loadDistributionLists():" << name
00309 << ":" << value.join( "," );
00310
00311 DistributionList *list = new DistributionList( this, name );
00312
00313 MissingEntryList missingEntries;
00314 QStringList::ConstIterator entryIt = value.constBegin();
00315 while ( entryIt != value.constEnd() ) {
00316 QString id = *entryIt++;
00317 QString email = entryIt != value.constEnd() ? *entryIt : QString();
00318
00319 kDebug(5700) << "----- Entry" << id;
00320
00321 Addressee a = addressBook()->findByUid( id );
00322 if ( !a.isEmpty() ) {
00323 list->insertEntry( a, email );
00324 } else {
00325 missingEntries.append( qMakePair( id, email ) );
00326 }
00327
00328 if ( entryIt == value.constEnd() ) {
00329 break;
00330 }
00331 ++entryIt;
00332 }
00333
00334 d->mMissingEntries.insert( name, missingEntries );
00335 }
00336
00337 return true;
00338 }
00339
00340 void ResourceFile::saveDistributionLists()
00341 {
00342 kDebug(5700) << "ResourceFile::saveDistributionLists()";
00343
00344 KConfig cfg( KStandardDirs::locateLocal( "data", "kabc/distlists" ) );
00345 KConfigGroup cg( &cfg, "DistributionLists" );
00346 cg.deleteGroup();
00347
00348 QMapIterator<QString, DistributionList*> it( mDistListMap );
00349 while ( it.hasNext() ) {
00350 DistributionList *list = it.next().value();
00351 kDebug(5700) << " Saving '" << list->name() << "'";
00352
00353 QStringList value;
00354 const DistributionList::Entry::List entries = list->entries();
00355 DistributionList::Entry::List::ConstIterator it;
00356 for ( it = entries.begin(); it != entries.end(); ++it ) {
00357 value.append( (*it).addressee().uid() );
00358 value.append( (*it).email() );
00359 }
00360
00361 if ( d->mMissingEntries.find( list->name() ) != d->mMissingEntries.end() ) {
00362 const MissingEntryList missList = d->mMissingEntries[ list->name() ];
00363 MissingEntryList::ConstIterator missIt;
00364 for ( missIt = missList.begin(); missIt != missList.end(); ++missIt ) {
00365 value.append( (*missIt).first );
00366 value.append( (*missIt).second );
00367 }
00368 }
00369
00370 cg.writeEntry( list->name(), value );
00371 }
00372
00373 cg.sync();
00374 }
00375
00376 void ResourceFile::saveToFile( QFile *file )
00377 {
00378 mFormat->saveAll( addressBook(), this, file );
00379
00380 saveDistributionLists();
00381 }
00382
00383 void ResourceFile::setFileName( const QString &fileName )
00384 {
00385 mDirWatch.stopScan();
00386 if ( mDirWatch.contains( mFileName ) ) {
00387 mDirWatch.removeFile( mFileName );
00388 }
00389
00390 mFileName = fileName;
00391
00392 mDirWatch.addFile( mFileName );
00393 mDirWatch.startScan();
00394 }
00395
00396 QString ResourceFile::fileName() const
00397 {
00398 return mFileName;
00399 }
00400
00401 void ResourceFile::setFormat( const QString &format )
00402 {
00403 mFormatName = format;
00404 delete mFormat;
00405
00406 FormatFactory *factory = FormatFactory::self();
00407 mFormat = factory->format( mFormatName );
00408 }
00409
00410 QString ResourceFile::format() const
00411 {
00412 return mFormatName;
00413 }
00414
00415 void ResourceFile::fileChanged( const QString &path)
00416 {
00417 kDebug(5700) << "ResourceFile::fileChanged():" << path;
00418
00419 if ( !addressBook() ) {
00420 return;
00421 }
00422
00423 if ( path == KStandardDirs::locateLocal( "data", "kabc/distlists" ) ) {
00424
00425 qDeleteAll( mDistListMap );
00426
00427 loadDistributionLists();
00428
00429 kDebug(5700) << "addressBookChanged()";
00430 addressBook()->emitAddressBookChanged();
00431
00432 return;
00433 }
00434
00435
00436 if ( mAsynchronous ) {
00437 asyncLoad();
00438 } else {
00439 load();
00440 kDebug(5700) << "addressBookChanged()";
00441 addressBook()->emitAddressBookChanged();
00442 }
00443 }
00444
00445 void ResourceFile::removeAddressee( const Addressee &addr )
00446 {
00447 QFile::remove(
00448 QFile::encodeName( KStandardDirs::locateLocal( "data", "kabc/photos/" ) + addr.uid() ) );
00449
00450 QFile::remove(
00451 QFile::encodeName( KStandardDirs::locateLocal( "data", "kabc/logos/" ) + addr.uid() ) );
00452
00453 QFile::remove(
00454 QFile::encodeName( KStandardDirs::locateLocal( "data", "kabc/sounds/" ) + addr.uid() ) );
00455
00456 mAddrMap.remove( addr.uid() );
00457 }
00458
00459 #include "resourcefile.moc"