Skip to content

Commit 26cb53d

Browse files
committed
WIP: rework of code
- use combo box, update gui - add session state to Preferences - add new signal to Bandwidth Scheduler - no WebGUI yet
1 parent 615b9d2 commit 26cb53d

11 files changed

Lines changed: 99 additions & 18 deletions

src/app/application.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,8 +1048,8 @@ int Application::exec()
10481048
// Otherwise, session will start unpaused regardless of the Schedule Pause Session setting being enabled,
10491049
// and Alternative Speed Limits active
10501050
// Should find a better way to handle this
1051-
BitTorrent::Session::instance()->applySessionState(BitTorrent::Session::instance()->isAltGlobalSpeedLimitEnabled() &&
1052-
BitTorrent::Session::instance()->isSchedulePauseSessionEnabled());
1051+
//BitTorrent::Session::instance()->applySessionState(BitTorrent::Session::instance()->isAltGlobalSpeedLimitEnabled() &&
1052+
// BitTorrent::Session::instance()->isSchedulePauseSessionEnabled());
10531053

10541054

10551055
});

src/base/bittorrent/bandwidthscheduler.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <QTime>
3737

3838
#include "base/preferences.h"
39+
#include "base/bittorrent/session.h"
3940

4041
using namespace std::chrono_literals;
4142

@@ -49,12 +50,31 @@ void BandwidthScheduler::start()
4950
{
5051
m_lastAlternative = isTimeForAlternative();
5152
emit bandwidthLimitRequested(m_lastAlternative);
53+
emit sessionPauseRequested(m_lastAlternative && getScheduleSessionPauseState());
5254

5355
// Timeout regularly to accommodate for external system clock changes
5456
// eg from the user or from a timesync utility
5557
m_timer.start(30s);
5658
}
5759

60+
bool BandwidthScheduler::getScheduleSessionPauseState()
61+
{
62+
const auto * const pref = Preferences::instance();
63+
64+
switch (pref->getSchedulerSessionState())
65+
{
66+
case Scheduler::AlternativeSessionState::Paused:
67+
return true;
68+
break;
69+
70+
//when not pausing, do nothing, fallback to default and return false
71+
case Scheduler::AlternativeSessionState::Limited:
72+
default:
73+
return false;
74+
break;
75+
}
76+
}
77+
5878
bool BandwidthScheduler::isTimeForAlternative() const
5979
{
6080
const Preferences *const pref = Preferences::instance();
@@ -113,10 +133,12 @@ bool BandwidthScheduler::isTimeForAlternative() const
113133
void BandwidthScheduler::onTimeout()
114134
{
115135
const bool alternative = isTimeForAlternative();
136+
const bool state = getScheduleSessionPauseState();
116137

117138
if (alternative != m_lastAlternative)
118139
{
119140
m_lastAlternative = alternative;
120141
emit bandwidthLimitRequested(alternative);
142+
emit sessionPauseRequested(alternative && state);
121143
}
122144
}

src/base/bittorrent/bandwidthscheduler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ class BandwidthScheduler : public QObject
4343

4444
signals:
4545
void bandwidthLimitRequested(bool alternative);
46+
void sessionPauseRequested(bool pause_requested);
4647

4748
private:
4849
bool isTimeForAlternative() const;
50+
bool getScheduleSessionPauseState();
4951
void onTimeout();
5052

5153
QTimer m_timer;

src/base/bittorrent/session.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@ namespace BitTorrent
501501
void paused();
502502
void resumed();
503503
void speedLimitModeChanged(bool alternative);
504+
void sessionStateChanged(bool state);
504505
void statsUpdated();
505506
void subcategoriesSupportChanged();
506507
void tagAdded(const Tag &tag);

src/base/bittorrent/sessionimpl.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2365,6 +2365,8 @@ void SessionImpl::enableBandwidthScheduler()
23652365
m_bwScheduler = new BandwidthScheduler(this);
23662366
connect(m_bwScheduler.data(), &BandwidthScheduler::bandwidthLimitRequested
23672367
, this, &SessionImpl::setAltGlobalSpeedLimitEnabled);
2368+
connect(m_bwScheduler.data(), &BandwidthScheduler::sessionPauseRequested
2369+
, this, &SessionImpl::setSchedulePauseSessionEnabled);
23682370
}
23692371
m_bwScheduler->start();
23702372
}
@@ -3647,7 +3649,7 @@ void SessionImpl::setAltGlobalSpeedLimitEnabled(const bool enabled)
36473649
// Save new state to remember it on startup
36483650
m_isAltGlobalSpeedLimitEnabled = enabled;
36493651
applyBandwidthLimits();
3650-
applySessionState(isAltGlobalSpeedLimitEnabled() && isSchedulePauseSessionEnabled());
3652+
36513653
// Notify
36523654
emit speedLimitModeChanged(m_isAltGlobalSpeedLimitEnabled);
36533655
}
@@ -3660,13 +3662,13 @@ bool SessionImpl::isSchedulePauseSessionEnabled() const
36603662

