-
Notifications
You must be signed in to change notification settings - Fork 70
/
kitmodel.cpp
227 lines (189 loc) · 4.76 KB
/
kitmodel.cpp
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
#include <QDataStream>
#include <QDebug>
#include "core/flash.h"
#include "core/os/os.h"
#include "kitmodel.h"
QDataStream &operator<<(QDataStream &out, const Kit &kit)
{
unsigned int version = 1;
out << version
<< kit.id
<< kit.name
<< kit.boot1
<< kit.flash
<< kit.snapshot
<< kit.type;
return out;
}
QDataStream &operator>>(QDataStream &in, Kit &kit)
{
unsigned int version;
in >> version;
if(version == 1)
{
in >> kit.id
>> kit.name
>> kit.boot1
>> kit.flash
>> kit.snapshot
>> kit.type;
}
else
qWarning() << "Unknown Kit serialization version " << version;
return in;
}
QDataStream &operator<<(QDataStream &out, const KitModel &kits)
{
unsigned int version = 1;
out << version << kits.kits << kits.nextID;
return out;
}
QDataStream &operator>>(QDataStream &in, KitModel &kits)
{
unsigned int version;
in >> version;
if(version == 1)
in >> kits.kits >> kits.nextID;
else
qWarning() << "Unknown KitModel serialization version " << version;
return in;
}
int KitModel::rowCount(const QModelIndex &) const
{
return kits.count();
}
QHash<int, QByteArray> KitModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[IDRole] = "id";
roles[NameRole] = "name";
roles[TypeRole] = "type";
roles[FlashRole] = "flash";
roles[Boot1Role] = "boot1";
roles[SnapshotRole] = "snapshot";
return roles;
}
QVariant KitModel::data(const QModelIndex &index, int role) const
{
return getDataRow(index.row(), role);
}
QVariant KitModel::getDataRow(const int row, int role) const
{
if(row < 0 || row >= kits.count())
return QVariant();
switch(role)
{
case IDRole:
return kits[row].id;
case NameRole:
return kits[row].name;
case TypeRole:
return kits[row].type;
case FlashRole:
return kits[row].flash;
case Boot1Role:
return kits[row].boot1;
case SnapshotRole:
return kits[row].snapshot;
default:
return QVariant();
}
}
bool KitModel::setDataRow(const int row, const QVariant &value, int role)
{
if(row < 0 || row >= kits.count())
return false;
switch(role)
{
case IDRole:
qWarning("ID not assignable");
return false;
break;
case NameRole:
kits[row].name = value.toString();
break;
case TypeRole:
kits[row].type = value.toString();
break;
case FlashRole:
kits[row].flash = value.toString();
break;
case Boot1Role:
kits[row].boot1 = value.toString();
break;
case SnapshotRole:
kits[row].snapshot = value.toString();
break;
default:
return false;
}
if(role == FlashRole)
{
// Refresh type as well
kits[row].type = typeForFlash(value.toString());
emit dataChanged(index(row), index(row), QVector<int>({FlashRole, TypeRole}));
}
else
emit dataChanged(index(row), index(row), QVector<int>({role}));
emit anythingChanged();
return true;
}
bool KitModel::copy(const int row)
{
if(row < 0 || row >= kits.count())
return false;
beginInsertRows({}, row, row);
kits.insert(row, kits[row]);
kits[row].id = nextID++;
endInsertRows();
emit anythingChanged();
return true;
}
bool KitModel::remove(const int row)
{
if(row < 0 || row >= kits.count())
return false;
// Do not remove the last remaining kit
if(kits.count() == 1)
return false;
beginRemoveRows({}, row, row);
kits.removeAt(row);
endRemoveRows();
emit anythingChanged();
return true;
}
void KitModel::addKit(QString name, QString boot1, QString flash, QString snapshot_path)
{
int row = kits.size();
beginInsertRows({}, row, row);
kits.append({nextID++, name, typeForFlash(flash), boot1, flash, snapshot_path});
endInsertRows();
emit anythingChanged();
}
int KitModel::indexForID(const unsigned int id)
{
auto it = std::find_if(kits.begin(), kits.end(),
[&] (const Kit &kit) { return kit.id == id; });
return it == kits.end() ? -1 : (it - kits.begin());
}
bool KitModel::allKitsEmpty() const
{
for(const auto& kit : kits)
if(!kit.boot1.isEmpty() || !kit.flash.isEmpty())
return false;
return true;
}
QString KitModel::typeForFlash(QString flash)
{
QString type = QStringLiteral("???");
QByteArray flash_path = flash.toUtf8();
FILE *file = fopen_utf8(flash_path.data(), "rb");
if(file)
{
std::string s = flash_read_type(file);
if(!s.empty())
type = QString::fromStdString(s);
fclose(file);
}
return type;
}