2018-04-14 15:32:41 +02:00
|
|
|
#include "singletons/pathmanager.hpp"
|
2018-01-05 02:23:49 +01:00
|
|
|
|
2018-04-14 15:32:41 +02:00
|
|
|
#include <QCoreApplication>
|
2018-04-20 00:15:57 +02:00
|
|
|
#include <QCryptographicHash>
|
2018-01-05 02:23:49 +01:00
|
|
|
#include <QDir>
|
|
|
|
#include <QStandardPaths>
|
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
namespace singletons {
|
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
PathManager::PathManager(int argc, char **argv)
|
2018-01-05 02:23:49 +01:00
|
|
|
{
|
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");
|
|
|
|
|
2018-01-05 02:23:49 +01:00
|
|
|
// Options
|
|
|
|
bool portable = false;
|
|
|
|
|
|
|
|
for (int i = 1; i < argc; ++i) {
|
|
|
|
if (strcmp(argv[i], "portable") == 0) {
|
|
|
|
portable = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-14 15:32:41 +02:00
|
|
|
if (QFileInfo::exists(QCoreApplication::applicationDirPath() + "/portable")) {
|
|
|
|
portable = true;
|
|
|
|
}
|
|
|
|
|
2018-01-05 02:23:49 +01:00
|
|
|
// Root path = %APPDATA%/Chatterino or the folder that the executable resides in
|
|
|
|
QString rootPath;
|
|
|
|
if (portable) {
|
2018-04-14 15:32:41 +02:00
|
|
|
rootPath.append(QCoreApplication::applicationDirPath());
|
2018-01-05 02:23:49 +01:00
|
|
|
} else {
|
|
|
|
// Get settings path
|
|
|
|
rootPath.append(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
|
|
|
|
if (rootPath.isEmpty()) {
|
2018-04-27 22:11:19 +02:00
|
|
|
throw std::runtime_error("Error finding writable location for settings");
|
2018-01-05 02:23:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this->settingsFolderPath = rootPath;
|
|
|
|
|
|
|
|
if (!QDir().mkpath(this->settingsFolderPath)) {
|
2018-04-27 22:11:19 +02:00
|
|
|
throw std::runtime_error("Error creating settings folder");
|
2018-01-05 02:23:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this->customFolderPath = rootPath + "/Custom";
|
|
|
|
|
|
|
|
if (!QDir().mkpath(this->customFolderPath)) {
|
2018-04-27 22:11:19 +02:00
|
|
|
throw std::runtime_error("Error creating custom folder");
|
2018-01-05 02:23:49 +01:00
|
|
|
}
|
|
|
|
|
2018-01-19 22:45:33 +01:00
|
|
|
this->cacheFolderPath = rootPath + "/Cache";
|
|
|
|
|
|
|
|
if (!QDir().mkpath(this->cacheFolderPath)) {
|
2018-04-27 22:11:19 +02:00
|
|
|
throw std::runtime_error("Error creating cache folder");
|
2018-01-19 22:45:33 +01:00
|
|
|
}
|
|
|
|
|
2018-01-28 14:23:55 +01:00
|
|
|
this->logsFolderPath = rootPath + "/Logs";
|
|
|
|
|
|
|
|
if (!QDir().mkpath(this->logsFolderPath)) {
|
2018-04-27 22:11:19 +02:00
|
|
|
throw std::runtime_error("Error creating logs folder");
|
2018-01-28 14:23:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this->channelsLogsFolderPath = this->logsFolderPath + "/Channels";
|
|
|
|
|
|
|
|
if (!QDir().mkpath(this->channelsLogsFolderPath)) {
|
2018-04-27 22:11:19 +02:00
|
|
|
throw std::runtime_error("Error creating channel logs folder");
|
2018-01-28 14:23:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this->whispersLogsFolderPath = this->logsFolderPath + "/Whispers";
|
|
|
|
|
|
|
|
if (!QDir().mkpath(this->whispersLogsFolderPath)) {
|
2018-04-27 22:11:19 +02:00
|
|
|
throw std::runtime_error("Error creating whisper logs folder");
|
2018-01-28 14:23:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this->mentionsLogsFolderPath = this->logsFolderPath + "/Mentions";
|
|
|
|
|
|
|
|
if (!QDir().mkpath(this->mentionsLogsFolderPath)) {
|
2018-04-27 22:11:19 +02:00
|
|
|
throw std::runtime_error("Error creating mentions logs folder");
|
2018-01-28 14:23:55 +01:00
|
|
|
}
|
2018-01-05 02:23:49 +01:00
|
|
|
}
|
|
|
|
|
2018-01-28 14:23:55 +01:00
|
|
|
bool PathManager::createFolder(const QString &folderPath)
|
|
|
|
{
|
|
|
|
return QDir().mkpath(folderPath);
|
|
|
|
}
|
|
|
|
|
2018-01-05 02:23:49 +01:00
|
|
|
} // namespace singletons
|
|
|
|
} // namespace chatterino
|