aboutsummaryrefslogtreecommitdiff
path: root/user.cpp
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2018-01-12 23:47:08 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2018-01-13 14:50:26 +0900
commit8f4a940d70cdcfc34a2ffe2d9a9561c0d821c56e (patch)
tree20d27d9eaeaef724e0de07fc85c9611614a63472 /user.cpp
parent1902ab6d923d2e3e475e5559ff409b68b3c66f67 (diff)
downloadlibquotient-8f4a940d70cdcfc34a2ffe2d9a9561c0d821c56e.tar.gz
libquotient-8f4a940d70cdcfc34a2ffe2d9a9561c0d821c56e.zip
User: Q_PROPERTYs; setAvatar()
Diffstat (limited to 'user.cpp')
-rw-r--r--user.cpp46
1 files changed, 41 insertions, 5 deletions
diff --git a/user.cpp b/user.cpp
index 6d2a2030..baa7bc45 100644
--- a/user.cpp
+++ b/user.cpp
@@ -23,9 +23,11 @@
#include "events/event.h"
#include "events/roommemberevent.h"
#include "jobs/generated/profile.h"
+#include "jobs/generated/content-repo.h"
#include <QtCore/QTimer>
#include <QtCore/QRegularExpression>
+#include <QtCore/QPointer>
using namespace QMatrixClient;
@@ -42,6 +44,9 @@ class User::Private
QString bridged;
Connection* connection;
Avatar avatar;
+ QPointer<UploadContentJob> avatarUploadJob = nullptr;
+
+ void setAvatar(UploadContentJob* job, User* q);
};
User::User(QString userId, Connection* connection)
@@ -66,19 +71,51 @@ QString User::name() const
void User::updateName(const QString& newName)
{
const auto oldName = name();
- if (d->name != newName)
+ if (oldName != newName)
{
d->name = newName;
- emit nameChanged(this, oldName);
+ emit nameChanged(newName, oldName);
}
}
+void User::updateAvatarUrl(const QUrl& newUrl)
+{
+ if (d->avatar.updateUrl(newUrl))
+ emit avatarChanged(this);
+}
+
void User::rename(const QString& newName)
{
auto job = d->connection->callApi<SetDisplayNameJob>(id(), newName);
connect(job, &BaseJob::success, this, [=] { updateName(newName); });
}
+bool User::setAvatar(const QString& fileName)
+{
+ if (isJobRunning(d->avatarUploadJob))
+ return false;
+ d->setAvatar(d->connection->uploadFile(fileName), this);
+ return true;
+}
+
+bool User::setAvatar(QIODevice* source)
+{
+ if (isJobRunning(d->avatarUploadJob) || !source->isReadable())
+ return false;
+ d->setAvatar(d->connection->uploadContent(source), this);
+ return true;
+}
+
+void User::Private::setAvatar(UploadContentJob* job, User* q)
+{
+ avatarUploadJob = job;
+ connect(job, &BaseJob::success, q, [this,q] {
+ auto* j = connection->callApi<SetAvatarUrlJob>(
+ userId, avatarUploadJob->contentUri());
+ connect(j, &BaseJob::success, q, [q] { emit q->avatarChanged(q); });
+ });
+}
+
QString User::displayname() const
{
if( !d->name.isEmpty() )
@@ -90,7 +127,7 @@ QString User::bridged() const {
return d->bridged;
}
-Avatar& User::avatarObject()
+const Avatar& User::avatarObject()
{
return d->avatar;
}
@@ -127,7 +164,6 @@ void User::processEvent(Event* event)
newName.truncate(match.capturedStart(0));
}
updateName(newName);
- if (d->avatar.updateUrl(e->avatarUrl()))
- emit avatarChanged(this);
+ updateAvatarUrl(e->avatarUrl());
}
}