diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 9369cb7..1836349 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -32,3 +32,4 @@ target_include_directories(squawk PRIVATE ${LMDB_INCLUDE_DIRS}) add_subdirectory(handlers) add_subdirectory(passwordStorageEngines) +add_subdirectory(utils) diff --git a/core/main.cpp b/core/main.cpp index cdfca7e..570bad4 100644 --- a/core/main.cpp +++ b/core/main.cpp @@ -21,6 +21,8 @@ #include "../ui/squawk.h" #include "signalcatcher.h" #include "squawk.h" +#include "utils/pathcheck.h" + #include #include #include @@ -28,6 +30,7 @@ #include #include #include +#include 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; diff --git a/core/networkaccess.cpp b/core/networkaccess.cpp index c2cd65d..48d26aa 100644 --- a/core/networkaccess.cpp +++ b/core/networkaccess.cpp @@ -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 Core::NetworkAccess::reportPathInvalid(const QStr { return storage.deletedFile(path); } + +void Core::NetworkAccess::moveFilesDirectory(const QString& newPath) +{ + +} diff --git a/core/networkaccess.h b/core/networkaccess.h index 89d0633..cf24fc4 100644 --- a/core/networkaccess.h +++ b/core/networkaccess.h @@ -26,6 +26,7 @@ #include #include #include +#include #include @@ -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& msgs, const QString& url); @@ -87,6 +89,7 @@ private: UrlStorage storage; std::map downloads; std::map uploads; + QString currentPath; struct Transfer { std::list messages; diff --git a/core/utils/CMakeLists.txt b/core/utils/CMakeLists.txt new file mode 100644 index 0000000..6722da7 --- /dev/null +++ b/core/utils/CMakeLists.txt @@ -0,0 +1,4 @@ +target_sources(squawk PRIVATE + pathcheck.h + pathcheck.cpp +) diff --git a/core/utils/pathcheck.cpp b/core/utils/pathcheck.cpp new file mode 100644 index 0000000..3f1b86a --- /dev/null +++ b/core/utils/pathcheck.cpp @@ -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 ""; + } +} diff --git a/core/utils/pathcheck.h b/core/utils/pathcheck.h new file mode 100644 index 0000000..8618012 --- /dev/null +++ b/core/utils/pathcheck.h @@ -0,0 +1,21 @@ +#ifndef PATHCHECK_H +#define PATHCHECK_H + +#include +#include +#include +#include +#include +#include +#include + +namespace Utils { + +QString downloadsPathCheck(); +QString defaultDownloadsPath(); + +QString getCanonicalWritablePath(const QString& path); + +} + +#endif // PATHCHECK_H diff --git a/shared/utils.h b/shared/utils.h index 6dcb141..564e2e6 100644 --- a/shared/utils.h +++ b/shared/utils.h @@ -24,8 +24,6 @@ #include #include -// #include "KIO/OpenFileManagerWindowJob" - #include namespace Shared { diff --git a/ui/widgets/settings/pagegeneral.cpp b/ui/widgets/settings/pagegeneral.cpp index e448f80..56cb610 100644 --- a/ui/widgets/settings/pagegeneral.cpp +++ b/ui/widgets/settings/pagegeneral.cpp @@ -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() diff --git a/ui/widgets/settings/pagegeneral.h b/ui/widgets/settings/pagegeneral.h index b8c30c5..cb89bfa 100644 --- a/ui/widgets/settings/pagegeneral.h +++ b/ui/widgets/settings/pagegeneral.h @@ -4,6 +4,7 @@ #include #include #include +#include namespace Ui { diff --git a/ui/widgets/settings/pagegeneral.ui b/ui/widgets/settings/pagegeneral.ui index 9921715..e010980 100644 --- a/ui/widgets/settings/pagegeneral.ui +++ b/ui/widgets/settings/pagegeneral.ui @@ -11,15 +11,33 @@ - - + + Downloads path - - + + + + 6 + + + + + false + + + + + + + PushButton + + + +