view ui/listupdatedialog.cpp @ 89:00f9b91f4039

Do not leak a byte if the file is empty
author Andre Heinecke <aheinecke@intevation.de>
date Thu, 20 Mar 2014 18:40:15 +0000
parents e52df5870c4f
children 6090e673c707
line wrap: on
line source
#include "listupdatedialog.h"
#include <QDebug>
#include <QProcess>
#include <QPushButton>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QListWidget>
#include <QVBoxLayout>
#include "certificate.h"

ListUpdateDialog::ListUpdateDialog(QDialog *parent,
                                   const CertificateList &listToInstall) :
    QDialog(parent),
    mCertificateList(listToInstall)
{
    qDebug() << "I am a happy list update dialog";
    setupGUI();
}

void ListUpdateDialog::setupGUI()
{
    /* Top level layout / widgets */
    QVBoxLayout *topLayout = new QVBoxLayout;
    QHBoxLayout *listLayout = new QHBoxLayout;
    QPushButton *executeUpdate = new QPushButton(tr("Update Stores"));
    connect(executeUpdate, &QPushButton::clicked,
            this, &ListUpdateDialog::executeUpdate);

    /* The remove groups */
    QVBoxLayout *removeGroupLayout = new QVBoxLayout;
    mRemoveListWidget = new QListWidget;
    removeGroupLayout->addWidget(mRemoveListWidget);
    QGroupBox *removeGroup = new QGroupBox(tr("Select certificates to be removed"));
    removeGroup->setLayout(removeGroupLayout);

    foreach (const Certificate& cert, mCertificateList.getRemoveCertificates()) {
        if (!cert.isValid()) {
            qWarning() << "Invalid certificate in list";
            continue;
        }
        QListWidgetItem* item = new QListWidgetItem(cert.shortDescription());
        item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
        item->setCheckState(Qt::Checked);
        mRemoveListWidget->addItem(item);
    }

    /* The install group */
    QVBoxLayout *installGroupLayout = new QVBoxLayout;
    mInstallListWidget = new QListWidget;
    QGroupBox *installGroup = new QGroupBox(tr("Select certificates to install"));
    installGroupLayout->addWidget(mInstallListWidget);
    installGroup->setLayout(installGroupLayout);

    foreach (const Certificate& cert, mCertificateList.getInstallCertificates()) {
        if (!cert.isValid()) {
            qWarning() << "Invalid certificate in list";
            continue;
        }
        QListWidgetItem* item = new QListWidgetItem(cert.shortDescription());
        item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
        item->setData(Qt::ToolTipRole, cert.details());
        item->setData(Qt::UserRole, cert.base64Line());
        item->setCheckState(Qt::Checked);
        mInstallListWidget->addItem(item);
    }

    /* Add groups to layout */
    listLayout->addWidget(installGroup);
    listLayout->addWidget(removeGroup);

    /* Fill top level layout */
    topLayout->addLayout(listLayout);
    topLayout->addWidget(executeUpdate);

    setLayout(topLayout);

    return;
}

void ListUpdateDialog::executeUpdate() {
    /* TODO move this in another dialog and call it async*/
    QProcess installerProcess;

    installerProcess.setProgram("cinst");
    installerProcess.start();
    installerProcess.waitForStarted();
    installerProcess.write("-----BEGIN CERTIFICATE LIST-----\r\n");
    installerProcess.write(mCertificateList.rawData().toLatin1());
    installerProcess.write("-----END CERTIFICATE LIST-----\r\n");

    QList<QListWidgetItem *> selectedItems = mInstallListWidget->selectedItems();
    selectedItems << mRemoveListWidget->selectedItems();

    foreach (const QListWidgetItem * item, selectedItems) {
        installerProcess.write(item->data(Qt::UserRole).toString().toLatin1());
        installerProcess.write("\r\n");
    }

    installerProcess.closeWriteChannel();
    installerProcess.waitForFinished();
    qDebug() << "cinst output: " << installerProcess.readAllStandardOutput();
    qDebug() << " Done " << " exitCode: " << installerProcess.exitCode();
    qDebug() << " Done " << " exitStatus: " << installerProcess.exitStatus();
}

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