summaryrefslogtreecommitdiff
path: root/view/sharedcache/ui/dsctriage.h
blob: 39f06ce79ff42884eed987a00f854092e471ebc4 (plain)
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
//
// Created by kat on 8/15/24.
//

#include <sharedcacheapi.h>
#include <binaryninjaapi.h>
#include "uitypes.h"
#include "viewframe.h"
#include "animation.h"
#include "uicontext.h"

#include <QTableView>
#include <QStandardItemModel>
#include <QSortFilterProxyModel>
#include <QHeaderView>
#include "filter.h"

#ifndef BINARYNINJA_DSCTRIAGE_H
#define BINARYNINJA_DSCTRIAGE_H


class DSCCacheBlocksView : public QWidget
{
	Q_OBJECT

	BinaryViewRef m_data;
	Ref<SharedCacheAPI::SharedCache> m_cache;

	uint64_t m_backingCacheCount = 0;
	std::vector<SharedCacheAPI::BackingCache> m_backingCaches;

	std::atomic<BNDSCViewLoadProgress> m_currentProgress;
	std::vector<uint64_t> m_blockSizeRatios;
	std::vector<uint64_t> m_targetBlockSizeForAnimation;
	uint64_t m_averageBlockSizeForAnimationInterp = 0;
	std::vector<uint64_t> m_blockLuminance;
	Animation* m_blockWaveAnimation;
	Animation* m_blockExpandAnimation;
	Animation* m_blockAutoselectAnimation;

	int m_selectedBlock = -1;

	int getBlockIndexAtPosition(const QPoint& clickPosition);

	void blockSelected(int index);

public:
	DSCCacheBlocksView(QWidget* parent, BinaryViewRef data, Ref<SharedCacheAPI::SharedCache> cache);
	virtual ~DSCCacheBlocksView() override;

protected:
	void mousePressEvent(QMouseEvent* event) override;
	void mouseReleaseEvent(QMouseEvent* event) override;
	void mouseDoubleClickEvent(QMouseEvent* event) override;
	void mouseMoveEvent(QMouseEvent* event) override;
	void keyPressEvent(QKeyEvent* event) override;
	void keyReleaseEvent(QKeyEvent* event) override;
	void focusInEvent(QFocusEvent* event) override;
	void focusOutEvent(QFocusEvent* event) override;
	void enterEvent(QEnterEvent* event) override;
	void leaveEvent(QEvent* event) override;
	void paintEvent(QPaintEvent* event) override;
	void resizeEvent(QResizeEvent* event) override;

public:
	QSize sizeHint() const override;
	QSize minimumSizeHint() const override;

signals:
	void loadDone();
	void selectionChanged(const SharedCacheAPI::BackingCache& index, bool automatic);
};


class CollapsibleSection : public QWidget
{
	Q_OBJECT

	QLabel* m_titleLabel;
	QLabel* m_subtitleRightLabel;
	QPushButton* m_collapseButton;

	bool m_collapsed = true;

	Animation* m_onContentAddedAnimation;

	QWidget* m_contentWidgetContainer;
	QWidget* m_contentWidget;

protected:
	QSize sizeHint() const override;

public:
	CollapsibleSection(QWidget* parent);
	void setTitle(const QString& title);
	void setSubtitleRight(const QString& subtitle);

	void setContentWidget(QWidget* contentWidget);

	void setCollapsed(bool collapsed, bool animated = true);
	bool isCollapsed() const { return m_collapsed; }
};


class FilterableTableView : public QTableView, public FilterTarget {
	Q_OBJECT

	bool m_filterByHiding;

public:
	FilterableTableView(QWidget* parent = nullptr, bool filterByHiding = true)
		: QTableView(parent), m_filterByHiding(filterByHiding) {
		viewport()->installEventFilter(this);
	}

	~FilterableTableView() override {}

	void setFilter(const std::string& filter) override {
		if (!m_filterByHiding)
		{
			emit filterTextChanged(QString::fromStdString(filter));
			return;
		}
		QString qFilter = QString::fromStdString(filter);
		for (int row = 0; row < model()->rowCount(); ++row) {
			bool match = false;
			for (int col = 0; col < model()->columnCount(); ++col) {
				QModelIndex index = model()->index(row, col);
				QString data = model()->data(index).toString();
				if (data.contains(qFilter, Qt::CaseInsensitive)) {
					match = true;
					break;
				}
			}
			setRowHidden(row, !match);
		}
	}

