summary refs log tree commit diff
path: root/include
diff options
context:
space:
mode:
authorKonstantinos Sideris <sideris.konstantin@gmail.com>2018-03-25 15:59:47 +0300
committerKonstantinos Sideris <sideris.konstantin@gmail.com>2018-03-25 15:59:47 +0300
commit72d5d6d2863a4e7c56089bb966ae9181e22e1b91 (patch)
treeeccdf668c865bd95edc7c71349f153f6b1a1c99c /include
parentLink with pthread on Linux (diff)
downloadnheko-72d5d6d2863a4e7c56089bb966ae9181e22e1b91.tar.xz
Show avatars in the completion popup
Diffstat (limited to 'include')
-rw-r--r--include/AvatarProvider.h16
-rw-r--r--include/MatrixClient.h9
-rw-r--r--include/timeline/TimelineItem.h6
3 files changed, 20 insertions, 11 deletions
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();