summaryrefslogtreecommitdiff
path: root/examples/triage/fileinfo.cpp
blob: 3250feae6c922e37b53872415af2d643347bbe16 (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
#include "fileinfo.h"
#include "fontsettings.h"
#include "theme.h"
#include "copyablelabel.h"
#include <QClipboard>
#include <QApplication>
#include <QToolTip>
#include <QPainter>
#include <QtConcurrent/QtConcurrent>
#include <QFuture>
#include <QFutureWatcher>

void FileInfoWidget::addCopyableField(const QString& name, const QVariant& value)
{
	auto& [row, column] = this->m_fieldPosition;

	const auto valueLabel = new CopyableLabel(value.toString(), getThemeColor(AlphanumericHighlightColor));
	valueLabel->setFont(getMonospaceFont(this));

	this->m_layout->addWidget(new QLabel(name), row, column);
	this->m_layout->addWidget(valueLabel, row++, column + 1);
}

void FileInfoWidget::addField(const QString& name, const QVariant& value)
{
	auto& [row, column] = this->m_fieldPosition;

	const auto valueLabel = new QLabel(value.toString());
	valueLabel->setFont(getMonospaceFont(this));

	this->m_layout->addWidget(new QLabel(name), row, column);
	this->m_layout->addWidget(valueLabel, row++, column + 1);
}

void FileInfoWidget::addHashField(
    const QString& hashName, const QCryptographicHash::Algorithm& algorithm, const QByteArray& data)
{
	auto& [row, column] = this->m_fieldPosition;

	const auto hashFieldColor = getThemeColor(AlphanumericHighlightColor);
	auto hashLabel = new CopyableLabel("Calculating...", hashFieldColor);
	hashLabel->setFont(getMonospaceFont(this));

	this->m_layout->addWidget(new QLabel(hashName), row, column);
	this->m_layout->addWidget(hashLabel, row++, column + 1);

	// Process the hash calculations in a separate thread and update the label when done
	QPointer<QFutureWatcher<QByteArray>> watcher = new QFutureWatcher<QByteArray>(this);
	connect(watcher, &QFutureWatcher<QByteArray>::finished, this, [watcher, hashLabel]() {
		if (watcher)
		{
			hashLabel->setText(watcher->result().toHex());
			watcher->deleteLater();
		}
	});
	QFuture<QByteArray> future = QtConcurrent::run([data, algorithm]() {
		return QCryptographicHash::hash(data, algorithm);
	});
	watcher->setFuture(future);
	connect(this, &QObject::destroyed, this, [watcher]() {
		if (watcher && watcher->isRunning()) {
			watcher->cancel();
			watcher->waitForFinished();
		}
	});
}

FileInfoWidget::FileInfoWidget(QWidget* parent, BinaryViewRef bv)
{
	this->m_layout = new QGridLayout();
	this->m_layout->setContentsMargins(0, 0, 0, 0);
	this->m_layout->setVerticalSpacing(1);

	const auto view = bv->GetParentView() ? bv->GetParentView() : bv;
	const auto filePath = bv->GetFile()->GetOriginalFilename();
	this->addCopyableField("Path: ", filePath.c_str());

	const auto fileSize = QString::number(view->GetLength(), 16).prepend("0x");
	this->addCopyableField("Size: ", fileSize);

	const auto bufferSize = fileSize.toUInt(nullptr, 16);
	const auto fileBuffer = std::make_unique<char[]>(bufferSize);
	view->Read(fileBuffer.get(), 0, bufferSize);

	const auto fileBytes = QByteArray(fileBuffer.get(), bufferSize);
	this->addHashField("MD5: ", QCryptographicHash::Md5, fileBytes);
	this->addHashField("SHA-1: ", QCryptographicHash::Sha1, fileBytes);
	this->addHashField("SHA-256: ", QCryptographicHash::Sha256, fileBytes);

	const auto scaledWidth = UIContext::getScaledWindowSize(20, 20).width();
	this->m_layout->setColumnMinimumWidth(FileInfoWidget::m_maxColumns * 3 - 1, scaledWidth);
	this->m_layout->setColumnStretch(FileInfoWidget::m_maxColumns * 3 - 1, 1);
	setLayout(this->m_layout);
}