~zeller-benjamin/qtcreator-plugin-ubuntu/qtc41-beta

« back to all changes in this revision

Viewing changes to ubuntudeviceswidget.cpp

  • Committer: Juhapekka Piiroinen
  • Date: 2013-09-04 15:30:00 UTC
  • mto: (23.1.14 binary-plugin)
  • mto: This revision was merged to the branch mainline in revision 24.
  • Revision ID: juhapekka.piiroinen@canonical.com-20130904153000-r4lhfhrjlwmop277
Added cordova plugin from ubuntu-qtcreator-plugins.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright 2013 Canonical Ltd.
3
 
 *
4
 
 * This program is free software; you can redistribute it and/or modify
5
 
 * it under the terms of the GNU Lesser General Public License as published by
6
 
 * the Free Software Foundation; version 2.1.
7
 
 *
8
 
 * This program is distributed in the hope that it will be useful,
9
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
 * GNU Lesser General Public License for more details.
12
 
 *
13
 
 * You should have received a copy of the GNU Lesser General Public License
14
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
 
 *
16
 
 * Author: Juhapekka Piiroinen <juhapekka.piiroinen@canonical.com>
17
 
 */
18
 
 
19
 
#include "ubuntudeviceswidget.h"
20
 
#include "ui_ubuntudeviceswidget.h"
21
 
 
22
 
#include "ubuntuconstants.h"
23
 
 
24
 
#include <QDebug>
25
 
#include <QDir>
26
 
 
27
 
 
28
 
UbuntuDevicesWidget *UbuntuDevicesWidget::m_instance = 0;
29
 
 
30
 
UbuntuDevicesWidget *UbuntuDevicesWidget::instance()
31
 
{
32
 
    return m_instance;
33
 
}
34
 
 
35
 
UbuntuDevicesWidget::UbuntuDevicesWidget(QWidget *parent) :
36
 
    QWidget(parent),
37
 
    ui(new Ui::UbuntuDevicesWidget)
38
 
{
39
 
    ui->setupUi(this);
40
 
 
41
 
    m_instance = this;
42
 
    m_deviceDetected = false;
43
 
    m_aboutToClose = false;
44
 
    //ui->widgetDeviceInfo->hide();
45
 
    ui->widgetSshProperties->hide();
46
 
    ui->pushButtonSshInstall->hide();
47
 
    ui->pushButtonSshRemove->hide();
48
 
    ui->widgetDeviceSerial->hide();
49
 
    //ui->pushButtonCancel->hide();
50
 
    ui->frameNoDevices->hide();
51
 
    ui->lblLoading->hide();
52
 
    ui->frameNoNetwork->hide();
53
 
 
54
 
    ui->progressBar->setMinimum(0);
55
 
    ui->progressBar->setMaximum(0);
56
 
 
57
 
    connect(&m_ubuntuProcess,SIGNAL(started(QString)),this,SLOT(onStarted(QString)));
58
 
    connect(&m_ubuntuProcess,SIGNAL(message(QString)),this,SLOT(onMessage(QString)));
59
 
    connect(&m_ubuntuProcess,SIGNAL(finished(QString,int)),this,SLOT(onFinished(QString, int)));
60
 
    connect(&m_ubuntuProcess,SIGNAL(error(QString)),this,SLOT(onError(QString)));
61
 
    detectDevices();
62
 
}
63
 
 
64
 
void UbuntuDevicesWidget::onMessage(QString msg) {
65
 
    m_reply.append(msg);
66
 
    ui->plainTextEdit->appendPlainText(msg.trimmed());
67
 
}
68
 
 
69
 
void UbuntuDevicesWidget::onStarted(QString cmd) {
70
 
    ui->stackedWidgetConnectedDevice->setCurrentIndex(1);
71
 
    ui->lblDeviceProcessInfo->setText(QFileInfo(cmd).baseName());
72
 
    //ui->widgetDeviceInfo->hide();
73
 
    //ui->pushButtonCancel->show();
74
 
    ui->lblLoading->show();
75
 
}
76
 
 
77
 
