1 | /* |
2 | This file is part of libkabc. |
3 | Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> |
4 | |
5 | This library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Library General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2 of the License, or (at your option) any later version. |
9 | |
10 | This library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Library General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Library General Public License |
16 | along with this library; see the file COPYING.LIB. If not, write to |
17 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
18 | Boston, MA 02110-1301, USA. |
19 | */ |
20 | |
21 | #include "distributionlist.h" |
22 | #include "resource.h" |
23 | |
24 | #include <kconfig.h> |
25 | #include <krandom.h> |
26 | #include <kstandarddirs.h> |
27 | #include <kdebug.h> |
28 | |
29 | #include <QApplication> |
30 | #include <QtCore/QPair> |
31 | #include <QtCore/QPointer> |
32 | |
33 | using namespace KABC; |
34 | |
35 | class DistributionList::Entry::Private |
36 | { |
37 | public: |
38 | Private() {} |
39 | Private( Addressee _addressee, const QString &_email ) |
40 | : addressee( _addressee ), email( _email ) {} |
41 | |
42 | Addressee addressee; |
43 | QString email; |
44 | }; |
45 | |
46 | DistributionList::Entry::Entry() |
47 | : d( new Private() ) |
48 | { |
49 | } |
50 | |
51 | DistributionList::Entry::Entry( const DistributionList::Entry &entry ) |
52 | : d( new Private( entry.d->addressee, entry.d->email ) ) |
53 | { |
54 | } |
55 | |
56 | DistributionList::Entry::Entry( const Addressee &_addressee, const QString &_email ) |
57 | : d( new Private( _addressee, _email ) ) |
58 | { |
59 | } |
60 | |
61 | DistributionList::Entry::~Entry() |
62 | { |
63 | delete d; |
64 | } |
65 | |
66 | DistributionList::Entry &DistributionList::Entry::operator=( const DistributionList::Entry &entry ) |
67 | { |
68 | d->addressee = entry.d->addressee; |
69 | d->email = entry.d->email; |
70 | |
71 | return *this; |
72 | } |
73 | |
74 | Addressee DistributionList::Entry::addressee() const |
75 | { |
76 | return d->addressee; |
77 | } |
78 | |
79 | QString DistributionList::Entry::email() const |
80 | { |
81 | return d->email; |
82 | } |
83 | |
84 | class DistributionList::Private |
85 | { |
86 | public: |
87 | Private( Resource *resource, const QString &identifier, |
88 | const QString &name ) |
89 | : mResource( resource ), mIdentifier( identifier ), mName( name ) |
90 | { |
91 | } |
92 | |
93 | QPointer<Resource> mResource; |
94 | QString mIdentifier; |
95 | QString mName; |
96 | Entry::List mEntries; |
97 | }; |
98 | |
99 | DistributionList::DistributionList( Resource *resource, const QString &name ) |
100 | : d( new Private( resource, KRandom::randomString(10), name ) ) |
101 | { |
102 | d->mResource->insertDistributionList( this ); |
103 | } |
104 | |
105 | DistributionList::DistributionList( Resource *resource, |
106 | const QString &identifier, const QString &name ) |
107 | : d( new Private( resource, identifier, name ) ) |
108 | { |
109 | d->mResource->insertDistributionList( this ); |
110 | } |
111 | |
112 | DistributionList::~DistributionList() |
113 | { |
114 | if ( d->mResource ) { |
115 | d->mResource->removeDistributionList( this ); |
116 | } |
117 | |
118 | delete d; |
119 | } |
120 | |
121 | void DistributionList::setIdentifier( const QString &identifier ) |
122 | { |
123 | d->mIdentifier = identifier; |
124 | } |
125 | |
126 | QString DistributionList::identifier() const |
127 | { |
128 | return d->mIdentifier; |
129 | } |
130 | |
131 | void DistributionList::setName( const QString &name ) |
132 | { |
133 | d->mName = name; |
134 | } |
135 | |
136 | QString DistributionList::name() const |
137 | { |
138 | return d->mName; |
139 | } |
140 | |
141 | void DistributionList::insertEntry( const Addressee &a, const QString &email ) |
142 | { |
143 | Entry e( a, email ); |
144 | |
145 | QList<Entry>::Iterator it; |
146 | for ( it = d->mEntries.begin(); it != d->mEntries.end(); ++it ) { |
147 | if ( ( *it ).addressee().uid() == a.uid() ) { |
148 | /** |
149 | We have to check if both email addresses contains no data, |
150 | a simple 'email1 == email2' wont work here |
151 | */ |
152 | if ( ( ( *it ).email().isNull() && email.isEmpty() ) || |
153 | ( ( *it ).email().isEmpty() && email.isNull() ) || |
154 | ( ( *it ).email() == email ) ) { |
155 | *it = e; |
156 | return; |
157 | } |
158 | } |
159 | } |
160 | d->mEntries.append( e ); |
161 | } |
162 | |
163 | void DistributionList::removeEntry( const Addressee &a, const QString &email ) |
164 | { |
165 | QList<Entry>::Iterator it; |
166 | for ( it = d->mEntries.begin(); it != d->mEntries.end(); ++it ) { |
167 | if ( ( *it ).addressee().uid() == a.uid() && ( *it ).email() == email ) { |
168 | d->mEntries.erase( it ); |
169 | return; |
170 | } |
171 | } |
172 | } |
173 | |
174 | QStringList DistributionList::emails() const |
175 | { |
176 | QStringList emails; |
177 | |
178 | Entry::List::ConstIterator it; |
179 | for ( it = d->mEntries.constBegin(); it != d->mEntries.constEnd(); ++it ) { |
180 | const Addressee a = ( *it ).addressee(); |
181 | QString email = ( *it ).email().isEmpty() ? a.fullEmail() : |
182 | a.fullEmail( ( *it ).email() ); |
183 | |
184 | if ( !email.isEmpty() ) { |
185 | emails.append( email ); |
186 | } |
187 | } |
188 | |
189 | return emails; |
190 | } |
191 | |
192 | DistributionList::Entry::List DistributionList::entries() const |
193 | { |
194 | return d->mEntries; |
195 | } |
196 | |
197 | Resource *DistributionList::resource() const |
198 | { |
199 | return d->mResource; |
200 | } |
201 | |
202 | typedef QList< QPair<QString, QString> > MissingEntryList; |
203 | |