forked from blue/squawk
downloaded files now stored with squawk:// prefix, that way I can move downloads folder without messing up the database
This commit is contained in:
parent
243edff8bd
commit
d8b5ccb2da
14 changed files with 75 additions and 44 deletions
|
@ -16,4 +16,6 @@ target_sources(squawk PRIVATE
|
|||
utils.h
|
||||
vcard.cpp
|
||||
vcard.h
|
||||
pathcheck.cpp
|
||||
pathcheck.h
|
||||
)
|
||||
|
|
55
shared/pathcheck.cpp
Normal file
55
shared/pathcheck.cpp
Normal file
|
@ -0,0 +1,55 @@
|
|||
#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() + "/");
|
||||
}
|
23
shared/pathcheck.h
Normal file
23
shared/pathcheck.h
Normal file
|
@ -0,0 +1,23 @@
|
|||
#ifndef PATHCHECK_H
|
||||
#define PATHCHECK_H
|
||||
|
||||
#include <QString>
|
||||
#include <QStandardPaths>
|
||||
#include <QSettings>
|
||||
#include <QApplication>
|
||||
#include <QDir>
|
||||
#include <QFileInfo>
|
||||
#include <QDebug>
|
||||
#include <QRegularExpression>
|
||||
|
||||
namespace Shared {
|
||||
|
||||
QString downloadsPathCheck();
|
||||
QString defaultDownloadsPath();
|
||||
|
||||
QString getAbsoluteWritablePath(const QString& path);
|
||||
QString resolvePath(QString path);
|
||||
|
||||
}
|
||||
|
||||
#endif // PATHCHECK_H
|
Loading…
Add table
Add a link
Reference in a new issue