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 rrenkert@582: andre@687: CertificateItemWidget::CertificateItemWidget(QWidget *parent, andre@687: const Certificate &cert, andre@687: bool state, andre@687: bool editable, andre@687: const QString &installLabel, andre@687: const QString &removeLabel) : andre@687: QWidget(parent), andre@687: mInstallLabel (installLabel), andre@687: mRemoveLabel (removeLabel) rrenkert@582: { rrenkert@582: mCertificate = cert; rrenkert@638: mState = state; rrenkert@638: mEditable = editable; rrenkert@582: setupGUI(); rrenkert@582: } rrenkert@582: andre@701: andre@701: /* We use the label as data to hide it in the normal dropdown menu and only andre@701: * show it when the popup is shown.*/ andre@701: rrenkert@582: void CertificateItemWidget::setupGUI() rrenkert@582: { andre@743: mLabel = new QLabel; andre@701: mComboBox = new IconOnlyTextPopupBox; andre@701: QStyle *fusionStyle = QStyleFactory::create("Fusion"); andre@701: if (!fusionStyle) { andre@701: qDebug() << "Failed to create fusion style"; andre@701: } else { andre@701: mComboBox->setStyle(fusionStyle); andre@701: } andre@701: andre@743: mComboBox->setIconSize(QSize(48, 48)); andre@743: mComboBox->setFixedWidth(68); andre@743: andre@744: const QString validity = tr("Valid: %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)); andre@743: const QString fpstring = tr("Fingerprint (SHA1): <%1>").arg(mCertificate.fingerprint()); andre@743: mLabel->setText(QString::fromLatin1("%1
%2
%3
%4
").arg andre@743: (mCertificate.subjectCN()).arg(mCertificate.subjectO()).arg(validity).arg andre@743: (fpstring)); andre@743: mLabel->setTextFormat(Qt::RichText); andre@701: andre@701: mLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); andre@780: andre@780: mLabel->setTextInteractionFlags( andre@780: Qt::TextSelectableByMouse | andre@780: Qt::TextSelectableByKeyboard); andre@701: mComboBox->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); rrenkert@627: connect(mComboBox, SIGNAL(currentIndexChanged(int)), rrenkert@627: this, SLOT(currentStateChanged(int))); rrenkert@582: rrenkert@638: QHBoxLayout *layout = new QHBoxLayout; rrenkert@582: if (mCertificate.isInstallCert()) { andre@701: mComboBox->addItem(QIcon(":/img/security-high.png"), QString(), mInstallLabel); andre@701: mComboBox->addItem(QIcon(":/img/security-low.png"), QString(), mRemoveLabel); rrenkert@638: if (mState) rrenkert@638: mComboBox->setCurrentIndex(0); rrenkert@638: else { rrenkert@638: mComboBox->setCurrentIndex(1); rrenkert@638: } rrenkert@638: layout->addWidget(mComboBox); rrenkert@638: } rrenkert@638: else if (!mCertificate.isInstallCert() && !mEditable){ rrenkert@650: QImage *img = new QImage(":/img/trash-empty.png"); rrenkert@638: QLabel *imgLabel = new QLabel; rrenkert@638: imgLabel->setPixmap(QPixmap::fromImage(*img)); andre@743: imgLabel->setFixedSize(48, 48); rrenkert@650: imgLabel->setMargin(2); rrenkert@638: layout->addWidget(imgLabel); rrenkert@582: } rrenkert@582: else { andre@710: mComboBox->addItem(QIcon(":/img/trash-empty.png"), QString(), tr("uninstall")); andre@710: mComboBox->addItem(QIcon(":/img/security-medium.png"), QString(), tr("keep")); rrenkert@638: if (mState) rrenkert@638: mComboBox->setCurrentIndex(0); rrenkert@638: else { rrenkert@638: mComboBox->setCurrentIndex(1); rrenkert@638: } rrenkert@638: layout->addWidget(mComboBox); rrenkert@582: } rrenkert@582: layout->addWidget(mLabel); rrenkert@582: this->setLayout(layout); rrenkert@582: } rrenkert@627: rrenkert@627: bool CertificateItemWidget::state() rrenkert@627: { andre@685: if (!mEditable) { andre@685: return true; andre@685: } andre@701: andre@701: const QString currentString = mComboBox->currentData().toString(); andre@701: andre@701: if (!mCertificate.isInstallCert()) { andre@701: return currentString == tr("uninstall"); andre@701: } andre@701: andre@701: return currentString == mInstallLabel; rrenkert@627: } rrenkert@627: rrenkert@638: void CertificateItemWidget::setState(bool state) rrenkert@638: { rrenkert@638: disconnect(mComboBox, SIGNAL(currentIndexChanged(int)), rrenkert@638: this, SLOT(currentStateChanged(int))); rrenkert@638: rrenkert@638: if (state) { rrenkert@638: mComboBox->setCurrentIndex(0); rrenkert@638: } rrenkert@638: else { rrenkert@638: mComboBox->setCurrentIndex(1); rrenkert@638: } rrenkert@638: connect(mComboBox, SIGNAL(currentIndexChanged(int)), rrenkert@638: this, SLOT(currentStateChanged(int))); rrenkert@638: } rrenkert@638: rrenkert@627: Certificate CertificateItemWidget::certificate() rrenkert@627: { rrenkert@627: return mCertificate; rrenkert@627: } rrenkert@627: rrenkert@627: void CertificateItemWidget::currentStateChanged(int) rrenkert@627: { rrenkert@627: bool state = mComboBox->currentData().toBool(); rrenkert@627: emit stateChanged(state, mCertificate); rrenkert@627: } andre@701: andre@701: void IconOnlyTextPopupBox::showPopup() { andre@701: for (int i = 0; i < count(); i++) { andre@701: setItemText(i, itemData(i).toString()); andre@701: } andre@701: QComboBox::showPopup(); andre@701: } andre@701: andre@701: void IconOnlyTextPopupBox::hidePopup() { andre@701: for (int i = 0; i < count(); i++) { andre@701: setItemText(i, QString()); andre@701: } andre@701: QComboBox::hidePopup(); andre@701: }