-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
executable file
·161 lines (131 loc) · 5.98 KB
/
mainwindow.cpp
File metadata and controls
executable file
·161 lines (131 loc) · 5.98 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "finderOfCopies.h"
typedef std::map<QByteArray, QVector<std::pair<QString, int>>> mapToTree;
main_window::main_window(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
setGeometry(QStyle::alignedRect(Qt::LeftToRight, Qt::AlignCenter, size(), qApp->desktop()->availableGeometry()));
ui->treeWidget->header()->setSectionResizeMode(0, QHeaderView::Stretch);
ui->treeWidget->header()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
setWindowTitle(QString("FileManager"));
ui->pushButton_2->setEnabled(false);
ui->pushButton_3->setEnabled(false);
ui->pushButton_4->setEnabled(false);
ui->pushButton_4->setEnabled(false);
ui->stopButton->setEnabled(false);
ui->progressBar->setValue(0);
ui->label->clear();
QCommonStyle style;
ui->actionExit->setIcon(style.standardIcon(QCommonStyle::SP_DialogCloseButton));
connect(ui->actionScan_Directory, &QAction::triggered, this, &main_window::select_directory);
connect(ui->actionExit, &QAction::triggered, this, &QWidget::close);
connect(ui->pushButton, &QPushButton::clicked, this, &main_window::select_directory);
connect(ui->pushButton_2, &QPushButton::clicked, this, &main_window::start_search);
connect(ui->pushButton_3, &QPushButton::clicked, this, &main_window::select_useless);
connect(ui->pushButton_4, &QPushButton::clicked, this, &main_window::delete_useless);
connect(ui->stopButton, &QPushButton::clicked, this, &main_window::interruption);
qRegisterMetaType<mapToTree>("mapToTree");
}
void main_window::interruption() {
thread->requestInterruption();
}
main_window::~main_window() = default;
void main_window::select_directory() {
QString dir = QFileDialog::getExistingDirectory(this, "Select Directory for Scanning",
QString(),
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
setWindowTitle(QString("Directory Content - %1").arg(dir));
curDir = dir;
ui->lineEdit->clear();
ui->lineEdit->insert(dir);
ui->pushButton_2->setEnabled(true);
ui->progressBar->setValue(0);
ui->treeWidget->clear();
}
void main_window::start_search() {
ui->progressBar->setValue(0);
ui->pushButton_3->setEnabled(false);
ui->pushButton_4->setEnabled(false);
ui->treeWidget->clear();
thread = new QThread;
auto *worker = new finder(curDir);
worker->moveToThread(thread);
connect(thread, SIGNAL(started()), worker, SLOT(process()));
connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
connect(worker, SIGNAL(addToTree(mapToTree)),
this, SLOT(addToTreeUI(mapToTree)));
connect(worker, SIGNAL(setProgressBar(long long)), this, SLOT(setProgress(long long)));
connect(worker, SIGNAL(finished()), this, SLOT(doFinishThings()));
time = std::clock();
ui->stopButton->setEnabled(true);
thread->start();
}
void main_window::setProgress(long long MAXS) {
ui->progressBar->setMaximum((int)std::abs(MAXS));
}
void main_window::doFinishThings() {
ui->stopButton->setEnabled(false);
time = std::clock() - time;
if (ui->treeWidget->topLevelItemCount() == 0) {
auto *item = new QTreeWidgetItem(ui->treeWidget);
item->setText(0, QString("Not Found Duplicates!)"));
ui->pushButton_3->setEnabled(false);
} else {
ui->pushButton_3->setEnabled(true);
ui->label->clear();
ui->label->setText(QString("In total: ") + QString::number(sum) + QString(" bytes!! (") +
QString::number(time / CLOCKS_PER_SEC) + QString(" sec)"));
}
}
void main_window::addToTreeUI(std::map<QByteArray, QVector<std::pair<QString, int>>> hashs) {
for (auto cur = hashs.begin(); cur != hashs.end(); ++cur) {
if (cur->second.size() != 1) {
//wasDuplicate = true;
auto *item = new QTreeWidgetItem(ui->treeWidget);
item->setText(0, QString("Found ") + QString::number(cur->second.size()) + QString(" duplicates"));
QFileInfo file_info_temp(cur->second[0].first);
item->setText(1,
QString::number(file_info_temp.size() * (cur->second.size() - 1)) +
QString(" bytes"));
sum += file_info_temp.size() * (cur->second.size() - 1);
for (auto child : cur->second) {
QTreeWidgetItem *childItem = new QTreeWidgetItem();
childItem->setText(0, child.first.mid(curDir.length() + 1, child.first.length() - curDir.length() - 1));
item->addChild(childItem);
}
ui->treeWidget->addTopLevelItem(item);
}
QFileInfo file_info_temp(cur->second[0].first);
sumProgress += cur->second.size() * file_info_temp.size();
ui->progressBar->setValue(ui->progressBar->value() + (int)sumProgress);
}
}
void main_window::select_useless() {
auto root = ui->treeWidget->invisibleRootItem();
for (int i = 0; i < root->childCount(); ++i) {
auto* top_item = root->child(i);
for (int j = 1; j < top_item->childCount(); j++) {
auto child_item = top_item->child(j);
child_item->setSelected(true);
}
}
ui->pushButton_4->setEnabled(true);
}
void main_window::delete_useless() {
QMessageBox::StandardButton reply = QMessageBox::question(this, "Deleting useless",
"All files will be deleted forever. \nDo you really want to continue?");
if (reply == QMessageBox::Yes) {
QVector<QString> paths;
for (auto u : ui->treeWidget->selectedItems()) {
paths.push_back(curDir + '/' + u->text(0));
u->~QTreeWidgetItem();
}
for (int i = 0; i < paths.size(); i++) {
QFile(paths[i]).remove();
}
}
}
void main_window::show_about_dialog() {//NOLINT
QMessageBox::aboutQt(this);
}