started to refactor Application

This commit is contained in:
fourtf 2018-07-07 11:41:01 +02:00
parent 6a418e6e59
commit c609a9fd37
32 changed files with 195 additions and 147 deletions

View file

@ -36,6 +36,8 @@ Application::Application(int _argc, char **_argv)
: argc_(_argc)
, argv_(_argv)
{
getSettings()->initialize();
getSettings()->load();
}
void Application::construct()
@ -45,23 +47,24 @@ void Application::construct()
// 1. Instantiate all classes
this->settings = getSettings();
this->paths = Paths::getInstance();
this->paths = getPaths();
this->themes = new Theme;
this->windows = new WindowManager;
this->logging = new Logging;
this->commands = new CommandController;
this->highlights = new HighlightController;
this->ignores = new IgnoreController;
this->taggedUsers = new TaggedUsersController;
this->accounts = new AccountController;
this->emotes = new Emotes;
this->fonts = new Fonts;
this->resources = new Resources;
this->moderationActions = new ModerationActions;
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->twitch.server = new TwitchServer;
this->twitch.pubsub = new PubSub;
this->addSingleton(this->twitch2 = new TwitchServer);
this->twitch.server = this->twitch2;
this->twitch.pubsub = this->twitch2->pubsub;
}
void Application::instantiate(int argc, char **argv)
@ -77,24 +80,9 @@ void Application::initialize()
isAppInitialized = true;
// 2. Initialize/load classes
this->settings->initialize();
this->settings->load();
this->commands->load();
this->logging->initialize();
this->windows->initialize();
this->resources->initialize();
this->highlights->initialize();
this->ignores->initialize();
this->emotes->initialize();
this->accounts->load();
this->twitch.server->initialize();
this->moderationActions->initialize();
for (Singleton *singleton : this->singletons_) {
singleton->initialize(*this);
}
// XXX
this->windows->updateWordTypeMask();
@ -197,11 +185,11 @@ void Application::initialize()
this->twitch.pubsub->start();
auto RequestModerationActions = [=]() {
this->twitch.pubsub->unlistenAllModerationActions();
this->twitch.server->pubsub->unlistenAllModerationActions();
// TODO(pajlada): Unlisten to all authed topics instead of only moderation topics
// this->twitch.pubsub->UnlistenAllAuthedTopics();
this->twitch.pubsub->listenToWhispers(this->accounts->twitch.getCurrent()); //
this->twitch.server->pubsub->listenToWhispers(this->accounts->twitch.getCurrent()); //
};
this->accounts->twitch.currentUserChanged.connect(RequestModerationActions);
@ -222,9 +210,14 @@ int Application::run(QApplication &qtApp)
void Application::save()
{
this->windows->save();
for (Singleton *singleton : this->singletons_) {
singleton->save();
}
}
this->commands->save();
void Application::addSingleton(Singleton *singleton)
{
this->singletons_.push_back(singleton);
}
Application *getApp()

View file

@ -6,6 +6,8 @@
namespace chatterino {
class Singleton;
class TwitchServer;
class PubSub;
@ -44,7 +46,9 @@ public:
friend void test();
Paths *paths = nullptr;
[[deprecated("use getSettings() instead")]] Settings *settings = nullptr;
[[deprecated("use getPaths() instead")]] Paths *paths = nullptr;
Theme *themes = nullptr;
WindowManager *windows = nullptr;
Logging *logging = nullptr;
@ -55,15 +59,15 @@ public:
AccountController *accounts = nullptr;
Emotes *emotes = nullptr;
NativeMessaging *nativeMessaging = nullptr;
Settings *settings = nullptr;
Fonts *fonts = nullptr;
Resources *resources = nullptr;
ModerationActions *moderationActions = nullptr;
TwitchServer *twitch2 = nullptr;
/// Provider-specific
struct {
TwitchServer *server = nullptr;
PubSub *pubsub = nullptr;
[[deprecated("use twitch2 instead")]] TwitchServer *server = nullptr;
[[deprecated("use twitch2->pubsub instead")]] PubSub *pubsub = nullptr;
} twitch;
void save();
@ -72,8 +76,12 @@ public:
static void runNativeMessagingHost();
private:
void addSingleton(Singleton *singleton);
int argc_;
char **argv_;
std::vector<Singleton *> singletons_;
};
Application *getApp();

