view ui/certificateitemwidget.cpp @ 1101:2b3526ef2d69

(issue111) Keep manually changed certificates in the manually changed list. This invents the new property "active certificate" an inactive certificate is a certificate that is displayed but has no impact on the store.
author Andre Heinecke <andre.heinecke@intevation.de>
date Mon, 15 Sep 2014 13:55:47 +0200
parents 9c4543128666
children 6f7b7d88f048
line wrap: on
line source
/* Copyright (C) 2014 by Bundesamt für Sicherheit in der Informationstechnik
 * Software engineering by Intevation GmbH
 *
 * This file is Free Software under the GNU GPL (v>=2)
 * and comes with ABSOLUTELY NO WARRANTY!
 * See LICENSE.txt for details.
 */
#include "certificateitemwidget.h"

#include <QHBoxLayout>
#include <QDebug>
#include <QStyleFactory>

CertificateItemWidget::CertificateItemWidget(QWidget *parent,
                                             const Certificate &cert,
                                             bool state,
                                             bool editable,
                                             const QString &installLabel,
                                             const QString &removeLabel) :
    QWidget(parent),
    mInstallLabel (installLabel),
    mRemoveLabel (removeLabel)
{
    if (mInstallLabel.isEmpty()) {
        mInstallLabel = tr("Install");
    }
    if (mRemoveLabel.isEmpty()) {
        mRemoveLabel = tr("Remove");
    }
    mCertificate = cert;
    mState = state;
    mEditable = editable;
    setupGUI();
}


/* We use the label as data to hide it in the normal dropdown menu and only
 * show it when the popup is shown.*/

void CertificateItemWidget::setupGUI()
{
    mLabel = new QLabel;
    mComboBox = new IconOnlyTextPopupBox;
    QStyle *fusionStyle = QStyleFactory::create("Fusion");
    if (!fusionStyle) {
        qDebug() << "Failed to create fusion style";
    } else {
        mComboBox->setStyle(fusionStyle);
    }

    mComboBox->setIconSize(QSize(32, 32));
    mComboBox->setFixedWidth(64);

    setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);

    const QString validity = tr("Validity: %1 until %2").arg(
            QLocale::system().toString(mCertificate.validFrom().date(), QLocale::ShortFormat)).arg(
            QLocale::system().toString(mCertificate.validTo().date(), QLocale::ShortFormat));
    const QString fpstring = tr("Fingerprint (SHA1): <code>%1</code>").arg(mCertificate.fingerprint());
    mLabel->setText(QString::fromLatin1("<big><b>%1</b></big><br/>%2<br/>%3<br/>%4").arg
        (mCertificate.subjectCN()).arg(mCertificate.subjectO()).arg(validity).arg
        (fpstring));
    mLabel->setTextFormat(Qt::RichText);

    mLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);

    mLabel->setTextInteractionFlags(
        Qt::TextSelectableByMouse |
        Qt::TextSelectableByKeyboard);
    mComboBox->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
    connect(mComboBox, SIGNAL(currentIndexChanged(int)),
        this, SLOT(currentStateChanged(int)));

    QHBoxLayout *layout = new QHBoxLayout;
    if (mCertificate.isInstallCert()) {
        mComboBox->addItem(QIcon(":/img/security-high.png"), QString(), mInstallLabel);
        mComboBox->addItem(QIcon(":/img/security-low.png"), QString(), mRemoveLabel);
        if (mState) {
            mComboBox->setCurrentIndex(0);
            mComboBox->setToolTip(tr("This certificate is currently installed."));
	}
        else {
            mComboBox->setCurrentIndex(1);
            mComboBox->setToolTip(tr("This certificate is currently not installed."));
        }
        layout->addWidget(mComboBox);
    }
    else if (!mCertificate.isInstallCert() && !mEditable){
        QImage *img = new QImage(":/img/trash-empty.png");
        QLabel *imgLabel = new QLabel;
        imgLabel->setPixmap(QPixmap::fromImage(*img));
        imgLabel->setFixedSize(64, 64);
        imgLabel->setMargin(8);
        imgLabel->setToolTip(tr("This certificate was uninstalled."));
        layout->addWidget(imgLabel);
    }
    else {
        mComboBox->addItem(QIcon(":/img/trash-empty.png"), QString(), tr("uninstall"));
        mComboBox->addItem(QIcon(":/img/security-medium.png"), QString(), tr("keep"));
        mComboBox->setToolTip(tr("This certificate is currently installed."));
        if (mState)
            mComboBox->setCurrentIndex(0);
        else {
            mComboBox->setCurrentIndex(1);
        }
        layout->addWidget(mComboBox);
    }
    layout->addWidget(mLabel);
    this->setLayout(layout);
}

bool CertificateItemWidget::state()
{
    if (!mEditable) {
        return true;
    }

    const QString currentString = mComboBox->currentData().toString();

    if (!mCertificate.isInstallCert()) {
        return currentString == tr("uninstall");
    }

    return currentString == mInstallLabel;
}

void CertificateItemWidget::setState(bool state)
{
    disconnect(mComboBox, SIGNAL(currentIndexChanged(int)),
        this, SLOT(currentStateChanged(int)));

    if (state) {
        mComboBox->setCurrentIndex(0);
    }
    else {
        mComboBox->setCurrentIndex(1);
    }
    connect(mComboBox, SIGNAL(currentIndexChanged(int)),
        this, SLOT(currentStateChanged(int)));
}

Certificate* CertificateItemWidget::certificate()
{
    return &mCertificate;
}

void CertificateItemWidget::currentStateChanged(int)
{
    bool state = !mComboBox->currentIndex();
    emit stateChanged(state, mCertificate);
}

void IconOnlyTextPopupBox::showPopup() {
    for (int i = 0; i < count(); i++) {
        setItemText(i, itemData(i).toString());
    }
    QComboBox::showPopup();
}

void IconOnlyTextPopupBox::hidePopup() {
    for (int i = 0; i < count(); i++) {
        setItemText(i, QString());
    }
    QComboBox::hidePopup();
}

http://wald.intevation.org/projects/trustbridge/