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
autoShutdownGroup->addAction(actionAutoExit_qBittorrent); autoShutdownGroup->addAction(actionAutoExit_qBittorrent);
autoShutdownGroup->addAction(actionAutoShutdown_system); autoShutdownGroup->addAction(actionAutoShutdown_system);
autoShutdownGroup->addAction(actionAutoSuspend_system); autoShutdownGroup->addAction(actionAutoSuspend_system);
autoShutdownGroup->addAction(actionAutoHibernate_system);
#if (!defined(Q_OS_UNIX) || defined(Q_OS_MAC)) || defined(QT_DBUS_LIB) #if (!defined(Q_OS_UNIX) || defined(Q_OS_MAC)) || defined(QT_DBUS_LIB)
actionAutoShutdown_system->setChecked(pref.shutdownWhenDownloadsComplete()); actionAutoShutdown_system->setChecked(pref.shutdownWhenDownloadsComplete());
actionAutoSuspend_system->setChecked(pref.suspendWhenDownloadsComplete()); actionAutoSuspend_system->setChecked(pref.suspendWhenDownloadsComplete());
actionAutoHibernate_system->setChecked(pref.hibernateWhenDownloadsComplete());
#else #else
actionAutoShutdown_system->setDisabled(true); actionAutoShutdown_system->setDisabled(true);
actionAutoSuspend_system->setDisabled(true); actionAutoSuspend_system->setDisabled(true);
actionAutoHibernate_system->setDisabled(true);
#endif #endif
actionAutoExit_qBittorrent->setChecked(pref.shutdownqBTWhenDownloadsComplete()); actionAutoExit_qBittorrent->setChecked(pref.shutdownqBTWhenDownloadsComplete());
@ -1414,6 +1417,12 @@ void MainWindow::on_actionAutoSuspend_system_toggled(bool enabled)
Preferences().setSuspendWhenDownloadsComplete(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) void MainWindow::on_actionAutoShutdown_system_toggled(bool enabled)
{ {
qDebug() << Q_FUNC_INFO << enabled; qDebug() << Q_FUNC_INFO << enabled;

1
src/mainwindow.h

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

9
src/mainwindow.ui

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

25
src/misc.cpp

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

8
src/preferences/preferences.h

@ -974,6 +974,14 @@ public:
void setSuspendWhenDownloadsComplete(bool suspend) { void setSuspendWhenDownloadsComplete(bool suspend) {
setValue(QString::fromUtf8("Preferences/Downloads/AutoSuspendOnCompletion"), 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 { bool shutdownqBTWhenDownloadsComplete() const {
return value(QString::fromUtf8("Preferences/Downloads/AutoShutDownqBTOnCompletion"), false).toBool(); return value(QString::fromUtf8("Preferences/Downloads/AutoShutDownqBTOnCompletion"), false).toBool();

15
src/qtlibtorrent/qbtsession.cpp

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

2
src/qtlibtorrent/qbtsession.h

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

Loading…
Cancel
Save