kabc
distributionlist.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "distributionlist.h"
00022 #include "resource.h"
00023
00024 #include <kconfig.h>
00025 #include <krandom.h>
00026 #include <kstandarddirs.h>
00027 #include <kdebug.h>
00028
00029 #include <QtGui/QApplication>
00030 #include <QtCore/QPair>
00031
00032 using namespace KABC;
00033
00034 class DistributionList::Entry::Private
00035 {
00036 public:
00037 Private() {}
00038 Private( Addressee _addressee, const QString &_email )
00039 : addressee( _addressee ), email( _email ) {}
00040
00041 Addressee addressee;
00042 QString email;
00043 };
00044
00045 DistributionList::Entry::Entry()
00046 : d( new Private() )
00047 {
00048 }
00049
00050 DistributionList::Entry::Entry( const DistributionList::Entry &entry )
00051 : d( new Private( entry.d->addressee, entry.d->email ) )
00052 {
00053 }
00054
00055 DistributionList::Entry::Entry( const Addressee &_addressee, const QString &_email )
00056 : d( new Private( _addressee, _email ) )
00057 {
00058 }
00059
00060 DistributionList::Entry::~Entry()
00061 {
00062 delete d;
00063 }
00064
00065 DistributionList::Entry &DistributionList::Entry::operator=( const DistributionList::Entry &entry )
00066 {
00067 d->addressee = entry.d->addressee;
00068 d->email = entry.d->email;
00069
00070 return *this;
00071 }
00072
00073 Addressee DistributionList::Entry::addressee() const
00074 {
00075 return d->addressee;
00076 }
00077
00078 QString DistributionList::Entry::email() const
00079 {
00080 return d->email;
00081 }
00082
00083 class DistributionList::Private
00084 {
00085 public:
00086 Private( Resource *resource, const QString &identifier,
00087 const QString &name )
00088 : mResource( resource ), mIdentifier( identifier ), mName( name )
00089 {
00090 }
00091
00092 Resource *mResource;
00093 QString mIdentifier;
00094 QString mName;
00095 Entry::List mEntries;
00096 };
00097
00098 DistributionList::DistributionList( Resource *resource, const QString &name )
00099 : d( new Private( resource, KRandom::randomString(10), name ) )
00100 {
00101 d->mResource->insertDistributionList( this );
00102 }
00103
00104 DistributionList::DistributionList( Resource *resource,
00105 const QString &identifier, const QString &name )
00106 : d( new Private( resource, identifier, name ) )
00107 {
00108 d->mResource->insertDistributionList( this );
00109 }
00110
00111 DistributionList::~DistributionList()
00112 {
00113 d->mResource->removeDistributionList( this );
00114
00115 delete d;
00116 }
00117
00118 void DistributionList::setIdentifier( const QString &identifier )
00119 {
00120 d->mIdentifier = identifier;
00121 }
00122
00123 QString DistributionList::identifier() const
00124 {
00125 return d->mIdentifier;
00126 }
00127
00128 void DistributionList::setName( const QString &name )
00129 {
00130 d->mName = name;
00131 }
00132
00133 QString DistributionList::name() const
00134 {
00135 return d->mName;
00136 }
00137
00138 void DistributionList::insertEntry( const Addressee &a, const QString &email )
00139 {
00140 Entry e( a, email );
00141
00142 QList<Entry>::Iterator it;
00143 for ( it = d->mEntries.begin(); it != d->mEntries.end(); ++it ) {
00144 if ( (*it).addressee().uid() == a.uid() ) {
00149 if ( ( (*it).email().isNull() && email.isEmpty() ) ||
00150 ( (*it).email().isEmpty() && email.isNull() ) ||
00151 ( (*it).email() == email ) ) {
00152 *it = e;
00153 return;
00154 }
00155 }
00156 }
00157 d->mEntries.append( e );
00158 }
00159
00160 void DistributionList::removeEntry( const Addressee &a, const QString &email )
00161 {
00162 QList<Entry>::Iterator it;
00163 for ( it = d->mEntries.begin(); it != d->mEntries.end(); ++it ) {
00164 if ( (*it).addressee().uid() == a.uid() && (*it).email() == email ) {
00165 d->mEntries.erase( it );
00166 return;
00167 }
00168 }
00169 }
00170
00171 QStringList DistributionList::emails() const
00172 {
00173 QStringList emails;
00174
00175 Entry::List::ConstIterator it;
00176 for ( it = d->mEntries.begin(); it != d->mEntries.end(); ++it ) {
00177 const Addressee a = (*it).addressee();
00178 QString email = (*it).email().isEmpty() ? a.fullEmail() :
00179 a.fullEmail( (*it).email() );
00180
00181 if ( !email.isEmpty() ) {
00182 emails.append( email );
00183 }
00184 }
00185
00186 return emails;
00187 }
00188
00189 DistributionList::Entry::List DistributionList::entries() const
00190 {
00191 return d->mEntries;
00192 }
00193
00194 Resource *DistributionList::resource() const
00195 {
00196 return d->mResource;
00197 }
00198
00199 typedef QList< QPair<QString, QString> > MissingEntryList;