aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/ios/simulatoroperationdialog.cpp
blob: 88ce5d48a69e3817ceaee76c64ba0723224e0767 (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
127
128
129
130
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#include "simulatoroperationdialog.h"
#include "ui_simulatoroperationdialog.h"

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

#include <QFutureWatcher>
#include <QLoggingCategory>
#include <QPushButton>

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

namespace Ios {
namespace Internal {

SimulatorOperationDialog::SimulatorOperationDialog(QWidget *parent) :
    // TODO: Maximize buttong only because of QTBUG-41932
    QDialog(parent,Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowMaximizeButtonHint),
    m_ui(new Ui::SimulatorOperationDialog)
{
    m_ui->setupUi(this);

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

SimulatorOperationDialog::~SimulatorOperationDialog()
{
    // Cancel all pending futures.
    foreach (auto watcher, m_futureWatchList) {
        if (!watcher->isFinished())
            watcher->cancel();
    }

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

    delete m_formatter;
    delete m_ui;
}

void SimulatorOperationDialog::addFutures(const QList<QFuture<void> > &futureList)
{
    foreach (auto future, futureList) {
        if (!future.isFinished() || !future.isCanceled()) {
            auto watcher = new QFutureWatcher<void>;
            watcher->setFuture(future);
            connect(watcher, &QFutureWatcher<void>::finished,
                    this, &SimulatorOperationDialog::futureFinished);
            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::futureFinished()
{
    auto watcher = static_cast<QFutureWatcher<void> *>(sender());
    m_futureWatchList.removeAll(watcher);
    watcher->deleteLater();
    updateInputs();
}

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

} // namespace Internal
} // namespace Ios