Inhibit sleep regardless of activity

"Active torrents" is a somewhat unintuitive concept as a basis for
preventing sleep, as torrents can become active or inactive on the
network at any time. This brings some predictability to the inhibit
sleep option, and will inhibit sleep as long as there are unpaused
downloads or uploads, regardless of network activity.

Closes #1696, #4592, #4655, #7019, #7159, #7452
This commit is contained in:
Lukas Greib 2018-08-02 15:45:21 -04:00
parent e59841d35c
commit 48cd993c92
9 changed files with 72 additions and 36 deletions

View File

@ -1863,20 +1863,26 @@ TorrentHandle *Session::findTorrent(const InfoHash &hash) const
bool Session::hasActiveTorrents() const
{
foreach (TorrentHandle *const torrent, m_torrents)
if (TorrentFilter::ActiveTorrent.match(torrent))
return true;
return false;
return std::any_of(m_torrents.begin(), m_torrents.end(), [](TorrentHandle *torrent)
{
return TorrentFilter::ActiveTorrent.match(torrent);
});
}
bool Session::hasUnfinishedTorrents() const
{
foreach (TorrentHandle *const torrent, m_torrents)
if (!torrent->isSeed() && !torrent->isPaused())
return true;
return std::any_of(m_torrents.begin(), m_torrents.end(), [](const TorrentHandle *torrent)
{
return (!torrent->isSeed() && !torrent->isPaused());
});
}
return false;
bool Session::hasRunningSeed() const
{
return std::any_of(m_torrents.begin(), m_torrents.end(), [](const TorrentHandle *torrent)
{
return (torrent->isSeed() && !torrent->isPaused());
});
}
void Session::banIP(const QString &ip)

View File

@ -454,6 +454,7 @@ namespace BitTorrent
TorrentStatusReport torrentStatusReport() const;
bool hasActiveTorrents() const;
bool hasUnfinishedTorrents() const;
bool hasRunningSeed() const;
const SessionStatus &status() const;
const CacheStatus &cacheStatus() const;
quint64 getAlltimeDL() const;

View File

@ -235,14 +235,24 @@ void Preferences::setSplashScreenDisabled(bool b)
}
// Preventing from system suspend while active torrents are presented.
bool Preferences::preventFromSuspend() const
bool Preferences::preventFromSuspendWhenDownloading() const
{
return value("Preferences/General/PreventFromSuspend", false).toBool();
return value("Preferences/General/PreventFromSuspendWhenDownloading", false).toBool();
}
void Preferences::setPreventFromSuspend(bool b)
void Preferences::setPreventFromSuspendWhenDownloading(bool b)
{
setValue("Preferences/General/PreventFromSuspend", b);
setValue("Preferences/General/PreventFromSuspendWhenDownloading", b);
}
bool Preferences::preventFromSuspendWhenSeeding() const
{
return value("Preferences/General/PreventFromSuspendWhenSeeding", false).toBool();
}
void Preferences::setPreventFromSuspendWhenSeeding(bool b)
{
setValue("Preferences/General/PreventFromSuspendWhenSeeding", b);
}
#ifdef Q_OS_WIN
@ -1552,6 +1562,8 @@ void Preferences::setSpeedWidgetGraphEnable(int id, const bool enable)
void Preferences::upgrade()
{
SettingsStorage *settingsStorage = SettingsStorage::instance();
QStringList labels = value("TransferListFilters/customLabels").toStringList();
if (!labels.isEmpty()) {
QVariantMap categories = value("BitTorrent/Session/Categories").toMap();
@ -1560,10 +1572,17 @@ void Preferences::upgrade()
categories[label] = "";
}
setValue("BitTorrent/Session/Categories", categories);
SettingsStorage::instance()->removeValue("TransferListFilters/customLabels");
settingsStorage->removeValue("TransferListFilters/customLabels");
}
SettingsStorage::instance()->removeValue("Preferences/Downloads/AppendLabel");
settingsStorage->removeValue("Preferences/Downloads/AppendLabel");
// Inhibit sleep based on running downloads/available seeds rather than network activity.
if (value("Preferences/General/PreventFromSuspend", false).toBool()) {
setPreventFromSuspendWhenDownloading(true);
setPreventFromSuspendWhenSeeding(true);
}
settingsStorage->removeValue("Preferences/General/PreventFromSuspend");
}
void Preferences::apply()

