178 lines
5.8 KiB
C++
178 lines
5.8 KiB
C++
#include "widget.h"
|
|
#include "ui_widget.h"
|
|
#include "extlist.h"
|
|
#include <QSettings>
|
|
Widget::Widget(QWidget *parent)
|
|
: QWidget(parent)
|
|
, ui(new Ui::Widget)
|
|
{
|
|
ui->setupUi(this);
|
|
|
|
setFixedSize(width(), height());
|
|
|
|
QSettings settings;
|
|
QVariant sett = settings.value("extTypes", QVariant(QStringList("report")));
|
|
m_exceptTypes = sett.toStringList();
|
|
|
|
m_path = QDir::currentPath();
|
|
setWindowTitle(m_path);
|
|
|
|
ui->pushButton->setIcon(QIcon(":/res/icons/open-file-48.png"));
|
|
ui->pushButton->setIconSize(QSize(28, 28));
|
|
ui->pushButton->setToolTip("Выбрать папку...");
|
|
|
|
ui->progressBar->setValue(0);
|
|
|
|
ui->pushButton_2->setEnabled(true);
|
|
ui->pushButton_2->setIcon(QIcon(":/res/icons/start-64.png"));
|
|
ui->pushButton_2->setIconSize(QSize(28,28));
|
|
ui->pushButton_2->setToolTip("Исключения");
|
|
|
|
ui->pushButton_3->setEnabled(true);
|
|
ui->pushButton_3->setIcon(QIcon(":/res/icons/save-50.png"));
|
|
ui->pushButton_3->setIconSize(QSize(26, 26));
|
|
ui->pushButton_3->setToolTip("Сохранить");
|
|
|
|
ui->pushButton_4->setEnabled(true);
|
|
ui->pushButton_4->setIcon(QIcon(":/res/icons/logout-52.png"));
|
|
ui->pushButton_4->setToolTip("Выход");
|
|
ui->pushButton_4->setIconSize(QSize(26, 22));
|
|
|
|
ui->pushButton_5->setEnabled(true);
|
|
ui->pushButton_5->setIcon(QIcon(":/res/icons/loop.png"));
|
|
ui->pushButton_5->setIconSize(QSize(24, 23));
|
|
ui->pushButton_5->setToolTip("Обновить");
|
|
connect(ui->pushButton_5, SIGNAL(clicked()), this, SLOT(calcDir()));
|
|
|
|
connect(ui->pushButton_4, SIGNAL(clicked()), this, SLOT(close()));
|
|
connect(ui->comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(calcDir()));
|
|
|
|
// m_exceptTypes.push_back("report");
|
|
|
|
QTimer::singleShot(300, this, &Widget::calcDir);
|
|
}
|
|
|
|
|
|
Widget::~Widget()
|
|
{
|
|
delete ui;
|
|
}
|
|
|
|
void Widget::on_pushButton_clicked()
|
|
{
|
|
QString dirName = QFileDialog::getExistingDirectory(this, "Выбор папки", m_path);
|
|
|
|
if (!dirName.isEmpty()) {
|
|
m_path = dirName;
|
|
setWindowTitle(m_path);
|
|
ui->progressBar->setValue(0);
|
|
QTimer::singleShot(200, this, &Widget::calcDir);
|
|
}
|
|
}
|
|
|
|
void Widget::calcDir() {
|
|
QDir dir(m_path);
|
|
QFileInfoList fileInfoList = dir.entryInfoList(QDir::Files, QDir::Name);
|
|
|
|
const int n = fileInfoList.size();
|
|
|
|
QJsonObject rootObj;
|
|
QJsonObject filesSet;
|
|
|
|
int cnt=0;
|
|
foreach (const QFileInfo& fileInfo, fileInfoList) {
|
|
int curProgress = cnt*100/n;
|
|
++cnt;
|
|
ui->progressBar->setValue(curProgress);
|
|
QApplication::processEvents();
|
|
QJsonObject fileObj, fileRecord;
|
|
QString filePath = fileInfo.filePath();
|
|
QString createDate = fileInfo.birthTime().toString("hh:mm:ss dd-MMM-yyyy");
|
|
QString lastModifyDate = fileInfo.lastModified().toString("hh:mm:ss dd-MMM-yyyy");
|
|
qint64 fileSize = fileInfo.size();
|
|
|
|
QString fileName = fileInfo.fileName();
|
|
bool flagCorrectFile = true;
|
|
foreach (const QString& ext, m_exceptTypes) {
|
|
QString s1 = "." + ext.toLower();
|
|
QString s2 = fileName.right(ext.length()+1).toLower();
|
|
if (s1 == s2) {
|
|
flagCorrectFile = false;
|
|
break;
|
|
}
|
|
}
|
|
if (!flagCorrectFile) continue;
|
|
|
|
QString crcString = "NaN";
|
|
qint64 crcValue = 0;
|
|
if (ui->comboBox->currentIndex()==0) {
|
|
QCryptographicHash hash(QCryptographicHash::Md5);
|
|
QFile file(filePath);
|
|
if (file.open(QIODevice::ReadOnly)) {
|
|
hash.addData(&file);
|
|
file.close();
|
|
crcString = QString(hash.result().toHex());
|
|
}
|
|
}
|
|
if (ui->comboBox->currentIndex()==1) {
|
|
QFile file(filePath);
|
|
if (file.open(QIODevice::ReadOnly)) {
|
|
QByteArray arr = file.readAll();
|
|
file.close();
|
|
crcValue = (qint64)calcCrc32(arr);
|
|
crcString = QString::number(crcValue, 16).toUpper();
|
|
while (crcString.length()<8) crcString.prepend('0');
|
|
}
|
|
}
|
|
// Собираем информацию
|
|
fileObj.insert("Create Date", createDate);
|
|
fileObj.insert("LastModify Date", lastModifyDate);
|
|
fileObj.insert("Size", fileSize);
|
|
fileObj.insert("CRC", crcString );
|
|
rootObj.insert(fileName, fileObj);
|
|
}
|
|
QJsonDocument doc(rootObj);
|
|
QString jsonString = "Количество файлов - " + QString::number(rootObj.count()) + "\n" +
|
|
doc.toJson(QJsonDocument::Indented);
|
|
ui->textEdit->setPlainText(jsonString);
|
|
ui->progressBar->setValue(100);
|
|
m_jDoc.swap(doc);
|
|
}
|
|
quint32 Widget::calcCrc32(QByteArray &arr) {
|
|
quint32 crc32 = 0xffffffff;
|
|
for(auto x: arr) {
|
|
crc32 = (crc32 >> 8) ^ CRC32Table[(crc32 ^ x) & 0xff];
|
|
}
|
|
crc32 ^= 0xffffffff;
|
|
|
|
return crc32;
|
|
}
|
|
|
|
void Widget::on_pushButton_3_clicked()
|
|
{
|
|
QString name = m_path+ "/" +QDateTime::currentDateTime().toString("yyyy-MM-dd_hhmmss")+".report";
|
|
QFile file(name);
|
|
if (!file.open(QIODevice::WriteOnly)) {
|
|
QMessageBox::critical(this, tr("Сохранение отчета"), tr("Ошибка создания файла"));
|
|
return;
|
|
}
|
|
QString data = m_jDoc.toJson(QJsonDocument::Indented);
|
|
file.write(data.toLocal8Bit());
|
|
file.close();
|
|
QMessageBox::information(this, tr("Сохранение отчета"), tr("Файл успешно сохранен\n")+name);
|
|
}
|
|
|
|
void Widget::on_pushButton_2_clicked()
|
|
{
|
|
extList dialog(&m_exceptTypes, this);
|
|
auto ret = dialog.exec();
|
|
if (ret != QDialog::Accepted) return;
|
|
|
|
QSettings sett;
|
|
const QVariant val(m_exceptTypes);
|
|
sett.setValue("extTypes", val);
|
|
QTimer::singleShot(300, this, SLOT(calcDir()));
|
|
|
|
}
|
|
|