From df677789d2ee206ea377c3938a9e9d24260c2eee Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Mon, 16 Aug 2010 17:35:32 +0000 Subject: [PATCH] FEATURE: Added Auto-Shutdown on downloads completion feature (Linux Only for now) --- Changelog | 1 + src/GUI.cpp | 6 ++++++ src/GUI.h | 1 + src/bittorrent.cpp | 24 ++++++++++++++++++++++++ src/bittorrent.h | 1 + src/misc.cpp | 14 ++++++++++++++ src/misc.h | 2 ++ src/preferences.h | 11 +++++++++++ src/src.pro | 3 +++ src/ui/mainwindow.ui | 12 +++++++++++- 10 files changed, 74 insertions(+), 1 deletion(-) diff --git a/Changelog b/Changelog index 41e2279ff..81dac1388 100644 --- a/Changelog +++ b/Changelog @@ -1,5 +1,6 @@ * Unreleased - Christophe Dumez - v2.4.0 - FEATURE: Added actions to "Move to top/bottom" of priority queue + - FEATURE: Added Auto-Shutdown on downloads completion feature * Tue Jul 27 2010 - Christophe Dumez - v2.3.0 - FEATURE: Simplified torrent root folder renaming/truncating (< v2.3.0 is no longer forward compatible) diff --git a/src/GUI.cpp b/src/GUI.cpp index d4f631a67..8fa6bd9d2 100644 --- a/src/GUI.cpp +++ b/src/GUI.cpp @@ -187,6 +187,7 @@ GUI::GUI(QWidget *parent, QStringList torrentCmdLine) : QMainWindow(parent), for actionSearch_engine->setChecked(Preferences::isSearchEnabled()); displaySearchTab(actionSearch_engine->isChecked()); displayRSSTab(actionRSS_Reader->isChecked()); + actionShutdown_when_downloads_complete->setChecked(Preferences::shutdownWhenDownloadsComplete()); show(); @@ -1066,6 +1067,11 @@ void GUI::on_actionTop_tool_bar_triggered() { Preferences::setToolbarDisplayed(is_visible); } +void GUI::on_actionShutdown_when_downloads_complete_triggered() { + bool is_checked = static_cast(sender())->isChecked(); + Preferences::setShutdownWhenDownloadsComplete(is_checked); +} + void GUI::on_actionSpeed_in_title_bar_triggered() { displaySpeedInTitle = static_cast(sender())->isChecked(); Preferences::showSpeedInTitleBar(displaySpeedInTitle); diff --git a/src/GUI.h b/src/GUI.h index ee7878720..8130fed04 100644 --- a/src/GUI.h +++ b/src/GUI.h @@ -170,6 +170,7 @@ private slots: void on_actionRSS_Reader_triggered(); void on_actionSpeed_in_title_bar_triggered(); void on_actionTop_tool_bar_triggered(); + void on_actionShutdown_when_downloads_complete_triggered(); }; #endif diff --git a/src/bittorrent.cpp b/src/bittorrent.cpp index 101162f98..8571ad3a6 100644 --- a/src/bittorrent.cpp +++ b/src/bittorrent.cpp @@ -720,6 +720,17 @@ bool Bittorrent::hasActiveTorrents() const { return false; } +bool Bittorrent::hasDownloadingTorrents() const { + std::vector torrents = getTorrents(); + std::vector::iterator torrentIT; + for(torrentIT = torrents.begin(); torrentIT != torrents.end(); torrentIT++) { + const QTorrentHandle h(*torrentIT); + if(h.is_valid() && (h.state() == torrent_status::downloading || h.state() == torrent_status::downloading_metadata)) + return true; + } + return false; +} + void Bittorrent::banIP(QString ip) { FilterParserThread::processFilterList(s, QStringList(ip)); Preferences::banIP(ip); @@ -2013,6 +2024,19 @@ void Bittorrent::addConsoleMessage(QString msg, QString) { TorrentPersistentData::saveSeedStatus(h); } qDebug("Received finished alert for %s", qPrintable(h.name())); + if(!was_already_seeded) { + // Auto-Shutdown + if(Preferences::shutdownWhenDownloadsComplete() && !hasDownloadingTorrents()) { + qDebug("Preparing for auto-shutdown because all downloads are complete!"); +#if LIBTORRENT_VERSION_MINOR < 15 + saveDHTEntry(); +#endif + saveSessionState(); + saveFastResumeData(); + delete s; + misc::shutdownComputer(); + } + } } } else if (save_resume_data_alert* p = dynamic_cast(a.get())) { diff --git a/src/bittorrent.h b/src/bittorrent.h index 2f99ffeb9..41b6437fc 100644 --- a/src/bittorrent.h +++ b/src/bittorrent.h @@ -108,6 +108,7 @@ public: session* getSession() const; QHash getTrackersInfo(QString hash) const; bool hasActiveTorrents() const; + bool hasDownloadingTorrents() const; bool isQueueingEnabled() const; int getMaximumActiveDownloads() const; int getMaximumActiveTorrents() const; diff --git a/src/misc.cpp b/src/misc.cpp index 6e14bcc9e..11ea0b878 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -67,6 +67,11 @@ const int UNLEN = 256; #include #endif +#ifdef Q_WS_X11 +#include +#include +#endif + QString misc::QDesktopServicesDataLocation() { #ifdef Q_WS_WIN LPWSTR path=new WCHAR[256]; @@ -184,6 +189,15 @@ long long misc::freeDiskSpaceOnPath(QString path) { #endif } +void misc::shutdownComputer() { +#ifdef Q_WS_X11 + // Use dbus to power off the system + // dbus-send --print-reply --system --dest=org.freedesktop.Hal /org/freedesktop/Hal/devices/computer org.freedesktop.Hal.Device.SystemPowerManagement.Shutdown + QDBusInterface computer("org.freedesktop.Hal", "/org/freedesktop/Hal/devices/computer", "org.freedesktop.Hal.Device.SystemPowerManagement", QDBusConnection::systemBus()); + computer.call("Shutdown"); +#endif +} + QString misc::truncateRootFolder(boost::intrusive_ptr t) { if(t->num_files() == 1) { // Single file torrent diff --git a/src/misc.h b/src/misc.h index 276fb2d05..8b04beb13 100644 --- a/src/misc.h +++ b/src/misc.h @@ -91,6 +91,8 @@ public: return x; } + static void shutdownComputer(); + static bool safeRemove(QString file_path) { QFile MyFile(file_path); if(!MyFile.exists()) return true; diff --git a/src/preferences.h b/src/preferences.h index 9684abd9e..f2d73c839 100644 --- a/src/preferences.h +++ b/src/preferences.h @@ -851,6 +851,17 @@ public: } // Advanced settings + + static bool shutdownWhenDownloadsComplete() { + QIniSettings settings("qBittorrent", "qBittorrent"); + return settings.value(QString::fromUtf8("Preferences/Downloads/AutoShutDownOnCompletion"), false).toBool(); + } + + static void setShutdownWhenDownloadsComplete(bool shutdown) { + QIniSettings settings("qBittorrent", "qBittorrent"); + settings.setValue(QString::fromUtf8("Preferences/Downloads/AutoShutDownOnCompletion"), shutdown); + } + static uint diskCacheSize() { QIniSettings settings("qBittorrent", "qBittorrent"); return settings.value(QString::fromUtf8("Preferences/Downloads/DiskCache"), 16).toUInt(); diff --git a/src/src.pro b/src/src.pro index 55614cb77..342e4b4dc 100644 --- a/src/src.pro +++ b/src/src.pro @@ -160,6 +160,9 @@ unix { QT += network !contains(DEFINES, DISABLE_GUI):QT += xml +unix { + QT += dbus +} DEFINES += QT_NO_CAST_TO_ASCII diff --git a/src/ui/mainwindow.ui b/src/ui/mainwindow.ui index 339d0cc9a..fbcd75baf 100644 --- a/src/ui/mainwindow.ui +++ b/src/ui/mainwindow.ui @@ -29,7 +29,7 @@ 0 0 914 - 25 + 23 @@ -64,6 +64,8 @@ + + @@ -320,6 +322,14 @@ Search &engine + + + true + + + Shutdown when downloads complete + +