kabc Library API Documentation

distributionlist.cpp

00001 /*
00002     This file is part of libkabc.
00003     Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to
00017     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018     Boston, MA 02111-1307, USA.
00019 */
00020 
00021 #include <ksimpleconfig.h>
00022 #include <kstandarddirs.h>
00023 #include <kdebug.h>
00024 
00025 #include "distributionlist.h"
00026 
00027 using namespace KABC;
00028 
00029 DistributionList::DistributionList( DistributionListManager *manager,
00030                                     const QString &name ) :
00031   mManager( manager ), mName( name )
00032 {
00033   mManager->insert( this );
00034 }
00035 
00036 DistributionList::~DistributionList()
00037 {
00038   mManager->remove( this );
00039 }
00040 
00041 void DistributionList::setName( const QString &name )
00042 {
00043   mName = name;
00044 }
00045 
00046 QString DistributionList::name() const
00047 {
00048   return mName;
00049 }
00050 
00051 void DistributionList::insertEntry( const Addressee &a, const QString &email )
00052 {
00053   Entry e( a, email );
00054 
00055   QValueList<Entry>::Iterator it;
00056   for( it = mEntries.begin(); it != mEntries.end(); ++it ) {
00057     if ( (*it).addressee.uid() == a.uid() ) {
00062       if ( ( (*it).email.isNull() && email.isEmpty() ) ||
00063            ( (*it).email.isEmpty() && email.isNull() ) ||
00064            ( (*it).email == email ) ) {
00065         *it = e;
00066         return;
00067       }
00068     }
00069   }
00070   mEntries.append( e );
00071 }
00072 
00073 void DistributionList::removeEntry( const Addressee &a, const QString &email )
00074 {
00075   QValueList<Entry>::Iterator it;
00076   for( it = mEntries.begin(); it != mEntries.end(); ++it ) {
00077     if ( (*it).addressee.uid() == a.uid() && (*it).email == email ) {
00078       mEntries.remove( it  );
00079       return;
00080     }
00081   }
00082 }
00083 
00084 QStringList DistributionList::emails() const
00085 {
00086   QStringList emails;
00087 
00088   Entry::List::ConstIterator it;
00089   for( it = mEntries.begin(); it != mEntries.end(); ++it ) {
00090     Addressee a = (*it).addressee;
00091     QString email = (*it).email.isEmpty() ? a.fullEmail() :
00092                                             a.fullEmail( (*it).email );
00093 
00094     if ( !email.isEmpty() ) {
00095       emails.append( email );
00096     }
00097   }
00098   
00099   return emails;
00100 }
00101 
00102 DistributionList::Entry::List DistributionList::entries() const
00103 {
00104   return mEntries;
00105 }
00106 
00107 
00108 DistributionListManager::DistributionListManager( AddressBook *ab ) :
00109   mAddressBook( ab )
00110 {
00111 }
00112 
00113 DistributionListManager::~DistributionListManager()
00114 {
00115 }
00116 
00117 DistributionList *DistributionListManager::list( const QString &name )
00118 {
00119   DistributionList *list;
00120   for( list = mLists.first(); list; list = mLists.next() ) {
00121     if ( list->name() == name ) return list;
00122   }
00123   
00124   return 0;
00125 }
00126 
00127 void DistributionListManager::insert( DistributionList *l )
00128 {
00129   DistributionList *list;
00130   for( list = mLists.first(); list; list = mLists.next() ) {
00131     if ( list->name() == l->name() ) {
00132       mLists.remove( list );
00133       break;
00134     }
00135   }
00136   mLists.append( l );
00137 }
00138 
00139 void DistributionListManager::remove( DistributionList *l )
00140 {
00141   DistributionList *list;
00142   for( list = mLists.first(); list; list = mLists.next() ) {
00143     if ( list->name() == l->name() ) {
00144       mLists.remove( list );
00145       return;
00146     }
00147   }
00148 }
00149 
00150 QStringList DistributionListManager::listNames()
00151 {
00152   QStringList names;
00153 
00154   DistributionList *list;
00155   for( list = mLists.first(); list; list = mLists.next() ) {
00156     names.append( list->name() );
00157   }
00158 
00159   return names;
00160 }
00161 
00162 bool DistributionListManager::load()
00163 {
00164   KSimpleConfig cfg( locateLocal( "data", "kabc/distlists" ) );
00165 
00166   QMap<QString,QString> entryMap = cfg.entryMap( mAddressBook->identifier() );
00167   if ( entryMap.isEmpty() ) {
00168     kdDebug(5700) << "No distlists for '" << mAddressBook->identifier() << "'" << endl;
00169     return false;
00170   }
00171 
00172   cfg.setGroup( mAddressBook->identifier() );
00173 
00174   QMap<QString,QString>::ConstIterator it;
00175   for( it = entryMap.begin(); it != entryMap.end(); ++it ) {
00176     QString name = it.key();
00177     QStringList value = cfg.readListEntry( name );
00178 
00179     kdDebug(5700) << "DLM::load(): " << name << ": " << value.join(",") << endl;
00180 
00181     DistributionList *list = new DistributionList( this, name );
00182 
00183     QStringList::ConstIterator it2 = value.begin();
00184     while( it2 != value.end() ) {
00185       QString id = *it2++;
00186       QString email = *it2;
00187 
00188       kdDebug(5700) << "----- Entry " << id << endl; 
00189       
00190       Addressee a = mAddressBook->findByUid( id );
00191       if ( !a.isEmpty() ) {
00192         list->insertEntry( a, email );
00193       }
00194       
00195       if ( it2 == value.end() ) break;
00196       ++it2;
00197     }
00198   }
00199   
00200   return true;
00201 }
00202 
00203 bool DistributionListManager::save()
00204 {
00205   kdDebug(5700) << "DistListManager::save()" << endl;
00206 
00207   KSimpleConfig cfg( locateLocal( "data", "kabc/distlists" ) );
00208 
00209   cfg.deleteGroup( mAddressBook->identifier() );
00210   cfg.setGroup( mAddressBook->identifier() );
00211   
00212   DistributionList *list;
00213   for( list = mLists.first(); list; list = mLists.next() ) {
00214     kdDebug(5700) << "  Saving '" << list->name() << "'" << endl;
00215     QStringList value;
00216     DistributionList::Entry::List entries = list->entries();
00217     DistributionList::Entry::List::ConstIterator it;
00218     for( it = entries.begin(); it != entries.end(); ++it ) {
00219       value.append( (*it).addressee.uid() );
00220       value.append( (*it).email );
00221     }
00222     cfg.writeEntry( list->name(), value );
00223   }
00224   
00225   cfg.sync();
00226   
00227   return true;
00228 }
00229 
00230 DistributionListWatcher* DistributionListWatcher::mSelf = 0;
00231 
00232 DistributionListWatcher::DistributionListWatcher()
00233  : QObject( 0, "DistributionListWatcher" )
00234 {
00235   mDirWatch = new KDirWatch;
00236   mDirWatch->addFile( locateLocal( "data", "kabc/distlists" ) );
00237   
00238   connect( mDirWatch, SIGNAL( dirty( const QString& ) ), SIGNAL( changed() ) );
00239   mDirWatch->startScan();
00240 }
00241 
00242 DistributionListWatcher::~DistributionListWatcher()
00243 {
00244   delete mDirWatch;
00245   mDirWatch = 0;
00246 }
00247 
00248 DistributionListWatcher *DistributionListWatcher::self()
00249 {
00250   if ( !mSelf )
00251     mSelf = new DistributionListWatcher();
00252 
00253   return mSelf;
00254 }
00255 
00256 #include "distributionlist.moc"
00257 
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.0.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Oct 8 12:22:07 2003 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001