• 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
  • sendvcards
sendvcardsjob.cpp
Go to the documentation of this file.
1 /*
2  This file is part of KAddressBook.
3 
4  Copyright (c) 2015 Laurent Montel <montel@kde.org>
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License along
17  with this program; if not, write to the Free Software Foundation, Inc.,
18  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20 
21 #include "sendvcardsjob.h"
22 #include <KABC/Addressee>
23 #include <KABC/ContactGroup>
24 #include <KABC/VCardConverter>
25 #include <Akonadi/Item>
26 #include <Akonadi/ItemFetchJob>
27 #include <Akonadi/ItemFetchScope>
28 #include <QDebug>
29 #include <util/vcardutil.h>
30 #include <Akonadi/Contact/ContactGroupExpandJob>
31 #include "pimcommon/temporaryfile/attachmenttemporaryfilesdirs.h"
32 #include <KTempDir>
33 #include <KStandardDirs>
34 #include <KToolInvocation>
35 #include <QFile>
36 
37 using namespace KABSendVCards;
38 
39 SendVcardsJob::SendVcardsJob(const Akonadi::Item::List &listItem, QObject *parent)
40  : QObject(parent),
41  mListItem(listItem),
42  mTempDir(0),
43  mExpandGroupJobCount(0)
44 {
45  //Don't delete it.
46  mAttachmentTemporary = new PimCommon::AttachmentTemporaryFilesDirs();
47 }
48 
49 SendVcardsJob::~SendVcardsJob()
50 {
51  delete mTempDir;
52  mTempDir = 0;
53  //Don't delete it.
54  mAttachmentTemporary = 0;
55 }
56 
57 bool SendVcardsJob::start()
58 {
59  if (mListItem.isEmpty()) {
60  qDebug()<<" No Item found";
61  mAttachmentTemporary->deleteLater();
62  mAttachmentTemporary = 0;
63  deleteLater();
64  return false;
65  }
66 
67  Q_FOREACH (const Akonadi::Item &item, mListItem) {
68  if (item.hasPayload<KABC::Addressee>()) {
69  const KABC::Addressee contact = item.payload<KABC::Addressee>();
70  QByteArray data = item.payloadData();
71  //Workaround about broken kaddressbook fields.
72  PimCommon::VCardUtil vcardUtil;
73  vcardUtil.adaptVcard(data);
74  createTemporaryDir();
75  const QString contactRealName(contact.realName());
76  const QString attachmentName = (contactRealName.isEmpty() ? QLatin1String("vcard") : contactRealName ) + QLatin1String( ".vcf" );
77  createTemporaryFile(data, attachmentName);
78  } else if (item.hasPayload<KABC::ContactGroup>()) {
79  ++mExpandGroupJobCount;
80  const KABC::ContactGroup group = item.payload<KABC::ContactGroup>();
81  const QString groupName(group.name());
82  const QString attachmentName = ( groupName.isEmpty() ? QLatin1String("vcard") : groupName ) + QLatin1String( ".vcf" );
83  Akonadi::ContactGroupExpandJob *expandJob = new Akonadi::ContactGroupExpandJob( group, this );
84  expandJob->setProperty("groupName", attachmentName);
85  connect( expandJob, SIGNAL(result(KJob*)), this, SLOT(slotExpandGroupResult(KJob*)) );
86  expandJob->start();
87  }
88  }
89 
90  if (mExpandGroupJobCount == 0) {
91  jobFinished();
92  }
93  return true;
94 }
95 
96 void SendVcardsJob::createTemporaryDir()
97 {
98  if (!mTempDir) {
99  mTempDir = new KTempDir( KStandardDirs::locateLocal( "tmp", QLatin1String("sendvcards") ) );
100  mTempDir->setAutoRemove(false);
101  mAttachmentTemporary->addTempDir(mTempDir->name());
102  }
103 }
104 
105 void SendVcardsJob::jobFinished()
106 {
107  const QStringList lstAttachment = mAttachmentTemporary->temporaryFiles();
108  if (!lstAttachment.isEmpty()) {
109  KToolInvocation::invokeMailer( QString(), QString(), QString(), QString(), QString(), QString(), lstAttachment );
110  } else {
111  //KF5 add i18n
112  sendVCardsError(QLatin1String("No VCard created."));
113  }
114  mAttachmentTemporary->removeTempFiles();
115  deleteLater();
116 }
117 
118 void SendVcardsJob::slotExpandGroupResult(KJob* job)
119 {
120  Akonadi::ContactGroupExpandJob *expandJob = qobject_cast<Akonadi::ContactGroupExpandJob*>( job );
121  Q_ASSERT( expandJob );
122 
123  const QString attachmentName = expandJob->property("groupName").toString();
124  KABC::VCardConverter converter;
125  const QByteArray groupData = converter.exportVCards(expandJob->contacts(), KABC::VCardConverter::v3_0);
126  createTemporaryDir();
127  createTemporaryFile(groupData, attachmentName);
128 
129  --mExpandGroupJobCount;
130  if (mExpandGroupJobCount == 0) {
131  jobFinished();
132  }
133 }
134 
135 void SendVcardsJob::createTemporaryFile(const QByteArray &data, const QString &filename)
136 {
137  QFile file(mTempDir->name() + QLatin1Char('/') + filename);
138  if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
139  qDebug()<<"Can not write vcard filename :"<<filename;
140  //KF5 add i18n
141  sendVCardsError(QString::fromLatin1("Temporary file \'%1\' can not created").arg(filename));
142  return;
143  }
144 
145  QTextStream out(&file);
146  out << data;
147  file.close();
148  mAttachmentTemporary->addTempFile(file.fileName());
149 }
QByteArray
KABSendVCards::SendVcardsJob::SendVcardsJob
SendVcardsJob(const Akonadi::Item::List &listItem, QObject *parent=0)
Definition: sendvcardsjob.cpp:39
QFile
KABSendVCards::SendVcardsJob::start
bool start()
Definition: sendvcardsjob.cpp:57
QTextStream
KABSendVCards::SendVcardsJob::sendVCardsError
void sendVCardsError(const QString &error)
QObject
QList::isEmpty
bool isEmpty() const
QObject::deleteLater
void deleteLater()
QString
QStringList
QLatin1Char
sendvcardsjob.h
QLatin1String
QString::fromLatin1
QString fromLatin1(const char *str, int size)
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
KABSendVCards::SendVcardsJob::~SendVcardsJob
~SendVcardsJob()
Definition: sendvcardsjob.cpp:49
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