• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kaddressbook

  • sources
  • kde-4.14
  • kdepim
  • kaddressbook
  • sendmail
mailsenderjob.cpp
Go to the documentation of this file.
1 /*
2  This file is part of KAddressBook.
3 
4  Copyright (c) 2014-2015 Laurent Montel <montel@kde.org>
5  based on code from Copyright (c) 2014 ClĂ©ment Vannier <clement.vannier@free.fr>
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License along
18  with this program; if not, write to the Free Software Foundation, Inc.,
19  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21 
22 #include "mailsenderjob.h"
23 #include "utils.h"
24 #include "kpimutils/email.h"
25 
26 #include <KJob>
27 
28 #include <Akonadi/Item>
29 #include <Akonadi/ItemFetchJob>
30 #include <Akonadi/ItemFetchScope>
31 
32 #include <KABC/Addressee>
33 #include <KABC/ContactGroup>
34 
35 #include <QDebug>
36 #include <QItemSelectionModel>
37 using namespace KABMailSender;
38 
39 MailSenderJob::MailSenderJob(const Akonadi::Item::List &listItem, QObject *parent)
40  : QObject(parent),
41  mListItem(listItem),
42  mFetchJobCount(0)
43 {
44 }
45 
46 MailSenderJob::~MailSenderJob()
47 {
48 
49 }
50 
51 void MailSenderJob::start()
52 {
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);
60  }
61  }
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);
69  if (!email.isEmpty() && !mEmailAddresses.contains(email)) {
70  mEmailAddresses << email;
71  }
72  }
73  }
74  const unsigned int nbContactReference(group.contactReferenceCount());
75  for(unsigned int i=0; i<nbContactReference; ++i){
76  KABC::ContactGroup::ContactReference reference = group.contactReference(i);
77 
78  Akonadi::Item item;
79  if (reference.gid().isEmpty()) {
80  item.setId( reference.uid().toLongLong() );
81  } else {
82  item.setGid( reference.gid() );
83  }
84  mItemToFetch << item;
85  }
86  }
87  }
88 
89  if(mItemToFetch.isEmpty()) {
90  finishJob();
91  } else {
92  fetchNextItem();
93  }
94 }
95 
96 void MailSenderJob::fetchNextItem()
97 {
98  if (mFetchJobCount < mItemToFetch.count()) {
99  fetchItem(mItemToFetch.at(mFetchJobCount));
100  ++mFetchJobCount;
101  } else {
102  finishJob();
103  }
104 }
105 
106 void MailSenderJob::fetchItem(const Akonadi::Item &item)
107 {
108  Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( item, this );
109  job->fetchScope().fetchFullPayload();
110 
111  connect( job, SIGNAL(result(KJob*)), SLOT(fetchJobFinished(KJob*)) );
112 }
113 
114 void MailSenderJob::fetchJobFinished(KJob *job)
115 {
116  if ( job->error() ) {
117  qDebug()<<" error during fetching "<<job->errorString();
118  fetchNextItem();
119  return;
120  }
121 
122  Akonadi::ItemFetchJob *fetchJob = qobject_cast<Akonadi::ItemFetchJob*>( job );
123 
124  if ( fetchJob->items().count() != 1 ) {
125  fetchNextItem();
126  return;
127  }
128 
129  const Akonadi::Item item = fetchJob->items().first();
130  const KABC::Addressee contact = item.payload<KABC::Addressee>();
131 
132  if( !contact.preferredEmail().isEmpty()) {
133  if (KPIMUtils::isValidSimpleAddress(contact.preferredEmail())) {
134  mEmailAddresses << KPIMUtils::normalizedAddress(contact.formattedName(), contact.preferredEmail());
135  }
136  }
137  fetchNextItem();
138 }
139 
140 void MailSenderJob::finishJob()
141 {
142  if (!mEmailAddresses.isEmpty()) {
143  emit sendMails(mEmailAddresses);
144  } else {
145  //TODO add messageBox
146  qDebug() << "No emails found in contacts.";
147  }
148  deleteLater();
149 }
mailsenderjob.h
KABMailSender::MailSenderJob::MailSenderJob
MailSenderJob(const Akonadi::Item::List &listItem, QObject *parent=0)
Definition: mailsenderjob.cpp:39
QStringList::contains
bool contains(const QString &str, Qt::CaseSensitivity cs) const
utils.h
KABMailSender::MailSenderJob::start
void start()
Definition: mailsenderjob.cpp:51
QObject
QList::isEmpty
bool isEmpty() const
QString::isEmpty
bool isEmpty() const
QObject::deleteLater
void deleteLater()
QString
KABMailSender::MailSenderJob::~MailSenderJob
~MailSenderJob()
Definition: mailsenderjob.cpp:46
KABMailSender::MailSenderJob::sendMails
void sendMails(const QStringList &emails)
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:32:34 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kaddressbook

Skip menu "kaddressbook"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal