mirror-chatterino2/src/singletons/Paths.cpp

150 lines
3.5 KiB
C++
Raw Normal View History

2018-06-28 19:46:45 +02:00
#include "singletons/Paths.hpp"
#include "singletons/Settings.hpp"
#include <QCoreApplication>
2018-04-20 00:15:57 +02:00
#include <QCryptographicHash>
#include <QDir>
#include <QStandardPaths>
2018-06-13 13:27:10 +02:00
#include <cassert>
2019-09-15 15:45:04 +02:00
#include "common/Modes.hpp"
2018-06-26 14:09:39 +02:00
#include "util/CombinePath.hpp"
2018-06-21 13:02:34 +02:00
using namespace std::literals;
namespace chatterino {
2018-06-28 19:51:07 +02:00
Paths *Paths::instance = nullptr;
2018-06-13 13:27:10 +02:00
2018-06-28 19:51:07 +02:00
Paths::Paths()
{
2018-08-02 14:23:27 +02:00
this->instance = this;
2018-06-21 13:02:34 +02:00
this->initAppFilePathHash();
2018-06-21 13:02:34 +02:00
this->initCheckPortable();
this->initRootDirectory();
2018-06-21 13:02:34 +02:00
this->initSubDirectories();
}
2018-06-28 19:51:07 +02:00
bool Paths::createFolder(const QString &folderPath)
{
return QDir().mkpath(folderPath);
}
2018-06-28 19:51:07 +02:00
bool Paths::isPortable()
2018-05-28 18:25:19 +02:00
{
2019-09-15 15:45:04 +02:00
return Modes::getInstance().isPortable;
2018-06-21 13:02:34 +02:00
}
QString Paths::cacheDirectory()
{
static const auto pathSetting = [] {
QStringSetting cachePathSetting("/cache/path");
cachePathSetting.connect([](const auto &newPath, auto) {
QDir().mkpath(newPath); //
});
return cachePathSetting;
}();
auto path = pathSetting.getValue();
if (path.isEmpty())
2018-10-21 13:43:02 +02:00
{
return this->cacheDirectory_;
}
return path;
}
2018-06-28 19:51:07 +02:00
void Paths::initAppFilePathHash()
2018-06-21 13:02:34 +02:00
{
this->applicationFilePathHash =
2018-08-06 21:17:03 +02:00
QCryptographicHash::hash(
QCoreApplication::applicationFilePath().toUtf8(),
QCryptographicHash::Sha224)
2018-06-21 13:02:34 +02:00
.toBase64()
.mid(0, 32)
.replace("+", "-")
.replace("/", "x");
}
2018-06-28 19:51:07 +02:00
void Paths::initCheckPortable()
2018-06-21 13:02:34 +02:00
{
2018-08-06 21:17:03 +02:00
this->portable_ = QFileInfo::exists(
combinePath(QCoreApplication::applicationDirPath(), "portable"));
2018-06-21 13:02:34 +02:00
}
void Paths::initRootDirectory()
2018-06-21 13:02:34 +02:00
{
2018-07-06 19:23:47 +02:00
assert(this->portable_.is_initialized());
2018-06-21 13:02:34 +02:00
2018-08-06 21:17:03 +02:00
// Root path = %APPDATA%/Chatterino or the folder that the executable
// resides in
2018-06-21 13:02:34 +02:00
this->rootAppDataDirectory = [&]() -> QString {
// portable
2018-10-21 13:43:02 +02:00
if (this->isPortable())
{
2018-06-21 13:02:34 +02:00
return QCoreApplication::applicationDirPath();
}
// permanent installation
2018-08-06 21:17:03 +02:00
QString path =
QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
2018-10-21 13:43:02 +02:00
if (path.isEmpty())
{
throw std::runtime_error("Could not create directory \""s +
path.toStdString() + "\"");
2018-06-21 13:02:34 +02:00
}
2018-08-06 21:17:03 +02:00
// create directory Chatterino2 instead of chatterino on windows because the
// ladder one is takes by chatterino 1 already
2018-06-21 13:02:34 +02:00
#ifdef Q_OS_WIN
path.replace("chatterino", "Chatterino");
path += "2";
#endif
return path;
}();
}
2018-06-28 19:51:07 +02:00
void Paths::initSubDirectories()
2018-06-21 13:02:34 +02:00
{
// required the app data directory to be set first
assert(!this->rootAppDataDirectory.isEmpty());
2018-08-06 21:17:03 +02:00
// create settings subdirectories and validate that they are created
// properly
2018-06-21 13:02:34 +02:00
auto makePath = [&](const std::string &name) -> QString {
2018-08-06 21:17:03 +02:00
auto path = combinePath(this->rootAppDataDirectory,
QString::fromStdString(name));
2018-06-21 13:02:34 +02:00
2018-10-21 13:43:02 +02:00
if (!QDir().mkpath(path))
{
throw std::runtime_error("Could not create directory \""s +
path.toStdString() + "\"");
2018-06-21 13:02:34 +02:00
}
return path;
};
makePath("");
this->settingsDirectory = makePath("Settings");
this->cacheDirectory_ = makePath("Cache");
2018-06-21 13:02:34 +02:00
this->messageLogDirectory = makePath("Logs");
this->miscDirectory = makePath("Misc");
2018-08-29 23:39:02 +02:00
this->twitchProfileAvatars = makePath("ProfileAvatars");
//QDir().mkdir(this->twitchProfileAvatars + "/twitch");
2018-05-28 18:25:19 +02:00
}
2018-07-07 11:41:01 +02:00
Paths *getPaths()
{
2018-08-02 14:23:27 +02:00
return Paths::instance;
2018-07-07 11:41:01 +02:00
}
} // namespace chatterino