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-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();
|
|
|
|
this->initAppDataDirectory();
|
|
|
|
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
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
return this->portable_.get();
|
2018-06-21 13:02:34 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-06-28 19:51:07 +02:00
|
|
|
void Paths::initAppDataDirectory()
|
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-06-21 22:02:35 +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-06-21 13:02:34 +02:00
|
|
|
if (path.isEmpty()) {
|
2018-08-06 21:17:03 +02:00
|
|
|
throw std::runtime_error(
|
|
|
|
"Error finding writable location for settings");
|
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
|
|
|
|
|
|
|
if (!QDir().mkpath(path)) {
|
2018-08-06 21:17:03 +02:00
|
|
|
throw std::runtime_error(
|
|
|
|
"Error creating appdata path %appdata%/chatterino/" + name);
|
2018-06-21 13:02:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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-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
|