aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/ios/simulatoroperationdialog.cpp
blob: 4b160313c7ce7d08b6523d1fb577257a3358be52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "simulatoroperationdialog.h"

#include <utils/layoutbuilder.h>
#include <utils/outputformatter.h>
#include <utils/qtcassert.h>

#include <QApplication>
#include <QDialogButtonBox>
#include <QFutureWatcher>
#include <QLoggingCategory>
#include <QPlainTextEdit>
#include <QProgressBar>
#include <QPushButton>

namespace {
Q_LOGGING_CATEGORY(iosCommon, "qtc.ios.common", QtWarningMsg)
}

namespace Ios::Internal {

SimulatorOperationDialog::SimulatorOperationDialog(QWidget *parent) :
    // TODO: Maximize buttong only because of QTBUG-41932
    QDialog(parent,Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowMaximizeButtonHint)
{
    resize(580, 320);
    setModal(true);
    setWindowTitle(tr("Simulator Operation Status"));

    auto messageEdit = new QPlainTextEdit;
    messageEdit->setReadOnly(true);

    m_progressBar = new QProgressBar;
    m_progressBar->setMaximum(0);
    m_progressBar->setValue(-1);

    m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);

    m_formatter = new Utils::OutputFormatter;
    m_formatter->setPlainTextEdit(messageEdit);

    using namespace Utils::Layouting;

    Column {
        messageEdit,
        m_progressBar,
        m_buttonBox
    }.attachTo(this);

    connect(m_buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
    connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
}

SimulatorOperationDialog::~SimulatorOperationDialog()
{
    // Cancel all pending futures.
    const auto futureWatchList = m_futureWatchList;
    for (auto watcher : futureWatchList) {
        if (!watcher->isFinished())
            watcher->cancel();
    }

    // wait for futures to finish
    for (auto watcher : futureWatchList) {
        if (!watcher->isFinished())
            watcher->waitForFinished();
        delete watcher;
    }

    delete m_formatter;
}

void SimulatorOperationDialog::addFutures(const QList<QFuture<void> > &futureList)
{
    for (auto future : futureList) {
        if (!future.isFinished() || !future.isCanceled()) {
            auto watcher = new QFutureWatcher<void>;
            connect(watcher, &QFutureWatcherBase::finished, this, [this, watcher] {
                m_futureWatchList.removeAll(watcher);
                watcher->deleteLater();
                updateInputs();
            });
            watcher->setFuture(future);
            m_futureWatchList << watcher;
        }
    }
    updateInputs();
}

void SimulatorOperationDialog::addMessage(const QString &message, Utils::OutputFormat format)
{
    m_formatter->appendMessage(message + "\n\n", format);
}

void SimulatorOperationDialog::addMessage(const SimulatorInfo &siminfo,
                                       const SimulatorControl::ResponseData &response,
                                       const QString &context)
{
    QTC_CHECK(siminfo.identifier == response.simUdid);
    if (response.success) {
        addMessage(tr("%1, %2\nOperation %3 completed successfully.").arg(siminfo.name)
                   .arg(siminfo.runtimeName).arg(context), Utils::StdOutFormat);
    } else {
        QString erroMsg = response.commandOutput.trimmed();
        QString message = tr("%1, %2\nOperation %3 failed.\nUDID: %4\nError: %5").arg(siminfo.name)
                .arg(siminfo.runtimeName).arg(context).arg(siminfo.identifier)
                .arg(erroMsg.isEmpty() ? tr("Unknown") : erroMsg);
        addMessage(message, Utils::StdErrFormat);
        qCDebug(iosCommon) << message;
    }
}

void SimulatorOperationDialog::updateInputs()
{
    bool enableOk = m_futureWatchList.isEmpty();
    m_buttonBox->button(QDialogButtonBox::Cancel)->setEnabled(!enableOk);
    m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(enableOk);
    if (enableOk) {
        addMessage(tr("Done."), Utils::NormalMessageFormat);
        m_progressBar->setMaximum(1); // Stop progress bar.
    }
}

} // Ios::Internal