2018-06-28 19:46:45 +02:00
|
|
|
#include "singletons/Paths.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>
|
2018-06-13 13:27:10 +02:00
|
|
|
#include <cassert>
|
2018-01-05 02:23:49 +01:00
|
|
|
|
2018-06-26 14:09:39 +02:00
|
|
|
#include "util/CombinePath.hpp"
|
2018-06-21 13:02:34 +02:00
|
|
|
|
2018-01-05 02:23:49 +01:00
|
|
|
namespace chatterino {
|
|
|
|
|
2018-06-13 13:27:10 +02:00
|
|
|
PathManager *PathManager::instance = nullptr;
|
|
|
|
|
2018-06-21 13:02:34 +02:00
|
|
|
PathManager::PathManager()
|
2018-01-05 02:23:49 +01:00
|
|
|
{
|
2018-06-21 13:02:34 +02:00
|
|
|
this->initAppFilePathHash();
|
2018-01-05 02:23:49 +01:00
|
|
|
|
2018-06-21 13:02:34 +02:00
|
|
|
this->initCheckPortable();
|
|
|
|
this->initAppDataDirectory();
|
|
|
|
this->initSubDirectories();
|
2018-01-05 02:23:49 +01:00
|
|
|
}
|
|
|
|
|
2018-06-21 13:02:34 +02:00
|
|
|
void PathManager::initInstance()
|
2018-06-13 13:27:10 +02:00
|
|
|
{
|
|
|
|
assert(!instance);
|
|
|
|
|
2018-06-21 13:02:34 +02:00
|
|
|
instance = new PathManager();
|
2018-06-13 13:27:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PathManager *PathManager::getInstance()
|
|
|
|
{
|
|
|
|
assert(instance);
|
|
|
|
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2018-01-28 14:23:55 +01:00
|
|
|
bool PathManager::createFolder(const QString &folderPath)
|
|
|
|
{
|
|
|
|
return QDir().mkpath(folderPath);
|
|
|
|
}
|
|
|
|
|
2018-05-28 18:25:19 +02:00
|
|
|
bool PathManager::isPortable()
|
|
|
|
{
|
2018-06-21 13:02:34 +02:00
|
|
|
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 =
|
2018-06-26 17:06:17 +02:00
|
|
|
QFileInfo::exists(combinePath(QCoreApplication::applicationDirPath(), "portable"));
|
2018-06-21 13:02:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void PathManager::initAppDataDirectory()
|
|
|
|
{
|
|
|
|
assert(this->portable.is_initialized());
|
|
|
|
|
|
|
|
// Root path = %APPDATA%/Chatterino or the folder that the executable resides in
|
|
|
|
|
|
|
|
this->rootAppDataDirectory = [&]() -> QString {
|
|
|
|
// portable
|
2018-06-21 22:02:35 +02:00
|
|
|
if (this->isPortable()) {
|
2018-06-21 13:02:34 +02:00
|
|
|
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 {
|
|
|
|
|
2018-06-26 17:06:17 +02:00
|
|
|
auto path = combinePath(this->rootAppDataDirectory, QString::fromStdString(name));
|
2018-06-21 13:02:34 +02:00
|
|
|
|
|
|
|
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");
|
2018-05-28 18:25:19 +02:00
|
|
|
}
|
|
|
|
|
2018-01-05 02:23:49 +01:00
|
|
|
} // namespace chatterino
|