-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmountpointdialog.cpp
More file actions
83 lines (65 loc) · 2.45 KB
/
mountpointdialog.cpp
File metadata and controls
83 lines (65 loc) · 2.45 KB
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
#include "mountpointdialog.h"
#include <QFile>
#include <QTextStream>
#include <QDebug>
#include <QRegularExpression>
MountPointDialog::MountPointDialog(QWidget *parent)
: QDialog(parent)
{
setWindowTitle(QObject::tr("Enter the mount point:"));
QVBoxLayout *layout = new QVBoxLayout(this);
QLabel *label = new QLabel(QObject::tr("Enter the mount point:"), this);
layout->addWidget(label);
mountPointLineEdit = new QLineEdit(this);
layout->addWidget(mountPointLineEdit);
// 查找默认挂载点并设置到 QLineEdit
QString defaultMountPoint = findDefaultMountPoint();
if (!defaultMountPoint.isEmpty()) {
mountPointLineEdit->setText(defaultMountPoint);
}
QHBoxLayout *buttonLayout = new QHBoxLayout();
confirmButton = new QPushButton(QObject::tr("Confirm"), this);
cancelButton = new QPushButton(QObject::tr("Cancel"), this);
buttonLayout->addWidget(confirmButton);
buttonLayout->addWidget(cancelButton);
layout->addLayout(buttonLayout);
connect(confirmButton, &QPushButton::clicked, this, &QDialog::accept);
connect(cancelButton, &QPushButton::clicked, this, &QDialog::reject);
}
QString MountPointDialog::getMountPoint() const
{
return mountPointLineEdit->text();
}
QString MountPointDialog::findDefaultMountPoint()
{
QString fstabPath = "/mnt/etc/fstab";
QFile file(fstabPath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qWarning() << QObject::tr("Unable to open file: ") << fstabPath;
return QString();
}
QTextStream in(&file);
QStringList searchPaths = {"/boot/EFI", "/boot/efi", "/boot"}; // 按顺序查找的路径
while (!in.atEnd()) {
QString line = in.readLine().trimmed();
if (line.startsWith("#") || line.isEmpty()) {
continue; // 跳过注释和空行
}
// 使用 QRegularExpression 分割行
QRegularExpression regex("\\s+"); // 匹配空白字符
QStringList fields = line.split(regex, Qt::SkipEmptyParts);
if (fields.size() < 2) {
continue;
}
QString mountPoint = fields.at(1); // 挂载点是第二个字段
// 检查是否匹配搜索路径
for (const QString &path : searchPaths) {
if (mountPoint == path) {
file.close();
return path; // 返回第一个匹配的路径
}
}
}
file.close();
return QString(); // 没有找到匹配的路径
}