KPeople

avatarimageprovider.cpp
1/*
2 SPDX-FileCopyrightText: 2022 Jonah BrĂ¼chert <jbb@kaidan.im>
3
4 SPDX-License-Identifier: LGPL-2.1-or-later
5*/
6
7#include "avatarimageprovider.h"
8
9#include <abstractcontact.h>
10#include <persondata.h>
11
12#include "kpeopledeclarative_debug.h"
13
14AvatarImageProvider::AvatarImageProvider()
16{
17}
18
19QPixmap AvatarImageProvider::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
20{
21 const auto base64encoded = QStringView(id).split(u'#').constFirst();
22 const auto decoded = QByteArray::fromBase64(base64encoded.toUtf8(), QByteArray::AbortOnBase64DecodingErrors);
23 if (decoded.isEmpty()) {
24 qCDebug(KPEOPLE_DECLARATIVE_LOG) << "AvatarImageProvider:" << id << "could not be decoded as a person uri";
25 return {};
26 }
27
28 auto personUri = QString::fromUtf8(decoded);
29 if (personUri.isEmpty()) {
30 qCDebug(KPEOPLE_DECLARATIVE_LOG()) << "AvatarImageProvider:"
31 << "passed person uri" << personUri << "was not valid utf8";
32 return {};
33 }
34
35 KPeople::PersonData person(personUri);
36 if (!person.isValid()) {
37 qCDebug(KPEOPLE_DECLARATIVE_LOG()) << "AvatarImageProvider:"
38 << "No contact found with person uri" << personUri;
39 return {};
40 }
41
42 const auto avatar = [&person]() -> QPixmap {
43 QVariant pic = person.contactCustomProperty(KPeople::AbstractContact::PictureProperty);
44 if (pic.canConvert<QImage>()) {
45 return QPixmap::fromImage(pic.value<QImage>());
46 } else if (pic.canConvert<QUrl>()) {
47 return QPixmap(pic.toUrl().toLocalFile());
48 }
49 return {};
50 }();
51
52 if (avatar.isNull()) {
53 return {};
54 }
55
56 if (size) {
57 *size = requestedSize;
58 }
59
60 return avatar.scaled(requestedSize);
61}
static const QString PictureProperty
QUrl or QPixmap property representing the contacts' avatar.
Allows to query the information about a given person.
Definition persondata.h:35
AbortOnBase64DecodingErrors
QByteArray fromBase64(const QByteArray &base64, Base64Options options)
QPixmap fromImage(QImage &&image, Qt::ImageConversionFlags flags)
QSize scaled(const QSize &s, Qt::AspectRatioMode mode) const const
QString fromUtf8(QByteArrayView str)
QList< QStringView > split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QString toLocalFile() const const
bool canConvert() const const
QUrl toUrl() const const
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:17:45 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.