squawk/shared/pathcheck.cpp

56 lines
1.6 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() + "/");
}