30 #include <QSslCertificate>
31 #include <QtGui/QTreeWidgetItem>
32 #include <QtGui/QStandardItemModel>
44 static const QSslCertificate::SubjectInfo fields[3] = {
45 QSslCertificate::Organization,
46 QSslCertificate::CommonName,
47 QSslCertificate::OrganizationalUnitName
49 for (
int i = 0; i < 3; i++) {
50 issuerText = cert.issuerInfo(fields[i]);
51 if (!issuerText.isEmpty()) {
59 class CaCertificateItem :
public QTreeWidgetItem
62 CaCertificateItem(QTreeWidgetItem *parent,
const QSslCertificate &cert,
bool isEnabled)
63 : QTreeWidgetItem(parent, m_type),
66 setEnabled(isEnabled);
69 QVariant data(
int column,
int role)
const
76 QString subjectText = m_cert.issuerInfo(QSslCertificate::CommonName);
78 return subjectText.toLower();
82 return m_cert.issuerInfo(QSslCertificate::OrganizationalUnitName);
86 return QTreeWidgetItem::data(column, role);
89 bool isEnabled()
const
91 return data(
OrgCnColumn, Qt::CheckStateRole).toInt() == Qt::Checked;
94 void setEnabled(
bool enabled)
96 setData(
OrgCnColumn, Qt::CheckStateRole, enabled ? Qt::Checked : Qt::Unchecked);
99 static const int m_type = QTreeWidgetItem::UserType;
100 QSslCertificate m_cert;
105 m_firstShowEvent(true),
106 m_blockItemChanged(false)
109 connect(m_ui.displaySelection, SIGNAL(clicked()), SLOT(displaySelectionClicked()));
110 connect(m_ui.disableSelection, SIGNAL(clicked()), SLOT(disableSelectionClicked()));
111 connect(m_ui.enableSelection, SIGNAL(clicked()), SLOT(enableSelectionClicked()));
112 connect(m_ui.removeSelection, SIGNAL(clicked()), SLOT(removeSelectionClicked()));
113 connect(m_ui.add, SIGNAL(clicked()), SLOT(addCertificateClicked()));
114 connect(m_ui.treeWidget, SIGNAL(itemChanged(QTreeWidgetItem*,
int)),
115 SLOT(itemChanged(QTreeWidgetItem*,
int)));
116 connect(m_ui.treeWidget, SIGNAL(itemSelectionChanged()),
117 SLOT(itemSelectionChanged()));
126 m_ui.treeWidget->clear();
127 m_ui.treeWidget->sortByColumn(-1);
128 m_knownCertificates.clear();
130 m_systemCertificatesParent =
new QTreeWidgetItem(m_ui.treeWidget);
131 m_systemCertificatesParent->setText(0,
i18n(
"System certificates"));
134 m_systemCertificatesParent->setExpanded(
true);
135 m_systemCertificatesParent->setFlags(m_systemCertificatesParent->flags() & ~Qt::ItemIsSelectable);
137 m_userCertificatesParent =
new QTreeWidgetItem(m_ui.treeWidget);
138 m_userCertificatesParent->setText(0,
i18n(
"User-added certificates"));
140 m_userCertificatesParent->setExpanded(
true);
141 m_userCertificatesParent->setFlags(m_userCertificatesParent->flags() & ~Qt::ItemIsSelectable);
144 kDebug(7029) <<
"# certs:" << caCerts.count();
146 addCertificateItem(caCert);
154 if (m_firstShowEvent) {
157 m_firstShowEvent =
false;
159 QWidget::showEvent(event);
167 QTreeWidgetItem *grandParent = m_systemCertificatesParent;
169 for (
int i = 0; i < 2; i++) {
170 for (
int j = 0; j < grandParent->childCount(); j++) {
172 QTreeWidgetItem *parentItem = grandParent->child(j);
173 for (
int k = 0; k < parentItem->childCount(); k++) {
174 CaCertificateItem *item =
static_cast<CaCertificateItem *
>(parentItem->child(k));
179 grandParent = m_userCertificatesParent;
182 kDebug(7029) <<
"# certs:" << newState.count();
196 void CaCertificatesPage::itemSelectionChanged()
198 kDebug(7029) << m_ui.treeWidget->selectionModel()->hasSelection();
199 int numRemovable = 0;
201 int numDisplayable = 0;
202 foreach(
const QTreeWidgetItem *twItem, m_ui.treeWidget->selectedItems()) {
203 const CaCertificateItem *item =
dynamic_cast<const CaCertificateItem *
>(twItem);
207 if (item->parent()->parent() == m_userCertificatesParent) {
210 if (item->isEnabled()) {
215 m_ui.displaySelection->setEnabled(numDisplayable);
216 m_ui.removeSelection->setEnabled(numRemovable);
217 m_ui.disableSelection->setEnabled(numEnabled);
218 m_ui.enableSelection->setEnabled(numDisplayable > numEnabled);
222 void CaCertificatesPage::displaySelectionClicked()
225 foreach(
const QTreeWidgetItem *twItem, m_ui.treeWidget->selectedItems()) {
226 const CaCertificateItem *item =
dynamic_cast<const CaCertificateItem *
>(twItem);
229 certs += item->m_cert;
233 dc.setCertificates(certs);
238 void CaCertificatesPage::disableSelectionClicked()
240 enableDisableSelectionClicked(
false);
244 void CaCertificatesPage::enableSelectionClicked()
246 enableDisableSelectionClicked(
true);
249 void CaCertificatesPage::enableDisableSelectionClicked(
bool isEnable)
251 const bool prevBlockItemChanged = m_blockItemChanged;
252 m_blockItemChanged =
true;
253 foreach(QTreeWidgetItem *twItem, m_ui.treeWidget->selectedItems()) {
254 CaCertificateItem *item =
dynamic_cast<CaCertificateItem *
>(twItem);
257 item->setEnabled(isEnable);
261 m_blockItemChanged = prevBlockItemChanged;
263 itemSelectionChanged();
268 void CaCertificatesPage::removeSelectionClicked()
270 bool didRemove =
false;
271 foreach(QTreeWidgetItem *twItem, m_ui.treeWidget->selectedItems()) {
272 const CaCertificateItem *item =
dynamic_cast<const CaCertificateItem *
>(twItem);
274 if (!item || item->parent()->parent() != m_userCertificatesParent) {
277 QTreeWidgetItem *parent = item->parent();
278 m_knownCertificates.remove(item->m_cert.digest().toHex());
281 if (parent->childCount() == 0) {
291 void CaCertificatesPage::addCertificateClicked()
295 this,
i18n(
"Pick Certificates"));
298 foreach (
const QString &certFile, certFiles) {
300 const int prevCertCount = certs.count();
301 certs += QSslCertificate::fromPath(certFile, QSsl::Pem);
302 if (prevCertCount == certs.count()) {
303 certs += QSslCertificate::fromPath(certFile, QSsl::Der);
305 if (prevCertCount == certs.count()) {
306 kDebug(7029) <<
"failed to load certificate file" << certFile;
310 bool didAddCertificates =
false;
311 foreach (
const QSslCertificate &cert, certs) {
313 if (!addCertificateItem(caCert)) {
316 didAddCertificates =
true;
319 if (didAddCertificates) {
325 void CaCertificatesPage::itemChanged(QTreeWidgetItem *item,
int column)
330 if (m_blockItemChanged) {
338 itemSelectionChanged();
344 for (
int i = 0; i < parent->childCount(); i ++) {
345 QTreeWidgetItem *candidate = parent->child(i);
355 if (m_knownCertificates.contains(caCert.
certHash)) {
356 kDebug(7029) <<
"CaCertificatesPage::addCertificateItem(): refusing duplicate";
359 const bool prevBlockItemChanged = m_blockItemChanged;
360 m_blockItemChanged =
true;
362 m_systemCertificatesParent : m_userCertificatesParent;
367 parent =
new QTreeWidgetItem(grandParent);
370 parent->setExpanded(
true);
371 parent->setFlags(parent->flags() & ~Qt::ItemIsSelectable);
375 m_knownCertificates.insert(caCert.
certHash);
376 m_blockItemChanged = prevBlockItemChanged;
380 #include "cacertificatespage.moc"
QString i18n(const char *text)
const QByteArray certHash
QList< KSslCaCertificate > _allKsslCaCertificates(KSslCertificateManager *cm)
void _setAllKsslCaCertificates(KSslCertificateManager *cm, const QList< KSslCaCertificate > &certsIn)
static QStringList getOpenFileNames(const KUrl &startDir=KUrl(), const QString &filter=QString(), QWidget *parent=0, const QString &caption=QString())
Creates a modal file dialog and returns the selected filenames or an empty list if none was chosen...
static KSslCertificateManager * self()
const QSslCertificate cert
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
static QTreeWidgetItem * findImmediateChild(QTreeWidgetItem *parent, const QString &issuerText)
CaCertificatesPage(QWidget *parent)
void showEvent(QShowEvent *event)
static QString nonemptyIssuer(const QSslCertificate &cert)