summaryrefslogtreecommitdiffstats
path: root/src/b2qt-flashing-wizard/platform_page.cpp
blob: 609a5aa00dc187ad6a98ece936e0e01b930ceea0 (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
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc
** All rights reserved.
** For any questions to Digia, please use the contact form at
** http://qt.digia.com/
**
** This file is part of Qt Enterprise Embedded.
**
** Licensees holding valid Qt Enterprise licenses may use this file in
** accordance with the Qt Enterprise License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.
**
** If you have questions regarding the use of this file, please use
** the contact form at http://qt.digia.com/
**
****************************************************************************/

#include "platform_page.h"
#include <QRadioButton>
#include <QLayout>
#include <QDebug>
#include <QDir>
#include <QLabel>
#include <QSpacerItem>
#include <QSettings>
#include "mainwindow.h" // Page_ enum

static QList<PlatformInfo> platforms;
extern QString G_SDKDIR;
extern PlatformInfo G_platforminfo;

QLabel *createErrorLabel(QWidget *parent)
{
    QLabel *label = new QLabel(parent);
    label->setAlignment(Qt::AlignHCenter);
    label->setWordWrap(true);

    QFont f = label->font();
    f.setBold(true);
    label->setFont(f);

    QPalette p = label->palette();
    p.setColor(QPalette::WindowText, Qt::red);
    label->setPalette(p);

    return label;
}

PlatformPage::PlatformPage(QWidget *parent)
    : QWizardPage(parent)
    , mError(createErrorLabel(this))
    , mLayout(new QVBoxLayout(this))
{
    setTitle(tr("Platform"));
    setSubTitle(tr("Select a platform to create a disk for"));
    mLayout->addSpacerItem(new QSpacerItem(40,40,QSizePolicy::Minimum, QSizePolicy::Expanding));
    mLayout->addWidget(mError);
    setLayout(mLayout);
}

PlatformPage::~PlatformPage()
{
}

bool PlatformPage::isComplete() const
{
    return true;
}

void PlatformPage::itemSelected()
{
    mError->clear();
    emit completeChanged();
}

static void loadDeployConfig(const QString &filename, const QString &version)
{
    qDebug() << "Trying to load config" << filename;
    if (!QFile::exists(filename))
        return;

    QFileInfo fi(filename);

    QSettings settings(filename, QSettings::IniFormat);

    foreach (const QString &group, settings.childGroups()) {
        PlatformInfo pi;

        settings.beginGroup(group);
        pi.name = group;
        pi.platform = settings.value("platform").toString();
        pi.os = settings.value("os").toString();
        pi.androidversion = settings.value("androidversion").toString();
        pi.board = settings.value("board").toString();
        pi.deployCommand = settings.value("deploycommand").toString();
        pi.deployCommand = QDir::cleanPath(fi.canonicalPath() + "/" + pi.deployCommand);
        pi.deployArguments = settings.value("deployarguments").toStringList();
        pi.asroot = settings.value("asroot").toBool();
        pi.version = version;
        settings.endGroup();

        if (pi.platform.isEmpty() || pi.os.isEmpty() || pi.deployCommand.isEmpty()) {
            qWarning() << "Invalid data";
            continue;
        }

        if (pi.os == "eAndroid" && pi.androidversion.isEmpty()) {
            qWarning() << "Invalid data";
            continue;
        }

        qDebug() << "Adding platform" << group;
        platforms.append(pi);
    }
}

void PlatformPage::initializePage()
{
    mError->clear();

    qDeleteAll(mButtons);
    mButtons.clear();

    QDir dir(G_SDKDIR);
    foreach (const QString i, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
        if (!i.startsWith("Boot2Qt-"))
          continue;

        QDir dir2(dir.absoluteFilePath(i));
        foreach (const QString j, dir2.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
            if (j.startsWith("emulator-"))
                continue;
            QStringList token = j.split('-');
            QString os = token.takeLast();
            QString name = token.join("-");

            loadDeployConfig(dir2.absoluteFilePath(j) + "/images/deploy.conf", i);
        }
    }
    if (platforms.isEmpty()) {
        mError->setText(tr("No suitable platform found in '%1'.\nMake sure you have"
                           "installed at least one hardware platform.").arg(G_SDKDIR));
    }

    for (int i = 0; i < platforms.count(); ++i) {
          const PlatformInfo &pi = platforms[i];
          QRadioButton *button = new QRadioButton;
          if (pi.os == "eAndroid")
              button->setText(pi.name + " | " + pi.os + " " + pi.androidversion + " (" + pi.version + ")");
          else
              button->setText(pi.name + " | " + pi.os + " (" + pi.version + ")");
          mLayout->insertWidget(0, button);
          connect(button, &QRadioButton::toggled, this, &PlatformPage::itemSelected);
          button->setProperty("b2qt-platform-name", i);
          mButtons.append(button);
    }
}

PlatformInfo PlatformPage::buttonData() const
{
    foreach (QRadioButton *button, mButtons) {
        if (button->isChecked()) {
            int id = button->property("b2qt-platform-name").toInt();
            return platforms[id];
        }
    }

    return PlatformInfo();
}

bool PlatformPage::validatePage()
{
    G_platforminfo = buttonData();
    qDebug() << "Selected:" << G_platforminfo.name;
    return true;
}

int PlatformPage::nextId() const
{
    if (G_platforminfo.board.startsWith("nexus7"))
        return MainWindow::Page_Device;
    else
        return MainWindow::Page_Disk;
}