mirror-chatterino2/src/singletons/Paths.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

153 lines
3.7 KiB
C++
Raw Normal View History

2018-06-28 19:46:45 +02:00
#include "singletons/Paths.hpp"
#include "common/Modes.hpp"
#include "singletons/Settings.hpp"
#include "util/CombinePath.hpp"
#include <QCoreApplication>
2018-04-20 00:15:57 +02:00
#include <QCryptographicHash>
#include <QDir>
#include <QStandardPaths>
#include <cassert>
2018-06-21 13:02:34 +02:00
using namespace std::literals;
namespace chatterino {
2018-06-28 19:51:07 +02:00
Paths::Paths()
{
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);
}
bool Paths::isPortable() const
2018-05-28 18:25:19 +02:00
{
return Modes::instance().isPortable;
2018-06-21 13:02:34 +02:00
}
QString Paths::cacheDirectory() const
{
static const auto pathSetting = [] {
QStringSetting cachePathSetting("/cache/path");
cachePathSetting.connect([](const auto &newPath, auto) {
if (!newPath.isEmpty())
{
QDir().mkpath(newPath);
}
});
return cachePathSetting;
}();
auto path = pathSetting.getValue();
if (path.isEmpty())
{
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 =
QCryptographicHash::hash(
QCoreApplication::applicationFilePath().toUtf8(),
QCryptographicHash::Sha224)
.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-07-06 19:23:47 +02:00
this->portable_ = QFileInfo::exists(
2018-06-26 17:06:17 +02:00
combinePath(QCoreApplication::applicationDirPath(), "portable"));
2018-06-21 13:02:34 +02:00
}
void Paths::initRootDirectory()
2018-06-21 13:02:34 +02:00
{
assert(this->portable_.has_value());
2018-06-21 13:02:34 +02:00
// Root path = %APPDATA%/Chatterino or the folder that the executable
// resides in
this->rootAppDataDirectory = [&]() -> QString {
// portable
2024-01-30 17:28:36 +01:00
if (Modes::instance().isPortable)
2018-06-21 22:02:35 +02:00
{
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("Could not create directory \""s +
path.toStdString() + "\"");
2018-06-21 13:02:34 +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());
// create settings subdirectories and validate that they are created
// properly
auto makePath = [&](const QString &name) -> QString {
auto path = combinePath(this->rootAppDataDirectory, name);
2018-06-21 13:02:34 +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");
this->twitchProfileAvatars =
makePath(combinePath("ProfileAvatars", "twitch"));
this->pluginsDirectory = makePath("Plugins");
this->themesDirectory = makePath("Themes");
this->crashdumpDirectory = makePath("Crashes");
#ifdef Q_OS_WIN
this->ipcDirectory = makePath("IPC");
#else
// NOTE: We do *NOT* use IPC on non-Windows platforms.
// If we start, we should re-consider this directory.
this->ipcDirectory = "/tmp";
#endif
2018-05-28 18:25:19 +02:00
}
} // namespace chatterino