-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.cpp
More file actions
369 lines (300 loc) · 11 KB
/
MainWindow.cpp
File metadata and controls
369 lines (300 loc) · 11 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
#include "MainWindow.hpp"
#include "ui_MainWindow.h"
#include "JiffieVersion.h"
#include "FileListModel.hpp"
#include "JunkFileFinder.hpp"
#include <QDesktopServices>
#include <QFileDialog>
#include <QFileInfo>
#include <QMessageBox>
#include <filesystem>
// QFile::remove fails with read only files, hence the hack.
// QFile::moveToTrash works, but it can be really really slow
// e.g. on large data masses on a slow network drive
inline bool removeFile(const QString& filePath)
{
#ifdef _WIN32
return std::filesystem::remove(filePath.toStdWString());
#else
return std::filesystem::remove(filePath.toStdString());
#endif
}
MainWindow::MainWindow(QWidget* parent) :
QMainWindow(parent),
_ui(new Ui::MainWindow),
_model(new FileListModel(this)),
_jiffie(new JunkFileFinder(this))
{
_ui->setupUi(this);
_ui->statusBar->showMessage("Welcome to Jiffie!");
_ui->listViewFiles->setModel(_model);
connect(_ui->pushButtonFindJunk, &QPushButton::clicked, this, &MainWindow::onStartStopSearch);
connect(_ui->pushButtonSelectAll, &QPushButton::clicked, this, &MainWindow::onSelectAll);
connect(_ui->pushButtonDeleteSelected, &QPushButton::clicked, this, &MainWindow::onRemoveSelected);
connect(_ui->listViewFiles, &QListView::customContextMenuRequested, this, &MainWindow::onCreateFileContextMenu);
initJiffie();
initMenuBar();
initStateMachine();
}
MainWindow::~MainWindow()
{
delete _ui;
}
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);
if (!fileInfo.exists())
{
QMessageBox::warning(this, "Failed to open", "The file does not appear to exist:\n\n" + filePath + "\n");
return;
}
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");
}
}
void MainWindow::onAbout()
{
const QString commitUrl =
QString("<a href='https://github.com/visuve/Jiffie/commit/%1'>%1</a>").arg(JIFFIE_COMMIT_HASH);
const QString licenseUrl =
QString("<a href='https://github.com/visuve/Jiffie/blob/%1/LICENSE.md'>LICENSE.md</a>").arg(JIFFIE_COMMIT_HASH);
QStringList text;
text << "<h3>Jiffie - Junk File Finder</h3>";
text << "<p>Version " + QString(JIFFIE_VERSION) + ".</p>";
text << "<p>Jiffie is yet another junk file finder.</p>";
text << "<p>Jiffie 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, "Jiffie", text.join('\n'));
}
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));
emit directorySelected();
}
}
void MainWindow::onStartStopSearch()
{
if (_jiffie->isRunning())
{
_jiffie->requestInterruption();
_jiffie->wait();
const QString message = QString("%1 Jiffie canceled!").arg(QTime::currentTime().toString());
_ui->statusBar->showMessage(message);
return;
}
const QString selectedDirectory = _ui->lineEditSelectedDirectory->text();
if (selectedDirectory.isEmpty())
{
onOpenDirectoryDialog();
onStartStopSearch();
return;
}
if (!QDir(selectedDirectory).exists())
{
QMessageBox::warning(this, "Invalid directory", '"' + selectedDirectory + '"' + " does not appear to exist!");
onOpenDirectoryDialog();
return;
}
const QString selectedWildcards = _ui->lineEditWildcards->text();
if (selectedWildcards.isEmpty())
{
QMessageBox::StandardButton reply = QMessageBox::warning(
this,
"No wildcards",
"Having no wildcards will include all files in the selected directory.\nAre you sure you want to continue?",
QMessageBox::Yes | QMessageBox::No);
if (reply != QMessageBox::Yes)
{
return;
}
}
_model->clear();
_jiffie->setDirectory(selectedDirectory);
_jiffie->setWildcards(selectedWildcards);
_jiffie->start();
const QString message = QString("%1 Jiffie started!").arg(QTime::currentTime().toString());
_ui->statusBar->showMessage(message);
emit searchStarted();
}
void MainWindow::onProgress(const QString& directoryPath)
{
const QString message =
QString("%1 Currently processing: %2")
.arg(QTime::currentTime().toString())
.arg(QDir::toNativeSeparators(directoryPath));
_ui->statusBar->showMessage(message);
}
void MainWindow::onFinished()
{
if (_model->rowCount() <= 0)
{
QMessageBox::information(
this,
"No junk files",
"No junk files were found.\n");
emit noResultsFound();
}
else
{
emit resultsFound();
}
const QString message =
QString("%1 Finished searching: %2")
.arg(QTime::currentTime().toString())
.arg(_ui->lineEditSelectedDirectory->text());
_ui->statusBar->showMessage(message);
}
void MainWindow::onSelectAll()
{
_model->selectAll();
}
void MainWindow::onRemoveSelected()
{
if (!_model->hasSelection())
{
QMessageBox::warning(this, "Failed to remove file(s)", "Nothing selected!\n");
return;
}
if (QMessageBox::question(
this,
"Confirm delete?",
"Are you sure you want to delete the selected files?") !=
QMessageBox::StandardButton::Yes)
{
return;
}
_model->removeIf([this](const FileListModel::FileItem& item)->bool
{
if (item.state != Qt::CheckState::Checked)
{
return false;
}
const QString path = QDir::toNativeSeparators(item.path);
if (!removeFile(path))
{
if (QFile::exists(path))
{
QMessageBox::warning(this, "Failed to remove file", "Failed to remove:\n\n" + path + "\n");
return false;
}
QMessageBox::warning(this, "Failed to remove file", path + "\n\ndoes not exist anymore!\n");
}
return true;
});
}
void MainWindow::onCreateFileContextMenu(const QPoint& pos)
{
const QModelIndex selection = _ui->listViewFiles->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));
QMenu menu(this);
menu.addActions({ openParentDirAction });
menu.exec(_ui->listViewFiles->mapToGlobal(pos));
}
void MainWindow::initJiffie()
{
connect(_jiffie, &JunkFileFinder::junkFound, _model, &FileListModel::addFilePath);
connect(_jiffie, &JunkFileFinder::progress, this, &MainWindow::onProgress);
connect(_jiffie, &JunkFileFinder::finished, this, &MainWindow::onFinished);
}
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, "Jiffie");
});
}
void MainWindow::initStateMachine()
{
auto initialState = new QState();
initialState->assignProperty(_ui->lineEditSelectedDirectory, "enabled", true);
initialState->assignProperty(_ui->lineEditWildcards, "enabled", false);
initialState->assignProperty(_ui->listViewFiles, "enabled", false);
initialState->assignProperty(_ui->pushButtonSelectAll, "enabled", false);
initialState->assignProperty(_ui->pushButtonDeleteSelected, "enabled", false);
initialState->assignProperty(_ui->pushButtonFindJunk, "enabled", false);
initialState->assignProperty(_ui->pushButtonFindJunk, "text", "Please select a directory");
initialState->setObjectName("empty");
auto readyState = new QState();
readyState->assignProperty(_ui->lineEditSelectedDirectory, "enabled", true);
readyState->assignProperty(_ui->lineEditWildcards, "enabled", true);
readyState->assignProperty(_ui->listViewFiles, "enabled", false);
readyState->assignProperty(_ui->pushButtonSelectAll, "enabled", false);
readyState->assignProperty(_ui->pushButtonDeleteSelected, "enabled", false);
readyState->assignProperty(_ui->pushButtonFindJunk, "enabled", true);
readyState->assignProperty(_ui->pushButtonFindJunk, "text", "Find Junk");
readyState->setObjectName("selected");
auto runningState = new QState();
runningState->assignProperty(_ui->lineEditSelectedDirectory, "enabled", false);
runningState->assignProperty(_ui->lineEditWildcards, "enabled", false);
runningState->assignProperty(_ui->listViewFiles, "enabled", false);
runningState->assignProperty(_ui->pushButtonSelectAll, "enabled", false);
runningState->assignProperty(_ui->pushButtonDeleteSelected, "enabled", false);
runningState->assignProperty(_ui->pushButtonFindJunk, "enabled", true);
runningState->assignProperty(_ui->pushButtonFindJunk, "text", "Cancel");
runningState->setObjectName("running");
auto finishedState = new QState();
finishedState->assignProperty(_ui->lineEditSelectedDirectory, "enabled", true);
finishedState->assignProperty(_ui->lineEditWildcards, "enabled", true);
finishedState->assignProperty(_ui->listViewFiles, "enabled", true);
finishedState->assignProperty(_ui->pushButtonSelectAll, "enabled", true);
finishedState->assignProperty(_ui->pushButtonDeleteSelected, "enabled", true);
finishedState->assignProperty(_ui->pushButtonFindJunk, "enabled", true);
finishedState->assignProperty(_ui->pushButtonFindJunk, "text", "Find Junk");
finishedState->setObjectName("finished");
initialState->addTransition(this, &MainWindow::directorySelected, readyState);
readyState->addTransition(this, &MainWindow::searchStarted, runningState);
runningState->addTransition(this, &MainWindow::resultsFound, finishedState);
runningState->addTransition(this, &MainWindow::noResultsFound, readyState);
// TODO: add a state where all results are cleared
_machine.addState(initialState);
_machine.addState(readyState);
_machine.addState(runningState);
_machine.addState(finishedState);
_machine.setInitialState(initialState);
_machine.start();
}