diff --git a/src/main.py b/src/main.py index 7150f73..075b403 100644 --- a/src/main.py +++ b/src/main.py @@ -631,10 +631,52 @@ class MainWindow(QMainWindow): self._scan_library() def _on_library_remove_selected(self): - for item in self.library_ready_list.selectedItems(): - self.library_ready_list.takeItem(self.library_ready_list.row(item)) - for item in self.library_source_list.selectedItems(): - self.library_source_list.takeItem(self.library_source_list.row(item)) + """Delete selected files from disk and clean up empty directories.""" + files_to_delete = [] + for lst in (self.library_ready_list, self.library_source_list): + for item in lst.selectedItems(): + data = item.data(Qt.ItemDataRole.UserRole) + if data and os.path.exists(data.get("path", "")): + files_to_delete.append(data) + + if not files_to_delete: + QMessageBox.information(self, "Info", "No files selected") + return + + total_size = sum(f.get("size", 0) for f in files_to_delete) + details = "" + for f in files_to_delete[:15]: + details += f" {f.get('artist', 'Unknown')} {f['title']} ({self._format_size(f.get('size', 0))})\n" + if len(files_to_delete) > 15: + details += f" ... and {len(files_to_delete) - 15} more\n" + + reply = QMessageBox.question( + self, "Delete Files", + f"Delete {len(files_to_delete)} file(s) from disk?\n\n{details}\n" + f"Total: {self._format_size(total_size)}\n\n" + f"This action cannot be undone.", + QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No + ) + if reply != QMessageBox.StandardButton.Yes: + return + + deleted = 0 + for f in files_to_delete: + path = f["path"] + try: + os.remove(path) + deleted += 1 + parent = os.path.dirname(path) + while parent != os.path.dirname(parent): + if os.listdir(parent): + break + os.rmdir(parent) + parent = os.path.dirname(parent) + except OSError as e: + logger.error(f"Failed to delete {path}: {e}") + + self._scan_library() + QMessageBox.information(self, "Done", f"Deleted {deleted} file(s)") def _on_download_clicked(self): url = self.url_input.text().strip()