Skip to content

Commit 6ab5f73

Browse files
committed
Schedule Pause bittorrent session with scheduler
1 parent 7172a71 commit 6ab5f73

12 files changed

Lines changed: 105 additions & 3 deletions

File tree

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
.vscode/
2+
.zed/
3+
.idea/
24

35
src/gui/geoip/GeoIP.dat
46
src/gui/geoip/GeoIP.dat.gz
@@ -41,3 +43,10 @@ src/icons/skin/build-icons/icons/*.png
4143

4244
# CMake build directory
4345
build/
46+
47+
48+
# clang cache
49+
.cache/
50+
51+
# Node.js dependencies
52+
node_modules/

src/base/bittorrent/bandwidthscheduler.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ void BandwidthScheduler::start()
4949
{
5050
m_lastAlternative = isTimeForAlternative();
5151
emit bandwidthLimitRequested(m_lastAlternative);
52+
emit sessionStateChangeRequested(Preferences::instance()->getSchedulerSessionState());
5253

5354
// Timeout regularly to accommodate for external system clock changes
5455
// eg from the user or from a timesync utility
@@ -119,4 +120,8 @@ void BandwidthScheduler::onTimeout()
119120
m_lastAlternative = alternative;
120121
emit bandwidthLimitRequested(alternative);
121122
}
123+
124+
// Always emit pause state when schedule is active
125+
if (alternative)
126+
emit sessionStateChangeRequested(Preferences::instance()->getSchedulerSessionState());
122127
}

src/base/bittorrent/bandwidthscheduler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include <QObject>
3333
#include <QTimer>
34+
#include "base/preferences.h"
3435

3536
class BandwidthScheduler : public QObject
3637
{
@@ -43,6 +44,7 @@ class BandwidthScheduler : public QObject
4344

4445
signals:
4546
void bandwidthLimitRequested(bool alternative);
47+
void sessionStateChangeRequested(Scheduler::AlternativeSessionState requestedState);
4648

4749
private:
4850
bool isTimeForAlternative() const;

src/base/bittorrent/session.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
#include "base/pathfwd.h"
3838
#include "base/tagset.h"
39+
#include "base/preferences.h"
3940
#include "addtorrenterror.h"
4041
#include "addtorrentparams.h"
4142
#include "categoryoptions.h"
@@ -256,6 +257,7 @@ namespace BitTorrent
256257
virtual void setUploadSpeedLimit(int limit) = 0;
257258
virtual bool isAltGlobalSpeedLimitEnabled() const = 0;
258259
virtual void setAltGlobalSpeedLimitEnabled(bool enabled) = 0;
260+
virtual void setAlternativeSessionState(Scheduler::AlternativeSessionState requestedState) = 0;
259261
virtual bool isBandwidthSchedulerEnabled() const = 0;
260262
virtual void setBandwidthSchedulerEnabled(bool enabled) = 0;
261263

@@ -497,6 +499,7 @@ namespace BitTorrent
497499
void paused();
498500
void resumed();
499501
void speedLimitModeChanged(bool alternative);
502+
void sessionStateChanged(Scheduler::AlternativeSessionState requestedState);
500503
void statsUpdated();
501504
void subcategoriesSupportChanged();
502505
void tagAdded(const Tag &tag);

src/base/bittorrent/sessionimpl.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2355,6 +2355,8 @@ void SessionImpl::enableBandwidthScheduler()
23552355
m_bwScheduler = new BandwidthScheduler(this);
23562356
connect(m_bwScheduler.data(), &BandwidthScheduler::bandwidthLimitRequested
23572357
, this, &SessionImpl::setAltGlobalSpeedLimitEnabled);
2358+
connect(m_bwScheduler.data(), &BandwidthScheduler::sessionStateChangeRequested
2359+
, this, &SessionImpl::setAlternativeSessionState);
23582360
}
23592361
m_bwScheduler->start();
23602362
}
@@ -3645,10 +3647,27 @@ void SessionImpl::setAltGlobalSpeedLimitEnabled(const bool enabled)
36453647
// Save new state to remember it on startup
36463648
m_isAltGlobalSpeedLimitEnabled = enabled;
36473649
applyBandwidthLimits();
3650+
36483651
// Notify
36493652
emit speedLimitModeChanged(m_isAltGlobalSpeedLimitEnabled);
36503653
}
36513654

3655+
void SessionImpl::setAlternativeSessionState(Scheduler::AlternativeSessionState requestedState)
3656+
{
3657+
auto *pref = Preferences::instance();
3658+
pref->setSchedulerSessionState(requestedState);
3659+
3660+
// Apply session pause/resume based on state and alt speed limit state
3661+
// This might be expanded with new states in the future, but for now we only have Paused and Limited
3662+
if (isAltGlobalSpeedLimitEnabled() && (requestedState == Scheduler::AlternativeSessionState::Paused))
3663+
pause();
3664+
else
3665+
resume();
3666+
3667+
// Notify
3668+
emit sessionStateChanged(requestedState);
3669+
}
3670+
36523671
bool SessionImpl::isBandwidthSchedulerEnabled() const
36533672
{
36543673
return m_isBandwidthSchedulerEnabled;

src/base/bittorrent/sessionimpl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ namespace BitTorrent
231231
void setUploadSpeedLimit(int limit) override;
232232
bool isAltGlobalSpeedLimitEnabled() const override;
233233
void setAltGlobalSpeedLimitEnabled(bool enabled) override;
234+
void setAlternativeSessionState(Scheduler::AlternativeSessionState requestedState) override;
234235
bool isBandwidthSchedulerEnabled() const override;
235236
void setBandwidthSchedulerEnabled(bool enabled) override;
236237

@@ -239,7 +240,7 @@ namespace BitTorrent
239240
int saveResumeDataInterval() const override;
240241
void setSaveResumeDataInterval(int value) override;
241242
std::chrono::minutes saveStatisticsInterval() const override;
242-
void setSaveStatisticsInterval(std::chrono::minutes value) override;
243+
void setSaveStatisticsInterval(std::chrono::minutes timeInMinutes) override;
243244
int shutdownTimeout() const override;
244245
void setShutdownTimeout(int value) override;
245246
int port() const override;

src/base/preferences.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,20 @@ void Preferences::setSchedulerDays(const Scheduler::Days days)
698698
setValue(u"Preferences/Scheduler/days"_s, days);
699699
}
700700

701+
Scheduler::AlternativeSessionState Preferences::getSchedulerSessionState() const
702+
{
703+
return value(u"Preferences/Scheduler/alt_session_state"_s, Scheduler::AlternativeSessionState::Limited);
704+
}
705+
706+
void Preferences::setSchedulerSessionState(const Scheduler::AlternativeSessionState state)
707+
{
708+
if (state == getSchedulerSessionState())
709+
return;
710+
711+
setValue(u"Preferences/Scheduler/alt_session_state"_s, state);
712+
}
713+
714+
701715
// Search
702716
bool Preferences::isSearchEnabled() const
703717
{

src/base/preferences.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ namespace Scheduler
5959
Sunday = 9
6060
};
6161
Q_ENUM_NS(Days)
62+
63+
enum class AlternativeSessionState : int
64+
{
65+
Limited = 0,
66+
Paused = 1
67+
};
68+
Q_ENUM_NS(AlternativeSessionState)
69+
70+
6271
}
6372

6473
namespace DNS
@@ -163,6 +172,8 @@ class Preferences final : public QObject
163172
void setSchedulerEndTime(const QTime &time);
164173
Scheduler::Days getSchedulerDays() const;
165174
void setSchedulerDays(Scheduler::Days days);
175+
void setSchedulerSessionState(Scheduler::AlternativeSessionState state);
176+
Scheduler::AlternativeSessionState getSchedulerSessionState() const;
166177

167178
// Search
168179
bool isSearchEnabled() const;

src/gui/optionsdialog.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,7 @@ void OptionsDialog::loadSpeedTabOptions()
10831083
m_ui->timeEditScheduleFrom->setTime(pref->getSchedulerStartTime());
10841084
m_ui->timeEditScheduleTo->setTime(pref->getSchedulerEndTime());
10851085
m_ui->comboBoxScheduleDays->setCurrentIndex(static_cast<int>(pref->getSchedulerDays()));
1086+
m_ui->comboBoxScheduleActions->setCurrentIndex(static_cast<int>(pref->getSchedulerSessionState()));
10861087

10871088
m_ui->checkLimituTPConnections->setChecked(session->isUTPRateLimited());
10881089
m_ui->checkLimitTransportOverhead->setChecked(session->includeOverheadInLimits());
@@ -1106,6 +1107,7 @@ void OptionsDialog::loadSpeedTabOptions()
11061107
connect(m_ui->timeEditScheduleFrom, &QDateTimeEdit::timeChanged, this, &ThisType::enableApplyButton);
11071108
connect(m_ui->timeEditScheduleTo, &QDateTimeEdit::timeChanged, this, &ThisType::enableApplyButton);
11081109
connect(m_ui->comboBoxScheduleDays, qComboBoxCurrentIndexChanged, this, &ThisType::enableApplyButton);
1110+
connect(m_ui->comboBoxScheduleActions, qComboBoxCurrentIndexChanged, this, &ThisType::enableApplyButton);
11091111

11101112
connect(m_ui->checkLimituTPConnections, &QAbstractButton::toggled, this, &ThisType::enableApplyButton);
11111113
connect(m_ui->checkLimitTransportOverhead, &QAbstractButton::toggled, this, &ThisType::enableApplyButton);
@@ -1132,6 +1134,7 @@ void OptionsDialog::saveSpeedTabOptions() const
11321134
pref->setSchedulerStartTime(m_ui->timeEditScheduleFrom->time());
11331135
pref->setSchedulerEndTime(m_ui->timeEditScheduleTo->time());
11341136
pref->setSchedulerDays(static_cast<Scheduler::Days>(m_ui->comboBoxScheduleDays->currentIndex()));
1137+
pref->setSchedulerSessionState(static_cast<Scheduler::AlternativeSessionState>(m_ui->comboBoxScheduleActions->currentIndex()));
11351138

11361139
session->setUTPRateLimited(m_ui->checkLimituTPConnections->isChecked());
11371140
session->setIncludeOverheadInLimits(m_ui->checkLimitTransportOverhead->isChecked());

src/gui/optionsdialog.ui

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2563,7 +2563,7 @@ readme[0-9].txt: filter 'readme1.txt', 'readme2.txt' but not 'readme10.txt'.</st
25632563
<item row="2" column="0" colspan="4">
25642564
<widget class="QGroupBox" name="groupBoxSchedule">
25652565
<property name="title">
2566-
<string>Schedule &amp;the use of alternative rate limits</string>
2566+
<string>Scheduler</string>
25672567
</property>
25682568
<property name="checkable">
25692569
<bool>true</bool>
@@ -2674,6 +2674,27 @@ readme[0-9].txt: filter 'readme1.txt', 'readme2.txt' but not 'readme10.txt'.</st
26742674
</item>
26752675
</widget>
26762676
</item>
2677+
<item row="2" column="0">
2678+
<widget class="QLabel" name="label_9">
2679+
<property name="text">
2680+
<string>Do:</string>
2681+
</property>
2682+
</widget>
2683+
</item>
2684+
<item row="2" column="1">
2685+
<widget class="QComboBox" name="comboBoxScheduleActions">
2686+
<item>
2687+
<property name="text">
2688+
<string>Use alternative limits</string>
2689+
</property>
2690+
</item>
2691+
<item>
2692+
<property name="text">
2693+
<string>Pause bittorrent session</string>
2694+
</property>
2695+
</item>
2696+
</widget>
2697+
</item>
26772698
</layout>
26782699
</widget>
26792700
</item>

0 commit comments

Comments
 (0)