Browse Source

Merge pull request #5111 from Chocobo1/refactor_shutdowndlg

Cleanup shutdowndlg
adaptive-webui-19844
sledgehammer999 9 years ago
parent
commit
2d0b9e6538
  1. 16
      src/app/application.cpp
  2. 2
      src/app/application.h
  3. 4
      src/base/types.h
  4. 16
      src/base/utils/misc.cpp
  5. 2
      src/base/utils/misc.h
  6. 5
      src/gui/CMakeLists.txt
  7. 6
      src/gui/gui.pri
  8. 64
      src/gui/shutdownconfirmdlg.cpp
  9. 22
      src/gui/shutdownconfirmdlg.h
  10. 4
      src/gui/shutdownconfirmdlg.ui

16
src/app/application.cpp

@ -48,7 +48,7 @@
#endif // Q_OS_MAC #endif // Q_OS_MAC
#include "mainwindow.h" #include "mainwindow.h"
#include "addnewtorrentdialog.h" #include "addnewtorrentdialog.h"
#include "shutdownconfirm.h" #include "shutdownconfirmdlg.h"
#else // DISABLE_GUI #else // DISABLE_GUI
#include <iostream> #include <iostream>
#endif // DISABLE_GUI #endif // DISABLE_GUI
@ -97,7 +97,7 @@ Application::Application(const QString &id, int &argc, char **argv)
: BaseApplication(id, argc, argv) : BaseApplication(id, argc, argv)
, m_running(false) , m_running(false)
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
, m_shutdownAct(ShutdownAction::None) , m_shutdownAct(ShutdownDialogAction::Exit)
#endif #endif
{ {
Logger::initInstance(); Logger::initInstance();
@ -298,15 +298,15 @@ void Application::allTorrentsFinished()
bool shutdown = pref->shutdownWhenDownloadsComplete(); bool shutdown = pref->shutdownWhenDownloadsComplete();
// Confirm shutdown // Confirm shutdown
ShutdownAction action = ShutdownAction::None; ShutdownDialogAction action = ShutdownDialogAction::Exit;
if (suspend) if (suspend)
action = ShutdownAction::Suspend; action = ShutdownDialogAction::Suspend;
else if (hibernate) else if (hibernate)
action = ShutdownAction::Hibernate; action = ShutdownDialogAction::Hibernate;
else if (shutdown) else if (shutdown)
action = ShutdownAction::Shutdown; action = ShutdownDialogAction::Shutdown;
if ((action == ShutdownAction::None) && (!pref->dontConfirmAutoExit())) { if ((action == ShutdownDialogAction::Exit) && (!pref->dontConfirmAutoExit())) {
if (!ShutdownConfirmDlg::askForConfirmation(action)) if (!ShutdownConfirmDlg::askForConfirmation(action))
return; return;
} }
@ -604,7 +604,7 @@ void Application::cleanup()
shutdownBRDestroy((HWND)m_window->effectiveWinId()); shutdownBRDestroy((HWND)m_window->effectiveWinId());
#endif // Q_OS_WIN #endif // Q_OS_WIN
delete m_window; delete m_window;
if (m_shutdownAct != ShutdownAction::None) { if (m_shutdownAct != ShutdownDialogAction::Exit) {
qDebug() << "Sending computer shutdown/suspend/hibernate signal..."; qDebug() << "Sending computer shutdown/suspend/hibernate signal...";
Utils::Misc::shutdownComputer(m_shutdownAct); Utils::Misc::shutdownComputer(m_shutdownAct);
} }

2
src/app/application.h

@ -114,7 +114,7 @@ private:
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
QPointer<MainWindow> m_window; QPointer<MainWindow> m_window;
ShutdownAction m_shutdownAct; ShutdownDialogAction m_shutdownAct;
#endif #endif
#ifndef DISABLE_WEBUI #ifndef DISABLE_WEBUI

4
src/base/types.h

@ -33,9 +33,9 @@
const qlonglong MAX_ETA = 8640000; const qlonglong MAX_ETA = 8640000;
enum class ShutdownAction enum class ShutdownDialogAction
{ {
None, Exit,
Shutdown, Shutdown,
Suspend, Suspend,
Hibernate Hibernate

16
src/base/utils/misc.cpp

@ -92,16 +92,16 @@ static struct { const char *source; const char *comment; } units[] = {
}; };
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
void Utils::Misc::shutdownComputer(ShutdownAction action) void Utils::Misc::shutdownComputer(const ShutdownDialogAction &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 (action != ShutdownAction::Shutdown) { if (action != ShutdownDialogAction::Shutdown) {
// 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()) {
if (action == ShutdownAction::Suspend) if (action == ShutdownDialogAction::Suspend)
login1Iface.call("Suspend", false); login1Iface.call("Suspend", false);
else else
login1Iface.call("Hibernate", false); login1Iface.call("Hibernate", false);
@ -111,7 +111,7 @@ void Utils::Misc::shutdownComputer(ShutdownAction action)
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()) {
if (action == ShutdownAction::Suspend) if (action == ShutdownDialogAction::Suspend)
upowerIface.call("Suspend"); upowerIface.call("Suspend");
else else
upowerIface.call("Hibernate"); upowerIface.call("Hibernate");
@ -121,7 +121,7 @@ void Utils::Misc::shutdownComputer(ShutdownAction action)
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());
if (action == ShutdownAction::Suspend) if (action == ShutdownDialogAction::Suspend)
halIface.call("Suspend", 5); halIface.call("Suspend", 5);
else else
halIface.call("Hibernate"); halIface.call("Hibernate");
@ -150,7 +150,7 @@ void Utils::Misc::shutdownComputer(ShutdownAction action)
#endif #endif
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
AEEventID EventToSend; AEEventID EventToSend;
if (action != ShutdownAction::Shutdown) if (action != ShutdownDialogAction::Shutdown)
EventToSend = kAESleep; EventToSend = kAESleep;
else else
EventToSend = kAEShutDown; EventToSend = kAEShutDown;
@ -203,9 +203,9 @@ void Utils::Misc::shutdownComputer(ShutdownAction action)
if (GetLastError() != ERROR_SUCCESS) if (GetLastError() != ERROR_SUCCESS)
return; return;
if (action == ShutdownAction::Suspend) if (action == ShutdownDialogAction::Suspend)
SetSuspendState(false, false, false); SetSuspendState(false, false, false);
else if (action == ShutdownAction::Hibernate) else if (action == ShutdownDialogAction::Hibernate)
SetSuspendState(true, false, false); 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);

2
src/base/utils/misc.h

@ -68,7 +68,7 @@ namespace Utils
bool isUrl(const QString &s); bool isUrl(const QString &s);
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
void shutdownComputer(ShutdownAction action); void shutdownComputer(const ShutdownDialogAction &action);
// Get screen center // Get screen center
QPoint screenCenter(QWidget *win); QPoint screenCenter(QWidget *win);
QSize smallIconSize(); QSize smallIconSize();

5
src/gui/CMakeLists.txt

@ -47,7 +47,7 @@ options_imp.h
previewlistdelegate.h previewlistdelegate.h
previewselect.h previewselect.h
scanfoldersdelegate.h scanfoldersdelegate.h
shutdownconfirm.h shutdownconfirmdlg.h
speedlimitdlg.h speedlimitdlg.h
statsdialog.h statsdialog.h
statusbar.h statusbar.h
@ -83,7 +83,7 @@ messageboxraised.cpp
options_imp.cpp options_imp.cpp
previewselect.cpp previewselect.cpp
scanfoldersdelegate.cpp scanfoldersdelegate.cpp
shutdownconfirm.cpp shutdownconfirmdlg.cpp
speedlimitdlg.cpp speedlimitdlg.cpp
statsdialog.cpp statsdialog.cpp
statusbar.cpp statusbar.cpp
@ -126,6 +126,7 @@ autoexpandabledialog.ui
statsdialog.ui statsdialog.ui
options.ui options.ui
torrentcreatordlg.ui torrentcreatordlg.ui
shutdownconfirmdlg.ui
) )
qbt_target_sources(about.qrc) qbt_target_sources(about.qrc)

6
src/gui/gui.pri

@ -39,7 +39,7 @@ HEADERS += \
$$PWD/messageboxraised.h \ $$PWD/messageboxraised.h \
$$PWD/options_imp.h \ $$PWD/options_imp.h \
$$PWD/advancedsettings.h \ $$PWD/advancedsettings.h \
$$PWD/shutdownconfirm.h \ $$PWD/shutdownconfirmdlg.h \
$$PWD/torrentmodel.h \ $$PWD/torrentmodel.h \
$$PWD/torrentcreatordlg.h \ $$PWD/torrentcreatordlg.h \
$$PWD/scanfoldersdelegate.h \ $$PWD/scanfoldersdelegate.h \
@ -80,7 +80,7 @@ SOURCES += \
$$PWD/advancedsettings.cpp \ $$PWD/advancedsettings.cpp \
$$PWD/trackerlogin.cpp \ $$PWD/trackerlogin.cpp \
$$PWD/options_imp.cpp \ $$PWD/options_imp.cpp \
$$PWD/shutdownconfirm.cpp \ $$PWD/shutdownconfirmdlg.cpp \
$$PWD/torrentmodel.cpp \ $$PWD/torrentmodel.cpp \
$$PWD/torrentcreatordlg.cpp \ $$PWD/torrentcreatordlg.cpp \
$$PWD/scanfoldersdelegate.cpp \ $$PWD/scanfoldersdelegate.cpp \
@ -107,7 +107,7 @@ FORMS += \
$$PWD/bandwidth_limit.ui \ $$PWD/bandwidth_limit.ui \
$$PWD/updownratiodlg.ui \ $$PWD/updownratiodlg.ui \
$$PWD/confirmdeletiondlg.ui \ $$PWD/confirmdeletiondlg.ui \
$$PWD/confirmshutdowndlg.ui \ $$PWD/shutdownconfirmdlg.ui \
$$PWD/torrentimportdlg.ui \ $$PWD/torrentimportdlg.ui \
$$PWD/executionlog.ui \ $$PWD/executionlog.ui \
$$PWD/addnewtorrentdialog.ui \ $$PWD/addnewtorrentdialog.ui \

64
src/gui/shutdownconfirm.cpp → src/gui/shutdownconfirmdlg.cpp

@ -30,34 +30,30 @@
* Contact : hammered999@gmail.com * Contact : hammered999@gmail.com
*/ */
#include <QVBoxLayout> #include "shutdownconfirmdlg.h"
#include <QHBoxLayout> #include "ui_shutdownconfirmdlg.h"
#include <QStyle> #include <QStyle>
#include <QIcon> #include <QIcon>
#include <QLabel>
#include <QDialogButtonBox> #include <QDialogButtonBox>
#include <QCheckBox>
#include <QPushButton> #include <QPushButton>
#include "base/preferences.h" #include "base/preferences.h"
#include "base/types.h" #include "base/utils/misc.h"
#include "shutdownconfirm.h"
#include "ui_confirmshutdowndlg.h"
ShutdownConfirmDlg::ShutdownConfirmDlg(const ShutdownAction &action) ShutdownConfirmDlg::ShutdownConfirmDlg(const ShutdownDialogAction &action)
: ui(new Ui::confirmShutdownDlg) : ui(new Ui::confirmShutdownDlg)
, m_timeout(15) , m_timeout(15)
, m_action(action) , m_action(action)
{ {
ui->setupUi(this); ui->setupUi(this);
initText();
QIcon warningIcon(style()->standardIcon(QStyle::SP_MessageBoxWarning)); QIcon warningIcon(style()->standardIcon(QStyle::SP_MessageBoxWarning));
ui->warningLabel->setPixmap(warningIcon.pixmap(32)); ui->warningLabel->setPixmap(warningIcon.pixmap(32));
updateText(); if (m_action == ShutdownDialogAction::Exit)
if (m_action == ShutdownAction::None)
ui->neverShowAgainCheckbox->setVisible(true); ui->neverShowAgainCheckbox->setVisible(true);
else else
ui->neverShowAgainCheckbox->setVisible(false); ui->neverShowAgainCheckbox->setVisible(false);
@ -66,12 +62,13 @@ ShutdownConfirmDlg::ShutdownConfirmDlg(const ShutdownAction &action)
QPushButton *cancelButton = ui->buttonBox->button(QDialogButtonBox::Cancel); QPushButton *cancelButton = ui->buttonBox->button(QDialogButtonBox::Cancel);
cancelButton->setFocus(); cancelButton->setFocus();
cancelButton->setDefault(true); cancelButton->setDefault(true);
// Always on top // Always on top
setWindowFlags(windowFlags()|Qt::WindowStaysOnTopHint); setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
move(Utils::Misc::screenCenter(this));
m_timer.setInterval(1000); // 1sec m_timer.setInterval(1000); // 1sec
connect(&m_timer, SIGNAL(timeout()), this, SLOT(updateSeconds())); connect(&m_timer, SIGNAL(timeout()), this, SLOT(updateSeconds()));
// Move to center
move(Utils::Misc::screenCenter(this));
} }
ShutdownConfirmDlg::~ShutdownConfirmDlg() ShutdownConfirmDlg::~ShutdownConfirmDlg()
@ -85,11 +82,10 @@ void ShutdownConfirmDlg::showEvent(QShowEvent *event)
m_timer.start(); m_timer.start();
} }
bool ShutdownConfirmDlg::askForConfirmation(const ShutdownAction &action) bool ShutdownConfirmDlg::askForConfirmation(const ShutdownDialogAction &action)
{ {
ShutdownConfirmDlg dlg(action); ShutdownConfirmDlg dlg(action);
dlg.exec(); return (dlg.exec() == QDialog::Accepted);
return dlg.shutdown();
} }
void ShutdownConfirmDlg::updateSeconds() void ShutdownConfirmDlg::updateSeconds()
@ -108,43 +104,43 @@ void ShutdownConfirmDlg::accept()
QDialog::accept(); QDialog::accept();
} }
bool ShutdownConfirmDlg::shutdown() const void ShutdownConfirmDlg::initText()
{
return (result() == QDialog::Accepted);
}
void ShutdownConfirmDlg::updateText()
{ {
QString text;
QPushButton *okButton = ui->buttonBox->button(QDialogButtonBox::Ok); QPushButton *okButton = ui->buttonBox->button(QDialogButtonBox::Ok);
switch (m_action) { switch (m_action) {
case ShutdownAction::None: case ShutdownDialogAction::Exit:
text = tr("qBittorrent will now exit."); m_msg = tr("qBittorrent will now exit.");
okButton->setText(tr("E&xit Now")); okButton->setText(tr("E&xit Now"));
setWindowTitle(tr("Exit confirmation")); setWindowTitle(tr("Exit confirmation"));
break; break;
case ShutdownAction::Shutdown: case ShutdownDialogAction::Shutdown:
text = tr("The computer is going to shutdown."); m_msg = tr("The computer is going to shutdown.");
okButton->setText(tr("&Shutdown Now")); okButton->setText(tr("&Shutdown Now"));
setWindowTitle(tr("Shutdown confirmation")); setWindowTitle(tr("Shutdown confirmation"));
break; break;
case ShutdownAction::Suspend: case ShutdownDialogAction::Suspend:
text = tr("The computer is going to enter suspend mode."); m_msg = tr("The computer is going to enter suspend mode.");
okButton->setText(tr("&Suspend Now")); okButton->setText(tr("&Suspend Now"));
setWindowTitle(tr("Suspend confirmation")); setWindowTitle(tr("Suspend confirmation"));
break; break;
case ShutdownAction::Hibernate: case ShutdownDialogAction::Hibernate:
text = tr("The computer is going to enter hibernation mode."); m_msg = tr("The computer is going to enter hibernation mode.");
okButton->setText(tr("&Hibernate Now")); okButton->setText(tr("&Hibernate Now"));
setWindowTitle(tr("Hibernate confirmation")); setWindowTitle(tr("Hibernate confirmation"));
break; break;
} }
text += "\n" + tr("You can cancel the action within %1 seconds.").arg(QString::number(m_timeout)) + "\n"; m_msg += "\n";
ui->shutdownText->setText(text); updateText();
}
void ShutdownConfirmDlg::updateText()
{
QString t = tr("You can cancel the action within %1 seconds.").arg(QString::number(m_timeout)) + "\n";
ui->shutdownText->setText(m_msg + t);
} }