View File

@ -123,8 +123,10 @@ public:
void setStartMinimized(bool b);
bool isSplashScreenDisabled() const;
void setSplashScreenDisabled(bool b);
bool preventFromSuspend() const;
void setPreventFromSuspend(bool b);
bool preventFromSuspendWhenDownloading() const;
void setPreventFromSuspendWhenDownloading(bool b);
bool preventFromSuspendWhenSeeding() const;
void setPreventFromSuspendWhenSeeding(bool b);
#ifdef Q_OS_WIN
bool WinStartup() const;
void setWinStartup(bool b);

View File

@ -342,7 +342,7 @@ MainWindow::MainWindow(QWidget *parent)
m_pwr = new PowerManagement(this);
m_preventTimer = new QTimer(this);
connect(m_preventTimer, &QTimer::timeout, this, &MainWindow::checkForActiveTorrents);
connect(m_preventTimer, &QTimer::timeout, this, &MainWindow::updatePowerManagementState);
// Configure BT session according to options
loadPreferences(false);
@ -1445,7 +1445,7 @@ void MainWindow::loadPreferences(bool configureSession)
showStatusBar(pref->isStatusbarDisplayed());
if (pref->preventFromSuspend() && !m_preventTimer->isActive()) {
if ((pref->preventFromSuspendWhenDownloading() || pref->preventFromSuspendWhenSeeding()) && !m_preventTimer->isActive()) {
m_preventTimer->start(PREVENT_SUSPEND_INTERVAL);
}
else {
@ -1971,9 +1971,11 @@ void MainWindow::on_actionAutoShutdown_toggled(bool enabled)
Preferences::instance()->setShutdownWhenDownloadsComplete(enabled);
}
void MainWindow::checkForActiveTorrents()
void MainWindow::updatePowerManagementState()
{
m_pwr->setActivityState(BitTorrent::Session::instance()->hasActiveTorrents());
const bool inhibitSuspend = (Preferences::instance()->preventFromSuspendWhenDownloading() && BitTorrent::Session::instance()->hasUnfinishedTorrents())
|| (Preferences::instance()->preventFromSuspendWhenSeeding() && BitTorrent::Session::instance()->hasRunningSeed());
m_pwr->setActivityState(inhibitSuspend);
}
#ifndef Q_OS_MAC

View File

@ -173,8 +173,8 @@ private slots:
void on_actionDownloadFromURL_triggered();
void on_actionExit_triggered();
void on_actionLock_triggered();
// Check for active torrents and set preventing from suspend state
void checkForActiveTorrents();
// Check for unpaused downloading or seeding torrents and prevent system suspend/sleep according to preferences
void updatePowerManagementState();
#if defined(Q_OS_WIN) || defined(Q_OS_MAC)
void checkProgramUpdate();
#endif

View File

@ -225,10 +225,12 @@ OptionsDialog::OptionsDialog(QWidget *parent)
connect(m_ui->checkShowSplash, &QAbstractButton::toggled, this, &ThisType::enableApplyButton);
connect(m_ui->checkProgramExitConfirm, &QAbstractButton::toggled, this, &ThisType::enableApplyButton);
connect(m_ui->checkProgramAutoExitConfirm, &QAbstractButton::toggled, this, &ThisType::enableApplyButton);
connect(m_ui->checkPreventFromSuspend, &QAbstractButton::toggled, this, &ThisType::enableApplyButton);
connect(m_ui->checkPreventFromSuspendWhenDownloading, &QAbstractButton::toggled, this, &ThisType::enableApplyButton);
connect(m_ui->checkPreventFromSuspendWhenSeeding, &QAbstractButton::toggled, this, &ThisType::enableApplyButton);
connect(m_ui->comboTrayIcon, qComboBoxCurrentIndexChanged, this, &ThisType::enableApplyButton);
#if (defined(Q_OS_UNIX) && !defined(Q_OS_MAC)) && !defined(QT_DBUS_LIB)
m_ui->checkPreventFromSuspend->setDisabled(true);
m_ui->checkPreventFromSuspendWhenDownloading->setDisabled(true);
m_ui->checkPreventFromSuspendWhenSeeding->setDisabled(true);
#endif
#if defined(Q_OS_WIN) || defined(Q_OS_MAC)
connect(m_ui->checkAssociateTorrents, &QAbstractButton::toggled, this, &ThisType::enableApplyButton);
@ -556,7 +558,8 @@ void OptionsDialog::saveOptions()
pref->setSplashScreenDisabled(isSplashScreenDisabled());
pref->setConfirmOnExit(m_ui->checkProgramExitConfirm->isChecked());
pref->setDontConfirmAutoExit(!m_ui->checkProgramAutoExitConfirm->isChecked());
pref->setPreventFromSuspend(preventFromSuspend());
pref->setPreventFromSuspendWhenDownloading(m_ui->checkPreventFromSuspendWhenDownloading->isChecked());
pref->setPreventFromSuspendWhenSeeding(m_ui->checkPreventFromSuspendWhenSeeding->isChecked());
#ifdef Q_OS_WIN
pref->setWinStartup(WinStartup());
// Windows: file association settings
@ -793,7 +796,8 @@ void OptionsDialog::loadOptions()
}
#endif
m_ui->checkPreventFromSuspend->setChecked(pref->preventFromSuspend());
m_ui->checkPreventFromSuspendWhenDownloading->setChecked(pref->preventFromSuspendWhenDownloading());
m_ui->checkPreventFromSuspendWhenSeeding->setChecked(pref->preventFromSuspendWhenSeeding());
#ifdef Q_OS_WIN
m_ui->checkStartup->setChecked(pref->WinStartup());
@ -1407,11 +1411,6 @@ bool OptionsDialog::WinStartup() const
}
#endif
bool OptionsDialog::preventFromSuspend() const
{
return m_ui->checkPreventFromSuspend->isChecked();
}
bool OptionsDialog::preAllocateAllFiles() const
{
return m_ui->checkPreallocateAll->isChecked();

View File

@ -122,7 +122,6 @@ private:
#endif
bool startMinimized() const;
bool isSplashScreenDisabled() const;
bool preventFromSuspend() const;
#ifdef Q_OS_WIN
bool WinStartup() const;
#endif

View File

@ -479,9 +479,16 @@
</property>
<layout class="QVBoxLayout" name="verticalLayout_16">
<item>
<widget class="QCheckBox" name="checkPreventFromSuspend">
<widget class="QCheckBox" name="checkPreventFromSuspendWhenDownloading">
<property name="text">
<string>Inhibit system sleep when torrents are active</string>
<string>Inhibit system sleep when torrents are downloading</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkPreventFromSuspendWhenSeeding">
<property name="text">
<string>Inhibit system sleep when torrents are seeding</string>
</property>
</widget>
</item>
@ -3369,7 +3376,8 @@ Use ';' to split multiple entries. Can use wildcard '*'.</string>
<tabstop>comboTrayIcon</tabstop>
<tabstop>checkAssociateTorrents</tabstop>
<tabstop>checkAssociateMagnetLinks</tabstop>
<tabstop>checkPreventFromSuspend</tabstop>
<tabstop>checkPreventFromSuspendWhenDownloading</tabstop>
<tabstop>checkPreventFromSuspendWhenSeeding</tabstop>
<tabstop>checkAdditionDialog</tabstop>
<tabstop>checkAdditionDialogFront</tabstop>
<tabstop>checkPreallocateAll</tabstop>