summaryrefslogtreecommitdiff
path: root/ui/qt5/mountdialog.cc
blob: 111bce659d40dd83290ebfd9c9ae0f01db50801b (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
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
/*
 * mountdialog.cc - Implementation of a dialog for selecting a mount point
 * 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 "mountdialog.hh"

#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QSet>
#include <QVBoxLayout>

MountDialog::MountDialog(QStringList skipParts, QStringList skipMounts,
                         HorizonWizard *wizard, QWidget *parent)
    : QDialog(parent, Qt::WindowTitleHint | Qt::WindowCloseButtonHint) {
    setWindowTitle(tr("Choose Partition and Mount Point"));

    QPushButton *ok = new QPushButton(tr("Confirm"));
    ok->setEnabled(false);
    connect(ok, &QPushButton::clicked, this, &QDialog::accept);
    QPushButton *cancel = new QPushButton(tr("Cancel"));
    connect(cancel, &QPushButton::clicked, this, &QDialog::reject);

    QVBoxLayout *buttonLayout = new QVBoxLayout;
    buttonLayout->setAlignment(Qt::AlignTop);
    buttonLayout->addWidget(ok);
    buttonLayout->addWidget(cancel);

#ifdef HAS_INSTALL_ENV
    QStringList partitions;

    for(const auto &disk : wizard->disks) {
        if(disk.has_fs()) {
            partitions << QString::fromStdString(disk.node());
        } else if(disk.has_label()) {
            for(const auto &part : disk.partitions()) {
                partitions << QString::fromStdString(part.node());
            }
        }
    }

    QSet<QString> parts = partitions.toSet().subtract(skipParts.toSet());
    partitions = parts.toList();
    partitions.sort();

    partList = new QListWidget;
    partList->setWhatsThis(tr("A list of the partitions available on this system."));
    partList->addItems(partitions);
    connect(partList, &QListWidget::currentItemChanged, [=] {
        ok->setEnabled(partList->currentItem() != nullptr);
    });
#else
    partInput = new QLineEdit;
    partInput->setWhatsThis(tr("Input the name of a partition, such as /dev/sda1, here."));
    connect(partInput, &QLineEdit::textChanged, [=] {
        ok->setEnabled(partInput->text().startsWith("/dev/"));
    });
#endif

    QStringList pathCandidates = {"/", "/home", "/opt", "/srv", "/usr",
                                  "/usr/local", "/var", "/var/db", "/var/log"};
    QSet<QString> paths = pathCandidates.toSet().subtract(skipMounts.toSet());
    pathCandidates = paths.toList();
    pathCandidates.sort();

    pathInput = new QComboBox;
    pathInput->setEditable(true);
    pathInput->setWhatsThis(tr("Select where the partition will be mounted."));
    pathInput->addItems(pathCandidates);

    QVBoxLayout *controlLayout = new QVBoxLayout;
    controlLayout->addWidget(new QLabel(tr("Partition")));
#ifdef HAS_INSTALL_ENV
    controlLayout->addWidget(partList);
#else  /* !HAS_INSTALL_ENV */
    controlLayout->addWidget(partInput);
#endif  /* HAS_INSTALL_ENV */
    controlLayout->addWidget(new QLabel(tr("will be mounted on")));
    controlLayout->addWidget(pathInput);

    QHBoxLayout *mainBox = new QHBoxLayout;
    mainBox->addLayout(controlLayout);
    mainBox->addLayout(buttonLayout);

    setLayout(mainBox);
}

QString MountDialog::partition() const {
#ifdef HAS_INSTALL_ENV
    assert(partList->currentItem() != nullptr);
    return partList->currentItem()->text();
#else  /* !HAS_INSTALL_ENV */
    return partInput->text();
#endif  /* HAS_INSTALL_ENV */
}

void MountDialog::setPartition(const QString &part) {
#ifdef HAS_INSTALL_ENV
    QList<QListWidgetItem *> candidate = partList->findItems(part, Qt::MatchExactly);
    if(candidate.empty()) return;
    partList->setCurrentItem(candidate.at(0));
#else  /* !HAS_INSTALL_ENV */
    partInput->setText(part);
#endif  /* HAS_INSTALL_ENV */
}

QString MountDialog::mountPoint() const {
    return pathInput->currentText();
}

void MountDialog::setMountPoint(const QString &path) {
    pathInput->setCurrentText(path);
}