void UbuntuDevicesWidget::onFinished(QString cmd, int code) {
78
 
    ui->stackedWidgetConnectedDevice->setCurrentIndex(0);
79
 
    if (m_aboutToClose) { return; }
80
 
 
81
 
    bool bOk = true;
82
 
    bool bHasNetwork = true;
83
 
 
84
 
    if (cmd == QString::fromLatin1("%0/device_search").arg(Ubuntu::Constants::UBUNTU_SCRIPTPATH)) {
85
 
        QStringList lines = m_reply.trimmed().split(QLatin1String("\n"));
86
 
        lines.takeFirst();
87
 
        foreach(QString line, lines) {
88
 
            QStringList lineData = line.split(QLatin1String("       "));
89
 
            if (lineData.count() == 2) {
90
 
                QString sSerialNumber = lineData.takeFirst();
91
 
                QString sDeviceInfo = lineData.takeFirst();
92
 
                //QString sDeviceName = lineData.takeFirst();
93
 
 
94
 
                //ui->lblDeviceName->setText(sDeviceName.trimmed());
95
 
                ui->comboBoxSerialNumber->addItem(sSerialNumber.trimmed(),sDeviceInfo);
96
 
                //ui->lblSerialnumber->setText(sSerialNumber.trimmed());
97
 
                m_deviceSerialNumber = sSerialNumber.trimmed();
98
 
 
99
 
            } else {
100
 
                //qDebug() << lineData.count() << lineData;
101
 
            }
102
 
        }
103
 
 
104
 
        // if there are no devices, or if there is no permission
105
 
        if (lines.count() == 0 || ui->comboBoxSerialNumber->currentText().startsWith(QLatin1String("???"))) {
106
 
            ui->frameNoDevices->show();
107
 
            //ui->widgetDeviceInfo->hide();
108
 
            ui->widgetDeviceSerial->hide();
109
 
            ui->comboBoxSerialNumber->clear();
110
 
            bOk = false;
111
 
            m_deviceDetected = false;
112
 
 
113
 
            ui->stackedWidgetDeviceConnected->setCurrentIndex(0);
114
 
            endAction(QString::fromLatin1(" * there is no device connected."));
115
 
        }
116
 
        if (lines.count() > 0) {
117
 
            ui->frameNoDevices->hide();
118
 
            ui->widgetDeviceSerial->show();
119
 
 
120
 
            m_deviceDetected = true;
121
 
            ui->stackedWidgetDeviceConnected->setCurrentIndex(1);
122
 
            endAction(QString::fromLatin1(" * found %0 devices.").arg(lines.count()));
123
 
 
124
 
            detectDeviceVersion();
125
 
        }
126
 
        emit updateDeviceActions();
127
 
 
128
 
    } else if (cmd == QString::fromLatin1("%0/device_version").arg(Ubuntu::Constants::UBUNTU_SCRIPTPATH)) {
129
 
        endAction(QString::fromLatin1("..device version detected."));
130
 
        detectHasNetworkConnection();
131
 
    } else if (cmd == QString::fromLatin1("%0/openssh_version").arg(Ubuntu::Constants::UBUNTU_SCRIPTPATH)) {
132
 
        if (m_reply.trimmed() != QLatin1String("(none)") && m_reply.trimmed() != QLatin1String("")) {
133
 
            endAction(QString::fromLatin1("..openssh-server (%0) is installed.").arg(m_reply.trimmed()));
134
 
            ui->widgetSshProperties->show();
135
 
            ui->pushButtonSshInstall->hide();
136
 
            ui->pushButtonSshRemove->show();
137
 
            ui->stackedWidgetDeveloperMode->setCurrentIndex(1);
138
 
            on_pushButtonPortForward_clicked();
139
 
        } else {
140
 
            endAction(QString::fromLatin1("..openssh-server was not installed."));
141
 
            ui->pushButtonSshInstall->show();
142
 
            ui->pushButtonSshRemove->hide();
143
 
            ui->widgetSshProperties->hide();
144
 
            ui->stackedWidgetDeveloperMode->setCurrentIndex(0);
145
 
        }
146
 
    } else if (cmd == QString::fromLatin1("%0/openssh_remove").arg(Ubuntu::Constants::UBUNTU_SCRIPTPATH)) {
147
 
        endAction(QString::fromLatin1("..openssh-server was removed."));
148
 
        detectOpenSsh();
149
 
    } else if (cmd == QString::fromLatin1("%0/openssh_install").arg(Ubuntu::Constants::UBUNTU_SCRIPTPATH)) {
150
 
        endAction(QString::fromLatin1("..openssh-server was installed."));
151
 
        detectOpenSsh();
152
 
    } else if (cmd == QString::fromLatin1("%0/device_portforward").arg(Ubuntu::Constants::UBUNTU_SCRIPTPATH)) {
153
 
        endAction(QString::fromLatin1("..ports forwarded."));
154
 
        on_pushButtonSshSetupPublicKey_clicked();
155
 
    } else if (cmd == QString::fromLatin1("%0/openssh_publickey").arg(Ubuntu::Constants::UBUNTU_SCRIPTPATH)) {
156
 
        endAction(QString::fromLatin1("..public key authentication is now set."));
157
 
    } else if (cmd == QString::fromLatin1("%0/device_network_clone").arg(Ubuntu::Constants::UBUNTU_SCRIPTPATH)) {
158
 
        endAction(QString::fromLatin1("..network configuration copied."));
159
 
        detectHasNetworkConnection();
160
 
    } else if (cmd == QString::fromLatin1("%0/device_time_clone").arg(Ubuntu::Constants::UBUNTU_SCRIPTPATH)) {
161
 
        endAction(QString::fromLatin1("..time configuration copied."));
162
 
    } else if (cmd == QString::fromLatin1("%0/device_hasnetwork").arg(Ubuntu::Constants::UBUNTU_SCRIPTPATH)) {
163
 
        if (m_reply.trimmed() == QString::fromLatin1("1")) {
164
 
            // we have network
165
 
            ui->frameNoNetwork->hide();
166
 
 
167
 
            detectOpenSsh();
168
 
        } else {
169
 
            // not set
170
 
            bHasNetwork = false;
171
 
            ui->frameNoNetwork->show();
172
 
            ui->stackedWidgetDeveloperMode->setCurrentIndex(2);
173
 
        }
174
 
    } else {
175
 
        // left empty
176
 
    }
177
 
 
178
 
    //if (bOk && bHasNetwork) {
179
 
    //    ui->widgetDeviceInfo->show();
180
 
    //}
181
 
   // ui->pushButtonCancel->hide();
182
 
    ui->lblLoading->hide();
183
 
    m_reply.clear();
184
 
}
185
 
 
186
 
