Downloads folder now is movable

This commit is contained in:
Blue 2022-02-19 21:31:49 +03:00
parent d8b5ccb2da
commit 73b1b58a96
Signed by: blue
GPG Key ID: 9B203B252A63EE38
16 changed files with 141 additions and 12 deletions

View File

@ -304,7 +304,11 @@ void Core::MessageHandler::performSending(Shared::Message data, bool newMessage)
//so, the final path changes. Let's assume it changes always since it costs me close to nothing //so, the final path changes. Let's assume it changes always since it costs me close to nothing
QString attachPath = data.getAttachPath(); QString attachPath = data.getAttachPath();
if (attachPath.size() > 0) { if (attachPath.size() > 0) {
changes.insert("attachPath", attachPath); QString squawkified = Shared::squawkifyPath(attachPath);
changes.insert("attachPath", squawkified);
if (attachPath != squawkified) {
data.setAttachPath(squawkified);
}
} }
if (ri != 0) { if (ri != 0) {

View File

@ -29,6 +29,7 @@
#include <shared/message.h> #include <shared/message.h>
#include <shared/messageinfo.h> #include <shared/messageinfo.h>
#include <shared/pathcheck.h>
namespace Core { namespace Core {

View File

@ -160,6 +160,7 @@ int main(int argc, char *argv[])
QObject::connect(&w, &Squawk::uploadVCard, squawk, &Core::Squawk::uploadVCard); QObject::connect(&w, &Squawk::uploadVCard, squawk, &Core::Squawk::uploadVCard);
QObject::connect(&w, &Squawk::responsePassword, squawk, &Core::Squawk::responsePassword); QObject::connect(&w, &Squawk::responsePassword, squawk, &Core::Squawk::responsePassword);
QObject::connect(&w, &Squawk::localPathInvalid, squawk, &Core::Squawk::onLocalPathInvalid); QObject::connect(&w, &Squawk::localPathInvalid, squawk, &Core::Squawk::onLocalPathInvalid);
QObject::connect(&w, &Squawk::changeDownloadsPath, squawk, &Core::Squawk::changeDownloadsPath);
QObject::connect(squawk, &Core::Squawk::newAccount, &w, &Squawk::newAccount); QObject::connect(squawk, &Core::Squawk::newAccount, &w, &Squawk::newAccount);
QObject::connect(squawk, &Core::Squawk::addContact, &w, &Squawk::addContact); QObject::connect(squawk, &Core::Squawk::addContact, &w, &Squawk::addContact);

View File

@ -438,10 +438,10 @@ void Core::NetworkAccess::onUploadProgress(qint64 bytesReceived, qint64 bytesTot
QString Core::NetworkAccess::getFileRemoteUrl(const QString& path) QString Core::NetworkAccess::getFileRemoteUrl(const QString& path)
{ {
QString p; QString p = Shared::squawkifyPath(path);
try { try {
p = storage.getUrl(path); p = storage.getUrl(p);
} catch (const Archive::NotFound& err) { } catch (const Archive::NotFound& err) {
} catch (...) { } catch (...) {
@ -574,10 +574,16 @@ std::list<Shared::MessageInfo> Core::NetworkAccess::reportPathInvalid(const QStr
void Core::NetworkAccess::moveFilesDirectory(const QString& newPath) void Core::NetworkAccess::moveFilesDirectory(const QString& newPath)
{ {
QDir dir; QDir dir(currentPath);
if (dir.rename(currentPath, newPath)) { bool success = true;
currentPath = newPath; qDebug() << "moving" << currentPath << "to" << newPath;
} else { for (QFileInfo fileInfo : dir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System)) {
QString fileName = fileInfo.fileName();
success = dir.rename(fileName, newPath + QDir::separator() + fileName) && success;
}
if (!success) {
qDebug() << "couldn't move downloads directory, most probably downloads will be broken"; qDebug() << "couldn't move downloads directory, most probably downloads will be broken";
} }
currentPath = newPath;
} }

View File

@ -785,3 +785,9 @@ void Core::Squawk::onLocalPathInvalid(const QString& path)
} }
} }
} }
void Core::Squawk::changeDownloadsPath(const QString& path)
{
network.moveFilesDirectory(path);
}

View File

@ -123,6 +123,7 @@ public slots:
void uploadVCard(const QString& account, const Shared::VCard& card); void uploadVCard(const QString& account, const Shared::VCard& card);
void responsePassword(const QString& account, const QString& password); void responsePassword(const QString& account, const QString& password);
void onLocalPathInvalid(const QString& path); void onLocalPathInvalid(const QString& path);
void changeDownloadsPath(const QString& path);
private: private:
typedef std::deque<Account*> Accounts; typedef std::deque<Account*> Accounts;

View File

@ -53,3 +53,26 @@ QString Shared::resolvePath(QString path)
QVariant dpv = settings.value("downloadsPath"); QVariant dpv = settings.value("downloadsPath");
return path.replace(squawk, dpv.toString() + "/"); return path.replace(squawk, dpv.toString() + "/");
} }
QString Shared::squawkifyPath(QString path)
{
QSettings settings;
QString current = settings.value("downloadsPath").toString();
if (path.startsWith(current)) {
path.replace(0, current.size() + 1, "squawk://");
}
return path;
}
bool Shared::isSubdirectoryOfSettings(const QString& path)
{
QSettings settings;
QDir oldPath(settings.value("downloadsPath").toString());
QDir newPath(path);
return newPath.canonicalPath().startsWith(oldPath.canonicalPath());
}

View File

@ -13,10 +13,13 @@
namespace Shared { namespace Shared {
QString downloadsPathCheck(); QString downloadsPathCheck();
QString downloadsPathCheck(QString path);
QString defaultDownloadsPath(); QString defaultDownloadsPath();
QString getAbsoluteWritablePath(const QString& path); QString getAbsoluteWritablePath(const QString& path);
QString resolvePath(QString path); QString resolvePath(QString path);
QString squawkifyPath(QString path);
bool isSubdirectoryOfSettings(const QString& path);
} }

View File

@ -125,6 +125,7 @@ void Squawk::onPreferences()
preferences = new Settings(); preferences = new Settings();
preferences->setAttribute(Qt::WA_DeleteOnClose); preferences->setAttribute(Qt::WA_DeleteOnClose);
connect(preferences, &Settings::destroyed, this, &Squawk::onPreferencesClosed); connect(preferences, &Settings::destroyed, this, &Squawk::onPreferencesClosed);
connect(preferences, &Settings::changeDownloadsPath, this, &Squawk::changeDownloadsPath);
preferences->show(); preferences->show();
} else { } else {

View File

@ -80,6 +80,7 @@ signals:
void uploadVCard(const QString& account, const Shared::VCard& card); void uploadVCard(const QString& account, const Shared::VCard& card);
void responsePassword(const QString& account, const QString& password); void responsePassword(const QString& account, const QString& password);
void localPathInvalid(const QString& path); void localPathInvalid(const QString& path);
void changeDownloadsPath(const QString& path);
public slots: public slots:
void writeSettings(); void writeSettings();

View File

@ -3,14 +3,58 @@
PageGeneral::PageGeneral(QWidget* parent): PageGeneral::PageGeneral(QWidget* parent):
QWidget(parent), QWidget(parent),
m_ui(new Ui::PageGeneral()) m_ui(new Ui::PageGeneral()),
dialog(nullptr)
{ {
m_ui->setupUi(this); m_ui->setupUi(this);
QSettings settings; QSettings settings;
m_ui->downloadsPathInput->setText(settings.value("downloadsPath").toString()); m_ui->downloadsPathInput->setText(settings.value("downloadsPath").toString());
connect(m_ui->downloadsPathButton, &QPushButton::clicked, this, &PageGeneral::onBrowseButtonClicked);
} }
PageGeneral::~PageGeneral() PageGeneral::~PageGeneral()
{ {
if (dialog != nullptr) {
dialog->deleteLater();
}
}
void PageGeneral::onBrowseButtonClicked()
{
if (dialog == nullptr) {
QSettings settings;
dialog = new QFileDialog(this, tr("Select where downloads folder is going to be"), settings.value("downloadsPath").toString());
dialog->setAttribute(Qt::WA_DeleteOnClose);
dialog->setAcceptMode(QFileDialog::AcceptSave); //I find it the most convinient way
dialog->setFileMode(QFileDialog::AnyFile); //Otherwise the directory is supposed to be
dialog->setOption(QFileDialog::ShowDirsOnly, true); //selected and not to be navigated
dialog->setOption(QFileDialog::DontConfirmOverwrite, true);
dialog->setModal(true);
connect(dialog, &QFileDialog::accepted, this, &PageGeneral::onDialogAccepted);
connect(dialog, &QFileDialog::destroyed, this, &PageGeneral::onDialogDestroyed);
dialog->show();
} else {
dialog->show();
dialog->raise();
dialog->activateWindow();
}
}
void PageGeneral::onDialogAccepted()
{
QStringList files = dialog->selectedFiles();
QString path;
if (files.size() > 0) {
path = files[0];
} else {
path = dialog->directory().canonicalPath();
}
m_ui->downloadsPathInput->setText(path);
emit variableModified("downloadsPath", path);
}
void PageGeneral::onDialogDestroyed()
{
dialog = nullptr;
} }

View File

@ -5,6 +5,7 @@
#include <QScopedPointer> #include <QScopedPointer>
#include <QVariant> #include <QVariant>
#include <QSettings> #include <QSettings>
#include <QFileDialog>
namespace Ui namespace Ui
{ {
@ -24,8 +25,14 @@ public:
signals: signals:
void variableModified(const QString& key, const QVariant& value); void variableModified(const QString& key, const QVariant& value);
private slots:
void onBrowseButtonClicked();
void onDialogAccepted();
void onDialogDestroyed();
private: private:
QScopedPointer<Ui::PageGeneral> m_ui; QScopedPointer<Ui::PageGeneral> m_ui;
QFileDialog* dialog;
}; };
#endif // PAGEGENERAL_H #endif // PAGEGENERAL_H

View File

@ -33,7 +33,7 @@
<item> <item>
<widget class="QPushButton" name="downloadsPathButton"> <widget class="QPushButton" name="downloadsPathButton">
<property name="text"> <property name="text">
<string>PushButton</string> <string>Browse</string>
</property> </property>
</widget> </widget>
</item> </item>

View File

@ -42,11 +42,21 @@ void Settings::apply()
for (const std::pair<const QString, QVariant>& pair: modifiedSettings) { for (const std::pair<const QString, QVariant>& pair: modifiedSettings) {
if (pair.first == "style") { if (pair.first == "style") {
Shared::Global::setStyle(pair.second.toString()); Shared::Global::setStyle(pair.second.toString());
settings.setValue(pair.first, pair.second);
} else if (pair.first == "theme") { } else if (pair.first == "theme") {
Shared::Global::setTheme(pair.second.toString()); Shared::Global::setTheme(pair.second.toString());
settings.setValue(pair.first, pair.second);
} else if (pair.first == "downloadsPath") {
QString path = pair.second.toString();
if (!Shared::isSubdirectoryOfSettings(path)) {
path = Shared::getAbsoluteWritablePath(path);
if (path.size() > 0) {
settings.setValue(pair.first, path);
emit changeDownloadsPath(path);
}
}
} }
settings.setValue(pair.first, pair.second);
} }
modifiedSettings.clear(); modifiedSettings.clear();

View File

@ -5,8 +5,10 @@
#include <QListWidgetItem> #include <QListWidgetItem>
#include <QScopedPointer> #include <QScopedPointer>
#include <QSettings> #include <QSettings>
#include <QDir>
#include "shared/global.h" #include "shared/global.h"
#include "shared/pathcheck.h"
namespace Ui namespace Ui
{ {
@ -23,6 +25,9 @@ public:
Settings(QWidget* parent = nullptr); Settings(QWidget* parent = nullptr);
~Settings(); ~Settings();
signals:
void changeDownloadsPath(const QString& path);
public slots: public slots:
void apply(); void apply();
void confirm(); void confirm();

View File

@ -10,6 +10,9 @@
<height>363</height> <height>363</height>
</rect> </rect>
</property> </property>
<property name="windowTitle">
<string>Preferences</string>
</property>
<layout class="QGridLayout" name="gridLayout"> <layout class="QGridLayout" name="gridLayout">
<property name="leftMargin"> <property name="leftMargin">
<number>0</number> <number>0</number>
@ -183,10 +186,23 @@
<verstretch>0</verstretch> <verstretch>0</verstretch>
</sizepolicy> </sizepolicy>
</property> </property>
<widget class="PageGeneral" name="General" native="true"/> <widget class="PageGeneral" name="General"/>
<widget class="PageAppearance" name="Appearance" native="true"/> <widget class="PageAppearance" name="Appearance"/>
</widget> </widget>
</item> </item>
<item row="2" column="0">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout> </layout>
</widget> </widget>
</item> </item>