Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
feat(files): Add maximum size to autoaccept downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
sphaerophoria committed Oct 12, 2018
1 parent 939f2c8 commit c8716e9
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 15 deletions.
19 changes: 19 additions & 0 deletions src/persistence/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ void Settings::loadGlobal()
QStandardPaths::locate(QStandardPaths::HomeLocation, QString(),
QStandardPaths::LocateDirectory))
.toString();
autoAcceptMaxSize =
static_cast<size_t>(s.value("autoAcceptMaxSize", 20 << 20 /*20 MB*/).toLongLong());
stylePreference = static_cast<StyleType>(s.value("stylePreference", 1).toInt());
}
s.endGroup();
Expand Down Expand Up @@ -499,6 +501,7 @@ void Settings::saveGlobal()
s.setValue("busySound", busySound);
s.setValue("fauxOfflineMessaging", fauxOfflineMessaging);
s.setValue("autoSaveEnabled", autoSaveEnabled);
s.setValue("autoAcceptMaxSize", static_cast<qlonglong>(autoAcceptMaxSize));
s.setValue("globalAutoAcceptDir", globalAutoAcceptDir);
s.setValue("stylePreference", static_cast<int>(stylePreference));
}
Expand Down Expand Up @@ -1509,6 +1512,22 @@ void Settings::setGlobalAutoAcceptDir(const QString& newValue)
}
}

size_t Settings::getMaxAutoAcceptSize() const
{
QMutexLocker locker{&bigLock};
return autoAcceptMaxSize;
}

void Settings::setMaxAutoAcceptSize(size_t size)
{
QMutexLocker locker{&bigLock};

if (size != autoAcceptMaxSize) {
autoAcceptMaxSize = size;
emit autoAcceptMaxSizeChanged(autoAcceptMaxSize);
}
}

const QFont& Settings::getChatMessageFont() const
{
QMutexLocker locker(&bigLock);
Expand Down
5 changes: 5 additions & 0 deletions src/persistence/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ public slots:
void enableLoggingChanged(bool enabled);
void autoAwayTimeChanged(int minutes);
void globalAutoAcceptDirChanged(const QString& path);
void autoAcceptMaxSizeChanged(size_t size);
void checkUpdatesChanged(bool enabled);
void widgetDataChanged(const QString& key);

Expand Down Expand Up @@ -438,6 +439,9 @@ public slots:
QString getGlobalAutoAcceptDir() const;
void setGlobalAutoAcceptDir(const QString& dir);

size_t getMaxAutoAcceptSize() const;
void setMaxAutoAcceptSize(size_t size);

bool getAutoGroupInvite(const ToxPk& id) const override;
void setAutoGroupInvite(const ToxPk& id, bool accept) override;

Expand Down Expand Up @@ -627,6 +631,7 @@ public slots:
QHash<QString, QString> autoAccept;
bool autoSaveEnabled;
QString globalAutoAcceptDir;
size_t autoAcceptMaxSize;

QList<Request> friendRequests;

Expand Down
14 changes: 9 additions & 5 deletions src/widget/form/chatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,12 +339,16 @@ void ChatForm::onFileRecvRequest(ToxFile file)

const Settings& settings = Settings::getInstance();
QString autoAcceptDir = settings.getAutoAcceptDir(f->getPublicKey());
// there is auto-accept for that contact
if (!autoAcceptDir.isEmpty()) {

if (autoAcceptDir.isEmpty() && settings.getAutoSaveEnabled()) {
autoAcceptDir = settings.getGlobalAutoAcceptDir();
}

auto maxAutoAcceptSize = settings.getMaxAutoAcceptSize();
bool autoAcceptSizeCheckPassed = maxAutoAcceptSize == 0 || maxAutoAcceptSize >= file.filesize;

if (!autoAcceptDir.isEmpty() && autoAcceptSizeCheckPassed) {
tfWidget->autoAcceptTransfer(autoAcceptDir);
// global autosave to global directory
} else if (settings.getAutoSaveEnabled()) {
tfWidget->autoAcceptTransfer(settings.getGlobalAutoAcceptDir());
}

Widget::getInstance()->updateFriendActivity(f);
Expand Down
11 changes: 11 additions & 0 deletions src/widget/form/settings/generalform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "ui_generalsettings.h"

#include <QFileDialog>
#include <cmath>

#include "src/core/core.h"
#include "src/core/coreav.h"
Expand Down Expand Up @@ -148,8 +149,10 @@ GeneralForm::GeneralForm(SettingsWidget* myParent)

bodyUI->autoAwaySpinBox->setValue(s.getAutoAwayTime());
bodyUI->autoSaveFilesDir->setText(s.getGlobalAutoAcceptDir());
bodyUI->maxAutoAcceptSizeMB->setValue(static_cast<double>(s.getMaxAutoAcceptSize()) / 1024 / 1024);
bodyUI->autoacceptFiles->setChecked(s.getAutoSaveEnabled());


#ifndef QTOX_PLATFORM_EXT
bodyUI->autoAwayLabel->setEnabled(false); // these don't seem to change the appearance of the widgets,
bodyUI->autoAwaySpinBox->setEnabled(false); // though they are unusable
Expand Down Expand Up @@ -244,6 +247,14 @@ void GeneralForm::on_autoSaveFilesDir_clicked()
bodyUI->autoSaveFilesDir->setText(directory);
}

void GeneralForm::on_maxAutoAcceptSizeMB_editingFinished()
{
auto newMaxSizeMB = bodyUI->maxAutoAcceptSizeMB->value();
auto newMaxSizeB = std::lround(newMaxSizeMB * 1024 * 1024);

Settings::getInstance().setMaxAutoAcceptSize(newMaxSizeB);
}

void GeneralForm::on_checkUpdates_stateChanged()
{
Settings::getInstance().setCheckUpdates(bodyUI->checkUpdates->isChecked());
Expand Down
1 change: 1 addition & 0 deletions src/widget/form/settings/generalform.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ private slots:
void on_cbFauxOfflineMessaging_stateChanged();

void on_autoacceptFiles_stateChanged();
void on_maxAutoAcceptSizeMB_editingFinished();
void on_autoSaveFilesDir_clicked();
void on_checkUpdates_stateChanged();

Expand Down
34 changes: 24 additions & 10 deletions src/widget/form/settings/generalsettings.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>1312</width>
<height>580</height>
<height>511</height>
</rect>
</property>
<property name="windowTitle">
Expand Down Expand Up @@ -39,8 +39,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>1276</width>
<height>587</height>
<width>1298</width>
<height>497</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_4" stretch="0,0">
Expand Down Expand Up @@ -278,6 +278,13 @@ instead of closing itself.</string>
<property name="leftMargin">
<number>0</number>
</property>
<item row="2" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Default directory to save files:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QPushButton" name="autoSaveFilesDir">
<property name="sizePolicy">
Expand All @@ -291,13 +298,6 @@ instead of closing itself.</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Default directory to save files:</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QCheckBox" name="autoacceptFiles">
<property name="sizePolicy">
Expand All @@ -314,6 +314,20 @@ instead of closing itself.</string>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="maxAutoAcceptSizeLabel">
<property name="text">
<string>Max autoaccept file size (0 to disable):</string>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QDoubleSpinBox" name="maxAutoAcceptSizeMB">
<property name="suffix">
<string> MB</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
Expand Down

0 comments on commit c8716e9

Please sign in to comment.