void UbuntuDevicesWidget::on_pushButtonReboot_clicked() {
187
 
    beginAction(QString::fromLatin1("Reboot device.."));
188
 
    m_ubuntuProcess.stop();
189
 
    m_ubuntuProcess.append(QStringList() << QString::fromLatin1("%0/device_reboot %1").arg(Ubuntu::Constants::UBUNTU_SCRIPTPATH).arg(serialNumber()) << QApplication::applicationDirPath());
190
 
    m_ubuntuProcess.start(QString::fromLatin1("Reboot device.."));
191
 
}
192
 
 
193
 
void UbuntuDevicesWidget::on_pushButtonShutdown_clicked() {
194
 
    beginAction(QString::fromLatin1("Shutdown device.."));
195
 
    m_ubuntuProcess.stop();
196
 
    m_ubuntuProcess.append(QStringList() << QString::fromLatin1("%0/device_shutdown %1").arg(Ubuntu::Constants::UBUNTU_SCRIPTPATH).arg(serialNumber()) << QApplication::applicationDirPath());
197
 
    m_ubuntuProcess.start(QString::fromLatin1("Shutdown device.."));
198
 
}
199
 
 
200
 
void UbuntuDevicesWidget::on_pushButtonRebootToBootloader_clicked() {
201
 
    beginAction(QString::fromLatin1("Reboot to bootloader.."));
202
 
    m_ubuntuProcess.stop();
203
 
    m_ubuntuProcess.append(QStringList() << QString::fromLatin1("%0/device_reboot2bootloader %1").arg(Ubuntu::Constants::UBUNTU_SCRIPTPATH).arg(serialNumber()) << QApplication::applicationDirPath());
204
 
    m_ubuntuProcess.start(QString::fromLatin1("Reboot to bootloader.."));
205
 
}
206
 
 
207
 
