Skip to content

Commit

Permalink
Pause sync when behind a captive portal
Browse files Browse the repository at this point in the history
Fixes: #11533
  • Loading branch information
erikjv committed Mar 18, 2024
1 parent 7a8d329 commit 9e6aa25
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/gui/guiutility.cpp
Expand Up @@ -104,6 +104,15 @@ bool Utility::internetConnectionIsMetered()
return false;
}

bool OCC::Utility::internetThroughCaptivePortal()
{
if (auto *qNetInfo = QNetworkInformation::instance()) {
return qNetInfo->isBehindCaptivePortal();
}

return false;
}

void Utility::markDirectoryAsSyncRoot(const QString &path)
{
Q_ASSERT(getDirectorySyncRootMarking(path).isEmpty());
Expand Down
1 change: 1 addition & 0 deletions src/gui/guiutility.h
Expand Up @@ -52,6 +52,7 @@ namespace Utility {
QString socketApiSocketPath();

bool internetConnectionIsMetered();
bool internetThroughCaptivePortal();

void markDirectoryAsSyncRoot(const QString &path);
QString getDirectorySyncRootMarking(const QString &path);
Expand Down
20 changes: 20 additions & 0 deletions src/gui/networksettings.cpp
Expand Up @@ -96,6 +96,7 @@ NetworkSettings::NetworkSettings(QWidget *parent)
checkAccountLocalhost();

connect(_ui->pauseSyncWhenMeteredCheckbox, &QAbstractButton::clicked, this, &NetworkSettings::saveMeteredSettings);
connect(_ui->pauseSyncWhenBehindCaptivePortalCheckBox, &QAbstractButton::clicked, this, &NetworkSettings::saveCaptivePortalSettings);
}

NetworkSettings::~NetworkSettings()
Expand Down Expand Up @@ -195,6 +196,18 @@ void NetworkSettings::loadMeteredSettings()
_ui->pauseSyncWhenMeteredCheckbox->setVisible(false);
}

void NetworkSettings::loadCaptivePortalSettings()
{
if (QNetworkInformation *qNetInfo = QNetworkInformation::instance()) {
if (qNetInfo->supports(QNetworkInformation::Feature::CaptivePortal)) {
_ui->pauseSyncWhenBehindCaptivePortalCheckBox->setChecked(ConfigFile().pauseSyncWhenBehindCaptivePortal());
return;
}
}

_ui->pauseSyncWhenBehindCaptivePortalCheckBox->setVisible(false);
}

void NetworkSettings::saveProxySettings()
{
ConfigFile cfgFile;
Expand Down Expand Up @@ -256,6 +269,13 @@ void NetworkSettings::saveMeteredSettings()
FolderMan::instance()->scheduler()->setPauseSyncWhenMetered(pauseSyncWhenMetered);
}

void OCC::NetworkSettings::saveCaptivePortalSettings()
{
bool pauseWhenCaptivePortal = _ui->pauseSyncWhenBehindCaptivePortalCheckBox->isChecked();
ConfigFile().setPauseSyncWhenBehindCaptivePortal(pauseWhenCaptivePortal);
FolderMan::instance()->scheduler()->setPauseSyncWhenBehindCaptivePortal(pauseWhenCaptivePortal);
}

