Debian Codegraph @

src/AvatarProvider.cpp

raw symbols SCIP
1 // SPDX-FileCopyrightText: Nheko Contributors
2 //
3 // SPDX-License-Identifier: GPL-3.0-or-later
4
5 #include <QBuffer>
6 #include <QPixmapCache>
7 #include <QPointer>
8 #include <memory>
9
10 #include "AvatarProvider.h"
11 #include "Cache.h"
12 #include "MxcImageProvider.h"
13
14 static QPixmapCache avatar_cache;
15
16 namespace AvatarProvider {
17 void
18 resolve(QString avatarUrl, int size, QObject *receiver, AvatarCallback callback)
19 {
20 const auto cacheKey = QStringLiteral("%1_size_%2").arg(avatarUrl).arg(size);
21
22 QPixmap pixmap;
23 if (avatarUrl.isEmpty()) {
24 callback(pixmap);
25 return;
26 }
27
28 if (avatar_cache.find(cacheKey, &pixmap)) {
29 callback(pixmap);
30 return;
31 }
32
33 MxcImageProvider::download(avatarUrl.remove(QStringLiteral("mxc://")),
34 QSize(size, size),
35 [callback, cacheKey, recv = QPointer<QObject>(receiver)](
36 QString, QSize, QImage img, QString) {
37 if (!recv)
38 return;
39
40 auto proxy = std::make_shared<AvatarProxy>();
41 QObject::connect(proxy.get(),
42 &AvatarProxy::avatarDownloaded,
43 recv,
44 [callback, cacheKey](QPixmap pm) {
45 if (!pm.isNull())
46 avatar_cache.insert(cacheKey, pm);
47 callback(pm);
48 });
49
50 if (img.isNull()) {
51 emit proxy->avatarDownloaded(QPixmap{});
52 return;
53 }
54
55 auto pm = QPixmap::fromImage(std::move(img));
56 emit proxy->avatarDownloaded(pm);
57 });
58 }
59
60 void
61 resolve(const QString &room_id,
62 const QString &user_id,
63 int size,
64 QObject *receiver,
65 AvatarCallback callback)
66 {
67 auto avatarUrl = cache::avatarUrl(room_id, user_id);
68
69 resolve(std::move(avatarUrl), size, receiver, callback);
70 }
71 }
72
73 #include "moc_AvatarProvider.cpp"