void UbuntuDevicesWidget::on_pushButtonRebootToRecovery_clicked() {
208
 
    beginAction(QString::fromLatin1("Reboot to recovery.."));
209
 
    m_ubuntuProcess.stop();
210
 
    m_ubuntuProcess.append(QStringList() << QString::fromLatin1("%0/device_reboot2recovery %1").arg(Ubuntu::Constants::UBUNTU_SCRIPTPATH).arg(serialNumber()) << QApplication::applicationDirPath());
211
 
    m_ubuntuProcess.start(QString::fromLatin1("Reboot to recovery.."));
212
 
}
213
 
 
214
 
void UbuntuDevicesWidget::detectOpenSsh() {
215
 
    beginAction(QString::fromLatin1("Detecting if openssh-server is installed.."));
216
 
    m_ubuntuProcess.stop();
217
 
    m_ubuntuProcess.append(QStringList() << QString::fromLatin1("%0/openssh_version %1").arg(Ubuntu::Constants::UBUNTU_SCRIPTPATH).arg(serialNumber()) << QApplication::applicationDirPath());
218
 
    m_ubuntuProcess.start(QString::fromLatin1("Detecting openssh-server"));
219
 
}
220
 
 
221
 
void UbuntuDevicesWidget::detectDevices() {
222
 
    beginAction(QString::fromLatin1("Detecting device.."));
223
 
    m_ubuntuProcess.stop();
224
 
    m_ubuntuProcess.append(QStringList() << QString::fromLatin1("%0/device_search").arg(Ubuntu::Constants::UBUNTU_SCRIPTPATH) << QApplication::applicationDirPath());
225
 
    m_ubuntuProcess.start(QString::fromLatin1("Searching Ubuntu Touch device"));
226
 
}
227
 
 
228
 
void UbuntuDevicesWidget::on_pushButtonSshConnect_clicked() {
229
 
    m_ubuntuProcess.stop();
230
 
    m_ubuntuProcess.append(QStringList() << QString::fromLatin1("%0/openssh_connect %1 %2 %3").arg(Ubuntu::Constants::UBUNTU_SCRIPTPATH).arg(serialNumber()).arg(ui->spinBoxSshPort->value()).arg(ui->lineEditUserName->text()) << QApplication::applicationDirPath());
231
 
    m_ubuntuProcess.start(QString::fromLatin1("Opening ssh connection to device"));
232
 
}
233
 
 
234
 
void UbuntuDevicesWidget::on_pushButtonCloneNetworkConfig_clicked() {
235
 
    beginAction(QString::fromLatin1("Clone network configuration from host to device.."));
236
 
    ui->frameNoNetwork->hide();
237
 
    m_ubuntuProcess.stop();
238
 
    m_ubuntuProcess.append(QStringList() << QString::fromLatin1("%0/device_network_clone %1").arg(Ubuntu::Constants::UBUNTU_SCRIPTPATH).arg(serialNumber()) << QApplication::applicationDirPath());
239
 
    m_ubuntuProcess.start(QString::fromLatin1("Clone network configuration from host to device.."));
240
 
}
241
 
 
242
 
void UbuntuDevicesWidget::on_comboBoxSerialNumber_currentIndexChanged( const QString & text ) {
243
 
    m_deviceSerialNumber = text;
244
 
    if (!text.isEmpty()) {
245
 
        ui->lblDeviceInfo->setText(ui->comboBoxSerialNumber->itemData(ui->comboBoxSerialNumber->currentIndex()).toString());
246
 
        // FIXME this call should not be called on init
247
 
        detectDeviceVersion();
248
 
    }
249
 
}
250
 
 
251
 
