-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathefibootentrymodel.cpp
More file actions
185 lines (168 loc) · 6.21 KB
/
Copy pathefibootentrymodel.cpp
File metadata and controls
185 lines (168 loc) · 6.21 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
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
/**
* SPDX-FileCopyrightText: 2026
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "efibootentrymodel.h"
#include <QLatin1StringView>
#include <qefi.h>
// Helper function to get icon name based on first device path type
static QString iconForDevicePath(const QByteArray &rawData)
{
// Parse the load option to get device path list
QEFILoadOption loadOption(rawData);
if (!loadOption.isValidated()) {
return QStringLiteral("computer");
}
const QList<QSharedPointer<QEFIDevicePath>> devicePaths = loadOption.devicePathList();
if (devicePaths.isEmpty()) {
return QStringLiteral("computer");
}
const QEFIDevicePath *firstDP = devicePaths.first().data();
const QEFIDevicePathType dpType = firstDP->type();
const quint8 dpSubType = firstDP->subType();
// Map device path types to icon names
switch (dpType) {
case DP_Media:
switch (dpSubType) {
case QEFIDevicePathMediaSubType::MEDIA_HD:
return QStringLiteral("drive-harddisk");
case QEFIDevicePathMediaSubType::MEDIA_CDROM:
return QStringLiteral("drive-optical");
case QEFIDevicePathMediaSubType::MEDIA_File:
return QStringLiteral("drive-partition");
default:
return QStringLiteral("drive-harddisk");
}
case DP_Message:
switch (dpSubType) {
case QEFIDevicePathMessageSubType::MSG_SATA:
case QEFIDevicePathMessageSubType::MSG_NVME:
case QEFIDevicePathMessageSubType::MSG_SCSI:
case QEFIDevicePathMessageSubType::MSG_ISCSI:
case QEFIDevicePathMessageSubType::MSG_SASEX:
case QEFIDevicePathMessageSubType::MSG_UFS:
case QEFIDevicePathMessageSubType::MSG_SD:
case QEFIDevicePathMessageSubType::MSG_EMMC:
return QStringLiteral("drive-harddisk");
case QEFIDevicePathMessageSubType::MSG_ATAPI:
return QStringLiteral("drive-optical");
case QEFIDevicePathMessageSubType::MSG_USB:
case QEFIDevicePathMessageSubType::MSG_USBClass:
return QStringLiteral("drive-removable-media");
case QEFIDevicePathMessageSubType::MSG_MACAddr:
case QEFIDevicePathMessageSubType::MSG_IPv4:
case QEFIDevicePathMessageSubType::MSG_IPv6:
case QEFIDevicePathMessageSubType::MSG_WiFi:
case QEFIDevicePathMessageSubType::MSG_BT:
case QEFIDevicePathMessageSubType::MSG_BTLE:
return QStringLiteral("network-wired");
default:
return QStringLiteral("drive-harddisk");
}
case DP_Hardware:
switch (dpSubType) {
case QEFIDevicePathHardwareSubType::HW_PCI:
return QStringLiteral("cpu");
default:
return QStringLiteral("computer");
}
case DP_ACPI:
return QStringLiteral("preferences-system-startup");
case DP_BIOSBoot:
return QStringLiteral("preferences-system-startup");
default:
return QStringLiteral("computer");
}
}
EfiBootEntryModel::EfiBootEntryModel(QObject *parent)
: QAbstractListModel(parent)
{
}
int EfiBootEntryModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid()) {
return 0;
}
return static_cast<int>(m_entries.size());
}
QVariant EfiBootEntryModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row() < 0 || index.row() >= rowCount()) {
return {};
}
const auto &entry = m_entries.at(static_cast<size_t>(index.row()));
switch (role) {
case EntryIdRole:
return entry.id;
case EntryIdHexRole:
return QStringLiteral("%1").arg(entry.id, 4, 16, QLatin1Char('0')).toUpper();
case NameRole:
return entry.name;
case PathRole:
return entry.path;
case IsDefaultRole:
return entry.isDefault;
case IsVisibleRole:
return entry.isVisible;
case IsBootNextRole:
return entry.isBootNext;
case IsCurrentRole:
return entry.isCurrent;
case IconNameRole: {
// First prioritize OS type detection based on path and name
const QString pathLower = entry.path.toLower();
const QString nameLower = entry.name.toLower();
if (pathLower.contains(QStringLiteral("microsoft")) || nameLower.contains(QStringLiteral("windows"))) {
return QStringLiteral("image-x-ico");
}
if (pathLower.contains(QStringLiteral("linux")) || nameLower.contains(QStringLiteral("linux")) ||
pathLower.contains(QStringLiteral("grub")) || nameLower.contains(QStringLiteral("grub")) ||
pathLower.contains(QStringLiteral("fedora")) || nameLower.contains(QStringLiteral("fedora")) ||
pathLower.contains(QStringLiteral("ubuntu")) || nameLower.contains(QStringLiteral("ubuntu")) ||
pathLower.contains(QStringLiteral("arch")) || nameLower.contains(QStringLiteral("arch"))) {
return QStringLiteral("preferences-system-linux");
}
if (pathLower.contains(QStringLiteral("freebsd")) || nameLower.contains(QStringLiteral("freebsd")) ||
pathLower.contains(QStringLiteral("bsd")) || nameLower.contains(QStringLiteral("bsd"))) {
return QStringLiteral("application-x-desktop");
}
// Fallback: Detect icon from device path type
QString deviceIcon = iconForDevicePath(entry.raw);
if (!deviceIcon.isEmpty()) {
return deviceIcon;
}
return QStringLiteral("computer");
}
default:
return {};
}
}
QHash<int, QByteArray> EfiBootEntryModel::roleNames() const
{
return {
{EntryIdRole, "entryId"},
{EntryIdHexRole, "entryIdHex"},
{NameRole, "name"},
{PathRole, "path"},
{IsDefaultRole, "isDefault"},
{IsVisibleRole, "isVisible"},
{IsBootNextRole, "isBootNext"},
{IsCurrentRole, "isCurrent"},
{IconNameRole, "iconName"},
};
}
void EfiBootEntryModel::setEntries(std::vector<Entry> entries)
{
beginResetModel();
m_entries = std::move(entries);
endResetModel();
}
const EfiBootEntryModel::Entry *EfiBootEntryModel::entryForId(quint16 id) const
{
for (const auto &entry : m_entries) {
if (entry.id == id) {
return &entry;
}
}
return nullptr;
}