Skip to content

Commit

Permalink
Add C++ Model for DeviceList
Browse files Browse the repository at this point in the history
  • Loading branch information
Chethan2k1 committed Jun 1, 2020
1 parent e25fc14 commit 208d89b
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 52 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ set(SRC_FILES
src/ui/Theme.cpp
src/ui/ThemeManager.cpp
src/ui/UserProfile.cpp
src/ui/UserProfileModel.cpp

src/AvatarProvider.cpp
src/BlurhashProvider.cpp
Expand Down Expand Up @@ -475,6 +476,7 @@ qt5_wrap_cpp(MOC_HEADERS
src/ui/Theme.h
src/ui/ThemeManager.h
src/ui/UserProfile.h
src/ui/UserProfileModel.h

src/notifications/Manager.h

Expand Down
1 change: 1 addition & 0 deletions resources/qml/TimelineView.qml
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ Page {

MouseArea {
anchors.fill: parent
Layout.alignment: Qt.AlignHCenter
onClicked: {
userProfile.user_data = modelData
userProfile.show()
Expand Down
46 changes: 30 additions & 16 deletions resources/qml/UserProfile.qml
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,17 @@ ApplicationWindow{
userProfileAvatar.url = chat.model.avatarUrl(user_data.userId).replace("mxc://", "image://MxcImage/")
userProfileName.text = user_data.userName
matrixUserID.text = user_data.userId
userProfile.userId = user_data.userId
log_devices()
userProfileList.userId = user_data.userId
}

function log_devices()
{
console.log(userProfile.deviceList);
userProfile.deviceList.forEach((item,index)=>{
console.log(item.device_id)
console.log(item.display_name)
})
}

UserProfileContent{
id: userProfile
UserProfileList{
id: userProfileList
onUserIdChanged : {
userProfileList.updateDeviceList()
}
onDeviceListUpdated : {
modelDeviceList.deviceList = userProfileList
}
}

background: Item{
Expand Down Expand Up @@ -78,9 +74,27 @@ ApplicationWindow{
ScrollBar.horizontal.policy: ScrollBar.AlwaysOn
ScrollBar.vertical.policy: ScrollBar.AlwaysOn

Label {
text: "ABC"
font.pixelSize: 700
ListView{
id: deviceList
anchors.fill: parent
clip: true
spacing: 10

model: UserProfileModel{
id: modelDeviceList
}

delegate: RowLayout{
width: parent.width
Text{
Layout.fillWidth: true
text: deviceID
}
Text{
Layout.fillWidth: true
text: displayName
}
}
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/Olm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ handle_to_device_messages(const std::vector<mtx::events::collections::DeviceEven
nhlog::crypto()->warn("validation error for olm message: {} {}",
e.what(),
j_msg.dump(2));

}

} else if (msg_type == to_string(mtx::events::EventType::RoomKeyRequest)) {
Expand Down Expand Up @@ -398,7 +397,6 @@ handle_key_request_message(const mtx::events::DeviceEvent<mtx::events::msg::KeyR
}

if (!utils::respondsToKeyRequests(req.content.room_id)) {

nhlog::crypto()->debug("ignoring all key requests for room {}",
req.content.room_id);

Expand Down
11 changes: 8 additions & 3 deletions src/timeline/TimelineViewManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <QMetaType>
#include <QPalette>
#include <QQmlContext>
#include <QQmlEngine>

#include "BlurhashProvider.h"
#include "ChatPage.h"
Expand All @@ -13,9 +14,11 @@
#include "MxcImageProvider.h"
#include "UserSettingsPage.h"
#include "dialogs/ImageOverlay.h"
#include "../ui/UserProfile.h"
#include "src/ui/UserProfile.h"
#include "src/ui/UserProfileModel.h"

Q_DECLARE_METATYPE(mtx::events::collections::TimelineEvents)
UserProfile deviceList;

void
TimelineViewManager::updateEncryptedDescriptions()
Expand Down Expand Up @@ -73,8 +76,9 @@ TimelineViewManager::TimelineViewManager(QSharedPointer<UserSettings> userSettin
qmlRegisterType<DelegateChoice>("im.nheko", 1, 0, "DelegateChoice");
qmlRegisterType<DelegateChooser>("im.nheko", 1, 0, "DelegateChooser");
qmlRegisterType<DeviceVerificationFlow>("im.nheko", 1, 0, "DeviceVerificationFlow");
qmlRegisterType<UserProfile>("im.nheko",1,0,"UserProfileContent");
qRegisterMetaType<DeviceInfo>();
qmlRegisterType<UserProfileModel>("im.nheko", 1, 0, "UserProfileModel");
qmlRegisterType<UserProfile>("im.nheko", 1, 0, "UserProfileList");

qRegisterMetaType<mtx::events::collections::TimelineEvents>();

#ifdef USE_QUICK_VIEW
Expand All @@ -97,6 +101,7 @@ TimelineViewManager::TimelineViewManager(QSharedPointer<UserSettings> userSettin
view->engine()->addImageProvider("colorimage", colorImgProvider);
view->engine()->addImageProvider("blurhash", blurhashProvider);
view->setSource(QUrl("qrc:///qml/TimelineView.qml"));
view->rootContext()->setContextProperty(QStringLiteral("devices"), &deviceList);

connect(dynamic_cast<ChatPage *>(parent),
&ChatPage::themeChanged,
Expand Down
49 changes: 30 additions & 19 deletions src/ui/UserProfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,34 @@
#include "Logging.h"
#include "Utils.h"
#include "mtx/responses/crypto.hpp"
#include <iostream>
#include <iostream> // added only for debugging

UserProfile::UserProfile(QObject *parent)
: QObject(parent)
{}

QVector<DeviceInfo>
UserProfile::getDeviceList(){
UserProfile::getDeviceList()
{
UserProfile::fetchDeviceList(this->userId);
return this->deviceList;
}

QString
UserProfile::getUserId (){
UserProfile::getUserId()
{
return this->userId;
}

void
UserProfile::setUserId (const QString &user_id){
if(this->userId != userId)
UserProfile::setUserId(const QString &user_id)
{
if (this->userId != userId)
return;
else
else {
emit UserProfile::userIdChanged();
this->userId = user_id;
}
}

void
Expand All @@ -34,14 +39,15 @@ UserProfile::fetchDeviceList(const QString &userID)
mtx::requests::QueryKeys req;
mtx::responses::QueryKeys res;
req.device_keys[userID.toStdString()] = {};

// to be removed
std::cout << json(req).dump(2) << std::endl;
http::client()->query_keys(
req,
[user_id = userID.toStdString(),this](const mtx::responses::QueryKeys &res,
mtx::http::RequestErr err) {
[user_id = userID.toStdString(), this](const mtx::responses::QueryKeys &res,
mtx::http::RequestErr err) {
if (err) {
nhlog::net()->warn("failed to query device keys: {} {}",
err->matrix_error.error,
nhlog::net()->warn("failed to query device keys: {},{}",
err->matrix_error.errcode,
static_cast<int>(err->status_code));
return;
}
Expand All @@ -53,17 +59,16 @@ UserProfile::fetchDeviceList(const QString &userID)
}

auto devices = res.device_keys.at(user_id);

QVector<DeviceInfo> deviceInfo;

for (const auto &d : devices) {
auto device = d.second;

// TODO: Verify signatures and ignore those that don't pass.
// std::cout<<d.first<<std::endl;
// std::cout<<device.unsigned_info.device_display_name<<std::endl;
DeviceInfo newdevice(QString::fromStdString(d.first),QString::fromStdString(device.unsigned_info.device_display_name))
newdevice->device_id = QString::fromStdString(d.first);
newdevice->display_name = QString::fromStdString(device.unsigned_info.device_display_name)
DeviceInfo newdevice(
QString::fromStdString(d.first),
QString::fromStdString(device.unsigned_info.device_display_name));
QString::fromStdString(device.unsigned_info.device_display_name);

deviceInfo.append(std::move(newdevice));
}
Expand All @@ -74,7 +79,13 @@ UserProfile::fetchDeviceList(const QString &userID)
return a.device_id > b.device_id;
});

this->deviceList = deviceInfo;
emit UserProfile::deviceListUpdated();
this->deviceList = std::move(deviceInfo);
emit UserProfile::deviceListUpdated();
});
}

void
UserProfile::updateDeviceList()
{
fetchDeviceList(this->userId);
}
22 changes: 10 additions & 12 deletions src/ui/UserProfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,29 @@

#include <QObject>
#include <QString>
#include <QVector>

#include "MatrixClient.h"

class DeviceInfo
{
public:
explicit DeviceInfo(QString device_id,QString display_name){
this->device_id = device_id;
this->display_name = display_name;
}
~DeviceInfo() = default;
DeviceInfo(const DeviceInfo &device){
this->device_id = device.device_id;
this->display_name = device.display_name;
}
DeviceInfo(const QString deviceID, const QString displayName)
: device_id(deviceID)
, display_name(displayName)
{}

DeviceInfo() {}

QString device_id;
QString display_name;
};
Q_DECLARE_METATYPE(DeviceInfo);

class UserProfile : public QObject
{
Q_OBJECT
Q_PROPERTY(QString userId READ getUserId WRITE setUserId NOTIFY userIdChanged)
Q_PROPERTY(QVector<DeviceInfo> deviceList READ getDeviceList NOTIFY deviceListUpdated)
Q_PROPERTY(QString userId READ getUserId WRITE setUserId)

public:
// constructor
explicit UserProfile(QObject *parent = 0);
Expand All @@ -39,8 +35,10 @@ class UserProfile : public QObject
void setUserId(const QString &userId);

Q_INVOKABLE void fetchDeviceList(const QString &userID);
Q_INVOKABLE void updateDeviceList();

signals:
void userIdChanged();
void deviceListUpdated();

private:
Expand Down
66 changes: 66 additions & 0 deletions src/ui/UserProfileModel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "UserProfileModel.h"
#include "UserProfile.h"
#include <QModelIndex>

UserProfileModel::UserProfileModel(QObject *parent)
: QAbstractListModel(parent)
, deviceList(nullptr)
{}

int
UserProfileModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid() || !this->deviceList)
return 0;
return this->deviceList->getDeviceList().size();
}

QVariant
UserProfileModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || !this->deviceList)
return QVariant();

const DeviceInfo device = this->deviceList->getDeviceList().at(index.row());
switch (role) {
case DEVICEID:
return QVariant(device.device_id);
case DISPLAYNAME:
return QVariant(device.display_name);
}
return QVariant();
}

QHash<int, QByteArray>
UserProfileModel::roleNames() const
{
QHash<int, QByteArray> names;
names[DEVICEID] = "deviceID";
names[DISPLAYNAME] = "displayName";
return names;
}

UserProfile *
UserProfileModel::getList() const
{
return (this->deviceList);
}

void
UserProfileModel::setList(UserProfile *devices)
{
beginResetModel();

if (devices)
devices->disconnect(this);

if (this->deviceList) {
const int index = this->deviceList->getDeviceList().size();
beginInsertRows(QModelIndex(), index, index);
endInsertRows();
}

this->deviceList = devices;

endResetModel();
}
29 changes: 29 additions & 0 deletions src/ui/UserProfileModel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <QAbstractListModel>

class UserProfile; // forward declaration of the class UserProfile

class UserProfileModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(UserProfile *deviceList READ getList WRITE setList)

public:
explicit UserProfileModel(QObject *parent = nullptr);

enum
{
DEVICEID,
DISPLAYNAME
};
UserProfile *getList() const;
void setList(UserProfile *devices);

int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role) const override;
virtual QHash<int, QByteArray> roleNames() const override;

private:
UserProfile *deviceList;
};

0 comments on commit 208d89b

Please sign in to comment.