From eec3865e536a6c4f95f917eefc2e94c8d0c158b1 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Fri, 12 Jan 2018 23:47:08 +0900 Subject: User: Q_PROPERTYs; setAvatar() --- user.cpp | 46 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) (limited to 'user.cpp') 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 #include +#include using namespace QMatrixClient; @@ -42,6 +44,9 @@ class User::Private QString bridged; Connection* connection; Avatar avatar; + QPointer 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(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( + 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()); } } -- cgit v1.2.3