File indexing completed on 2024-05-05 16:16:34
0001 /* 0002 SPDX-FileCopyrightText: 2022 Jonah BrĂ¼chert <jbb@kaidan.im> 0003 0004 SPDX-License-Identifier: LGPL-2.1-or-later 0005 */ 0006 0007 #include "avatarimageprovider.h" 0008 0009 #include <abstractcontact.h> 0010 #include <persondata.h> 0011 0012 #include "kpeopledeclarative_debug.h" 0013 0014 AvatarImageProvider::AvatarImageProvider() 0015 : QQuickImageProvider(QQuickImageProvider::Pixmap) 0016 { 0017 } 0018 0019 QPixmap AvatarImageProvider::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize) 0020 { 0021 const auto base64encoded = QStringView(id).split(u'#').constFirst(); 0022 const auto decoded = QByteArray::fromBase64(base64encoded.toUtf8(), QByteArray::AbortOnBase64DecodingErrors); 0023 if (decoded.isEmpty()) { 0024 qCDebug(KPEOPLE_DECLARATIVE_LOG) << "AvatarImageProvider:" << id << "could not be decoded as a person uri"; 0025 return {}; 0026 } 0027 0028 auto personUri = QString::fromUtf8(decoded); 0029 if (personUri.isEmpty()) { 0030 qCDebug(KPEOPLE_DECLARATIVE_LOG()) << "AvatarImageProvider:" 0031 << "passed person uri" << personUri << "was not valid utf8"; 0032 return {}; 0033 } 0034 0035 KPeople::PersonData person(personUri); 0036 if (!person.isValid()) { 0037 qCDebug(KPEOPLE_DECLARATIVE_LOG()) << "AvatarImageProvider:" 0038 << "No contact found with person uri" << personUri; 0039 return {}; 0040 } 0041 0042 const auto avatar = [&person]() -> QPixmap { 0043 QVariant pic = person.contactCustomProperty(KPeople::AbstractContact::PictureProperty); 0044 if (pic.canConvert<QImage>()) { 0045 return QPixmap::fromImage(pic.value<QImage>()); 0046 } else if (pic.canConvert<QUrl>()) { 0047 return QPixmap(pic.toUrl().toLocalFile()); 0048 } 0049 return {}; 0050 }(); 0051 0052 if (avatar.isNull()) { 0053 return {}; 0054 } 0055 0056 if (size) { 0057 *size = requestedSize; 0058 } 0059 0060 return avatar.scaled(requestedSize); 0061 }