Add avatar support.
This commit is contained in:
@@ -3,10 +3,7 @@
|
||||
#include "libqmatrixclient/connection.h"
|
||||
|
||||
Controller::Controller(QObject *parent) : QObject(parent) {
|
||||
connect(m_connection, &QMatrixClient::Connection::connected, this, &Controller::connected);
|
||||
connect(m_connection, &QMatrixClient::Connection::resolveError, this, &Controller::reconnect);
|
||||
connect(m_connection, &QMatrixClient::Connection::syncError, this, &Controller::reconnect);
|
||||
connect(m_connection, &QMatrixClient::Connection::syncDone, this, &Controller::resync);
|
||||
|
||||
}
|
||||
|
||||
Controller::~Controller() {
|
||||
@@ -31,6 +28,18 @@ void Controller::login(QString home, QString user, QString pass) {
|
||||
}
|
||||
}
|
||||
|
||||
void Controller::setConnection(QMatrixClient::Connection* conn) {
|
||||
qDebug() << "Setting controller connection.";
|
||||
m_connection = conn;
|
||||
roomListModel = new RoomListModel(m_connection);
|
||||
emit roomListModelChanged();
|
||||
connect(m_connection, &QMatrixClient::Connection::connected, this, &Controller::connected);
|
||||
connect(m_connection, &QMatrixClient::Connection::resolveError, this, &Controller::reconnect);
|
||||
connect(m_connection, &QMatrixClient::Connection::syncError, this, &Controller::reconnect);
|
||||
connect(m_connection, &QMatrixClient::Connection::syncDone, this, &Controller::resync);
|
||||
emit connectionChanged();
|
||||
}
|
||||
|
||||
void Controller::logout() {
|
||||
userID = "";
|
||||
token = "";
|
||||
|
||||
@@ -15,13 +15,11 @@ class Controller : public QObject
|
||||
Q_OBJECT
|
||||
|
||||
Q_PROPERTY(RoomListModel *roomListModel READ getRoomListModel NOTIFY roomListModelChanged)
|
||||
Q_PROPERTY(QMatrixClient::Connection *connection READ getConnection WRITE setConnection NOTIFY connectionChanged)
|
||||
Q_PROPERTY(bool isLogin READ getIsLogin WRITE setIsLogin NOTIFY isLoginChanged)
|
||||
Q_PROPERTY(QString userID READ getUserID WRITE setUserID NOTIFY userIDChanged)
|
||||
Q_PROPERTY(QByteArray token READ getToken WRITE setToken NOTIFY tokenChanged)
|
||||
|
||||
private:
|
||||
QMatrixClient::Connection* m_connection = new QMatrixClient::Connection();
|
||||
|
||||
public:
|
||||
explicit Controller(QObject *parent = nullptr);
|
||||
~Controller();
|
||||
@@ -33,9 +31,13 @@ public:
|
||||
// All the non-Q_INVOKABLE functions.
|
||||
|
||||
// All the Q_PROPERTYs.
|
||||
RoomListModel* roomListModel = new RoomListModel(m_connection);
|
||||
RoomListModel* roomListModel;
|
||||
RoomListModel* getRoomListModel() { return roomListModel; }
|
||||
|
||||
QMatrixClient::Connection* m_connection;
|
||||
QMatrixClient::Connection* getConnection() { return m_connection; }
|
||||
void setConnection(QMatrixClient::Connection* conn);
|
||||
|
||||
bool isLogin = false;
|
||||
bool getIsLogin() { return isLogin; }
|
||||
void setIsLogin(bool n) {
|
||||
@@ -70,6 +72,7 @@ private:
|
||||
|
||||
signals:
|
||||
void roomListModelChanged();
|
||||
void connectionChanged();
|
||||
void isLoginChanged();
|
||||
void userIDChanged();
|
||||
void tokenChanged();
|
||||
|
||||
71
matrix/imageprovider.cpp
Normal file
71
matrix/imageprovider.cpp
Normal file
@@ -0,0 +1,71 @@
|
||||
#include "imageprovider.h"
|
||||
|
||||
#include "connection.h"
|
||||
#include "jobs/mediathumbnailjob.h"
|
||||
|
||||
#include <QtCore/QWaitCondition>
|
||||
#include <QtCore/QDebug>
|
||||
#include <QMetaObject>
|
||||
|
||||
using QMatrixClient::MediaThumbnailJob;
|
||||
|
||||
ImageProvider::ImageProvider(QObject *parent)
|
||||
: QQuickImageProvider(QQmlImageProviderBase::Image,
|
||||
QQmlImageProviderBase::ForceAsynchronousImageLoading)
|
||||
{
|
||||
#if (QT_VERSION < QT_VERSION_CHECK(5, 10, 0))
|
||||
qRegisterMetaType<MediaThumbnailJob*>();
|
||||
#endif
|
||||
}
|
||||
|
||||
QImage ImageProvider::requestImage(const QString& id,
|
||||
QSize* pSize, const QSize& requestedSize)
|
||||
{
|
||||
if (!id.startsWith("mxc://"))
|
||||
{
|
||||
qWarning() << "ImageProvider: won't fetch an invalid id:" << id
|
||||
<< "doesn't follow server/mediaId pattern";
|
||||
return {};
|
||||
}
|
||||
|
||||
QUrl mxcUri { id };
|
||||
qDebug() << "ImageProvider::requestImage:" << mxcUri.toString();
|
||||
|
||||
MediaThumbnailJob* job = nullptr;
|
||||
QReadLocker locker(&m_lock);
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
|
||||
QMetaObject::invokeMethod(m_connection,
|
||||
[=] { return m_connection->getThumbnail(mxcUri, requestedSize); },
|
||||
Qt::BlockingQueuedConnection, &job);
|
||||
#else
|
||||
QMetaObject::invokeMethod(m_connection, "getThumbnail",
|
||||
Qt::BlockingQueuedConnection, Q_RETURN_ARG(MediaThumbnailJob*, job),
|
||||
Q_ARG(QUrl, mxcUri), Q_ARG(QSize, requestedSize));
|
||||
#endif
|
||||
if (!job)
|
||||
{
|
||||
qDebug() << "ImageProvider: failed to send a request";
|
||||
return {};
|
||||
}
|
||||
QImage result;
|
||||
{
|
||||
QWaitCondition condition; // The most compact way to block on a signal
|
||||
job->connect(job, &MediaThumbnailJob::finished, job, [&] {
|
||||
result = job->thumbnail();
|
||||
condition.wakeAll();
|
||||
});
|
||||
condition.wait(&m_lock);
|
||||
}
|
||||
|
||||
if( pSize != nullptr )
|
||||
*pSize = result.size();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void ImageProvider::setConnection(QMatrixClient::Connection* connection)
|
||||
{
|
||||
QWriteLocker locker(&m_lock);
|
||||
|
||||
m_connection = connection;
|
||||
}
|
||||
28
matrix/imageprovider.h
Normal file
28
matrix/imageprovider.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#ifndef IMAGEPROVIDER_H
|
||||
#define IMAGEPROVIDER_H
|
||||
|
||||
#include <QtQuick/QQuickImageProvider>
|
||||
#include <QtCore/QReadWriteLock>
|
||||
|
||||
namespace QMatrixClient {
|
||||
class Connection;
|
||||
}
|
||||
|
||||
class ImageProvider: public QQuickImageProvider
|
||||
{
|
||||
public:
|
||||
explicit ImageProvider(QObject *parent = nullptr);
|
||||
|
||||
QImage requestImage(const QString& id, QSize* pSize,
|
||||
const QSize& requestedSize) override;
|
||||
|
||||
void setConnection(QMatrixClient::Connection* connection);
|
||||
|
||||
void initializeEngine(QQmlEngine *engine, const char *uri);
|
||||
|
||||
private:
|
||||
QMatrixClient::Connection* m_connection;
|
||||
QReadWriteLock m_lock;
|
||||
};
|
||||
|
||||
#endif // IMAGEPROVIDER_H
|
||||
@@ -53,8 +53,8 @@ QVariant RoomListModel::data(const QModelIndex& index, int role) const {
|
||||
if(role == ValueRole) {
|
||||
return room->topic();
|
||||
}
|
||||
if(role == IconRole) {
|
||||
return room->avatar(48);
|
||||
if(role == AvatarRole) {
|
||||
return room->avatarUrl();
|
||||
}
|
||||
return QVariant();
|
||||
}
|
||||
@@ -63,7 +63,7 @@ QHash<int, QByteArray> RoomListModel::roleNames() const {
|
||||
QHash<int, QByteArray> roles;
|
||||
roles[NameRole] = "name";
|
||||
roles[ValueRole] = "value";
|
||||
roles[IconRole] = "icon";
|
||||
roles[AvatarRole] = "avatar";
|
||||
return roles;
|
||||
}
|
||||
|
||||
@@ -75,8 +75,3 @@ void RoomListModel::namesChanged(QMatrixClient::Room* room) {
|
||||
void RoomListModel::unreadMessagesChanged(QMatrixClient::Room* room) {
|
||||
|
||||
}
|
||||
|
||||
RoomModel::RoomModel(QString name, QString value) {
|
||||
m_name = name;
|
||||
m_value = value;
|
||||
}
|
||||
|
||||
@@ -12,28 +12,6 @@ namespace QMatrixClient {
|
||||
class Room;
|
||||
}
|
||||
|
||||
class RoomModel : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
Q_PROPERTY(QString name READ getName)
|
||||
Q_PROPERTY(QString value READ getValue)
|
||||
|
||||
public:
|
||||
explicit RoomModel(QString name, QString value);
|
||||
|
||||
QString getName() { return m_name; }
|
||||
QString getValue() { return m_value; }
|
||||
|
||||
signals:
|
||||
void nameChanged();
|
||||
void valueChanged();
|
||||
|
||||
private:
|
||||
QString m_name;
|
||||
QString m_value;
|
||||
};
|
||||
|
||||
class RoomListModel : public QAbstractListModel
|
||||
{
|
||||
Q_OBJECT
|
||||
@@ -43,7 +21,7 @@ public:
|
||||
~RoomListModel();
|
||||
|
||||
enum RoomModelRoles {
|
||||
NameRole, ValueRole, IconRole
|
||||
NameRole, ValueRole, AvatarRole
|
||||
};
|
||||
|
||||
QHash<int, QByteArray> roleNames() const;
|
||||
|
||||
Reference in New Issue
Block a user