Add option to hibernate computer in Auto-Shutdown menu

This commit is contained in:
Bruno Barbieri 2014-07-06 06:13:36 -03:00
parent 273725d9dc
commit 00e09435b2
8 changed files with 59 additions and 13 deletions

View file

@ -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;

View file

@ -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();

View file

@ -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 @@
<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>

View file

@ -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);

View file

@ -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);

View file

@ -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();

View file

@ -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;
}

View file

@ -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();