Minimum code to get multiple accounts working.
This commit is contained in:
parent
8fb16d0700
commit
2992804472
|
@ -29,7 +29,8 @@ SOURCES += src/main.cpp \
|
||||||
src/emojimodel.cpp \
|
src/emojimodel.cpp \
|
||||||
src/matriqueroom.cpp \
|
src/matriqueroom.cpp \
|
||||||
src/userlistmodel.cpp \
|
src/userlistmodel.cpp \
|
||||||
src/imageitem.cpp
|
src/imageitem.cpp \
|
||||||
|
src/accountlistmodel.cpp
|
||||||
|
|
||||||
RESOURCES += \
|
RESOURCES += \
|
||||||
res.qrc
|
res.qrc
|
||||||
|
@ -89,4 +90,5 @@ HEADERS += \
|
||||||
src/emojimodel.h \
|
src/emojimodel.h \
|
||||||
src/matriqueroom.h \
|
src/matriqueroom.h \
|
||||||
src/userlistmodel.h \
|
src/userlistmodel.h \
|
||||||
src/imageitem.h
|
src/imageitem.h \
|
||||||
|
src/accountlistmodel.h
|
||||||
|
|
|
@ -151,11 +151,6 @@ Page {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var replaceViewFunction = function() {
|
|
||||||
if (matriqueController.isLogin) stackView.replace(roomPage)
|
|
||||||
matriqueController.isLoginChanged.disconnect(replaceViewFunction)
|
|
||||||
}
|
|
||||||
matriqueController.isLoginChanged.connect(replaceViewFunction)
|
|
||||||
controller.loginWithCredentials(serverField.text, usernameField.text, passwordField.text)
|
controller.loginWithCredentials(serverField.text, usernameField.text, passwordField.text)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,52 +8,64 @@ import "component"
|
||||||
import "form"
|
import "form"
|
||||||
|
|
||||||
Page {
|
Page {
|
||||||
property var connection
|
// Page {
|
||||||
|
// id: accountForm
|
||||||
|
// parent: null
|
||||||
|
|
||||||
|
// padding: 64
|
||||||
|
|
||||||
|
// ColumnLayout {
|
||||||
|
// RowLayout {
|
||||||
|
// Layout.preferredHeight: 60
|
||||||
|
|
||||||
|
// ImageStatus {
|
||||||
|
// Layout.preferredWidth: height
|
||||||
|
// Layout.fillHeight: true
|
||||||
|
|
||||||
|
// source: matriqueController.isLogin ? connection.localUser && connection.localUser.avatarUrl ? "image://mxc/" + connection.localUser.avatarUrl : "" : "qrc:/asset/img/avatar.png"
|
||||||
|
// displayText: matriqueController.isLogin && connection.localUser.displayName ? connection.localUser.displayName : ""
|
||||||
|
// }
|
||||||
|
|
||||||
|
// ColumnLayout {
|
||||||
|
// Layout.fillWidth: true
|
||||||
|
// Layout.fillHeight: true
|
||||||
|
|
||||||
|
// Label {
|
||||||
|
// font.pointSize: 18
|
||||||
|
// text: matriqueController.isLogin ? connection.localUser.displayName : ""
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Label {
|
||||||
|
// font.pointSize: 12
|
||||||
|
// text: matriqueController.isLogin ? connection.localUser.id : ""
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Button {
|
||||||
|
// text: "Logout"
|
||||||
|
// highlighted: true
|
||||||
|
|
||||||
|
// onClicked: {
|
||||||
|
// matriqueController.logout()
|
||||||
|
// Qt.quit()
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
Page{
|
Page{
|
||||||
id: accountForm
|
id: accountForm
|
||||||
|
|
||||||
parent: null
|
parent: null
|
||||||
|
|
||||||
padding: 64
|
// Button {
|
||||||
|
// flat: true
|
||||||
|
// highlighted: true
|
||||||
|
// text: "Login"
|
||||||
|
|
||||||
ColumnLayout {
|
// onClicked: stackView.push(loginPage)
|
||||||
RowLayout {
|
// }
|
||||||
Layout.preferredHeight: 60
|
|
||||||
|
|
||||||
ImageStatus {
|
|
||||||
Layout.preferredWidth: height
|
|
||||||
Layout.fillHeight: true
|
|
||||||
|
|
||||||
source: matriqueController.isLogin ? connection.localUser && connection.localUser.avatarUrl ? "image://mxc/" + connection.localUser.avatarUrl : "" : "qrc:/asset/img/avatar.png"
|
|
||||||
displayText: matriqueController.isLogin && connection.localUser.displayName ? connection.localUser.displayName : ""
|
|
||||||
}
|
|
||||||
|
|
||||||
ColumnLayout {
|
|
||||||
Layout.fillWidth: true
|
|
||||||
Layout.fillHeight: true
|
|
||||||
|
|
||||||
Label {
|
|
||||||
font.pointSize: 18
|
|
||||||
text: matriqueController.isLogin ? connection.localUser.displayName : ""
|
|
||||||
}
|
|
||||||
|
|
||||||
Label {
|
|
||||||
font.pointSize: 12
|
|
||||||
text: matriqueController.isLogin ? connection.localUser.id : ""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Button {
|
|
||||||
text: "Logout"
|
|
||||||
highlighted: true
|
|
||||||
|
|
||||||
onClicked: {
|
|
||||||
matriqueController.logout()
|
|
||||||
Qt.quit()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Page {
|
Page {
|
||||||
|
|
64
qml/main.qml
64
qml/main.qml
|
@ -25,12 +25,6 @@ ApplicationWindow {
|
||||||
|
|
||||||
Material.theme: MSettings.darkTheme ? Material.Dark : Material.Light
|
Material.theme: MSettings.darkTheme ? Material.Dark : Material.Light
|
||||||
|
|
||||||
Settings {
|
|
||||||
property alias homeserver: matriqueController.homeserver
|
|
||||||
property alias userID: matriqueController.userID
|
|
||||||
property alias token: matriqueController.token
|
|
||||||
}
|
|
||||||
|
|
||||||
FontLoader { id: materialFont; source: "qrc:/asset/font/material.ttf" }
|
FontLoader { id: materialFont; source: "qrc:/asset/font/material.ttf" }
|
||||||
|
|
||||||
Controller {
|
Controller {
|
||||||
|
@ -77,15 +71,13 @@ ApplicationWindow {
|
||||||
|
|
||||||
parent: null
|
parent: null
|
||||||
|
|
||||||
connection: window.connection
|
connection: accountListView.currentConnection
|
||||||
}
|
}
|
||||||
|
|
||||||
Setting {
|
Setting {
|
||||||
id: settingPage
|
id: settingPage
|
||||||
|
|
||||||
parent: null
|
parent: null
|
||||||
|
|
||||||
connection: window.connection
|
|
||||||
}
|
}
|
||||||
|
|
||||||
RowLayout {
|
RowLayout {
|
||||||
|
@ -104,25 +96,34 @@ ApplicationWindow {
|
||||||
anchors.fill: parent
|
anchors.fill: parent
|
||||||
spacing: 0
|
spacing: 0
|
||||||
|
|
||||||
SideNavButton {
|
ListView {
|
||||||
|
property var currentConnection: null
|
||||||
|
|
||||||
Layout.fillWidth: true
|
Layout.fillWidth: true
|
||||||
Layout.preferredHeight: width
|
Layout.fillHeight: true
|
||||||
|
|
||||||
|
id: accountListView
|
||||||
|
|
||||||
|
model: AccountListModel { controller: matriqueController }
|
||||||
|
|
||||||
|
spacing: 0
|
||||||
|
|
||||||
|
delegate: SideNavButton {
|
||||||
|
width: parent.width
|
||||||
|
height: width
|
||||||
|
|
||||||
ImageStatus {
|
ImageStatus {
|
||||||
anchors.fill: parent
|
anchors.fill: parent
|
||||||
anchors.margins: 12
|
anchors.margins: 12
|
||||||
|
|
||||||
source: matriqueController.isLogin ? connection.localUser && connection.localUser.avatarUrl ? "image://mxc/" + connection.localUser.avatarUrl : "" : "qrc:/asset/img/avatar.png"
|
// source: matriqueController.isLogin ? connection.localUser && connection.localUser.avatarUrl ? "image://mxc/" + connection.localUser.avatarUrl : "" : "qrc:/asset/img/avatar.png"
|
||||||
displayText: matriqueController.isLogin && connection.localUser.displayName ? connection.localUser.displayName : ""
|
displayText: name
|
||||||
}
|
}
|
||||||
|
|
||||||
page: roomPage
|
page: roomPage
|
||||||
|
|
||||||
|
onClicked: accountListView.currentConnection = connection
|
||||||
}
|
}
|
||||||
|
|
||||||
Rectangle {
|
|
||||||
Layout.fillHeight: true
|
|
||||||
|
|
||||||
color: "transparent"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SideNavButton {
|
SideNavButton {
|
||||||
|
@ -241,7 +242,20 @@ ApplicationWindow {
|
||||||
anchors.fill: parent
|
anchors.fill: parent
|
||||||
|
|
||||||
icon: "\ue8b8"
|
icon: "\ue8b8"
|
||||||
color: parent.highlighted ? Material.accent : "white"
|
color: "white"
|
||||||
|
}
|
||||||
|
page: loginPage
|
||||||
|
}
|
||||||
|
|
||||||
|
SideNavButton {
|
||||||
|
Layout.fillWidth: true
|
||||||
|
Layout.preferredHeight: width
|
||||||
|
|
||||||
|
MaterialIcon {
|
||||||
|
anchors.fill: parent
|
||||||
|
|
||||||
|
icon: "\ue8b8"
|
||||||
|
color: "white"
|
||||||
}
|
}
|
||||||
page: settingPage
|
page: settingPage
|
||||||
}
|
}
|
||||||
|
@ -272,13 +286,9 @@ ApplicationWindow {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Component.onCompleted: {
|
Binding {
|
||||||
imageProvider.connection = matriqueController.connection
|
target: imageProvider
|
||||||
|
property: "connection"
|
||||||
if (matriqueController.userID && matriqueController.token) {
|
value: matriqueController.connection
|
||||||
matriqueController.login();
|
|
||||||
} else {
|
|
||||||
stackView.replace(loginPage);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,71 @@
|
||||||
|
#include "accountlistmodel.h"
|
||||||
|
|
||||||
|
AccountListModel::AccountListModel(QObject* parent)
|
||||||
|
: QAbstractListModel(parent) {}
|
||||||
|
|
||||||
|
void AccountListModel::setController(Controller* value) {
|
||||||
|
if (m_controller != value) {
|
||||||
|
beginResetModel();
|
||||||
|
m_connections.clear();
|
||||||
|
|
||||||
|
m_controller = value;
|
||||||
|
|
||||||
|
for (auto c : m_controller->connections()) m_connections.append(c);
|
||||||
|
|
||||||
|
connect(m_controller, &Controller::connectionAdded, this,
|
||||||
|
[=](Connection* conn) {
|
||||||
|
beginInsertRows(QModelIndex(), m_connections.count(),
|
||||||
|
m_connections.count());
|
||||||
|
m_connections.append(conn);
|
||||||
|
endInsertRows();
|
||||||
|
});
|
||||||
|
connect(m_controller, &Controller::connectionDropped, this,
|
||||||
|
[=](Connection* conn) {
|
||||||
|
const auto it =
|
||||||
|
std::find(m_connections.begin(), m_connections.end(), conn);
|
||||||
|
if (it == m_connections.end())
|
||||||
|
return; // Already deleted, nothing to do
|
||||||
|
const int row = it - m_connections.begin();
|
||||||
|
beginRemoveRows(QModelIndex(), row, row);
|
||||||
|
m_connections.erase(it);
|
||||||
|
endRemoveRows();
|
||||||
|
});
|
||||||
|
emit controllerChanged();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
QVariant AccountListModel::data(const QModelIndex& index, int role) const {
|
||||||
|
if (!index.isValid()) return QVariant();
|
||||||
|
|
||||||
|
if (index.row() >= m_controller->connections().count()) {
|
||||||
|
qDebug()
|
||||||
|
<< "UserListModel, something's wrong: index.row() >= m_users.count()";
|
||||||
|
return QVariant();
|
||||||
|
}
|
||||||
|
auto m_connection = m_controller->connections().at(index.row());
|
||||||
|
if (role == NameRole) {
|
||||||
|
return m_connection->user()->displayname();
|
||||||
|
}
|
||||||
|
if (role == AvatarRole) {
|
||||||
|
return m_connection->user()->avatar(64);
|
||||||
|
}
|
||||||
|
if (role == ConnectionRole) {
|
||||||
|
return QVariant::fromValue(m_connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
return QVariant();
|
||||||
|
}
|
||||||
|
|
||||||
|
int AccountListModel::rowCount(const QModelIndex& parent) const {
|
||||||
|
if (parent.isValid()) return 0;
|
||||||
|
|
||||||
|
return m_controller->connections().count();
|
||||||
|
}
|
||||||
|
|
||||||
|
QHash<int, QByteArray> AccountListModel::roleNames() const {
|
||||||
|
QHash<int, QByteArray> roles;
|
||||||
|
roles[NameRole] = "name";
|
||||||
|
roles[AvatarRole] = "avatar";
|
||||||
|
roles[ConnectionRole] = "connection";
|
||||||
|
return roles;
|
||||||
|
}
|
|
@ -0,0 +1,34 @@
|
||||||
|
#ifndef ACCOUNTLISTMODEL_H
|
||||||
|
#define ACCOUNTLISTMODEL_H
|
||||||
|
|
||||||
|
#include "controller.h"
|
||||||
|
|
||||||
|
#include <QAbstractListModel>
|
||||||
|
#include <QObject>
|
||||||
|
|
||||||
|
class AccountListModel : public QAbstractListModel {
|
||||||
|
Q_OBJECT
|
||||||
|
Q_PROPERTY(Controller* controller READ controller WRITE setController NOTIFY
|
||||||
|
controllerChanged)
|
||||||
|
public:
|
||||||
|
enum EventRoles { NameRole = Qt::UserRole + 1, AvatarRole, ConnectionRole };
|
||||||
|
|
||||||
|
AccountListModel(QObject* parent = nullptr);
|
||||||
|
|
||||||
|
QVariant data(const QModelIndex& index, int role = NameRole) const override;
|
||||||
|
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
|
||||||
|
|
||||||
|
QHash<int, QByteArray> roleNames() const override;
|
||||||
|
|
||||||
|
Controller* controller() { return m_controller; }
|
||||||
|
void setController(Controller* value);
|
||||||
|
|
||||||
|
private:
|
||||||
|
Controller* m_controller;
|
||||||
|
QVector<Connection*> m_connections;
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void controllerChanged();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // ACCOUNTLISTMODEL_H
|
|
@ -1,6 +1,7 @@
|
||||||
#include "controller.h"
|
#include "controller.h"
|
||||||
|
|
||||||
#include "matriqueroom.h"
|
#include "matriqueroom.h"
|
||||||
|
#include "settings.h"
|
||||||
|
|
||||||
#include "events/eventcontent.h"
|
#include "events/eventcontent.h"
|
||||||
#include "events/roommessageevent.h"
|
#include "events/roommessageevent.h"
|
||||||
|
@ -8,7 +9,21 @@
|
||||||
#include "csapi/joining.h"
|
#include "csapi/joining.h"
|
||||||
|
|
||||||
#include <QClipboard>
|
#include <QClipboard>
|
||||||
#include <QSystemTrayIcon>
|
#include <QFile>
|
||||||
|
#include <QFileInfo>
|
||||||
|
#include <QtCore/QDebug>
|
||||||
|
#include <QtCore/QDir>
|
||||||
|
#include <QtCore/QElapsedTimer>
|
||||||
|
#include <QtCore/QFileInfo>
|
||||||
|
#include <QtCore/QStandardPaths>
|
||||||
|
#include <QtCore/QStringBuilder>
|
||||||
|
#include <QtCore/QTimer>
|
||||||
|
#include <QtGui/QCloseEvent>
|
||||||
|
#include <QtGui/QDesktopServices>
|
||||||
|
#include <QtGui/QMovie>
|
||||||
|
#include <QtGui/QPixmap>
|
||||||
|
#include <QtNetwork/QAuthenticator>
|
||||||
|
#include <QtNetwork/QNetworkReply>
|
||||||
|
|
||||||
Controller::Controller(QObject* parent) : QObject(parent) {
|
Controller::Controller(QObject* parent) : QObject(parent) {
|
||||||
tray->setIcon(QIcon(":/asset/img/icon.png"));
|
tray->setIcon(QIcon(":/asset/img/icon.png"));
|
||||||
|
@ -25,84 +40,187 @@ Controller::Controller(QObject* parent) : QObject(parent) {
|
||||||
|
|
||||||
Connection::setRoomType<MatriqueRoom>();
|
Connection::setRoomType<MatriqueRoom>();
|
||||||
|
|
||||||
connect(m_connection, &Connection::connected, this, &Controller::connected);
|
invokeLogin();
|
||||||
connect(m_connection, &Connection::resolveError, this,
|
|
||||||
&Controller::reconnect);
|
|
||||||
connect(m_connection, &Connection::syncError, this, &Controller::reconnect);
|
|
||||||
connect(m_connection, &Connection::syncDone, this, &Controller::resync);
|
|
||||||
connect(m_connection, &Connection::connected, this,
|
|
||||||
&Controller::connectionChanged);
|
|
||||||
|
|
||||||
connect(m_connection, &Connection::connected, [=] { setBusy(true); });
|
|
||||||
connect(m_connection, &Connection::syncDone, [=] { setBusy(false); });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Controller::~Controller() {
|
Controller::~Controller() {
|
||||||
m_connection->saveState();
|
// m_connection->saveState();
|
||||||
m_connection->stopSync();
|
// m_connection->stopSync();
|
||||||
m_connection->deleteLater();
|
// m_connection->deleteLater();
|
||||||
}
|
|
||||||
|
|
||||||
void Controller::login() {
|
|
||||||
if (!m_isLogin) {
|
|
||||||
m_connection->setHomeserver(QUrl(m_homeserver));
|
|
||||||
m_connection->connectWithToken(m_userID, m_token, "");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Controller::loginWithCredentials(QString serverAddr, QString user,
|
void Controller::loginWithCredentials(QString serverAddr, QString user,
|
||||||
QString pass) {
|
QString pass) {
|
||||||
if (!m_isLogin) {
|
|
||||||
if (!user.isEmpty() && !pass.isEmpty()) {
|
if (!user.isEmpty() && !pass.isEmpty()) {
|
||||||
|
Connection* m_connection = new Connection(this);
|
||||||
m_connection->setHomeserver(QUrl(serverAddr));
|
m_connection->setHomeserver(QUrl(serverAddr));
|
||||||
m_connection->connectToServer(user, pass, "");
|
m_connection->connectToServer(user, pass, "");
|
||||||
}
|
connect(m_connection, &Connection::connected, [=] {
|
||||||
} else {
|
AccountSettings account(m_connection->userId());
|
||||||
qCritical() << "You are already logged in.";
|
account.setKeepLoggedIn(true);
|
||||||
|
account.clearAccessToken(); // Drop the legacy - just in case
|
||||||
|
account.setHomeserver(m_connection->homeserver());
|
||||||
|
account.setDeviceId(m_connection->deviceId());
|
||||||
|
account.setDeviceName("Matrique");
|
||||||
|
if (!saveAccessToken(account, m_connection->accessToken()))
|
||||||
|
qWarning() << "Couldn't save access token";
|
||||||
|
account.sync();
|
||||||
|
addConnection(m_connection);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Controller::logout() {
|
void Controller::addConnection(Connection* c) {
|
||||||
m_connection->logout();
|
Q_ASSERT_X(c, __FUNCTION__, "Attempt to add a null connection");
|
||||||
setUserID("");
|
|
||||||
setToken("");
|
m_connections.push_back(c);
|
||||||
setIsLogin(false);
|
|
||||||
|
connect(c, &Connection::syncDone, this, [=] {
|
||||||
|
// gotEvents(c);
|
||||||
|
|
||||||
|
// Borrowed the logic from Quiark's code in Tensor to cache not too
|
||||||
|
// aggressively and not on the first sync. The static variable instance
|
||||||
|
// is created per-closure, meaning per-connection (which is why this
|
||||||
|
// code is not in gotEvents() ).
|
||||||
|
static int counter = 0;
|
||||||
|
if (++counter % 17 == 2) c->saveState();
|
||||||
|
});
|
||||||
|
connect(c, &Connection::loggedOut, this, [=] { dropConnection(c); });
|
||||||
|
|
||||||
|
using namespace QMatrixClient;
|
||||||
|
|
||||||
|
c->sync(30000);
|
||||||
|
|
||||||
|
emit connectionAdded(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Controller::dropConnection(Connection* c) {
|
||||||
|
Q_ASSERT_X(c, __FUNCTION__, "Attempt to drop a null connection");
|
||||||
|
m_connections.removeOne(c);
|
||||||
|
|
||||||
|
Q_ASSERT(!m_connections.contains(c) && !c->syncJob());
|
||||||
|
emit connectionAdded(c);
|
||||||
|
c->deleteLater();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline QString accessTokenFileName(const AccountSettings& account) {
|
||||||
|
QString fileName = account.userId();
|
||||||
|
fileName.replace(':', '_');
|
||||||
|
return QStandardPaths::writableLocation(
|
||||||
|
QStandardPaths::AppLocalDataLocation) +
|
||||||
|
'/' + fileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Controller::invokeLogin() {
|
||||||
|
using namespace QMatrixClient;
|
||||||
|
const auto accounts = SettingsGroup("Accounts").childGroups();
|
||||||
|
bool autoLoggedIn = false;
|
||||||
|
for (const auto& accountId : accounts) {
|
||||||
|
AccountSettings account{accountId};
|
||||||
|
if (!account.homeserver().isEmpty()) {
|
||||||
|
auto accessToken = loadAccessToken(account);
|
||||||
|
if (accessToken.isEmpty()) {
|
||||||
|
// Try to look in the legacy location (QSettings) and if found,
|
||||||
|
// migrate it from there to a file.
|
||||||
|
accessToken = account.accessToken().toLatin1();
|
||||||
|
if (accessToken.isEmpty())
|
||||||
|
continue; // No access token anywhere, no autologin
|
||||||
|
|
||||||
|
saveAccessToken(account, accessToken);
|
||||||
|
account.clearAccessToken(); // Clean the old place
|
||||||
|
}
|
||||||
|
|
||||||
|
autoLoggedIn = true;
|
||||||
|
auto c = new Connection(account.homeserver(), this);
|
||||||
|
auto deviceName = account.deviceName();
|
||||||
|
connect(c, &Connection::connected, this, [=] {
|
||||||
|
c->loadState();
|
||||||
|
addConnection(c);
|
||||||
|
});
|
||||||
|
c->connectWithToken(account.userId(), accessToken, account.deviceId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
QByteArray Controller::loadAccessToken(const AccountSettings& account) {
|
||||||
|
QFile accountTokenFile{accessTokenFileName(account)};
|
||||||
|
if (accountTokenFile.open(QFile::ReadOnly)) {
|
||||||
|
if (accountTokenFile.size() < 1024) return accountTokenFile.readAll();
|
||||||
|
|
||||||
|
qWarning() << "File" << accountTokenFile.fileName() << "is"
|
||||||
|
<< accountTokenFile.size()
|
||||||
|
<< "bytes long - too long for a token, ignoring it.";
|
||||||
|
}
|
||||||
|
qWarning() << "Could not open access token file"
|
||||||
|
<< accountTokenFile.fileName();
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Controller::saveAccessToken(const AccountSettings& account,
|
||||||
|
const QByteArray& accessToken) {
|
||||||
|
// (Re-)Make a dedicated file for access_token.
|
||||||
|
QFile accountTokenFile{accessTokenFileName(account)};
|
||||||
|
accountTokenFile.remove(); // Just in case
|
||||||
|
|
||||||
|
auto fileDir = QFileInfo(accountTokenFile).dir();
|
||||||
|
if (!((fileDir.exists() || fileDir.mkpath(".")) &&
|
||||||
|
accountTokenFile.open(QFile::WriteOnly))) {
|
||||||
|
emit errorOccured();
|
||||||
|
} else {
|
||||||
|
// Try to restrict access rights to the file. The below is useless
|
||||||
|
// on Windows: FAT doesn't control access at all and NTFS is
|
||||||
|
// incompatible with the UNIX perms model used by Qt. If the attempt
|
||||||
|
// didn't have the effect, at least ask the user if it's fine to save
|
||||||
|
// the token to a file readable by others.
|
||||||
|
// TODO: use system-specific API to ensure proper access.
|
||||||
|
if ((accountTokenFile.setPermissions(QFile::ReadOwner |
|
||||||
|
QFile::WriteOwner) &&
|
||||||
|
!(accountTokenFile.permissions() &
|
||||||
|
(QFile::ReadGroup | QFile::ReadOther)))) {
|
||||||
|
accountTokenFile.write(accessToken);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Controller::connected() {
|
void Controller::connected() {
|
||||||
setHomeserver(m_connection->homeserver().toString());
|
// setHomeserver(m_connection->homeserver().toString());
|
||||||
setUserID(m_connection->userId());
|
// setUserID(m_connection->userId());
|
||||||
setToken(m_connection->accessToken());
|
// setToken(m_connection->accessToken());
|
||||||
m_connection->loadState();
|
// m_connection->loadState();
|
||||||
resync();
|
// resync();
|
||||||
setIsLogin(true);
|
// setIsLogin(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Controller::resync() { m_connection->sync(30000); }
|
void Controller::resync() { /*m_connection->sync(30000);*/
|
||||||
|
}
|
||||||
|
|
||||||
void Controller::reconnect() {
|
void Controller::reconnect() {
|
||||||
qDebug() << "Connection lost. Reconnecting...";
|
// qDebug() << "Connection lost. Reconnecting...";
|
||||||
m_connection->connectWithToken(m_userID, m_token, "");
|
// m_connection->connectWithToken(m_userID, m_token, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Controller::joinRoom(const QString& alias) {
|
void Controller::joinRoom(const QString& alias) {
|
||||||
JoinRoomJob* joinRoomJob = m_connection->joinRoom(alias);
|
// JoinRoomJob* joinRoomJob = m_connection->joinRoom(alias);
|
||||||
setBusy(true);
|
// setBusy(true);
|
||||||
joinRoomJob->connect(joinRoomJob, &JoinRoomJob::finished,
|
// joinRoomJob->connect(joinRoomJob, &JoinRoomJob::finished,
|
||||||
[=] { setBusy(false); });
|
// [=] { setBusy(false); });
|
||||||
}
|
}
|
||||||
|
|
||||||
void Controller::createRoom(const QString& name, const QString& topic) {
|
void Controller::createRoom(const QString& name, const QString& topic) {
|
||||||
CreateRoomJob* createRoomJob =
|
// CreateRoomJob* createRoomJob =
|
||||||
((Connection*)m_connection)
|
// ((Connection*)m_connection)
|
||||||
->createRoom(Connection::PublishRoom, "", name, topic, QStringList());
|
// ->createRoom(Connection::PublishRoom, "", name, topic,
|
||||||
setBusy(true);
|
// QStringList());
|
||||||
createRoomJob->connect(createRoomJob, &CreateRoomJob::finished,
|
// setBusy(true);
|
||||||
[=] { setBusy(false); });
|
// createRoomJob->connect(createRoomJob, &CreateRoomJob::finished,
|
||||||
|
// [=] { setBusy(false); });
|
||||||
}
|
}
|
||||||
|
|
||||||
void Controller::createDirectChat(const QString& userID) {
|
void Controller::createDirectChat(const QString& userID) {
|
||||||
m_connection->requestDirectChat(userID);
|
// m_connection->requestDirectChat(userID);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Controller::copyToClipboard(const QString& text) {
|
void Controller::copyToClipboard(const QString& text) {
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#define CONTROLLER_H
|
#define CONTROLLER_H
|
||||||
|
|
||||||
#include "connection.h"
|
#include "connection.h"
|
||||||
|
#include "settings.h"
|
||||||
#include "user.h"
|
#include "user.h"
|
||||||
|
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
|
@ -15,12 +16,6 @@ using namespace QMatrixClient;
|
||||||
class Controller : public QObject {
|
class Controller : public QObject {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
Q_PROPERTY(Connection* connection READ connection CONSTANT)
|
|
||||||
Q_PROPERTY(bool isLogin READ isLogin WRITE setIsLogin NOTIFY isLoginChanged)
|
|
||||||
Q_PROPERTY(QString homeserver READ homeserver WRITE setHomeserver NOTIFY
|
|
||||||
homeserverChanged)
|
|
||||||
Q_PROPERTY(QString userID READ userID WRITE setUserID NOTIFY userIDChanged)
|
|
||||||
Q_PROPERTY(QByteArray token READ token WRITE setToken NOTIFY tokenChanged)
|
|
||||||
Q_PROPERTY(bool busy READ busy WRITE setBusy NOTIFY busyChanged)
|
Q_PROPERTY(bool busy READ busy WRITE setBusy NOTIFY busyChanged)
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
@ -28,80 +23,50 @@ class Controller : public QObject {
|
||||||
~Controller();
|
~Controller();
|
||||||
|
|
||||||
// All the Q_INVOKABLEs.
|
// All the Q_INVOKABLEs.
|
||||||
Q_INVOKABLE void login();
|
|
||||||
Q_INVOKABLE void loginWithCredentials(QString, QString, QString);
|
Q_INVOKABLE void loginWithCredentials(QString, QString, QString);
|
||||||
Q_INVOKABLE void logout();
|
|
||||||
|
QVector<Connection*> connections() { return m_connections; }
|
||||||
|
|
||||||
// All the non-Q_INVOKABLE functions.
|
// All the non-Q_INVOKABLE functions.
|
||||||
|
void addConnection(Connection* c);
|
||||||
|
void dropConnection(Connection* c);
|
||||||
|
|
||||||
// All the Q_PROPERTYs.
|
// All the Q_PROPERTYs.
|
||||||
Connection* m_connection = new Connection();
|
|
||||||
Connection* connection() { return m_connection; }
|
|
||||||
|
|
||||||
bool isLogin() { return m_isLogin; }
|
|
||||||
void setIsLogin(bool n) {
|
|
||||||
if (n != m_isLogin) {
|
|
||||||
m_isLogin = n;
|
|
||||||
emit isLoginChanged();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
QString userID() { return m_userID; }
|
|
||||||
void setUserID(QString n) {
|
|
||||||
if (n != m_userID) {
|
|
||||||
m_userID = n;
|
|
||||||
emit userIDChanged();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
QByteArray token() { return m_token; }
|
|
||||||
void setToken(QByteArray n) {
|
|
||||||
if (n != m_token) {
|
|
||||||
m_token = n;
|
|
||||||
emit tokenChanged();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
QString homeserver() { return m_homeserver; }
|
|
||||||
void setHomeserver(QString n) {
|
|
||||||
if (n != m_homeserver) {
|
|
||||||
m_homeserver = n;
|
|
||||||
emit homeserverChanged();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool busy() { return m_busy; }
|
bool busy() { return m_busy; }
|
||||||
void setBusy(bool b) {
|
void setBusy(bool value) {
|
||||||
if (b != m_busy) {
|
if (value != m_busy) {
|
||||||
m_busy = b;
|
m_busy = value;
|
||||||
emit busyChanged();
|
emit busyChanged();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QVector<Connection*> m_connections;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QClipboard* m_clipboard = QApplication::clipboard();
|
QClipboard* m_clipboard = QApplication::clipboard();
|
||||||
QSystemTrayIcon* tray = new QSystemTrayIcon();
|
QSystemTrayIcon* tray = new QSystemTrayIcon();
|
||||||
QMenu* trayMenu = new QMenu();
|
QMenu* trayMenu = new QMenu();
|
||||||
|
|
||||||
bool m_isLogin = false;
|
|
||||||
QString m_userID;
|
|
||||||
QByteArray m_token;
|
|
||||||
QString m_homeserver;
|
|
||||||
bool m_busy = false;
|
bool m_busy = false;
|
||||||
|
|
||||||
void connected();
|
void connected();
|
||||||
void resync();
|
void resync();
|
||||||
void reconnect();
|
void reconnect();
|
||||||
|
QByteArray loadAccessToken(const AccountSettings& account);
|
||||||
|
bool saveAccessToken(const AccountSettings& account,
|
||||||
|
const QByteArray& accessToken);
|
||||||
|
void loadSettings();
|
||||||
|
void saveSettings() const;
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void invokeLogin();
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void connectionChanged();
|
|
||||||
void isLoginChanged();
|
|
||||||
void userIDChanged();
|
|
||||||
void tokenChanged();
|
|
||||||
void homeserverChanged();
|
|
||||||
void busyChanged();
|
void busyChanged();
|
||||||
void errorOccured();
|
void errorOccured();
|
||||||
void toggleWindow();
|
void toggleWindow();
|
||||||
|
void connectionAdded(Connection* conn);
|
||||||
|
void connectionDropped(Connection* conn);
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void joinRoom(const QString& alias);
|
void joinRoom(const QString& alias);
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
#include <QQmlApplicationEngine>
|
#include <QQmlApplicationEngine>
|
||||||
#include <QQmlContext>
|
#include <QQmlContext>
|
||||||
|
|
||||||
|
#include "accountlistmodel.h"
|
||||||
#include "controller.h"
|
#include "controller.h"
|
||||||
#include "emojimodel.h"
|
#include "emojimodel.h"
|
||||||
#include "imageitem.h"
|
#include "imageitem.h"
|
||||||
|
@ -38,6 +39,7 @@ int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
qmlRegisterType<ImageItem>("Matrique", 0, 1, "ImageItem");
|
qmlRegisterType<ImageItem>("Matrique", 0, 1, "ImageItem");
|
||||||
qmlRegisterType<Controller>("Matrique", 0, 1, "Controller");
|
qmlRegisterType<Controller>("Matrique", 0, 1, "Controller");
|
||||||
|
qmlRegisterType<AccountListModel>("Matrique", 0, 1, "AccountListModel");
|
||||||
qmlRegisterType<RoomListModel>("Matrique", 0, 1, "RoomListModel");
|
qmlRegisterType<RoomListModel>("Matrique", 0, 1, "RoomListModel");
|
||||||
qmlRegisterType<UserListModel>("Matrique", 0, 1, "UserListModel");
|
qmlRegisterType<UserListModel>("Matrique", 0, 1, "UserListModel");
|
||||||
qmlRegisterType<MessageEventModel>("Matrique", 0, 1, "MessageEventModel");
|
qmlRegisterType<MessageEventModel>("Matrique", 0, 1, "MessageEventModel");
|
||||||
|
|
|
@ -17,10 +17,7 @@ class UserListModel : public QAbstractListModel {
|
||||||
Q_PROPERTY(
|
Q_PROPERTY(
|
||||||
QMatrixClient::Room* room READ room WRITE setRoom NOTIFY roomChanged)
|
QMatrixClient::Room* room READ room WRITE setRoom NOTIFY roomChanged)
|
||||||
public:
|
public:
|
||||||
enum EventRoles {
|
enum EventRoles { NameRole = Qt::UserRole + 1, AvatarRole };
|
||||||
NameRole = Qt::UserRole + 1,
|
|
||||||
AvatarRole
|
|
||||||
};
|
|
||||||
|
|
||||||
using User = QMatrixClient::User;
|
using User = QMatrixClient::User;
|
||||||
|
|
||||||
|
@ -30,8 +27,7 @@ class UserListModel : public QAbstractListModel {
|
||||||
void setRoom(QMatrixClient::Room* room);
|
void setRoom(QMatrixClient::Room* room);
|
||||||
User* userAt(QModelIndex index);
|
User* userAt(QModelIndex index);
|
||||||
|
|
||||||
QVariant data(const QModelIndex& index,
|
QVariant data(const QModelIndex& index, int role = NameRole) const override;
|
||||||
int role = NameRole) const override;
|
|
||||||
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
|
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
|
||||||
|
|
||||||
QHash<int, QByteArray> roleNames() const override;
|
QHash<int, QByteArray> roleNames() const override;
|
||||||
|
|
Loading…
Reference in New Issue