Add error callback of join room and add room.

Also fix a bug of not being able to log out when the access token is
revoked.
This commit is contained in:
Black Hat 2018-10-13 18:54:33 +08:00
parent fa3082ca6b
commit d9592e4a7c
4 changed files with 32 additions and 26 deletions

View File

@ -254,6 +254,7 @@ ApplicationWindow {
onAccepted: spectralController.createRoom(currentConnection, addRoomDialogNameTextField.text, addRoomDialogTopicTextField.text) onAccepted: spectralController.createRoom(currentConnection, addRoomDialogNameTextField.text, addRoomDialogTopicTextField.text)
} }
} }
MenuItem { MenuItem {
text: "Join Room" text: "Join Room"
@ -304,7 +305,7 @@ ApplicationWindow {
placeholderText: "@bot:matrix.org" placeholderText: "@bot:matrix.org"
} }
onAccepted: currentConnection.createDirectChat(directChatDialogTextField.text) onAccepted: spectralController.createDirectChat(currentConnection, directChatDialogTextField.text)
} }
} }
} }

View File

@ -8,6 +8,7 @@
#include "events/roommessageevent.h" #include "events/roommessageevent.h"
#include "csapi/joining.h" #include "csapi/joining.h"
#include "csapi/logout.h"
#include <QClipboard> #include <QClipboard>
#include <QFile> #include <QFile>
@ -75,7 +76,7 @@ void Controller::loginWithCredentials(QString serverAddr, QString user,
}); });
connect(m_connection, &Connection::loginError, connect(m_connection, &Connection::loginError,
[=](QString error, QByteArray detail) { [=](QString error, QByteArray detail) {
emit errorOccured("Login", error); emit errorOccured("Login Failed", error);
}); });
} }
} }
@ -89,7 +90,15 @@ void Controller::logout(Connection* conn) {
SettingsGroup("Accounts").remove(conn->userId()); SettingsGroup("Accounts").remove(conn->userId());
QFile(accessTokenFileName(AccountSettings(conn->userId()))).remove(); QFile(accessTokenFileName(AccountSettings(conn->userId()))).remove();
conn->logout(); auto job = conn->callApi<LogoutJob>();
connect(job, &LogoutJob::finished, conn, [=] {
conn->stopSync();
emit conn->stateChanged();
emit conn->loggedOut();
});
connect(job, &LogoutJob::failure, this, [=] {
emit errorOccured("Server-side Logout Failed", job->errorString());
});
} }
void Controller::addConnection(Connection* c) { void Controller::addConnection(Connection* c) {
@ -172,18 +181,26 @@ bool Controller::saveAccessToken(const AccountSettings& account,
void Controller::joinRoom(Connection* c, const QString& alias) { void Controller::joinRoom(Connection* c, const QString& alias) {
JoinRoomJob* joinRoomJob = c->joinRoom(alias); JoinRoomJob* joinRoomJob = c->joinRoom(alias);
setBusy(true); joinRoomJob->connect(joinRoomJob, &JoinRoomJob::failure, [=] {
joinRoomJob->connect(joinRoomJob, &JoinRoomJob::finished, emit errorOccured("Join Room Failed", joinRoomJob->errorString());
[=] { setBusy(false); }); });
} }
void Controller::createRoom(Connection* c, const QString& name, void Controller::createRoom(Connection* c, const QString& name,
const QString& topic) { const QString& topic) {
CreateRoomJob* createRoomJob = CreateRoomJob* createRoomJob =
c->createRoom(Connection::PublishRoom, "", name, topic, QStringList()); c->createRoom(Connection::PublishRoom, "", name, topic, QStringList());
setBusy(true); createRoomJob->connect(createRoomJob, &CreateRoomJob::failure, [=] {
createRoomJob->connect(createRoomJob, &CreateRoomJob::finished, emit errorOccured("Create Room Failed", createRoomJob->errorString());
[=] { setBusy(false); }); });
}
void Controller::createDirectChat(Connection* c, const QString& userID) {
CreateRoomJob* createRoomJob = c->createDirectChat(userID);
createRoomJob->connect(createRoomJob, &CreateRoomJob::failure, [=] {
emit errorOccured("Create Direct Chat Failed",
createRoomJob->errorString());
});
} }
void Controller::copyToClipboard(const QString& text) { void Controller::copyToClipboard(const QString& text) {

View File

@ -15,8 +15,6 @@ using namespace QMatrixClient;
class Controller : public QObject { class Controller : public QObject {
Q_OBJECT Q_OBJECT
Q_PROPERTY(bool busy READ busy WRITE setBusy NOTIFY busyChanged)
Q_PROPERTY(int accountCount READ accountCount NOTIFY connectionAdded NOTIFY Q_PROPERTY(int accountCount READ accountCount NOTIFY connectionAdded NOTIFY
connectionDropped) connectionDropped)
@ -34,14 +32,6 @@ class Controller : public QObject {
void dropConnection(Connection* c); void dropConnection(Connection* c);
// All the Q_PROPERTYs. // All the Q_PROPERTYs.
bool busy() { return m_busy; }
void setBusy(bool value) {
if (value != m_busy) {
m_busy = value;
emit busyChanged();
}
}
int accountCount() { return m_connections.count(); } int accountCount() { return m_connections.count(); }
Q_INVOKABLE QColor color(QString userId); Q_INVOKABLE QColor color(QString userId);
@ -53,8 +43,6 @@ class Controller : public QObject {
QMenu* trayMenu = new QMenu(); QMenu* trayMenu = new QMenu();
QVector<Connection*> m_connections; QVector<Connection*> m_connections;
bool m_busy = false;
QByteArray loadAccessToken(const AccountSettings& account); QByteArray loadAccessToken(const AccountSettings& account);
bool saveAccessToken(const AccountSettings& account, bool saveAccessToken(const AccountSettings& account,
const QByteArray& accessToken); const QByteArray& accessToken);
@ -77,6 +65,7 @@ class Controller : public QObject {
void logout(Connection* conn); void logout(Connection* conn);
void joinRoom(Connection* c, const QString& alias); void joinRoom(Connection* c, const QString& alias);
void createRoom(Connection* c, const QString& name, const QString& topic); void createRoom(Connection* c, const QString& name, const QString& topic);
void createDirectChat(Connection* c, const QString& userID);
void copyToClipboard(const QString& text); void copyToClipboard(const QString& text);
void playAudio(QUrl localFile); void playAudio(QUrl localFile);
void showMessage(const QString& title, const QString& msg, const QIcon& icon); void showMessage(const QString& title, const QString& msg, const QIcon& icon);

View File

@ -32,11 +32,6 @@ int main(int argc, char *argv[]) {
app.setQuitOnLastWindowClosed(false); app.setQuitOnLastWindowClosed(false);
qRegisterMetaType<SpectralRoom *>("SpectralRoom*");
qRegisterMetaType<User *>("User*");
qRegisterMetaType<MessageEventType>("MessageEventType");
qRegisterMetaType<SpectralRoom *>("SpectralRoom");
qmlRegisterType<ImageItem>("Spectral", 0, 1, "ImageItem"); qmlRegisterType<ImageItem>("Spectral", 0, 1, "ImageItem");
qmlRegisterType<Controller>("Spectral", 0, 1, "Controller"); qmlRegisterType<Controller>("Spectral", 0, 1, "Controller");
qmlRegisterType<AccountListModel>("Spectral", 0, 1, "AccountListModel"); qmlRegisterType<AccountListModel>("Spectral", 0, 1, "AccountListModel");
@ -48,6 +43,10 @@ int main(int argc, char *argv[]) {
"RoomMessageEvent", "ENUM"); "RoomMessageEvent", "ENUM");
qmlRegisterUncreatableType<RoomType>("Spectral", 0, 1, "RoomType", "ENUM"); qmlRegisterUncreatableType<RoomType>("Spectral", 0, 1, "RoomType", "ENUM");
qRegisterMetaType<User *>("User*");
qRegisterMetaType<MessageEventType>("MessageEventType");
qRegisterMetaType<SpectralRoom *>("SpectralRoom");
QQmlApplicationEngine engine; QQmlApplicationEngine engine;
engine.addImportPath("qrc:/imports"); engine.addImportPath("qrc:/imports");