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 <QHBoxLayout>
rrenkert@582: #include <QDebug>
andre@701: #include <QStyleFactory>
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: {
andre@1011:     if (mInstallLabel.isEmpty()) {
andre@1011:         mInstallLabel = tr("Install");
andre@1011:     }
andre@1011:     if (mRemoveLabel.isEmpty()) {
andre@1011:         mRemoveLabel = tr("Remove");
andre@1011:     }
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: 
emanuel@842:     mComboBox->setIconSize(QSize(32, 32));
emanuel@842:     mComboBox->setFixedWidth(64);
andre@743: 
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): <code>%1</code>").arg(mCertificate.fingerprint());
emanuel@842:     mLabel->setText(QString::fromLatin1("<big><b>%1</b></big><br/>%2<br/>%3<br/>%4").arg
andre@743:         (mCertificate.subjectCN()).arg(mCertificate.subjectO()).arg(validity).arg
andre@743:         (fpstring));
andre@743:     mLabel->setTextFormat(Qt::RichText);
andre@701: 
emanuel@842:     mLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
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);
emanuel@842:         if (mState) {
rrenkert@638:             mComboBox->setCurrentIndex(0);
emanuel@842:             mComboBox->setToolTip(tr("This certificate is currently installed."));
emanuel@842: 	}
rrenkert@638:         else {
rrenkert@638:             mComboBox->setCurrentIndex(1);
emanuel@842:             mComboBox->setToolTip(tr("This certificate is currently not installed."));
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));
emanuel@842:         imgLabel->setFixedSize(64, 64);
emanuel@842:         imgLabel->setMargin(8);
emanuel@842:         imgLabel->setToolTip(tr("This certificate was uninstalled."));
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"));
emanuel@842:         mComboBox->setToolTip(tr("This certificate is currently installed."));
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: {
andre@839:     bool state = !mComboBox->currentIndex();
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: }