Messagelib

contactdisplaymessagememento.cpp
1 /*
2  * SPDX-FileCopyrightText: 2012-2021 Laurent Montel <[email protected]>
3  *
4  * SPDX-License-Identifier: LGPL-2.0-or-later
5  */
6 
7 #include "contactdisplaymessagememento.h"
8 #include "gravatarsettings.h"
9 #include "messageviewer_debug.h"
10 #include "settings/messageviewersettings.h"
11 #include <Akonadi/Contact/ContactSearchJob>
12 #include <Gravatar/GravatarResolvUrlJob>
13 #include <KIO/TransferJob>
14 
15 using namespace MessageViewer;
16 
17 ContactDisplayMessageMemento::ContactDisplayMessageMemento(const QString &emailAddress)
18  : QObject(nullptr)
19  , mEmailAddress(emailAddress)
20 {
21  if (!emailAddress.isEmpty()) {
22  mSearchJob = new Akonadi::ContactSearchJob();
24  connect(mSearchJob.data(), &Akonadi::ContactSearchJob::result, this, &ContactDisplayMessageMemento::slotSearchJobFinished);
25  } else {
26  mFinished = true;
27  }
28 }
29 
30 ContactDisplayMessageMemento::~ContactDisplayMessageMemento()
31 {
32  if (mSearchJob) {
33  disconnect(mSearchJob.data(), &Akonadi::ContactSearchJob::result, this, &ContactDisplayMessageMemento::slotSearchJobFinished);
34  mSearchJob->kill();
35  }
36 }
37 
38 void ContactDisplayMessageMemento::slotSearchJobFinished(KJob *job)
39 {
40  mFinished = true;
41  auto searchJob = static_cast<Akonadi::ContactSearchJob *>(job);
42  if (searchJob->error()) {
43  qCWarning(MESSAGEVIEWER_LOG) << "Unable to fetch contact:" << searchJob->errorText();
44  Q_EMIT update(MimeTreeParser::Delayed);
45  return;
46  }
47 
48  const int contactSize(searchJob->contacts().size());
49  if (contactSize >= 1) {
50  if (contactSize > 1) {
51  qCWarning(MESSAGEVIEWER_LOG) << " more than 1 contact was found we return first contact";
52  }
53 
54  const KContacts::Addressee addressee = searchJob->contacts().at(0);
55  processAddress(addressee);
56  if (!searchPhoto(searchJob->contacts())) {
57  // We have a data raw => we can update message
58  if (mPhoto.isIntern()) {
59  Q_EMIT update(MimeTreeParser::Delayed);
60  } else {
61  const QUrl url = QUrl::fromUserInput(mPhoto.url(), QString(), QUrl::AssumeLocalFile);
62  if (!url.isEmpty()) {
63  bool ok = false;
64  QImage image;
65 
66  if (url.isLocalFile()) {
67  if (image.load(url.toLocalFile())) {
68  ok = true;
69  }
70  } else {
71  QByteArray imageData;
72  KIO::TransferJob *jobTransfert = KIO::get(url, KIO::NoReload);
73  QObject::connect(jobTransfert, &KIO::TransferJob::data, [&imageData](KIO::Job *, const QByteArray &data) {
74  imageData.append(data);
75  });
76  if (jobTransfert->exec()) {
77  if (image.loadFromData(imageData)) {
78  ok = true;
79  }
80  }
81  }
82  if (ok) {
83  mImageFromUrl = image;
84  Q_EMIT update(MimeTreeParser::Delayed);
85  }
86  }
87  }
88  }
89  }
90  if (mPhoto.isEmpty() && mPhoto.url().isEmpty()) {
91  // No url, no photo => search gravatar
92  if (Gravatar::GravatarSettings::self()->gravatarSupportEnabled()) {
93  auto resolvUrljob = new Gravatar::GravatarResolvUrlJob(this);
94  resolvUrljob->setEmail(mEmailAddress);
95  resolvUrljob->setUseDefaultPixmap(Gravatar::GravatarSettings::self()->gravatarUseDefaultImage());
96  resolvUrljob->setFallbackGravatar(Gravatar::GravatarSettings::self()->fallbackToGravatar());
97  resolvUrljob->setUseLibravatar(Gravatar::GravatarSettings::self()->libravatarSupportEnabled());
98  if (resolvUrljob->canStart()) {
99  connect(resolvUrljob, &Gravatar::GravatarResolvUrlJob::finished, this, &ContactDisplayMessageMemento::slotGravatarResolvUrlFinished);
100  resolvUrljob->start();
101  } else {
102  resolvUrljob->deleteLater();
103  }
104  }
105  }
106 }
107 
108 bool ContactDisplayMessageMemento::finished() const
109 {
110  return mFinished;
111 }
112 
113 void ContactDisplayMessageMemento::detach()
114 {
115  disconnect(this, SIGNAL(update(MimeTreeParser::UpdateMode)), nullptr, nullptr);
116  disconnect(this, SIGNAL(changeDisplayMail(Viewer::DisplayFormatMessage, bool)), nullptr, nullptr);
117 }
118 
119 bool ContactDisplayMessageMemento::searchPhoto(const KContacts::AddresseeList &list)
120 {
121  bool foundPhoto = false;
122  for (const KContacts::Addressee &addressee : list) {
123  const KContacts::Picture photo = addressee.photo();
124  if (!photo.isEmpty()) {
125  mPhoto = photo;
126  foundPhoto = true;
127  break;
128  }
129  }
130  return foundPhoto;
131 }
132 
133 QImage ContactDisplayMessageMemento::imageFromUrl() const
134 {
135  return mImageFromUrl;
136 }
137 
138 QPixmap ContactDisplayMessageMemento::gravatarPixmap() const
139 {
140  return mGravatarPixmap;
141 }
142 
143 void ContactDisplayMessageMemento::processAddress(const KContacts::Addressee &addressee)
144 {
145  Viewer::DisplayFormatMessage forceDisplayTo = Viewer::UseGlobalSetting;
146  bool mailAllowToRemoteContent = false;
147  const QStringList customs = addressee.customs();
148  for (const QString &custom : customs) {
149  if (custom.contains(QLatin1String("MailPreferedFormatting"))) {
150  const QString value = addressee.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("MailPreferedFormatting"));
151  if (value == QLatin1String("TEXT")) {
152  forceDisplayTo = Viewer::Text;
153  } else if (value == QLatin1String("HTML")) {
154  forceDisplayTo = Viewer::Html;
155  } else {
156  forceDisplayTo = Viewer::UseGlobalSetting;
157  }
158  } else if (custom.contains(QLatin1String("MailAllowToRemoteContent"))) {
159  const QString value = addressee.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("MailAllowToRemoteContent"));
160  mailAllowToRemoteContent = (value == QLatin1String("TRUE"));
161  }
162  }
163  Q_EMIT changeDisplayMail(forceDisplayTo, mailAllowToRemoteContent);
164 }
165 
166 KContacts::Picture ContactDisplayMessageMemento::photo() const
167 {
168  return mPhoto;
169 }
170 
171 void ContactDisplayMessageMemento::slotGravatarResolvUrlFinished(Gravatar::GravatarResolvUrlJob *job)
172 {
173  if (job && job->hasGravatar()) {
174  mGravatarPixmap = job->pixmap();
175  Q_EMIT update(MimeTreeParser::Delayed);
176  }
177 }
QString custom(const QString &app, const QString &name) const
bool loadFromData(const uchar *data, int len, const char *format)
bool load(QIODevice *device, const char *format)
bool exec()
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
QUrl fromUserInput(const QString &userInput)
bool isEmpty() const const
void update()
bool isEmpty() const const
QString toLocalFile() const const
QByteArray & append(char ch)
KIOCORE_EXPORT TransferJob * get(const QUrl &url, LoadType reload=NoReload, JobFlags flags=DefaultFlags)
QString toLower() const const
void data(KIO::Job *job, const QByteArray &data)
AssumeLocalFile
QStringList customs() const
bool isEmpty() const
Picture photo() const
void result(KJob *job)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Q_EMITQ_EMIT
QString errorText() const
bool isLocalFile() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Thu Dec 2 2021 23:06:07 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.