1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2014  sledgehammer999 <hammered999@gmail.com>
4  * Copyright (C) 2011  Christophe Dumez <chris@qbittorrent.org>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19  *
20  * In addition, as a special exception, the copyright holders give permission to
21  * link this program with the OpenSSL project's "OpenSSL" library (or with
22  * modified versions of it that use the same license as the "OpenSSL" library),
23  * and distribute the linked executables. You must obey the GNU General Public
24  * License in all respects for all of the code used other than "OpenSSL".  If you
25  * modify file(s), you may extend this exception to your version of the file(s),
26  * but you are not obligated to do so. If you do not wish to do so, delete this
27  * exception statement from your version.
28  */
29 
30 #include "shutdownconfirmdialog.h"
31 
32 #include <QDialogButtonBox>
33 #include <QIcon>
34 #include <QPushButton>
35 #include <QStyle>
36 
37 #include "base/preferences.h"
38 #include "ui_shutdownconfirmdialog.h"
39 #include "utils.h"
40 
ShutdownConfirmDialog(QWidget * parent,const ShutdownDialogAction & action)41 ShutdownConfirmDialog::ShutdownConfirmDialog(QWidget *parent, const ShutdownDialogAction &action)
42     : QDialog(parent)
43     , m_ui(new Ui::ShutdownConfirmDialog)
44     , m_timeout(15)
45     , m_action(action)
46 {
47     m_ui->setupUi(this);
48 
49     initText();
50     QIcon warningIcon(style()->standardIcon(QStyle::SP_MessageBoxWarning));
51     m_ui->warningLabel->setPixmap(warningIcon.pixmap(32));
52 
53     if (m_action == ShutdownDialogAction::Exit)
54         m_ui->neverShowAgainCheckbox->setVisible(true);
55     else
56         m_ui->neverShowAgainCheckbox->setVisible(false);
57 
58     // Cancel Button
59     QPushButton *cancelButton = m_ui->buttonBox->button(QDialogButtonBox::Cancel);
60     cancelButton->setFocus();
61     cancelButton->setDefault(true);
62 
63     // Always on top
64     setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
65     move(Utils::Gui::screenCenter(this));
66 
67     m_timer.setInterval(1000); // 1sec
68     connect(&m_timer, &QTimer::timeout, this, &ShutdownConfirmDialog::updateSeconds);
69 
70     Utils::Gui::resize(this);
71 }
72 
~ShutdownConfirmDialog()73 ShutdownConfirmDialog::~ShutdownConfirmDialog()
74 {
75     delete m_ui;
76 }
77 
showEvent(QShowEvent * event)78 void ShutdownConfirmDialog::showEvent(QShowEvent *event)
79 {
80     QDialog::showEvent(event);
81     m_timer.start();
82 }
83 
askForConfirmation(QWidget * parent,const ShutdownDialogAction & action)84 bool ShutdownConfirmDialog::askForConfirmation(QWidget *parent, const ShutdownDialogAction &action)
85 {
86     ShutdownConfirmDialog dlg(parent, action);
87     return (dlg.exec() == QDialog::Accepted);
88 }
89 
updateSeconds()90 void ShutdownConfirmDialog::updateSeconds()
91 {
92     --m_timeout;
93     updateText();
94     if (m_timeout == 0)
95     {
96         m_timer.stop();
97         accept();
98     }
99 }
100 
accept()101 void ShutdownConfirmDialog::accept()
102 {
103     Preferences::instance()->setDontConfirmAutoExit(m_ui->neverShowAgainCheckbox->isChecked());
104     QDialog::accept();
105 }
106 
initText()107 void ShutdownConfirmDialog::initText()
108 {
109     QPushButton *okButton = m_ui->buttonBox->button(QDialogButtonBox::Ok);
110 
111     switch (m_action)
112     {
113     case ShutdownDialogAction::Exit:
114         m_msg = tr("qBittorrent will now exit.");
115         okButton->setText(tr("E&xit Now"));
116         setWindowTitle(tr("Exit confirmation"));
117         break;
118     case ShutdownDialogAction::Shutdown:
119         m_msg = tr("The computer is going to shutdown.");
120         okButton->setText(tr("&Shutdown Now"));
121         setWindowTitle(tr("Shutdown confirmation"));
122         break;
123     case ShutdownDialogAction::Suspend:
124         m_msg = tr("The computer is going to enter suspend mode.");
125         okButton->setText(tr("&Suspend Now"));
126         setWindowTitle(tr("Suspend confirmation"));
127         break;
128     case ShutdownDialogAction::Hibernate:
129         m_msg = tr("The computer is going to enter hibernation mode.");
130         okButton->setText(tr("&Hibernate Now"));
131         setWindowTitle(tr("Hibernate confirmation"));
132         break;
133     }
134 
135     m_msg += '\n';
136     updateText();
137 }
138 
updateText()139 void ShutdownConfirmDialog::updateText()
140 {
141     QString t = tr("You can cancel the action within %1 seconds.").arg(QString::number(m_timeout)) + '\n';
142     m_ui->shutdownText->setText(m_msg + t);
143 }
144