mirror-chatterino2/src/Application.cpp

236 lines
7 KiB
C++
Raw Normal View History

2018-06-26 14:09:39 +02:00
#include "Application.hpp"
#include "controllers/accounts/AccountController.hpp"
#include "controllers/commands/CommandController.hpp"
#include "controllers/highlights/HighlightController.hpp"
#include "controllers/ignores/IgnoreController.hpp"
#include "controllers/moderationactions/ModerationActions.hpp"
2018-06-26 14:09:39 +02:00
#include "controllers/taggedusers/TaggedUsersController.hpp"
2018-07-06 19:23:47 +02:00
#include "providers/twitch/PubsubClient.hpp"
2018-06-26 14:09:39 +02:00
#include "providers/twitch/TwitchServer.hpp"
2018-06-28 19:46:45 +02:00
#include "singletons/Emotes.hpp"
#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-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 <atomic>
namespace chatterino {
static std::atomic<bool> isAppConstructed{false};
static std::atomic<bool> isAppInitialized{false};
static Application *staticApp = nullptr;
// this class is responsible for handling the workflow of Chatterino
// It will create the instances of the major classes, and connect their signals to each other
Application::Application(int _argc, char **_argv)
2018-07-06 19:23:47 +02:00
: argc_(_argc)
, argv_(_argv)
{
2018-07-07 11:41:01 +02:00
getSettings()->initialize();
getSettings()->load();
}
2018-04-09 22:59:19 +02:00
void Application::construct()
{
assert(isAppConstructed == false);
isAppConstructed = true;
// 1. Instantiate all classes
this->settings = getSettings();
2018-07-07 11:41:01 +02:00
this->paths = getPaths();
this->addSingleton(this->themes = new Theme);
this->addSingleton(this->windows = new WindowManager);
this->addSingleton(this->logging = new Logging);
this->addSingleton(this->commands = new CommandController);
this->addSingleton(this->highlights = new HighlightController);
this->addSingleton(this->ignores = new IgnoreController);
this->addSingleton(this->taggedUsers = new TaggedUsersController);
this->addSingleton(this->accounts = new AccountController);
this->addSingleton(this->emotes = new Emotes);
this->addSingleton(this->fonts = new Fonts);
this->addSingleton(this->resources = new Resources);
this->addSingleton(this->moderationActions = new ModerationActions);
this->addSingleton(this->twitch2 = new TwitchServer);
this->twitch.server = this->twitch2;
this->twitch.pubsub = this->twitch2->pubsub;
}
void Application::instantiate(int argc, char **argv)
{
assert(staticApp == nullptr);
2017-12-31 22:58:35 +01:00
staticApp = new Application(argc, argv);
}
void Application::initialize()
{
assert(isAppInitialized == false);
isAppInitialized = true;
// 2. Initialize/load classes
2018-07-07 11:41:01 +02:00
for (Singleton *singleton : this->singletons_) {
singleton->initialize(*this);
}
// XXX
this->windows->updateWordTypeMask();
#ifdef Q_OS_WIN
#ifdef QT_DEBUG
#ifdef C_DEBUG_NM
this->nativeMessaging->registerHost();
this->nativeMessaging->openGuiMessageQueue();
#endif
#else
this->nativeMessaging->registerHost();
this->nativeMessaging->openGuiMessageQueue();
#endif
#endif
2018-06-26 17:42:35 +02:00
this->twitch.pubsub->signals_.whisper.sent.connect([](const auto &msg) {
2018-06-26 17:06:17 +02:00
Log("WHISPER SENT LOL"); //
});
2018-06-26 17:42:35 +02:00
this->twitch.pubsub->signals_.whisper.received.connect([](const auto &msg) {
2018-06-26 17:06:17 +02:00
Log("WHISPER RECEIVED LOL"); //
});
2018-06-26 17:42:35 +02:00
this->twitch.pubsub->signals_.moderation.chatCleared.connect([this](const auto &action) {
2018-04-29 13:24:37 +02:00
auto chan = this->twitch.server->getChannelOrEmptyByID(action.roomID);
if (chan->isEmpty()) {
return;
}
QString text = QString("%1 cleared the chat").arg(action.source.name);
auto msg = Message::createSystemMessage(text);
2018-06-26 17:06:17 +02:00
postToThread([chan, msg] { chan->addMessage(msg); });
});
2018-06-26 17:42:35 +02:00
this->twitch.pubsub->signals_.moderation.modeChanged.connect([this](const auto &action) {
2018-04-29 13:24:37 +02:00
auto chan = this->twitch.server->getChannelOrEmptyByID(action.roomID);
if (chan->isEmpty()) {
return;
}
2018-06-26 17:06:17 +02:00
QString text = QString("%1 turned %2 %3 mode") //
.arg(action.source.name)
.arg(action.state == ModeChangedAction::State::On ? "on" : "off")
.arg(action.getModeName());
2018-04-29 13:24:37 +02:00
if (action.duration > 0) {
text.append(" (" + QString::number(action.duration) + " seconds)");
}
auto msg = Message::createSystemMessage(text);
2018-06-26 17:06:17 +02:00
postToThread([chan, msg] { chan->addMessage(msg); });
});
2018-06-26 17:42:35 +02:00
this->twitch.pubsub->signals_.moderation.moderationStateChanged.connect(
[this](const auto &action) {
auto chan = this->twitch.server->getChannelOrEmptyByID(action.roomID);
if (chan->isEmpty()) {
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
2018-06-26 17:42:35 +02:00
if (action.modded) {
text = QString("%1 modded %2").arg(action.source.name, action.target.name);
} else {
text = QString("%1 unmodded %2").arg(action.source.name, action.target.name);
}
2018-04-29 13:24:37 +02:00
auto msg = Message::createSystemMessage(text);
2018-06-26 17:42:35 +02:00
postToThread([chan, msg] { chan->addMessage(msg); });
});
2018-06-26 17:42:35 +02:00
this->twitch.pubsub->signals_.moderation.userBanned.connect([&](const auto &action) {
auto chan = this->twitch.server->getChannelOrEmptyByID(action.roomID);
if (chan->isEmpty()) {
return;
}
auto msg = Message::createTimeoutMessage(action);
msg->flags |= Message::PubSub;
2018-06-26 17:06:17 +02:00
postToThread([chan, msg] { chan->addOrReplaceTimeout(msg); });
});
2018-06-26 17:42:35 +02:00
this->twitch.pubsub->signals_.moderation.userUnbanned.connect([&](const auto &action) {
auto chan = this->twitch.server->getChannelOrEmptyByID(action.roomID);
if (chan->isEmpty()) {
return;
}
auto msg = Message::createUntimeoutMessage(action);
2018-06-26 17:06:17 +02:00
postToThread([chan, msg] { chan->addMessage(msg); });
});
this->twitch.pubsub->start();
auto RequestModerationActions = [=]() {
2018-07-07 11:41:01 +02:00
this->twitch.server->pubsub->unlistenAllModerationActions();
// TODO(pajlada): Unlisten to all authed topics instead of only moderation topics
// this->twitch.pubsub->UnlistenAllAuthedTopics();
2018-07-07 11:41:01 +02:00
this->twitch.server->pubsub->listenToWhispers(this->accounts->twitch.getCurrent()); //
};
2018-05-26 20:26:25 +02:00
this->accounts->twitch.currentUserChanged.connect(RequestModerationActions);
RequestModerationActions();
}
int Application::run(QApplication &qtApp)
{
// Start connecting to the IRC Servers (Twitch only for now)
this->twitch.server->connect();
// Show main window
this->windows->getMainWindow().show();
return qtApp.exec();
}
void Application::save()
{
2018-07-07 11:41:01 +02:00
for (Singleton *singleton : this->singletons_) {
singleton->save();
}
}
2018-07-07 11:41:01 +02:00
void Application::addSingleton(Singleton *singleton)
{
this->singletons_.push_back(singleton);
}
Application *getApp()
{
assert(staticApp != nullptr);
return staticApp;
}
bool appInitialized()
{
return isAppInitialized;
}
} // namespace chatterino