[Kde-pim] KDE/kdepimlibs/kabc/plugins/file

Kevin Krammer kevin.krammer at gmx.at
Wed Oct 3 19:24:17 BST 2007


SVN commit 720793 by krake:

Implemented distribution list I/O based on the distlists file the old
approach had been using.

- load and save code is basically copy&paste from the respective methods of
  the old DistributionListManager.

- in ResourceFile::save() I changed form the direct call to Format::saveAll
  to the already existing saveToFile() method so the distribution lists
  are saved as well

- use the already exisiting dir watcher to also watch the distslists file

CCMAIL: kde-pim at kde.org



 M  +114 -7    resourcefile.cpp  
 M  +3 -1      resourcefile.h  


--- trunk/KDE/kdepimlibs/kabc/plugins/file/resourcefile.cpp #720792:720793
@@ -44,6 +44,8 @@
 
 using namespace KABC;
 
+typedef QList< QPair<QString, QString> > MissingEntryList;
+
 class ResourceFile::ResourceFilePrivate
 {
   public:
@@ -52,6 +54,8 @@
 
     KIO::Job *mSaveJob;
     bool mIsSaving;
+
+    QMap< QString, MissingEntryList > mMissingEntries;
 };
 
 ResourceFile::ResourceFile()
@@ -103,12 +107,14 @@
     mFormat = factory->format( mFormatName );
   }
 
-  connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) );
-  connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) );
-  connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) );
+  connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged(const QString&) ) );
+  connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged(const QString&) ) );
+  connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged(const QString&) ) );
 
   setFileName( fileName );
 
+  mDirWatch.addFile( KStandardDirs::locateLocal( "data", "kabc/distlists" ) );
+
   mLock = 0;
 }
 
@@ -241,7 +247,12 @@
 bool ResourceFile::clearAndLoad( QFile *file )
 {
   clear();
-  return mFormat->loadAll( addressBook(), this, file );
+
+  bool addresseesOk = mFormat->loadAll( addressBook(), this, file );
+
+  bool listsOk = loadDistributionLists();
+
+  return addresseesOk && listsOk;
 }
 
 bool ResourceFile::asyncLoad()
@@ -323,7 +334,7 @@
   bool ok = false;
 
   if ( saveFile.open() ) {
-    mFormat->saveAll( addressBook(), this, &saveFile );
+    saveToFile( &saveFile );
     ok = saveFile.finalize();
   }
 
@@ -395,9 +406,93 @@
   mTempFile = 0;
 }
 
+bool ResourceFile::loadDistributionLists()
+{
+  KConfig cfg( KStandardDirs::locateLocal( "data", "kabc/distlists" ) );
+
+  KConfigGroup cg( &cfg, "DistributionLists" );
+  QMap<QString,QString> entryMap = cg.entryMap();
+
+  d->mMissingEntries.clear();
+
+  QMap<QString,QString>::ConstIterator it;
+  for ( it = entryMap.constBegin(); it != entryMap.constEnd(); ++it ) {
+    QString name = it.key();
+    QStringList value = cg.readEntry( name, QStringList() );
+
+    kDebug(5700) << "ResourceFile::loadDistributionLists():" << name
+                 << ":" << value.join( "," );
+
+    DistributionList *list = new DistributionList( this, name );
+
+    MissingEntryList missingEntries;
+    QStringList::ConstIterator entryIt = value.constBegin();
+    while ( entryIt != value.constEnd() ) {
+      QString id = *entryIt++;
+      QString email = entryIt != value.constEnd() ? *entryIt : QString();
+
+      kDebug(5700) << "----- Entry" << id;
+
+      Addressee a = addressBook()->findByUid( id );
+      if ( !a.isEmpty() ) {
+        list->insertEntry( a, email );
+      } else {
+        missingEntries.append( qMakePair( id, email ) );
+      }
+
+      if ( entryIt == value.constEnd() ) {
+        break;
+      }
+      ++entryIt;
+    }
+
+    d->mMissingEntries.insert( name, missingEntries );
+  }
+
+  return true;
+}
+
+void ResourceFile::saveDistributionLists()
+{
+  kDebug(5700) << "ResourceFile::saveDistributionLists()";
+
+  KConfig cfg( KStandardDirs::locateLocal( "data", "kabc/distlists" ) );
+  KConfigGroup cg( &cfg, "DistributionLists" );
+  cg.deleteGroup();
+
+  QMapIterator<QString, DistributionList*> it( mDistListMap );
+  while ( it.hasNext() ) {
+    DistributionList *list = it.next().value();
+    kDebug(5700) << "  Saving '" << list->name() << "'";
+
+    QStringList value;
+    const DistributionList::Entry::List entries = list->entries();
+    DistributionList::Entry::List::ConstIterator it;
+    for ( it = entries.begin(); it != entries.end(); ++it ) {
+      value.append( (*it).addressee().uid() );
+      value.append( (*it).email() );
+    }
+
+    if ( d->mMissingEntries.find( list->name() ) != d->mMissingEntries.end() ) {
+      const MissingEntryList missList = d->mMissingEntries[ list->name() ];
+      MissingEntryList::ConstIterator missIt;
+      for ( missIt = missList.begin(); missIt != missList.end(); ++missIt ) {
+        value.append( (*missIt).first );
+        value.append( (*missIt).second );
+      }
+    }
+
+    cg.writeEntry( list->name(), value );
+  }
+
+  cg.sync();
+}
+
 void ResourceFile::saveToFile( QFile *file )
 {
   mFormat->saveAll( addressBook(), this, file );
+
+  saveDistributionLists();
 }
 
 void ResourceFile::setFileName( const QString &fileName )
@@ -432,14 +527,26 @@
   return mFormatName;
 }
 
-void ResourceFile::fileChanged()
+void ResourceFile::fileChanged( const QString &path)
 {
-  kDebug(5700) << "ResourceFile::fileChanged():" << mFileName;
+  kDebug(5700) << "ResourceFile::fileChanged():" << path;
 
   if ( !addressBook() ) {
     return;
   }
 
+  if ( path == KStandardDirs::locateLocal( "data", "kabc/distlists" ) ) {
+    // clear old distribution lists
+    qDeleteAll( mDistListMap );
+
+    loadDistributionLists();
+
+    kDebug(5700) << "addressBookChanged()";
+    addressBook()->emitAddressBookChanged();
+
+    return;
+  }
+
 //  clear(); // moved to clearAndLoad()
   if ( mAsynchronous ) {
     asyncLoad();
--- trunk/KDE/kdepimlibs/kabc/plugins/file/resourcefile.h #720792:720793
@@ -148,7 +148,7 @@
     void uploadFinished( KJob *job );
 
   protected Q_SLOTS:
-    void fileChanged();
+    void fileChanged( const QString &path);
 
   protected:
     void init( const QString &fileName, const QString &format );
@@ -165,6 +165,8 @@
     void deleteLocalTempFile();
     void deleteStaleTempFile();
     bool hasTempFile() const { return mTempFile != 0; }
+    bool loadDistributionLists();
+    void saveDistributionLists();
 
     QString mFileName;
     QString mFormatName;
_______________________________________________
KDE PIM mailing list kde-pim at kde.org
https://mail.kde.org/mailman/listinfo/kde-pim
KDE PIM home page at http://pim.kde.org/



More information about the kde-pim mailing list