Framework for looking up contacts directly in nepomuk in addition to going through...
[kdepim.git] / messagecomposer / aliasesexpandjob.cpp
blobe51f71186c6e4a64c8d7b7988397591ea5d4ad09
1 /*
2 * This file is part of KMail.
4 * Copyright (c) 2010 KDAB
6 * Author: Tobias Koenig <tokoe@kde.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include "aliasesexpandjob.h"
24 #include "aliasesexpandjob_p.h"
26 #include <akonadi/contact/contactgroupexpandjob.h>
27 #include <akonadi/contact/contactgroupsearchjob.h>
28 #include <akonadi/contact/contactsearchjob.h>
30 #include <KPIMUtils/Email>
32 #include <messagecore/stringutil.h>
34 using namespace MessageComposer;
36 DistributionListExpandJob::DistributionListExpandJob( const QString &name, QObject *parent )
37 : KJob( parent ), mListName( name ), mIsEmpty( false )
41 DistributionListExpandJob::~DistributionListExpandJob()
45 void DistributionListExpandJob::start()
47 if ( mListName.isEmpty() ) {
48 emitResult();
49 return;
52 Akonadi::ContactGroupSearchJob *job = new Akonadi::ContactGroupSearchJob( this );
53 job->setQuery( Akonadi::ContactGroupSearchJob::Name, mListName );
54 connect( job, SIGNAL(result(KJob*)), SLOT(slotSearchDone(KJob*)) );
57 QString DistributionListExpandJob::addresses() const
59 return mEmailAddresses.join( QLatin1String( ", " ) );
62 bool DistributionListExpandJob::isEmpty() const
64 return mIsEmpty;
67 void DistributionListExpandJob::slotSearchDone( KJob *job )
69 if ( job->error() ) {
70 setError( job->error() );
71 setErrorText( job->errorText() );
72 emitResult();
73 return;
76 const Akonadi::ContactGroupSearchJob *searchJob = qobject_cast<Akonadi::ContactGroupSearchJob*>( job );
78 const KABC::ContactGroup::List groups = searchJob->contactGroups();
79 if ( groups.isEmpty() ) {
80 emitResult();
81 return;
84 Akonadi::ContactGroupExpandJob *expandJob = new Akonadi::ContactGroupExpandJob( groups.first() );
85 connect( expandJob, SIGNAL(result(KJob*)), SLOT(slotExpansionDone(KJob*)) );
86 expandJob->start();
89 void DistributionListExpandJob::slotExpansionDone( KJob *job )
91 if ( job->error() ) {
92 setError( job->error() );
93 setErrorText( job->errorText() );
94 emitResult();
95 return;
98 const Akonadi::ContactGroupExpandJob *expandJob = qobject_cast<Akonadi::ContactGroupExpandJob*>( job );
100 const KABC::Addressee::List contacts = expandJob->contacts();
102 foreach ( const KABC::Addressee &contact, contacts )
103 mEmailAddresses << contact.fullEmail();
105 mIsEmpty = mEmailAddresses.isEmpty();
107 emitResult();
111 AliasesExpandJob::AliasesExpandJob( const QString &recipients, const QString &defaultDomain, QObject *parent )
112 : KJob( parent ),
113 mRecipients( KPIMUtils::splitAddressList( recipients ) ),
114 mDefaultDomain( defaultDomain ),
115 mDistributionListExpansionJobs( 0 ),
116 mNicknameExpansionJobs( 0 )
120 AliasesExpandJob::~AliasesExpandJob()
124 void AliasesExpandJob::start()
126 // At first we try to expand the recipient to a distribution list
127 // or nick name and save the results in a map for later lookup
128 foreach ( const QString &recipient, mRecipients ) {
129 // check for distribution list
130 DistributionListExpandJob *expandJob = new DistributionListExpandJob( recipient, this );
131 expandJob->setProperty( "recipient", recipient );
132 connect( expandJob, SIGNAL(result(KJob*)), SLOT(slotDistributionListExpansionDone(KJob*)) );
133 mDistributionListExpansionJobs++;
134 expandJob->start();
136 // check for nick name
137 Akonadi::ContactSearchJob *searchJob = new Akonadi::ContactSearchJob( this );
138 searchJob->setProperty( "recipient", recipient );
139 searchJob->setQuery( Akonadi::ContactSearchJob::NickName, recipient.toLower() );
140 connect( searchJob, SIGNAL(result(KJob*)), SLOT(slotNicknameExpansionDone(KJob*)) );
141 mNicknameExpansionJobs++;
142 searchJob->start();
145 if ( mDistributionListExpansionJobs == 0 && mNicknameExpansionJobs == 0 )
146 emitResult();
149 QString AliasesExpandJob::addresses() const
151 return mEmailAddresses;
154 QStringList AliasesExpandJob::emptyDistributionLists() const
156 return mEmptyDistributionLists;
159 void AliasesExpandJob::slotDistributionListExpansionDone( KJob *job )
161 if ( job->error() ) {
162 setError( job->error() );
163 setErrorText( job->errorText() );
164 emitResult();
165 return;
168 const DistributionListExpandJob *expandJob = qobject_cast<DistributionListExpandJob*>( job );
169 const QString recipient = expandJob->property( "recipient" ).toString();
171 DistributionListExpansionResult result;
172 result.addresses = expandJob->addresses();
173 result.isEmpty = expandJob->isEmpty();
175 mDistListExpansionResults.insert( recipient, result );
177 mDistributionListExpansionJobs--;
178 if ( mDistributionListExpansionJobs == 0 && mNicknameExpansionJobs == 0 )
179 finishExpansion();
182 void AliasesExpandJob::slotNicknameExpansionDone( KJob *job )
184 if ( job->error() ) {
185 setError( job->error() );
186 setErrorText( job->errorText() );
187 emitResult();
188 return;
191 const Akonadi::ContactSearchJob *searchJob = qobject_cast<Akonadi::ContactSearchJob*>( job );
192 const KABC::Addressee::List contacts = searchJob->contacts();
193 const QString recipient = searchJob->property( "recipient" ).toString();
195 foreach ( const KABC::Addressee &contact, contacts ) {
196 if ( contact.nickName().toLower() == recipient.toLower() ) {
197 mNicknameExpansionResults.insert( recipient, contact.fullEmail() );
198 break;
202 mNicknameExpansionJobs--;
203 if ( mDistributionListExpansionJobs == 0 && mNicknameExpansionJobs == 0 )
204 finishExpansion();
207 void AliasesExpandJob::finishExpansion()
209 foreach ( const QString &recipient, mRecipients ) {
210 if( recipient.isEmpty() )
211 continue;
212 if ( !mEmailAddresses.isEmpty() )
213 mEmailAddresses += QLatin1String( ", " );
215 const QString receiver = recipient.trimmed();
217 // take prefetched expand distribution list results
218 const DistributionListExpansionResult result = mDistListExpansionResults.value( recipient );
220 if ( result.isEmpty ) {
221 mEmailAddresses += receiver;
222 mEmptyDistributionLists << receiver;
223 continue;
226 if ( !result.addresses.isEmpty() ) {
227 mEmailAddresses += result.addresses;
228 continue;
231 // take prefetched expand nick name results
232 if ( !mNicknameExpansionResults.value( recipient ).isEmpty() ) {
233 mEmailAddresses += mNicknameExpansionResults.value( recipient );
234 continue;
237 // check whether the address is missing the domain part
238 QString displayName, addrSpec, comment;
239 KPIMUtils::splitAddress( receiver, displayName, addrSpec, comment );
240 if ( !addrSpec.contains( QLatin1Char('@') ) ) {
241 if ( !mDefaultDomain.isEmpty() )
242 mEmailAddresses += KPIMUtils::normalizedAddress( displayName, addrSpec + QLatin1Char( '@' ) +
243 mDefaultDomain, comment );
244 else
245 mEmailAddresses += MessageCore::StringUtil::guessEmailAddressFromLoginName( addrSpec );
246 } else
247 mEmailAddresses += receiver;
250 emitResult();
253 #include "aliasesexpandjob.moc"
254 #include "aliasesexpandjob_p.moc"