Code cleanup
This commit is contained in:
@@ -15,7 +15,10 @@ class AccountListModel : public QAbstractListModel
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
enum EventRoles { UserRole = Qt::UserRole + 1, ConnectionRole };
|
||||
enum EventRoles {
|
||||
UserRole = Qt::UserRole + 1,
|
||||
ConnectionRole,
|
||||
};
|
||||
|
||||
AccountListModel(QObject *parent = nullptr);
|
||||
|
||||
|
||||
@@ -52,9 +52,7 @@ Controller::Controller(QObject *parent)
|
||||
|
||||
connect(&m_ncm, &QNetworkConfigurationManager::onlineStateChanged, this, &Controller::isOnlineChanged);
|
||||
|
||||
QTimer::singleShot(0, this, [=] {
|
||||
invokeLogin();
|
||||
});
|
||||
QTimer::singleShot(0, this, [=] { invokeLogin(); });
|
||||
}
|
||||
|
||||
Controller::~Controller()
|
||||
@@ -65,6 +63,12 @@ Controller::~Controller()
|
||||
}
|
||||
}
|
||||
|
||||
Controller &Controller::instance()
|
||||
{
|
||||
static Controller _instance;
|
||||
return _instance;
|
||||
}
|
||||
|
||||
inline QString accessTokenFileName(const AccountSettings &account)
|
||||
{
|
||||
QString fileName = account.userId();
|
||||
@@ -103,12 +107,8 @@ void Controller::loginWithCredentials(QString serverAddr, QString user, QString
|
||||
addConnection(conn);
|
||||
setConnection(conn);
|
||||
});
|
||||
connect(conn, &Connection::networkError, [=](QString error, QString, int, int) {
|
||||
Q_EMIT errorOccured("Network Error", error);
|
||||
});
|
||||
connect(conn, &Connection::loginError, [=](QString error, QString) {
|
||||
Q_EMIT errorOccured("Login Failed", error);
|
||||
});
|
||||
connect(conn, &Connection::networkError, [=](QString error, QString, int, int) { Q_EMIT errorOccured("Network Error", error); });
|
||||
connect(conn, &Connection::loginError, [=](QString error, QString) { Q_EMIT errorOccured("Login Failed", error); });
|
||||
}
|
||||
|
||||
void Controller::loginWithAccessToken(QString serverAddr, QString user, QString token, QString deviceName)
|
||||
@@ -137,9 +137,7 @@ void Controller::loginWithAccessToken(QString serverAddr, QString user, QString
|
||||
addConnection(conn);
|
||||
setConnection(conn);
|
||||
});
|
||||
connect(conn, &Connection::networkError, this, [=](QString error, QString, int, int) {
|
||||
Q_EMIT errorOccured("Network Error", error);
|
||||
});
|
||||
connect(conn, &Connection::networkError, this, [=](QString error, QString, int, int) { Q_EMIT errorOccured("Network Error", error); });
|
||||
conn->connectWithToken(user, token, deviceName);
|
||||
}
|
||||
|
||||
@@ -177,9 +175,7 @@ void Controller::logout(Connection *conn)
|
||||
else
|
||||
setConnection(nullptr);
|
||||
});
|
||||
connect(logoutJob, &LogoutJob::failure, this, [=] {
|
||||
Q_EMIT errorOccured("Server-side Logout Failed", logoutJob->errorString());
|
||||
});
|
||||
connect(logoutJob, &LogoutJob::failure, this, [=] { Q_EMIT errorOccured("Server-side Logout Failed", logoutJob->errorString()); });
|
||||
}
|
||||
|
||||
void Controller::addConnection(Connection *c)
|
||||
@@ -198,9 +194,7 @@ void Controller::addConnection(Connection *c)
|
||||
c->sync(30000);
|
||||
c->saveState();
|
||||
});
|
||||
connect(c, &Connection::loggedOut, this, [=] {
|
||||
dropConnection(c);
|
||||
});
|
||||
connect(c, &Connection::loggedOut, this, [=] { dropConnection(c); });
|
||||
connect(&m_ncm, &QNetworkConfigurationManager::onlineStateChanged, [=](bool status) {
|
||||
if (!status) {
|
||||
return;
|
||||
@@ -210,8 +204,6 @@ void Controller::addConnection(Connection *c)
|
||||
c->sync(30000);
|
||||
});
|
||||
|
||||
using namespace Quotient;
|
||||
|
||||
setBusy(true);
|
||||
|
||||
c->sync();
|
||||
@@ -230,7 +222,6 @@ void Controller::dropConnection(Connection *c)
|
||||
|
||||
void Controller::invokeLogin()
|
||||
{
|
||||
using namespace Quotient;
|
||||
const auto accounts = SettingsGroup("Accounts").childGroups();
|
||||
for (const auto &accountId : accounts) {
|
||||
AccountSettings account {accountId};
|
||||
@@ -246,9 +237,7 @@ void Controller::invokeLogin()
|
||||
Q_EMIT errorOccured("Login Failed", error);
|
||||
logout(c);
|
||||
});
|
||||
connect(c, &Connection::networkError, this, [=](QString error, QString, int, int) {
|
||||
Q_EMIT errorOccured("Network Error", error);
|
||||
});
|
||||
connect(c, &Connection::networkError, this, [=](QString error, QString, int, int) { Q_EMIT errorOccured("Network Error", error); });
|
||||
c->connectWithToken(account.userId(), accessToken, account.deviceId());
|
||||
}
|
||||
}
|
||||
@@ -368,25 +357,19 @@ void Controller::joinRoom(Connection *c, const QString &alias)
|
||||
|
||||
auto knownServer = alias.mid(alias.indexOf(":") + 1);
|
||||
auto joinRoomJob = c->joinRoom(alias, QStringList {knownServer});
|
||||
joinRoomJob->connect(joinRoomJob, &JoinRoomJob::failure, [=] {
|
||||
Q_EMIT errorOccured("Join Room Failed", joinRoomJob->errorString());
|
||||
});
|
||||
joinRoomJob->connect(joinRoomJob, &JoinRoomJob::failure, [=] { Q_EMIT errorOccured("Join Room Failed", joinRoomJob->errorString()); });
|
||||
}
|
||||
|
||||
void Controller::createRoom(Connection *c, const QString &name, const QString &topic)
|
||||
{
|
||||
auto createRoomJob = c->createRoom(Connection::PublishRoom, "", name, topic, QStringList());
|
||||
createRoomJob->connect(createRoomJob, &CreateRoomJob::failure, [=] {
|
||||
Q_EMIT errorOccured("Create Room Failed", createRoomJob->errorString());
|
||||
});
|
||||
createRoomJob->connect(createRoomJob, &CreateRoomJob::failure, [=] { Q_EMIT errorOccured("Create Room Failed", createRoomJob->errorString()); });
|
||||
}
|
||||
|
||||
void Controller::createDirectChat(Connection *c, const QString &userID)
|
||||
{
|
||||
auto createRoomJob = c->createDirectChat(userID);
|
||||
createRoomJob->connect(createRoomJob, &CreateRoomJob::failure, [=] {
|
||||
Q_EMIT errorOccured("Create Direct Chat Failed", createRoomJob->errorString());
|
||||
});
|
||||
createRoomJob->connect(createRoomJob, &CreateRoomJob::failure, [=] { Q_EMIT errorOccured("Create Direct Chat Failed", createRoomJob->errorString()); });
|
||||
}
|
||||
|
||||
void Controller::playAudio(QUrl localFile)
|
||||
@@ -394,18 +377,14 @@ void Controller::playAudio(QUrl localFile)
|
||||
auto player = new QMediaPlayer;
|
||||
player->setMedia(localFile);
|
||||
player->play();
|
||||
connect(player, &QMediaPlayer::stateChanged, [=] {
|
||||
player->deleteLater();
|
||||
});
|
||||
connect(player, &QMediaPlayer::stateChanged, [=] { player->deleteLater(); });
|
||||
}
|
||||
|
||||
void Controller::changeAvatar(Connection *conn, QUrl localFile)
|
||||
{
|
||||
auto job = conn->uploadFile(localFile.toLocalFile());
|
||||
if (isJobRunning(job)) {
|
||||
connect(job, &BaseJob::success, this, [conn, job] {
|
||||
conn->callApi<SetAvatarUrlJob>(conn->userId(), job->contentUri());
|
||||
});
|
||||
connect(job, &BaseJob::success, this, [conn, job] { conn->callApi<SetAvatarUrlJob>(conn->userId(), job->contentUri()); });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -432,7 +411,7 @@ void Controller::changePassword(Connection *connection, const QString ¤tPa
|
||||
{
|
||||
NeochatChangePasswordJob *job = connection->callApi<NeochatChangePasswordJob>(newPassword, false);
|
||||
connect(job, &BaseJob::result, this, [this, job, currentPassword, newPassword, connection] {
|
||||
if(job->error() == 103) {
|
||||
if (job->error() == 103) {
|
||||
QJsonObject replyData = job->jsonData();
|
||||
QJsonObject authData;
|
||||
authData["session"] = replyData["session"];
|
||||
@@ -442,11 +421,9 @@ void Controller::changePassword(Connection *connection, const QString ¤tPa
|
||||
QJsonObject identifier = {{"type", "m.id.user"}, {"user", connection->user()->id()}};
|
||||
authData["identifier"] = identifier;
|
||||
NeochatChangePasswordJob *innerJob = connection->callApi<NeochatChangePasswordJob>(newPassword, false, authData);
|
||||
connect(innerJob, &BaseJob::success, this, [this]() {
|
||||
Q_EMIT passwordStatus(PasswordStatus::Success);
|
||||
});
|
||||
connect(innerJob, &BaseJob::failure, this, [innerJob, this] () {
|
||||
if(innerJob->jsonData()["errcode"] == "M_FORBIDDEN") {
|
||||
connect(innerJob, &BaseJob::success, this, [this]() { Q_EMIT passwordStatus(PasswordStatus::Success); });
|
||||
connect(innerJob, &BaseJob::failure, this, [innerJob, this]() {
|
||||
if (innerJob->jsonData()["errcode"] == "M_FORBIDDEN") {
|
||||
Q_EMIT passwordStatus(PasswordStatus::Wrong);
|
||||
} else {
|
||||
Q_EMIT passwordStatus(PasswordStatus::Other);
|
||||
@@ -456,11 +433,8 @@ void Controller::changePassword(Connection *connection, const QString ¤tPa
|
||||
});
|
||||
}
|
||||
|
||||
NeochatChangePasswordJob::NeochatChangePasswordJob(const QString& newPassword,
|
||||
bool logoutDevices,
|
||||
const Omittable<QJsonObject>& auth)
|
||||
: BaseJob(HttpVerb::Post, QStringLiteral("ChangePasswordJob"),
|
||||
QStringLiteral("/_matrix/client/r0") % "/account/password")
|
||||
NeochatChangePasswordJob::NeochatChangePasswordJob(const QString &newPassword, bool logoutDevices, const Omittable<QJsonObject> &auth)
|
||||
: BaseJob(HttpVerb::Post, QStringLiteral("ChangePasswordJob"), QStringLiteral("/_matrix/client/r0") % "/account/password")
|
||||
{
|
||||
QJsonObject _data;
|
||||
addParam<>(_data, QStringLiteral("new_password"), newPassword);
|
||||
@@ -468,3 +442,60 @@ NeochatChangePasswordJob::NeochatChangePasswordJob(const QString& newPassword,
|
||||
addParam<IfNotEmpty>(_data, QStringLiteral("auth"), auth);
|
||||
setRequestData(std::move(_data));
|
||||
}
|
||||
|
||||
QVector<Connection *> Controller::connections() const
|
||||
{
|
||||
return m_connections;
|
||||
}
|
||||
|
||||
int Controller::accountCount() const
|
||||
{
|
||||
return m_connections.count();
|
||||
}
|
||||
|
||||
bool Controller::quitOnLastWindowClosed() const
|
||||
{
|
||||
return QApplication::quitOnLastWindowClosed();
|
||||
}
|
||||
|
||||
void Controller::setQuitOnLastWindowClosed(bool value)
|
||||
{
|
||||
if (quitOnLastWindowClosed() != value) {
|
||||
QApplication::setQuitOnLastWindowClosed(value);
|
||||
Q_EMIT quitOnLastWindowClosedChanged();
|
||||
}
|
||||
}
|
||||
|
||||
bool Controller::isOnline() const
|
||||
{
|
||||
return m_ncm.isOnline();
|
||||
}
|
||||
|
||||
bool Controller::busy() const
|
||||
{
|
||||
return m_busy;
|
||||
}
|
||||
|
||||
void Controller::setBusy(bool busy)
|
||||
{
|
||||
if (m_busy == busy) {
|
||||
return;
|
||||
}
|
||||
m_busy = busy;
|
||||
Q_EMIT busyChanged();
|
||||
}
|
||||
|
||||
Connection *Controller::connection() const
|
||||
{
|
||||
if (m_connection.isNull())
|
||||
return nullptr;
|
||||
return m_connection;
|
||||
}
|
||||
|
||||
void Controller::setConnection(Connection *connection)
|
||||
{
|
||||
if (connection == m_connection)
|
||||
return;
|
||||
m_connection = connection;
|
||||
Q_EMIT connectionChanged();
|
||||
}
|
||||
|
||||
@@ -34,78 +34,30 @@ class Controller : public QObject
|
||||
Q_PROPERTY(KAboutData aboutData READ aboutData WRITE setAboutData NOTIFY aboutDataChanged)
|
||||
|
||||
public:
|
||||
static Controller &instance()
|
||||
{
|
||||
static Controller _instance;
|
||||
return _instance;
|
||||
}
|
||||
static Controller &instance();
|
||||
|
||||
QVector<Connection *> connections() const;
|
||||
|
||||
void setConnection(Connection *conn);
|
||||
Connection *connection() const;
|
||||
|
||||
void addConnection(Connection *c);
|
||||
void dropConnection(Connection *c);
|
||||
|
||||
Q_INVOKABLE void loginWithCredentials(QString, QString, QString, QString);
|
||||
Q_INVOKABLE void loginWithAccessToken(QString, QString, QString, QString);
|
||||
|
||||
Q_INVOKABLE void changePassword(Connection *connection, const QString ¤tPassword, const QString &newPassword);
|
||||
|
||||
QVector<Connection *> connections() const
|
||||
{
|
||||
return m_connections;
|
||||
}
|
||||
int accountCount() const;
|
||||
|
||||
// All the non-Q_INVOKABLE functions.
|
||||
void addConnection(Connection *c);
|
||||
void dropConnection(Connection *c);
|
||||
bool isOnline() const;
|
||||
|
||||
// All the Q_PROPERTYs.
|
||||
int accountCount()
|
||||
{
|
||||
return m_connections.count();
|
||||
}
|
||||
bool quitOnLastWindowClosed() const;
|
||||
void setQuitOnLastWindowClosed(bool value);
|
||||
|
||||
bool quitOnLastWindowClosed() const
|
||||
{
|
||||
return QApplication::quitOnLastWindowClosed();
|
||||
}
|
||||
void setQuitOnLastWindowClosed(bool value)
|
||||
{
|
||||
if (quitOnLastWindowClosed() != value) {
|
||||
QApplication::setQuitOnLastWindowClosed(value);
|
||||
|
||||
Q_EMIT quitOnLastWindowClosedChanged();
|
||||
}
|
||||
}
|
||||
|
||||
bool isOnline() const
|
||||
{
|
||||
return m_ncm.isOnline();
|
||||
}
|
||||
|
||||
bool busy() const
|
||||
{
|
||||
return m_busy;
|
||||
}
|
||||
void setBusy(bool busy)
|
||||
{
|
||||
if (m_busy == busy) {
|
||||
return;
|
||||
}
|
||||
m_busy = busy;
|
||||
Q_EMIT busyChanged();
|
||||
}
|
||||
|
||||
Connection *connection() const
|
||||
{
|
||||
if (m_connection.isNull())
|
||||
return nullptr;
|
||||
|
||||
return m_connection;
|
||||
}
|
||||
|
||||
void setConnection(Connection *conn)
|
||||
{
|
||||
if (conn == m_connection)
|
||||
return;
|
||||
m_connection = conn;
|
||||
Q_EMIT connectionChanged();
|
||||
}
|
||||
bool busy() const;
|
||||
void setBusy(bool busy);
|
||||
|
||||
void setAboutData(KAboutData aboutData);
|
||||
KAboutData aboutData() const;
|
||||
@@ -113,7 +65,7 @@ public:
|
||||
enum PasswordStatus {
|
||||
Success,
|
||||
Wrong,
|
||||
Other
|
||||
Other,
|
||||
};
|
||||
Q_ENUM(PasswordStatus);
|
||||
|
||||
@@ -164,12 +116,11 @@ public Q_SLOTS:
|
||||
void markAllMessagesAsRead(Quotient::Connection *conn);
|
||||
};
|
||||
|
||||
//TODO libQuotient 0.7: Drop
|
||||
class NeochatChangePasswordJob : public BaseJob {
|
||||
// TODO libQuotient 0.7: Drop
|
||||
class NeochatChangePasswordJob : public BaseJob
|
||||
{
|
||||
public:
|
||||
explicit NeochatChangePasswordJob(const QString& newPassword,
|
||||
bool logoutDevices,
|
||||
const Omittable<QJsonObject>& auth = none);
|
||||
explicit NeochatChangePasswordJob(const QString &newPassword, bool logoutDevices, const Omittable<QJsonObject> &auth = none);
|
||||
};
|
||||
|
||||
#endif // CONTROLLER_H
|
||||
|
||||
Reference in New Issue
Block a user