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(handlers)
add_subdirectory(passwordStorageEngines) add_subdirectory(passwordStorageEngines)
add_subdirectory(utils)

View File

@ -21,6 +21,8 @@
#include "../ui/squawk.h" #include "../ui/squawk.h"
#include "signalcatcher.h" #include "signalcatcher.h"
#include "squawk.h" #include "squawk.h"
#include "utils/pathcheck.h"
#include <QLibraryInfo> #include <QLibraryInfo>
#include <QSettings> #include <QSettings>
#include <QStandardPaths> #include <QStandardPaths>
@ -28,6 +30,7 @@
#include <QtCore/QObject> #include <QtCore/QObject>
#include <QtCore/QThread> #include <QtCore/QThread>
#include <QtWidgets/QApplication> #include <QtWidgets/QApplication>
#include <QDir>
int main(int argc, char *argv[]) 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; Squawk w;

View File

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

View File

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

View File

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

View File

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

View File

@ -11,15 +11,33 @@
</rect> </rect>
</property> </property>
<layout class="QFormLayout" name="formLayout"> <layout class="QFormLayout" name="formLayout">
<item row="0" column="0"> <item row="1" column="0">
<widget class="QLabel" name="label"> <widget class="QLabel" name="downloadsPathLabel">
<property name="text"> <property name="text">
<string>Downloads path</string> <string>Downloads path</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="0" column="1"> <item row="1" column="1">
<widget class="QLineEdit" name="downloadsPathInput"/> <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> </item>
</layout> </layout>
</widget> </widget>