2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2017 Vladimir Golovnev <glassez@yandex.ru>
4 * Copyright (C) 2010 Christophe Dumez <chris@qbittorrent.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * In addition, as a special exception, the copyright holders give permission to
21 * link this program with the OpenSSL project's "OpenSSL" library (or with
22 * modified versions of it that use the same license as the "OpenSSL" library),
23 * and distribute the linked executables. You must obey the GNU General Public
24 * License in all respects for all of the code used other than "OpenSSL". If you
25 * modify file(s), you may extend this exception to your version of the file(s),
26 * but you are not obligated to do so. If you do not wish to do so, delete this
27 * exception statement from your version.
30 #include "bandwidthscheduler.h"
38 #include "base/preferences.h"
40 using namespace std::chrono_literals
;
42 BandwidthScheduler::BandwidthScheduler(QObject
*parent
)
44 , m_lastAlternative(false)
46 connect(&m_timer
, &QTimer::timeout
, this, &BandwidthScheduler::onTimeout
);
49 void BandwidthScheduler::start()
51 m_lastAlternative
= isTimeForAlternative();
52 emit
bandwidthLimitRequested(m_lastAlternative
);
54 // Timeout regularly to accommodate for external system clock changes
55 // eg from the user or from a timesync utility
59 bool BandwidthScheduler::isTimeForAlternative() const
61 const Preferences
*const pref
= Preferences::instance();
63 QTime start
= pref
->getSchedulerStartTime();
64 QTime end
= pref
->getSchedulerEndTime();
65 const QTime now
= QTime::currentTime();
66 const Scheduler::Days schedulerDays
= pref
->getSchedulerDays();
67 const int day
= QDate::currentDate().dayOfWeek();
68 bool alternative
= false;
72 std::swap(start
, end
);
76 if ((start
<= now
) && (end
>= now
))
78 switch (schedulerDays
)
80 case Scheduler::Days::EveryDay
:
81 alternative
= !alternative
;
83 case Scheduler::Days::Monday
:
84 case Scheduler::Days::Tuesday
:
85 case Scheduler::Days::Wednesday
:
86 case Scheduler::Days::Thursday
:
87 case Scheduler::Days::Friday
:
88 case Scheduler::Days::Saturday
:
89 case Scheduler::Days::Sunday
:
91 const int offset
= static_cast<int>(Scheduler::Days::Monday
) - 1;
92 const int dayOfWeek
= static_cast<int>(schedulerDays
) - offset
;
94 alternative
= !alternative
;
97 case Scheduler::Days::Weekday
:
98 if ((day
>= 1) && (day
<= 5))
99 alternative
= !alternative
;
101 case Scheduler::Days::Weekend
:
102 if ((day
== 6) || (day
== 7))
103 alternative
= !alternative
;
114 void BandwidthScheduler::onTimeout()
116 const bool alternative
= isTimeForAlternative();
118 if (alternative
!= m_lastAlternative
)
120 m_lastAlternative
= alternative
;
121 emit
bandwidthLimitRequested(alternative
);