Skip to content

Commit 79ba1db

Browse files
committed
Small refactoring of delete func
1 parent dd658e4 commit 79ba1db

7 files changed

+84
-27
lines changed

QtFileExplorer.pro

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@ SOURCES += main.cpp\
1818
mywindows.cpp \
1919
fileinfo.cpp \
2020
imagepreview.cpp \
21-
keypresseater.cpp
21+
keypresseater.cpp \
22+
refreshtask.cpp
2223

2324
HEADERS += \
2425
deletetask.h \
2526
mywindows.h \
2627
fileinfo.h \
2728
imagepreview.h \
28-
keypresseater.h
29+
keypresseater.h \
30+
refreshtask.h
2931

3032
FORMS +=
3133

deletetask.cpp

+18-12
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,37 @@ deletetask::deletetask(QStringList something, QLabel *status,
77
this->todelete = something;
88
}
99

10-
void deletetask::run() {
11-
status->setText("DELETING " + todelete.at(0));
12-
13-
for (int i = 0; i < todelete.length(); ++i) {
14-
// qDebug() << "DELETE" << shiftList.at(i);
15-
// Old delete = RM -r
16-
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
17-
18-
QFileInfo tmp(todelete.at(i));
10+
void deletetask::deleteFileRm(QString filepath) {
11+
QFileInfo tmp(filepath);
1912
if (tmp.isFile()) {
20-
QFile file(todelete.at(i));
13+
QFile file(filepath);
2114
if (!file.remove()) {
2215
qDebug() << "File not deleted: " << file.fileName();
2316
}
2417
} else {
25-
QDir folder(todelete.at(i));
18+
QDir folder(filepath);
2619
if (!folder.removeRecursively()) {
2720
qDebug() << "Not all was deleted: " << folder.absolutePath();
2821
}
2922
}
23+
}
24+
25+
void deletetask::run() {
26+
status->setText("DELETING " + todelete.at(0));
27+
28+
for (int i = 0; i < todelete.length(); ++i) {
29+
// qDebug() << "DELETE" << shiftList.at(i);
30+
// Old delete = RM -r
31+
QString toDelete = todelete.at(i);
32+
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
33+
deleteFileRm(toDelete);
3034
#else
3135
// New delete = Move to Trash
32-
QString toDelete = todelete.at(i);
3336
if (!QFile::moveToTrash(toDelete)) {
3437
qDebug() << "Not deleted: " << toDelete;
38+
qDebug() << "Trying to rm ";
39+
deleteFileRm(toDelete);
40+
3541
}
3642
#endif
3743
}

deletetask.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class deletetask : public QRunnable {
1111
public:
1212
deletetask(QStringList, QLabel *, QList<QString> *);
1313
void run() override;
14-
14+
void deleteFileRm(QString filepath);
1515
private:
1616
QLabel *status;
1717
QList<QString> *deleteList;

mywindows.cpp

+16-12
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,7 @@ void myWindows::clickedNew(QModelIndex index, QModelIndex) {
161161
updatePath(index);
162162
QString fileName = model->fileName(index);
163163
QFileInfo infoFile(lastFilePath);
164-
QString ext = fileName.split(".")
165-
.back(); // We could use here QFileInfo::completeSuffix()
164+
QString ext = fileName.split(".").back(); // We could use here QFileInfo::completeSuffix()
166165
int SIZE_NAME_MAX = 50;
167166
if (fileName.length() > SIZE_NAME_MAX) {
168167
info->setName(fileName.mid(0, SIZE_NAME_MAX));
@@ -341,23 +340,28 @@ void myWindows::saveSettings() {
341340
settings.setValue("depthMax", MAX_DEPTH);
342341
}
343342

343+
int myWindows::canDelete() {
344+
QMessageBox box;
345+
box.setText("Selected files/folders will be eternally deleted !!");
346+
box.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
347+
box.setWindowFlags(Qt::WindowStaysOnTopHint);
348+
box.setDefaultButton(QMessageBox::Ok);
349+
return box.exec();
350+
}
351+
352+
344353
void myWindows::keyboardDel() {
345354
// If oldDelete i.e rm -r, ask for confirmation else move to bin
346355
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
347-
QMessageBox box;
348-
box.setText("Selected files/folders will be eternally deleted !!");
349-
box.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
350-
box.setWindowFlags(Qt::WindowStaysOnTopHint);
351-
box.setDefaultButton(QMessageBox::Ok);
352-
int ret = box.exec();
356+
int ret = canDelete();
357+
#else
358+
// New way, we can "delete" all the time
359+
int ret = QMessageBox::Ok;
360+
#endif
353361
if (ret == QMessageBox::Ok) {
354362
deletetask *task = new deletetask(shiftList, deleteStatus, toDelete);
355363
QThreadPool::globalInstance()->start(task);
356364
}
357-
#else
358-
deletetask *task = new deletetask(shiftList, deleteStatus, toDelete);
359-
QThreadPool::globalInstance()->start(task);
360-
#endif
361365
}
362366

363367
// To add coloration to folders/files

mywindows.h

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class myWindows : public QWidget {
6262
int screenW;
6363
void closeEvent(QCloseEvent *);
6464

65+
int canDelete();
6566
public slots:
6667
void clickedNew(QModelIndex index, QModelIndex index2);
6768
void keyboardEvent();

refreshtask.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "refreshtask.h"
2+
3+
4+
5+
refreshtask::refreshtask(QLabel *preview, QString path, int depth)
6+
{
7+
shouldrun = false;
8+
maxdepth = depth;
9+
currentPath = path;
10+
this->preview = preview;
11+
}
12+
13+
14+
void refreshtask::run() {
15+
16+
17+
}
18+
19+
void refreshtask::stop() {
20+
shouldrun = true;
21+
}

refreshtask.h

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef REFRESHTASK_H
2+
#define REFRESHTASK_H
3+
4+
#include <QRunnable>
5+
#include <QFileInfo>
6+
#include <QLabel>
7+
#include <QDir>
8+
9+
10+
class refreshtask: public QRunnable
11+
{
12+
public:
13+
refreshtask(QLabel *preview, QString currentPath, int depth);
14+
void run() override;
15+
void stop();
16+
private:
17+
bool shouldrun;
18+
QLabel *preview;
19+
QString currentPath;
20+
int maxdepth;
21+
};
22+
23+
#endif // REFRESHTASK_H

0 commit comments

Comments
 (0)