Perform initial refactoring work

Things that were once singletons are no longer singletons, but are
instead stored in the "Application" singleton

Some singletons still remain, and some renaming/renamespacing is left
This commit is contained in:
Rasmus Karlsson
2018-04-27 22:11:19 +02:00
parent 32b6417a55
commit ae26b835b6
78 changed files with 850 additions and 773 deletions
-25
View File
@@ -3,31 +3,6 @@
namespace chatterino {
namespace singletons {
namespace {
inline QString getEnvString(const char *target)
{
char *val = std::getenv(target);
if (val == nullptr) {
return QString();
}
return QString(val);
}
} // namespace
AccountManager::AccountManager()
{
qDebug() << "init AccountManager";
}
AccountManager &AccountManager::getInstance()
{
static AccountManager instance;
return instance;
}
void AccountManager::load()
{
this->Twitch.load();
+3 -2
View File
@@ -7,10 +7,11 @@ namespace singletons {
class AccountManager
{
AccountManager();
AccountManager() = default;
friend class Application;
public:
static AccountManager &getInstance();
~AccountManager() = delete;
void load();
+5 -13
View File
@@ -1,4 +1,6 @@
#include "singletons/commandmanager.hpp"
#include "application.hpp"
#include "debug/log.hpp"
#include "messages/messagebuilder.hpp"
#include "providers/twitch/twitchserver.hpp"
@@ -17,20 +19,10 @@ using namespace chatterino::providers::twitch;
namespace chatterino {
namespace singletons {
CommandManager::CommandManager()
{
qDebug() << "init CommandManager";
}
CommandManager &CommandManager::getInstance()
{
static CommandManager instance;
return instance;
}
void CommandManager::loadCommands()
{
this->filePath = PathManager::getInstance().customFolderPath + "/Commands.txt";
auto app = getApp();
this->filePath = app->paths->customFolderPath + "/Commands.txt";
QFile textFile(this->filePath);
if (!textFile.open(QIODevice::ReadOnly)) {
@@ -162,7 +154,7 @@ QString CommandManager::execCommand(const QString &text, ChannelPtr channel, boo
messages::MessageBuilder b;
b.emplace<messages::TextElement>(
singletons::AccountManager::getInstance().Twitch.getCurrent()->getUserName(),
getApp()->accounts->Twitch.getCurrent()->getUserName(),
messages::MessageElement::Text);
b.emplace<messages::TextElement>("->", messages::MessageElement::Text);
b.emplace<messages::TextElement>(words[1], messages::MessageElement::Text);
+3 -3
View File
@@ -2,6 +2,7 @@
#include <QMap>
#include <QString>
#include <memory>
#include <mutex>
@@ -16,11 +17,10 @@ namespace singletons {
class CommandManager
{
CommandManager();
CommandManager() = default;
friend class Application;
public:
static CommandManager &getInstance();
QString execCommand(const QString &text, std::shared_ptr<Channel> channel, bool dryRun);
void loadCommands();
+12 -14
View File
@@ -1,4 +1,6 @@
#include "emotemanager.hpp"
#include "application.hpp"
#include "common.hpp"
#include "singletons/settingsmanager.hpp"
#include "singletons/windowmanager.hpp"
@@ -80,21 +82,17 @@ EmoteManager::EmoteManager()
: findShortCodesRegex(":([-+\\w]+):")
{
qDebug() << "init EmoteManager";
auto &accountManager = AccountManager::getInstance();
}
accountManager.Twitch.userChanged.connect([this] {
auto currentUser = AccountManager::getInstance().Twitch.getCurrent();
void EmoteManager::initialize()
{
getApp()->accounts->Twitch.userChanged.connect([this] {
auto currentUser = getApp()->accounts->Twitch.getCurrent();
assert(currentUser);
this->refreshTwitchEmotes(currentUser);
});
}
EmoteManager &EmoteManager::getInstance()
{
static EmoteManager instance;
return instance;
}
void EmoteManager::loadGlobalEmotes()
{
this->loadEmojis();
@@ -558,14 +556,14 @@ util::EmoteData EmoteManager::getCheerImage(long long amount, bool animated)
pajlada::Signals::NoArgSignal &EmoteManager::getGifUpdateSignal()
{
if (!this->gifUpdateTimerInitiated) {
auto app = getApp();
this->gifUpdateTimerInitiated = true;
this->gifUpdateTimer.setInterval(30);
this->gifUpdateTimer.start();
auto &settingManager = singletons::SettingManager::getInstance();
settingManager.enableGifAnimations.connect([this](bool enabled, auto) {
app->settings->enableGifAnimations.connect([this](bool enabled, auto) {
if (enabled) {
this->gifUpdateTimer.start();
} else {
@@ -576,8 +574,8 @@ pajlada::Signals::NoArgSignal &EmoteManager::getGifUpdateSignal()
QObject::connect(&this->gifUpdateTimer, &QTimer::timeout, [this] {
this->gifUpdateTimerSignal.invoke();
// fourtf:
auto &windowManager = singletons::WindowManager::getInstance();
windowManager.repaintGifEmotes();
auto app = getApp();
app->windows->repaintGifEmotes();
});
}
+4 -1
View File
@@ -23,9 +23,12 @@ namespace singletons {
class EmoteManager
{
EmoteManager();
friend class Application;
public:
static EmoteManager &getInstance();
~EmoteManager() = delete;
void initialize();
void loadGlobalEmotes();
-2
View File
@@ -71,8 +71,6 @@ rapidjson::Document CreateUnlistenMessage(const std::vector<std::string> &topics
rj::set(msg, "type", "UNLISTEN");
auto &accountManager = AccountManager::getInstance();
rapidjson::Value data(rapidjson::kObjectType);
rapidjson::Value topics(rapidjson::kArrayType);
+11 -15
View File
@@ -1,4 +1,6 @@
#include "singletons/loggingmanager.hpp"
#include "application.hpp"
#include "debug/log.hpp"
#include "singletons/pathmanager.hpp"
#include "singletons/settingsmanager.hpp"
@@ -11,23 +13,16 @@
namespace chatterino {
namespace singletons {
LoggingManager::LoggingManager()
: pathManager(PathManager::getInstance())
void LoggingManager::initialize()
{
qDebug() << "init LoggingManager";
}
LoggingManager &LoggingManager::getInstance()
{
static LoggingManager instance;
return instance;
this->pathManager = getApp()->paths;
}
void LoggingManager::addMessage(const QString &channelName, messages::MessagePtr message)
{
const auto &settings = singletons::SettingManager::getInstance();
auto app = getApp();
if (!settings.enableLogging) {
if (!app->settings->enableLogging) {
return;
}
@@ -45,13 +40,14 @@ void LoggingManager::addMessage(const QString &channelName, messages::MessagePtr
QString LoggingManager::getDirectoryForChannel(const QString &channelName)
{
if (channelName.startsWith("/whispers")) {
return this->pathManager.whispersLogsFolderPath;
return this->pathManager->whispersLogsFolderPath;
} else if (channelName.startsWith("/mentions")) {
return this->pathManager.mentionsLogsFolderPath;
return this->pathManager->mentionsLogsFolderPath;
} else {
QString logPath(this->pathManager.channelsLogsFolderPath + QDir::separator() + channelName);
QString logPath(this->pathManager->channelsLogsFolderPath + QDir::separator() +
channelName);
if (!this->pathManager.createFolder(logPath)) {
if (!this->pathManager->createFolder(logPath)) {
debug::Log("Error creating channel logs folder for channel {}", channelName);
}
+7 -6
View File
@@ -3,8 +3,6 @@
#include "messages/message.hpp"
#include "singletons/helper/loggingchannel.hpp"
#include <boost/noncopyable.hpp>
#include <memory>
namespace chatterino {
@@ -12,14 +10,17 @@ namespace singletons {
class PathManager;
class LoggingManager : boost::noncopyable
class LoggingManager
{
LoggingManager();
LoggingManager() = default;
friend class Application;
PathManager &pathManager;
PathManager *pathManager = nullptr;
public:
static LoggingManager &getInstance();
~LoggingManager() = delete;
void initialize();
void addMessage(const QString &channelName, messages::MessagePtr message);
+5 -9
View File
@@ -1,5 +1,6 @@
#include "nativemessagingmanager.hpp"
#include "application.hpp"
#include "providers/twitch/twitchserver.hpp"
#include "singletons/pathmanager.hpp"
#include "util/posttothread.hpp"
@@ -17,7 +18,7 @@ namespace ipc = boost::interprocess;
#ifdef Q_OS_WIN
#include <QProcess>
#include <windows.h>
#include <Windows.h>
#include "singletons/windowmanager.hpp"
#include "widgets/attachedwindow.hpp"
#endif
@@ -35,12 +36,6 @@ NativeMessagingManager::NativeMessagingManager()
qDebug() << "init NativeMessagingManager";
}
NativeMessagingManager &NativeMessagingManager::getInstance()
{
static NativeMessagingManager manager;
return manager;
}
void NativeMessagingManager::writeByteArray(QByteArray a)
{
char *data = a.data();
@@ -53,6 +48,8 @@ void NativeMessagingManager::writeByteArray(QByteArray a)
void NativeMessagingManager::registerHost()
{
auto app = getApp();
// create manifest
QJsonDocument document;
QJsonObject root_obj;
@@ -70,8 +67,7 @@ void NativeMessagingManager::registerHost()
root_obj.insert("allowed_extensions", allowed_extensions);
// save the manifest
QString manifestPath =
PathManager::getInstance().settingsFolderPath + "/native-messaging-manifest.json";
QString manifestPath = app->paths->settingsFolderPath + "/native-messaging-manifest.json";
document.setObject(root_obj);
+2 -1
View File
@@ -8,9 +8,10 @@ namespace singletons {
class NativeMessagingManager
{
NativeMessagingManager();
friend class Application;
public:
static NativeMessagingManager &getInstance();
~NativeMessagingManager() = delete;
class ReceiverThread : public QThread
{
+9 -33
View File
@@ -8,18 +8,7 @@
namespace chatterino {
namespace singletons {
PathManager::PathManager()
{
qDebug() << "init PathManager";
}
PathManager &PathManager::getInstance()
{
static PathManager instance;
return instance;
}
bool PathManager::init(int argc, char **argv)
PathManager::PathManager(int argc, char **argv)
{
// hash of app path
this->appPathHash = QCryptographicHash::hash(QCoreApplication::applicationFilePath().toUtf8(),
@@ -50,64 +39,51 @@ bool PathManager::init(int argc, char **argv)
// Get settings path
rootPath.append(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
if (rootPath.isEmpty()) {
printf("Error finding writable location for settings\n");
return false;
throw std::runtime_error("Error finding writable location for settings");
}
}
this->settingsFolderPath = rootPath;
if (!QDir().mkpath(this->settingsFolderPath)) {
printf("Error creating directory: %s\n", qPrintable(this->settingsFolderPath));
return false;
throw std::runtime_error("Error creating settings folder");
}
this->customFolderPath = rootPath + "/Custom";
if (!QDir().mkpath(this->customFolderPath)) {
printf("Error creating directory: %s\n", qPrintable(this->customFolderPath));
return false;
throw std::runtime_error("Error creating custom folder");
}
this->cacheFolderPath = rootPath + "/Cache";
if (!QDir().mkpath(this->cacheFolderPath)) {
printf("Error creating cache directory: %s\n", qPrintable(this->cacheFolderPath));
return false;
throw std::runtime_error("Error creating cache folder");
}
this->logsFolderPath = rootPath + "/Logs";
if (!QDir().mkpath(this->logsFolderPath)) {
printf("Error creating logs directory: %s\n", qPrintable(this->logsFolderPath));
return false;
throw std::runtime_error("Error creating logs folder");
}
this->channelsLogsFolderPath = this->logsFolderPath + "/Channels";
if (!QDir().mkpath(this->channelsLogsFolderPath)) {
printf("Error creating channelsLogs directory: %s\n",
qPrintable(this->channelsLogsFolderPath));
return false;
throw std::runtime_error("Error creating channel logs folder");
}
this->whispersLogsFolderPath = this->logsFolderPath + "/Whispers";
if (!QDir().mkpath(this->whispersLogsFolderPath)) {
printf("Error creating whispersLogs directory: %s\n",
qPrintable(this->whispersLogsFolderPath));
return false;
throw std::runtime_error("Error creating whisper logs folder");
}
this->mentionsLogsFolderPath = this->logsFolderPath + "/Mentions";
if (!QDir().mkpath(this->mentionsLogsFolderPath)) {
printf("Error creating mentionsLogs directory: %s\n",
qPrintable(this->mentionsLogsFolderPath));
return false;
throw std::runtime_error("Error creating mentions logs folder");
}
return true;
}
bool PathManager::createFolder(const QString &folderPath)
+2 -5
View File
@@ -7,13 +7,10 @@ namespace singletons {
class PathManager
{
PathManager();
PathManager(int argc, char **argv);
friend class Application;
public:
static PathManager &getInstance();
bool init(int argc, char **argv);
// %APPDATA%/chatterino or ExecutablePath for portable mode
QString settingsFolderPath;
-6
View File
@@ -443,12 +443,6 @@ void PubSubManager::AddClient()
this->websocketClient.connect(con);
}
PubSubManager &PubSubManager::getInstance()
{
static PubSubManager instance;
return instance;
}
void PubSubManager::Start()
{
this->mainThread.reset(new std::thread(std::bind(&PubSubManager::RunThread, this)));
+3 -2
View File
@@ -69,6 +69,7 @@ private:
class PubSubManager
{
PubSubManager();
friend class Application;
using WebsocketMessagePtr = websocketpp::config::asio_tls_client::message_type::ptr;
using WebsocketContextPtr = websocketpp::lib::shared_ptr<boost::asio::ssl::context>;
@@ -80,13 +81,13 @@ class PubSubManager
std::unique_ptr<std::thread> mainThread;
public:
~PubSubManager() = delete;
enum class State {
Connected,
Disconnected,
};
static PubSubManager &getInstance();
void Start();
bool IsConnected() const
+10 -6
View File
@@ -1,4 +1,6 @@
#include "singletons/settingsmanager.hpp"
#include "application.hpp"
#include "debug/log.hpp"
#include "singletons/pathmanager.hpp"
#include "singletons/resourcemanager.hpp"
@@ -17,8 +19,7 @@ void _actuallyRegisterSetting(std::weak_ptr<pajlada::Settings::ISettingData> set
}
SettingManager::SettingManager()
: snapshot(nullptr)
, _ignoredKeywords(new std::vector<QString>)
: _ignoredKeywords(new std::vector<QString>)
{
qDebug() << "init SettingManager";
@@ -35,8 +36,10 @@ SettingManager::SettingManager()
this->moderationActions.connect([this](auto, auto) { this->updateModerationActions(); });
this->ignoredKeywords.connect([this](auto, auto) { this->updateIgnoredKeywords(); });
this->timestampFormat.connect(
[](auto, auto) { singletons::WindowManager::getInstance().layoutVisibleChatWidgets(); });
this->timestampFormat.connect([](auto, auto) {
auto app = getApp();
app->windows->layoutVisibleChatWidgets();
});
}
MessageElement::Flags SettingManager::getWordFlags()
@@ -51,7 +54,8 @@ bool SettingManager::isIgnoredEmote(const QString &)
void SettingManager::initialize()
{
QString settingsPath = PathManager::getInstance().settingsFolderPath + "/settings.json";
auto app = getApp();
QString settingsPath = app->paths->settingsFolderPath + "/settings.json";
pajlada::Settings::SettingManager::load(qPrintable(settingsPath));
}
@@ -88,7 +92,7 @@ void SettingManager::updateWordTypeMask()
if (newMask != this->wordFlags) {
this->wordFlags = newMask;
emit wordFlagsChanged();
this->wordFlagsChanged.invoke();
}
}
+7 -13
View File
@@ -13,17 +13,20 @@ namespace singletons {
void _actuallyRegisterSetting(std::weak_ptr<pajlada::Settings::ISettingData> setting);
class SettingManager : public QObject
class SettingManager
{
Q_OBJECT
using BoolSetting = ChatterinoSetting<bool>;
using FloatSetting = ChatterinoSetting<float>;
using IntSetting = ChatterinoSetting<int>;
using StringSetting = ChatterinoSetting<std::string>;
using QStringSetting = ChatterinoSetting<QString>;
SettingManager();
friend class Application;
public:
~SettingManager() = delete;
messages::MessageElement::Flags getWordFlags();
bool isIgnoredEmote(const QString &emote);
@@ -112,11 +115,6 @@ public:
BoolSetting inlineWhispers = {"/whispers/enableInlineWhispers", true};
static SettingManager &getInstance()
{
static SettingManager instance;
return instance;
}
void updateWordTypeMask();
void saveSnapshot();
@@ -124,17 +122,13 @@ public:
std::vector<ModerationAction> getModerationActions() const;
const std::shared_ptr<std::vector<QString>> getIgnoredKeywords() const;
signals:
void wordFlagsChanged();
pajlada::Signals::NoArgSignal wordFlagsChanged;
private:
std::vector<ModerationAction> _moderationActions;
std::unique_ptr<rapidjson::Document> snapshot;
std::shared_ptr<std::vector<QString>> _ignoredKeywords;
SettingManager();
void updateModerationActions();
void updateIgnoredKeywords();
+2 -8
View File
@@ -28,12 +28,6 @@ double getMultiplierByTheme(const QString &themeName)
} // namespace detail
ThemeManager &ThemeManager::getInstance()
{
static ThemeManager instance;
return instance;
}
ThemeManager::ThemeManager()
: themeName("/appearance/theme/name", "Dark")
, themeHue("/appearance/theme/hue", 0.0)
@@ -42,8 +36,8 @@ ThemeManager::ThemeManager()
this->update();
this->themeName.connectSimple([this](auto) { this->update(); });
this->themeHue.connectSimple([this](auto) { this->update(); });
this->themeName.connectSimple([this](auto) { this->update(); }, false);
this->themeHue.connectSimple([this](auto) { this->update(); }, false);
}
void ThemeManager::update()
+2 -1
View File
@@ -14,9 +14,10 @@ class WindowManager;
class ThemeManager
{
ThemeManager();
friend class Application;
public:
static ThemeManager &getInstance();
~ThemeManager() = delete;
inline bool isLightTheme() const
{
+11 -12
View File
@@ -1,4 +1,6 @@
#include "windowmanager.hpp"
#include "application.hpp"
#include "debug/log.hpp"
#include "providers/twitch/twitchserver.hpp"
#include "singletons/fontmanager.hpp"
@@ -18,12 +20,6 @@
namespace chatterino {
namespace singletons {
WindowManager &WindowManager::getInstance()
{
static WindowManager instance(ThemeManager::getInstance());
return instance;
}
void WindowManager::showSettingsDialog()
{
QTimer::singleShot(80, [] { widgets::SettingsDialog::showDialog(); });
@@ -53,11 +49,9 @@ void WindowManager::showAccountSelectPopup(QPoint point)
w->setFocus();
}
WindowManager::WindowManager(ThemeManager &_themeManager)
: themeManager(_themeManager)
WindowManager::WindowManager()
{
qDebug() << "init WindowManager";
_themeManager.repaintVisibleChatWidgets.connect([this] { this->repaintVisibleChatWidgets(); });
}
void WindowManager::layoutVisibleChatWidgets(Channel *channel)
@@ -102,7 +96,7 @@ widgets::Window &WindowManager::createWindow(widgets::Window::WindowType type)
{
util::assertInGuiThread();
auto *window = new widgets::Window(this->themeManager, type);
auto *window = new widgets::Window(type);
this->windows.push_back(window);
window->show();
@@ -143,10 +137,14 @@ void WindowManager::initialize()
{
util::assertInGuiThread();
auto app = getApp();
app->themes->repaintVisibleChatWidgets.connect(
[this] { this->repaintVisibleChatWidgets(); });
assert(!this->initialized);
// load file
QString settingsPath = PathManager::getInstance().settingsFolderPath + SETTINGS_FILENAME;
QString settingsPath = app->paths->settingsFolderPath + SETTINGS_FILENAME;
QFile file(settingsPath);
file.open(QIODevice::ReadOnly);
QByteArray data = file.readAll();
@@ -230,6 +228,7 @@ void WindowManager::initialize()
void WindowManager::save()
{
util::assertInGuiThread();
auto app = getApp();
QJsonDocument document;
@@ -301,7 +300,7 @@ void WindowManager::save()
document.setObject(obj);
// save file
QString settingsPath = PathManager::getInstance().settingsFolderPath + SETTINGS_FILENAME;
QString settingsPath = app->paths->settingsFolderPath + SETTINGS_FILENAME;
QFile file(settingsPath);
file.open(QIODevice::WriteOnly | QIODevice::Truncate);
file.write(document.toJson());
+3 -6
View File
@@ -5,14 +5,13 @@
namespace chatterino {
namespace singletons {
class ThemeManager;
class WindowManager
{
explicit WindowManager(ThemeManager &_themeManager);
WindowManager();
friend class Application;
public:
static WindowManager &getInstance();
~WindowManager() = delete;
void showSettingsDialog();
void showAccountSelectPopup(QPoint point);
@@ -37,8 +36,6 @@ public:
pajlada::Signals::Signal<Channel *> layout;
private:
ThemeManager &themeManager;
bool initialized = false;
std::vector<widgets::Window *> windows;