20 #include <QFileDialog>
22 #include <QPushButton>
43 mAuthNotifyLayout =
new QVBoxLayout;
44 this->setLayout( mAuthNotifyLayout );
46 mAuthNotifyLayout->addWidget( mAuthNotify );
51 connect( btnImportFile, &QToolButton::clicked,
this, &QgsAuthImportCertDialog::btnImportFile_clicked );
52 connect( chkAllowInvalid, &QCheckBox::toggled,
this, &QgsAuthImportCertDialog::chkAllowInvalid_toggled );
54 connect( buttonBox, &QDialogButtonBox::accepted,
this, &QDialog::accept );
55 connect( buttonBox, &QDialogButtonBox::rejected,
this, &QDialog::reject );
57 connect( teCertText, &QPlainTextEdit::textChanged,
this, &QgsAuthImportCertDialog::validateCertificates );
59 connect( radioImportFile, &QAbstractButton::toggled,
this, &QgsAuthImportCertDialog::updateGui );
60 connect( radioImportText, &QAbstractButton::toggled,
this, &QgsAuthImportCertDialog::updateGui );
65 radioImportText->setHidden(
true );
66 teCertText->setHidden(
true );
70 radioImportFile->setHidden(
true );
71 frameImportFile->setHidden(
true );
74 radioImportFile->setChecked(
true );
79 grpbxImportCert->setTitle( tr(
"Import Certificate Authorities" ) );
82 okButton()->setText( tr(
"Import" ) );
83 okButton()->setEnabled(
false );
84 teValidation->setFocus();
92 return QList<QSslCertificate>();
103 if ( !radioImportFile->isChecked() )
106 return leImportFile->text();
115 if ( !radioImportText->isChecked() )
118 return teCertText->toPlainText().trimmed();
127 return chkAllowInvalid->isChecked();
136 return cmbbxTrust->trustPolicy();
139 void QgsAuthImportCertDialog::updateGui()
141 frameImportFile->setEnabled( radioImportFile->isChecked() );
142 teCertText->setEnabled( radioImportText->isChecked() );
143 validateCertificates();
146 void QgsAuthImportCertDialog::validateCertificates()
149 teValidation->clear();
150 teValidation->setStyleSheet( QString() );
153 QList<QSslCertificate> certs;
154 QList<QSslCertificate> nixcerts;
156 const bool allowinvalid = chkAllowInvalid->isChecked();
157 const bool filterCAs = ( mFilter ==
CaFilter );
160 if ( radioImportFile->isChecked() && !leImportFile->text().isEmpty() )
164 else if ( radioImportText->isChecked() && !teCertText->toPlainText().trimmed().isEmpty() )
169 const int certssize = certs.size();
171 const auto constCerts = certs;
172 for (
const QSslCertificate &cert : constCerts )
190 valid = ( certssize > 0
191 && ( allowinvalid || certssize == validcerts )
192 && ( !filterCAs || nixcerts.size() < certssize ) );
194 if ( !nixcerts.isEmpty() )
196 const auto constNixcerts = nixcerts;
197 for (
const QSslCertificate &nixcert : constNixcerts )
199 certs.removeOne( nixcert );
208 teValidation->setStyleSheet(
213 QString msg = tr(
"Certificates found: %1\n"
214 "Certificates valid: %2" ).arg( certssize ).arg( validcerts );
218 msg += tr(
"\nAuthorities/Issuers: %1%2" ).arg( cas )
219 .arg( !nixcerts.isEmpty() && nixcerts.size() < certssize ?
" (others not imported)" :
"" );
222 teValidation->setText( msg );
224 okButton()->setEnabled( valid );
227 void QgsAuthImportCertDialog::btnImportFile_clicked()
229 const QString &fn = getOpenFileName( tr(
"Open Certificate File" ), tr(
"All files (*.*);;PEM (*.pem);;DER (*.der)" ) );
232 leImportFile->setText( fn );
234 validateCertificates();
237 void QgsAuthImportCertDialog::chkAllowInvalid_toggled(
bool checked )
240 validateCertificates();
243 QString QgsAuthImportCertDialog::getOpenFileName(
const QString &title,
const QString &extfilter )
246 const QString recentdir = settings.
value( QStringLiteral(
"UI/lastAuthImportCertOpenFileDir" ), QDir::homePath() ).toString();
247 QString f = QFileDialog::getOpenFileName(
this, title, recentdir, extfilter );
251 this->activateWindow();
255 settings.
setValue( QStringLiteral(
"UI/lastAuthImportCertOpenFileDir" ), QFileInfo( f ).absoluteDir().path() );
260 QPushButton *QgsAuthImportCertDialog::okButton()
262 return buttonBox->button( QDialogButtonBox::Ok );