22
src/gui/shutdownconfirm.h → src/gui/shutdownconfirmdlg.h

@ -28,34 +28,30 @@
* Contact : chris@qbittorrent.org * Contact : chris@qbittorrent.org
*/ */
#ifndef SHUTDOWNCONFIRM_H #ifndef SHUTDOWNCONFIRMDLG_H
#define SHUTDOWNCONFIRM_H #define SHUTDOWNCONFIRMDLG_H
#include <QDialog> #include <QDialog>
#include <QTimer> #include <QTimer>
#include "base/utils/misc.h" #include "base/types.h"
class QLabel;
class QCheckBox;
namespace Ui namespace Ui
{ {
class confirmShutdownDlg; class confirmShutdownDlg;
} }
class ShutdownConfirmDlg : public QDialog class ShutdownConfirmDlg: public QDialog
{ {
Q_OBJECT Q_OBJECT
public: public:
ShutdownConfirmDlg(const ShutdownAction &action); ShutdownConfirmDlg(const ShutdownDialogAction &action);
~ShutdownConfirmDlg(); ~ShutdownConfirmDlg();
bool shutdown() const;
static bool askForConfirmation(const ShutdownAction &action); static bool askForConfirmation(const ShutdownDialogAction &action);
protected: protected:
void showEvent(QShowEvent *event); void showEvent(QShowEvent *event) override;
private slots: private slots:
void updateSeconds(); void updateSeconds();
@ -63,13 +59,15 @@ private slots:
private: private:
// Methods // Methods
void initText();
void updateText(); void updateText();
// Vars // Vars
Ui::confirmShutdownDlg *ui; Ui::confirmShutdownDlg *ui;
QTimer m_timer; QTimer m_timer;
int m_timeout; int m_timeout;
ShutdownAction m_action; ShutdownDialogAction m_action;
QString m_msg;
}; };
#endif // SHUTDOWNCONFIRM_H #endif // SHUTDOWNCONFIRM_H

4
src/gui/confirmshutdowndlg.ui → src/gui/shutdownconfirmdlg.ui

@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>407</width> <width>410</width>
<height>103</height> <height>140</height>
</rect> </rect>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout_3"> <layout class="QVBoxLayout" name="verticalLayout_3">
Loading…
Cancel
Save