File indexing completed on 2024-11-24 04:50:43

0001 // SPDX-FileCopyrightText: 2022 Carl Schwan <carl@carlschwan.eu>
0002 // SPDX-License-Identifier: GPL-3.0-only
0003 
0004 #pragma once
0005 
0006 #include <QQuickAsyncImageProvider>
0007 
0008 #include <KContacts/Addressee>
0009 #include <QNetworkAccessManager>
0010 #include <QReadWriteLock>
0011 
0012 namespace Akonadi
0013 {
0014 class ContactSearchJob;
0015 }
0016 class QDnsLookup;
0017 class QNetworkReply;
0018 
0019 class ThumbnailResponse : public QQuickImageResponse
0020 {
0021     Q_OBJECT
0022 public:
0023     ThumbnailResponse(QString mediaId, QSize requestedSize, QNetworkAccessManager *qnam);
0024     ~ThumbnailResponse() override = default;
0025 
0026 private Q_SLOTS:
0027     void startRequest();
0028     void prepareResult();
0029     void doCancel();
0030 
0031 private:
0032     bool searchPhoto(const KContacts::AddresseeList &list);
0033     void queryImage(const QString &hostame = QStringLiteral("https://seccdn.libravatar.org/avatar/"));
0034     void imageQueried(QNetworkReply *reply);
0035     void dnsLookupFinished(QDnsLookup *dns);
0036     QString m_email;
0037     QSize requestedSize;
0038     const QString localFile;
0039     QNetworkAccessManager *m_qnam;
0040 
0041     QImage m_image;
0042     KContacts::Picture m_photo;
0043     QString errorStr;
0044     Akonadi::ContactSearchJob *job = nullptr;
0045     mutable QReadWriteLock lock; // Guards ONLY these two members above
0046 
0047     QQuickTextureFactory *textureFactory() const override;
0048     QString errorString() const override;
0049     void cancel() override;
0050 };
0051 
0052 class ContactImageProvider : public QQuickAsyncImageProvider
0053 {
0054 public:
0055     explicit ContactImageProvider();
0056     QQuickImageResponse *requestImageResponse(const QString &id, const QSize &requestedSize) override;
0057 
0058 private:
0059     QNetworkAccessManager qnam;
0060 };