changed settings paths

This commit is contained in:
fourtf
2018-06-21 13:02:34 +02:00
parent e0ecd97184
commit 2f91e3097a
17 changed files with 158 additions and 94 deletions
+4 -1
View File
@@ -25,13 +25,16 @@ LoggingChannel::LoggingChannel(const QString &_channelName)
this->subDirectory = QStringLiteral("Channels") + QDir::separator() + channelName;
}
// FOURTF: change this when adding more providers
this->subDirectory = "Twitch/" + this->subDirectory;
auto app = getApp();
app->settings->logPath.connect([this](const QString &logPath, auto) {
auto app = getApp();
if (logPath.isEmpty()) {
this->baseDirectory = app->paths->logsFolderPath;
this->baseDirectory = app->paths->messageLogDirectory;
} else {
this->baseDirectory = logPath;
}
+3 -2
View File
@@ -69,7 +69,7 @@ void NativeMessagingManager::registerHost()
auto registerManifest = [&](const QString &manifestFilename, const QString &registryKeyName,
const QJsonDocument &document) {
// save the manifest
QString manifestPath = app->paths->settingsFolderPath + manifestFilename;
QString manifestPath = app->paths->miscDirectory + manifestFilename;
QFile file(manifestPath);
file.open(QIODevice::WriteOnly | QIODevice::Truncate);
file.write(document.toJson());
@@ -235,7 +235,8 @@ void NativeMessagingManager::ReceiverThread::handleMessage(const QJsonObject &ro
std::string &NativeMessagingManager::getGuiMessageQueueName()
{
static std::string name =
"chatterino_gui" + singletons::PathManager::getInstance()->appPathHash.toStdString();
"chatterino_gui" +
singletons::PathManager::getInstance()->applicationFilePathHash.toStdString();
return name;
}
+80 -59
View File
@@ -6,76 +6,27 @@
#include <QStandardPaths>
#include <cassert>
#include "util/combine_path.hpp"
namespace chatterino {
namespace singletons {
PathManager *PathManager::instance = nullptr;
PathManager::PathManager(int argc, char **argv)
PathManager::PathManager()
{
// hash of app path
this->appPathHash = QCryptographicHash::hash(QCoreApplication::applicationFilePath().toUtf8(),
QCryptographicHash::Sha224)
.toBase64()
.mid(0, 32)
.replace("+", "-")
.replace("/", "x");
this->initAppFilePathHash();
// Options
this->portable = false;
for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i], "portable") == 0) {
this->portable = true;
}
}
if (QFileInfo::exists(QCoreApplication::applicationDirPath() + "/this->portable")) {
this->portable = true;
}
// Root path = %APPDATA%/Chatterino or the folder that the executable resides in
QString rootPath;
if (this->portable) {
rootPath.append(QCoreApplication::applicationDirPath());
} else {
// Get settings path
rootPath.append(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
if (rootPath.isEmpty()) {
throw std::runtime_error("Error finding writable location for settings");
}
}
this->settingsFolderPath = rootPath;
if (!QDir().mkpath(this->settingsFolderPath)) {
throw std::runtime_error("Error creating settings folder");
}
this->customFolderPath = rootPath + "/Custom";
if (!QDir().mkpath(this->customFolderPath)) {
throw std::runtime_error("Error creating custom folder");
}
this->cacheFolderPath = rootPath + "/Cache";
if (!QDir().mkpath(this->cacheFolderPath)) {
throw std::runtime_error("Error creating cache folder");
}
this->logsFolderPath = rootPath + "/Logs";
if (!QDir().mkpath(this->logsFolderPath)) {
throw std::runtime_error("Error creating logs folder");
}
this->initCheckPortable();
this->initAppDataDirectory();
this->initSubDirectories();
}
void PathManager::initInstance(int argc, char **argv)
void PathManager::initInstance()
{
assert(!instance);
instance = new PathManager(argc, argv);
instance = new PathManager();
}
PathManager *PathManager::getInstance()
@@ -92,7 +43,77 @@ bool PathManager::createFolder(const QString &folderPath)
bool PathManager::isPortable()
{
return this->portable;
return this->portable.get();
}
void PathManager::initAppFilePathHash()
{
this->applicationFilePathHash =
QCryptographicHash::hash(QCoreApplication::applicationFilePath().toUtf8(),
QCryptographicHash::Sha224)
.toBase64()
.mid(0, 32)
.replace("+", "-")
.replace("/", "x");
}
void PathManager::initCheckPortable()
{
this->portable =
QFileInfo::exists(util::combinePath(QCoreApplication::applicationDirPath(), "portable"));
}
void PathManager::initAppDataDirectory()
{
assert(this->portable.is_initialized());
// Root path = %APPDATA%/Chatterino or the folder that the executable resides in
this->rootAppDataDirectory = [&]() -> QString {
// portable
if (this->portable) {
return QCoreApplication::applicationDirPath();
}
// permanent installation
QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
if (path.isEmpty()) {
throw std::runtime_error("Error finding writable location for settings");
}
// create directory Chatterino2 instead of chatterino on windows because the ladder one is takes by
// chatterino 1 already
#ifdef Q_OS_WIN
path.replace("chatterino", "Chatterino");
path += "2";
#endif
return path;
}();
}
void PathManager::initSubDirectories()
{
// required the app data directory to be set first
assert(!this->rootAppDataDirectory.isEmpty());
// create settings subdirectories and validate that they are created properly
auto makePath = [&](const std::string &name) -> QString {
auto path = util::combinePath(this->rootAppDataDirectory, QString::fromStdString(name));
if (!QDir().mkpath(path)) {
throw std::runtime_error("Error creating appdata path %appdata%/chatterino/" + name);
}
return path;
};
makePath("");
this->settingsDirectory = makePath("Settings");
this->cacheDirectory = makePath("Cache");
this->messageLogDirectory = makePath("Logs");
this->miscDirectory = makePath("Misc");
}
} // namespace singletons
+23 -12
View File
@@ -1,38 +1,49 @@
#pragma once
#include <QString>
#include <boost/optional.hpp>
namespace chatterino {
namespace singletons {
class PathManager
{
PathManager(int argc, char **argv);
PathManager();
public:
static void initInstance(int argc, char **argv);
static void initInstance();
static PathManager *getInstance();
// %APPDATA%/chatterino or ExecutablePath for portable mode
QString settingsFolderPath;
// Root directory for the configuration files. %APPDATA%/chatterino or ExecutablePath for
// portable mode
QString rootAppDataDirectory;
// %APPDATA%/chatterino/Custom or ExecutablePath/Custom for portable mode
QString customFolderPath;
// Directory for settings files. Same as <appDataDirectory>/Settings
QString settingsDirectory;
// %APPDATA%/chatterino/Cache or ExecutablePath/Cache for portable mode
QString cacheFolderPath;
// Directory for cache files. Same as <appDataDirectory>/Misc
QString cacheDirectory;
// Default folder for logs. %APPDATA%/chatterino/Logs or ExecutablePath/Logs for portable mode
QString logsFolderPath;
// Directory for message log files. Same as <appDataDirectory>/Misc
QString messageLogDirectory;
QString appPathHash;
// Directory for miscellaneous files. Same as <appDataDirectory>/Misc
QString miscDirectory;
// Hash of QCoreApplication::applicationFilePath()
QString applicationFilePathHash;
bool createFolder(const QString &folderPath);
bool isPortable();
private:
static PathManager *instance;
bool portable;
boost::optional<bool> portable;
void initAppFilePathHash();
void initCheckPortable();
void initAppDataDirectory();
void initSubDirectories();
};
} // namespace singletons
+1 -1
View File
@@ -66,7 +66,7 @@ bool SettingManager::isIgnoredEmote(const QString &)
void SettingManager::load()
{
auto app = getApp();
QString settingsPath = app->paths->settingsFolderPath + "/settings.json";
QString settingsPath = app->paths->settingsDirectory + "/settings.json";
pajlada::Settings::SettingManager::load(qPrintable(settingsPath));
}
+5 -3
View File
@@ -17,7 +17,7 @@
#include <QDebug>
#define SETTINGS_FILENAME "/layout.json"
#define SETTINGS_FILENAME "/window-layout.json"
namespace chatterino {
namespace singletons {
@@ -157,7 +157,7 @@ void WindowManager::initialize()
assert(!this->initialized);
// load file
QString settingsPath = app->paths->settingsFolderPath + SETTINGS_FILENAME;
QString settingsPath = app->paths->settingsDirectory + SETTINGS_FILENAME;
QFile file(settingsPath);
file.open(QIODevice::ReadOnly);
QByteArray data = file.readAll();
@@ -311,7 +311,7 @@ void WindowManager::save()
document.setObject(obj);
// save file
QString settingsPath = app->paths->settingsFolderPath + SETTINGS_FILENAME;
QString settingsPath = app->paths->settingsDirectory + SETTINGS_FILENAME;
QFile file(settingsPath);
file.open(QIODevice::WriteOnly | QIODevice::Truncate);
@@ -459,6 +459,8 @@ float WindowManager::getUiScaleValue(int scale)
return 3.5f;
case 10:
return 4;
default:
assert(false);
}
}