view ui/certificatelistwidget.cpp @ 739:5f0fca113ff4

Factor out Info Panel creation.
author Andre Heinecke <andre.heinecke@intevation.de>
date Fri, 04 Jul 2014 11:15:34 +0200
parents 3d669400104a
children a467204a35f5
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 "certificatelistwidget.h"
#include <QDebug>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGroupBox>
#include <QLabel>

#include "certificateitemwidget.h"

CertificateListWidget::CertificateListWidget(QWidget *parent, Qt::WindowFlags flags) :
    QWidget(parent, flags)
{
    setupGUI();
}

void CertificateListWidget::setupGUI()
{
    QHBoxLayout *mainLayout = new QHBoxLayout;
    QVBoxLayout *detailMainLayout = new QVBoxLayout;
    mCertificateList = new QListWidget;
    mCertificateList->setFixedWidth(250);
    connect(mCertificateList,
        SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)),
        this,
        SLOT(updateDetails(QListWidgetItem*)));

    QHBoxLayout *detailLayout = new QHBoxLayout;
    QVBoxLayout *detailLabelLayout = new QVBoxLayout;
    QVBoxLayout *detailContentLayout = new QVBoxLayout;
    QGroupBox *detailBox = new QGroupBox(tr("Details"));
    QLabel *subjectCN = new QLabel(tr("Subject Common Name:"));
    QLabel *subjectOU = new QLabel(tr("Subject Organisation:"));
    QLabel *validFrom = new QLabel(tr("Valid from:"));
    QLabel *validTo = new QLabel(tr("Valid to:"));
    QLabel *fingerprint = new QLabel(tr("Fingerprint (SHA1):"));
    detailLabelLayout->addWidget(subjectCN);
    detailLabelLayout->addWidget(subjectOU);
    detailLabelLayout->addWidget(validFrom);
    detailLabelLayout->addWidget(validTo);
    detailLabelLayout->addWidget(fingerprint);
    mSubjectCN = new QLabel(tr(""));
    mSubjectO = new QLabel(tr(""));
    mValidFrom = new QLabel(tr(""));
    mValidTo = new QLabel(tr(""));
    mFingerprint = new QLabel(tr(""));
    mFingerprint->setFont(QFont("DejaVu Sans Mono"));
    detailContentLayout->addWidget(mSubjectCN);
    detailContentLayout->addWidget(mSubjectO);
    detailContentLayout->addWidget(mValidFrom);
    detailContentLayout->addWidget(mValidTo);
    detailContentLayout->addWidget(mFingerprint);
    detailLayout->addLayout(detailLabelLayout);
    detailLayout->addLayout(detailContentLayout);
    detailBox->setLayout(detailLayout);
    detailMainLayout->addWidget(detailBox);
    detailMainLayout->addStretch(1);

    mainLayout->addWidget(mCertificateList);
    mainLayout->addLayout(detailMainLayout);
    this->setLayout(mainLayout);
}

void CertificateListWidget::addCertificate(
    const Certificate &certificate,
    bool state,
    bool editable,
    const QString &installLabel,
    const QString &removeLabel)
{
    QListWidgetItem* item = new QListWidgetItem(mCertificateList);
    item->setData(Qt::UserRole,
        QVariant::fromValue(certificate));
    mCertificateList->addItem(item);
    CertificateItemWidget *widget =
        new CertificateItemWidget(mCertificateList, certificate, state, editable,
                installLabel, removeLabel);
    connect(widget, SIGNAL(stateChanged(bool, const Certificate&)),
        this, SLOT(certStateChanged(bool, const Certificate&)));
    item->setSizeHint(widget->minimumSizeHint());
    mCertificateList->setItemWidget(item, widget);
    emit certListChanged(-1);
}

void CertificateListWidget::addCertificates(const QList<Certificate> &list)
{

}

void CertificateListWidget::removeCertificate(const Certificate &cert)
{
    for (int i = 0; i < mCertificateList->count(); i++) {
        QListWidgetItem *item = mCertificateList->item(i);
        CertificateItemWidget *itemWidget =
            static_cast<CertificateItemWidget*>(mCertificateList->itemWidget(item));
        if (itemWidget->certificate() == cert) {
            QListWidgetItem* item = mCertificateList->takeItem(i);
            delete(item);
        }
    }
    emit certListChanged(-1);
}

