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
+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