2018-06-28 19:46:45 +02:00
|
|
|
#include "singletons/Paths.hpp"
|
2018-01-05 02:23:49 +01:00
|
|
|
|
2018-08-19 16:20:14 +02:00
|
|
|
#include "singletons/Settings.hpp"
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2019-10-05 16:40:04 +02:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2018-01-05 02:23:49 +01:00
|
|
|
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-01-05 02:23:49 +01:00
|
|
|
{
|
2018-08-02 14:23:27 +02:00
|
|
|
this->instance = this;
|
|
|
|
|
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();
|
2019-10-05 16:40:04 +02:00
|
|
|
this->initRootDirectory();
|
2018-06-21 13:02:34 +02:00
|
|
|
this->initSubDirectories();
|
2018-01-05 02:23:49 +01:00
|
|
|
}
|
|
|
|
|
2018-06-28 19:51:07 +02:00
|
|
|
bool Paths::createFolder(const QString &folderPath)
|
2018-01-28 14:23:55 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-08-19 16:20:14 +02:00
|
|
|
QString Paths::cacheDirectory()
|
|
|
|
{
|
2019-05-10 23:28:05 +02:00
|
|
|
static const auto pathSetting = [] {
|
2018-08-19 16:20:14 +02:00
|
|
|
QStringSetting cachePathSetting("/cache/path");
|
|
|
|
|
|
|
|
cachePathSetting.connect([](const auto &newPath, auto) {
|
|
|
|
QDir().mkpath(newPath); //
|
|
|
|
});
|
|
|
|
|
|
|
|
return cachePathSetting;
|
2019-05-10 23:28:05 +02:00
|
|
|
}();
|
|
|
|
|
|
|
|
auto path = pathSetting.getValue();
|
2018-08-19 16:20:14 +02:00
|
|
|
|
2019-05-08 08:51:14 +02:00
|
|
|
if (path.isEmpty())
|
2018-10-21 13:43:02 +02:00
|
|
|
{
|
2018-08-19 16:20:14 +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
|
|
|
}
|
|
|
|
|
2019-10-05 16:40:04 +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())
|
|
|
|
{
|
2019-10-05 16:40:04 +02:00
|
|
|
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))
|
|
|
|
{
|
2019-10-05 16:40:04 +02:00
|
|
|
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");
|
2018-08-19 16:20:14 +02:00
|
|
|
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");
|
2019-09-10 23:55:43 +02:00
|
|
|
//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
|
|
|
}
|
|
|
|
|
2018-01-05 02:23:49 +01:00
|
|
|
} // namespace chatterino
|