From 346950d7b72a924c4e8de466fe4fda93536b718b Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Fri, 5 Jan 2018 02:23:49 +0100 Subject: [PATCH] Add a PathManager that takes care of all paths and creating folders Move all path/folder-related code from SettingManager to PathManager --- chatterino.pro | 6 ++-- src/application.cpp | 2 ++ src/main.cpp | 6 ++-- src/singletons/pathmanager.cpp | 57 ++++++++++++++++++++++++++++++ src/singletons/pathmanager.hpp | 22 ++++++++++++ src/singletons/settingsmanager.cpp | 32 ++--------------- src/singletons/settingsmanager.hpp | 2 +- 7 files changed, 92 insertions(+), 35 deletions(-) create mode 100644 src/singletons/pathmanager.cpp create mode 100644 src/singletons/pathmanager.hpp diff --git a/chatterino.pro b/chatterino.pro index 06b870845..a30538fcc 100644 --- a/chatterino.pro +++ b/chatterino.pro @@ -114,7 +114,8 @@ SOURCES += \ src/twitch/twitchaccountmanager.cpp \ src/singletons/helper/completionmodel.cpp \ src/singletons/resourcemanager.cpp \ - src/singletons/helper/ircmessagehandler.cpp + src/singletons/helper/ircmessagehandler.cpp \ + src/singletons/pathmanager.cpp HEADERS += \ src/precompiled_headers.hpp \ @@ -198,7 +199,8 @@ HEADERS += \ src/singletons/helper/ircmessagehandler.hpp \ src/util/serialize-custom.hpp \ src/messages/highlightphrase.hpp \ - src/messages/selection.hpp + src/messages/selection.hpp \ + src/singletons/pathmanager.hpp PRECOMPILED_HEADER = diff --git a/src/application.cpp b/src/application.cpp index b92f4bbfe..9f893e622 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -20,6 +20,8 @@ Application::Application() logging::init(); + singletons::SettingManager::getInstance().init(); + singletons::WindowManager::getInstance().initMainWindow(); // Initialize everything we need diff --git a/src/main.cpp b/src/main.cpp index 6d9ba5702..350a59281 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,5 @@ #include "application.hpp" -#include "singletons/settingsmanager.hpp" +#include "singletons/pathmanager.hpp" #include #include @@ -24,9 +24,9 @@ int main(int argc, char *argv[]) #endif // Initialize settings - bool success = chatterino::singletons::SettingManager::getInstance().init(argc, argv); + bool success = chatterino::singletons::PathManager::getInstance().init(argc, argv); if (!success) { - printf("Error initializing settings\n"); + printf("Error initializing paths\n"); return 1; } diff --git a/src/singletons/pathmanager.cpp b/src/singletons/pathmanager.cpp new file mode 100644 index 000000000..8d31c9de1 --- /dev/null +++ b/src/singletons/pathmanager.cpp @@ -0,0 +1,57 @@ +#include "pathmanager.hpp" + +#include +#include + +namespace chatterino { +namespace singletons { + +PathManager &PathManager::getInstance() +{ + static PathManager instance; + return instance; +} + +bool PathManager::init(int argc, char **argv) +{ + // Options + bool portable = false; + + for (int i = 1; i < argc; ++i) { + if (strcmp(argv[i], "portable") == 0) { + portable = true; + } + } + + // Root path = %APPDATA%/Chatterino or the folder that the executable resides in + QString rootPath; + if (portable) { + rootPath.append(QDir::currentPath()); + } else { + // Get settings path + rootPath.append(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)); + if (rootPath.isEmpty()) { + printf("Error finding writable location for settings\n"); + return false; + } + } + + this->settingsFolderPath = rootPath; + + if (!QDir().mkpath(this->settingsFolderPath)) { + printf("Error creating directory: %s\n", qPrintable(this->settingsFolderPath)); + return false; + } + + this->customFolderPath = rootPath + "/Custom"; + + if (!QDir().mkpath(this->customFolderPath)) { + printf("Error creating directory: %s\n", qPrintable(this->customFolderPath)); + return false; + } + + return true; +} + +} // namespace singletons +} // namespace chatterino diff --git a/src/singletons/pathmanager.hpp b/src/singletons/pathmanager.hpp new file mode 100644 index 000000000..6855eab61 --- /dev/null +++ b/src/singletons/pathmanager.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include + +namespace chatterino { +namespace singletons { + +class PathManager +{ + PathManager() = default; + +public: + static PathManager &getInstance(); + + bool init(int argc, char **argv); + + QString settingsFolderPath; + QString customFolderPath; +}; + +} // namespace singletons +} // namespace chatterino diff --git a/src/singletons/settingsmanager.cpp b/src/singletons/settingsmanager.cpp index 87a4d6e5d..1bda84939 100644 --- a/src/singletons/settingsmanager.cpp +++ b/src/singletons/settingsmanager.cpp @@ -1,6 +1,7 @@ #include "singletons/settingsmanager.hpp" #include "appdatapath.hpp" #include "debug/log.hpp" +#include "singletons/pathmanager.hpp" #include #include @@ -42,38 +43,11 @@ bool SettingManager::isIgnoredEmote(const QString &) return false; } -bool SettingManager::init(int argc, char **argv) +void SettingManager::init() { - // Options - bool portable = false; - - for (int i = 1; i < argc; ++i) { - if (strcmp(argv[i], "portable") == 0) { - portable = true; - } - } - - QString settingsPath; - if (portable) { - settingsPath.append(QDir::currentPath()); - } else { - // Get settings path - settingsPath.append(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)); - if (settingsPath.isEmpty()) { - printf("Error finding writable location for settings\n"); - return false; - } - } - - if (!QDir().mkpath(settingsPath)) { - printf("Error creating directories for settings: %s\n", qPrintable(settingsPath)); - return false; - } - settingsPath.append("/settings.json"); + QString settingsPath = PathManager::getInstance().settingsFolderPath + "/settings.json"; pajlada::Settings::SettingManager::load(qPrintable(settingsPath)); - - return true; } void SettingManager::updateWordTypeMask() diff --git a/src/singletons/settingsmanager.hpp b/src/singletons/settingsmanager.hpp index 67233409d..44ab5275c 100644 --- a/src/singletons/settingsmanager.hpp +++ b/src/singletons/settingsmanager.hpp @@ -25,7 +25,7 @@ public: messages::Word::Flags getWordTypeMask(); bool isIgnoredEmote(const QString &emote); - bool init(int argc, char **argv); + void init(); /// Appearance BoolSetting showTimestamps = {"/appearance/messages/showTimestamps", true};