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