-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckfilehashwidget.cpp
More file actions
executable file
·80 lines (75 loc) · 2.42 KB
/
Copy pathcheckfilehashwidget.cpp
File metadata and controls
executable file
·80 lines (75 loc) · 2.42 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
#include "checkfilehashwidget.h"
#include "algorithminterface.h"
#include "enumTypes.h"
#include <QBoxLayout>
#include <QFileInfo>
#include "md5.h"
#include "sha1.h"
#include "crc32.h"
#include <QMessageBox>
#include <QFileDialog>
CheckFileHashWidget::CheckFileHashWidget(int Type, QFileInfoList list, QWidget *parent) :
QWidget(parent), algorithmType(Type)
{
QLabel *labelHash = new QLabel("File's hash:");
QLabel *labelInput = new QLabel("Input hash for check:");
labelIcon = new QLabel();
compareWithFile = new QPushButton("Compare file");
hashLine = new QLineEdit();
inputLine = new QLineEdit();
hashLine->setReadOnly(true);
QVBoxLayout *vbox = new QVBoxLayout();
QHBoxLayout *hbox = new QHBoxLayout();
hbox->addWidget(inputLine);
hbox->addWidget(labelIcon);
connect(this, SIGNAL(statusChanged(QString)), this->parent(), SLOT(changeStatus(QString)));
hashLine->setText(getHash(list.at(0).absoluteFilePath()));
vbox->addWidget(labelHash);
vbox->addWidget(hashLine);
vbox->addWidget(labelInput);
vbox->addLayout(hbox);
vbox->addWidget(compareWithFile);
setLayout(vbox);
connect(inputLine, SIGNAL(textChanged(QString)), SLOT(checkLines(QString)));
connect(compareWithFile, SIGNAL(clicked()), SLOT(compareFileAndLine()));
}
void CheckFileHashWidget::checkLines(QString string)
{
if(!string.compare(hashLine->text(), Qt::CaseInsensitive))
icon.load(":/icon_ok.png");
else
icon.load(":/icon_bad.png");
labelIcon->resize(icon.size());
labelIcon->setPixmap(icon);
}
void CheckFileHashWidget::compareFileAndLine()
{
QString path = QFileDialog::getOpenFileUrl(this, "Open file", QDir::currentPath(), "All Files (*)").toLocalFile();
inputLine->setText(getHash(path));
}
QString CheckFileHashWidget::getHash(QString path)
{
QString hash;
AlgorithmInterface *algorithm;
switch (algorithmType)
{
case AlgorithmType::crc32:
algorithm = new Crc32();
break;
case AlgorithmType::md5:
algorithm = new MD5();
break;
case AlgorithmType::sha1:
algorithm = new Sha1();
break;
default:
algorithm = new Sha1();
break;
}
if(algorithm->calculateFile(path) != ErrorType::noError)
statusChanged("Error with file");
else
hash = algorithm->getHashString();
delete algorithm;
return hash;
}