1 /* === This file is part of Calamares - <https://calamares.io> ===
2  *
3  *   SPDX-FileCopyrightText: 2016 Teo Mrnjavac <teo@kde.org>
4  *   SPDX-FileCopyrightText: 2020 Adriaan de Groot <groot@kde.org>
5  *   SPDX-License-Identifier: GPL-3.0-or-later
6  *
7  *   Calamares is Free Software: see the License-Identifier above.
8  *
9  */
10 
11 
12 #include "EncryptWidget.h"
13 
14 #include "ui_EncryptWidget.h"
15 
16 #include "utils/CalamaresUtilsGui.h"
17 #include "utils/Retranslator.h"
18 
EncryptWidget(QWidget * parent)19 EncryptWidget::EncryptWidget( QWidget* parent )
20     : QWidget( parent )
21     , m_ui( new Ui::EncryptWidget )
22     , m_state( Encryption::Disabled )
23 {
24     m_ui->setupUi( this );
25 
26     m_ui->m_iconLabel->setFixedWidth( m_ui->m_iconLabel->height() );
27     m_ui->m_passphraseLineEdit->hide();
28     m_ui->m_confirmLineEdit->hide();
29     m_ui->m_iconLabel->hide();
30 
31     connect( m_ui->m_encryptCheckBox, &QCheckBox::stateChanged, this, &EncryptWidget::onCheckBoxStateChanged );
32     connect( m_ui->m_passphraseLineEdit, &QLineEdit::textEdited, this, &EncryptWidget::onPassphraseEdited );
33     connect( m_ui->m_confirmLineEdit, &QLineEdit::textEdited, this, &EncryptWidget::onPassphraseEdited );
34 
35     setFixedHeight( m_ui->m_passphraseLineEdit->height() );  // Avoid jumping up and down
36     updateState();
37 
38     CALAMARES_RETRANSLATE_SLOT( &EncryptWidget::retranslate );
39 }
40 
41 
42 void
reset()43 EncryptWidget::reset()
44 {
45     m_ui->m_passphraseLineEdit->clear();
46     m_ui->m_confirmLineEdit->clear();
47 
48     m_ui->m_encryptCheckBox->setChecked( false );
49 }
50 
51 
52 EncryptWidget::Encryption
state() const53 EncryptWidget::state() const
54 {
55     return m_state;
56 }
57 
58 
59 void
setText(const QString & text)60 EncryptWidget::setText( const QString& text )
61 {
62     m_ui->m_encryptCheckBox->setText( text );
63 }
64 
65 
66 QString
passphrase() const67 EncryptWidget::passphrase() const
68 {
69     if ( m_state == Encryption::Confirmed )
70     {
71         return m_ui->m_passphraseLineEdit->text();
72     }
73     return QString();
74 }
75 
76 
77 void
retranslate()78 EncryptWidget::retranslate()
79 {
80     m_ui->retranslateUi( this );
81     onPassphraseEdited();  // For the tooltip
82 }
83 
84 
85 ///@brief Give @p label the @p pixmap from the standard-pixmaps
86 static void
applyPixmap(QLabel * label,CalamaresUtils::ImageType pixmap)87 applyPixmap( QLabel* label, CalamaresUtils::ImageType pixmap )
88 {
89     label->setFixedWidth( label->height() );
90     label->setPixmap( CalamaresUtils::defaultPixmap( pixmap, CalamaresUtils::Original, label->size() ) );
91 }
92 
93 void
updateState()94 EncryptWidget::updateState()
95 {
96     if ( m_ui->m_passphraseLineEdit->isVisible() )
97     {
98         QString p1 = m_ui->m_passphraseLineEdit->text();
99         QString p2 = m_ui->m_confirmLineEdit->text();
100 
101         if ( p1.isEmpty() && p2.isEmpty() )
102         {
103             applyPixmap( m_ui->m_iconLabel, CalamaresUtils::StatusWarning );
104             m_ui->m_iconLabel->setToolTip( tr( "Please enter the same passphrase in both boxes." ) );
105         }
106         else if ( p1 == p2 )
107         {
108             applyPixmap( m_ui->m_iconLabel, CalamaresUtils::StatusOk );
109             m_ui->m_iconLabel->setToolTip( QString() );
110         }
111         else
112         {
113             applyPixmap( m_ui->m_iconLabel, CalamaresUtils::StatusError );
114             m_ui->m_iconLabel->setToolTip( tr( "Please enter the same passphrase in both boxes." ) );
115         }
116     }
117 
118     Encryption newState;
119     if ( m_ui->m_encryptCheckBox->isChecked() )
120     {
121         if ( !m_ui->m_passphraseLineEdit->text().isEmpty()
122              && m_ui->m_passphraseLineEdit->text() == m_ui->m_confirmLineEdit->text() )
123         {
124             newState = Encryption::Confirmed;
125         }
126         else
127         {
128             newState = Encryption::Unconfirmed;
129         }
130     }
131     else
132     {
133         newState = Encryption::Disabled;
134     }
135 
136     if ( newState != m_state )
137     {
138         m_state = newState;
139         Q_EMIT stateChanged( m_state );
140     }
141 }
142 
143 void
onPassphraseEdited()144 EncryptWidget::onPassphraseEdited()
145 {
146     if ( !m_ui->m_iconLabel->isVisible() )
147     {
148         m_ui->m_iconLabel->show();
149     }
150 
151     updateState();
152 }
153 
154 
155 void
onCheckBoxStateChanged(int checked)156 EncryptWidget::onCheckBoxStateChanged( int checked )
157 {
158     // @p checked is a Qt::CheckState, 0 is "unchecked" and 2 is "checked"
159     m_ui->m_passphraseLineEdit->setVisible( checked );
160     m_ui->m_confirmLineEdit->setVisible( checked );
161     m_ui->m_iconLabel->setVisible( checked );
162     m_ui->m_passphraseLineEdit->clear();
163     m_ui->m_confirmLineEdit->clear();
164     m_ui->m_iconLabel->clear();
165 
166     updateState();
167 }
168