void CertificateListWidget::clear()
{
    mCertificateList->clear();
}

QStringList CertificateListWidget::certificates()
{
    QStringList list;
    for (int i = 0; i < mCertificateList->count(); i++) {
        QListWidgetItem *item = mCertificateList->item(i);
        CertificateItemWidget *itemWidget =
            static_cast<CertificateItemWidget*>(mCertificateList->itemWidget(item));
        list <<  itemWidget->certificate().base64Line();
    }
    return list;
}

QStringList CertificateListWidget::selectedCertificates() {
    QStringList list;
    for (int i = 0; i < mCertificateList->count(); i++) {
        QListWidgetItem *item = mCertificateList->item(i);
        CertificateItemWidget *itemWidget =
            static_cast<CertificateItemWidget*>(mCertificateList->itemWidget(item));
        if (itemWidget->state()) {
            list << itemWidget->certificate().base64Line();
        }
    }
    return list;
}

QStringList CertificateListWidget::unselectedCertificates() {
    QStringList list;
    for (int i = 0; i < mCertificateList->count(); i++) {
        QListWidgetItem *item = mCertificateList->item(i);
        CertificateItemWidget *itemWidget =
            static_cast<CertificateItemWidget*>(mCertificateList->itemWidget(item));
        if (!itemWidget->state()) {
            list << itemWidget->certificate().base64Line();
        }
    }
    return list;
}

QList<Certificate> CertificateListWidget::certificateList()
{
    QList<Certificate> list;
    for (int i = 0; i < mCertificateList->count(); i++) {
        QListWidgetItem *item = mCertificateList->item(i);
        CertificateItemWidget *itemWidget =
            static_cast<CertificateItemWidget*>(mCertificateList->itemWidget(item));
        list << itemWidget->certificate();
    }

    return list;
}

void CertificateListWidget::setCertState(bool state, const Certificate &cert)
{
    for (int i = 0; i < mCertificateList->count(); i++) {
        QListWidgetItem *item = mCertificateList->item(i);
        CertificateItemWidget *itemWidget =
            static_cast<CertificateItemWidget*>(mCertificateList->itemWidget(item));
        if (itemWidget->certificate() == cert &&
            itemWidget->state() != state) {
            itemWidget->setState(state);
        }
    }
}

void CertificateListWidget::updateDetails(QListWidgetItem *item)
{
    if (item == NULL) {
        return;
    }
    Certificate cert = item->data(Qt::UserRole).value<Certificate>();
    mSubjectCN->setText(cert.subjectCN());
    mSubjectO->setText(cert.subjectO());
    mValidFrom->setText(cert.validFrom().toString());
    mValidTo->setText(cert.validTo().toString());
    mFingerprint->setText(cert.fingerprint());
}

void CertificateListWidget::certStateChanged(bool state, const Certificate &cert)
{
    int selected = 0;
    for (int i = 0; i < mCertificateList->count(); i++) {
        QListWidgetItem *item = mCertificateList->item(i);
        CertificateItemWidget *itemWidget =
            static_cast<CertificateItemWidget*>(mCertificateList->itemWidget(item));
        if (itemWidget->state()) {
            selected++;
        }
    }
    emit certListChanged(selected);
    emit certChanged(state, cert);
}

int CertificateListWidget::selectedCertCount()
{
    int selected = 0;
    for (int i = 0; i < mCertificateList->count(); i++) {
        QListWidgetItem *item = mCertificateList->item(i);
        CertificateItemWidget *itemWidget =
            static_cast<CertificateItemWidget*>(mCertificateList->itemWidget(item));
        if (itemWidget->state()) {
            selected++;
        }
    }
    return selected;
}

bool CertificateListWidget::contains(const Certificate &cert)
{
    for (int i = 0; i < mCertificateList->count(); i++) {
        QListWidgetItem *item = mCertificateList->item(i);
        CertificateItemWidget *itemWidget =
            static_cast<CertificateItemWidget*>(mCertificateList->itemWidget(item));
        if (itemWidget->certificate() == cert) {
            return true;
        }
    }
    return false;
}

void CertificateListWidget::setSelected(int index)
{
    if (index <= 0) {
        index = 0;
    }
    mCertificateList->setFocus();
    mCertificateList->item(index)->setSelected(true);
    mCertificateList->setCurrentRow(index);
}

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