aboutsummaryrefslogtreecommitdiff
path: root/user.cpp
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2017-11-28 12:42:03 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2017-11-28 12:42:03 +0900
commit357625eb55e2f4569bb487ffe14a9236188e25f3 (patch)
treed39340ab74f25a23a5855f679973628f7457fd87 /user.cpp
parent94e6636d8225a0561ed7df3fa8081c5b0183610c (diff)
parent8f762a2458db773f6db24b568b2e944427297c2b (diff)
downloadlibquotient-357625eb55e2f4569bb487ffe14a9236188e25f3.tar.gz
libquotient-357625eb55e2f4569bb487ffe14a9236188e25f3.zip
Merge branch 'master' into kitsune-gtad
Diffstat (limited to 'user.cpp')
-rw-r--r--user.cpp102
1 files changed, 19 insertions, 83 deletions
diff --git a/user.cpp b/user.cpp
index aa1aa447..faad6231 100644
--- a/user.cpp
+++ b/user.cpp
@@ -19,14 +19,12 @@
#include "user.h"
#include "connection.h"
+#include "avatar.h"
#include "events/event.h"
#include "events/roommemberevent.h"
-#include "jobs/mediathumbnailjob.h"
#include "jobs/generated/profile.h"
#include <QtCore/QTimer>
-#include <QtCore/QDebug>
-#include <QtGui/QIcon>
#include <QtCore/QRegularExpression>
using namespace QMatrixClient;
@@ -35,35 +33,20 @@ class User::Private
{
public:
Private(QString userId, Connection* connection)
- : q(nullptr), userId(std::move(userId)), connection(connection)
- , defaultIcon(QIcon::fromTheme(QStringLiteral("user-available")))
- , avatarValid(false) , avatarOngoingRequest(false)
+ : userId(std::move(userId)), connection(connection)
+ , avatar(connection, QIcon::fromTheme(QStringLiteral("user-available")))
{ }
- User* q;
QString userId;
QString name;
- QUrl avatarUrl;
- Connection* connection;
-
- QPixmap avatar;
- QIcon defaultIcon;
- QSize requestedSize;
- bool avatarValid;
- bool avatarOngoingRequest;
- /// Map of requested size to the actual pixmap used for it
- /// (it's a shame that QSize has no predefined qHash()).
- QHash<QPair<int,int>, QPixmap> scaledAvatars;
QString bridged;
-
- void requestAvatar();
+ Connection* connection;
+ Avatar avatar;
};
User::User(QString userId, Connection* connection)
- : QObject(connection), d(new Private(userId, connection))
-{
- d->q = this; // Initialization finished
-}
+ : QObject(connection), d(new Private(std::move(userId), connection))
+{ }
User::~User()
{
@@ -107,45 +90,19 @@ QString User::bridged() const {
return d->bridged;
}
-QPixmap User::avatar(int width, int height)
+Avatar& User::avatarObject()
{
- QSize size(width, height);
-
- // FIXME: Alternating between longer-width and longer-height requests
- // is a sure way to trick the below code into constantly getting another
- // image from the server because the existing one is alleged unsatisfactory.
- // This is plain abuse by the client, though; so not critical for now.
- if( (!d->avatarValid && d->avatarUrl.isValid() && !d->avatarOngoingRequest)
- || width > d->requestedSize.width()
- || height > d->requestedSize.height() )
- {
- qCDebug(MAIN) << "Getting avatar for" << id()
- << "from" << d->avatarUrl.toString();
- d->requestedSize = size;
- d->avatarOngoingRequest = true;
- QTimer::singleShot(0, this, SLOT(requestAvatar()));
- }
-
- if( d->avatar.isNull() )
- {
- if (d->defaultIcon.isNull())
- return d->avatar;
-
- d->avatar = d->defaultIcon.pixmap(size);
- }
+ return d->avatar;
+}
- auto& pixmap = d->scaledAvatars[{width, height}]; // Create the entry if needed
- if (pixmap.isNull())
- {
- pixmap = d->avatar.scaled(width, height,
- Qt::KeepAspectRatio, Qt::SmoothTransformation);
- }
- return pixmap;
+QPixmap User::avatar(int width, int height)
+{
+ return d->avatar.get(width, height, [=] { emit avatarChanged(this); });
}
-const QUrl& User::avatarUrl() const
+QUrl User::avatarUrl() const
{
- return d->avatarUrl;
+ return d->avatar.url();
}
void User::processEvent(Event* event)
@@ -157,36 +114,15 @@ void User::processEvent(Event* event)
return;
auto newName = e->displayName();
- QRegularExpression reSuffix(" \\((IRC|Gitter)\\)$");
- auto match = reSuffix.match(d->name);
+ QRegularExpression reSuffix(" \\((IRC|Gitter|Telegram)\\)$");
+ auto match = reSuffix.match(newName);
if (match.hasMatch())
{
d->bridged = match.captured(1);
newName.truncate(match.capturedStart(0));
}
updateName(newName);
- if( d->avatarUrl != e->avatarUrl() )
- {
- d->avatarUrl = e->avatarUrl();
- d->avatarValid = false;
- }
+ if (d->avatar.updateUrl(e->avatarUrl()))
+ emit avatarChanged(this);
}
}
-
-void User::requestAvatar()
-{
- d->requestAvatar();
-}
-
-void User::Private::requestAvatar()
-{
- auto* job = connection->callApi<MediaThumbnailJob>(avatarUrl, requestedSize);
- connect( job, &MediaThumbnailJob::success, [=]() {
- avatarOngoingRequest = false;
- avatarValid = true;
- avatar = job->scaledThumbnail(requestedSize);
- scaledAvatars.clear();
- emit q->avatarChanged(q);
- });
-}
-