Skip to content

Commit

Permalink
Same as previous commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
Charlie Lin committed Jun 6, 2021
1 parent 2dbde32 commit 008370d
Show file tree
Hide file tree
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
Expand Up @@ -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
Expand Up @@ -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");

Expand Down
2 changes: 1 addition & 1 deletion src/core/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
53 changes: 27 additions & 26 deletions src/gui/ChannelsModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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 };
};
Loading

0 comments on commit 008370d

Please sign in to comment.