void UbuntuDevicesWidget::onError(QString msg) {
252
 
    ui->plainTextEdit->appendHtml(QString::fromLatin1("<p style=\"color: red\">%0</p>").arg(msg));
253
 
}
254
 
 
255
 
void UbuntuDevicesWidget::on_pushButtonRefresh_clicked() {
256
 
    m_deviceDetected = false;
257
 
    m_ubuntuProcess.stop();
258
 
 
259
 
    ui->plainTextEdit->clear();
260
 
 
261
 
    m_reply.clear();
262
 
 
263
 
    ui->frameNoDevices->hide();
264
 
 
265
 
    //ui->widgetDeviceInfo->hide();
266
 
    ui->lblLoading->show();
267
 
 //   ui->pushButtonCancel->show();
268
 
 
269
 
    //ui->lblDeviceName->setText(QLatin1String(""));
270
 
    ui->comboBoxSerialNumber->clear();
271
 
   // ui->lblSerialnumber->setText(QLatin1String(""));
272
 
 
273
 
    detectDevices();
274
 
}
275
 
 
276
 
void UbuntuDevicesWidget::beginAction(QString msg) {
277
 
    ui->plainTextEdit->appendHtml(QString::fromLatin1("<p style=\"color: #888\">%0</p>").arg(msg));
278
 
}
279
 
 
280
 
void UbuntuDevicesWidget::endAction(QString msg) {
281
 
    ui->plainTextEdit->appendHtml(QString::fromLatin1("<p style=\"color: #888\">%0</p>").arg(msg));
282
 
}
283
 
 
284
 
void UbuntuDevicesWidget::on_pushButtonPortForward_clicked() {
285
 
    beginAction(QString::fromLatin1("Enabling port forward.."));
286
 
    m_ubuntuProcess.stop();
287
 
    m_ubuntuProcess.append(QStringList() << QString::fromLatin1("%0/device_portforward %1 %2 %3").arg(Ubuntu::Constants::UBUNTU_SCRIPTPATH).arg(serialNumber()).arg(ui->spinBoxSshPort->value()).arg(ui->spinBoxQmlPort->value()) << QApplication::applicationDirPath());
288
 
    m_ubuntuProcess.start(QString::fromLatin1("Enabling port forward.."));
289
 
}
290
 
 
291
 
void UbuntuDevicesWidget::on_pushButtonSshSetupPublicKey_clicked() {
292
 
    beginAction(QString::fromLatin1("Setting up public key authentication.."));
293
 
    m_ubuntuProcess.stop();
294
 
    m_ubuntuProcess.append(QStringList() << QString::fromLatin1("%0/openssh_publickey %1 %2").arg(Ubuntu::Constants::UBUNTU_SCRIPTPATH).arg(serialNumber()).arg(ui->lineEditUserName->text()) << QApplication::applicationDirPath());
295
 
    m_ubuntuProcess.start(QString::fromLatin1("Setting up public key authentication.."));
296
 
}
297
 
 
298
 
void UbuntuDevicesWidget::on_pushButtonUpgradeToDailyImage_clicked() {
299
 
    beginAction(QString::fromLatin1("Upgrade device to daily.."));
300
 
    m_ubuntuProcess.stop();
301
 
    m_ubuntuProcess.append(QStringList() << QString::fromLatin1("%0/device_flashdaily %1").arg(Ubuntu::Constants::UBUNTU_SCRIPTPATH).arg(serialNumber()) << QApplication::applicationDirPath());
302
 
    m_ubuntuProcess.start(QString::fromLatin1("Upgrade device to daily.."));
303
 
}
304
 
 
305
 
