Messagelib

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

KDE's Doxygen guidelines are available online.