From a1e0fa550938a4e53961cf8fec479ea973ad9ca1 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sat, 16 Apr 2016 13:01:29 +0800 Subject: [PATCH 1/3] Fix Coverity Scan 143909. Also, the setting "Confirmation on auto-exit when downloads finish" wasn't working before. --- src/app/application.cpp | 72 +++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 39 deletions(-) diff --git a/src/app/application.cpp b/src/app/application.cpp index 49b4dfbf0..9d9568a68 100644 --- a/src/app/application.cpp +++ b/src/app/application.cpp @@ -285,49 +285,43 @@ void Application::allTorrentsFinished() { #ifndef DISABLE_GUI Preferences *const pref = Preferences::instance(); + bool isExit = pref->shutdownqBTWhenDownloadsComplete(); + bool isShutdown = pref->shutdownWhenDownloadsComplete(); + bool isSuspend = pref->suspendWhenDownloadsComplete(); + bool isHibernate = pref->hibernateWhenDownloadsComplete(); - bool will_shutdown = (pref->shutdownWhenDownloadsComplete() - || pref->shutdownqBTWhenDownloadsComplete() - || pref->suspendWhenDownloadsComplete() - || pref->hibernateWhenDownloadsComplete()); + bool haveAction = isExit || isShutdown || isSuspend || isHibernate; + if (!haveAction) return; - // Auto-Shutdown - if (will_shutdown) { - bool suspend = pref->suspendWhenDownloadsComplete(); - bool hibernate = pref->hibernateWhenDownloadsComplete(); - bool shutdown = pref->shutdownWhenDownloadsComplete(); + ShutdownDialogAction action = ShutdownDialogAction::Exit; + if (isSuspend) + action = ShutdownDialogAction::Suspend; + else if (isHibernate) + action = ShutdownDialogAction::Hibernate; + else if (isShutdown) + action = ShutdownDialogAction::Shutdown; - // Confirm shutdown - ShutdownDialogAction action = ShutdownDialogAction::Exit; - if (suspend) - action = ShutdownDialogAction::Suspend; - else if (hibernate) - action = ShutdownDialogAction::Hibernate; - else if (shutdown) - action = ShutdownDialogAction::Shutdown; - - if ((action == ShutdownDialogAction::Exit) && (!pref->dontConfirmAutoExit())) { - if (!ShutdownConfirmDlg::askForConfirmation(action)) - return; - } - else { //exit and shutdown - if (!ShutdownConfirmDlg::askForConfirmation(action)) - return; - } - - // Actually shut down - 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 - m_shutdownAct = action; - } - qDebug("Exiting the application"); - exit(); + // ask confirm + if ((action == ShutdownDialogAction::Exit) && (pref->dontConfirmAutoExit())) { + // do nothing & skip confirm } + else { + if (!ShutdownConfirmDlg::askForConfirmation(action)) return; + } + + // Actually shut down + if (action != ShutdownDialogAction::Exit) { + 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 + m_shutdownAct = action; + } + + qDebug("Exiting the application"); + exit(); #endif // DISABLE_GUI } From fbc002f21239999eeb489d4a255541b615623ac9 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sat, 16 Apr 2016 13:50:41 +0800 Subject: [PATCH 2/3] Enable access to shutdown functions when configured with `--disable-gui` option --- src/app/application.cpp | 10 +++++----- src/app/application.h | 2 +- src/base/utils/misc.cpp | 2 -- src/base/utils/misc.h | 3 ++- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/app/application.cpp b/src/app/application.cpp index 9d9568a68..b2f4f5477 100644 --- a/src/app/application.cpp +++ b/src/app/application.cpp @@ -96,9 +96,7 @@ namespace Application::Application(const QString &id, int &argc, char **argv) : BaseApplication(id, argc, argv) , m_running(false) -#ifndef DISABLE_GUI , m_shutdownAct(ShutdownDialogAction::Exit) -#endif { Logger::initInstance(); SettingsStorage::initInstance(); @@ -283,7 +281,6 @@ void Application::torrentFinished(BitTorrent::TorrentHandle *const torrent) void Application::allTorrentsFinished() { -#ifndef DISABLE_GUI Preferences *const pref = Preferences::instance(); bool isExit = pref->shutdownqBTWhenDownloadsComplete(); bool isShutdown = pref->shutdownWhenDownloadsComplete(); @@ -301,6 +298,7 @@ void Application::allTorrentsFinished() else if (isShutdown) action = ShutdownDialogAction::Shutdown; +#ifndef DISABLE_GUI // ask confirm if ((action == ShutdownDialogAction::Exit) && (pref->dontConfirmAutoExit())) { // do nothing & skip confirm @@ -308,6 +306,7 @@ void Application::allTorrentsFinished() else { if (!ShutdownConfirmDlg::askForConfirmation(action)) return; } +#endif // DISABLE_GUI // Actually shut down if (action != ShutdownDialogAction::Exit) { @@ -322,7 +321,6 @@ void Application::allTorrentsFinished() qDebug("Exiting the application"); exit(); -#endif // DISABLE_GUI } bool Application::sendParams(const QStringList ¶ms) @@ -589,6 +587,7 @@ void Application::cleanup() delete m_fileLogger; Logger::freeInstance(); IconProvider::freeInstance(); + #ifndef DISABLE_GUI #ifdef Q_OS_WIN typedef BOOL (WINAPI *PSHUTDOWNBRDESTROY)(HWND); @@ -598,9 +597,10 @@ void Application::cleanup() shutdownBRDestroy((HWND)m_window->effectiveWinId()); #endif // Q_OS_WIN delete m_window; +#endif // DISABLE_GUI + if (m_shutdownAct != ShutdownDialogAction::Exit) { qDebug() << "Sending computer shutdown/suspend/hibernate signal..."; Utils::Misc::shutdownComputer(m_shutdownAct); } -#endif } diff --git a/src/app/application.h b/src/app/application.h index c399c2645..8c9c098ec 100644 --- a/src/app/application.h +++ b/src/app/application.h @@ -111,10 +111,10 @@ private slots: private: bool m_running; + ShutdownDialogAction m_shutdownAct; #ifndef DISABLE_GUI QPointer m_window; - ShutdownDialogAction m_shutdownAct; #endif #ifndef DISABLE_WEBUI diff --git a/src/base/utils/misc.cpp b/src/base/utils/misc.cpp index 37ac7c0b8..eb37aed40 100644 --- a/src/base/utils/misc.cpp +++ b/src/base/utils/misc.cpp @@ -91,7 +91,6 @@ static struct { const char *source; const char *comment; } units[] = { QT_TRANSLATE_NOOP3("misc", "EiB", "exbibytes (1024 pebibytes)") }; -#ifndef DISABLE_GUI void Utils::Misc::shutdownComputer(const ShutdownDialogAction &action) { #if (defined(Q_OS_UNIX) && !defined(Q_OS_MAC)) && defined(QT_DBUS_LIB) @@ -216,7 +215,6 @@ void Utils::Misc::shutdownComputer(const ShutdownDialogAction &action) (PTOKEN_PRIVILEGES) NULL, 0); #endif } -#endif // DISABLE_GUI #ifndef DISABLE_GUI // Get screen center diff --git a/src/base/utils/misc.h b/src/base/utils/misc.h index f6e566ae0..947c944f7 100644 --- a/src/base/utils/misc.h +++ b/src/base/utils/misc.h @@ -67,8 +67,9 @@ namespace Utils QString parseHtmlLinks(const QString &raw_text); bool isUrl(const QString &s); -#ifndef DISABLE_GUI void shutdownComputer(const ShutdownDialogAction &action); + +#ifndef DISABLE_GUI // Get screen center QPoint screenCenter(QWidget *win); QSize smallIconSize(); From 34db2f5dd96669676b859d8dba8b67e577fd1085 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Mon, 18 Apr 2016 00:08:55 +0800 Subject: [PATCH 3/3] Fix qBittorrent doesn't exit immediately when "all donwloads are done -> exit" option enabled. This fix is provided by glassez. --- src/app/application.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/application.cpp b/src/app/application.cpp index b2f4f5477..b8a1a1975 100644 --- a/src/app/application.cpp +++ b/src/app/application.cpp @@ -363,7 +363,7 @@ int Application::exec(const QStringList ¶ms) BitTorrent::Session::initInstance(); connect(BitTorrent::Session::instance(), SIGNAL(torrentFinished(BitTorrent::TorrentHandle *const)), SLOT(torrentFinished(BitTorrent::TorrentHandle *const))); - connect(BitTorrent::Session::instance(), SIGNAL(allTorrentsFinished()), SLOT(allTorrentsFinished())); + connect(BitTorrent::Session::instance(), SIGNAL(allTorrentsFinished()), SLOT(allTorrentsFinished()), Qt::QueuedConnection); #ifndef DISABLE_COUNTRIES_RESOLUTION Net::GeoIPManager::initInstance();