1
0
forked from blue/squawk
squawk/shared/pathcheck.cpp

79 lines
2.1 KiB
C++

#include "pathcheck.h"
QRegularExpression squawk("^squawk:\\/\\/");
QString Shared::downloadsPathCheck()
{
QSettings settings;
QVariant dpv = settings.value("downloadsPath");
QString path;
if (!dpv.isValid()) {
path = defaultDownloadsPath();
qDebug() << "no downloadsPath variable in config, using default" << path;
path = getAbsoluteWritablePath(path);
return path;
} else {
path = dpv.toString();
path = getAbsoluteWritablePath(path);
if (path.size() == 0) {
path = defaultDownloadsPath();
qDebug() << "falling back to the default downloads path" << path;
path = getAbsoluteWritablePath(path);
}
return path;
}
}
QString Shared::defaultDownloadsPath()
{
return QStandardPaths::writableLocation(QStandardPaths::DownloadLocation) + "/" + QApplication::applicationName();
}
QString Shared::getAbsoluteWritablePath(const QString& path)
{
QDir location(path);
if (!location.exists()) {
bool res = location.mkpath(location.absolutePath());
if (!res) {
qDebug() << "couldn't create directory" << path;
return "";
}
}
QFileInfo info(location.absolutePath());
if (info.isWritable()) {
return location.absolutePath();
} else {
qDebug() << "directory" << path << "is not writable";
return "";
}
}
QString Shared::resolvePath(QString path)
{
QSettings settings;
QVariant dpv = settings.value("downloadsPath");
return path.replace(squawk, dpv.toString() + "/");
}
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());
}