diff --git a/include/ChatPage.h b/include/ChatPage.h
index 0eae3838..3da84b33 100644
--- a/include/ChatPage.h
+++ b/include/ChatPage.h
@@ -83,7 +83,6 @@ private slots:
void updateTopBarAvatar(const QString &roomid, const QPixmap &img);
void updateOwnProfileInfo(const QUrl &avatar_url, const QString &display_name);
void updateOwnCommunitiesInfo(const QList<QString> &own_communities);
- void setOwnAvatar(const QPixmap &img);
void initialSyncCompleted(const mtx::responses::Sync &response);
void syncCompleted(const mtx::responses::Sync &response);
void changeTopRoomInfo(const QString &room_id);
diff --git a/include/Community.h b/include/Community.h
index 0d70dee1..5f3adba0 100644
--- a/include/Community.h
+++ b/include/Community.h
@@ -19,9 +19,6 @@ public:
inline QString getLongDescription() const;
inline const QList<QString> getRoomList() const;
-signals:
- void roomsChanged(QList<QString> &rooms);
-
private:
QUrl avatar_;
QString name_;
diff --git a/include/MatrixClient.h b/include/MatrixClient.h
index 6847ab22..d3eebe49 100644
--- a/include/MatrixClient.h
+++ b/include/MatrixClient.h
@@ -47,11 +47,13 @@ public:
const QString &server) noexcept;
void versions() noexcept;
void fetchRoomAvatar(const QString &roomid, const QUrl &avatar_url);
- void fetchUserAvatar(const QString &userId, const QUrl &avatarUrl);
+ //! Download user's avatar.
+ void fetchUserAvatar(const QUrl &avatarUrl,
+ std::function<void(QImage)> onSuccess,
+ std::function<void(QString)> onError);
void fetchCommunityAvatar(const QString &communityId, const QUrl &avatarUrl);
void fetchCommunityProfile(const QString &communityId);
void fetchCommunityRooms(const QString &communityId);
- void fetchOwnAvatar(const QUrl &avatar_url);
void downloadImage(const QString &event_id, const QUrl &url);
void downloadFile(const QString &event_id, const QUrl &url);
void messages(const QString &room_id, const QString &from_token, int limit = 30) noexcept;
@@ -120,7 +122,6 @@ signals:
void communityAvatarRetrieved(const QString &communityId, const QPixmap &img);
void communityProfileRetrieved(const QString &communityId, const QJsonObject &profile);
void communityRoomsRetrieved(const QString &communityId, const QJsonObject &rooms);
- void ownAvatarRetrieved(const QPixmap &img);
void imageDownloaded(const QString &event_id, const QPixmap &img);
void fileDownloaded(const QString &event_id, const QByteArray &data);
diff --git a/src/AvatarProvider.cc b/src/AvatarProvider.cc
index b8ea9e20..881ef2d5 100644
--- a/src/AvatarProvider.cc
+++ b/src/AvatarProvider.cc
@@ -27,8 +27,6 @@ void
AvatarProvider::init(QSharedPointer<MatrixClient> client)
{
client_ = client;
-
- connect(client_.data(), &MatrixClient::userAvatarRetrieved, &AvatarProvider::updateAvatar);
}
void
@@ -65,7 +63,13 @@ AvatarProvider::resolve(const QString &userId, std::function<void(QImage)> callb
// Add the current timeline item to the waiting list for this avatar.
if (!toBeResolved_.contains(userId)) {
- client_->fetchUserAvatar(userId, avatars_[userId].url);
+ client_->fetchUserAvatar(avatars_[userId].url,
+ [userId](QImage image) { updateAvatar(userId, image); },
+ [userId](QString error) {
+ qWarning()
+ << error << ": failed to retrieve user avatar"
+ << userId;
+ });
QList<std::function<void(QImage)>> items;
items.push_back(callback);
diff --git a/src/ChatPage.cc b/src/ChatPage.cc
index 71552bcf..596f72dc 100644
--- a/src/ChatPage.cc
+++ b/src/ChatPage.cc
@@ -349,7 +349,6 @@ ChatPage::ChatPage(QSharedPointer<MatrixClient> client,
}
});
- connect(client_.data(), &MatrixClient::ownAvatarRetrieved, this, &ChatPage::setOwnAvatar);
connect(client_.data(), &MatrixClient::joinedRoom, this, [=](const QString &room_id) {
emit showNotification("You joined the room.");
removeInvite(room_id);
@@ -494,12 +493,6 @@ ChatPage::bootstrap(QString userid, QString homeserver, QString token)
}
void
-ChatPage::setOwnAvatar(const QPixmap &img)
-{
- user_info_widget_->setAvatar(img.toImage());
-}
-
-void
ChatPage::syncCompleted(const mtx::responses::Sync &response)
{
syncTimeoutTimer_->stop();
@@ -597,7 +590,10 @@ ChatPage::updateOwnProfileInfo(const QUrl &avatar_url, const QString &display_na
user_info_widget_->setDisplayName(display_name);
if (avatar_url.isValid())
- client_->fetchOwnAvatar(avatar_url);
+ client_->fetchUserAvatar(
+ avatar_url,
+ [=](QImage img) { user_info_widget_->setAvatar(img); },
+ [=](QString error) { qWarning() << error << ": failed to fetch own avatar"; });
}
void
diff --git a/src/Community.cc b/src/Community.cc
index df425e88..a68f2b18 100644
--- a/src/Community.cc
+++ b/src/Community.cc
@@ -39,6 +39,4 @@ Community::parseRooms(const QJsonObject &rooms)
for (auto i = 0; i < rooms["chunk"].toArray().size(); i++) {
rooms_.append(rooms["chunk"].toArray()[i].toObject()["room_id"].toString());
}
-
- emit roomsChanged(rooms_);
}
diff --git a/src/MatrixClient.cc b/src/MatrixClient.cc
index fdc675b4..dd7c1c88 100644
--- a/src/MatrixClient.cc
+++ b/src/MatrixClient.cc
@@ -681,7 +681,9 @@ MatrixClient::fetchCommunityRooms(const QString &communityId)
}
void
-MatrixClient::fetchUserAvatar(const QString &userId, const QUrl &avatarUrl)
+MatrixClient::fetchUserAvatar(const QUrl &avatarUrl,
+ std::function<void(QImage)> onSuccess,
+ std::function<void(QString)> onError)
{
QList<QString> url_parts = avatarUrl.toString().split("mxc://");
@@ -704,25 +706,23 @@ MatrixClient::fetchUserAvatar(const QString &userId, const QUrl &avatarUrl)
QNetworkRequest avatar_request(endpoint);
auto reply = get(avatar_request);
- connect(reply, &QNetworkReply::finished, this, [this, reply, userId]() {
+ connect(reply, &QNetworkReply::finished, this, [this, reply, onSuccess, onError]() {
reply->deleteLater();
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
- if (status == 0 || status >= 400) {
- qWarning() << reply->errorString();
- return;
- }
+ if (status == 0 || status >= 400)
+ return onError(reply->errorString());
auto data = reply->readAll();
if (data.size() == 0)
- return;
+ return onError("received avatar with no data");
QImage img;
img.loadFromData(data);
- emit userAvatarRetrieved(userId, img);
+ onSuccess(std::move(img));
});
}
@@ -781,52 +781,6 @@ MatrixClient::downloadFile(const QString &event_id, const QUrl &url)
}
void
-MatrixClient::fetchOwnAvatar(const QUrl &avatar_url)
-{
- QList<QString> url_parts = avatar_url.toString().split("mxc://");
-
- if (url_parts.size() != 2) {
- qDebug() << "Invalid format for media " << avatar_url.toString();
- return;
- }
-
- QUrlQuery query;
- query.addQueryItem("width", "512");
- query.addQueryItem("height", "512");
- query.addQueryItem("method", "crop");
-
- QString media_url =
- QString("%1/_matrix/media/r0/thumbnail/%2").arg(getHomeServer().toString(), url_parts[1]);
-
- QUrl endpoint(media_url);
- endpoint.setQuery(query);
-
- QNetworkRequest avatar_request(endpoint);
-
- auto reply = get(avatar_request);
- connect(reply, &QNetworkReply::finished, this, [this, reply]() {
- reply->deleteLater();
-
- int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
-
- if (status == 0 || status >= 400) {
- qWarning() << reply->errorString();
- return;
- }
-
- auto img = reply->readAll();
-
- if (img.size() == 0)
- return;
-
- QPixmap pixmap;
- pixmap.loadFromData(img);
-
- emit ownAvatarRetrieved(pixmap);
- });
-}
-
-void
MatrixClient::messages(const QString &roomid, const QString &from_token, int limit) noexcept
{
QUrlQuery query;
|