Skip to content

Commit 2d73251

Browse files
committed
+ support delete multiple files
1 parent e64e010 commit 2d73251

3 files changed

Lines changed: 85 additions & 19 deletions

File tree

Src/devicebridge.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ class DeviceBridge : public QObject
206206
void PullFromStorage(QString devicePath, QString localPath, QString bundleId = "");
207207
void PullMultipleFromStorage(QList<QPair<QString,QString>> pairs, QString bundleId = "");
208208
void DeleteFromStorage(QString devicePath, QString bundleId = "");
209+
void DeleteMultipleFromStorage(QStringList devicePaths, QString bundleId = "");
209210
void MakeDirectoryToStorage(QString devicePath, QString bundleId = "");
210211
void RenameToStorage(QString oldPath, QString newPath, QString bundleId = "");
211212
private:

Src/devicebridge_filemanager.cpp

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
void DeviceBridge::GetAccessibleStorage(QString startPath, QString bundleId, bool partialUpdate)
77
{
88
afc_filemanager_action(MobileOperation::FILE_LIST, [=, this](afc_client_t& afc){
9-
if (partialUpdate) {
9+
if (partialUpdate && startPath != "/") {
1010
QString prefix = startPath + "/";
1111
for (auto it = m_accessibleStorage.begin(); it != m_accessibleStorage.end(); ) {
1212
if (it.key().startsWith(prefix))
@@ -168,6 +168,49 @@ void DeviceBridge::PullMultipleFromStorage(QList<QPair<QString,QString>> pairs,
168168
}, bundleId);
169169
}
170170

171+
void DeviceBridge::DeleteMultipleFromStorage(QStringList devicePaths, QString bundleId)
172+
{
173+
afc_filemanager_action(MobileOperation::DELETE_FILE, [=, this](afc_client_t& afc){
174+
int totalFiles = devicePaths.size();
175+
if (totalFiles == 0) return;
176+
177+
bool hasError = false;
178+
179+
for (int i = 0; i < totalFiles; i++) {
180+
const QString& devicePath = devicePaths[i];
181+
QString filename = QFileInfo(devicePath).fileName();
182+
QString fileLabel = QString("[%1/%2] %3").arg(i + 1).arg(totalFiles).arg(filename);
183+
184+
emit FileManagerChanged(GenericStatus::IN_PROGRESS, FileOperation::DELETE_OP,
185+
(i * 100) / totalFiles, fileLabel);
186+
187+
afc_error_t err = afc_remove_path(afc, devicePath.toUtf8().data());
188+
if (err != AFC_E_SUCCESS) {
189+
hasError = true;
190+
emit FileManagerChanged(GenericStatus::IN_PROGRESS, FileOperation::DELETE_OP,
191+
((i + 1) * 100) / totalFiles, fileLabel + " - FAILED - " + QString::number(err));
192+
}
193+
}
194+
195+
// Find common ancestor directory of all deleted paths for partial refresh
196+
auto splitPath = [](const QString& p) { return p.split('/', Qt::SkipEmptyParts); };
197+
QStringList common = splitPath(QFileInfo(devicePaths.first()).path());
198+
for (const QString& path : devicePaths) {
199+
QStringList parts = splitPath(QFileInfo(path).path());
200+
int minLen = std::min(common.size(), parts.size());
201+
int i = 0;
202+
while (i < minLen && common[i] == parts[i]) i++;
203+
common = common.mid(0, i);
204+
}
205+
QString commonDir = (common.isEmpty() ? "/" : "/" + common.join('/')) + "/";
206+
207+
if (hasError)
208+
emit FileManagerChanged(GenericStatus::FAILED, FileOperation::DELETE_OP, 100, "Completed with errors");
209+
else
210+
emit FileManagerChanged(GenericStatus::SUCCESS, FileOperation::DELETE_OP, 100, commonDir);
211+
}, bundleId);
212+
}
213+
171214
void DeviceBridge::DeleteFromStorage(QString devicePath, QString bundleId)
172215
{
173216
afc_filemanager_action(MobileOperation::DELETE_FILE, [=, this](afc_client_t& afc){

Src/mainwindow_filemanager.cpp

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,10 @@ void MainWindow::OnFileManagerChanged(GenericStatus status, FileOperation operat
305305
QString storage = ui->storageOption->currentText();
306306
storage = storage.contains("User's Data", Qt::CaseInsensitive) ? "" : storage;
307307

308-
// partial update
309-
QFileInfo fileInfo(message);
310-
QString dirPath = fileInfo.dir().absolutePath();
308+
// partial update — trailing '/' means message is already the refresh directory
309+
QString dirPath = message.endsWith('/')
310+
? message.chopped(1)
311+
: QFileInfo(message).dir().absolutePath();
311312
DeviceBridge::Get()->GetAccessibleStorage(dirPath, storage, true);
312313
}
313314
}
@@ -426,21 +427,42 @@ void MainWindow::OnPushFileClicked()
426427

427428
void MainWindow::OnDeleteFileClicked()
428429
{
429-
FileManagerAction([this](QString& initialText, QString& storageAccess){
430-
QMessageBox msgBox(this);
431-
msgBox.setWindowTitle("Delete");
432-
msgBox.setText("Are you sure to delete " + initialText + " from device?");
433-
msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
434-
msgBox.setDefaultButton(QMessageBox::Ok);
435-
436-
int ret = msgBox.exec();
437-
if (ret == QMessageBox::Ok) {
438-
DeviceBridge::Get()->DeleteFromStorage(initialText, storageAccess);
439-
}
440-
else {
441-
m_loadingFileOperation->close();
442-
}
443-
}, false);
430+
QString storage = ui->storageOption->currentText();
431+
storage = storage.contains("User's Data", Qt::CaseInsensitive) ? "" : storage;
432+
433+
QModelIndexList selected = ui->fileBrowserTree->selectionModel()->selectedIndexes();
434+
QStringList selectedPaths;
435+
for (const QModelIndex& idx : selected) {
436+
if (idx.column() != 0) continue;
437+
QString path = idx.data(Qt::UserRole).toString();
438+
if (path.isEmpty()) continue;
439+
if (!selectedPaths.contains(path))
440+
selectedPaths.append(path);
441+
}
442+
443+
if (selectedPaths.isEmpty()) {
444+
QMessageBox::critical(this, "Error", "Please choose file(s) in File Manager's Browser!", QMessageBox::Ok);
445+
return;
446+
}
447+
448+
QString confirmMsg = selectedPaths.size() == 1
449+
? "Are you sure to delete " + selectedPaths.first() + " from device?"
450+
: QString("Are you sure to delete %1 items from device?").arg(selectedPaths.size());
451+
452+
QMessageBox msgBox(this);
453+
msgBox.setWindowTitle("Delete");
454+
msgBox.setText(confirmMsg);
455+
msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
456+
msgBox.setDefaultButton(QMessageBox::Ok);
457+
458+
if (msgBox.exec() == QMessageBox::Ok) {
459+
if (selectedPaths.size() == 1)
460+
DeviceBridge::Get()->DeleteFromStorage(selectedPaths.first(), storage);
461+
else
462+
DeviceBridge::Get()->DeleteMultipleFromStorage(selectedPaths, storage);
463+
} else {
464+
m_loadingFileOperation->close();
465+
}
444466
}
445467

446468
void MainWindow::OnRenameFileClicked()

0 commit comments

Comments
 (0)