view ui/administratorwindow.cpp @ 406:5ee694fa3da7

Add license header for NSIS scripts
author Andre Heinecke <aheinecke@intevation.de>
date Wed, 16 Apr 2014 13:31:16 +0000
parents 17e1c8f37d72
children 340b8759d005
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"

AdministratorWindow::AdministratorWindow() {
    QString path = QStandardPaths::locate(
        QStandardPaths::DataLocation, QString("certlist_last.txt"));
    certList.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 *settings = menu->addAction(tr("Settings"));
    menu->addSeparator();
    QAction *help = menu->addAction(tr("Help"));
    QAction *about = menu->addAction(tr("About"));
    menu->addSeparator();
    QAction *quit = menu->addAction(tr("Quit"));
    connect(createInstaller, SIGNAL(triggered()), this, SLOT(createInstaller()));
    connect(settings, SIGNAL(triggered()), this, SLOT(showSettings()));
    connect(help, SIGNAL(triggered()), this, SLOT(showHelp()));
    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("Managed Certificates"));
    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->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("Administrator Application") + "</h2>");
    QLabel *subTitle = new QLabel("This Software creates a signed file containing certificates");
    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(tr("Save"));
    connect(saveButton, SIGNAL(clicked()), this, SLOT(saveCertificateFile()));
    loadButton = new QPushButton(tr("Load"));
    connect(loadButton, SIGNAL(clicked()), this, SLOT(loadCertificateFile()));
    addButton = new QPushButton(tr("Add"));
    connect(addButton, SIGNAL(clicked()), this, SLOT(addCertificates()));
    removeButton = new QPushButton(tr("Remove"));
    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 path = QStandardPaths::locate(
        QStandardPaths::DataLocation, QString("certlist_last.txt"));
    QString certFile = QFileDialog::getOpenFileName(
        this, tr("Select certificate file"), path, "*.txt");
    qDebug() << "selected: " + certFile;
    certList.readList(certFile.toLocal8Bit().constData());
    if (!certList.isValid()) {
        qDebug() << "Not a valid list.";
    }
    else {
        certificateModel->removeAll();
        loadCertificateTable();
    }
}

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

void AdministratorWindow::addCertificates()
{
    QString certFile = QFileDialog::getOpenFileName(
        this, tr("Select certificate"), QDir::homePath(), "*.pem *.der");
    QList<Certificate> certs = Certificate::fromFileName(certFile);
    addToCertificateTable(certs);
}

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, certList.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::showSettings()
{
    qDebug() << "show settingsdialog";
}

void AdministratorWindow::showHelp()
{
    qDebug() << "show helpdialog";
}

void AdministratorWindow::showAbout()
{
    qDebug() << "show aboutdialog";
}

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/