view ui/certificatetablemodel.cpp @ 502:e551de11d8b6

Properly handle the case that the file does not exist. TRUNCATE makes create file fail if the file does not exist but we need TRUNCATE in the case that the file already exists
author Andre Heinecke <aheinecke@intevation.de>
date Mon, 28 Apr 2014 09:18:07 +0000
parents 5200b8e9b2ae
children 320a64d58e62
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 "certificatetablemodel.h"

#include <QFont>

CertificateTabelModel::CertificateTabelModel(QObject *parent)
    : QAbstractTableModel(parent)
{
    header.append("");
    header.append(tr("Subject CN"));
    header.append(tr("Subject O"));
    header.append(tr("Issuer CN"));
    header.append(tr("Issuer O"));
    header.append(tr("Valid from"));
    header.append(tr("Valid to"));
    header.append(tr("SHA1 Fingerprint"));

}

void CertificateTabelModel::addCertificate(const Certificate& certificate,
    bool editable)
{
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    mCertificates.append(certificate);
    mCertificates.last().setEditable(editable);
    endInsertRows();
}

bool CertificateTabelModel::removeRows(int row, int count,
    const QModelIndex &index)
{
    beginRemoveRows(QModelIndex(), row, row + count - 1);
    if ((row + count - 1) > mCertificates.size()) {
        return false;
    }
    for (int i = row + count - 1; i >= row; i--) {
        mCertificates.removeAt(i);
    }
    endRemoveRows();
    return true;
}

bool CertificateTabelModel::removeAll()
{
    return removeRows(0, mCertificates.size(), QModelIndex());
}

QVariant CertificateTabelModel::data(const QModelIndex &index,
        int role) const
{
    if (index.row() > mCertificates.size() || index.row() < 0) {
        return QVariant();
    }

    if (role == Qt::DisplayRole) {
        Certificate cert = mCertificates.at(index.row());
        QVariant ret;
        switch(index.column()) {
        case 0: ret = cert.isInstallCert(); break;
        case 1: ret = cert.subjectCN(); break;
        case 2: ret = cert.subjectO(); break;
        case 3: ret = cert.issuerCN(); break;
        case 4: ret = cert.issuerO(); break;
        case 5: ret = cert.validFrom(); break;
        case 6: ret = cert.validTo(); break;
        case 7: ret = cert.fingerprint(); break;
        default: ;
        }
        return ret;
    }
    if (role == Qt::UserRole) {
        Certificate cert = mCertificates.at(index.row());
        return cert.isEditable();
    }
    if (role == Qt::FontRole && index.column() == 7) {
        QFont block("DejaVu Sans Mono");
        return block;
    }

    return QVariant();
}

bool CertificateTabelModel::setData(const QModelIndex &index,
    const QVariant &value, int role)
{
    if (index.row() > mCertificates.size() || index.row() < 0) {
        return false;
    }

    bool newValue = value.toBool();
    Certificate &cert = mCertificates[index.row()];
    if (!cert.isEditable()) {
        return false;
    }
    cert.setInstallCert(newValue);

    emit dataChanged(index, index);

    return true;
}

QVariant CertificateTabelModel::headerData(int section,
        Qt::Orientation orientation, int role) const
{
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
        return header.at(section);
    }
    return QVariant();
}

int CertificateTabelModel::rowCount(const QModelIndex&) const
{
    return mCertificates.size();
}

int CertificateTabelModel::columnCount(const QModelIndex&) const
{
    return header.size();
}

Qt::ItemFlags CertificateTabelModel::flags(const QModelIndex &index) const
{
    if (!index.isValid ())
        return Qt::ItemIsSelectable;

    if (index.column() == 0) {
        return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
    }

    return QAbstractTableModel::flags(index);
}

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