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
14using namespace MessageViewer;
15
16ContactDisplayMessageMemento::ContactDisplayMessageMemento(const QString &emailAddress)
17 : QObject(nullptr)
18 , mEmailAddress(emailAddress)
19{
20 if (!emailAddress.isEmpty()) {
21 mSearchJob = new Akonadi::ContactSearchJob();
22 mSearchJob->setQuery(Akonadi::ContactSearchJob::Email, emailAddress.toLower(), Akonadi::ContactSearchJob::ExactMatch);
23 connect(mSearchJob.data(), &Akonadi::ContactSearchJob::result, this, &ContactDisplayMessageMemento::slotSearchJobFinished);
24 } else {
25 mFinished = true;
26 }
27}
28
29ContactDisplayMessageMemento::~ContactDisplayMessageMemento()
30{
31 if (mSearchJob) {
32 disconnect(mSearchJob.data(), &Akonadi::ContactSearchJob::result, this, &ContactDisplayMessageMemento::slotSearchJobFinished);
33 mSearchJob->kill();
34 }
35}
36
37void 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 {
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, this, [&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
107bool ContactDisplayMessageMemento::finished() const
108{
109 return mFinished;
110}
111
112void ContactDisplayMessageMemento::detach()
113{
114 disconnect(this, SIGNAL(update(MimeTreeParser::UpdateMode)), nullptr, nullptr);
115 disconnect(this, SIGNAL(changeDisplayMail(Viewer::DisplayFormatMessage, bool)), nullptr, nullptr);
116}
117
118bool 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
132QImage ContactDisplayMessageMemento::imageFromUrl() const
133{
134 return mImageFromUrl;
135}
136
137QPixmap ContactDisplayMessageMemento::gravatarPixmap() const
138{
139 return mGravatarPixmap;
140}
141
142void 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
165KContacts::Picture ContactDisplayMessageMemento::photo() const
166{
167 return mPhoto;
168}
169
170void 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
QStringList customs() const
Picture photo() const
bool isIntern() const
bool isEmpty() const
QString url() const
void data(KIO::Job *job, const QByteArray &data)
bool exec()
KIOCORE_EXPORT TransferJob * get(const QUrl &url, LoadType reload=NoReload, JobFlags flags=DefaultFlags)
QByteArray & append(QByteArrayView data)
bool load(QIODevice *device, const char *format)
bool loadFromData(QByteArrayView data, const char *format)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
T * data() const const
bool isEmpty() const const
AssumeLocalFile
QUrl fromUserInput(const QString &userInput, const QString &workingDirectory, UserInputResolutionOptions options)
bool isEmpty() const const
bool isLocalFile() const const
QString toLocalFile() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:59:13 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.