Refactor error handling and move unrelated functions out of ActionsHandler
This commit is contained in:
@@ -9,6 +9,8 @@
|
||||
#include <QDebug>
|
||||
#include <QStringBuilder>
|
||||
|
||||
#include "controller.h"
|
||||
|
||||
ActionsHandler::ActionsHandler(QObject *parent)
|
||||
: QObject(parent)
|
||||
{
|
||||
@@ -48,7 +50,7 @@ void ActionsHandler::setConnection(Connection *connection)
|
||||
if (m_connection != nullptr) {
|
||||
connect(m_connection, &Connection::directChatAvailable, this, [this](Quotient::Room *room) {
|
||||
room->setDisplayed(true);
|
||||
Q_EMIT roomJoined(room->id());
|
||||
Q_EMIT Controller::instance().roomJoined(room->id());
|
||||
});
|
||||
}
|
||||
Q_EMIT connectionChanged();
|
||||
@@ -140,35 +142,6 @@ QVariantList ActionsHandler::commands() const
|
||||
return commands;
|
||||
}
|
||||
|
||||
void ActionsHandler::joinRoom(const QString &alias)
|
||||
{
|
||||
if (!alias.contains(":")) {
|
||||
Q_EMIT showMessage(MessageType::Error, i18n("The room id you are trying to join is not valid"));
|
||||
return;
|
||||
}
|
||||
|
||||
const auto knownServer = alias.mid(alias.indexOf(":") + 1);
|
||||
auto joinRoomJob = m_connection->joinRoom(alias, QStringList{knownServer});
|
||||
|
||||
Quotient::JoinRoomJob::connect(joinRoomJob, &JoinRoomJob::failure, [=] {
|
||||
Q_EMIT showMessage(MessageType::Error, i18n("Server error when joining the room \"%1\": %2", joinRoomJob->errorString()));
|
||||
});
|
||||
Quotient::JoinRoomJob::connect(joinRoomJob, &JoinRoomJob::success, [this, joinRoomJob] {
|
||||
Q_EMIT roomJoined(joinRoomJob->roomId());
|
||||
});
|
||||
}
|
||||
|
||||
void ActionsHandler::createRoom(const QString &name, const QString &topic)
|
||||
{
|
||||
auto createRoomJob = m_connection->createRoom(Connection::PublishRoom, "", name, topic, QStringList());
|
||||
Quotient::CreateRoomJob::connect(createRoomJob, &CreateRoomJob::failure, [=] {
|
||||
Q_EMIT showMessage(MessageType::Error, i18n("Room creation failed: \"%1\"", createRoomJob->errorString()));
|
||||
});
|
||||
Quotient::CreateRoomJob::connect(createRoomJob, &CreateRoomJob::success, [=] {
|
||||
Q_EMIT roomJoined(createRoomJob->roomId());
|
||||
});
|
||||
}
|
||||
|
||||
void ActionsHandler::postMessage(const QString &text,
|
||||
const QString &attachementPath,
|
||||
const QString &replyEventId,
|
||||
@@ -265,13 +238,13 @@ void ActionsHandler::postMessage(const QString &text,
|
||||
return;
|
||||
}
|
||||
if (splittedText.count() > 1) {
|
||||
joinRoom(splittedText[0] + ":" + splittedText[1]);
|
||||
Controller::instance().joinRoom(splittedText[0] + ":" + splittedText[1]);
|
||||
return;
|
||||
} else if (splittedText[0].indexOf(":") != -1) {
|
||||
joinRoom(splittedText[0]);
|
||||
Controller::instance().joinRoom(splittedText[0]);
|
||||
return;
|
||||
} else {
|
||||
joinRoom(splittedText[0] + ":matrix.org");
|
||||
Controller::instance().joinRoom(splittedText[0] + ":matrix.org");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -50,22 +50,10 @@ Q_SIGNALS:
|
||||
/// These messages will be displayed in the room view header.
|
||||
void showMessage(MessageType messageType, QString message);
|
||||
|
||||
/// \brief Emitted when an action made the user join a room.
|
||||
///
|
||||
/// Either when a new room was created, a direct chat was started
|
||||
/// or a group chat was joined. The UI will react to this signal
|
||||
/// and switch to the newly joined room.
|
||||
void roomJoined(QString roomName);
|
||||
|
||||
void roomChanged();
|
||||
void connectionChanged();
|
||||
|
||||
public Q_SLOTS:
|
||||
/// \brief Create new room for a group chat.
|
||||
void createRoom(const QString &name, const QString &topic);
|
||||
|
||||
/// \brief Join a room.
|
||||
void joinRoom(const QString &alias);
|
||||
|
||||
/// \brief Post a message.
|
||||
///
|
||||
|
||||
@@ -36,6 +36,7 @@
|
||||
|
||||
#include "csapi/account-data.h"
|
||||
#include "csapi/content-repo.h"
|
||||
#include "csapi/joining.h"
|
||||
#include "csapi/logout.h"
|
||||
#include "csapi/profile.h"
|
||||
#include "csapi/registration.h"
|
||||
@@ -53,6 +54,8 @@
|
||||
#include "trayicon.h"
|
||||
#endif
|
||||
|
||||
using namespace Quotient;
|
||||
|
||||
Controller::Controller(QObject *parent)
|
||||
: QObject(parent)
|
||||
{
|
||||
@@ -159,7 +162,7 @@ void Controller::loginWithAccessToken(const QString &serverAddr, const QString &
|
||||
setActiveConnection(conn);
|
||||
});
|
||||
connect(conn, &Connection::networkError, this, [=](QString error, const QString &, int, int) {
|
||||
Q_EMIT errorOccured("Network Error", std::move(error));
|
||||
Q_EMIT errorOccured(i18n("Network Error: %1", error));
|
||||
});
|
||||
conn->connectWithToken(user, token, deviceName);
|
||||
}
|
||||
@@ -195,7 +198,7 @@ void Controller::logout(Connection *conn, bool serverSideLogout)
|
||||
}
|
||||
auto logoutJob = conn->callApi<LogoutJob>();
|
||||
connect(logoutJob, &LogoutJob::failure, this, [=] {
|
||||
Q_EMIT errorOccured("Server-side Logout Failed", logoutJob->errorString());
|
||||
Q_EMIT errorOccured(i18n("Server-side Logout Failed: %1", logoutJob->errorString()));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -267,16 +270,16 @@ void Controller::invokeLogin()
|
||||
});
|
||||
connect(connection, &Connection::loginError, this, [=](const QString &error, const QString &) {
|
||||
if (error == "Unrecognised access token") {
|
||||
Q_EMIT errorOccured(i18n("Login Failed"), i18n("Access Token invalid or revoked"));
|
||||
Q_EMIT errorOccured(i18n("Login Failed: Access Token invalid or revoked"));
|
||||
logout(connection, false);
|
||||
} else {
|
||||
Q_EMIT errorOccured(i18n("Login Failed"), error);
|
||||
Q_EMIT errorOccured(i18n("Login Failed", error));
|
||||
logout(connection, true);
|
||||
}
|
||||
Q_EMIT initiated();
|
||||
});
|
||||
connect(connection, &Connection::networkError, this, [=](const QString &error, const QString &, int, int) {
|
||||
Q_EMIT errorOccured("Network Error", error);
|
||||
Q_EMIT errorOccured(i18n("Network Error: %1", error));
|
||||
});
|
||||
connection->connectWithToken(account.userId(), accessToken, account.deviceId());
|
||||
}
|
||||
@@ -354,7 +357,7 @@ bool Controller::saveAccessTokenToFile(const AccountSettings &account, const QBy
|
||||
|
||||
auto fileDir = QFileInfo(accountTokenFile).dir();
|
||||
if (!((fileDir.exists() || fileDir.mkpath(".")) && accountTokenFile.open(QFile::WriteOnly))) {
|
||||
Q_EMIT errorOccured("I/O Denied", "Cannot save access token.");
|
||||
Q_EMIT errorOccured("I/O Denied: Cannot save access token.");
|
||||
} else {
|
||||
accountTokenFile.write(accessToken);
|
||||
return true;
|
||||
@@ -558,3 +561,32 @@ NeochatDeleteDeviceJob::NeochatDeleteDeviceJob(const QString &deviceId, const Om
|
||||
addParam<IfNotEmpty>(_data, QStringLiteral("auth"), auth);
|
||||
setRequestData(std::move(_data));
|
||||
}
|
||||
|
||||
void Controller::createRoom(const QString &name, const QString &topic)
|
||||
{
|
||||
auto createRoomJob = m_connection->createRoom(Connection::PublishRoom, "", name, topic, QStringList());
|
||||
Quotient::CreateRoomJob::connect(createRoomJob, &CreateRoomJob::failure, [=] {
|
||||
Q_EMIT errorOccured(i18n("Room creation failed: \"%1\"", createRoomJob->errorString()));
|
||||
});
|
||||
Quotient::CreateRoomJob::connect(createRoomJob, &CreateRoomJob::success, [=] {
|
||||
Q_EMIT roomJoined(createRoomJob->roomId());
|
||||
});
|
||||
}
|
||||
|
||||
void Controller::joinRoom(const QString &alias)
|
||||
{
|
||||
if (!alias.contains(":")) {
|
||||
Q_EMIT errorOccured(i18n("The room id you are trying to join is not valid"));
|
||||
return;
|
||||
}
|
||||
|
||||
const auto knownServer = alias.mid(alias.indexOf(":") + 1);
|
||||
auto joinRoomJob = m_connection->joinRoom(alias, QStringList{knownServer});
|
||||
|
||||
connect(joinRoomJob, &JoinRoomJob::failure, [=] {
|
||||
Q_EMIT errorOccured(i18n("Server error when joining the room \"%1\": %2", joinRoomJob->errorString()));
|
||||
});
|
||||
connect(joinRoomJob, &JoinRoomJob::success, [this, joinRoomJob] {
|
||||
Q_EMIT errorOccured(joinRoomJob->roomId());
|
||||
});
|
||||
}
|
||||
|
||||
@@ -72,6 +72,12 @@ public:
|
||||
};
|
||||
Q_ENUM(PasswordStatus);
|
||||
|
||||
/// \brief Create new room for a group chat.
|
||||
void createRoom(const QString &name, const QString &topic);
|
||||
|
||||
/// \brief Join a room.
|
||||
void joinRoom(const QString &alias);
|
||||
|
||||
private:
|
||||
explicit Controller(QObject *parent = nullptr);
|
||||
~Controller() override;
|
||||
@@ -94,7 +100,14 @@ private Q_SLOTS:
|
||||
Q_SIGNALS:
|
||||
void busyChanged();
|
||||
/// Error occurred because of user inputs
|
||||
void errorOccured(QString error, QString detail);
|
||||
void errorOccured(const QString &error);
|
||||
|
||||
/// \brief Emitted when an action made the user join a room.
|
||||
///
|
||||
/// Either when a new room was created, a direct chat was started
|
||||
/// or a group chat was joined. The UI will react to this signal
|
||||
/// and switch to the newly joined room.
|
||||
void roomJoined(const QString &roomName);
|
||||
|
||||
/// Error occurred because of server or bug in NeoChat
|
||||
void globalErrorOccured(QString error, QString detail);
|
||||
|
||||
Reference in New Issue
Block a user