mirror-chatterino2/src/singletons/pathmanager.cpp

83 lines
2.2 KiB
C++
Raw Normal View History

#include "singletons/pathmanager.hpp"
#include <QCoreApplication>
2018-04-20 00:15:57 +02:00
#include <QCryptographicHash>
#include <QDir>
#include <QStandardPaths>
namespace chatterino {
namespace singletons {
PathManager::PathManager(int argc, char **argv)
{
2018-04-20 00:15:57 +02:00
// hash of app path
this->appPathHash = QCryptographicHash::hash(QCoreApplication::applicationFilePath().toUtf8(),
QCryptographicHash::Sha224)
.toBase64()
.mid(0, 32)
.replace("+", "-")
.replace("/", "x");
// Options
2018-05-28 18:25:19 +02:00
this->portable = false;
for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i], "portable") == 0) {
2018-05-28 18:25:19 +02:00
this->portable = true;
}
}
2018-05-28 18:25:19 +02:00
if (QFileInfo::exists(QCoreApplication::applicationDirPath() + "/this->portable")) {
this->portable = true;
}
// Root path = %APPDATA%/Chatterino or the folder that the executable resides in
QString rootPath;
2018-05-28 18:25:19 +02:00
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");
}
2018-01-19 22:45:33 +01:00
this->cacheFolderPath = rootPath + "/Cache";
if (!QDir().mkpath(this->cacheFolderPath)) {
throw std::runtime_error("Error creating cache folder");
2018-01-19 22:45:33 +01:00
}
this->logsFolderPath = rootPath + "/Logs";
if (!QDir().mkpath(this->logsFolderPath)) {
throw std::runtime_error("Error creating logs folder");
}
}
bool PathManager::createFolder(const QString &folderPath)
{
return QDir().mkpath(folderPath);
}
2018-05-28 18:25:19 +02:00
bool PathManager::isPortable()
{
return this->portable;
}
} // namespace singletons
} // namespace chatterino