Sync translations from Transifex and run lupdate
[qBittorrent.git] / src / base / bittorrent / bandwidthscheduler.cpp
blobbf0f493bc6c8afda1982f3af17174e8e0f858bc8
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 <utility>
34 #include <QDate>
35 #include <QTime>
36 #include <QTimer>
38 #include "base/preferences.h"
40 BandwidthScheduler::BandwidthScheduler(QObject *parent)
41 : QObject(parent)
42 , m_lastAlternative(false)
44 connect(&m_timer, &QTimer::timeout, this, &BandwidthScheduler::onTimeout);
47 void BandwidthScheduler::start()
49 m_lastAlternative = isTimeForAlternative();
50 emit bandwidthLimitRequested(m_lastAlternative);
52 // Timeout regularly to accommodate for external system clock changes
53 // eg from the user or from a timesync utility
54 m_timer.start(30000);
57 bool BandwidthScheduler::isTimeForAlternative() const
59 const Preferences *const pref = Preferences::instance();
61 QTime start = pref->getSchedulerStartTime();
62 QTime end = pref->getSchedulerEndTime();
63 const QTime now = QTime::currentTime();
64 const int schedulerDays = pref->getSchedulerDays();
65 const int day = QDate::currentDate().dayOfWeek();
66 bool alternative = false;
68 if (start > end)
70 std::swap(start, end);
71 alternative = true;
74 if ((start <= now) && (end >= now))
76 switch (schedulerDays)
78 case EVERY_DAY:
79 alternative = !alternative;
80 break;
81 case WEEK_ENDS:
82 if ((day == 6) || (day == 7))
83 alternative = !alternative;
84 break;
85 case WEEK_DAYS:
86 if ((day != 6) && (day != 7))
87 alternative = !alternative;
88 break;
89 default:
90 if (day == (schedulerDays - 2))
91 alternative = !alternative;
95 return alternative;
98 void BandwidthScheduler::onTimeout()
100 const bool alternative = isTimeForAlternative();
102 if (alternative != m_lastAlternative)
104 m_lastAlternative = alternative;
105 emit bandwidthLimitRequested(alternative);