diff --git a/include/AvatarProvider.h b/include/AvatarProvider.h
index f3441290..95a5765d 100644
--- a/include/AvatarProvider.h
+++ b/include/AvatarProvider.h
@@ -25,9 +25,12 @@
class MatrixClient;
class TimelineItem;
+//! Saved cache data per user.
struct AvatarData
{
+ //! The avatar image of the user.
QImage img;
+ //! The url that was used to download the avatar.
QUrl url;
};
@@ -37,17 +40,22 @@ class AvatarProvider : public QObject
public:
static void init(QSharedPointer<MatrixClient> client);
- static void resolve(const QString &userId, std::function<void(QImage)> callback);
+ //! The callback is called with the downloaded avatar for the given user
+ //! or the avatar is downloaded first and then saved for re-use.
+ static void resolve(const QString &userId,
+ QObject *receiver,
+ std::function<void(QImage)> callback);
+ //! Used to initialize the mapping user -> avatar url.
static void setAvatarUrl(const QString &userId, const QUrl &url);
-
- static void clear();
+ //! Remove all saved data.
+ static void clear() { avatars_.clear(); };
private:
+ //! Update the cache with the downloaded avatar.
static void updateAvatar(const QString &uid, const QImage &img);
static QSharedPointer<MatrixClient> client_;
using UserID = QString;
static std::map<UserID, AvatarData> avatars_;
- static std::map<UserID, std::vector<std::function<void(QImage)>>> toBeResolved_;
};
diff --git a/include/MatrixClient.h b/include/MatrixClient.h
index 3052a118..0e15dc2d 100644
--- a/include/MatrixClient.h
+++ b/include/MatrixClient.h
@@ -29,6 +29,7 @@ class DownloadMediaProxy : public QObject
signals:
void imageDownloaded(const QPixmap &data);
void fileDownloaded(const QByteArray &data);
+ void avatarDownloaded(const QImage &img);
};
/*
@@ -59,14 +60,12 @@ public:
void versions() noexcept;
void fetchRoomAvatar(const QString &roomid, const QUrl &avatar_url);
//! Download user's avatar.
- void fetchUserAvatar(const QUrl &avatarUrl,
- std::function<void(QImage)> onSuccess,
- std::function<void(QString)> onError);
+ QSharedPointer<DownloadMediaProxy> fetchUserAvatar(const QUrl &avatarUrl);
void fetchCommunityAvatar(const QString &communityId, const QUrl &avatarUrl);
void fetchCommunityProfile(const QString &communityId);
void fetchCommunityRooms(const QString &communityId);
- DownloadMediaProxy *downloadImage(const QUrl &url);
- DownloadMediaProxy *downloadFile(const QUrl &url);
+ QSharedPointer<DownloadMediaProxy> downloadImage(const QUrl &url);
+ QSharedPointer<DownloadMediaProxy> downloadFile(const QUrl &url);
void messages(const QString &room_id, const QString &from_token, int limit = 30) noexcept;
void uploadImage(const QString &roomid,
const QString &filename,
diff --git a/include/timeline/TimelineItem.h b/include/timeline/TimelineItem.h
index ade2f834..b7a5623f 100644
--- a/include/timeline/TimelineItem.h
+++ b/include/timeline/TimelineItem.h
@@ -182,7 +182,8 @@ TimelineItem::setupLocalWidgetLayout(Widget *widget,
headerLayout_->addLayout(widgetLayout_);
messageLayout_->addLayout(headerLayout_, 1);
- AvatarProvider::resolve(userid, [this](const QImage &img) { setUserAvatar(img); });
+ AvatarProvider::resolve(
+ userid, this, [this](const QImage &img) { setUserAvatar(img); });
} else {
setupSimpleLayout();
@@ -230,7 +231,8 @@ TimelineItem::setupWidgetLayout(Widget *widget,
headerLayout_->addLayout(widgetLayout_);
messageLayout_->addLayout(headerLayout_, 1);
- AvatarProvider::resolve(sender, [this](const QImage &img) { setUserAvatar(img); });
+ AvatarProvider::resolve(
+ sender, this, [this](const QImage &img) { setUserAvatar(img); });
} else {
setupSimpleLayout();
|