diff options
author | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2018-01-12 23:47:08 +0900 |
---|---|---|
committer | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2018-01-13 14:50:26 +0900 |
commit | 8f4a940d70cdcfc34a2ffe2d9a9561c0d821c56e (patch) | |
tree | 20d27d9eaeaef724e0de07fc85c9611614a63472 | |
parent | 1902ab6d923d2e3e475e5559ff409b68b3c66f67 (diff) | |
download | libquotient-8f4a940d70cdcfc34a2ffe2d9a9561c0d821c56e.tar.gz libquotient-8f4a940d70cdcfc34a2ffe2d9a9561c0d821c56e.zip |
User: Q_PROPERTYs; setAvatar()
-rw-r--r-- | user.cpp | 46 | ||||
-rw-r--r-- | user.h | 25 |
2 files changed, 57 insertions, 14 deletions
@@ -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()); } } @@ -29,6 +29,10 @@ namespace QMatrixClient class User: public QObject { Q_OBJECT + Q_PROPERTY(QString id READ id CONSTANT) + Q_PROPERTY(QString name READ name NOTIFY nameChanged) + Q_PROPERTY(QString displayName READ displayname NOTIFY nameChanged STORED false) + Q_PROPERTY(QString bridgeName READ bridged NOTIFY nameChanged STORED false) public: User(QString userId, Connection* connection); ~User() override; @@ -36,40 +40,43 @@ namespace QMatrixClient /** * Returns the id of the user */ - Q_INVOKABLE QString id() const; + QString id() const; /** * Returns the name chosen by the user */ - Q_INVOKABLE QString name() const; + QString name() const; /** * Returns the name that should be used to display the user. */ - Q_INVOKABLE QString displayname() const; + QString displayname() const; /** * Returns the name of bridge the user is connected from or empty. */ - Q_INVOKABLE QString bridged() const; + QString bridged() const; - Avatar& avatarObject(); - QImage avatar(int dimension); - QImage avatar(int requestedWidth, int requestedHeight); + const Avatar& avatarObject(); + Q_INVOKABLE QImage avatar(int dimension); + Q_INVOKABLE QImage avatar(int requestedWidth, int requestedHeight); - QUrl avatarUrl() const; + Q_INVOKABLE QUrl avatarUrl() const; void processEvent(Event* event); public slots: void rename(const QString& newName); + bool setAvatar(const QString& fileName); + bool setAvatar(QIODevice* source); signals: - void nameChanged(User*, QString); + void nameChanged(QString newName, QString oldName); void avatarChanged(User* user); private slots: void updateName(const QString& newName); + void updateAvatarUrl(const QUrl& newUrl); private: class Private; |