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( QLatin1String(
"" ) );
153 QList<QSslCertificate> certs;
154 QList<QSslCertificate> nixcerts;
156 bool allowinvalid = chkAllowInvalid->isChecked();
157 bool filterCAs = ( mFilter ==
CaFilter );
160 if ( radioImportFile->isChecked() && !leImportFile->text().isEmpty() )
164 else if ( radioImportText->isChecked() && !teCertText->toPlainText().trimmed().isEmpty() )
169 int certssize = certs.size();
171 Q_FOREACH (
const QSslCertificate &cert, certs )
189 valid = ( certssize > 0
190 && ( allowinvalid || certssize == validcerts )
191 && ( !filterCAs || nixcerts.size() < certssize ) );
193 if ( !nixcerts.isEmpty() )
195 Q_FOREACH (
const QSslCertificate &nixcert, nixcerts )
197 certs.removeOne( nixcert );
206 teValidation->setStyleSheet(
211 QString msg = tr(
"Certificates found: %1\n" 212 "Certificates valid: %2" ).arg( certssize ).arg( validcerts );
216 msg += tr(
"\nAuthorities/Issuers: %1%2" ).arg( cas )
217 .arg( !nixcerts.isEmpty() && nixcerts.size() < certssize ?
" (others not imported)" :
"" );
220 teValidation->setText( msg );
222 okButton()->setEnabled( valid );
225 void QgsAuthImportCertDialog::btnImportFile_clicked()
227 const QString &fn = getOpenFileName( tr(
"Open Certificate File" ), tr(
"All files (*.*);;PEM (*.pem);;DER (*.der)" ) );
230 leImportFile->setText( fn );
232 validateCertificates();
235 void QgsAuthImportCertDialog::chkAllowInvalid_toggled(
bool checked )
238 validateCertificates();
241 QString QgsAuthImportCertDialog::getOpenFileName(
const QString &title,
const QString &extfilter )
244 QString recentdir = settings.
value( QStringLiteral(
"UI/lastAuthImportCertOpenFileDir" ), QDir::homePath() ).toString();
245 QString f = QFileDialog::getOpenFileName(
this, title, recentdir, extfilter );
249 this->activateWindow();
253 settings.
setValue( QStringLiteral(
"UI/lastAuthImportCertOpenFileDir" ), QFileInfo( f ).absoluteDir().path() );
258 QPushButton *QgsAuthImportCertDialog::okButton()
260 return buttonBox->button( QDialogButtonBox::Ok );
This class is a composition of two QSettings instances:
const QString certTextToImport()
Gets certificate text to import.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
static QList< QSslCertificate > certsFromFile(const QString &certspath)
Returns a list of concatenated certs from a PEM or DER formatted file.
static bool certificateIsAuthorityOrIssuer(const QSslCertificate &cert)
Gets whether a certificate is an Authority or can at least sign other certificates.
bool allowInvalidCerts()
Whether to allow importation of invalid certificates (so trust policy can be overridden) ...
const QString certFileToImport()
Gets the file path to a certificate to import.
CertInput
Type of inputs for certificates.
static bool certIsViable(const QSslCertificate &cert)
certIsViable checks for viability errors of cert and whether it is NULL
QgsAuthCertUtils::CertTrustPolicy certTrustPolicy()
Defined trust policy for imported certificates.
static QString greenTextStyleSheet(const QString &selector="*")
Green text stylesheet representing valid, trusted, etc. certificate.
static QList< QSslCertificate > certsFromString(const QString &pemtext)
Returns a list of concatenated certs from a PEM Base64 text block.
const QList< QSslCertificate > certificatesToImport()
Gets list of certificate objects to import.
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
static QString redTextStyleSheet(const QString &selector="*")
Red text stylesheet representing invalid, untrusted, etc. certificate.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
CertTrustPolicy
Type of certificate trust policy.
CertFilter
Type of filter to apply to dialog.
QgsAuthImportCertDialog(QWidget *parent=nullptr, QgsAuthImportCertDialog::CertFilter filter=NoFilter, QgsAuthImportCertDialog::CertInput input=AllInputs)
Construct a dialog for importing certificates.