refactor: Un-singletonize Paths & Updates (#5092)

This commit is contained in:
pajlada
2024-01-16 21:56:43 +01:00
committed by GitHub
parent 7f935665f9
commit 718696db53
60 changed files with 237 additions and 165 deletions
+11 -6
View File
@@ -128,7 +128,12 @@ namespace chatterino {
using namespace std::string_literals;
void CrashHandler::initialize(Settings & /*settings*/, Paths &paths)
CrashHandler::CrashHandler(const Paths &paths_)
: paths(paths_)
{
}
void CrashHandler::initialize(Settings & /*settings*/, const Paths &paths_)
{
auto optSettings = readRecoverySettings(paths);
if (optSettings)
@@ -146,7 +151,7 @@ void CrashHandler::saveShouldRecover(bool value)
{
this->shouldRecover_ = value;
QFile file(QDir(getPaths()->crashdumpDirectory).filePath(RECOVERY_FILE));
QFile file(QDir(this->paths.crashdumpDirectory).filePath(RECOVERY_FILE));
if (!file.open(QFile::WriteOnly | QFile::Truncate))
{
qCWarning(chatterinoCrashhandler)
@@ -160,7 +165,8 @@ void CrashHandler::saveShouldRecover(bool value)
}
#ifdef CHATTERINO_WITH_CRASHPAD
std::unique_ptr<crashpad::CrashpadClient> installCrashHandler(const Args &args)
std::unique_ptr<crashpad::CrashpadClient> installCrashHandler(
const Args &args, const Paths &paths)
{
// Currently, the following directory layout is assumed:
// [applicationDirPath]
@@ -188,15 +194,14 @@ std::unique_ptr<crashpad::CrashpadClient> installCrashHandler(const Args &args)
// Argument passed in --database
// > Crash reports are written to this database, and if uploads are enabled,
// uploaded from this database to a crash report collection server.
auto databaseDir =
base::FilePath(nativeString(getPaths()->crashdumpDirectory));
auto databaseDir = base::FilePath(nativeString(paths.crashdumpDirectory));
auto client = std::make_unique<crashpad::CrashpadClient>();
std::map<std::string, std::string> annotations{
{
"canRestart"s,
canRestart(*getPaths(), args) ? "true"s : "false"s,
canRestart(paths, args) ? "true"s : "false"s,
},
{
"exePath"s,
+8 -2
View File
@@ -13,10 +13,15 @@
namespace chatterino {
class Args;
class Paths;
class CrashHandler : public Singleton
{
const Paths &paths;
public:
explicit CrashHandler(const Paths &paths_);
bool shouldRecover() const
{
return this->shouldRecover_;
@@ -25,14 +30,15 @@ public:
/// Sets and saves whether Chatterino should restart on a crash
void saveShouldRecover(bool value);
void initialize(Settings &settings, Paths &paths) override;
void initialize(Settings &settings, const Paths &paths) override;
private:
bool shouldRecover_ = false;
};
#ifdef CHATTERINO_WITH_CRASHPAD
std::unique_ptr<crashpad::CrashpadClient> installCrashHandler(const Args &args);
std::unique_ptr<crashpad::CrashpadClient> installCrashHandler(
const Args &args, const Paths &paths);
#endif
} // namespace chatterino
+1 -1
View File
@@ -6,7 +6,7 @@ Emotes::Emotes()
{
}
void Emotes::initialize(Settings &settings, Paths &paths)
void Emotes::initialize(Settings &settings, const Paths &paths)
{
this->emojis.load();
+1 -1
View File
@@ -25,7 +25,7 @@ class Emotes final : public IEmotes, public Singleton
public:
Emotes();
void initialize(Settings &settings, Paths &paths) override;
void initialize(Settings &settings, const Paths &paths) override;
bool isIgnoredEmote(const QString &emote);
+1 -1
View File
@@ -79,7 +79,7 @@ Fonts::Fonts()
this->fontsByType_.resize(size_t(FontStyle::EndType));
}
void Fonts::initialize(Settings &, Paths &)
void Fonts::initialize(Settings &, const Paths &)
{
this->chatFontFamily.connect(
[this]() {
+1 -1
View File
@@ -43,7 +43,7 @@ class Fonts final : public Singleton
public:
Fonts();
void initialize(Settings &settings, Paths &paths) override;
void initialize(Settings &settings, const Paths &paths) override;
// font data gets set in createFontData(...)
+2 -1
View File
@@ -1,5 +1,6 @@
#include "singletons/ImageUploader.hpp"
#include "Application.hpp"
#include "common/Env.hpp"
#include "common/network/NetworkRequest.hpp"
#include "common/network/NetworkResult.hpp"
@@ -50,7 +51,7 @@ void ImageUploader::logToFile(const QString &originalFilePath,
{
const QString logFileName =
combinePath((getSettings()->logPath.getValue().isEmpty()
? getPaths()->messageLogDirectory
? getIApp()->getPaths().messageLogDirectory
: getSettings()->logPath),
"ImageUploader.json");
+4 -4
View File
@@ -34,11 +34,11 @@ namespace chatterino {
using namespace literals;
void registerNmManifest(Paths &paths, const QString &manifestFilename,
void registerNmManifest(const Paths &paths, const QString &manifestFilename,
const QString &registryKeyName,
const QJsonDocument &document);
void registerNmHost(Paths &paths)
void registerNmHost(const Paths &paths)
{
if (paths.isPortable())
{
@@ -80,7 +80,7 @@ void registerNmHost(Paths &paths)
}
}
void registerNmManifest(Paths &paths, const QString &manifestFilename,
void registerNmManifest(const Paths &paths, const QString &manifestFilename,
const QString &registryKeyName,
const QJsonDocument &document)
{
@@ -99,7 +99,7 @@ void registerNmManifest(Paths &paths, const QString &manifestFilename,
#endif
}
std::string &getNmQueueName(Paths &paths)
std::string &getNmQueueName(const Paths &paths)
{
static std::string name =
"chatterino_gui" + paths.applicationFilePathHash.toStdString();
+2 -2
View File
@@ -16,8 +16,8 @@ class Channel;
using ChannelPtr = std::shared_ptr<Channel>;
void registerNmHost(Paths &paths);
std::string &getNmQueueName(Paths &paths);
void registerNmHost(const Paths &paths);
std::string &getNmQueueName(const Paths &paths);
Atomic<std::optional<QString>> &nmIpcError();
+2 -11
View File
@@ -15,12 +15,8 @@ using namespace std::literals;
namespace chatterino {
Paths *Paths::instance = nullptr;
Paths::Paths()
{
this->instance = this;
this->initAppFilePathHash();
this->initCheckPortable();
@@ -33,12 +29,12 @@ bool Paths::createFolder(const QString &folderPath)
return QDir().mkpath(folderPath);
}
bool Paths::isPortable()
bool Paths::isPortable() const
{
return Modes::instance().isPortable;
}
QString Paths::cacheDirectory()
QString Paths::cacheDirectory() const
{
static const auto pathSetting = [] {
QStringSetting cachePathSetting("/cache/path");
@@ -146,9 +142,4 @@ void Paths::initSubDirectories()
this->crashdumpDirectory = makePath("Crashes");
}
Paths *getPaths()
{
return Paths::instance;
}
} // namespace chatterino
+3 -6
View File
@@ -9,8 +9,6 @@ namespace chatterino {
class Paths
{
public:
static Paths *instance;
Paths();
// Root directory for the configuration files. %APPDATA%/chatterino or
@@ -42,9 +40,10 @@ public:
QString themesDirectory;
bool createFolder(const QString &folderPath);
bool isPortable();
[[deprecated("use Modes::instance().portable instead")]] bool isPortable()
const;
QString cacheDirectory();
QString cacheDirectory() const;
private:
void initAppFilePathHash();
@@ -58,6 +57,4 @@ private:
QString cacheDirectory_;
};
Paths *getPaths();
} // namespace chatterino
+4 -4
View File
@@ -219,7 +219,7 @@ bool Theme::isLightTheme() const
return this->isLight_;
}
void Theme::initialize(Settings &settings, Paths &paths)
void Theme::initialize(Settings &settings, const Paths &paths)
{
this->themeName.connect(
[this](auto themeName) {
@@ -228,7 +228,7 @@ void Theme::initialize(Settings &settings, Paths &paths)
},
false);
this->loadAvailableThemes();
this->loadAvailableThemes(paths);
this->update();
}
@@ -328,11 +328,11 @@ std::vector<std::pair<QString, QVariant>> Theme::availableThemes() const
return packagedThemes;
}
void Theme::loadAvailableThemes()
void Theme::loadAvailableThemes(const Paths &paths)
{
this->availableThemes_ = Theme::builtInThemes;
auto dir = QDir(getPaths()->themesDirectory);
auto dir = QDir(paths.themesDirectory);
for (const auto &info :
dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot, QDir::Name))
{
+2 -2
View File
@@ -43,7 +43,7 @@ public:
static const int AUTO_RELOAD_INTERVAL_MS = 500;
void initialize(Settings &settings, Paths &paths) final;
void initialize(Settings &settings, const Paths &paths) final;
bool isLightTheme() const;
@@ -169,7 +169,7 @@ private:
*
* NOTE: This is currently not built to be reloadable
**/
void loadAvailableThemes();
void loadAvailableThemes(const Paths &paths);
std::optional<ThemeDescriptor> findThemeByKey(const QString &key);
+2 -1
View File
@@ -33,7 +33,8 @@ using namespace literals;
QString avatarFilePath(const QString &channelName)
{
// TODO: cleanup channel (to be used as a file) and use combinePath
return getPaths()->twitchProfileAvatars % '/' % channelName % u".png";
return getIApp()->getPaths().twitchProfileAvatars % '/' % channelName %
u".png";
}
bool hasAvatarForChannel(const QString &channelName)
+6 -13
View File
@@ -26,21 +26,14 @@ namespace {
} // namespace
Updates::Updates()
: currentVersion_(CHATTERINO_VERSION)
Updates::Updates(const Paths &paths_)
: paths(paths_)
, currentVersion_(CHATTERINO_VERSION)
, updateGuideLink_("https://chatterino.com")
{
qCDebug(chatterinoUpdate) << "init UpdateManager";
}
Updates &Updates::instance()
{
// fourtf: don't add this class to the application class
static Updates instance;
return instance;
}
/// Checks if the online version is newer or older than the current version.
bool Updates::isDowngradeOf(const QString &online, const QString &current)
{
@@ -97,7 +90,7 @@ void Updates::installUpdates()
box->exec();
QDesktopServices::openUrl(this->updateGuideLink_);
#elif defined Q_OS_WIN
if (getPaths()->isPortable())
if (this->paths.isPortable())
{
QMessageBox *box =
new QMessageBox(QMessageBox::Information, "Chatterino Update",
@@ -136,7 +129,7 @@ void Updates::installUpdates()
QByteArray object = result.getData();
auto filename =
combinePath(getPaths()->miscDirectory, "update.zip");
combinePath(this->paths.miscDirectory, "update.zip");
QFile file(filename);
file.open(QIODevice::Truncate | QIODevice::WriteOnly);
@@ -196,7 +189,7 @@ void Updates::installUpdates()
QByteArray object = result.getData();
auto filePath =
combinePath(getPaths()->miscDirectory, "Update.exe");
combinePath(this->paths.miscDirectory, "Update.exe");
QFile file(filePath);
file.open(QIODevice::Truncate | QIODevice::WriteOnly);
+9 -4
View File
@@ -5,11 +5,19 @@
namespace chatterino {
class Paths;
/**
* To check for updates, use the `checkForUpdates` method.
* The class by itself does not start any automatic updates.
*/
class Updates
{
Updates();
const Paths &paths;
public:
explicit Updates(const Paths &paths_);
enum Status {
None,
Searching,
@@ -21,9 +29,6 @@ public:
WriteFileFailed,
};
// fourtf: don't add this class to the application class
static Updates &instance();
static bool isDowngradeOf(const QString &online, const QString &current);
void checkForUpdates();
+3 -3
View File
@@ -91,8 +91,8 @@ void WindowManager::showAccountSelectPopup(QPoint point)
w->setFocus();
}
WindowManager::WindowManager()
: windowLayoutFilePath(combinePath(getPaths()->settingsDirectory,
WindowManager::WindowManager(const Paths &paths)
: windowLayoutFilePath(combinePath(paths.settingsDirectory,
WindowManager::WINDOW_LAYOUT_FILENAME))
{
qCDebug(chatterinoWindowmanager) << "init WindowManager";
@@ -338,7 +338,7 @@ void WindowManager::setEmotePopupPos(QPoint pos)
this->emotePopupPos_ = pos;
}
void WindowManager::initialize(Settings &settings, Paths &paths)
void WindowManager::initialize(Settings &settings, const Paths &paths)
{
(void)paths;
assertInGuiThread();
+7 -2
View File
@@ -37,9 +37,14 @@ class WindowManager final : public Singleton
public:
static const QString WINDOW_LAYOUT_FILENAME;
WindowManager();
explicit WindowManager(const Paths &paths);
~WindowManager() override;
WindowManager(const WindowManager &) = delete;
WindowManager(WindowManager &&) = delete;
WindowManager &operator=(const WindowManager &) = delete;
WindowManager &operator=(WindowManager &&) = delete;
static void encodeTab(SplitContainer *tab, bool isSelected,
QJsonObject &obj);
static void encodeChannel(IndirectChannel channel, QJsonObject &obj);
@@ -93,7 +98,7 @@ public:
QPoint emotePopupPos();
void setEmotePopupPos(QPoint pos);
void initialize(Settings &settings, Paths &paths) override;
void initialize(Settings &settings, const Paths &paths) override;
void save() override;
void closeAll();
+4 -2
View File
@@ -1,5 +1,6 @@
#include "LoggingChannel.hpp"
#include "Application.hpp"
#include "common/QLogging.hpp"
#include "messages/Message.hpp"
#include "messages/MessageThread.hpp"
@@ -44,8 +45,9 @@ LoggingChannel::LoggingChannel(const QString &_channelName,
QDir::separator() + this->subDirectory;
getSettings()->logPath.connect([this](const QString &logPath, auto) {
this->baseDirectory =
logPath.isEmpty() ? getPaths()->messageLogDirectory : logPath;
this->baseDirectory = logPath.isEmpty()
? getIApp()->getPaths().messageLogDirectory
: logPath;
this->openLogFile();
});
}