summaryrefslogtreecommitdiffstats
path: root/src/b2qt-flashing-wizard/disk_page.cpp
blob: c0d1a3a0d9f24fdfdd20546e8a02a3425490c477 (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
/****************************************************************************
**
** 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 "disk_page.h"
#include "common.h"
#include "elevator.h"
#include "mainwindow.h" // for Page_ enum
#include <QRadioButton>
#include <QLayout>
#include <QDebug>
#include <QDir>
#include <QListWidget>
#include <QLabel>
#include <QTimer>
#include <QProcess>
#include <QMessageBox>

extern QString G_device;
QLabel *createErrorLabel(QWidget *parent);

DiskPage::DiskPage(QWidget *parent)
    : QWizardPage(parent)
    , mListWidget(new QListWidget(this))
    , mError(createErrorLabel(this))
    , mLayout(new QVBoxLayout(this))
{
    setTitle(tr("Disk"));
    setSubTitle(tr("Select a disk to be used"));
    mLayout->addWidget(mListWidget);
    mLayout->addSpacerItem(new QSpacerItem(40,40,QSizePolicy::Minimum, QSizePolicy::Expanding));
    mLayout->addWidget(mError);
    setLayout(mLayout);
    connect(mListWidget, &QListWidget::itemClicked, this, &DiskPage::itemSelected);

    QTimer *timer = new QTimer(this);
    timer->setInterval(1000);
    connect(timer, &QTimer::timeout, this, &DiskPage::updateDeviceList);
    timer->start();

    // Remember devices already present at startup
    foreach (const DiskInfo &di, list()) {
        if (!di.removable)
            mHiddenDevices += di.path;
    }
}

DiskPage::~DiskPage()
{
}

QList<DiskPage::DiskInfo> DiskPage::list() const
{
    QList<DiskInfo> diList;

    QDir sys("/sys/block");

    foreach (const QString &i, sys.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
        DiskInfo di;

        if (readAll(sys.absoluteFilePath(i) + "/removable").trimmed() == "1")
            di.removable = true;
        else
            di.removable = false;

        di.logicalBlockSize = readAll(sys.absoluteFilePath(i) + "/queue/logical_block_size").trimmed().toULong();
        di.blocks = readAll(sys.absoluteFilePath(i) + "/size").trimmed().toULong();
        di.name = i;
        di.path = "/dev/" + i;

        checkForDeviceMounted(di.path, di.mountPoint, di.mountPartition);

        diList.append(di);
    }

    return diList;
}

bool DiskPage::isComplete() const
{
    QListWidgetItem *item = mListWidget->currentItem();
    if (!item)
        return false;

    QString path = item->data(Qt::UserRole).toString();
    if (path.isEmpty())
        return false;
    if (!mListItems.contains(path))
        return false;

    return true;
}

void DiskPage::itemSelected()
{
    emit completeChanged();
}

void DiskPage::updateDeviceList()
{
    if (wizard()->currentPage() != this)
        return;

    QList<DiskInfo> diList = list();

    QSet<QString> currentDevices;

    {   // filter devices by name, size etc.
        QMutableListIterator<DiskInfo> iter(diList);

        while (iter.hasNext()) {
            iter.next();
            const DiskInfo &di = iter.value();

            if (di.logicalBlockSize == 0 || di.blocks == 0 || di.name.startsWith("ram")
                || di.name.startsWith("sr") || di.name.startsWith("fd") {
                iter.remove();
                continue;
            }
            currentDevices += di.path;
        }
    }

    { // Update hidden devices list if a device was removed
        QMutableSetIterator<QString> iter(mHiddenDevices);

        while (iter.hasNext()) {
            iter.next();
            if (!currentDevices.contains(iter.value()))
                  iter.remove();
        }
    }
    currentDevices.subtract(mHiddenDevices);

    // Remove devices from map and UI
    QMutableMapIterator<QString, QListWidgetItem*> iter(mListItems);
    while (iter.hasNext()) {
        iter.next();
        if (!currentDevices.contains(iter.key())) {
            mDiskInfo.remove(iter.key());
            delete iter.value();
            iter.remove();
        }
    }

    foreach (const DiskInfo &di, diList) {
        if (!currentDevices.contains(di.path))
              continue;

        QString text = tr("<h3>%1</h3><table style=\"margin-left:10px\"><tr><td>Size</td>"
          "<td style=\"padding-left:10\">%2</td></tr><tr><td>Removable</td>"
          "<td style=\"padding-left:10\">%3</td><tr><td>Mounted</td>"
          "<td style=\"padding-left:10\">%4</td></tr></table>");
        double size = di.logicalBlockSize * di.blocks;
        QString sizeText;

        if (size < 1000) {
            sizeText = QString::number(size) + tr(" B");
        } else if (size < 1000000) {
            sizeText = QString::number(size / 1000) + tr(" KB");
        } else if (size < 1000000000) {
            sizeText = QString::number(size / 1000000) + tr(" MB");
        } else {
            sizeText = QString::number(qRound(size / 1000 / 1000 / 1000)) + tr(" GB");
        }

        text = text.arg(di.name, sizeText, di.removable?tr("yes"):tr("no"), di.mountPoint.isEmpty()?tr("no"):di.mountPoint);

        mDiskInfo[di.path] = di;
        if (mListItems.contains(di.path)) {
            // update existing item
            QListWidgetItem *item = mListItems.value(di.path);
            QLabel *label = qobject_cast<QLabel*>(mListWidget->itemWidget(item));
            label->setText(text);
        } else {
            // new item
            QListWidgetItem *item = new QListWidgetItem();
            item->setData(Qt::UserRole, di.path);
            QLabel *label = new QLabel;
            label->setText(text);
            mListWidget->addItem(item);
            mListWidget->setItemWidget(item, label);
            item->setSizeHint(label->size());
            mListItems.insert(di.path, item);
        }
    }

    if (mListWidget->count() == 0)
        mError->setText(tr("No suitable disk device found. Connect a device."));
    else
        mError->setText("");

    emit completeChanged();
}

void DiskPage::initializePage()
{
    mError->clear();
    updateDeviceList();
}

bool DiskPage::validatePage()
{
    QListWidgetItem *item = mListWidget->currentItem();
    if (!item)
        return false;
    QString path = item->data(Qt::UserRole).toString();
    if (path.isEmpty())
        return false;

    DiskInfo di = mDiskInfo[path];

    if (!di.mountPoint.isEmpty()) {
        // Ask the user before unmount
        if (QMessageBox::Ok != QMessageBox::warning(this, tr("Unmount action"), tr("The disk you selected is mounted at '%1'. It will be unmounted now.").arg(di.mountPoint), QMessageBox::Ok | QMessageBox::Cancel))
            return false;

        QStringList args = elevate();
        args << "umount" << di.mountPoint;

        int rc = QProcess::execute(args.takeFirst(), args);
        if (rc != 0) {
            QMessageBox::critical(this, tr("Unmount failed"), tr("Could not umount the disk."));
            return false;
        }
    }

    G_device = path;
    return true;
}

int DiskPage::nextId() const
{
    return MainWindow::Page_Commit;
}