view ui/listupdatedialog.cpp @ 164:6d64d7e9fa32

Implemented port_dirname for windows.
author Sascha Wilde <wilde@intevation.de>
date Tue, 25 Mar 2014 12:01:26 +0100
parents 252ffe6e27fd
children 3ebebd055d3a
line wrap: on
line source
#include "listupdatedialog.h"
#include <QCoreApplication>
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QProcess>
#include <QPushButton>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QListWidget>
#include <QVBoxLayout>
#include "certificate.h"

ListUpdateDialog::ListUpdateDialog(QMainWindow *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->setData(Qt::ToolTipRole, cert.details());
        item->setData(Qt::UserRole, cert.base64Line());
        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;
    QFileInfo fi(QCoreApplication::applicationFilePath());
    QDir myDir = fi.absoluteDir();
    QString instProcName = "cinst";
    if (!fi.suffix().isEmpty()) {
        instProcName += "." + fi.suffix();
    }
    QString instPath = myDir.absoluteFilePath(instProcName);

    installerProcess.setProgram(instPath);
    installerProcess.start();
    installerProcess.waitForStarted();
    if (installerProcess.state() == QProcess::NotRunning) {
        qWarning() << "Failed to start installer Process.";
        /* TODO ERROR message for the user */
        return;
    }

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

    /* Items to install */
    for (int i = 0; i < mInstallListWidget->count(); i++) {
        QListWidgetItem *item = mInstallListWidget->item(i);
        if (!item->checkState() == Qt::Checked) {
            continue;
        }
        installerProcess.write(item->data(Qt::UserRole).toString().toLatin1());
        installerProcess.write("\r\n");
    }

    /* Items to remove */
    for (int i = 0; i < mRemoveListWidget->count(); i++) {
        QListWidgetItem *item = mRemoveListWidget->item(i);
        if (!item->checkState() == Qt::Checked) {
            continue;
        }
        installerProcess.write(item->data(Qt::UserRole).toString().toLatin1());
        installerProcess.write("\r\n");
    }

    installerProcess.closeWriteChannel();
    installerProcess.waitForFinished();

    if (installerProcess.exitStatus() == QProcess::CrashExit) {
        /* Woops */
        qWarning() << "Installer process crashed";
    } else if (installerProcess.exitStatus() != QProcess::NormalExit) {
        /* Can not Happen. there are only those two values but maybe
         * qt changed.. */
        qWarning() << "Exit status neither normal nor crash.";
        return;
    }

    if (installerProcess.exitCode() == 0) {
        qDebug() << "Success!";
        qDebug() << "output: " << installerProcess.readAllStandardOutput();
    } else {
        /* TODO handle errors defined by errorcodes.h */
        qDebug() << "Installer Process returned: " << installerProcess.exitCode();
        qDebug() << "output: " << installerProcess.readAllStandardOutput();
        return;
    }
}

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