Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a timeout to the reboot/poweroff ... confirmation dialog #315

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 98 additions & 22 deletions lxqtpowermanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,67 @@
#include <QtDebug>
#include <QScreen>
#include <QFile>
#include <QTimer>
#include <QPushButton>
#include "lxqttranslator.h"
#include "lxqtglobals.h"
#include "lxqtsettings.h"
#include <XdgIcon>

#define DEFAULT_TIMEOUT 10

namespace LXQt {

class LXQT_API MessageBox: public QMessageBox
class LXQT_API MessageBoxWithTimeout: public QMessageBox
{
Q_DECLARE_TR_FUNCTIONS(LXQt::MessageBox)
Q_DECLARE_TR_FUNCTIONS(LXQt::MessageBoxWithTimeout)

public:
explicit MessageBox(QWidget *parent = nullptr): QMessageBox(parent) {}
int m_timeout = -1;
QTimer m_timer;
QString m_title;

explicit MessageBoxWithTimeout(QWidget *parent) : QMessageBox(parent) {}

void setTimeout(int timeout)
{
assert(defaultButton());

/* FIXME: warn if timeout is < -1 */
if (timeout < -1)
timeout = -1;

if (m_timer.isActive())
m_timer.stop();

m_timeout = timeout;
if (m_timeout >= 0)
{
m_timer.setInterval(1000);
connect(&m_timer, &QTimer::timeout, this, &MessageBoxWithTimeout::updateTimeout);
m_timer.start();
}
updateTitle();
}

void setWindowTitle(const QString &title)
{
m_title = title;
updateTitle();
}

void updateTitle()
{
if (m_timeout >= 0)
QMessageBox::setWindowTitle(QString::fromUtf8("%1 (%2)").arg(m_title).arg(- m_timeout));
else
QMessageBox::setWindowTitle(m_title);
}

virtual ~MessageBoxWithTimeout()
{
if (m_timer.isActive())
m_timer.stop();
}

static QWidget *parentWidget()
{
Expand All @@ -57,23 +105,45 @@ class LXQT_API MessageBox: public QMessageBox
return nullptr;
}

static bool question(const QString& title, const QString& text)
private Q_SLOTS:

void updateTimeout() {
if (m_timeout == 0)
{
defaultButton()->animateClick();
return;
}
if (m_timeout > 0)
m_timeout --;

updateTitle();
}

public:
static bool question(const QString& title, const QString& text, int timeout = -1)
{
MessageBox msgBox(parentWidget());
MessageBoxWithTimeout msgBox(parentWidget());
msgBox.setWindowTitle(title);
msgBox.setText(text);
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
msgBox.setDefaultButton(QMessageBox::Yes);
msgBox.setIcon(QMessageBox::Question);
if (timeout > -1)
msgBox.setTimeout(timeout);

return (msgBox.exec() == QMessageBox::Yes);
}


static void warning(const QString& title, const QString& text)
{
Q_UNUSED(title)
Q_UNUSED(text)
QMessageBox::warning(parentWidget(), tr("LXQt Power Manager Error"), tr("Hibernate failed."));
MessageBoxWithTimeout msgBox(parentWidget());
msgBox.setWindowTitle(title);
msgBox.setText(text);
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.setIcon(QMessageBox::Warning);

msgBox.exec();
}
};

Expand All @@ -90,6 +160,7 @@ PowerManager::PowerManager(QObject * parent, bool skipWarning)
QString sessionConfig(QFile::decodeName(qgetenv("LXQT_SESSION_CONFIG")));
Settings settings(sessionConfig.isEmpty() ? QL1S("session") : sessionConfig);
m_skipWarning = settings.value(QL1S("leave_confirmation")).toBool() ? false : true;
m_timeoutWarning = settings.value(QL1S("timeout_confirmation"), DEFAULT_TIMEOUT).toInt();
}

PowerManager::~PowerManager()
Expand Down Expand Up @@ -145,8 +216,9 @@ QList<QAction*> PowerManager::availableActions()
void PowerManager::suspend()
{
if (m_skipWarning ||
MessageBox::question(tr("LXQt Session Suspend"),
tr("Do you want to really suspend your computer?<p>Suspends the computer into a low power state. System state is not preserved if the power is lost.")))
MessageBoxWithTimeout::question(tr("LXQt Session Suspend"),
tr("Do you want to really suspend your computer?<p>Suspends the computer into a low power state. System state is not preserved if the power is lost."),
m_timeoutWarning))
{
m_power->suspend();
}
Expand All @@ -155,8 +227,9 @@ void PowerManager::suspend()
void PowerManager::hibernate()
{
if (m_skipWarning ||
MessageBox::question(tr("LXQt Session Hibernate"),
tr("Do you want to really hibernate your computer?<p>Hibernates the computer into a low power state. System state is preserved if the power is lost.")))
MessageBoxWithTimeout::question(tr("LXQt Session Hibernate"),
tr("Do you want to really hibernate your computer?<p>Hibernates the computer into a low power state. System state is preserved if the power is lost."),
m_timeoutWarning))
{
m_power->hibernate();
}
Expand All @@ -165,8 +238,9 @@ void PowerManager::hibernate()
void PowerManager::reboot()
{
if (m_skipWarning ||
MessageBox::question(tr("LXQt Session Reboot"),
tr("Do you want to really restart your computer? All unsaved work will be lost...")))
MessageBoxWithTimeout::question(tr("LXQt Session Reboot"),
tr("Do you want to really restart your computer? All unsaved work will be lost..."),
m_timeoutWarning))
{
m_power->reboot();
}
Expand All @@ -175,8 +249,9 @@ void PowerManager::reboot()
void PowerManager::shutdown()
{
if (m_skipWarning ||
MessageBox::question(tr("LXQt Session Shutdown"),
tr("Do you want to really switch off your computer? All unsaved work will be lost...")))
MessageBoxWithTimeout::question(tr("LXQt Session Shutdown"),
tr("Do you want to really switch off your computer? All unsaved work will be lost..."),
m_timeoutWarning))
{
m_power->shutdown();
}
Expand All @@ -185,21 +260,22 @@ void PowerManager::shutdown()
void PowerManager::logout()
{
if (m_skipWarning ||
MessageBox::question(tr("LXQt Session Logout"),
tr("Do you want to really logout? All unsaved work will be lost...")))
MessageBoxWithTimeout::question(tr("LXQt Session Logout"),
tr("Do you want to really logout? All unsaved work will be lost..."),
m_timeoutWarning))
{
m_power->logout();
}
}

void PowerManager::hibernateFailed()
{
MessageBox::warning(tr("LXQt Power Manager Error"), tr("Hibernate failed."));
MessageBoxWithTimeout::warning(tr("LXQt Power Manager Error"), tr("Hibernate failed."));
}

void PowerManager::suspendFailed()
{
MessageBox::warning(tr("LXQt Power Manager Error"), tr("Suspend failed."));
MessageBoxWithTimeout::warning(tr("LXQt Power Manager Error"), tr("Suspend failed."));
}

} // namespace LXQt
1 change: 1 addition & 0 deletions lxqtpowermanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public Q_SLOTS:
private:
LXQt::Power * m_power;
bool m_skipWarning;
int m_timeoutWarning;

private Q_SLOTS:
void hibernateFailed();
Expand Down