-
Notifications
You must be signed in to change notification settings - Fork 1
/
hivemodel.cpp
185 lines (151 loc) · 4.36 KB
/
hivemodel.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
#include "hivemodel.h"
#include <QApplication>
#include <QDebug>
#include <QStyle>
#include <hivex.h>
#include <errno.h>
#include "nodeitem.h"
HiveModel::HiveModel(HiveItem *root, QObject *parent)
: QAbstractItemModel(parent), m_root(root)
{
}
HiveModel *HiveModel::loadHive(char *hiveLocation)
{
hive_h *hive = hivex_open(hiveLocation, 0);
if (!hive)
{
qCritical() << "Error code" << errno
<< "while loading hive from location" << hiveLocation;
return nullptr;
}
hive_node_h root = hivex_root(hive);
if (!root)
{
qCritical() << "Error code" << errno
<< "while finding root hive" << hiveLocation;
}
HiveItem *rootItem = static_cast<HiveItem *>(
new NodeItem(nullptr, 0, "<ROOT>", hive, root));
return new HiveModel(rootItem);
}
HiveModel::~HiveModel()
{
if (m_root)
delete m_root;
}
QModelIndex HiveModel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent) || !m_root)
return QModelIndex{};
HiveItem *parentItem;
if (!parent.isValid())
parentItem = m_root;
else
parentItem = static_cast<HiveItem *>(parent.internalPointer());
if (!parentItem)
{
qWarning() << "Cannot index into invalid hive item";
return QModelIndex{};
}
HiveItem *childItem = parentItem->childItem(row);
if (childItem)
return createIndex(row, column, childItem);
else
return QModelIndex{};
}
QModelIndex HiveModel::parent(const QModelIndex &index) const
{
if (!index.isValid() || !m_root)
return QModelIndex{};
HiveItem *childItem = static_cast<HiveItem *>(index.internalPointer());
if (!childItem)
{
qWarning() << "Cannot get parent of invalid hive item";
return QModelIndex{};
}
HiveItem *parentItem = childItem->parentItem();
if (!parentItem || parentItem == m_root)
return QModelIndex{};
return createIndex(parentItem->index(), 0, parentItem);
}
bool HiveModel::hasChildren(const QModelIndex &parent) const
{
if (!m_root)
return false;
HiveItem *parentItem;
if (!parent.isValid())
parentItem = m_root;
else
parentItem = static_cast<HiveItem *>(parent.internalPointer());
if (!parentItem)
{
qWarning() << "Cannot check children of invalid hive item";
return false;
}
bool hasChildren = parentItem->hasChildren();
// qDebug() << "HiveItem" << parentItem->name() << "has children:" << hasChildren;
return hasChildren;
}
int HiveModel::rowCount(const QModelIndex &parent) const
{
HiveItem *parentItem;
if (parent.column() > 0 || !m_root)
return 0;
if (!parent.isValid())
parentItem = m_root;
else
parentItem = static_cast<HiveItem *>(parent.internalPointer());
if (!parentItem)
{
qWarning() << "Cannot get child count of invalid hive item";
return 0;
}
int count = parentItem->childCount();
// qDebug() << "HiveItem" << parentItem->name() << "has" << count << "children";
return count;
}
int HiveModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 1;
}
QVariant HiveModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || !m_root)
return QVariant();
if (role != Qt::DisplayRole && role != Qt::DecorationRole)
return QVariant{};
HiveItem *item = static_cast<HiveItem *>(index.internalPointer());
if (!item)
{
qWarning() << "Could not load data for item";
return QVariant{};
}
switch (role)
{
case Qt::DisplayRole:
return item->name();
case Qt::DecorationRole:
if (item->isNode())
return QApplication::style()->standardIcon(QStyle::SP_DirIcon);
return QVariant{};
}
return QVariant{};
}
Qt::ItemFlags HiveModel::flags(const QModelIndex &index) const
{
if (!index.isValid() || !m_root)
return 0;
return QAbstractItemModel::flags(index);
}
QVariant HiveModel::headerData(int section, Qt::Orientation orientation, int role) const
{
Q_UNUSED(section);
Q_UNUSED(orientation);
Q_UNUSED(role);
if (!m_root)
return QVariant{};
// if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
// return QString{"Name"};
return QVariant{};
}