view ui/certificateitemwidget.cpp @ 1402:1adf72328b75 tip

Added tag 1.0 for changeset ee807f64e21e
author Andre Heinecke <andre.heinecke@intevation.de>
date Tue, 27 Jan 2015 15:18:32 +0100
parents 9017c524e762
children
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>
#include <QToolButton>
#include <QSignalBlocker>
#include <QPushButton>
#include <QMessageBox>

void CheckLessToolBtn::paintEvent(QPaintEvent * pe) {

    /* Hack to always paint the button as if it were
     * not checked. */
    bool oldchecked = isChecked();
    QIcon oldIcon = icon();
    QIcon tmpIcon;
    if (isEnabled())
        tmpIcon = QIcon(oldIcon.pixmap(QSize(48, 48), QIcon::Normal, oldchecked ? QIcon::On : QIcon::Off));
    else {
        tmpIcon = QIcon(oldIcon.pixmap(QSize(48, 48), QIcon::Disabled, oldchecked ? QIcon::On : QIcon::Off));
    }
    QSignalBlocker blk(this);
    setChecked(false);
    setIcon(tmpIcon);
    QToolButton::paintEvent(pe);
    setIcon(oldIcon);
    setChecked(oldchecked);
}

CertificateItemWidget::CertificateItemWidget(QWidget *parent,
                                             const Certificate &cert,
                                             bool state,
                                             QToolButton *btn) :
    QWidget(parent),
    mButton(btn)
{
    mCertificate = cert;
    mOriginalState = state;
    btn->setCheckable(true);
/*    btn->setStyleSheet("QToolButton:Checked{"
         "border: 1px solid #8f8f91;"
         "background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,"
                                           "stop: 0 #f6f7fa, stop: 1 #dadbde);"
    "}"
    );*/
    setState(state);
    setupGUI();
}

void CertificateItemWidget::setupGUI()
{
    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());

    QVBoxLayout *labelLayout = new QVBoxLayout;
    QLabel *firstLabel = new QLabel(QString::fromLatin1("<big><b>%1</b></big> ").arg
        (mCertificate.subjectCN()));

    QHBoxLayout *firstLabelButtonLayout = new QHBoxLayout;
    firstLabelButtonLayout->addWidget(firstLabel);
    QPushButton *detailsBtn = new QPushButton;
    detailsBtn->setIcon(QIcon(":/img/document-preview.png"));
    detailsBtn->setToolTip(tr("Open the Windows certificate information dialog."));

    firstLabelButtonLayout->addWidget(detailsBtn);
    firstLabelButtonLayout->addStretch(-1);
    connect(detailsBtn, SIGNAL(clicked()), this, SLOT(showCertDlg()));
    labelLayout->addLayout(firstLabelButtonLayout);
    QLabel *secondLabel = new QLabel(QString::fromLatin1("%2<br/>%3<br/>%4").arg
        (mCertificate.subjectO()).arg(validity).arg
        (fpstring));

    labelLayout->addWidget(secondLabel);
    secondLabel->setTextFormat(Qt::RichText);
    secondLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);

    secondLabel->setTextInteractionFlags(
        Qt::TextSelectableByMouse |
        Qt::TextSelectableByKeyboard);
    firstLabel->setTextFormat(Qt::RichText);
    firstLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);

    firstLabel->setTextInteractionFlags(
        Qt::TextSelectableByMouse |
        Qt::TextSelectableByKeyboard);
    mButton->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
    connect(mButton, SIGNAL(toggled (bool)),
        this, SLOT(currentStateChanged(bool)));

    QHBoxLayout *layout = new QHBoxLayout;
    layout->addWidget(mButton);
    mButton->setFixedSize(64, 64);
    mButton->setIconSize(QSize(48, 48));
    layout->addLayout(labelLayout);
    this->setLayout(layout);
}

void CertificateItemWidget::showCertDlg()
{
    /* This is a totally evil cast but legitimate on Windows
     * HANDLES are only 32 bit even on Windows 64 bit */
    if (!mCertificate.showNativeUI((void*)effectiveWinId())) {
        qDebug() << "Failed to show native dialog";
#ifdef WIN32
        // maybe old windows version
        QMessageBox::warning(this, tr("TrustBridge error"), tr("Failed to open native viewer."));
#else
        QMessageBox::warning(this, tr("TrustBridge error"), tr("Failed to open 'gcr-viewer'.") + "\n" +
                tr("TrustBridge uses 'gcr-viewer' to show certificate details. Please ensure that 'gcr-viewer' is installed."));
#endif
    }
    return;
}

bool CertificateItemWidget::state()
{
    if (!mButton->isEnabled()) {
        return true;
    }

    return !mButton->isChecked();
}

void CertificateItemWidget::setState(bool state)
{
    /* The internal state we get here is inverted for Ui reasons the logic
     * is if a certificate is selected for installation the button
     * is disabled (as this is the default) Only those that are
     * unselected have the enabled button. */
    mButton->blockSignals(true); // code did this and not the user
    mButton->setChecked(!state);
    mButton->blockSignals(false);
    if (mButton->isEnabled()) {
        mButton->setToolTip(mButton->property(!state ? "ToolTip_On" :
                    "ToolTip_Off").toString());
    }
}

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

void CertificateItemWidget::currentStateChanged(bool state)
{
    mButton->setToolTip(mButton->property(state ? "ToolTip_On" :
                "ToolTip_Off").toString());
    emit stateChanged(state, mCertificate);
}

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