Commit f4b45b6d authored by Ingemar Ceicer's avatar Ingemar Ceicer

Lika filer 2

parent a4000160
......@@ -19,7 +19,7 @@
#include "ui_hash.h"
#include "ui_license.h"
#include "extqlineedit.h"
#include <algorithm>
void Hash::findDuplicates()
{
ui->actionCopyPath->setDisabled(true);
......@@ -220,11 +220,23 @@ void Hash::collectDuplicates(QVector<PathHash> &v_ph)
}
}
for(int i = 0; i < ny.size(); i++) {
// for(int i = 0; i < ny.size(); i++) {
// qDebug() << ny[i].hash << " " << ny[i].path;
// }
qDebug() << ny.count();
// in some func
// populate the vector
auto sortFunc = [](PathHash a, PathHash b) -> bool {
return a.hash < b.hash;
};
std::sort(ny.begin(), ny.end(), sortFunc);
for(int i = 0, j = -1; i < ny.size(); i++, j++) {
if(j >= 0) {
if(ny[j].hash != ny[i].hash)
qDebug() << "***************";
}
qDebug() << ny[i].hash << " " << ny[i].path;
}
qDebug() << ny.count();
}
/*
hashSum
Copyright (C) 2011-2020 Ingemar Ceicer
http://ceicer.org/
programmering1 (at) ceicer (dot) org
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#include "hash.h"
#include "ui_hash.h"
#include "ui_license.h"
#include "extqlineedit.h"
void Hash::findDuplicates()
{
ui->actionCopyPath->setDisabled(true);
ui->actionSaveToFile->setDisabled(true);
ui->actionCopyHashSum->setDisabled(true);
hurmangaFoldrar = 0;
QString conf_file_name = QDir::toNativeSeparators("/" APPLICATION_NAME ".conf");
k = new Config(conf_file_name);
QString sokvag;
std::string s = k->getConf("open_path");
std::string allwaysopenhomedirectory = k->getConf("allwaysopenhomedirectory");
std::string dialogruta = k->getConf("usenativedialogs");
QString qs = QString::fromStdString(s);
QFileInfo info(qs);
if(info.isReadable() && allwaysopenhomedirectory == "0")
sokvag = qs + QDir::toNativeSeparators("/*");
else
sokvag = QDir::homePath();
QFileDialog *dialog = new QFileDialog(this, tr("Select a folder to Find identical files"),
sokvag,
tr("All Files (*)"));
if(dialogruta == "0")
dialog->setOption(QFileDialog::DontUseNativeDialog, true);
else if(dialogruta == "1")
dialog->setOption(QFileDialog::DontUseNativeDialog, false);
dialog->setFileMode(QFileDialog::Directory);
dialog->setOption(QFileDialog::ShowDirsOnly);
dialog->setReadOnly(true);
QString directoryName;
if(dialog->exec()) {
ui->progressLabel->clear();
ui->progressLabel->toolTip().clear();
ui->lineEdit->clear();
QDir dir = dialog->directory();
directoryName = dir.path();
k->setConf("open_path", directoryName.toStdString());
delete k;
} else {
delete k;
return;
}
QString tmp = "";
QVector<Hash::PathHash> v_ph;
bool lyckats = rekursivt2(directoryName, v_ph);
if(lyckats == false)
QMessageBox::critical(this, APPLICATION_NAME " " VERSION, tr("Could not check the hash sum. Check your file permissions."));
collectDuplicates(v_ph);
}
bool Hash::rekursivt2(QDir path, QVector<Hash::PathHash> &v_ph)
{
// bool lyckades;
QString conf_file_name = QDir::toNativeSeparators("/" APPLICATION_NAME ".conf");
k = new Config(conf_file_name);
std::string fil = k->getConf("defaultfilename");
QString defaultfn = QString::fromStdString(fil);
std::string algoritm = k->getConf("algorithm");
QString alg = QString::fromStdString(algoritm);
std::string writedatetime = k->getConf("writeDateTimeHashSumFile");
std::string open_path = k->getConf("open_path");
QString qopen_path = QString::fromStdString(open_path);
delete k;
// indent += "\t";
QFileInfoList list = path.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
PathHash ph;
k = new Config(conf_file_name);
std::string s = k->getConf("algorithm");
delete k;
foreach(QFileInfo finfo, list) {
if(finfo.isDir()) {
rekursivt2(QDir(finfo.absoluteFilePath()), v_ph);
hurmangaFoldrar++;
} else {
ph.path = finfo.absoluteFilePath();
ph.hash = checksum2(finfo.absoluteFilePath(), s);
v_ph.append(ph);
hurmangaFiler++;
}
}
QString tmp = "";
QApplication::processEvents();
if(hurmangaFoldrar < 2)
ui->progressLabel->setText(tr("I have been looking into ") + tmp.setNum(hurmangaFoldrar + 1) + tr(" folder for files. I have managed to calculate the hash sum in all folders containing files.\n") + tr("(Algorithm: ") + alg + ") ...");
else
ui->progressLabel->setText(tr("I have been looking into ") + tmp.setNum(hurmangaFoldrar + 1) + tr(" folders for files. I have managed to calculate the hash sum in all folders containing files.\n") + tr("(Algorithm: ") + alg + ") ...");
ui->statusBar->showMessage(tr("Path: ") + QDir::toNativeSeparators(path.path()));
if(QDir::toNativeSeparators(path.path()) == QDir::toNativeSeparators(qopen_path)) {
if(hurmangaFoldrar < 2)
ui->progressLabel->setText(tr("I have been looking into ") + tmp.setNum(hurmangaFoldrar + 1) + tr(" folder for files. I have managed to calculate the hash sum in all folders containing files.\n") + tr("(Algorithm: ") + alg + ")");
else
ui->progressLabel->setText(tr("I have been looking into ") + tmp.setNum(hurmangaFoldrar + 1) + tr(" folders for files. I have managed to calculate the hash sum in all folders containing files.\n") + tr("(Algorithm: ") + alg + ")");
ui->statusBar->showMessage(tr("I have checked all folders and created all the files! Mission accomplished."));
QString s;
s.setNum(hurmangaFiler);
ui->lineEdit->setText(tr("Mission accomplished! I have calculated the hash sum of ") + s + tr(" files."));
hurmangaFiler = 0;
}
return true;
}
QString Hash::checksum2(QString in, std::string s)
{
QByteArray ba;
QFile f(in);
if(f.open(QFile::ReadOnly)) {
ba = f.readAll();
f.close();
} else
return "0";
QByteArray sum;
QString ut;
if(s == "md4")
sum = QCryptographicHash::hash(ba, QCryptographicHash::Md4);
else if(s == "md5")
sum = QCryptographicHash::hash(ba, QCryptographicHash::Md5);
else if(s == "sha1")
sum = QCryptographicHash::hash(ba, QCryptographicHash::Sha1);
else if(s == "sha224")
sum = QCryptographicHash::hash(ba, QCryptographicHash::Sha224);
else if(s == "sha256")
sum = QCryptographicHash::hash(ba, QCryptographicHash::Sha256);
else if(s == "sha384")
sum = QCryptographicHash::hash(ba, QCryptographicHash::Sha384);
else if(s == "sha512")
sum = QCryptographicHash::hash(ba, QCryptographicHash::Sha512);
else if(s == "sha3_224")
sum = QCryptographicHash::hash(ba, QCryptographicHash::Sha3_224);
else if(s == "sha3_256")
sum = QCryptographicHash::hash(ba, QCryptographicHash::Sha3_256);
if(s == "sha3_384")
sum = QCryptographicHash::hash(ba, QCryptographicHash::Sha3_384);
else if(s == "sha3_512")
sum = QCryptographicHash::hash(ba, QCryptographicHash::Sha3_512);
else if(s == "keccak_224")
sum = QCryptographicHash::hash(ba, QCryptographicHash::Keccak_224);
else if(s == "keccak_256")
sum = QCryptographicHash::hash(ba, QCryptographicHash::Keccak_256);
else if(s == "keccak_384")
sum = QCryptographicHash::hash(ba, QCryptographicHash::Keccak_384);
else if(s == "keccak_512")
sum = QCryptographicHash::hash(ba, QCryptographicHash::Keccak_512);
sum = sum.toHex();
ut = (QString) sum;
// qDebug() << ut << " " << in;
return ut;
}
void Hash::collectDuplicates(QVector<PathHash> &v_ph)
{
QStringList path;
QStringList hash;
foreach(Hash::PathHash hp, v_ph) {
hash.append(hp.hash);
path.append(hp.path);
}
qDebug() << v_ph.count();
QStringList tmp = hash;
for(int i = 0; i < hash.size(); i++) {
tmp[i] = "0";
if(!tmp.contains(hash[i])) {
path[i] = "0";
}
tmp = hash;
}
hash = tmp;
QVector<Hash::PathHash> ny;
for(int i = 0; i < path.size(); i++) {
if(path[i] != "0") {
ny.append(v_ph[i]);
}
}
for(int i = 0; i < ny.size(); i++) {
qDebug() << ny[i].hash << " " << ny[i].path;
}
qDebug() << ny.count();
}
......@@ -121,9 +121,11 @@ private:
struct PathHash {
QString path;
QString hash;
};
void collectDuplicates(QVector<PathHash> &v_ph);
bool rekursivt2(QDir, QVector<Hash::PathHash> &v_ph);
static bool compareAlphabet(const Hash::PathHash & a, const Hash::PathHash & b);
private slots:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment