mirror-chatterino2/src/Application.cpp

432 lines
13 KiB
C++
Raw Normal View History

2018-06-26 14:09:39 +02:00
#include "Application.hpp"
2019-09-22 10:53:39 +02:00
#include <atomic>
#include "common/Args.hpp"
#include "common/QLogging.hpp"
#include "common/Version.hpp"
2018-06-26 14:09:39 +02:00
#include "controllers/accounts/AccountController.hpp"
#include "controllers/commands/CommandController.hpp"
#include "controllers/hotkeys/HotkeyController.hpp"
2018-06-26 14:09:39 +02:00
#include "controllers/ignores/IgnoreController.hpp"
#include "controllers/notifications/NotificationController.hpp"
#include "debug/AssertInGuiThread.hpp"
2018-08-07 01:35:24 +02:00
#include "messages/MessageBuilder.hpp"
2018-08-02 14:23:27 +02:00
#include "providers/bttv/BttvEmotes.hpp"
#include "providers/chatterino/ChatterinoBadges.hpp"
#include "providers/ffz/FfzBadges.hpp"
2018-08-02 14:23:27 +02:00
#include "providers/ffz/FfzEmotes.hpp"
#include "providers/irc/Irc2.hpp"
2018-07-06 19:23:47 +02:00
#include "providers/twitch/PubsubClient.hpp"
#include "providers/twitch/TwitchIrcServer.hpp"
#include "providers/twitch/TwitchMessageBuilder.hpp"
#include "singletons/Emotes.hpp"
2018-06-28 19:46:45 +02:00
#include "singletons/Fonts.hpp"
#include "singletons/Logging.hpp"
#include "singletons/NativeMessaging.hpp"
#include "singletons/Paths.hpp"
#include "singletons/Resources.hpp"
#include "singletons/Settings.hpp"
2018-06-28 20:03:04 +02:00
#include "singletons/Theme.hpp"
2018-08-11 12:47:03 +02:00
#include "singletons/Toasts.hpp"
2019-09-02 10:52:01 +02:00
#include "singletons/Updates.hpp"
2018-06-26 14:09:39 +02:00
#include "singletons/WindowManager.hpp"
2018-06-27 13:03:38 +02:00
#include "util/IsBigEndian.hpp"
2018-06-26 14:09:39 +02:00
#include "util/PostToThread.hpp"
#include "util/RapidjsonHelpers.hpp"
2019-09-22 15:32:36 +02:00
#include "widgets/Notebook.hpp"
#include "widgets/Window.hpp"
2019-09-22 15:32:36 +02:00
#include "widgets/splits/Split.hpp"
#include <QDesktopServices>
namespace chatterino {
static std::atomic<bool> isAppInitialized{false};
2018-08-02 14:23:27 +02:00
Application *Application::instance = nullptr;
// this class is responsible for handling the workflow of Chatterino
2018-08-06 21:17:03 +02:00
// It will create the instances of the major classes, and connect their signals
// to each other
2018-08-02 14:23:27 +02:00
Application::Application(Settings &_settings, Paths &_paths)
: themes(&this->emplace<Theme>())
2018-08-02 14:23:27 +02:00
, fonts(&this->emplace<Fonts>())
, emotes(&this->emplace<Emotes>())
, accounts(&this->emplace<AccountController>())
, hotkeys(&this->emplace<HotkeyController>())
2018-08-02 14:23:27 +02:00
, windows(&this->emplace<WindowManager>())
2018-08-19 15:09:00 +02:00
, toasts(&this->emplace<Toasts>())
2018-08-19 19:02:49 +02:00
2018-08-02 14:23:27 +02:00
, commands(&this->emplace<CommandController>())
, notifications(&this->emplace<NotificationController>())
, twitch(&this->emplace<TwitchIrcServer>())
, chatterinoBadges(&this->emplace<ChatterinoBadges>())
, ffzBadges(&this->emplace<FfzBadges>())
2018-08-02 14:23:27 +02:00
, logging(&this->emplace<Logging>())
{
2018-08-02 14:23:27 +02:00
this->instance = this;
this->fonts->fontChanged.connect([this]() {
this->windows->layoutChannelViews();
});
}
2018-08-02 14:23:27 +02:00
void Application::initialize(Settings &settings, Paths &paths)
{
assert(isAppInitialized == false);
isAppInitialized = true;
// Show changelog
if (!getArgs().isFramelessEmbed &&
getSettings()->currentVersion.getValue() != "" &&
getSettings()->currentVersion.getValue() != CHATTERINO_VERSION)
{
auto box = new QMessageBox(QMessageBox::Information, "Chatterino 2",
"Show changelog?",
QMessageBox::Yes | QMessageBox::No);
box->setAttribute(Qt::WA_DeleteOnClose);
if (box->exec() == QMessageBox::Yes)
{
QDesktopServices::openUrl(
QUrl("https://www.chatterino.com/changelog"));
}
}
if (!getArgs().isFramelessEmbed)
{
getSettings()->currentVersion.setValue(CHATTERINO_VERSION);
if (getSettings()->enableExperimentalIrc)
{
Irc::instance().load();
}
}
2019-09-14 20:45:01 +02:00
2018-10-21 13:43:02 +02:00
for (auto &singleton : this->singletons_)
{
2018-08-02 14:23:27 +02:00
singleton->initialize(settings, paths);
2018-07-07 11:41:01 +02:00
}
2019-09-22 15:32:36 +02:00
// add crash message
if (!getArgs().isFramelessEmbed && getArgs().crashRecovery)
2019-09-22 15:32:36 +02:00
{
if (auto selected =
this->windows->getMainWindow().getNotebook().getSelectedPage())
{
if (auto container = dynamic_cast<SplitContainer *>(selected))
{
for (auto &&split : container->getSplits())
{
if (auto channel = split->getChannel(); !channel->isEmpty())
{
channel->addMessage(makeSystemMessage(
"Chatterino unexpectedly crashed and restarted. "
"You can disable automatic restarts in the "
"settings."));
}
}
}
}
}
this->windows->updateWordTypeMask();
if (!getArgs().isFramelessEmbed)
{
this->initNm(paths);
}
2018-08-02 14:23:27 +02:00
this->initPubsub();
}
int Application::run(QApplication &qtApp)
{
assert(isAppInitialized);
this->twitch->connect();
2018-08-02 14:23:27 +02:00
if (!getArgs().isFramelessEmbed)
{
this->windows->getMainWindow().show();
}
2018-08-02 14:23:27 +02:00
getSettings()->betaUpdates.connect(
[] {
Updates::instance().checkForUpdates();
},
false);
getSettings()->moderationActions.delayedItemsChanged.connect([this] {
this->windows->forceLayoutChannelViews();
});
2019-09-02 10:52:01 +02:00
getSettings()->highlightedMessages.delayedItemsChanged.connect([this] {
this->windows->forceLayoutChannelViews();
});
getSettings()->highlightedUsers.delayedItemsChanged.connect([this] {
this->windows->forceLayoutChannelViews();
});
getSettings()->removeSpacesBetweenEmotes.connect([this] {
this->windows->forceLayoutChannelViews();
});
2018-08-02 14:23:27 +02:00
return qtApp.exec();
}
void Application::save()
{
2018-10-21 13:43:02 +02:00
for (auto &singleton : this->singletons_)
{
2018-08-02 14:23:27 +02:00
singleton->save();
}
}
2018-09-17 12:51:16 +02:00
void Application::initNm(Paths &paths)
2018-08-02 14:23:27 +02:00
{
2019-09-08 18:06:43 +02:00
(void)paths;
#ifdef Q_OS_WIN
2019-08-19 23:13:20 +02:00
# if defined QT_NO_DEBUG || defined C_DEBUG_NM
2018-09-17 12:51:16 +02:00
registerNmHost(paths);
this->nmServer.start();
2018-08-15 22:46:20 +02:00
# endif
#endif
2018-08-02 14:23:27 +02:00
}
2018-08-02 14:23:27 +02:00
void Application::initPubsub()
{
this->twitch->pubsub->signals_.moderation.chatCleared.connect(
2018-08-06 21:17:03 +02:00
[this](const auto &action) {
auto chan = this->twitch->getChannelOrEmptyByID(action.roomID);
2018-10-21 13:43:02 +02:00
if (chan->isEmpty())
{
2018-08-06 21:17:03 +02:00
return;
}
2018-04-29 13:24:37 +02:00
2018-08-06 21:17:03 +02:00
QString text =
QString("%1 cleared the chat").arg(action.source.login);
2018-04-29 13:24:37 +02:00
2018-08-07 01:35:24 +02:00
auto msg = makeSystemMessage(text);
postToThread([chan, msg] {
chan->addMessage(msg);
});
2018-08-06 21:17:03 +02:00
});
this->twitch->pubsub->signals_.moderation.modeChanged.connect(
2018-08-06 21:17:03 +02:00
[this](const auto &action) {
auto chan = this->twitch->getChannelOrEmptyByID(action.roomID);
2018-10-21 13:43:02 +02:00
if (chan->isEmpty())
{
2018-08-06 21:17:03 +02:00
return;
}
2018-04-29 13:24:37 +02:00
2018-08-06 21:17:03 +02:00
QString text =
QString("%1 turned %2 %3 mode")
.arg(action.source.login)
2018-08-06 21:17:03 +02:00
.arg(action.state == ModeChangedAction::State::On ? "on"
: "off")
.arg(action.getModeName());
2018-04-29 13:24:37 +02:00
2018-10-21 13:43:02 +02:00
if (action.duration > 0)
{
text += QString(" (%1 seconds)").arg(action.duration);
2018-08-06 21:17:03 +02:00
}
2018-04-29 13:24:37 +02:00
2018-08-07 01:35:24 +02:00
auto msg = makeSystemMessage(text);
postToThread([chan, msg] {
chan->addMessage(msg);
});
2018-08-06 21:17:03 +02:00
});
this->twitch->pubsub->signals_.moderation.moderationStateChanged.connect(
2018-06-26 17:42:35 +02:00
[this](const auto &action) {
auto chan = this->twitch->getChannelOrEmptyByID(action.roomID);
2018-10-21 13:43:02 +02:00
if (chan->isEmpty())
{
2018-06-26 17:42:35 +02:00
return;
}
2018-04-29 13:24:37 +02:00
2018-06-26 17:42:35 +02:00
QString text;
2018-04-29 13:24:37 +02:00
text = QString("%1 %2 %3")
.arg(action.source.login,
(action.modded ? "modded" : "unmodded"),
action.target.login);
2018-04-29 13:24:37 +02:00
2018-08-07 01:35:24 +02:00
auto msg = makeSystemMessage(text);
postToThread([chan, msg] {
chan->addMessage(msg);
});
2018-06-26 17:42:35 +02:00
});
this->twitch->pubsub->signals_.moderation.userBanned.connect(
2018-08-06 21:17:03 +02:00
[&](const auto &action) {
auto chan = this->twitch->getChannelOrEmptyByID(action.roomID);
2018-10-21 13:43:02 +02:00
if (chan->isEmpty())
{
2018-08-06 21:17:03 +02:00
return;
}
postToThread([chan, action] {
MessageBuilder msg(action);
msg->flags.set(MessageFlag::PubSub);
chan->addOrReplaceTimeout(msg.release());
2018-08-07 01:35:24 +02:00
});
2018-08-06 21:17:03 +02:00
});
this->twitch->pubsub->signals_.moderation.messageDeleted.connect(
[&](const auto &action) {
auto chan = this->twitch->getChannelOrEmptyByID(action.roomID);
if (chan->isEmpty() || getSettings()->hideDeletionActions)
{
return;
}
MessageBuilder msg;
TwitchMessageBuilder::deletionMessage(action, &msg);
msg->flags.set(MessageFlag::PubSub);
postToThread([chan, msg = msg.release()] {
auto replaced = false;
LimitedQueueSnapshot<MessagePtr> snapshot =
chan->getMessageSnapshot();
int snapshotLength = snapshot.size();
// without parens it doesn't build on windows
int end = (std::max)(0, snapshotLength - 200);
for (int i = snapshotLength - 1; i >= end; --i)
{
auto &s = snapshot[i];
if (!s->flags.has(MessageFlag::PubSub) &&
s->timeoutUser == msg->timeoutUser)
{
chan->replaceMessage(s, msg);
replaced = true;
break;
}
}
if (!replaced)
{
chan->addMessage(msg);
}
});
});
this->twitch->pubsub->signals_.moderation.userUnbanned.connect(
2018-08-06 21:17:03 +02:00
[&](const auto &action) {
auto chan = this->twitch->getChannelOrEmptyByID(action.roomID);
2018-10-21 13:43:02 +02:00
if (chan->isEmpty())
{
2018-08-06 21:17:03 +02:00
return;
}
2018-08-07 01:35:24 +02:00
auto msg = MessageBuilder(action).release();
postToThread([chan, msg] {
chan->addMessage(msg);
});
2018-08-06 21:17:03 +02:00
});
this->twitch->pubsub->signals_.moderation.automodMessage.connect(
[&](const auto &action) {
auto chan = this->twitch->getChannelOrEmptyByID(action.roomID);
if (chan->isEmpty())
{
return;
}
postToThread([chan, action] {
const auto p = makeAutomodMessage(action);
chan->addMessage(p.first);
chan->addMessage(p.second);
});
});
this->twitch->pubsub->signals_.moderation.automodUserMessage.connect(
[&](const auto &action) {
auto chan = this->twitch->getChannelOrEmptyByID(action.roomID);
if (chan->isEmpty())
{
return;
}
auto msg = MessageBuilder(action).release();
postToThread([chan, msg] {
chan->addMessage(msg);
});
chan->deleteMessage(msg->id);
});
this->twitch->pubsub->signals_.moderation.automodInfoMessage.connect(
[&](const auto &action) {
auto chan = this->twitch->getChannelOrEmptyByID(action.roomID);
if (chan->isEmpty())
{
return;
}
postToThread([chan, action] {
const auto p = makeAutomodInfoMessage(action);
chan->addMessage(p);
});
});
this->twitch->pubsub->signals_.pointReward.redeemed.connect(
[&](auto &data) {
QString channelId;
if (rj::getSafe(data, "channel_id", channelId))
{
auto chan = this->twitch->getChannelOrEmptyByID(channelId);
2020-08-22 11:45:18 +02:00
auto reward = ChannelPointReward(data);
2020-08-22 11:45:18 +02:00
postToThread([chan, reward] {
if (auto channel =
dynamic_cast<TwitchChannel *>(chan.get()))
{
channel->addChannelPointReward(reward);
}
});
}
else
{
qCDebug(chatterinoApp)
<< "Couldn't find channel id of point reward";
}
});
this->twitch->pubsub->start();
auto RequestModerationActions = [=]() {
this->twitch->pubsub->unlistenAllModerationActions();
2018-08-06 21:17:03 +02:00
// TODO(pajlada): Unlisten to all authed topics instead of only
// moderation topics this->twitch->pubsub->UnlistenAllAuthedTopics();
this->twitch->pubsub->listenToWhispers(
this->accounts->twitch.getCurrent());
};
2018-05-26 20:26:25 +02:00
this->accounts->twitch.currentUserChanged.connect(RequestModerationActions);
RequestModerationActions();
}
Application *getApp()
{
2018-08-02 14:23:27 +02:00
assert(Application::instance != nullptr);
assertInGuiThread();
2018-08-02 14:23:27 +02:00
return Application::instance;
}
} // namespace chatterino