view ui/administratorwindow.cpp @ 531:dec797c7230c

Fix usage of OU / O
author Andre Heinecke <aheinecke@intevation.de>
date Tue, 29 Apr 2014 15:02:31 +0000
parents c81123298387
children d7ab9e734317
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 "administratorwindow.h"

#include <QDebug>
#include <QMessageBox>
#include <QAction>
#include <QMenu>
#include <QApplication>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGroupBox>
#include <QSplitter>
#include <QLabel>
#include <QImage>
#include <QCheckBox>
#include <QHeaderView>
#include <QFileDialog>
#include <QStandardPaths>
#include <QSortFilterProxyModel>

#include "certificatetabledelegate.h"
#include "createinstallerdialog.h"
#include "createcertlistdialog.h"
#include "aboutdialog.h"

AdministratorWindow::AdministratorWindow() {
    setWindowTitle(tr("TrustBridge Administration"));
    QString path = QStandardPaths::locate(
        QStandardPaths::DataLocation, QString("current_certificates.txt"));
    mCertList.readList(path.toLocal8Bit());
    createActions();
    createMenuBar();
    createContent();
    loadCertificateTable();
    resize(1190, 700);
}

void AdministratorWindow::createActions()
{
}

void AdministratorWindow::createMenuBar()
{
    menuBar = new QMenuBar(this);
    QMenu *menu = new QMenu(tr("Menu"), menuBar);
    menuBar->addMenu(menu);
    QAction *createInstaller = menu->addAction(tr("Create installer ..."));
    QAction *about = menu->addAction(tr("About TrustBridge"));
    menu->addSeparator();
    QAction *quit = menu->addAction(tr("Quit"));
    connect(createInstaller, SIGNAL(triggered()), this, SLOT(createInstaller()));
    connect(about, SIGNAL(triggered()), this, SLOT(showAbout()));
    connect(quit, SIGNAL(triggered()), qApp, SLOT(quit()));
    setMenuBar(menuBar);
}

void AdministratorWindow::createContent()
{
    // Create a central widget containing the main layout.
    QWidget *base = new QWidget;

    // Layouts and Container
    QVBoxLayout *mainLayout = new QVBoxLayout;
    QVBoxLayout *certLayout = new QVBoxLayout;
    QHBoxLayout *headerLayout = new QHBoxLayout;
    QVBoxLayout *headerTextLayout = new QVBoxLayout;
    QHBoxLayout *bottomLayout = new QHBoxLayout;

    // The certificate list
    QGroupBox *certBox = new QGroupBox(
        tr("All managed root certificates of the certificate list: "));
    certificateView = new QTableView;
    certificateModel = new CertificateTabelModel();
    QSortFilterProxyModel *filterModel = new QSortFilterProxyModel(this);
    filterModel->setSourceModel(certificateModel);
    CertificateTableDelegate *delegate = new CertificateTableDelegate(certificateView);
    certificateView->setModel(filterModel);
    certificateView->setItemDelegate(delegate);
    certificateView->horizontalHeader()->setStretchLastSection(true);
    certificateView->resizeColumnsToContents();
    certificateView->setColumnWidth(0, 60);
    certificateView->setSelectionBehavior(QAbstractItemView::SelectRows);
    connect(certificateView, SIGNAL(clicked(const QModelIndex&)), this,
        SLOT(clickedCertificate(const QModelIndex&)));
    certificateView->verticalHeader()->setVisible(false);
    certificateView->setSortingEnabled(true);
    certLayout->addWidget(certificateView);
    certBox->setLayout(certLayout);

    // The header (icon, about text)
    QImage *logoImage = new QImage(":/img/logo.png");
    QLabel *logo = new QLabel;
    logo->setBackgroundRole(QPalette::Base);
    logo->setPixmap(QPixmap::fromImage(*logoImage));
    QLabel *title = new QLabel("<h2>" + tr("TrustBridge Administration") + "</h2>");
    QLabel *subTitle = new QLabel(
        tr("Management application of the BSI certificate installer"));
    headerTextLayout->addWidget(title);
    headerTextLayout->addWidget(subTitle);
    headerLayout->addWidget(logo);
    headerLayout->addLayout(headerTextLayout);
    headerLayout->setStretch(0, 0);
    headerLayout->setStretch(1, 10);

    // The buttons.
    bottomLayout->setAlignment(Qt::AlignBottom);
    saveButton = new QPushButton(QIcon(":/img/document-save.png"), tr("Save list"));
    connect(saveButton, SIGNAL(clicked()), this, SLOT(saveCertificateFile()));
    loadButton = new QPushButton(QIcon(":/img/document-open.png"), tr("Load list"));
    connect(loadButton, SIGNAL(clicked()), this, SLOT(loadCertificateFile()));
    addButton = new QPushButton(QIcon(":/img/document-new.png"), tr("Add certificate"));
    connect(addButton, SIGNAL(clicked()), this, SLOT(addCertificates()));
    removeButton = new QPushButton(QIcon(":/img/document-close.png"), tr("Remove certificate"));
    removeButton->setEnabled(false);
    connect(removeButton, SIGNAL(clicked()), this, SLOT(removeCertificates()));
    bottomLayout->addWidget(saveButton);
    bottomLayout->addWidget(loadButton);
    bottomLayout->addWidget(addButton);
    bottomLayout->addWidget(removeButton);
    bottomLayout->insertStretch(4, 10);

    mainLayout->addLayout(headerLayout);
    mainLayout->addWidget(certBox);
    mainLayout->addLayout(bottomLayout);


    // QMainWindow allready has a layout. All child layouts and widgets are
    // managed in the central widget.
    base->setLayout(mainLayout);
    setCentralWidget(base);
}

