Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Same as previous commit.
Browse files Browse the repository at this point in the history
Charlie Lin committed Jun 5, 2021
1 parent b9e0805 commit 5aef756
Showing 7 changed files with 156 additions and 136 deletions.
158 changes: 87 additions & 71 deletions src/app/Application.cpp
Original file line number Diff line number Diff line change
@@ -4,108 +4,124 @@
#include <QSettings>
#include <QtQml>

Application::Application(QQmlContext *ctx, QObject *parent) : QObject(parent) {
ctx->setContextProperty("qtVersion", QT_VERSION_STR);
ctx->setContextProperty("hmi", this);
ctx->setContextProperty("auth", &m_auth);
ctx->setContextProperty("user", &m_user);
ctx->setContextProperty("settingsModel", &m_settingsModel);
Application::Application(QQmlContext *ctx, QObject *parent) : QObject(parent)
{
ctx->setContextProperty("qtVersion", QT_VERSION_STR);
ctx->setContextProperty("hmi", this);
ctx->setContextProperty("auth", &m_auth);
ctx->setContextProperty("user", &m_user);
ctx->setContextProperty("settingsModel", &m_settingsModel);

qmlRegisterUncreatableType<GuildsModel>("ui", 1, 0, "ViewMode", {});
qmlRegisterUncreatableType<GuildsModel>("ui", 1, 0, "ViewIconDirection", {});
qmlRegisterUncreatableType<GuildsModel>("ui", 1, 0, "ViewMode", {});
qmlRegisterUncreatableType<GuildsModel>("ui", 1, 0, "ViewIconDirection",
{});

connect(&m_auth, &Authenticator::authenticationSuccess, this,
&Application::handleLoginSuccess);
connect(&m_auth, &Authenticator::authenticationSuccess, this,
&Application::handleLoginSuccess);

connect(&m_auth, &Authenticator::authenticationFailed, this,
&Application::handleLoginFailed);
connect(&m_auth, &Authenticator::authenticationFailed, this,
&Application::handleLoginFailed);

connect(&m_auth, &Authenticator::saveSettings, this,
&Application::saveAuthSettings);
connect(&m_auth, &Authenticator::saveSettings, this,
&Application::saveAuthSettings);

connect(&m_req, &Requester::guildsFinished, this,
&Application::handleGuildsFinished);
connect(&m_req, &Requester::guildsFinished, this,
&Application::handleGuildsFinished);

connect(&m_req, &Requester::guildIconBase64, this,
&Application::handleGuildsIcons);
connect(&m_req, &Requester::guildIconBase64, this,
&Application::handleGuildsIcons);

connect(&m_req, &Requester::channelFinished,
[&](const QByteArray &data) { m_user.setChannelsForGuild(data); });
connect(&m_req, &Requester::channelFinished,
[&](const QByteArray &data) { m_user.setChannelsForGuild(data); });

connect(&m_req, &Requester::messagesFinished, this, [&](QByteArray data) {
m_user.setMessagesForChannel(m_guildsModel->selectedID(), data);
});
connect(&m_req, &Requester::messagesFinished, this, [&](QByteArray data) {
m_user.setMessagesForChannel(m_guildsModel->selectedID(), data);
});
}

bool Application::guildModelVisible() const { return m_guildModelVisible; }
bool Application::guildModelVisible() const
{
return m_guildModelVisible;
}

GuildsModel *Application::guildsModel() const { return m_guildsModel.get(); }
GuildsModel *Application::guildsModel() const
{
return m_guildsModel.get();
}

void Application::loadSettings() {
m_settings.loadSettings();
void Application::loadSettings()
{
m_settings.loadSettings();

m_settingsModel.setSettings(&m_settings);
m_settingsModel.setSettings(&m_settings);

const auto t = m_settings.token();
const auto me = m_settings.meInfo();
if (!t.isEmpty() && !me.isEmpty()) {
handleLoginSuccess(t, QJsonDocument::fromJson(me));
}
const auto t = m_settings.token();
const auto me = m_settings.meInfo();
if (!t.isEmpty() && !me.isEmpty()) {
handleLoginSuccess(t, QJsonDocument::fromJson(me));
}
}

void Application::saveSettings() {
m_settings.save();
updateUI();
void Application::saveSettings()
{
m_settings.save();
updateUI();
}

void Application::handleLoginSuccess(const QString &token,
const QJsonDocument &meInfo) {
emit loginSuccess();
m_req.setToken(token);
m_user.populate(meInfo);
m_req.requestGuilds();
const QJsonDocument &meInfo)
{
emit loginSuccess();
m_req.setToken(token);
m_user.populate(meInfo);
m_req.requestGuilds();
}

void Application::handleLoginFailed(const QString &errorNumber,
const QString &errorMessage) {
QString title = "Error(" + errorNumber + ")";
emit showError(title, errorMessage);
const QString &errorMessage)
{
QString title = "Error(" + errorNumber + ")";
emit showError(title, errorMessage);
}

void Application::handleGuildsFinished(const QByteArray &data) {
m_user.setGuilds(data);
m_guildsModel = std::make_unique<GuildsModel>(&m_user.guilds);
m_guildModelVisible = true;
emit guildsModelChanged();
m_req.requestChannels(m_user.guildIDs());
m_req.requestGuildsImages(m_user.pairsGuildIDandHashImg());
updateUI();
void Application::handleGuildsFinished(const QByteArray &data)
{
m_user.setGuilds(data);
m_guildsModel = std::make_unique<GuildsModel>(&m_user.guilds);
m_guildModelVisible = true;
emit guildsModelChanged();
m_req.requestChannels(m_user.guildIDs());
m_req.requestGuildsImages(m_user.pairsGuildIDandHashImg());
updateUI();

connect(m_guildsModel->channelsModel(), &ChannelsModel::requestMessages,
&m_req, &Requester::requestMessages);
connect(m_guildsModel->channelsModel(), &ChannelsModel::requestMessages,
&m_req, &Requester::requestMessages);

connect(m_guildsModel->channelsModel(), &ChannelsModel::sendMessage, &m_req,
&Requester::sendMessage);
connect(m_guildsModel->channelsModel(), &ChannelsModel::sendMessage, &m_req,
&Requester::sendMessage);

connect(&m_user, &User::messagesUpdated, m_guildsModel.get(),
&GuildsModel::updateMessages);
connect(&m_user, &User::messagesUpdated, m_guildsModel.get(),
&GuildsModel::updateMessages);
}

void Application::saveAuthSettings(QString token, QByteArray meInfo) {
m_settings.saveAuthSettings(token, meInfo);
void Application::saveAuthSettings(QString token, QByteArray meInfo)
{
m_settings.saveAuthSettings(token, meInfo);
}

void Application::handleGuildsIcons(snowflake guildID, QByteArray imgBase64) {
m_user.setIconBase64(guildID, std::move(imgBase64));
m_guildsModel->updateGuildIcon();
void Application::handleGuildsIcons(snowflake guildID, QByteArray imgBase64)
{
m_user.setIconBase64(guildID, std::move(imgBase64));
m_guildsModel->updateGuildIcon();
}

void Application::updateUI() {
m_guildsModel->setViewMode(m_settings.guildsViewAsIcon()
? GuildsModel::ViewMode::Icon
: GuildsModel::ViewMode::Text);
m_guildsModel->setViewIconDirection(
m_settings.guildsIconsAsHorizontalDirection()
? GuildsModel::ViewIconDirection::Horizontal
: GuildsModel::ViewIconDirection::Vertical);
void Application::updateUI()
{
m_guildsModel->setViewMode(m_settings.guildsViewAsIcon()
? GuildsModel::ViewMode::Icon
: GuildsModel::ViewMode::Text);
m_guildsModel->setViewIconDirection(
m_settings.guildsIconsAsHorizontalDirection()
? GuildsModel::ViewIconDirection::Horizontal
: GuildsModel::ViewIconDirection::Vertical);
}
2 changes: 1 addition & 1 deletion src/app/main.cpp
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ int main(int argc, char *argv[])
QGuiApplication::setApplicationName("unofficial-discord-client");
QGuiApplication::setOrganizationName("Coding Bunker");
QGuiApplication::setApplicationVersion("0.0.1");
// QGuiApplication;
// QGuiApplication;

QQuickStyle::setStyle("Material");

2 changes: 1 addition & 1 deletion src/core/Settings.cpp
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@ void Settings::loadSettings()
{
QSettings settings(QSettings::Format::NativeFormat,
QSettings::Scope::UserScope, m_settingsFilename);
const auto& token {settings.value(SettingsCategory::token)};
const auto &token{ settings.value(SettingsCategory::token) };
if (token.isValid() && !token.isNull()) {
m_token = token.toString();
m_meInfo = settings.value(SettingsCategory::meInfo).toByteArray();
53 changes: 27 additions & 26 deletions src/gui/ChannelsModel.hpp
Original file line number Diff line number Diff line change
@@ -5,44 +5,45 @@

#include <QAbstractListModel>

class ChannelsModel : public QAbstractListModel {
// clang-format off
class ChannelsModel : public QAbstractListModel
{
// clang-format off
Q_OBJECT
Q_PROPERTY(int selected READ selected NOTIFY selectedChanged)
Q_PROPERTY(MessagesModel* msgModel READ msgModel NOTIFY msgModelChanged)
// clang-format on
// clang-format on

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

void setChannels(QList<Channel> *c);
void setChannels(QList<Channel> *c);

int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index,
int role = Qt::DisplayRole) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index,
int role = Qt::DisplayRole) const override;

enum Role { Name = Qt::UserRole + 1 };
enum Role { Name = Qt::UserRole + 1 };

QHash<int, QByteArray> roleNames() const override;
QHash<int, QByteArray> roleNames() const override;

Q_INVOKABLE void select(int index);
Q_INVOKABLE void sendMsg(QString txt);
Q_INVOKABLE void select(int index);
Q_INVOKABLE void sendMsg(QString txt);

int selected() const;
MessagesModel *msgModel();
int selected() const;
MessagesModel *msgModel();

void updateMessages();
void updateMessages();

signals:
void selectedChanged();
void requestMessages(snowflake channelID);
void msgModelChanged();
void sendMessage(snowflake channelID, QString txt);
signals:
void selectedChanged();
void requestMessages(snowflake channelID);
void msgModelChanged();
void sendMessage(snowflake channelID, QString txt);

private:
QList<Channel> *m_channels{nullptr};
int m_selected{-1};
MessagesModel m_msgModel;
private:
QList<Channel> *m_channels{ nullptr };
int m_selected{ -1 };
MessagesModel m_msgModel;

void resetSelected();
void resetSelected();
};
2 changes: 1 addition & 1 deletion src/gui/MessagesModel.hpp
Original file line number Diff line number Diff line change
@@ -24,5 +24,5 @@ class MessagesModel : public QAbstractListModel
QHash<int, QByteArray> roleNames() const override;

private:
QList<Message>* m_messages{nullptr};
QList<Message> *m_messages{ nullptr };
};
51 changes: 27 additions & 24 deletions src/handlers/Authenticator.cpp
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@
#include <QNetworkReply>
#include <QSettings>

void Authenticator::requestLogin(QString& email, QString& pass, QString& twoFA)
void Authenticator::requestLogin(QString &email, QString &pass, QString &twoFA)
{
QNetworkRequest req(DiscordAPI::login);
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
@@ -19,9 +19,9 @@ void Authenticator::requestLogin(QString& email, QString& pass, QString& twoFA)
data["password"] = pass;
QJsonDocument doc(data);

const auto* reply {m_nam.post(req, doc.toJson())};
const auto *reply{ m_nam.post(req, doc.toJson()) };
connect(reply, &QNetworkReply::finished, this, [this, twoFA]() {
const auto r {qobject_cast<QNetworkReply *>(sender())};
const auto r{ qobject_cast<QNetworkReply *>(sender()) };
r->deleteLater();

if (r->error() != QNetworkReply::NetworkError::NoError) {
@@ -38,22 +38,25 @@ void Authenticator::handleLoginFailure(QNetworkReply::NetworkError error,
QString messageError = "";

if (301 <= error && error <= 399) {
const auto& doc {QJsonDocument::fromJson(body.toLatin1())};
const auto& obj {doc.object()};
const auto &doc{ QJsonDocument::fromJson(body.toLatin1()) };
const auto &obj{ doc.object() };

if (obj.value("email").isArray() || obj.value("password").isArray()) {
if (obj.value("email").isArray()) {
const QJsonArray& emailResponses {obj.value("email").toArray()};
const QJsonArray &emailResponses{
obj.value("email").toArray()
};
messageError += "On email: ";
for (const auto& error : emailResponses) {
for (const auto &error : emailResponses) {
messageError += error.toString() + "\n";
}
}
if (obj.value("password").isArray()) {
const QJsonArray& passwordResponses {
obj.value("password").toArray()};
const QJsonArray &passwordResponses{
obj.value("password").toArray()
};
messageError += "On password: ";
for (const auto& error : passwordResponses) {
for (const auto &error : passwordResponses) {
messageError += error.toString() + "\n";
}
}
@@ -67,13 +70,13 @@ void Authenticator::handleLoginFailure(QNetworkReply::NetworkError error,

void Authenticator::handleLoginResponse(QString body, QString twoFA)
{
const auto& doc {QJsonDocument::fromJson(body.toLatin1())};
const auto& obj {doc.object()};
const auto &doc{ QJsonDocument::fromJson(body.toLatin1()) };
const auto &obj{ doc.object() };

const auto& token {obj.value("token").toString()};
const auto mfa {obj.value("mfa").toBool()};
const auto sms {obj.value("sms").toBool()};
const auto& ticket {obj.value("ticket").toString()};
const auto &token{ obj.value("token").toString() };
const auto mfa{ obj.value("mfa").toBool() };
const auto sms{ obj.value("sms").toBool() };
const auto &ticket{ obj.value("ticket").toString() };

Q_UNUSED(sms) // TODO: handle sms

@@ -103,13 +106,13 @@ void Authenticator::request2FA(QString ticket, QString mfa)
data["ticket"] = ticket;
QJsonDocument doc(data);

const auto* reply {m_nam.post(req, doc.toJson())};
const auto *reply{ m_nam.post(req, doc.toJson()) };
connect(reply, &QNetworkReply::finished, this, [&]() {
const auto r {qobject_cast<QNetworkReply *>(sender())};
const auto r{ qobject_cast<QNetworkReply *>(sender()) };
r->deleteLater();
const auto& doc {QJsonDocument::fromJson(r->readAll())};
const auto& obj {doc.object()};
m_token = obj.value("token").toString();
const auto &doc{ QJsonDocument::fromJson(r->readAll()) };
const auto &obj{ doc.object() };
m_token = obj.value("token").toString();
handlePersonInfo();
});
}
@@ -120,11 +123,11 @@ void Authenticator::handlePersonInfo()
req.setRawHeader("authorization", m_token.toLatin1());
req.setRawHeader("User-Agent", DiscordAPI::fakeBrowserUserAgent);

const auto* reply {m_nam.get(req)};
const auto *reply{ m_nam.get(req) };
connect(reply, &QNetworkReply::finished, this, [&]() {
const auto r {qobject_cast<QNetworkReply *>(sender())};
const auto r{ qobject_cast<QNetworkReply *>(sender()) };
r->deleteLater();
const auto& info {QJsonDocument::fromJson(r->readAll())};
const auto &info{ QJsonDocument::fromJson(r->readAll()) };
emit saveSettings(m_token, info.toJson());
emit authenticationSuccess(m_token, info);
});
24 changes: 12 additions & 12 deletions src/handlers/Requester.cpp
Original file line number Diff line number Diff line change
@@ -15,9 +15,9 @@ void Requester::setToken(const QString &token)

void Requester::requestGuilds()
{
const auto* reply {request(DiscordAPI::guilds)};
const auto *reply{ request(DiscordAPI::guilds) };
connect(reply, &QNetworkReply::finished, this, [&]() {
const auto r {qobject_cast<QNetworkReply *>(sender())};
const auto r{ qobject_cast<QNetworkReply *>(sender()) };
r->deleteLater();
emit guildsFinished(r->readAll());
});
@@ -26,9 +26,9 @@ void Requester::requestGuilds()
void Requester::requestChannels(const QList<snowflake> &guildIDs)
{
for (auto id : guildIDs) {
const auto* reply {request(DiscordAPI::channels.arg(id))};
const auto *reply{ request(DiscordAPI::channels.arg(id)) };
connect(reply, &QNetworkReply::finished, this, [&]() {
const auto r {qobject_cast<QNetworkReply *>(sender())};
const auto r{ qobject_cast<QNetworkReply *>(sender()) };
r->deleteLater();
emit channelFinished(r->readAll());
});
@@ -42,10 +42,10 @@ void Requester::requestGuildsImages(QList<QPair<snowflake, QString>> &&items)
continue;
}

const auto* reply
{request(DiscordAPI::guildIcon.arg(i.first).arg(i.second))};
const auto *reply{ request(
DiscordAPI::guildIcon.arg(i.first).arg(i.second)) };
connect(reply, &QNetworkReply::finished, this, [this, i]() {
const auto r {qobject_cast<QNetworkReply *>(sender())};
const auto r{ qobject_cast<QNetworkReply *>(sender()) };
r->deleteLater();
emit guildIconBase64(i.first, r->readAll());
});
@@ -54,9 +54,9 @@ void Requester::requestGuildsImages(QList<QPair<snowflake, QString>> &&items)

void Requester::requestMessages(snowflake channelID)
{
const auto* reply {request(DiscordAPI::messages.arg(channelID))};
const auto *reply{ request(DiscordAPI::messages.arg(channelID)) };
connect(reply, &QNetworkReply::finished, this, [&]() {
const auto r {qobject_cast<QNetworkReply *>(sender())};
const auto r{ qobject_cast<QNetworkReply *>(sender()) };
const auto &tmp{ QJsonDocument::fromJson(r->readAll()) };
qDebug().noquote() << tmp.toJson(QJsonDocument::Indented);
r->deleteLater();
@@ -75,7 +75,7 @@ void Requester::sendMessage(snowflake channelID, QString txt)
data["content"] = txt;
QJsonDocument doc(data);

const auto post {m_nam.post(msg, doc.toJson())};
const auto post{ m_nam.post(msg, doc.toJson()) };
connect(post, &QNetworkReply::finished, this, [=]() {
if (post->error() != QNetworkReply::NoError) {
qDebug() << post->errorString();
@@ -86,8 +86,8 @@ void Requester::sendMessage(snowflake channelID, QString txt)

QNetworkReply *Requester::request(const QString &api)
{
QUrl url{api};
QNetworkRequest req{url};
QUrl url{ api };
QNetworkRequest req{ url };
req.setRawHeader("authorization", m_token.toLatin1());
return m_nam.get(req);
}

0 comments on commit 5aef756

Please sign in to comment.