From 00e09435b2a933bf9bf8b7e206e9e9103f2b30de Mon Sep 17 00:00:00 2001 From: Bruno Barbieri Date: Sun, 6 Jul 2014 06:13:36 -0300 Subject: [PATCH] Add option to hibernate computer in Auto-Shutdown menu --- src/mainwindow.cpp | 9 +++++++++ src/mainwindow.h | 1 + src/mainwindow.ui | 9 +++++++++ src/misc.cpp | 25 ++++++++++++++++++------- src/misc.h | 3 ++- src/preferences/preferences.h | 8 ++++++++ src/qtlibtorrent/qbtsession.cpp | 15 +++++++++++---- src/qtlibtorrent/qbtsession.h | 2 +- 8 files changed, 59 insertions(+), 13 deletions(-) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 320ae3546..531ca0b07 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -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) 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; diff --git a/src/mainwindow.h b/src/mainwindow.h index 4c38e33a2..5b8f35033 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -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(); diff --git a/src/mainwindow.ui b/src/mainwindow.ui index 92fa14821..739efe265 100644 --- a/src/mainwindow.ui +++ b/src/mainwindow.ui @@ -67,6 +67,7 @@ + @@ -352,6 +353,14 @@ Suspend system + + + true + + + Hibernate system + + true diff --git a/src/misc.cpp b/src/misc.cpp index 7addb0d86..89208b71d 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -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) { #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) { 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); diff --git a/src/misc.h b/src/misc.h index e25a7929b..00dae66af 100644 --- a/src/misc.h +++ b/src/misc.h @@ -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 } #ifndef DISABLE_GUI - void shutdownComputer(bool sleep=false); + void shutdownComputer(shutDownAction action=SHUTDOWN_COMPUTER); #endif QString parseHtmlLinks(const QString &raw_text); diff --git a/src/preferences/preferences.h b/src/preferences/preferences.h index 4bb10456d..889696119 100755 --- a/src/preferences/preferences.h +++ b/src/preferences/preferences.h @@ -974,6 +974,14 @@ public: void setSuspendWhenDownloadsComplete(bool suspend) { 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(); diff --git a/src/qtlibtorrent/qbtsession.cpp b/src/qtlibtorrent/qbtsession.cpp index 97a3b0638..d1908e2e9 100755 --- a/src/qtlibtorrent/qbtsession.cpp +++ b/src/qtlibtorrent/qbtsession.cpp @@ -197,8 +197,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 } @@ -2282,7 +2282,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; @@ -2300,11 +2301,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 { @@ -2313,14 +2317,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; } diff --git a/src/qtlibtorrent/qbtsession.h b/src/qtlibtorrent/qbtsession.h index d2510275a..0defbe060 100755 --- a/src/qtlibtorrent/qbtsession.h +++ b/src/qtlibtorrent/qbtsession.h @@ -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: private: explicit QBtSession(); static QBtSession* m_instance; - enum shutDownAction { NO_SHUTDOWN, SHUTDOWN_COMPUTER, SUSPEND_COMPUTER }; public: static QBtSession* instance();