1
0
forked from blue/squawk

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 untrusted user: 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
QString attachPath = data.getAttachPath();
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) {

View File

@ -29,6 +29,7 @@
#include <shared/message.h>
#include <shared/messageinfo.h>
#include <shared/pathcheck.h>
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::responsePassword, squawk, &Core::Squawk::responsePassword);
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::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 p;
QString p = Shared::squawkifyPath(path);
try {
p = storage.getUrl(path);
p = storage.getUrl(p);
} catch (const Archive::NotFound& err) {
} catch (...) {
@ -574,10 +574,16 @@ std::list<Shared::MessageInfo> Core::NetworkAccess::reportPathInvalid(const QStr
void Core::NetworkAccess::moveFilesDirectory(const QString& newPath)
{
QDir dir;
if (dir.rename(currentPath, newPath)) {
currentPath = newPath;
} else {
QDir dir(currentPath);
bool success = true;
qDebug() << "moving" << currentPath << "to" << newPath;
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";
}
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 responsePassword(const QString& account, const QString& password);
void onLocalPathInvalid(const QString& path);
void changeDownloadsPath(const QString& path);
private:
typedef std::deque<Account*> Accounts;

View File

@ -53,3 +53,26 @@ QString Shared::resolvePath(QString path)
QVariant dpv = settings.value("downloadsPath");
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 {
QString downloadsPathCheck();
QString downloadsPathCheck(QString path);
QString defaultDownloadsPath();
QString getAbsoluteWritablePath(const 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->setAttribute(Qt::WA_DeleteOnClose);
connect(preferences, &Settings::destroyed, this, &Squawk::onPreferencesClosed);
connect(preferences, &Settings::changeDownloadsPath, this, &Squawk::changeDownloadsPath);
preferences->show();
} else {

View File

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

View File

@ -3,14 +3,58 @@
PageGeneral::PageGeneral(QWidget* parent):
QWidget(parent),
m_ui(new Ui::PageGeneral())
m_ui(new Ui::PageGeneral()),
dialog(nullptr)
{
m_ui->setupUi(this);
QSettings settings;
m_ui->downloadsPathInput->setText(settings.value("downloadsPath").toString());
connect(m_ui->downloadsPathButton, &QPushButton::clicked, this, &PageGeneral::onBrowseButtonClicked);
}
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 <QVariant>
#include <QSettings>
#include <QFileDialog>
namespace Ui
{
@ -24,8 +25,14 @@ public:
signals:
void variableModified(const QString& key, const QVariant& value);
private slots:
void onBrowseButtonClicked();
void onDialogAccepted();
void onDialogDestroyed();
private:
QScopedPointer<Ui::PageGeneral> m_ui;
QFileDialog* dialog;
};
#endif // PAGEGENERAL_H

View File

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

View File

@ -42,11 +42,21 @@ void Settings::apply()
for (const std::pair<const QString, QVariant>& pair: modifiedSettings) {
if (pair.first == "style") {
Shared::Global::setStyle(pair.second.toString());
settings.setValue(pair.first, pair.second);
} else if (pair.first == "theme") {
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();

View File

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

View File

@ -10,6 +10,9 @@
<height>363</height>
</rect>
</property>
<property name="windowTitle">
<string>Preferences</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<property name="leftMargin">
<number>0</number>
@ -183,10 +186,23 @@
<verstretch>0</verstretch>
</sizepolicy>
</property>
<widget class="PageGeneral" name="General" native="true"/>
<widget class="PageAppearance" name="Appearance" native="true"/>
<widget class="PageGeneral" name="General"/>
<widget class="PageAppearance" name="Appearance"/>
</widget>
</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>
</widget>
</item>