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 public:
00036 Private() {}
00037 Private( Addressee _addressee, const QString &_email )
00038 : addressee( _addressee ), email( _email ) {}
00039
00040 Addressee addressee;
00041 QString email;
00042 };
00043
00044 DistributionList::Entry::Entry()
00045 : d( new Private() )
00046 {
00047 }
00048
00049 DistributionList::Entry::Entry( const DistributionList::Entry &entry )
00050 : d( new Private( entry.d->addressee, entry.d->email ) )
00051 {
00052 }
00053
00054 DistributionList::Entry::Entry( const Addressee &_addressee, const QString &_email )
00055 : d( new Private( _addressee, _email ) )
00056 {
00057 }
00058
00059 DistributionList::Entry::~Entry()
00060 {
00061 delete d;
00062 }
00063
00064 DistributionList::Entry &DistributionList::Entry::operator=( const DistributionList::Entry &entry )
00065 {
00066 d->addressee = entry.d->addressee;
00067 d->email = entry.d->email;
00068
00069 return *this;
00070 }
00071
00072 Addressee DistributionList::Entry::addressee() const
00073 {
00074 return d->addressee;
00075 }
00076
00077 QString DistributionList::Entry::email() const
00078 {
00079 return d->email;
00080 }
00081
00082 class DistributionList::Private
00083 {
00084 public:
00085 Private( Resource *resource, const QString &identifier,
00086 const QString &name )
00087 : mResource( resource ), mIdentifier( identifier ), mName( name )
00088 {
00089 }
00090
00091 Resource *mResource;
00092 QString mIdentifier;
00093 QString mName;
00094 Entry::List mEntries;
00095 };
00096
00097 DistributionList::DistributionList( Resource *resource, const QString &name )
00098 : d( new Private( resource, KRandom::randomString(10), name ) )
00099 {
00100 d->mResource->insertDistributionList( this );
00101 }
00102
00103 DistributionList::DistributionList( Resource *resource, const QString &identifier, const QString &name )
00104 : d( new Private( resource, identifier, name ) )
00105 {
00106 d->mResource->insertDistributionList( this );
00107 }
00108
00109 DistributionList::~DistributionList()
00110 {
00111 d->mResource->removeDistributionList( this );
00112
00113 delete d;
00114 }
00115
00116 void DistributionList::setIdentifier( const QString &identifier )
00117 {
00118 d->mIdentifier = identifier;
00119 }
00120
00121 QString DistributionList::identifier() const
00122 {
00123 return d->mIdentifier;
00124 }
00125
00126 void DistributionList::setName( const QString &name )
00127 {
00128 d->mName = name;
00129 }
00130
00131 QString DistributionList::name() const
00132 {
00133 return d->mName;
00134 }
00135
00136 void DistributionList::insertEntry( const Addressee &a, const QString &email )
00137 {
00138 Entry e( a, email );
00139
00140 QList<Entry>::Iterator it;
00141 for ( it = d->mEntries.begin(); it != d->mEntries.end(); ++it ) {
00142 if ( (*it).addressee().uid() == a.uid() ) {
00147 if ( ( (*it).email().isNull() && email.isEmpty() ) ||
00148 ( (*it).email().isEmpty() && email.isNull() ) ||
00149 ( (*it).email() == email ) ) {
00150 *it = e;
00151 return;
00152 }
00153 }
00154 }
00155 d->mEntries.append( e );
00156 }
00157
00158 void DistributionList::removeEntry( const Addressee &a, const QString &email )
00159 {
00160 QList<Entry>::Iterator it;
00161 for ( it = d->mEntries.begin(); it != d->mEntries.end(); ++it ) {
00162 if ( (*it).addressee().uid() == a.uid() && (*it).email() == email ) {
00163 d->mEntries.erase( it );
00164 return;
00165 }
00166 }
00167 }
00168
00169 QStringList DistributionList::emails() const
00170 {
00171 QStringList emails;
00172
00173 Entry::List::ConstIterator it;
00174 for ( it = d->mEntries.begin(); it != d->mEntries.end(); ++it ) {
00175 const Addressee a = (*it).addressee();
00176 QString email = (*it).email().isEmpty() ? a.fullEmail() :
00177 a.fullEmail( (*it).email() );
00178
00179 if ( !email.isEmpty() ) {
00180 emails.append( email );
00181 }
00182 }
00183
00184 return emails;
00185 }
00186
00187 DistributionList::Entry::List DistributionList::entries() const
00188 {
00189 return d->mEntries;
00190 }
00191
00192 Resource* DistributionList::resource() const
00193 {
00194 return d->mResource;
00195 }
00196
00197 typedef QList< QPair<QString, QString> > MissingEntryList;