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