Enable customizing the save statistics time interval
[qBittorrent.git] / src / base / bittorrent / bandwidthscheduler.cpp
blobbb8d1d0a88900a9138927c6247eaf40073aef63d
1 /*
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"
32 #include <chrono>
33 #include <utility>
35 #include <QDate>
36 #include <QTime>
38 #include "base/preferences.h"
40 using namespace std::chrono_literals;
42 BandwidthScheduler::BandwidthScheduler(QObject *parent)
43 : QObject(parent)
45 connect(&m_timer, &QTimer::timeout, this, &BandwidthScheduler::onTimeout);
48 void BandwidthScheduler::start()
50 m_lastAlternative = isTimeForAlternative();
51 emit bandwidthLimitRequested(m_lastAlternative);
53 // Timeout regularly to accommodate for external system clock changes
54 // eg from the user or from a timesync utility
55 m_timer.start(30s);
58 bool BandwidthScheduler::isTimeForAlternative() const
60 const Preferences *const pref = Preferences::instance();
62 QTime start = pref->getSchedulerStartTime();
63 QTime end = pref->getSchedulerEndTime();
64 const QTime now = QTime::currentTime();
65 const Scheduler::Days schedulerDays = pref->getSchedulerDays();
66 const int day = QDate::currentDate().dayOfWeek();
67 bool alternative = false;
69 if (start > end)
71 std::swap(start, end);
72 alternative = true;
75 if ((start <= now) && (end >= now))
77 switch (schedulerDays)
79 case Scheduler::Days::EveryDay:
80 alternative = !alternative;
81 break;
82 case Scheduler::Days::Monday:
83 case Scheduler::Days::Tuesday:
84 case Scheduler::Days::Wednesday:
85 case Scheduler::Days::Thursday:
86 case Scheduler::Days::Friday:
87 case Scheduler::Days::Saturday:
88 case Scheduler::Days::Sunday:
90 const int offset = static_cast<int>(Scheduler::Days::Monday) - 1;
91 const int dayOfWeek = static_cast<int>(schedulerDays) - offset;
92 if (day == dayOfWeek)
93 alternative = !alternative;
95 break;
96 case Scheduler::Days::Weekday:
97 if ((day >= 1) && (day <= 5))
98 alternative = !alternative;
99 break;
100 case Scheduler::Days::Weekend:
101 if ((day == 6) || (day == 7))
102 alternative = !alternative;
103 break;
104 default:
105 Q_ASSERT(false);
106 break;
110 return alternative;
113 void BandwidthScheduler::onTimeout()
115 const bool alternative = isTimeForAlternative();
117 if (alternative != m_lastAlternative)
119 m_lastAlternative = alternative;
120 emit bandwidthLimitRequested(alternative);