void NetworkSettings::checkEmptyProxyHost()
{
if (_ui->hostLineEdit->isEnabled() && _ui->hostLineEdit->text().isEmpty()) {
Expand Down
2 changes: 2 additions & 0 deletions src/gui/networksettings.h
Expand Up @@ -42,6 +42,7 @@ private slots:
void saveProxySettings();
void saveBWLimitSettings();
void saveMeteredSettings();
void saveCaptivePortalSettings();

/// Red marking of host field if empty and enabled
void checkEmptyProxyHost();
Expand All @@ -55,6 +56,7 @@ private slots:
void loadProxySettings();
void loadBWLimitSettings();
void loadMeteredSettings();
void loadCaptivePortalSettings();
CredentialManager *_credentialManager;


Expand Down
7 changes: 7 additions & 0 deletions src/gui/networksettings.ui
Expand Up @@ -21,6 +21,13 @@
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="pauseSyncWhenBehindCaptivePortalCheckBox">
<property name="text">
<string>Pause synchronization when behind a captive portal</string>
</property>
</widget>
</item>
<item>
<widget class="QGroupBox" name="proxyGroupBox">
<property name="enabled">
Expand Down
19 changes: 19 additions & 0 deletions src/gui/scheduling/syncscheduler.cpp
Expand Up @@ -107,6 +107,7 @@ class FolderPriorityQueue
SyncScheduler::SyncScheduler(FolderMan *parent)
: QObject(parent)
, _pauseSyncWhenMetered(ConfigFile().pauseSyncWhenMetered())
, _pauseSyncWhenBehindCaptivePortal(ConfigFile().pauseSyncWhenBehindCaptivePortal())
, _queue(new FolderPriorityQueue)
{
new ETagWatcher(parent, this);
Expand Down Expand Up @@ -176,6 +177,16 @@ void SyncScheduler::startNext()
}
}

if (_pauseSyncWhenBehindCaptivePortal && Utility::internetThroughCaptivePortal()) {
if (syncPriority == Priority::High) {
qCInfo(lcSyncScheduler) << "Scheduler is paused due to a captive portal, BUT next sync is HIGH priority, so allow sync to start";
} else {
enqueueFolder(_currentSync, syncPriority);
qCInfo(lcSyncScheduler) << "Scheduler is paused due to a captive portal, next sync is not started";
return;
}
}

connect(
_currentSync, &Folder::syncFinished, this,
[this](const SyncResult &result) {
Expand Down Expand Up @@ -213,3 +224,11 @@ void SyncScheduler::setPauseSyncWhenMetered(bool pauseSyncWhenMetered)
startNext();
}
}

void OCC::SyncScheduler::setPauseSyncWhenBehindCaptivePortal(bool pauseSyncWhenBehindCaptivePortal)
{
_pauseSyncWhenBehindCaptivePortal = pauseSyncWhenBehindCaptivePortal;
if (!pauseSyncWhenBehindCaptivePortal) {
startNext();
}
}
3 changes: 2 additions & 1 deletion src/gui/scheduling/syncscheduler.h
Expand Up @@ -57,13 +57,14 @@ class SyncScheduler : public QObject
bool hasCurrentRunningSyncRunning() const;

void setPauseSyncWhenMetered(bool pauseSyncWhenMetered);

void setPauseSyncWhenBehindCaptivePortal(bool pauseSyncWhenBehindCaptivePortal);

private:
void startNext();

bool _running = false;
bool _pauseSyncWhenMetered;
bool _pauseSyncWhenBehindCaptivePortal;
QPointer<Folder> _currentSync;
FolderPriorityQueue *_queue;
};
Expand Down
16 changes: 16 additions & 0 deletions src/libsync/configfile.cpp
Expand Up @@ -98,6 +98,12 @@ const QString pauseSyncWhenMeteredC()
{
return QStringLiteral("pauseWhenMetered");
}

const QString pauseSyncWhenBehindCaptivePortalC()
{
return QStringLiteral("pauseSyncWhenBehindCaptivePortal");
}

const QString moveToTrashC() { return QStringLiteral("moveToTrash"); }

const QString issuesWidgetFilterC()
Expand Down Expand Up @@ -708,6 +714,16 @@ void ConfigFile::setPauseSyncWhenMetered(bool isChecked)
setValue(pauseSyncWhenMeteredC(), isChecked);
}

bool ConfigFile::pauseSyncWhenBehindCaptivePortal() const
{
return getValue(pauseSyncWhenBehindCaptivePortalC(), {}, false).toBool();
}

void ConfigFile::setPauseSyncWhenBehindCaptivePortal(bool isChecked)
{
setValue(pauseSyncWhenBehindCaptivePortalC(), isChecked);
}

bool ConfigFile::moveToTrash() const
{
if (Theme::instance()->enableMoveToTrash()) {
Expand Down
3 changes: 3 additions & 0 deletions src/libsync/configfile.h
Expand Up @@ -141,6 +141,9 @@ class OWNCLOUDSYNC_EXPORT ConfigFile
bool pauseSyncWhenMetered() const;
void setPauseSyncWhenMetered(bool isChecked);

bool pauseSyncWhenBehindCaptivePortal() const;
void setPauseSyncWhenBehindCaptivePortal(bool isChecked);

/** If we should move the files deleted on the server in the trash */
bool moveToTrash() const;
void setMoveToTrash(bool);
Expand Down

0 comments on commit 9e6aa25

Please sign in to comment.