distributionlist.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <qapplication.h>
00022 #include <qpair.h>
00023 #include <qvaluelist.h>
00024
00025 #include <ksimpleconfig.h>
00026 #include <kstandarddirs.h>
00027 #include <kdebug.h>
00028
00029 #include "distributionlist.h"
00030
00031 using namespace KABC;
00032
00033 DistributionList::DistributionList( DistributionListManager *manager,
00034 const QString &name ) :
00035 mManager( manager ), mName( name )
00036 {
00037 mManager->insert( this );
00038 }
00039
00040 DistributionList::~DistributionList()
00041 {
00042 mManager->remove( this );
00043 }
00044
00045 void DistributionList::setName( const QString &name )
00046 {
00047 mName = name;
00048 }
00049
00050 QString DistributionList::name() const
00051 {
00052 return mName;
00053 }
00054
00055 void DistributionList::insertEntry( const Addressee &a, const QString &email )
00056 {
00057 Entry e( a, email );
00058
00059 QValueList<Entry>::Iterator it;
00060 for( it = mEntries.begin(); it != mEntries.end(); ++it ) {
00061 if ( (*it).addressee.uid() == a.uid() ) {
00066 if ( ( (*it).email.isNull() && email.isEmpty() ) ||
00067 ( (*it).email.isEmpty() && email.isNull() ) ||
00068 ( (*it).email == email ) ) {
00069 *it = e;
00070 return;
00071 }
00072 }
00073 }
00074 mEntries.append( e );
00075 }
00076
00077 void DistributionList::removeEntry( const Addressee &a, const QString &email )
00078 {
00079 QValueList<Entry>::Iterator it;
00080 for( it = mEntries.begin(); it != mEntries.end(); ++it ) {
00081 if ( (*it).addressee.uid() == a.uid() && (*it).email == email ) {
00082 mEntries.remove( it );
00083 return;
00084 }
00085 }
00086 }
00087
00088 QStringList DistributionList::emails() const
00089 {
00090 QStringList emails;
00091
00092 Entry::List::ConstIterator it;
00093 for( it = mEntries.begin(); it != mEntries.end(); ++it ) {
00094 Addressee a = (*it).addressee;
00095 QString email = (*it).email.isEmpty() ? a.fullEmail() :
00096 a.fullEmail( (*it).email );
00097
00098 if ( !email.isEmpty() ) {
00099 emails.append( email );
00100 }
00101 }
00102
00103 return emails;
00104 }
00105
00106 DistributionList::Entry::List DistributionList::entries() const
00107 {
00108 return mEntries;
00109 }
00110
00111 typedef QValueList< QPair<QString, QString> > MissingEntryList;
00112
00113 class DistributionListManager::DistributionListManagerPrivate
00114 {
00115 public:
00116 AddressBook *mAddressBook;
00117 QMap< QString, MissingEntryList > mMissingEntries;
00118 };
00119
00120 DistributionListManager::DistributionListManager( AddressBook *ab )
00121 : d( new DistributionListManagerPrivate )
00122 {
00123 d->mAddressBook = ab;
00124 mLists.setAutoDelete( true );
00125 }
00126
00127 DistributionListManager::~DistributionListManager()
00128 {
00129 mLists.clear();
00130
00131 delete d;
00132 d = 0;
00133 }
00134
00135 DistributionList *DistributionListManager::list( const QString &name )
00136 {
00137 DistributionList *list;
00138 for( list = mLists.first(); list; list = mLists.next() ) {
00139 if ( list->name() == name ) return list;
00140 }
00141
00142 return 0;
00143 }
00144
00145 void DistributionListManager::insert( DistributionList *l )
00146 {
00147 if ( !l )
00148 return;
00149
00150 DistributionList *list;
00151 for( list = mLists.first(); list; list = mLists.next() ) {
00152 if ( list->name() == l->name() ) {
00153 mLists.remove( list );
00154 break;
00155 }
00156 }
00157 mLists.append( l );
00158 }
00159
00160 void DistributionListManager::remove( DistributionList *l )
00161 {
00162 if ( !l )
00163 return;
00164
00165 DistributionList *list;
00166 for( list = mLists.first(); list; list = mLists.next() ) {
00167 if ( list->name() == l->name() ) {
00168 mLists.remove( list );
00169 return;
00170 }
00171 }
00172 }
00173
00174 QStringList DistributionListManager::listNames()
00175 {
00176 QStringList names;
00177
00178 DistributionList *list;
00179 for( list = mLists.first(); list; list = mLists.next() ) {
00180 names.append( list->name() );
00181 }
00182
00183 return names;
00184 }
00185
00186 bool DistributionListManager::load()
00187 {
00188 KSimpleConfig cfg( locateLocal( "data", "kabc/distlists" ) );
00189
00190 QMap<QString,QString> entryMap = cfg.entryMap( "DistributionLists" );
00191 cfg.setGroup( "DistributionLists" );
00192
00193
00194 mLists.clear();
00195 d->mMissingEntries.clear();
00196
00197 QMap<QString,QString>::ConstIterator it;
00198 for( it = entryMap.constBegin(); it != entryMap.constEnd(); ++it ) {
00199 QString name = it.key();
00200 QStringList value = cfg.readListEntry( name );
00201
00202 kdDebug(5700) << "DLM::load(): " << name << ": " << value.join(",") << endl;
00203
00204 DistributionList *list = new DistributionList( this, name );
00205
00206 MissingEntryList missingEntries;
00207 QStringList::ConstIterator entryIt = value.constBegin();
00208 while( entryIt != value.constEnd() ) {
00209 QString id = *entryIt++;
00210 QString email = *entryIt;
00211
00212 kdDebug(5700) << "----- Entry " << id << endl;
00213
00214 Addressee a = d->mAddressBook->findByUid( id );
00215 if ( !a.isEmpty() ) {
00216 list->insertEntry( a, email );
00217 } else {
00218 missingEntries.append( qMakePair( id, email ) );
00219 }
00220
00221 if ( entryIt == value.end() )
00222 break;
00223 ++entryIt;
00224 }
00225
00226 d->mMissingEntries.insert( name, missingEntries );
00227 }
00228
00229 return true;
00230 }
00231
00232 bool DistributionListManager::save()
00233 {
00234 kdDebug(5700) << "DistListManager::save()" << endl;
00235
00236 KSimpleConfig cfg( locateLocal( "data", "kabc/distlists" ) );
00237
00238 cfg.deleteGroup( "DistributionLists" );
00239 cfg.setGroup( "DistributionLists" );
00240
00241 DistributionList *list;
00242 for( list = mLists.first(); list; list = mLists.next() ) {
00243 kdDebug(5700) << " Saving '" << list->name() << "'" << endl;
00244
00245 QStringList value;
00246 const DistributionList::Entry::List entries = list->entries();
00247 DistributionList::Entry::List::ConstIterator it;
00248 for( it = entries.begin(); it != entries.end(); ++it ) {
00249 value.append( (*it).addressee.uid() );
00250 value.append( (*it).email );
00251 }
00252
00253 if ( d->mMissingEntries.find( list->name() ) != d->mMissingEntries.end() ) {
00254 const MissingEntryList missList = d->mMissingEntries[ list->name() ];
00255 MissingEntryList::ConstIterator missIt;
00256 for ( missIt = missList.begin(); missIt != missList.end(); ++missIt ) {
00257 value.append( (*missIt).first );
00258 value.append( (*missIt).second );
00259 }
00260 }
00261
00262 cfg.writeEntry( list->name(), value );
00263 }
00264
00265 cfg.sync();
00266
00267 return true;
00268 }
00269
00270 DistributionListWatcher* DistributionListWatcher::mSelf = 0;
00271
00272 DistributionListWatcher::DistributionListWatcher()
00273 : QObject( qApp, "DistributionListWatcher" )
00274 {
00275 mDirWatch = new KDirWatch;
00276 mDirWatch->addFile( locateLocal( "data", "kabc/distlists" ) );
00277
00278 connect( mDirWatch, SIGNAL( dirty( const QString& ) ), SIGNAL( changed() ) );
00279 mDirWatch->startScan();
00280 }
00281
00282 DistributionListWatcher::~DistributionListWatcher()
00283 {
00284 delete mDirWatch;
00285 mDirWatch = 0;
00286 }
00287
00288 DistributionListWatcher *DistributionListWatcher::self()
00289 {
00290 kdWarning( !qApp ) << "No QApplication object available, you'll get a memleak!" << endl;
00291
00292 if ( !mSelf )
00293 mSelf = new DistributionListWatcher();
00294
00295 return mSelf;
00296 }
00297
00298 #include "distributionlist.moc"
|