summaryrefslogtreecommitdiff
path: root/ui/qt5/partitionmountpage.cc
blob: 4bd8c30c53777d08c08a5dbec26d073f3a9334c3 (plain) (blame)
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
/*
 * partitionmountpage.cc - Implementation of UI.Partition.Install.Mount page
 * horizon-qt5, the Qt 5 user interface for
 * Project Horizon
 *
 * Copyright (c) 2020 Adélie Linux and contributors.  All rights reserved.
 * This code is licensed under the AGPL 3.0 license, as noted in the
 * LICENSE-code file in the root directory of this repository.
 *
 * SPDX-License-Identifier: AGPL-3.0-only
 */

#include "partitionmountpage.hh"
#include "mountdialog.hh"

#include <functional>
#include <QHBoxLayout>
#include <QVBoxLayout>

PartitionMountPage::PartitionMountPage(QWidget *parent)
    : HorizonWizardPage(parent) {
    loadWatermark("disk");
    setTitle(tr("Set Mount Points"));

    mountList = new QListWidget;

    QVBoxLayout *buttonLayout = new QVBoxLayout;
    addMountButton = new QPushButton(tr("&Add Mount..."));
    addMountButton->setIcon(QIcon::fromTheme("list-add"));
    connect(addMountButton, &QPushButton::clicked, [=] {
        QStringList parts, paths;
        for(const auto &mount : mountList->findItems("", Qt::MatchContains)) {
            parts << mount->data(Qt::UserRole + 1).toString();
            paths << mount->data(Qt::UserRole + 2).toString();
        }
        MountDialog md(parts, paths, horizonWizard());
        if(md.exec() == QDialog::Accepted) {
            QListWidgetItem *mount = new QListWidgetItem;
            QString part = md.partition();
            QString path = md.mountPoint();
            mount->setText(tr("%1 on %2").arg(part).arg(path));
            mount->setIcon(QIcon::fromTheme("drive-harddisk"));
            mount->setData(Qt::UserRole + 1, part);
            mount->setData(Qt::UserRole + 2, path);

            mountList->addItem(mount);
        }
    });
    buttonLayout->addWidget(addMountButton);

    delMountButton = new QPushButton(tr("&Remove Mount"));
    delMountButton->setEnabled(false);
    delMountButton->setIcon(QIcon::fromTheme("list-remove"));
    connect(delMountButton, &QPushButton::clicked, [=] {
        delete mountList->takeItem(mountList->currentRow());
    });
    buttonLayout->addWidget(delMountButton);

    /*rescanButton = new QPushButton(tr("Re&scan Devices"));
    rescanButton->setIcon(QIcon::fromTheme("view-refresh"));
    connect(rescanButton, &QPushButton::clicked, [=] {

    });
    buttonLayout->addWidget(rescanButton);*/

    connect(mountList, &QListWidget::currentItemChanged, [=] {
        delMountButton->setEnabled(mountList->currentItem() != nullptr);
    });

    std::function<void()> listRowsChanged { [=] {
        emit completeChanged();
    } };
    connect(mountList->model(), &QAbstractItemModel::rowsInserted, listRowsChanged);
    connect(mountList->model(), &QAbstractItemModel::rowsRemoved, listRowsChanged);

    QHBoxLayout *layout = new QHBoxLayout;
    layout->addStretch();
    layout->addWidget(mountList);
    layout->addLayout(buttonLayout);
    layout->addStretch();

    setLayout(layout);
}

bool PartitionMountPage::isComplete() const {
    return !mountList->findItems(" on /", Qt::MatchEndsWith).isEmpty();
}

QStringList PartitionMountPage::mountLines() const {
    QStringList lines;
    for(const auto &mount : mountList->findItems("", Qt::MatchContains)) {
       lines << QString("mount %1 %2").arg(mount->data(Qt::UserRole + 1).toString())
                                      .arg(mount->data(Qt::UserRole + 2).toString());
    }
    return lines;
}