calamares/src/modules/partition/gui/EncryptWidget.cpp

177 lines
4.6 KiB
C++
Raw Normal View History

/* === This file is part of Calamares - <https://github.com/calamares> ===
2016-04-22 16:00:19 +02:00
*
* Copyright 2016, Teo Mrnjavac <teo@kde.org>
* Copyright 2020, Adriaan de Groot <groot@kde.org>
2016-04-22 16:00:19 +02:00
*
* Calamares 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 3 of the License, or
* (at your option) any later version.
*
* Calamares 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 Calamares. If not, see <http://www.gnu.org/licenses/>.
*/
#include "EncryptWidget.h"
#include "ui_EncryptWidget.h"
#include "utils/CalamaresUtilsGui.h"
#include "utils/Retranslator.h"
2016-04-22 16:00:19 +02:00
EncryptWidget::EncryptWidget( QWidget* parent )
: QWidget( parent )
, m_ui( new Ui::EncryptWidget )
, m_state( Encryption::Disabled )
2016-04-22 16:00:19 +02:00
{
m_ui->setupUi( this );
2016-04-22 16:00:19 +02:00
m_ui->m_iconLabel->setFixedWidth( m_ui->m_iconLabel->height() );
m_ui->m_passphraseLineEdit->hide();
m_ui->m_confirmLineEdit->hide();
m_ui->m_iconLabel->hide();
2016-04-22 16:00:19 +02:00
connect( m_ui->m_encryptCheckBox, &QCheckBox::stateChanged, this, &EncryptWidget::onCheckBoxStateChanged );
connect( m_ui->m_passphraseLineEdit, &QLineEdit::textEdited, this, &EncryptWidget::onPassphraseEdited );
connect( m_ui->m_confirmLineEdit, &QLineEdit::textEdited, this, &EncryptWidget::onPassphraseEdited );
2016-04-22 16:00:19 +02:00
setFixedHeight( m_ui->m_passphraseLineEdit->height() ); // Avoid jumping up and down
2016-04-22 16:00:19 +02:00
updateState();
CALAMARES_RETRANSLATE_SLOT( &EncryptWidget::retranslate )
2016-04-22 16:00:19 +02:00
}
void
EncryptWidget::reset()
{
m_ui->m_passphraseLineEdit->clear();
m_ui->m_confirmLineEdit->clear();
m_ui->m_encryptCheckBox->setChecked( false );
}
EncryptWidget::Encryption
2016-04-22 16:00:19 +02:00
EncryptWidget::state() const
{
return m_state;
}
void
EncryptWidget::setText( const QString& text )
{
m_ui->m_encryptCheckBox->setText( text );
}
2016-04-22 16:00:19 +02:00
QString
EncryptWidget::passphrase() const
{
if ( m_state == Encryption::Confirmed )
{
return m_ui->m_passphraseLineEdit->text();
}
2016-04-22 16:00:19 +02:00
return QString();
}
void
EncryptWidget::retranslate()
2016-04-22 16:00:19 +02:00
{
m_ui->retranslateUi( this );
onPassphraseEdited(); // For the tooltip
2016-04-22 16:00:19 +02:00
}
///@brief Give @p label the @p pixmap from the standard-pixmaps
static void
applyPixmap( QLabel* label, CalamaresUtils::ImageType pixmap )
{
label->setFixedWidth( label->height() );
label->setPixmap( CalamaresUtils::defaultPixmap( pixmap, CalamaresUtils::Original, label->size() ) );
}
2016-04-22 16:00:19 +02:00
void
EncryptWidget::updateState()
{
if ( m_ui->m_passphraseLineEdit->isVisible() )
{
QString p1 = m_ui->m_passphraseLineEdit->text();
QString p2 = m_ui->m_confirmLineEdit->text();
if ( p1.isEmpty() && p2.isEmpty() )
{
applyPixmap( m_ui->m_iconLabel, CalamaresUtils::StatusWarning );
m_ui->m_iconLabel->setToolTip( tr( "Please enter the same passphrase in both boxes." ) );
}
else if ( p1 == p2 )
{
applyPixmap( m_ui->m_iconLabel, CalamaresUtils::StatusOk );
m_ui->m_iconLabel->setToolTip( QString() );
}
else
{
applyPixmap( m_ui->m_iconLabel, CalamaresUtils::StatusError );
m_ui->m_iconLabel->setToolTip( tr( "Please enter the same passphrase in both boxes." ) );
}
}
Encryption newState;
if ( m_ui->m_encryptCheckBox->isChecked() )
2016-04-22 16:00:19 +02:00
{
if ( !m_ui->m_passphraseLineEdit->text().isEmpty()
&& m_ui->m_passphraseLineEdit->text() == m_ui->m_confirmLineEdit->text() )
2016-04-22 16:00:19 +02:00
{
newState = Encryption::Confirmed;
2016-04-22 16:00:19 +02:00
}
else
{
newState = Encryption::Unconfirmed;
2016-04-22 16:00:19 +02:00
}
}
else
{
newState = Encryption::Disabled;
2016-04-22 16:00:19 +02:00
}
if ( newState != m_state )
{
m_state = newState;
emit stateChanged( m_state );
}
}
void
EncryptWidget::onPassphraseEdited()
{
if ( !m_ui->m_iconLabel->isVisible() )
{
m_ui->m_iconLabel->show();
}
2016-04-22 16:00:19 +02:00
updateState();
}
void
EncryptWidget::onCheckBoxStateChanged( int checked )
2016-04-22 16:00:19 +02:00
{
// @p checked is a Qt::CheckState, 0 is "unchecked" and 2 is "checked"
m_ui->m_passphraseLineEdit->setVisible( checked );
m_ui->m_confirmLineEdit->setVisible( checked );
m_ui->m_iconLabel->setVisible( checked );
m_ui->m_passphraseLineEdit->clear();
m_ui->m_confirmLineEdit->clear();
m_ui->m_iconLabel->clear();
2016-04-22 16:00:19 +02:00
updateState();
}