forked from blue/squawk
first thoughts about downloads path changing
This commit is contained in:
parent
da19eb86bb
commit
243edff8bd
11 changed files with 122 additions and 10 deletions
4
core/utils/CMakeLists.txt
Normal file
4
core/utils/CMakeLists.txt
Normal file
|
@ -0,0 +1,4 @@
|
|||
target_sources(squawk PRIVATE
|
||||
pathcheck.h
|
||||
pathcheck.cpp
|
||||
)
|
47
core/utils/pathcheck.cpp
Normal file
47
core/utils/pathcheck.cpp
Normal file
|
@ -0,0 +1,47 @@
|
|||
#include "pathcheck.h"
|
||||
|
||||
QString Utils::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 = getCanonicalWritablePath(path);
|
||||
return path;
|
||||
} else {
|
||||
path = dpv.toString();
|
||||
path = getCanonicalWritablePath(path);
|
||||
if (path.size() == 0) {
|
||||
path = defaultDownloadsPath();
|
||||
qDebug() << "falling back to the default downloads path" << path;
|
||||
path = getCanonicalWritablePath(path);
|
||||
}
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
||||
QString Utils::defaultDownloadsPath()
|
||||
{
|
||||
return QStandardPaths::writableLocation(QStandardPaths::DownloadLocation) + "/" + QApplication::applicationName();
|
||||
}
|
||||
|
||||
QString Utils::getCanonicalWritablePath(const QString& path)
|
||||
{
|
||||
QDir location(path);
|
||||
if (!location.exists()) {
|
||||
bool res = location.mkpath(location.canonicalPath());
|
||||
if (!res) {
|
||||
qDebug() << "couldn't create directory" << path;
|
||||
return "";
|
||||
}
|
||||
}
|
||||
QFileInfo info(location.canonicalPath());
|
||||
if (info.isWritable()) {
|
||||
return location.canonicalPath();
|
||||
} else {
|
||||
qDebug() << "directory" << path << "is not writable";
|
||||
return "";
|
||||
}
|
||||
}
|
21
core/utils/pathcheck.h
Normal file
21
core/utils/pathcheck.h
Normal file
|
@ -0,0 +1,21 @@
|
|||
#ifndef PATHCHECK_H
|
||||
#define PATHCHECK_H
|
||||
|
||||
#include <QString>
|
||||
#include <QStandardPaths>
|
||||
#include <QSettings>
|
||||
#include <QApplication>
|
||||
#include <QDir>
|
||||
#include <QFileInfo>
|
||||
#include <QDebug>
|
||||
|
||||
namespace Utils {
|
||||
|
||||
QString downloadsPathCheck();
|
||||
QString defaultDownloadsPath();
|
||||
|
||||
QString getCanonicalWritablePath(const QString& path);
|
||||
|
||||
}
|
||||
|
||||
#endif // PATHCHECK_H
|
Loading…
Add table
Add a link
Reference in a new issue