void AdministratorWindow::loadCertificateFile()
{
    QString lastCertFile = mSettings.value("LastCertList", QDir::homePath()).toString();
    QString certFile = QFileDialog::getOpenFileName(
        this, tr("Select certificate list file"), lastCertFile, "*.txt");
    mCertList.readList(certFile.toLocal8Bit().constData());
    if (!mCertList.isValid()) {
        QMessageBox::warning(this, tr("Error!"), tr("Failed to load the certificate list."));
    } else {
        certificateModel->removeAll();
        loadCertificateTable();
        mSettings.setValue("LastCertList", certFile);
    }
}

void AdministratorWindow::saveCertificateFile()
{
    CreateCertListDialog *dialog = new CreateCertListDialog(this);
    dialog->show();
}

void AdministratorWindow::addCertificates()
{
    QStringList certFiles = QFileDialog::getOpenFileNames(
        this, tr("Select certificate"), QDir::homePath(), "*.pem *.der *.crt *.cer");
    for (int i = 0; i < certFiles.size(); i++) {
        QString certFile = certFiles.at(i);
        QList<Certificate> certs = Certificate::fromFileName(certFile);
        addToCertificateTable(certs);
    }
    certificateView->resizeColumnsToContents();
    certificateView->setColumnWidth(0, 60);
}

void AdministratorWindow::removeCertificates()
{
    QModelIndexList list = certificateView->selectionModel()->selectedRows();
    for (int i = list.size() -1 ; i >= 0; i--) {
        certificateModel->removeRow(list.at(i).row(), list.at(i));
    }
}

void AdministratorWindow::loadCertificateTable() {
    foreach(const Certificate &cert, mCertList.getCertificates()) {
        certificateModel->addCertificate(cert, true);
    }
    certificateView->resizeColumnsToContents();
    certificateView->setColumnWidth(0, 60);
}

void AdministratorWindow::addToCertificateTable(const QList<Certificate> &certs)
{
    foreach(const Certificate &cert, certs) {
        certificateModel->addCertificate(cert, false);
    }
}

void AdministratorWindow::showAbout()
{
    AboutDialog *dialog = new AboutDialog(this);
    dialog->show();
}

void AdministratorWindow::createInstaller()
{
    qDebug() << "create Installer";
    CreateInstallerDialog *dialog = new CreateInstallerDialog(this);
    dialog->show();
}

void AdministratorWindow::clickedCertificate(const QModelIndex &index)
{
    if (index.data(Qt::UserRole).toBool()) {
        removeButton->setEnabled(false);
    }
    else {
        removeButton->setEnabled(true);
    }
}

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