View file

@ -8,14 +8,13 @@ class Application;
class Singleton : boost::noncopyable
{
virtual ~Singleton() = default;
virtual void initialize(Application &application)
public:
virtual void initialize(Application &app)
{
(void)(application);
(void)(app);
}
virtual void finalize()
virtual void save()
{
}
};

View file

@ -33,7 +33,7 @@ AccountController::AccountController()
});
}
void AccountController::load()
void AccountController::initialize(Application &app)
{
this->twitch.load();
}

View file

@ -1,5 +1,7 @@
#pragma once
#include "common/Singleton.hpp"
#include <QObject>
#include "common/SignalVector.hpp"
@ -11,14 +13,14 @@ namespace chatterino {
class AccountModel;
class AccountController
class AccountController : public Singleton
{
public:
AccountController();
AccountModel *createModel(QObject *parent);
void load();
virtual void initialize(Application &app) override;
TwitchAccountManager twitch;

View file

@ -43,6 +43,11 @@ CommandController::CommandController()
this->items.itemRemoved.connect(addFirstMatchToMap);
}
void CommandController::initialize(Application &app)
{
this->load();
}
void CommandController::load()
{
auto app = getApp();

View file

@ -1,5 +1,7 @@
#pragma once
#include "common/Singleton.hpp"
#include <QMap>
#include <memory>
#include <mutex>
@ -12,7 +14,7 @@ class Channel;
class CommandModel;
class CommandController
class CommandController : public Singleton
{
public:
CommandController();
@ -20,14 +22,16 @@ public:
QString execCommand(const QString &text, std::shared_ptr<Channel> channel, bool dryRun);
QStringList getDefaultTwitchCommandList();
void load();
void save();
virtual void initialize(Application &app) override;
virtual void save() override;
CommandModel *createModel(QObject *parent);
UnsortedSignalVector<Command> items;
private:
void load();
QMap<QString, Command> commandsMap_;
std::mutex mutex_;

View file

@ -12,7 +12,7 @@ HighlightController::HighlightController()
{
}
void HighlightController::initialize()
void HighlightController::initialize(Application &app)
{
assert(!this->initialized_);
this->initialized_ = true;

View file

@ -1,5 +1,7 @@
#pragma once
#include "common/Singleton.hpp"
#include "common/SignalVector.hpp"
#include "controllers/highlights/HighlightBlacklistUser.hpp"
#include "controllers/highlights/HighlightPhrase.hpp"
@ -12,12 +14,12 @@ class UserHighlightModel;
class HighlightModel;
class HighlightBlacklistModel;
class HighlightController
class HighlightController : public Singleton
{
public:
HighlightController();
void initialize();
virtual void initialize(Application &app) override;
UnsortedSignalVector<HighlightPhrase> phrases;
UnsortedSignalVector<HighlightBlacklistUser> blacklistedUsers;

View file

@ -7,7 +7,7 @@
namespace chatterino {
void IgnoreController::initialize()
void IgnoreController::initialize(Application &)
{
assert(!this->initialized_);
this->initialized_ = true;

View file

@ -1,5 +1,7 @@
#pragma once
#include "common/Singleton.hpp"
#include "common/SignalVector.hpp"
#include "controllers/ignores/IgnorePhrase.hpp"
#include "singletons/Settings.hpp"
@ -8,10 +10,10 @@ namespace chatterino {
class IgnoreModel;
class IgnoreController
class IgnoreController : public Singleton
{
public:
void initialize();
virtual void initialize(Application &app) override;
UnsortedSignalVector<IgnorePhrase> phrases;

View file

@ -12,7 +12,7 @@ ModerationActions::ModerationActions()
{
}
void ModerationActions::initialize()
void ModerationActions::initialize(Application &app)
{
assert(!this->initialized_);
this->initialized_ = true;

View file

@ -1,5 +1,7 @@
#pragma once
#include "common/Singleton.hpp"
#include "common/ChatterinoSetting.hpp"
#include "common/SignalVector.hpp"
#include "controllers/moderationactions/ModerationAction.hpp"
@ -8,12 +10,12 @@ namespace chatterino {
class ModerationActionModel;
class ModerationActions
class ModerationActions final : public Singleton
{
public:
ModerationActions();
void initialize();
virtual void initialize(Application &app) override;
UnsortedSignalVector<ModerationAction> items;

View file

@ -1,5 +1,7 @@
#pragma once
#include "common/Singleton.hpp"
#include "common/SignalVector.hpp"
#include "controllers/taggedusers/TaggedUser.hpp"
@ -7,7 +9,7 @@ namespace chatterino {
class TaggedUsersModel;
class TaggedUsersController
class TaggedUsersController : public Singleton
{
public:
TaggedUsersController();

View file

@ -1,9 +1,11 @@
#include "TwitchServer.hpp"
#include "Application.hpp"
#include "common/Common.hpp"
#include "controllers/accounts/AccountController.hpp"
#include "controllers/highlights/HighlightController.hpp"
#include "providers/twitch/IrcMessageHandler.hpp"
#include "providers/twitch/PubsubClient.hpp"
#include "providers/twitch/TwitchAccount.hpp"
#include "providers/twitch/TwitchHelpers.hpp"
#include "providers/twitch/TwitchMessageBuilder.hpp"
@ -24,16 +26,22 @@ TwitchServer::TwitchServer()
, watchingChannel(Channel::getEmpty(), Channel::Type::TwitchWatching)
{
qDebug() << "init TwitchServer";
this->pubsub = new PubSub;
}
void TwitchServer::initialize()
void TwitchServer::initialize(Application &app)
{
getApp()->accounts->twitch.currentUserChanged.connect(
this->app = &app;
app.accounts->twitch.currentUserChanged.connect(
[this]() { postToThread([this] { this->connect(); }); });
}
void TwitchServer::initializeConnection(IrcConnection *connection, bool isRead, bool isWrite)
{
assert(this->app);
std::shared_ptr<TwitchAccount> account = getApp()->accounts->twitch.getCurrent();
qDebug() << "logging in as" << account->getUserName();

View file

@ -1,6 +1,7 @@
#pragma once
#include "common/MutexValue.hpp"
#include "common/Singleton.hpp"
#include "providers/irc/AbstractIrcServer.hpp"
#include "providers/twitch/TwitchAccount.hpp"
#include "providers/twitch/TwitchChannel.hpp"
@ -11,12 +12,15 @@
namespace chatterino {
class TwitchServer final : public AbstractIrcServer
class PubSub;
class TwitchServer : public AbstractIrcServer, public Singleton
{
public:
TwitchServer();
virtual ~TwitchServer() override = default;
void initialize();
virtual void initialize(Application &app) override;
// fourtf: ugh
void forEachChannelAndSpecialChannels(std::function<void(ChannelPtr)> func);
@ -29,6 +33,8 @@ public:
const ChannelPtr mentionsChannel;
IndirectChannel watchingChannel;
PubSub *pubsub;
protected:
void initializeConnection(IrcConnection *connection, bool isRead, bool isWrite) override;
std::shared_ptr<Channel> createChannel(const QString &channelName) override;
@ -44,6 +50,8 @@ protected:
private:
void onMessageSendRequested(TwitchChannel *channel, const QString &message, bool &sent);
Application *app = nullptr;
std::mutex lastMessageMutex_;
std::queue<std::chrono::steady_clock::time_point> lastMessagePleb_;
std::queue<std::chrono::steady_clock::time_point> lastMessageMod_;

View file

@ -5,10 +5,10 @@
namespace chatterino {
void Emotes::initialize()
void Emotes::initialize(Application &app)
{
getApp()->accounts->twitch.currentUserChanged.connect([this] {
auto currentUser = getApp()->accounts->twitch.getCurrent();
app.accounts->twitch.currentUserChanged.connect([this, &app] {
auto currentUser = app.accounts->twitch.getCurrent();
assert(currentUser);
this->twitch.refresh(currentUser);
});

View file

@ -1,5 +1,7 @@
#pragma once
#include "common/Singleton.hpp"
#define GIF_FRAME_LENGTH 33
#include "providers/bttv/BttvEmotes.hpp"
@ -10,12 +12,10 @@
namespace chatterino {
class Emotes
class Emotes final : public Singleton
{
public:
~Emotes() = delete;
void initialize();
virtual void initialize(Application &app) override;
bool isIgnoredEmote(const QString &emote);

View file

@ -26,37 +26,38 @@ Fonts::Fonts()
: chatFontFamily("/appearance/currentFontFamily", DEFAULT_FONT_FAMILY)
, chatFontSize("/appearance/currentFontSize", DEFAULT_FONT_SIZE)
{
qDebug() << "init FontManager";
this->chatFontFamily.connect([this](const std::string &, auto) {
assertInGuiThread();
if (getApp()->windows) {
getApp()->windows->incGeneration();
}
for (auto &map : this->fontsByType_) {
map.clear();
}
this->fontChanged.invoke();
});
this->chatFontSize.connect([this](const int &, auto) {
assertInGuiThread();
if (getApp()->windows) {
getApp()->windows->incGeneration();
}
for (auto &map : this->fontsByType_) {
map.clear();
}
this->fontChanged.invoke();
});
this->fontsByType_.resize(size_t(EndType));
}
void Fonts::initialize(Application &app)
{
this->chatFontFamily.connect([this, &app](const std::string &, auto) {
assertInGuiThread();
if (app.windows) {
app.windows->incGeneration();
}
for (auto &map : this->fontsByType_) {
map.clear();
}
this->fontChanged.invoke();
});
this->chatFontSize.connect([this, &app](const int &, auto) {
assertInGuiThread();
if (app.windows) {
app.windows->incGeneration();
}
for (auto &map : this->fontsByType_) {
map.clear();
}
this->fontChanged.invoke();
});
}
QFont Fonts::getFont(Fonts::Type type, float scale)
{
return this->getOrCreateFontData(type, scale).font;

View file

@ -1,5 +1,7 @@
#pragma once
#include "common/Singleton.hpp"
#include <QFont>
#include <QFontDatabase>
#include <QFontMetrics>
@ -11,11 +13,13 @@
namespace chatterino {
class Fonts : boost::noncopyable
class Fonts final : public Singleton
{
public:
Fonts();
virtual void initialize(Application &app) override;
// font data gets set in createFontData(...)
enum Type : uint8_t {
Tiny,

View file

@ -12,16 +12,13 @@
namespace chatterino {
void Logging::initialize()
void Logging::initialize(Application &app)
{
this->pathManager = getApp()->paths;
}
void Logging::addMessage(const QString &channelName, MessagePtr message)
{
auto app = getApp();
if (!app->settings->enableLogging) {
if (!getSettings()->enableLogging) {
return;
}

View file

@ -1,5 +1,7 @@
#pragma once
#include "common/Singleton.hpp"
#include "messages/Message.hpp"
#include "singletons/helper/LoggingChannel.hpp"
@ -9,16 +11,14 @@ namespace chatterino {
class Paths;
class Logging
class Logging : public Singleton
{
Paths *pathManager = nullptr;
public:
Logging() = default;
~Logging() = delete;
void initialize();
virtual void initialize(Application &app) override;
void addMessage(const QString &channelName, MessagePtr message);

View file

@ -1,17 +1,16 @@
#pragma once
#include "common/Singleton.hpp"
#include <QThread>
namespace chatterino {
class NativeMessaging
class NativeMessaging final
{
public:
// fourtf: don't add this class to the application class
NativeMessaging();
~NativeMessaging() = delete;
class ReceiverThread : public QThread
{
public:

View file

@ -114,4 +114,9 @@ void Paths::initSubDirectories()
this->miscDirectory = makePath("Misc");
}
Paths *getPaths()
{
return Paths::getInstance();
}
} // namespace chatterino

View file

@ -45,4 +45,6 @@ private:
boost::optional<bool> portable_;
};
Paths *getPaths();
} // namespace chatterino

View file

@ -302,7 +302,7 @@ Resources::Resources()
qDebug() << "init ResourceManager";
}
void Resources::initialize()
void Resources::initialize(Application &app)
{
this->loadDynamicTwitchBadges();

View file

@ -1,5 +1,7 @@
#pragma once
#include "common/Singleton.hpp"
#include "common/Emotemap.hpp"
#include <QIcon>
@ -11,14 +13,14 @@
namespace chatterino {
class Resources
class Resources : public Singleton
{
public:
Resources();
~Resources() = delete;
void initialize();
virtual void initialize(Application &app) override;
struct {
QIcon left;

View file

@ -29,26 +29,11 @@ Settings &Settings::getInstance()
void Settings::initialize()
{
this->timestampFormat.connect([](auto, auto) {
auto app = getApp();
app->windows->layoutChannelViews();
});
this->emoteScale.connect([](auto, auto) { getApp()->windows->forceLayoutChannelViews(); });
this->timestampFormat.connect([](auto, auto) { getApp()->windows->forceLayoutChannelViews(); });
this->alternateMessageBackground.connect(
[](auto, auto) { getApp()->windows->forceLayoutChannelViews(); });
this->separateMessages.connect(
[](auto, auto) { getApp()->windows->forceLayoutChannelViews(); });
this->collpseMessagesMinLines.connect(
[](auto, auto) { getApp()->windows->forceLayoutChannelViews(); });
}
void Settings::load()
{
auto app = getApp();
QString settingsPath = app->paths->settingsDirectory + "/settings.json";
QString settingsPath = getPaths()->settingsDirectory + "/settings.json";
pajlada::Settings::SettingManager::load(qPrintable(settingsPath));
}

View file

@ -1,6 +1,7 @@
#pragma once
#include "common/SerializeCustom.hpp"
#include "common/Singleton.hpp"
#include <QBrush>
#include <QColor>
@ -10,13 +11,11 @@ namespace chatterino {
class WindowManager;
class Theme
class Theme final : public Singleton
{
public:
Theme();
~Theme() = delete;
inline bool isLightTheme() const
{
return this->isLight_;

View file

@ -206,17 +206,16 @@ Window *WindowManager::windowAt(int index)
return this->windows_.at(index);
}
void WindowManager::initialize()
void WindowManager::initialize(Application &app)
{
assertInGuiThread();
auto app = getApp();
app->themes->repaintVisibleChatWidgets_.connect([this] { this->repaintVisibleChatWidgets(); });
app.themes->repaintVisibleChatWidgets_.connect([this] { this->repaintVisibleChatWidgets(); });
assert(!this->initialized_);
// load file
QString settingsPath = app->paths->settingsDirectory + SETTINGS_FILENAME;
QString settingsPath = getPaths()->settingsDirectory + SETTINGS_FILENAME;
QFile file(settingsPath);
file.open(QIODevice::ReadOnly);
QByteArray data = file.readAll();
@ -301,6 +300,22 @@ void WindowManager::initialize()
mainWindow_->getNotebook().addPage(true);
}
auto settings = getSettings();
settings->timestampFormat.connect([this](auto, auto) {
auto app = getApp();
this->layoutChannelViews();
});
settings->emoteScale.connect([this](auto, auto) { this->forceLayoutChannelViews(); });
settings->timestampFormat.connect([this](auto, auto) { this->forceLayoutChannelViews(); });
settings->alternateMessageBackground.connect(
[this](auto, auto) { this->forceLayoutChannelViews(); });
settings->separateMessages.connect([this](auto, auto) { this->forceLayoutChannelViews(); });
settings->collpseMessagesMinLines.connect(
[this](auto, auto) { this->forceLayoutChannelViews(); });
this->initialized_ = true;
}
@ -321,9 +336,12 @@ void WindowManager::save()
case Window::Type::Main:
window_obj.insert("type", "main");
break;
case Window::Type::Popup:
window_obj.insert("type", "popup");
break;
case Window::Type::Attached:;
}
// window geometry

View file

@ -1,15 +1,16 @@
#pragma once
#include <common/Singleton.hpp>
#include "widgets/Window.hpp"
#include "widgets/splits/SplitContainer.hpp"
namespace chatterino {
class WindowManager
class WindowManager : public Singleton
{
public:
WindowManager();
~WindowManager() = delete;
static void encodeChannel(IndirectChannel channel, QJsonObject &obj);
static IndirectChannel decodeChannel(const QJsonObject &obj);
@ -36,8 +37,8 @@ public:
int windowCount();
Window *windowAt(int index);
void save();
void initialize();
virtual void initialize(Application &app) override;
virtual void save() override;
void closeAll();
int getGeneration() const;

View file

@ -676,7 +676,7 @@ void ChannelView::wheelEvent(QWheelEvent *event)
auto snapshot = this->getMessagesSnapshot();
int snapshotLength = int(snapshot.getLength());
int i = std::min(int(desired), snapshotLength);
int i = std::min<int>(int(desired), snapshotLength);
if (delta > 0) {
qreal scrollFactor = fmod(desired, 1);