KPeople

avatarimageprovider.cpp
1 /*
2  SPDX-FileCopyrightText: 2022 Jonah BrĂ¼chert <[email protected]>
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 
14 AvatarImageProvider::AvatarImageProvider()
16 {
17 }
18 
19 QPixmap 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 }
QPixmap fromImage(const QImage &image, Qt::ImageConversionFlags flags)
QUrl toUrl() const const
QString fromUtf8(const char *str, int size)
AbortOnBase64DecodingErrors
Allows to query the information about a given person.
Definition: persondata.h:34
T value() const const
static const QString PictureProperty
QUrl or QPixmap property representing the contacts' avatar.
QByteArray fromBase64(const QByteArray &base64, QByteArray::Base64Options options)
QString toLocalFile() const const
bool canConvert(int targetTypeId) const const
QSize scaled(int width, int height, Qt::AspectRatioMode mode) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Tue Aug 16 2022 03:52:14 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.