#include <QtGui>
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <conio.h>
#include "lm-gui.h"
#include <QString>

// if we include <QtGui> there is no need to include every class used: <QString>, <QFileDialog>,...
MainWindow::MainWindow (QWidget * parent)
{
    setupUi (this);  // this sets up GUI

    // signals/slots mechanism in action
    connect (pushButton_Upgrade, SIGNAL (clicked ()), this,
             SLOT (runLicenseManager ()));
    connect (pushButton_Close, SIGNAL (clicked ()), this,
             SLOT (close ()));
}

void
MainWindow::runLicenseManager ()
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    WCHAR args[100] = L"";
    WCHAR cli[100] = L".\\callupgrade.bat ";

    ZeroMemory (&si, sizeof (si));
    si.cb = sizeof (si);
    ZeroMemory (&pi, sizeof (pi));

    lineEdit->text ().toWCharArray (args);
    wcscat (cli, args);

    if (!CreateProcess
            (L".\\callupgrade.bat", cli, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
    {
        QMessageBox::warning(this, tr("Application"),
                             tr("Cannot execute %1\n")
                             .arg("upgrade.bat"));
        getch ();
        return;
    }

    WaitForSingleObject (pi.hProcess, INFINITE);

    // Close process and thread handles.
    CloseHandle (pi.hProcess);
    CloseHandle (pi.hThread);
    getch ();
}
