Enable customizing the save statistics time interval
[qBittorrent.git] / src / gui / shutdownconfirmdialog.cpp
blob0a787ae1ddb6141c6d44485748d32444dcf957d1
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>
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.
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.
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.
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.
30 #include "shutdownconfirmdialog.h"
32 #include <chrono>
34 #include <QDialogButtonBox>
35 #include <QIcon>
36 #include <QPushButton>
37 #include <QStyle>
39 #include "base/preferences.h"
40 #include "ui_shutdownconfirmdialog.h"
41 #include "utils.h"
43 using namespace std::chrono_literals;
45 ShutdownConfirmDialog::ShutdownConfirmDialog(QWidget *parent, const ShutdownDialogAction &action)
46 : QDialog(parent)
47 , m_ui(new Ui::ShutdownConfirmDialog)
48 , m_action(action)
50 m_ui->setupUi(this);
52 initText();
53 QIcon warningIcon(style()->standardIcon(QStyle::SP_MessageBoxWarning));
54 m_ui->warningLabel->setPixmap(warningIcon.pixmap(32));
56 if (m_action == ShutdownDialogAction::Exit)
57 m_ui->neverShowAgainCheckbox->setVisible(true);
58 else
59 m_ui->neverShowAgainCheckbox->setVisible(false);
61 // Cancel Button
62 QPushButton *cancelButton = m_ui->buttonBox->button(QDialogButtonBox::Cancel);
63 cancelButton->setFocus();
64 cancelButton->setDefault(true);
66 connect(m_ui->buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
67 connect(m_ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
69 // Always on top
70 setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
71 move(Utils::Gui::screenCenter(this));
73 m_timer.setInterval(1s);
74 connect(&m_timer, &QTimer::timeout, this, &ShutdownConfirmDialog::updateSeconds);
77 ShutdownConfirmDialog::~ShutdownConfirmDialog()
79 delete m_ui;
82 void ShutdownConfirmDialog::showEvent(QShowEvent *event)
84 QDialog::showEvent(event);
85 m_timer.start();
88 bool ShutdownConfirmDialog::askForConfirmation(QWidget *parent, const ShutdownDialogAction &action)
90 ShutdownConfirmDialog dlg(parent, action);
91 return (dlg.exec() == QDialog::Accepted);
94 void ShutdownConfirmDialog::updateSeconds()
96 --m_timeout;
97 updateText();
98 if (m_timeout == 0)
100 m_timer.stop();
101 accept();
105 void ShutdownConfirmDialog::accept()
107 Preferences::instance()->setDontConfirmAutoExit(m_ui->neverShowAgainCheckbox->isChecked());
108 QDialog::accept();
111 void ShutdownConfirmDialog::initText()
113 QPushButton *okButton = m_ui->buttonBox->button(QDialogButtonBox::Ok);
115 switch (m_action)
117 case ShutdownDialogAction::Exit:
118 m_msg = tr("qBittorrent will now exit.");
119 okButton->setText(tr("E&xit Now"));
120 setWindowTitle(tr("Exit confirmation"));
121 break;
122 case ShutdownDialogAction::Shutdown:
123 m_msg = tr("The computer is going to shutdown.");
124 okButton->setText(tr("&Shutdown Now"));
125 setWindowTitle(tr("Shutdown confirmation"));
126 break;
127 case ShutdownDialogAction::Suspend:
128 m_msg = tr("The computer is going to enter suspend mode.");
129 okButton->setText(tr("&Suspend Now"));
130 setWindowTitle(tr("Suspend confirmation"));
131 break;
132 case ShutdownDialogAction::Hibernate:
133 m_msg = tr("The computer is going to enter hibernation mode.");
134 okButton->setText(tr("&Hibernate Now"));
135 setWindowTitle(tr("Hibernate confirmation"));
136 break;
139 m_msg += u'\n';
140 updateText();
143 void ShutdownConfirmDialog::updateText()
145 QString t = tr("You can cancel the action within %1 seconds.").arg(QString::number(m_timeout)) + u'\n';
146 m_ui->shutdownText->setText(m_msg + t);