2022-02-17 17:26:15 +00:00
|
|
|
#include "pathcheck.h"
|
|
|
|
|
2022-02-18 21:27:09 +00:00
|
|
|
QRegularExpression squawk("^squawk:\\/\\/");
|
|
|
|
QString Shared::downloadsPathCheck()
|
2022-02-17 17:26:15 +00:00
|
|
|
{
|
|
|
|
QSettings settings;
|
|
|
|
QVariant dpv = settings.value("downloadsPath");
|
|
|
|
QString path;
|
|
|
|
if (!dpv.isValid()) {
|
|
|
|
path = defaultDownloadsPath();
|
|
|
|
qDebug() << "no downloadsPath variable in config, using default" << path;
|
2022-02-18 21:27:09 +00:00
|
|
|
path = getAbsoluteWritablePath(path);
|
2022-02-17 17:26:15 +00:00
|
|
|
return path;
|
|
|
|
} else {
|
|
|
|
path = dpv.toString();
|
2022-02-18 21:27:09 +00:00
|
|
|
path = getAbsoluteWritablePath(path);
|
2022-02-17 17:26:15 +00:00
|
|
|
if (path.size() == 0) {
|
|
|
|
path = defaultDownloadsPath();
|
|
|
|
qDebug() << "falling back to the default downloads path" << path;
|
2022-02-18 21:27:09 +00:00
|
|
|
path = getAbsoluteWritablePath(path);
|
2022-02-17 17:26:15 +00:00
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-18 21:27:09 +00:00
|
|
|
QString Shared::defaultDownloadsPath()
|
2022-02-17 17:26:15 +00:00
|
|
|
{
|
|
|
|
return QStandardPaths::writableLocation(QStandardPaths::DownloadLocation) + "/" + QApplication::applicationName();
|
|
|
|
}
|
|
|
|
|
2022-02-18 21:27:09 +00:00
|
|
|
QString Shared::getAbsoluteWritablePath(const QString& path)
|
2022-02-17 17:26:15 +00:00
|
|
|
{
|
|
|
|
QDir location(path);
|
|
|
|
if (!location.exists()) {
|
2022-02-18 21:27:09 +00:00
|
|
|
bool res = location.mkpath(location.absolutePath());
|
2022-02-17 17:26:15 +00:00
|
|
|
if (!res) {
|
|
|
|
qDebug() << "couldn't create directory" << path;
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
2022-02-18 21:27:09 +00:00
|
|
|
QFileInfo info(location.absolutePath());
|
2022-02-17 17:26:15 +00:00
|
|
|
if (info.isWritable()) {
|
2022-02-18 21:27:09 +00:00
|
|
|
return location.absolutePath();
|
2022-02-17 17:26:15 +00:00
|
|
|
} else {
|
|
|
|
qDebug() << "directory" << path << "is not writable";
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
2022-02-18 21:27:09 +00:00
|
|
|
|
|
|
|
QString Shared::resolvePath(QString path)
|
|
|
|
{
|
|
|
|
QSettings settings;
|
|
|
|
QVariant dpv = settings.value("downloadsPath");
|
|
|
|
return path.replace(squawk, dpv.toString() + "/");
|
|
|
|
}
|
2022-02-19 18:31:49 +00:00
|
|
|
|
|
|
|
QString Shared::squawkifyPath(QString path)
|
|
|
|
{
|
|
|
|
QSettings settings;
|
|
|
|
QString current = settings.value("downloadsPath").toString();
|
|
|
|
|
|
|
|
if (path.startsWith(current)) {
|
|
|
|
path.replace(0, current.size() + 1, "squawk://");
|
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Shared::isSubdirectoryOfSettings(const QString& path)
|
|
|
|
{
|
|
|
|
|
|
|
|
QSettings settings;
|
|
|
|
QDir oldPath(settings.value("downloadsPath").toString());
|
|
|
|
QDir newPath(path);
|
|
|
|
|
|
|
|
return newPath.canonicalPath().startsWith(oldPath.canonicalPath());
|
|
|
|
}
|
|
|
|
|