aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qnx/qnxdeployqtlibrariesdialog.cpp
blob: 9a893c912a572a6198f05b0771e02e4ec719e42b (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Copyright (C) 2016 BlackBerry Limited. All rights reserved.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "qnxdeployqtlibrariesdialog.h"

#include "qnxconstants.h"
#include "qnxqtversion.h"
#include "qnxtr.h"

#include <projectexplorer/deployablefile.h>
#include <projectexplorer/devicesupport/idevice.h>
#include <qtsupport/qtversionmanager.h>
#include <remotelinux/genericdirectuploadservice.h>

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

#include <QComboBox>
#include <QDialog>
#include <QDir>
#include <QFormLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QMessageBox>
#include <QPlainTextEdit>
#include <QProgressBar>
#include <QPushButton>
#include <QSpacerItem>
#include <QVBoxLayout>
#include <QVariant>

using namespace ProjectExplorer;
using namespace QtSupport;
using namespace RemoteLinux;
using namespace Utils;

namespace Qnx::Internal {

QnxDeployQtLibrariesDialog::QnxDeployQtLibrariesDialog(const IDevice::ConstPtr &device,
                                                       QWidget *parent)
    : QDialog(parent)
    , m_device(device)
{
    setWindowTitle(Tr::tr("Deploy Qt to QNX Device"));

    m_qtLibraryCombo = new QComboBox(this);
    const QList<QtVersion*> qtVersions = QtVersionManager::sortVersions(
                QtVersionManager::versions(QtVersion::isValidPredicate(
                equal(&QtVersion::type, QString::fromLatin1(Constants::QNX_QNX_QT)))));
    for (QtVersion *v : qtVersions)
        m_qtLibraryCombo->addItem(v->displayName(), v->uniqueId());

    m_deployButton = new QPushButton(Tr::tr("Deploy"), this);
    m_deployButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    m_basePathLabel = new QLabel(this);

    m_remoteDirectory = new QLineEdit(this);
    m_remoteDirectory->setText(QLatin1String("/qt"));

    m_deployProgress = new QProgressBar(this);
    m_deployProgress->setValue(0);
    m_deployProgress->setTextVisible(true);

    m_deployLogWindow = new QPlainTextEdit(this);

    m_closeButton = new QPushButton(Tr::tr("Close"), this);

    m_uploadService = new GenericDirectUploadService(this);
    m_uploadService->setDevice(m_device);

    auto horizontalLayout = new QHBoxLayout();
    horizontalLayout->addWidget(m_qtLibraryCombo);
    horizontalLayout->addWidget(m_deployButton);

    auto horizontalLayout_3 = new QHBoxLayout();
    horizontalLayout_3->setSpacing(0);
    horizontalLayout_3->addWidget(m_basePathLabel);
    horizontalLayout_3->addWidget(m_remoteDirectory);

    auto formLayout = new QFormLayout();
    formLayout->addRow(Tr::tr("Qt library to deploy:"), horizontalLayout);
    formLayout->addRow(Tr::tr("Remote directory:"), horizontalLayout_3);

    auto horizontalLayout_2 = new QHBoxLayout();
    horizontalLayout_2->addItem(new QSpacerItem(218, 20, QSizePolicy::Expanding, QSizePolicy::Minimum));
    horizontalLayout_2->addWidget(m_closeButton);

    auto verticalLayout = new QVBoxLayout(this);
    verticalLayout->addLayout(formLayout);
    verticalLayout->addWidget(m_deployProgress);
    verticalLayout->addWidget(m_deployLogWindow);
    verticalLayout->addLayout(horizontalLayout_2);

    connect(m_uploadService, &AbstractRemoteLinuxDeployService::progressMessage,
            this, &QnxDeployQtLibrariesDialog::updateProgress);
    connect(m_uploadService, &AbstractRemoteLinuxDeployService::progressMessage,
            m_deployLogWindow, &QPlainTextEdit::appendPlainText);
    connect(m_uploadService, &AbstractRemoteLinuxDeployService::errorMessage,
            m_deployLogWindow, &QPlainTextEdit::appendPlainText);
    connect(m_uploadService, &AbstractRemoteLinuxDeployService::warningMessage,
            this, [this](const QString &message) {
        if (!message.contains("stat:"))
            m_deployLogWindow->appendPlainText(message);
    });
    connect(m_uploadService, &AbstractRemoteLinuxDeployService::stdOutData,
            m_deployLogWindow, &QPlainTextEdit::appendPlainText);
    connect(m_uploadService, &AbstractRemoteLinuxDeployService::stdErrData,
            m_deployLogWindow, &QPlainTextEdit::appendPlainText);
    connect(m_uploadService, &AbstractRemoteLinuxDeployService::finished,
            this, &QnxDeployQtLibrariesDialog::handleUploadFinished);

    connect(&m_checkDirProcess, &QtcProcess::done,
            this, &QnxDeployQtLibrariesDialog::handleCheckDirDone);
    connect(&m_removeDirProcess, &QtcProcess::done,
            this, &QnxDeployQtLibrariesDialog::handleRemoveDirDone);

    connect(m_deployButton, &QAbstractButton::clicked,
            this, &QnxDeployQtLibrariesDialog::deployLibraries);
    connect(m_closeButton, &QAbstractButton::clicked,
            this, &QWidget::close);
}

QnxDeployQtLibrariesDialog::~QnxDeployQtLibrariesDialog() = default;

int QnxDeployQtLibrariesDialog::execAndDeploy(int qtVersionId, const QString &remoteDirectory)
{
    m_remoteDirectory->setText(remoteDirectory);
    m_qtLibraryCombo->setCurrentIndex(m_qtLibraryCombo->findData(qtVersionId));

    deployLibraries();
    return exec();
}

void QnxDeployQtLibrariesDialog::closeEvent(QCloseEvent *event)
{
    // A disabled Deploy button indicates the upload is still running
    if (!m_deployButton->isEnabled()) {
        const int answer = QMessageBox::question(this, windowTitle(),
            Tr::tr("Closing the dialog will stop the deployment. Are you sure you want to do this?"),
            QMessageBox::Yes | QMessageBox::No);
        if (answer == QMessageBox::No)
            event->ignore();
        else if (answer == QMessageBox::Yes)
            m_uploadService->stop();
    }
}

void QnxDeployQtLibrariesDialog::deployLibraries()
{
    QTC_ASSERT(m_state == Inactive, return);

    if (m_remoteDirectory->text().isEmpty()) {
        QMessageBox::warning(this, windowTitle(),
                             Tr::tr("Please input a remote directory to deploy to."));
        return;
    }

    QTC_ASSERT(!m_device.isNull(), return);

    m_progressCount = 0;
    m_deployProgress->setValue(0);
    m_remoteDirectory->setEnabled(false);
    m_deployButton->setEnabled(false);
    m_qtLibraryCombo->setEnabled(false);
    m_deployLogWindow->clear();

    startCheckDirProcess();
}

void QnxDeployQtLibrariesDialog::startUpload()
{
    QTC_CHECK(m_state == CheckingRemoteDirectory || m_state == RemovingRemoteDirectory);

    m_state = Uploading;

    QList<DeployableFile> filesToUpload = gatherFiles();

    m_deployProgress->setRange(0, filesToUpload.count());

    m_uploadService->setDeployableFiles(filesToUpload);
    m_uploadService->start();
}

void QnxDeployQtLibrariesDialog::updateProgress(const QString &progressMessage)
{
    QTC_CHECK(m_state == Uploading);

    const int progress = progressMessage.count("sftp> put") + progressMessage.count("sftp> ln -s");
    if (progress != 0) {
        m_progressCount += progress;
        m_deployProgress->setValue(m_progressCount);
    }
}

void QnxDeployQtLibrariesDialog::handleUploadFinished()
{
    m_remoteDirectory->setEnabled(true);
    m_deployButton->setEnabled(true);
    m_qtLibraryCombo->setEnabled(true);

    m_state = Inactive;
}

QList<DeployableFile> QnxDeployQtLibrariesDialog::gatherFiles()
{
    QList<DeployableFile> result;

    const int qtVersionId = m_qtLibraryCombo->itemData(m_qtLibraryCombo->currentIndex()).toInt();

    auto qtVersion = dynamic_cast<const QnxQtVersion *>(QtVersionManager::version(qtVersionId));

    QTC_ASSERT(qtVersion, return result);

    if (HostOsInfo::isWindowsHost()) {
        result.append(gatherFiles(qtVersion->libraryPath().toString(),
                                  QString(),
                                  QStringList() << QLatin1String("*.so.?")));
        result.append(gatherFiles(qtVersion->libraryPath().toString() + QLatin1String("/fonts")));
    } else {
        result.append(gatherFiles(qtVersion->libraryPath().toString()));
    }

    result.append(gatherFiles(qtVersion->pluginPath().toString()));
    result.append(gatherFiles(qtVersion->importsPath().toString()));
    result.append(gatherFiles(qtVersion->qmlPath().toString()));
    return result;
}

QList<DeployableFile> QnxDeployQtLibrariesDialog::gatherFiles(
        const QString &dirPath, const QString &baseDirPath, const QStringList &nameFilters)
{
    QList<DeployableFile> result;
    if (dirPath.isEmpty())
        return result;

    static const QStringList unusedDirs = {"include", "mkspecs", "cmake", "pkgconfig"};
    const QString dp = dirPath.endsWith('/') ? dirPath.left(dirPath.size() - 1) : dirPath;
    if (unusedDirs.contains(dp))
        return result;

    const QDir dir(dirPath);
    QFileInfoList list = dir.entryInfoList(nameFilters,
            QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);

    for (auto &fileInfo : list) {
        if (fileInfo.isDir()) {
            result.append(gatherFiles(fileInfo.absoluteFilePath(), baseDirPath.isEmpty() ?
                                          dirPath : baseDirPath));
        } else {
            static const QStringList unusedSuffixes = {"cmake", "la", "prl", "a", "pc"};
            if (unusedSuffixes.contains(fileInfo.suffix()))
                continue;

            QString remoteDir;
            if (baseDirPath.isEmpty()) {
                remoteDir = fullRemoteDirectory() + QLatin1Char('/') +
                        QFileInfo(dirPath).baseName();
            } else {
                QDir baseDir(baseDirPath);
                baseDir.cdUp();
                remoteDir = fullRemoteDirectory() + QLatin1Char('/') +
                        baseDir.relativeFilePath(dirPath);
            }
            result.append(DeployableFile(FilePath::fromString(fileInfo.absoluteFilePath()),
                                         remoteDir));
        }
    }

    return result;
}

QString QnxDeployQtLibrariesDialog::fullRemoteDirectory() const
{
    return m_remoteDirectory->text();
}

void QnxDeployQtLibrariesDialog::startCheckDirProcess()
{
    QTC_CHECK(m_state == Inactive);
    m_state = CheckingRemoteDirectory;
    m_deployLogWindow->appendPlainText(Tr::tr("Checking existence of \"%1\"")
                                           .arg(fullRemoteDirectory()));
    m_checkDirProcess.setCommand({m_device->filePath("test"),
                                  {"-d", fullRemoteDirectory()}});
    m_checkDirProcess.start();
}

void QnxDeployQtLibrariesDialog::startRemoveDirProcess()
{
    QTC_CHECK(m_state == CheckingRemoteDirectory);
    m_state = RemovingRemoteDirectory;
    m_deployLogWindow->appendPlainText(Tr::tr("Removing \"%1\"").arg(fullRemoteDirectory()));
    m_removeDirProcess.setCommand({m_device->filePath("rm"),
                                  {"-rf", fullRemoteDirectory()}});
    m_removeDirProcess.start();
}

void QnxDeployQtLibrariesDialog::handleCheckDirDone()
{
    QTC_CHECK(m_state == CheckingRemoteDirectory);
    if (handleError(m_checkDirProcess))
        return;

    if (m_checkDirProcess.exitCode() == 0) { // Directory exists
        const int answer = QMessageBox::question(this, windowTitle(),
            Tr::tr("The remote directory \"%1\" already exists.\n"
               "Deploying to that directory will remove any files already present.\n\n"
               "Are you sure you want to continue?").arg(fullRemoteDirectory()),
               QMessageBox::Yes | QMessageBox::No);
        if (answer == QMessageBox::Yes)
            startRemoveDirProcess();
        else
            handleUploadFinished();
    } else {
        startUpload();
    }
}

void QnxDeployQtLibrariesDialog::handleRemoveDirDone()
{
    QTC_CHECK(m_state == RemovingRemoteDirectory);
    if (handleError(m_removeDirProcess))
        return;

    QTC_ASSERT(m_removeDirProcess.exitCode() == 0, return);
    startUpload();
}

// Returns true if the error appeared, false when finished with success
bool QnxDeployQtLibrariesDialog::handleError(const QtcProcess &process)
{
    if (process.result() == ProcessResult::FinishedWithSuccess)
        return false;

    m_deployLogWindow->appendPlainText(Tr::tr("Connection failed: %1").arg(process.errorString()));
    handleUploadFinished();
    return true;
}

} // Qnx::Internal