Add a PathManager that takes care of all paths and creating folders

Move all path/folder-related code from SettingManager to PathManager
This commit is contained in:
Rasmus Karlsson 2018-01-05 02:23:49 +01:00
parent c3770707aa
commit 346950d7b7
7 changed files with 92 additions and 35 deletions

View file

@ -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 =

View file

@ -20,6 +20,8 @@ Application::Application()
logging::init();
singletons::SettingManager::getInstance().init();
singletons::WindowManager::getInstance().initMainWindow();
// Initialize everything we need

View file

@ -1,5 +1,5 @@
#include "application.hpp"
#include "singletons/settingsmanager.hpp"
#include "singletons/pathmanager.hpp"
#include <QAbstractNativeEventFilter>
#include <QApplication>
@ -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;
}

View file

@ -0,0 +1,57 @@
#include "pathmanager.hpp"
#include <QDir>
#include <QStandardPaths>
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

View file

@ -0,0 +1,22 @@
#pragma once
#include <QString>
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

View file

@ -1,6 +1,7 @@
#include "singletons/settingsmanager.hpp"
#include "appdatapath.hpp"
#include "debug/log.hpp"
#include "singletons/pathmanager.hpp"
#include <QDir>
#include <QStandardPaths>
@ -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()

View file

@ -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};