qBittorrent/src/gui/shutdownconfirmdlg.cpp

140 lines
4.5 KiB
C++
Raw Normal View History

/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2014 sledgehammer999 <hammered999@gmail.com>
* Copyright (C) 2011 Christophe Dumez <chris@qbittorrent.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
#include "shutdownconfirmdlg.h"
2016-04-11 11:00:09 +03:00
#include <QDialogButtonBox>
#include <QIcon>
#include <QPushButton>
#include <QStyle>
2016-03-21 00:54:07 +03:00
#include "base/preferences.h"
2016-04-11 11:00:09 +03:00
#include "base/utils/misc.h"
#include "ui_shutdownconfirmdlg.h"
2016-03-21 00:54:07 +03:00
2015-11-11 06:52:33 +03:00
ShutdownConfirmDlg::ShutdownConfirmDlg(QWidget *parent, const ShutdownDialogAction &action)
: QDialog(parent)
, m_ui(new Ui::confirmShutdownDlg)
, m_timeout(15)
, m_action(action)
{
m_ui->setupUi(this);
2016-03-21 00:54:07 +03:00
initText();
QIcon warningIcon(style()->standardIcon(QStyle::SP_MessageBoxWarning));
m_ui->warningLabel->setPixmap(warningIcon.pixmap(32));
2016-03-21 12:02:51 +03:00
if (m_action == ShutdownDialogAction::Exit)
m_ui->neverShowAgainCheckbox->setVisible(true);
2016-03-21 12:02:51 +03:00
else
m_ui->neverShowAgainCheckbox->setVisible(false);
2016-03-21 12:02:51 +03:00
// Cancel Button
QPushButton *cancelButton = m_ui->buttonBox->button(QDialogButtonBox::Cancel);
cancelButton->setFocus();
cancelButton->setDefault(true);
// Always on top
2016-04-11 11:00:09 +03:00
setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
move(Utils::Misc::screenCenter(this));
m_timer.setInterval(1000); // 1sec
2015-11-25 19:30:43 +03:00
connect(&m_timer, SIGNAL(timeout()), this, SLOT(updateSeconds()));
}
2016-04-04 00:10:18 +03:00
ShutdownConfirmDlg::~ShutdownConfirmDlg()
{
delete m_ui;
2016-04-04 00:10:18 +03:00
}
void ShutdownConfirmDlg::showEvent(QShowEvent *event)
{
QDialog::showEvent(event);
m_timer.start();
}
2015-11-11 06:52:33 +03:00
bool ShutdownConfirmDlg::askForConfirmation(QWidget *parent, const ShutdownDialogAction &action)
{
2015-11-11 06:52:33 +03:00
ShutdownConfirmDlg dlg(parent, action);
return (dlg.exec() == QDialog::Accepted);
}
void ShutdownConfirmDlg::updateSeconds()
{
--m_timeout;
updateText();
if (m_timeout == 0) {
m_timer.stop();
accept();
}
}
2016-03-21 00:54:07 +03:00
void ShutdownConfirmDlg::accept()
{
Preferences::instance()->setDontConfirmAutoExit(m_ui->neverShowAgainCheckbox->isChecked());
2016-03-21 00:54:07 +03:00
QDialog::accept();
}
void ShutdownConfirmDlg::initText()
{
QPushButton *okButton = m_ui->buttonBox->button(QDialogButtonBox::Ok);
switch (m_action) {
case ShutdownDialogAction::Exit:
m_msg = tr("qBittorrent will now exit.");
2016-03-21 12:02:51 +03:00
okButton->setText(tr("E&xit Now"));
setWindowTitle(tr("Exit confirmation"));
break;
case ShutdownDialogAction::Shutdown:
m_msg = tr("The computer is going to shutdown.");
2016-03-21 12:02:51 +03:00
okButton->setText(tr("&Shutdown Now"));
setWindowTitle(tr("Shutdown confirmation"));
break;
case ShutdownDialogAction::Suspend:
m_msg = tr("The computer is going to enter suspend mode.");
2016-03-21 12:02:51 +03:00
okButton->setText(tr("&Suspend Now"));
setWindowTitle(tr("Suspend confirmation"));
break;
case ShutdownDialogAction::Hibernate:
m_msg = tr("The computer is going to enter hibernation mode.");
2016-03-21 12:02:51 +03:00
okButton->setText(tr("&Hibernate Now"));
setWindowTitle(tr("Hibernate confirmation"));
break;
}
m_msg += "\n";
updateText();
}
void ShutdownConfirmDlg::updateText()
{
QString t = tr("You can cancel the action within %1 seconds.").arg(QString::number(m_timeout)) + "\n";
m_ui->shutdownText->setText(m_msg + t);
}