-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.cpp
More file actions
465 lines (382 loc) · 14.2 KB
/
MainWindow.cpp
File metadata and controls
465 lines (382 loc) · 14.2 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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#include "MainWindow.hpp"
#include "ui_MainWindow.h"
#include "ResultModel.hpp"
#include "DuffVersion.h"
#include <QActionGroup>
#include <QDebug>
#include <QDesktopServices>
#include <QDir>
#include <QFileDialog>
#include <QMessageBox>
#include <QTime>
#include <QTimer>
#include <QTreeWidgetItem>
QString reason(HashCalculator::ErrorType error)
{
switch (error)
{
case HashCalculator::ErrorType::Empty:
return "The file appears empty";
case HashCalculator::ErrorType::Open:
return "The file could not be opened";
case HashCalculator::ErrorType::Read:
return "The file could not be read";
}
return "Uknown reason";
};
QPalette windowTextPalette(const QColor& color)
{
static QPalette palette;
palette.setColor(QPalette::WindowText, color);
return palette;
}
MainWindow::MainWindow(QWidget* parent) :
QMainWindow(parent),
ui(new Ui::MainWindow()),
_hashCalculator(new HashCalculator(this)),
_model(new ResultModel(this))
{
ui->setupUi(this);
ui->treeViewResults->setModel(_model);
connect(ui->treeViewResults, &QTreeWidget::customContextMenuRequested, this, &MainWindow::createFileContextMenu);
connect(ui->lineEditSelectedDirectory, &QLineEdit::textChanged, [this](const QString& text)
{
const QFileInfo info(text);
QPalette palette;
if (info.isDir())
{
emit inputReady();
}
else
{
palette.setColor(text.isEmpty() ? QPalette::Window : QPalette::Text, Qt::red);
emit inputIncomplete();
}
ui->lineEditSelectedDirectory->setPalette(palette);
});
connect(ui->pushButtonDeleteSelected, &QPushButton::clicked, this, &MainWindow::deleteSelected);
connect(ui->pushButtonRefresh, &QPushButton::clicked, this, &MainWindow::onRefresh);
connect(_model, &QAbstractItemModel::dataChanged, this, &MainWindow::onDataChanged);
connect(_model, &QAbstractItemModel::modelReset, this, &MainWindow::updateSelectedLabel);
connect(_model, &QAbstractItemModel::rowsInserted, this, &MainWindow::updateSelectedLabel);
connect(_model, &QAbstractItemModel::rowsRemoved, this, &MainWindow::updateSelectedLabel);
initMenuBar();
initHashCalculator();
initStateMachine();
processCommandLine();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::onOpenDirectoryDialog()
{
QFileDialog dialog(this);
dialog.setFileMode(QFileDialog::Directory);
dialog.setOption(QFileDialog::ShowDirsOnly, true);
if (dialog.exec() == QFileDialog::Accepted)
{
_model->clear();
const QString directory = dialog.selectedFiles().first();
ui->lineEditSelectedDirectory->setText(QDir::toNativeSeparators(directory));
}
}
void MainWindow::onFindDuplicates()
{
const QString selectedDirectory = ui->lineEditSelectedDirectory->text();
if (selectedDirectory.isEmpty())
{
onOpenDirectoryDialog();
onFindDuplicates();
}
if (!QDir(selectedDirectory).exists())
{
QMessageBox::warning(this, "Selected directory", '"' + selectedDirectory + '"' + " does not appear to exist!");
onOpenDirectoryDialog();
return;
}
populateTree(selectedDirectory);
}
void MainWindow::onProcessing(const QString& filePath, qint64 bytesRead, qint64 bytesLeft)
{
const QString message =
QString("%1 Processing: %2 %3/%4")
.arg(QTime::currentTime().toString())
.arg(filePath)
.arg(locale().formattedDataSize(bytesRead))
.arg(locale().formattedDataSize(bytesLeft));
ui->statusBar->setPalette(windowTextPalette(Qt::darkCyan));
ui->statusBar->showMessage(message);
}
void MainWindow::onDuplicateFound(const QString& hashString, const QString& filePath)
{
const QString message =
QString("%1 Found duplicate: %2 -> %3")
.arg(QTime::currentTime().toString())
.arg(filePath)
.arg(hashString);
ui->statusBar->setPalette(windowTextPalette(Qt::darkYellow));
ui->statusBar->showMessage(message);
}
void MainWindow::onFinished()
{
if (_model->rowCount() <= 0)
{
QMessageBox::information(
this,
"No duplicate files",
"No duplicate files were found.\n");
}
ui->menuAlgorithm->setEnabled(true);
ui->treeViewResults->expandAll();
const QString message =
QString("%1 Finished searching: %2")
.arg(QTime::currentTime().toString())
.arg(ui->lineEditSelectedDirectory->text());
ui->statusBar->setPalette(windowTextPalette(Qt::darkGreen));
ui->statusBar->showMessage(message);
}
void MainWindow::onFailure(const QString& filePath, HashCalculator::ErrorType error)
{
const QString message =
QString("%1 Failed to process %2. Reason: %3")
.arg(QTime::currentTime().toString())
.arg(filePath)
.arg(reason(error));
ui->statusBar->setPalette(windowTextPalette(Qt::red));
ui->statusBar->showMessage(message);
qWarning() << filePath << char(error);
}
void MainWindow::onDataChanged(const QModelIndex&, const QModelIndex&, const QVector<int>& roles)
{
if (roles.isEmpty() || roles.contains(Qt::CheckStateRole))
{
updateSelectedLabel();
}
}
void MainWindow::onRefresh()
{
_model->removeInexistentPaths();
ui->treeViewResults->expandAll();
updateSelectedLabel();
}
void MainWindow::deleteSelected()
{
const QStringList filePaths = _model->selectedPaths();
if (filePaths.empty())
{
QMessageBox::warning(this, "Failed to remove file", "Nothing selected!\n");
return;
}
const QString message =
QString("Are you sure you want to delete the selected %1 file(s)?")
.arg(filePaths.size());
if (QMessageBox::question(this, "Confirm delete?", message) !=
QMessageBox::StandardButton::Yes)
{
return;
}
for (const QString& filePath : filePaths)
{
if (!QFile::remove(filePath))
{
if (QFile::exists(filePath))
{
QMessageBox::warning(this, "Failed to remove file", "Failed to remove:\n\n" + filePath + "\n");
continue;
}
QMessageBox::warning(this, "Failed to remove file", filePath + "\n\ndoes not exist anymore!\n");
}
_model->removePath(filePath);
}
ui->treeViewResults->expandAll();
}
void MainWindow::initMenuBar()
{
ui->actionOpen->setIcon(QApplication::style()->standardIcon(QStyle::SP_DirOpenIcon));
connect(ui->actionOpen, &QAction::triggered, this, &MainWindow::onOpenDirectoryDialog);
ui->actionExit->setIcon(QApplication::style()->standardIcon(QStyle::SP_DialogCloseButton));
connect(ui->actionExit, &QAction::triggered, qApp, &QApplication::quit);
ui->actionAbout->setIcon(QApplication::style()->standardIcon(QStyle::SP_MessageBoxInformation));
connect(ui->actionAbout, &QAction::triggered, this, &MainWindow::onAbout);
ui->actionLicenses->setIcon(QApplication::style()->standardIcon(QStyle::SP_TitleBarMenuButton));
connect(ui->actionLicenses, &QAction::triggered, [this]()
{
QMessageBox::aboutQt(this, "Duff");
});
auto algorithmGroup = new QActionGroup(this);
algorithmGroup->addAction(ui->actionMD5);
algorithmGroup->addAction(ui->actionSHA_1);
algorithmGroup->addAction(ui->actionSHA_256);
algorithmGroup->addAction(ui->actionSHA_512);
algorithmGroup->setExclusive(true);
connect(ui->actionMD5, &QAction::triggered,
std::bind(&HashCalculator::setAlgorithm, _hashCalculator, QCryptographicHash::Algorithm::Md5));
connect(ui->actionSHA_1, &QAction::triggered,
std::bind(&HashCalculator::setAlgorithm, _hashCalculator, QCryptographicHash::Algorithm::Sha1));
connect(ui->actionSHA_256, &QAction::triggered,
std::bind(&HashCalculator::setAlgorithm, _hashCalculator, QCryptographicHash::Algorithm::Sha256));
connect(ui->actionSHA_512, &QAction::triggered,
std::bind(&HashCalculator::setAlgorithm, _hashCalculator, QCryptographicHash::Algorithm::Sha512));
ui->actionSHA_256->setChecked(true);
}
void MainWindow::initHashCalculator()
{
qRegisterMetaType<HashCalculator::ErrorType>("ErrorType");
connect(_hashCalculator, &HashCalculator::processing, this, &MainWindow::onProcessing);
connect(_hashCalculator, &HashCalculator::duplicateFound, _model, &ResultModel::addPath);
connect(_hashCalculator, &HashCalculator::duplicateFound, this, &MainWindow::onDuplicateFound);
connect(_hashCalculator, &HashCalculator::finished, this, &MainWindow::onFinished);
connect(_hashCalculator, &HashCalculator::failure, this, &MainWindow::onFailure);
}
void MainWindow::initStateMachine()
{
auto emptyState = new QState();
emptyState->assignProperty(ui->pushButtonFindDuplicates, "enabled", false);
emptyState->assignProperty(ui->pushButtonRefresh, "enabled", false);
emptyState->assignProperty(ui->pushButtonDeleteSelected, "enabled", false);
emptyState->assignProperty(ui->lineEditSelectedDirectory, "enabled", true);
emptyState->assignProperty(ui->treeViewResults, "enabled", false);
emptyState->setObjectName("empty");
auto readyState = new QState();
readyState->assignProperty(ui->pushButtonFindDuplicates, "text", "Find duplicates");
readyState->assignProperty(ui->pushButtonFindDuplicates, "enabled", true);
readyState->assignProperty(ui->pushButtonRefresh, "enabled", true);
readyState->assignProperty(ui->pushButtonDeleteSelected, "enabled", true);
readyState->assignProperty(ui->lineEditSelectedDirectory, "enabled", true);
readyState->assignProperty(ui->treeViewResults, "enabled", true);
readyState->setObjectName("ready");
auto runningState = new QState();
runningState->assignProperty(ui->pushButtonFindDuplicates, "text", "Cancel");
runningState->assignProperty(ui->pushButtonRefresh, "enabled", false);
runningState->assignProperty(ui->pushButtonDeleteSelected, "enabled", false);
runningState->assignProperty(ui->lineEditSelectedDirectory, "enabled", false);
runningState->assignProperty(ui->treeViewResults, "enabled", false);
runningState->setObjectName("running");
emptyState->addTransition(this, &MainWindow::inputReady, readyState);
readyState->addTransition(this, &MainWindow::inputIncomplete, emptyState);
readyState->addTransition(ui->pushButtonFindDuplicates, &QAbstractButton::clicked, runningState);
runningState->addTransition(_hashCalculator, &HashCalculator::finished, readyState);
runningState->addTransition(ui->pushButtonFindDuplicates, &QAbstractButton::clicked, readyState);
connect(readyState, &QState::entered, _hashCalculator, &QThread::requestInterruption);
connect(runningState, &QState::entered, this, &MainWindow::onFindDuplicates);
_machine.addState(emptyState);
_machine.addState(readyState);
_machine.addState(runningState);
_machine.setInitialState(emptyState);
_machine.start();
}
void MainWindow::processCommandLine()
{
const QStringList args = QCoreApplication::arguments();
if (args.count() == 2)
{
// TODO: investigate why this does not work when called directly
QTimer::singleShot(100, std::bind(&QLineEdit::setText, ui->lineEditSelectedDirectory, args[1]));
}
}
void MainWindow::populateTree(const QString& directory)
{
_model->clear();
ui->menuAlgorithm->setEnabled(false);
_hashCalculator->setDirectory(directory);
if (!ui->lineEditWildcards->text().isEmpty())
{
_hashCalculator->setWildcards(ui->lineEditWildcards->text());
}
_hashCalculator->start();
}
void MainWindow::updateSelectedLabel()
{
const int selectedCount = _model->selectedCount();
const int totalCount = _model->totalCount();
const QString text = QString("%1 / %2 selected").arg(selectedCount).arg(totalCount);
ui->labelSelectedCount->setText(text);
}
void MainWindow::createFileContextMenu(const QPoint& pos)
{
const QModelIndex selection = ui->treeViewResults->indexAt(pos);
if (!selection.isValid())
{
qDebug() << "Invalid selection";
return;
}
const QVariant variant = _model->data(selection, Qt::DisplayRole);
if (!variant.isValid())
{
qDebug() << "Invalid variant";
return;
}
const QString filePath = variant.toString();
if (filePath.isEmpty())
{
qDebug() << "File path is empty / no file selected";
return;
}
auto openFileAction = new QAction("Open file", this);
connect(openFileAction, &QAction::triggered, std::bind(&MainWindow::openFileWithDefaultAssociation, this, filePath));
auto openParentDirAction = new QAction("Open parent directory", this);
connect(openParentDirAction, &QAction::triggered, std::bind(&MainWindow::openParentDirectory, this, filePath));
auto removeFileAction = new QAction("Delete file", this);
connect(removeFileAction, &QAction::triggered, std::bind(&MainWindow::removeFile, this, filePath));
QMenu menu(this);
menu.addActions({ openFileAction, openParentDirAction, removeFileAction });
menu.exec(ui->treeViewResults->mapToGlobal(pos));
}
void MainWindow::openFileWithDefaultAssociation(const QString& filePath)
{
const QUrl url = QUrl::fromLocalFile(filePath);
if (!QDesktopServices::openUrl(url))
{
QMessageBox::warning(this, "Failed to open", "Failed to open file:\n\n" + filePath + "\n");
}
}
void MainWindow::openParentDirectory(const QString& filePath)
{
const QFileInfo fileInfo(filePath);
const QUrl url = QUrl::fromLocalFile(fileInfo.dir().path());
if (!QDesktopServices::openUrl(url))
{
QMessageBox::warning(this, "Failed to open", "Failed to open directory:\n\n" + filePath + "\n");
}
}
bool MainWindow::removeFile(const QString& filePath)
{
if (QMessageBox::question(
this,
"Remove file?", "Are you sure you want to remove:\n\n" + filePath + "\n\nThe file will be permanently deleted.\n",
QMessageBox::Yes | QMessageBox::No,
QMessageBox::No) != QMessageBox::Yes)
{
return false;
}
if (!QFile::remove(filePath))
{
if (QFile::exists(filePath))
{
QMessageBox::warning(this, "Failed to remove file", "Failed to remove:\n\n" + filePath + "\n");
return false;
}
QMessageBox::warning(this, "Failed to remove file", filePath + "\n\ndoes not exist anymore!\n");
}
_model->removePath(filePath);
ui->treeViewResults->expandAll();
return true;
}
void MainWindow::onAbout()
{
const QString commitUrl =
QString("<a href='https://github.com/visuve/Duff/commit/%1'>%1</a>").arg(DUFF_COMMIT_HASH);
const QString licenseUrl =
QString("<a href='https://github.com/visuve/Duff/blob/%1/LICENSE.md'>LICENSE.md</a>").arg(DUFF_COMMIT_HASH);
QStringList text;
text << "<h3>Duff - Duplicate File Finder</h3>";
text << "<p>Version " + QString(DUFF_VERSION) + ".</p>";
text << "<p>Duff is yet another duplicate file finder.</p>";
text << "<p>Duff calculates checksums of files withing given folder. "
"The paths of the files containing a non-unique checksum are displayed in the main view.</p>";
text << "<p>Duff is open source (GPLv2) and written in C++ and uses Qt framework.</p>";
text << "<p>See Licenses menu and " << licenseUrl << " for more details.</p>";
text << "<p>Git commit hash this build is from: " << commitUrl << "</p>";
QMessageBox::about(this, "Duff", text.join('\n'));
}