24 #include "kpimutils/email.h"
28 #include <Akonadi/Item>
29 #include <Akonadi/ItemFetchJob>
30 #include <Akonadi/ItemFetchScope>
32 #include <KABC/Addressee>
33 #include <KABC/ContactGroup>
36 #include <QItemSelectionModel>
37 using namespace KABMailSender;
53 Q_FOREACH (
const Akonadi::Item &item, mListItem) {
54 if (item.hasPayload<KABC::Addressee>()) {
55 const KABC::Addressee contact = item.payload<KABC::Addressee>();
56 const QString preferredEmail = contact.preferredEmail();
57 if( !preferredEmail.isEmpty() && !mEmailAddresses.
contains(preferredEmail) ){
58 if (KPIMUtils::isValidSimpleAddress(contact.preferredEmail())) {
59 mEmailAddresses << KPIMUtils::normalizedAddress(contact.formattedName(), preferredEmail);
62 }
else if (item.hasPayload<KABC::ContactGroup>()) {
63 const KABC::ContactGroup group = item.payload<KABC::ContactGroup>();
64 unsigned int nbDataCount(group.dataCount());
65 for(
unsigned int i=0; i<nbDataCount; ++i) {
66 const QString currentEmail(group.data(i).email());
67 if (KPIMUtils::isValidSimpleAddress(currentEmail)) {
68 const QString email = KPIMUtils::normalizedAddress(group.data(i).name(), currentEmail);
70 mEmailAddresses << email;
74 const unsigned int nbContactReference(group.contactReferenceCount());
75 for(
unsigned int i=0; i<nbContactReference; ++i){
76 KABC::ContactGroup::ContactReference reference = group.contactReference(i);
79 if (reference.gid().isEmpty()) {
80 item.setId( reference.uid().toLongLong() );
82 item.setGid( reference.gid() );
89 if(mItemToFetch.isEmpty()) {
96 void MailSenderJob::fetchNextItem()
98 if (mFetchJobCount < mItemToFetch.count()) {
99 fetchItem(mItemToFetch.at(mFetchJobCount));
106 void MailSenderJob::fetchItem(
const Akonadi::Item &item)
108 Akonadi::ItemFetchJob *job =
new Akonadi::ItemFetchJob( item,
this );
109 job->fetchScope().fetchFullPayload();
111 connect( job, SIGNAL(result(KJob*)), SLOT(fetchJobFinished(KJob*)) );
114 void MailSenderJob::fetchJobFinished(KJob *job)
116 if ( job->error() ) {
117 qDebug()<<
" error during fetching "<<job->errorString();
122 Akonadi::ItemFetchJob *fetchJob = qobject_cast<Akonadi::ItemFetchJob*>( job );
124 if ( fetchJob->items().count() != 1 ) {
129 const Akonadi::Item item = fetchJob->items().first();
130 const KABC::Addressee contact = item.payload<KABC::Addressee>();
132 if( !contact.preferredEmail().isEmpty()) {
133 if (KPIMUtils::isValidSimpleAddress(contact.preferredEmail())) {
134 mEmailAddresses << KPIMUtils::normalizedAddress(contact.formattedName(), contact.preferredEmail());
140 void MailSenderJob::finishJob()
142 if (!mEmailAddresses.
isEmpty()) {
146 qDebug() <<
"No emails found in contacts.";
MailSenderJob(const Akonadi::Item::List &listItem, QObject *parent=0)
bool contains(const QString &str, Qt::CaseSensitivity cs) const
void sendMails(const QStringList &emails)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)