first thoughts about downloads path changing

This commit is contained in:
Blue 2022-02-17 20:26:15 +03:00
parent da19eb86bb
commit 243edff8bd
Signed by: blue
GPG Key ID: 9B203B252A63EE38
11 changed files with 122 additions and 10 deletions

View File

@ -32,3 +32,4 @@ target_include_directories(squawk PRIVATE ${LMDB_INCLUDE_DIRS})
add_subdirectory(handlers)
add_subdirectory(passwordStorageEngines)
add_subdirectory(utils)

View File

@ -21,6 +21,8 @@
#include "../ui/squawk.h"
#include "signalcatcher.h"
#include "squawk.h"
#include "utils/pathcheck.h"
#include <QLibraryInfo>
#include <QSettings>
#include <QStandardPaths>
@ -28,6 +30,7 @@
#include <QtCore/QObject>
#include <QtCore/QThread>
#include <QtWidgets/QApplication>
#include <QDir>
int main(int argc, char *argv[])
{
@ -106,7 +109,13 @@ int main(int argc, char *argv[])
}
}
}
QString path = Utils::downloadsPathCheck();
if (path.size() > 0) {
settings.setValue("downloadsPath", path);
} else {
qDebug() << "couldn't initialize directory for downloads, quitting";
return -1;
}
Squawk w;

View File

@ -28,8 +28,11 @@ Core::NetworkAccess::NetworkAccess(QObject* parent):
manager(0),
storage("fileURLStorage"),
downloads(),
uploads()
uploads(),
currentPath()
{
QSettings settings;
currentPath = settings.value("downloadsPath").toString();
}
Core::NetworkAccess::~NetworkAccess()
@ -515,8 +518,7 @@ bool Core::NetworkAccess::checkAndAddToUploading(const QString& acc, const QStri
QString Core::NetworkAccess::prepareDirectory(const QString& jid)
{
QString path = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
path += "/" + QApplication::applicationName();
QString path = currentPath;
if (jid.size() > 0) {
path += "/" + jid;
}
@ -563,3 +565,8 @@ std::list<Shared::MessageInfo> Core::NetworkAccess::reportPathInvalid(const QStr
{
return storage.deletedFile(path);
}
void Core::NetworkAccess::moveFilesDirectory(const QString& newPath)
{
}

View File

@ -26,6 +26,7 @@
#include <QFileInfo>
#include <QFile>
#include <QStandardPaths>
#include <QSettings>
#include <set>
@ -65,6 +66,7 @@ public slots:
void downladFile(const QString& url);
void registerFile(const QString& url, const QString& account, const QString& jid, const QString& id);
void registerFile(const QString& url, const QString& path, const QString& account, const QString& jid, const QString& id);
void moveFilesDirectory(const QString& newPath);
private:
void startDownload(const std::list<Shared::MessageInfo>& msgs, const QString& url);
@ -87,6 +89,7 @@ private:
UrlStorage storage;
std::map<QString, Transfer*> downloads;
std::map<QString, Transfer*> uploads;
QString currentPath;
struct Transfer {
std::list<Shared::MessageInfo> messages;

View File

@ -0,0 +1,4 @@
target_sources(squawk PRIVATE
pathcheck.h
pathcheck.cpp
)

47
core/utils/pathcheck.cpp Normal file
View 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
View 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

View File

@ -24,8 +24,6 @@
#include <QColor>
#include <QRegularExpression>
// #include "KIO/OpenFileManagerWindowJob"
#include <vector>
namespace Shared {

View File

@ -6,6 +6,9 @@ PageGeneral::PageGeneral(QWidget* parent):
m_ui(new Ui::PageGeneral())
{
m_ui->setupUi(this);
QSettings settings;
m_ui->downloadsPathInput->setText(settings.value("downloadsPath").toString());
}
PageGeneral::~PageGeneral()

View File

@ -4,6 +4,7 @@
#include <QWidget>
#include <QScopedPointer>
#include <QVariant>
#include <QSettings>
namespace Ui
{

View File

@ -11,15 +11,33 @@
</rect>
</property>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="label">
<item row="1" column="0">
<widget class="QLabel" name="downloadsPathLabel">
<property name="text">
<string>Downloads path</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="downloadsPathInput"/>
<item row="1" column="1">
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing">
<number>6</number>
</property>
<item>
<widget class="QLineEdit" name="downloadsPathInput">
<property name="enabled">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="downloadsPathButton">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>