diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 204cd84ee..7361e9ebf 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -260,12 +260,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_WS_X11) || 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()); @@ -1421,6 +1424,11 @@ 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 abdc61486..7e05477a8 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..4cbbf0855 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 6f0eb38b9..1fa232cf0 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_WS_X11) && 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", @@ -127,7 +136,7 @@ void misc::shutdownComputer(bool sleep) { } #endif #ifdef Q_WS_MAC - AEEventID EventToSend; + if (action != SHUTDOWN_COMPUTER) if (sleep) EventToSend = kAESleep; else @@ -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 a9032a2f6..b689cb48d 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 d10a14343..665230d84 100755 --- a/src/preferences/preferences.h +++ b/src/preferences/preferences.h @@ -967,6 +967,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(); } diff --git a/src/qtlibtorrent/qbtsession.cpp b/src/qtlibtorrent/qbtsession.cpp index e525edea7..00e170031 100755 --- a/src/qtlibtorrent/qbtsession.cpp +++ b/src/qtlibtorrent/qbtsession.cpp @@ -201,8 +201,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 } @@ -2301,7 +2301,8 @@ void QBtSession::readAlerts() { #ifndef DISABLE_GUI bool will_shutdown = (pref.shutdownWhenDownloadsComplete() || pref.shutdownqBTWhenDownloadsComplete() || - pref.suspendWhenDownloadsComplete()) + pref.suspendWhenDownloadsComplete() || + pref.hibernateWhenDownloadsComplete()) && !hasDownloadingTorrents(); #else bool will_shutdown = false; @@ -2319,11 +2320,14 @@ void QBtSession::readAlerts() { // 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 { @@ -2332,14 +2336,17 @@ void QBtSession::readAlerts() { 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 95573f913..b94272b0b 100755 --- a/src/qtlibtorrent/qbtsession.h +++ b/src/qtlibtorrent/qbtsession.h @@ -50,6 +50,7 @@ #include "qtracker.h" #include "qtorrenthandle.h" #include "trackerinfos.h" +#include "misc.h" #define MAX_SAMPLES 20 @@ -78,7 +79,6 @@ public: private: explicit QBtSession(); static QBtSession* m_instance; - enum shutDownAction { NO_SHUTDOWN, SHUTDOWN_COMPUTER, SUSPEND_COMPUTER }; public: static QBtSession* instance();