	void scrollToFirstItem() override {
		if (model()->rowCount() > 0) {
			scrollTo(model()->index(0, 0));
		}
	}

	void scrollToCurrentItem() override {
		QModelIndex currentIndex = selectionModel()->currentIndex();
		if (currentIndex.isValid()) {
			scrollTo(currentIndex);
		}
	}

	void selectFirstItem() override {
		if (model()->rowCount() > 0) {
			QModelIndex firstIndex = model()->index(0, 0);
			selectionModel()->select(firstIndex, QItemSelectionModel::ClearAndSelect);
		}
	}

	void activateFirstItem() override {
		if (model()->rowCount() > 0) {
			QModelIndex firstIndex = model()->index(0, 0);
			setCurrentIndex(firstIndex);
			emit activated(firstIndex);
		}
	}

	bool eventFilter(QObject* obj, QEvent* event) override {
		if (event->type() == QEvent::KeyPress) {
			QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
			if (keyEvent->key() == Qt::Key_Escape) {
				clearSelection();
				return true;
			}
			if (keyEvent->key() == Qt::Key_Enter || keyEvent->key() == Qt::Key_Return) {
				emit activated(currentIndex());
				return true;
			}
		}
		return QTableView::eventFilter(obj, event);
	}

signals:
	void filterTextChanged(const QString& text);
};

class SymbolTableView;

class SymbolTableModel : public QAbstractTableModel {
	Q_OBJECT

	SymbolTableView* m_parent;
	std::string m_filter;
	std::vector<SharedCacheAPI::DSCSymbol> m_symbols;

public:
	explicit SymbolTableModel(SymbolTableView* parent);

	int rowCount(const QModelIndex& parent = QModelIndex()) const override;
	int columnCount(const QModelIndex& parent = QModelIndex()) const override;
	QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
	QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;

	void updateSymbols();

	void setFilter(std::string text);

	const SharedCacheAPI::DSCSymbol& symbolAt(int row) const;
};


class SymbolTableView : public QTableView, public FilterTarget
{
	Q_OBJECT
	friend class SymbolTableModel;

	std::vector<SharedCacheAPI::DSCSymbol> m_symbols;

	SymbolTableModel* m_model;

public:
	SymbolTableView(QWidget* parent, Ref<SharedCacheAPI::SharedCache> cache);
	virtual ~SymbolTableView() override;

	void scrollToFirstItem() override {
		if (model()->rowCount() > 0) {
			scrollTo(model()->index(0, 0));
		}
	}

	void scrollToCurrentItem() override {
		QModelIndex currentIndex = selectionModel()->currentIndex();
		if (currentIndex.isValid()) {
			scrollTo(currentIndex);
		}
	}

	void selectFirstItem() override {
		if (model()->rowCount() > 0) {
			QModelIndex firstIndex = model()->index(0, 0);
			selectionModel()->select(firstIndex, QItemSelectionModel::ClearAndSelect);
		}
	}

	void activateFirstItem() override {
		if (model()->rowCount() > 0) {
			QModelIndex firstIndex = model()->index(0, 0);
			setCurrentIndex(firstIndex);
			emit activated(firstIndex);
		}
	}

	SharedCacheAPI::DSCSymbol getSymbolAtRow(int row) const
	{
		return m_model->symbolAt(row);
	}

	void setFilter(const std::string& filter) override;
};


class DSCTriageView : public QWidget, public View
{
	BinaryViewRef m_data;
	QVBoxLayout* m_layout;
	Ref<SharedCacheAPI::SharedCache> m_cache;

	SplitTabWidget* m_triageTabs;
	DockableTabCollection* m_triageCollection;

	SplitTabWidget* m_bottomRegionTabs;
	DockableTabCollection* m_bottomRegionCollection;

	std::vector<SharedCacheAPI::SharedCacheMachOHeader> m_headers;

public:
	DSCTriageView(QWidget* parent, BinaryViewRef data);
	BinaryViewRef getData() override;
	void setSelectionOffsets(BNAddressRange range) override {};
	QFont getFont() override;
	bool navigate(uint64_t offset) override;
	uint64_t getCurrentOffset() override;
};


class DSCTriageViewType : public ViewType
{
public:
	DSCTriageViewType();
	int getPriority(BinaryViewRef data, const QString& filename) override;
	QWidget* create(BinaryViewRef data, ViewFrame* viewFrame) override;
	static void Register();
};


#endif	// BINARYNINJA_DSCTRIAGE_H