rrenkert@582: /* Copyright (C) 2014 by Bundesamt für Sicherheit in der Informationstechnik rrenkert@582: * Software engineering by Intevation GmbH rrenkert@582: * rrenkert@582: * This file is Free Software under the GNU GPL (v>=2) rrenkert@582: * and comes with ABSOLUTELY NO WARRANTY! rrenkert@582: * See LICENSE.txt for details. rrenkert@582: */ rrenkert@582: #include "certificateitemwidget.h" rrenkert@582: rrenkert@582: #include rrenkert@582: #include andre@701: #include andre@1106: #include andre@1234: #include andre@1293: #include andre@1297: #include andre@1234: andre@1234: void CheckLessToolBtn::paintEvent(QPaintEvent * pe) { andre@1234: andre@1234: /* Hack to always paint the button as if it were andre@1234: * not checked. */ andre@1234: bool oldchecked = isChecked(); andre@1234: QIcon oldIcon = icon(); andre@1234: QIcon tmpIcon; andre@1234: if (isEnabled()) andre@1234: tmpIcon = QIcon(oldIcon.pixmap(QSize(48, 48), QIcon::Normal, oldchecked ? QIcon::On : QIcon::Off)); andre@1234: else { andre@1234: tmpIcon = QIcon(oldIcon.pixmap(QSize(48, 48), QIcon::Disabled, oldchecked ? QIcon::On : QIcon::Off)); andre@1234: } andre@1234: QSignalBlocker blk(this); andre@1234: setChecked(false); andre@1234: setIcon(tmpIcon); andre@1234: QToolButton::paintEvent(pe); andre@1234: setIcon(oldIcon); andre@1234: setChecked(oldchecked); andre@1234: } rrenkert@582: andre@687: CertificateItemWidget::CertificateItemWidget(QWidget *parent, andre@687: const Certificate &cert, andre@687: bool state, andre@1106: QToolButton *btn) : andre@687: QWidget(parent), andre@1106: mButton(btn) rrenkert@582: { rrenkert@582: mCertificate = cert; andre@1109: mOriginalState = state; andre@1106: btn->setCheckable(true); andre@1234: /* btn->setStyleSheet("QToolButton:Checked{" andre@1109: "border: 1px solid #8f8f91;" andre@1109: "background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1," andre@1109: "stop: 0 #f6f7fa, stop: 1 #dadbde);" andre@1109: "}" andre@1234: );*/ andre@1109: setState(state); rrenkert@582: setupGUI(); rrenkert@582: } rrenkert@582: rrenkert@582: void CertificateItemWidget::setupGUI() rrenkert@582: { andre@786: setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); andre@786: emanuel@842: const QString validity = tr("Validity: %1 until %2").arg( andre@743: QLocale::system().toString(mCertificate.validFrom().date(), QLocale::ShortFormat)).arg( andre@743: QLocale::system().toString(mCertificate.validTo().date(), QLocale::ShortFormat)); emanuel@842: const QString fpstring = tr("Fingerprint (SHA1): %1").arg(mCertificate.fingerprint()); andre@1293: andre@1293: QVBoxLayout *labelLayout = new QVBoxLayout; andre@1293: QLabel *firstLabel = new QLabel(QString::fromLatin1("%1 ").arg andre@1293: (mCertificate.subjectCN())); andre@1293: andre@1293: QHBoxLayout *firstLabelButtonLayout = new QHBoxLayout; andre@1293: firstLabelButtonLayout->addWidget(firstLabel); andre@1293: QPushButton *detailsBtn = new QPushButton; andre@1298: detailsBtn->setIcon(QIcon(":/img/document-preview.png")); andre@1293: detailsBtn->setToolTip(tr("Open the Windows certificate information dialog.")); andre@1293: andre@1293: firstLabelButtonLayout->addWidget(detailsBtn); andre@1293: firstLabelButtonLayout->addStretch(-1); andre@1293: connect(detailsBtn, SIGNAL(clicked()), this, SLOT(showCertDlg())); andre@1293: labelLayout->addLayout(firstLabelButtonLayout); andre@1293: QLabel *secondLabel = new QLabel(QString::fromLatin1("%2
%3
%4").arg andre@1293: (mCertificate.subjectO()).arg(validity).arg andre@743: (fpstring)); andre@701: andre@1293: labelLayout->addWidget(secondLabel); andre@1293: secondLabel->setTextFormat(Qt::RichText); andre@1293: secondLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); andre@780: andre@1293: secondLabel->setTextInteractionFlags( andre@780: Qt::TextSelectableByMouse | andre@780: Qt::TextSelectableByKeyboard); andre@1293: firstLabel->setTextFormat(Qt::RichText); andre@1293: firstLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); andre@1293: andre@1293: firstLabel->setTextInteractionFlags( andre@1293: Qt::TextSelectableByMouse | andre@1293: Qt::TextSelectableByKeyboard); andre@1106: mButton->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); andre@1106: connect(mButton, SIGNAL(toggled (bool)), andre@1106: this, SLOT(currentStateChanged(bool))); rrenkert@582: rrenkert@638: QHBoxLayout *layout = new QHBoxLayout; andre@1106: layout->addWidget(mButton); andre@1106: mButton->setFixedSize(64, 64); andre@1106: mButton->setIconSize(QSize(48, 48)); andre@1293: layout->addLayout(labelLayout); rrenkert@582: this->setLayout(layout); rrenkert@582: } rrenkert@627: andre@1288: void CertificateItemWidget::showCertDlg() andre@1288: { andre@1288: /* This is a totally evil cast but legitimate on Windows andre@1288: * HANDLES are only 32 bit even on Windows 64 bit */ andre@1297: if (!mCertificate.showNativeUI((void*)effectiveWinId())) { andre@1297: qDebug() << "Failed to show native dialog"; andre@1297: #ifdef WIN32 andre@1297: // maybe old windows version andre@1297: QMessageBox::warning(this, tr("TrustBridge error"), tr("Failed to open native viewer.")); andre@1297: #else andre@1297: QMessageBox::warning(this, tr("TrustBridge error"), tr("Failed to open 'gcr-viewer'.") + "\n" + andre@1297: tr("TrustBridge uses 'gcr-viewer' to show certificate details. Please ensure that 'gcr-viewer' is installed.")); andre@1297: #endif andre@1297: } andre@1288: return; andre@1288: } andre@1288: rrenkert@627: bool CertificateItemWidget::state() rrenkert@627: { andre@1106: if (!mButton->isEnabled()) { andre@685: return true; andre@685: } andre@701: andre@1109: return !mButton->isChecked(); rrenkert@627: } rrenkert@627: rrenkert@638: void CertificateItemWidget::setState(bool state) rrenkert@638: { andre@1109: /* The internal state we get here is inverted for Ui reasons the logic andre@1109: * is if a certificate is selected for installation the button andre@1109: * is disabled (as this is the default) Only those that are andre@1109: * unselected have the enabled button. */ andre@1109: mButton->blockSignals(true); // code did this and not the user andre@1106: mButton->setChecked(!state); andre@1109: mButton->blockSignals(false); andre@1109: if (mButton->isEnabled()) { andre@1109: mButton->setToolTip(mButton->property(!state ? "ToolTip_On" : andre@1109: "ToolTip_Off").toString()); andre@1109: } rrenkert@638: } rrenkert@638: andre@1108: Certificate CertificateItemWidget::certificate() rrenkert@627: { andre@1108: return mCertificate; rrenkert@627: } rrenkert@627: andre@1106: void CertificateItemWidget::currentStateChanged(bool state) rrenkert@627: { andre@1109: mButton->setToolTip(mButton->property(state ? "ToolTip_On" : andre@1109: "ToolTip_Off").toString()); andre@1109: emit stateChanged(state, mCertificate); rrenkert@627: }