void UbuntuDevicesWidget::on_pushButtonUpgradeToDailyImageWithBootstrap_clicked() {
306
 
    beginAction(QString::fromLatin1("Upgrade device to daily with a full bootstrap.."));
307
 
    m_ubuntuProcess.stop();
308
 
    m_ubuntuProcess.append(QStringList() << QString::fromLatin1("%0/device_flashdaily_bootstrap %1").arg(Ubuntu::Constants::UBUNTU_SCRIPTPATH).arg(serialNumber()) << QApplication::applicationDirPath());
309
 
    m_ubuntuProcess.start(QString::fromLatin1("Upgrade device to daily with a full bootstrap.."));
310
 
}
311
 
 
312
 
void UbuntuDevicesWidget::detectHasNetworkConnection() {
313
 
    beginAction(QString::fromLatin1("Check if the device is connected to a network.."));
314
 
    m_ubuntuProcess.stop();
315
 
    m_ubuntuProcess.append(QStringList() << QString::fromLatin1("%0/device_hasnetwork %1").arg(Ubuntu::Constants::UBUNTU_SCRIPTPATH).arg(serialNumber()) << QApplication::applicationDirPath());
316
 
    m_ubuntuProcess.start(QString::fromLatin1("Check if the device is connected to a network"));
317
 
}
318
 
 
319
 
QString UbuntuDevicesWidget::serialNumber() {
320
 
    return ui->comboBoxSerialNumber->currentText();
321
 
}
322
 
 
323
 
void UbuntuDevicesWidget::detectDeviceVersion() {
324
 
    beginAction(QString::fromLatin1("Check device image version.."));
325
 
    m_ubuntuProcess.stop();
326
 
    m_ubuntuProcess.append(QStringList() << QString::fromLatin1("%0/device_version %1").arg(Ubuntu::Constants::UBUNTU_SCRIPTPATH).arg(serialNumber()) << QApplication::applicationDirPath());
327
 
    m_ubuntuProcess.start(QString::fromLatin1("Check device image version.."));
328
 
}
329
 
 
330
 
UbuntuDevicesWidget::~UbuntuDevicesWidget()
331
 
{
332
 
    m_aboutToClose = true;
333
 
    m_ubuntuProcess.stop();
334
 
    delete ui;
335
 
}
336
 
 
337
 
void UbuntuDevicesWidget::on_pushButtonSshInstall_clicked() {
338
 
    beginAction(QString::fromLatin1("Installing openssh-server.."));
339
 
    m_ubuntuProcess.stop();
340
 
    m_ubuntuProcess.append(QStringList() << QString::fromLatin1("%0/openssh_install %1").arg(Ubuntu::Constants::UBUNTU_SCRIPTPATH).arg(serialNumber()) << QApplication::applicationDirPath());
341
 
    m_ubuntuProcess.start(QString::fromLatin1("Installing openssh-server.."));
342
 
}
343
 
 
344
 
void UbuntuDevicesWidget::on_pushButtonCloneTimeConfig_clicked() {
345
 
    beginAction(QString::fromLatin1("Cloning time configuration from host to device.."));
346
 
    m_ubuntuProcess.stop();
347
 
    m_ubuntuProcess.append(QStringList() << QString::fromLatin1("%0/device_time_clone %1").arg(Ubuntu::Constants::UBUNTU_SCRIPTPATH).arg(serialNumber()) << QApplication::applicationDirPath());
348
 
    m_ubuntuProcess.start(QString::fromLatin1("Cloning time configuration from host to device.."));
349
 
}
350
 
 
351
 
void UbuntuDevicesWidget::on_pushButtonSshRemove_clicked() {
352
 
    beginAction(QString::fromLatin1("Removing openssh-server.."));
353
 
    m_ubuntuProcess.stop();
354
 
    m_ubuntuProcess.append(QStringList() << QString::fromLatin1("%0/openssh_remove %1").arg(Ubuntu::Constants::UBUNTU_SCRIPTPATH).arg(serialNumber()) << QApplication::applicationDirPath());
355
 
    m_ubuntuProcess.start(QString::fromLatin1("Removing openssh-server.."));
356
 
}