36613663
void SessionImpl::setSchedulePauseSessionEnabled(const bool enabled)
36623664
{
3663-
if (enabled == isSchedulePauseSessionEnabled()) return;
3665+
//if (enabled == isSchedulePauseSessionEnabled()) return;
36643666

36653667
// Save new state to remember it on startup
36663668
m_isSchedulePauseSessionEnabled = enabled;
3667-
3669+
applySessionState(isAltGlobalSpeedLimitEnabled() && isSchedulePauseSessionEnabled());
36683670
// Notify
3669-
//emit speedLimitModeChanged(m_isAltGlobalSpeedLimitEnabled);
3671+
emit sessionStateChanged(m_isSchedulePauseSessionEnabled);
36703672
}
36713673

36723674
bool SessionImpl::isBandwidthSchedulerEnabled() const

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: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,8 @@ void OptionsDialog::loadSpeedTabOptions()
10821082
m_ui->timeEditScheduleFrom->setTime(pref->getSchedulerStartTime());
10831083
m_ui->timeEditScheduleTo->setTime(pref->getSchedulerEndTime());
10841084
m_ui->comboBoxScheduleDays->setCurrentIndex(static_cast<int>(pref->getSchedulerDays()));
1085-
m_ui->checkBoxSchedulePauseSession->setChecked(session->isSchedulePauseSessionEnabled());
1085+
//m_ui->comboBoxScheduleActions->setCurrentIndex(static_cast<int>(session->isSchedulePauseSessionEnabled()));
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,7 +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);
1109-
connect(m_ui->checkBoxSchedulePauseSession, &QAbstractButton::toggled, this, &ThisType::enableApplyButton);
1110+
connect(m_ui->comboBoxScheduleActions, qComboBoxCurrentIndexChanged, this, &ThisType::enableApplyButton);
11101111

11111112
connect(m_ui->checkLimituTPConnections, &QAbstractButton::toggled, this, &ThisType::enableApplyButton);
11121113
connect(m_ui->checkLimitTransportOverhead, &QAbstractButton::toggled, this, &ThisType::enableApplyButton);
@@ -1133,7 +1134,8 @@ void OptionsDialog::saveSpeedTabOptions() const
11331134
pref->setSchedulerStartTime(m_ui->timeEditScheduleFrom->time());
11341135
pref->setSchedulerEndTime(m_ui->timeEditScheduleTo->time());
11351136
pref->setSchedulerDays(static_cast<Scheduler::Days>(m_ui->comboBoxScheduleDays->currentIndex()));
1136-
session->setSchedulePauseSessionEnabled(m_ui->checkBoxSchedulePauseSession->isChecked());
1137+
pref->setSchedulerSessionState(static_cast<Scheduler::AlternativeSessionState>(m_ui->comboBoxScheduleActions->currentIndex()));
1138+
session->setSchedulePauseSessionEnabled(m_ui->comboBoxScheduleActions->currentIndex());
11371139

11381140
session->setUTPRateLimited(m_ui->checkLimituTPConnections->isChecked());
11391141
session->setIncludeOverheadInLimits(m_ui->checkLimitTransportOverhead->isChecked());
@@ -2287,10 +2289,20 @@ void OptionsDialog::on_IPSubnetWhitelistButton_clicked()
22872289
dialog->open();
22882290
}
22892291

2290-
void OptionsDialog::on_groupBoxSchedule_clicked(bool checked)
2292+
void OptionsDialog::on_comboBoxScheduleActions_currentIndexChanged(int index)
22912293
{
2292-
if(!checked)
2293-
{
2294-
m_ui->checkBoxSchedulePauseSession->setChecked(checked);
2294+
Scheduler::AlternativeSessionState state;
2295+
bool isPaused = false;
2296+
switch (index) {
2297+
case 1:
2298+
state = Scheduler::AlternativeSessionState::Paused;
2299+
isPaused = true;
2300+
break;
2301+
case 0:
2302+
default:
2303+
state = Scheduler::AlternativeSessionState::Limited;
2304+
break;
22952305
}
2306+
Preferences::instance()->setSchedulerSessionState(state);
2307+
BitTorrent::Session::instance()->setSchedulePauseSessionEnabled(isPaused);
22962308
}

src/gui/optionsdialog.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ private slots:
113113
void setupWebUIAPIKey();
114114
#endif
115115

116-
void on_groupBoxSchedule_clicked(bool checked);
116+
void on_comboBoxScheduleActions_currentIndexChanged(int index);
117117

118118
private:
119119
void showEvent(QShowEvent *e) override;

src/gui/optionsdialog.ui

Lines changed: 18 additions & 4 deletions
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,13 +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="1" column="4">
2678-
<widget class="QCheckBox" name="checkBoxSchedulePauseSession">
2677+
<item row="2" column="0">
2678+
<widget class="QLabel" name="label_9">
26792679
<property name="text">
2680-
<string>Schedule Pause session</string>
2680+
<string>Do:</string>
26812681
</property>
26822682
</widget>
26832683
</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>
26842698
</layout>
26852699
</widget>
26862700
</item>

0 commit comments

Comments
 (0)