• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

kabc

resourcefile.cpp

00001 /*
00002     This file is part of libkabc.
00003 
00004     Copyright (c) 2001,2003 Cornelius Schumacher <schumacher@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to
00018     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019     Boston, MA 02110-1301, USA.
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();
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() << "Unable to lock file '" << mFileName
00147              << "':" << 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     // try to create the file
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() << 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();
00249 
00250   // create backup file
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();
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   KConfigGroup cgId( &cfg, "DistributionLists-Identifiers" );
00300   const QStringList entryList = cg.keyList();
00301 
00302   d->mMissingEntries.clear();
00303 
00304   QStringList::ConstIterator it;
00305   for ( it = entryList.constBegin(); it != entryList.constEnd(); ++it ) {
00306     const QString name = *it;
00307     const QStringList value = cg.readEntry( name, QStringList() );
00308 
00309     kDebug() << name << ":" << value.join( "," );
00310 
00311     DistributionList *list = 0;
00312     if ( cgId.isValid() ) {
00313       const QString identifier = cgId.readEntry( name, QString() );
00314       if ( !identifier.isEmpty() )
00315         list = new DistributionList( this, identifier, name );
00316     }
00317 
00318     if ( list == 0 )
00319         list = new DistributionList( this, name );
00320 
00321     MissingEntryList missingEntries;
00322     QStringList::ConstIterator entryIt = value.constBegin();
00323     while ( entryIt != value.constEnd() ) {
00324       QString id = *entryIt++;
00325       QString email = entryIt != value.constEnd() ? *entryIt : QString();
00326       if ( email.isEmpty() && !email.isNull() ) {
00327         email = QString();
00328       }
00329 
00330       kDebug() << "----- Entry" << id;
00331 
00332       Addressee a = addressBook()->findByUid( id );
00333       if ( !a.isEmpty() ) {
00334         list->insertEntry( a, email );
00335       } else {
00336         missingEntries.append( qMakePair( id, email ) );
00337       }
00338 
00339       if ( entryIt == value.constEnd() ) {
00340         break;
00341       }
00342       ++entryIt;
00343     }
00344 
00345     d->mMissingEntries.insert( name, missingEntries );
00346   }
00347 
00348   return true;
00349 }
00350 
00351 void ResourceFile::saveDistributionLists()
00352 {
00353   kDebug();
00354 
00355   KConfig cfg( KStandardDirs::locateLocal( "data", "kabc/distlists" ) );
00356   KConfigGroup cg( &cfg, "DistributionLists" );
00357   cg.deleteGroup();
00358   KConfigGroup cgId( &cfg, "DistributionLists-Identifiers" );
00359   cgId.deleteGroup();
00360 
00361   QMapIterator<QString, DistributionList*> it( mDistListMap );
00362   while ( it.hasNext() ) {
00363     DistributionList *list = it.next().value();
00364     kDebug() << "  Saving '" << list->name() << "'";
00365 
00366     QStringList value;
00367     const DistributionList::Entry::List entries = list->entries();
00368     DistributionList::Entry::List::ConstIterator it;
00369     for ( it = entries.begin(); it != entries.end(); ++it ) {
00370       value.append( (*it).addressee().uid() );
00371       value.append( (*it).email() );
00372     }
00373 
00374     if ( d->mMissingEntries.find( list->name() ) != d->mMissingEntries.end() ) {
00375       const MissingEntryList missList = d->mMissingEntries[ list->name() ];
00376       MissingEntryList::ConstIterator missIt;
00377       for ( missIt = missList.begin(); missIt != missList.end(); ++missIt ) {
00378         value.append( (*missIt).first );
00379         value.append( (*missIt).second );
00380       }
00381     }
00382 
00383     cg.writeEntry( list->name(), value );
00384     cgId.writeEntry( list->name(), list->identifier() );
00385   }
00386 
00387   cfg.sync();
00388 }
00389 
00390 void ResourceFile::saveToFile( QFile *file )
00391 {
00392   mFormat->saveAll( addressBook(), this, file );
00393 
00394   saveDistributionLists();
00395 }
00396 
00397 void ResourceFile::setFileName( const QString &fileName )
00398 {
00399   mDirWatch.stopScan();
00400   if ( mDirWatch.contains( mFileName ) ) {
00401     mDirWatch.removeFile( mFileName );
00402   }
00403 
00404   mFileName = fileName;
00405 
00406   mDirWatch.addFile( mFileName );
00407   mDirWatch.startScan();
00408 }
00409 
00410 QString ResourceFile::fileName() const
00411 {
00412   return mFileName;
00413 }
00414 
00415 void ResourceFile::setFormat( const QString &format )
00416 {
00417   mFormatName = format;
00418   delete mFormat;
00419 
00420   FormatFactory *factory = FormatFactory::self();
00421   mFormat = factory->format( mFormatName );
00422 }
00423 
00424 QString ResourceFile::format() const
00425 {
00426   return mFormatName;
00427 }
00428 
00429 void ResourceFile::fileChanged( const QString &path )
00430 {
00431   kDebug() << path;
00432 
00433   if ( !addressBook() ) {
00434     return;
00435   }
00436 
00437   if ( path == KStandardDirs::locateLocal( "data", "kabc/distlists" ) ) {
00438     // clear old distribution lists
00439     // take a copy of mDistListMap, then clear it and finally qDeleteAll
00440     // the copy to avoid problems with removeDistributionList() called by
00441     // ~DistributionList().
00442     DistributionListMap tempDistListMap( mDistListMap );
00443     mDistListMap.clear();
00444     qDeleteAll( tempDistListMap );
00445 
00446     loadDistributionLists();
00447 
00448     kDebug() << "addressBookChanged()";
00449     addressBook()->emitAddressBookChanged();
00450 
00451     return;
00452   }
00453 
00454 //  clear(); // moved to clearAndLoad()
00455   if ( mAsynchronous ) {
00456     asyncLoad();
00457   } else {
00458     load();
00459     kDebug() << "addressBookChanged()";
00460     addressBook()->emitAddressBookChanged();
00461   }
00462 }
00463 
00464 void ResourceFile::removeAddressee( const Addressee &addr )
00465 {
00466   QFile::remove(
00467     QFile::encodeName( KStandardDirs::locateLocal( "data", "kabc/photos/" ) + addr.uid() ) );
00468 
00469   QFile::remove(
00470     QFile::encodeName( KStandardDirs::locateLocal( "data", "kabc/logos/" ) + addr.uid() ) );
00471 
00472   QFile::remove(
00473     QFile::encodeName( KStandardDirs::locateLocal( "data", "kabc/sounds/" ) + addr.uid() ) );
00474 
00475   mAddrMap.remove( addr.uid() );
00476 }
00477 
00478 #include "resourcefile.moc"

kabc

Skip menu "kabc"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  • kabc
  • kblog
  • kcal
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal