Browse Source

Merge pull request #1447 from BrunoReX/hibernation

Add option to hibernate computer in Auto-Shutdown menu
adaptive-webui-19844
sledgehammer999 11 years ago
parent
commit
d6d20074be
  1. 9
      src/mainwindow.cpp
  2. 1
      src/mainwindow.h
  3. 9
      src/mainwindow.ui
  4. 25
      src/misc.cpp
  5. 3
      src/misc.h
  6. 8
      src/preferences/preferences.h
  7. 15
      src/qtlibtorrent/qbtsession.cpp
  8. 2
      src/qtlibtorrent/qbtsession.h

9
src/mainwindow.cpp

@ -261,12 +261,15 @@ MainWindow::MainWindow(QWidget *parent, const QStringList& torrentCmdLine) : QMa @@ -261,12 +261,15 @@ MainWindow::MainWindow(QWidget *parent, const QStringList& torrentCmdLine) : QMa
autoShutdownGroup->addAction(actionAutoExit_qBittorrent);
autoShutdownGroup->addAction(actionAutoShutdown_system);
autoShutdownGroup->addAction(actionAutoSuspend_system);
autoShutdownGroup->addAction(actionAutoHibernate_system);
#if (!defined(Q_OS_UNIX) || defined(Q_OS_MAC)) || defined(QT_DBUS_LIB)
actionAutoShutdown_system->setChecked(pref.shutdownWhenDownloadsComplete());
actionAutoSuspend_system->setChecked(pref.suspendWhenDownloadsComplete());
actionAutoHibernate_system->setChecked(pref.hibernateWhenDownloadsComplete());
#else
actionAutoShutdown_system->setDisabled(true);
actionAutoSuspend_system->setDisabled(true);
actionAutoHibernate_system->setDisabled(true);
#endif
actionAutoExit_qBittorrent->setChecked(pref.shutdownqBTWhenDownloadsComplete());
@ -1414,6 +1417,12 @@ void MainWindow::on_actionAutoSuspend_system_toggled(bool enabled) @@ -1414,6 +1417,12 @@ void MainWindow::on_actionAutoSuspend_system_toggled(bool enabled)
Preferences().setSuspendWhenDownloadsComplete(enabled);
}
void MainWindow::on_actionAutoHibernate_system_toggled(bool enabled)
{
qDebug() << Q_FUNC_INFO << enabled;
Preferences().setHibernateWhenDownloadsComplete(enabled);
}
void MainWindow::on_actionAutoShutdown_system_toggled(bool enabled)
{
qDebug() << Q_FUNC_INFO << enabled;

1
src/mainwindow.h

@ -211,6 +211,7 @@ private slots: @@ -211,6 +211,7 @@ private slots:
void on_actionExecution_Logs_triggered(bool checked);
void on_actionAutoExit_qBittorrent_toggled(bool );
void on_actionAutoSuspend_system_toggled(bool );
void on_actionAutoHibernate_system_toggled(bool );
void on_actionAutoShutdown_system_toggled(bool );
// Check for active torrents and set preventing from suspend state
void checkForActiveTorrents();

9
src/mainwindow.ui

@ -67,6 +67,7 @@ @@ -67,6 +67,7 @@
<addaction name="actionAutoShutdown_Disabled"/>
<addaction name="actionAutoExit_qBittorrent"/>
<addaction name="actionAutoSuspend_system"/>
<addaction name="actionAutoHibernate_system"/>
<addaction name="actionAutoShutdown_system"/>
</widget>
<addaction name="actionCreate_torrent"/>
@ -352,6 +353,14 @@ @@ -352,6 +353,14 @@
<string>Suspend system</string>
</property>
</action>
<action name="actionAutoHibernate_system">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Hibernate system</string>
</property>
</action>
<action name="actionAutoShutdown_system">
<property name="checkable">
<bool>true</bool>

25
src/misc.cpp

@ -81,29 +81,38 @@ static struct { const char *source; const char *comment; } units[] = { @@ -81,29 +81,38 @@ static struct { const char *source; const char *comment; } units[] = {
};
#ifndef DISABLE_GUI
void misc::shutdownComputer(bool sleep) {
void misc::shutdownComputer(shutDownAction action) {
#if (defined(Q_OS_UNIX) && !defined(Q_OS_MAC)) && defined(QT_DBUS_LIB)
// Use dbus to power off / suspend the system
if (sleep) {
if (action != SHUTDOWN_COMPUTER) {
// Some recent systems use systemd's logind
QDBusInterface login1Iface("org.freedesktop.login1", "/org/freedesktop/login1",
"org.freedesktop.login1.Manager", QDBusConnection::systemBus());
if (login1Iface.isValid()) {
login1Iface.call("Suspend", false);
if (action == SUSPEND_COMPUTER)
login1Iface.call("Suspend", false);
else
login1Iface.call("Hibernate", false);
return;
}
// Else, other recent systems use UPower
QDBusInterface upowerIface("org.freedesktop.UPower", "/org/freedesktop/UPower",
"org.freedesktop.UPower", QDBusConnection::systemBus());
if (upowerIface.isValid()) {
upowerIface.call("Suspend");
if (action == SUSPEND_COMPUTER)
upowerIface.call("Suspend");
else
upowerIface.call("Hibernate");
return;
}
// HAL (older systems)
QDBusInterface halIface("org.freedesktop.Hal", "/org/freedesktop/Hal/devices/computer",
"org.freedesktop.Hal.Device.SystemPowerManagement",
QDBusConnection::systemBus());
halIface.call("Suspend", 5);
if (action == SUSPEND_COMPUTER)
halIface.call("Suspend", 5);
else
halIface.call("Hibernate");
} else {
// Some recent systems use systemd's logind
QDBusInterface login1Iface("org.freedesktop.login1", "/org/freedesktop/login1",
@ -128,7 +137,7 @@ void misc::shutdownComputer(bool sleep) { @@ -128,7 +137,7 @@ void misc::shutdownComputer(bool sleep) {
#endif
#ifdef Q_OS_MAC
AEEventID EventToSend;
if (sleep)
if (action != SHUTDOWN_COMPUTER)
EventToSend = kAESleep;
else
EventToSend = kAEShutDown;
@ -189,8 +198,10 @@ void misc::shutdownComputer(bool sleep) { @@ -189,8 +198,10 @@ void misc::shutdownComputer(bool sleep) {
if (GetLastError() != ERROR_SUCCESS)
return;
if (sleep)
if (action == SUSPEND_COMPUTER)
SetSuspendState(false, false, false);
else if (action == HIBERNATE_COMPUTER)
SetSuspendState(true, false, false);
else
InitiateSystemShutdownA(0, QCoreApplication::translate("misc", "qBittorrent will shutdown the computer now because all downloads are complete.").toLocal8Bit().data(), 10, true, false);

3
src/misc.h

@ -48,6 +48,7 @@ @@ -48,6 +48,7 @@
#endif
const qlonglong MAX_ETA = 8640000;
enum shutDownAction { NO_SHUTDOWN, SHUTDOWN_COMPUTER, SUSPEND_COMPUTER, HIBERNATE_COMPUTER };
/* Miscellaneaous functions that can be useful */
namespace misc
@ -75,7 +76,7 @@ namespace misc @@ -75,7 +76,7 @@ namespace misc
}
#ifndef DISABLE_GUI
void shutdownComputer(bool sleep=false);
void shutdownComputer(shutDownAction action=SHUTDOWN_COMPUTER);
#endif
QString parseHtmlLinks(const QString &raw_text);

8
src/preferences/preferences.h

@ -975,6 +975,14 @@ public: @@ -975,6 +975,14 @@ public:
setValue(QString::fromUtf8("Preferences/Downloads/AutoSuspendOnCompletion"), suspend);
}
bool hibernateWhenDownloadsComplete() const {
return value(QString::fromUtf8("Preferences/Downloads/AutoHibernateOnCompletion"), false).toBool();
}
void setHibernateWhenDownloadsComplete(bool hibernate) {
setValue(QString::fromUtf8("Preferences/Downloads/AutoHibernateOnCompletion"), hibernate);
}
bool shutdownqBTWhenDownloadsComplete() const {
return value(QString::fromUtf8("Preferences/Downloads/AutoShutDownqBTOnCompletion"), false).toBool();
}

15
src/qtlibtorrent/qbtsession.cpp

@ -198,8 +198,8 @@ QBtSession::~QBtSession() { @@ -198,8 +198,8 @@ QBtSession::~QBtSession() {
qDebug("BTSession destructor OUT");
#ifndef DISABLE_GUI
if (m_shutdownAct != NO_SHUTDOWN) {
qDebug() << "Sending computer shutdown/suspend signal...";
misc::shutdownComputer(m_shutdownAct == SUSPEND_COMPUTER);
qDebug() << "Sending computer shutdown/suspend/hibernate signal...";
misc::shutdownComputer(m_shutdownAct);
}
#endif
}
@ -2286,7 +2286,8 @@ void QBtSession::handleTorrentFinishedAlert(libtorrent::torrent_finished_alert* @@ -2286,7 +2286,8 @@ void QBtSession::handleTorrentFinishedAlert(libtorrent::torrent_finished_alert*
#ifndef DISABLE_GUI
bool will_shutdown = (pref.shutdownWhenDownloadsComplete() ||
pref.shutdownqBTWhenDownloadsComplete() ||
pref.suspendWhenDownloadsComplete())
pref.suspendWhenDownloadsComplete() ||
pref.hibernateWhenDownloadsComplete())
&& !hasDownloadingTorrents();
#else
bool will_shutdown = false;
@ -2304,11 +2305,14 @@ void QBtSession::handleTorrentFinishedAlert(libtorrent::torrent_finished_alert* @@ -2304,11 +2305,14 @@ void QBtSession::handleTorrentFinishedAlert(libtorrent::torrent_finished_alert*
// Auto-Shutdown
if (will_shutdown) {
bool suspend = pref.suspendWhenDownloadsComplete();
bool hibernate = pref.hibernateWhenDownloadsComplete();
bool shutdown = pref.shutdownWhenDownloadsComplete();
// Confirm shutdown
QString confirm_msg;
if (suspend) {
confirm_msg = tr("The computer will now go to sleep mode unless you cancel within the next 15 seconds...");
} else if (hibernate) {
confirm_msg = tr("The computer will now go to hibernation mode unless you cancel within the next 15 seconds...");
} else if (shutdown) {
confirm_msg = tr("The computer will now be switched off unless you cancel within the next 15 seconds...");
} else {
@ -2317,14 +2321,17 @@ void QBtSession::handleTorrentFinishedAlert(libtorrent::torrent_finished_alert* @@ -2317,14 +2321,17 @@ void QBtSession::handleTorrentFinishedAlert(libtorrent::torrent_finished_alert*
if (!ShutdownConfirmDlg::askForConfirmation(confirm_msg))
return;
// Actually shut down
if (suspend || shutdown) {
if (suspend || hibernate || shutdown) {
qDebug("Preparing for auto-shutdown because all downloads are complete!");
// Disabling it for next time
pref.setShutdownWhenDownloadsComplete(false);
pref.setSuspendWhenDownloadsComplete(false);
pref.setHibernateWhenDownloadsComplete(false);
// Make sure preferences are synced before exiting
if (suspend)
m_shutdownAct = SUSPEND_COMPUTER;
else if (hibernate)
m_shutdownAct = HIBERNATE_COMPUTER;
else
m_shutdownAct = SHUTDOWN_COMPUTER;
}

2
src/qtlibtorrent/qbtsession.h

@ -53,6 +53,7 @@ @@ -53,6 +53,7 @@
#include "qtorrenthandle.h"
#include "trackerinfos.h"
#include "alertdispatcher.h"
#include "misc.h"
#define MAX_SAMPLES 20
@ -82,7 +83,6 @@ public: @@ -82,7 +83,6 @@ public:
private:
explicit QBtSession();
static QBtSession* m_instance;
enum shutDownAction { NO_SHUTDOWN, SHUTDOWN_COMPUTER, SUSPEND_COMPUTER };
public:
static QBtSession* instance();

Loading…
Cancel
Save