mirror of
https://github.com/KeksPirates/SoftwareManager.git
synced 2026-08-04 17:59:43 +02:00
feat: multi select, delete and cancel downloads
This commit is contained in:
@@ -64,29 +64,44 @@ class ContextMenu_Downloads:
|
|||||||
clipboard.setText(magnet_link)
|
clipboard.setText(magnet_link)
|
||||||
|
|
||||||
def cancelDownloadAction(self):
|
def cancelDownloadAction(self):
|
||||||
if not hasattr(self, '_context_menu_row'):
|
selected_rows = sorted(set(index.row() for index in self.downloadList.selectedIndexes()), reverse=True)
|
||||||
|
|
||||||
|
if not selected_rows:
|
||||||
return
|
return
|
||||||
row = self._context_menu_row
|
|
||||||
|
names = []
|
||||||
|
items_to_remove = []
|
||||||
|
|
||||||
with state.downloads_lock:
|
with state.downloads_lock:
|
||||||
if row < 0 or row >= len(state.active_downloads):
|
keys = list(state.active_downloads.keys())
|
||||||
return
|
|
||||||
magnet_link = list(state.active_downloads.keys())[row]
|
for row in selected_rows:
|
||||||
|
if 0 <= row < len(keys):
|
||||||
|
magnet_link = keys[row]
|
||||||
magnetdl = state.active_downloads[magnet_link]
|
magnetdl = state.active_downloads[magnet_link]
|
||||||
|
|
||||||
# Cache the name BEFORE removing from session
|
|
||||||
try:
|
try:
|
||||||
torrent_name = magnetdl.status().name
|
name = magnetdl.status().name
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
torrent_name = "Unknown"
|
name = "Unknown"
|
||||||
|
|
||||||
|
names.append(name)
|
||||||
|
items_to_remove.append((magnet_link, magnetdl, name))
|
||||||
|
|
||||||
confirm = QMessageBox.question(
|
confirm = QMessageBox.question(
|
||||||
self.main_window, "Cancel Download",
|
self.main_window,
|
||||||
f"Are you sure you want to cancel the download of '{torrent_name}'?",
|
"Cancel Downloads",
|
||||||
|
f"Are you sure you want to cancel {len(names)} download(s)?\n\n" + "\n".join(names),
|
||||||
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No
|
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No
|
||||||
)
|
)
|
||||||
if confirm == QMessageBox.StandardButton.Yes:
|
|
||||||
|
if confirm != QMessageBox.StandardButton.Yes:
|
||||||
|
return
|
||||||
|
|
||||||
|
for magnet_link, magnetdl, name in items_to_remove:
|
||||||
with state.downloads_lock:
|
with state.downloads_lock:
|
||||||
state.active_downloads.pop(magnet_link, None)
|
state.active_downloads.pop(magnet_link, None)
|
||||||
|
|
||||||
remove_download_log(magnet_link)
|
remove_download_log(magnet_link)
|
||||||
|
|
||||||
if state.dl_session:
|
if state.dl_session:
|
||||||
@@ -95,44 +110,65 @@ class ContextMenu_Downloads:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
consoleLog(f"Exception while removing download from LibTorrent: {e}")
|
consoleLog(f"Exception while removing download from LibTorrent: {e}")
|
||||||
|
|
||||||
consoleLog(f"Cancelled download: {torrent_name}", True)
|
consoleLog(f"Cancelled download: {name}", True)
|
||||||
|
|
||||||
|
|
||||||
def deleteFileAction(self):
|
def deleteFileAction(self):
|
||||||
if not hasattr(self, '_context_menu_row'):
|
selected_rows = sorted(set(index.row() for index in self.downloadList.selectedIndexes()), reverse=True)
|
||||||
|
|
||||||
|
if not selected_rows:
|
||||||
return
|
return
|
||||||
row = self._context_menu_row
|
|
||||||
|
items_to_remove = []
|
||||||
|
|
||||||
with state.downloads_lock:
|
with state.downloads_lock:
|
||||||
if row < 0 or row >= len(state.active_downloads):
|
keys = list(state.active_downloads.keys())
|
||||||
return
|
|
||||||
magnet_link = list(state.active_downloads.keys())[row]
|
for row in selected_rows:
|
||||||
|
if 0 <= row < len(keys):
|
||||||
|
magnet_link = keys[row]
|
||||||
magnetdl = state.active_downloads[magnet_link]
|
magnetdl = state.active_downloads[magnet_link]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
status = magnetdl.status()
|
status = magnetdl.status()
|
||||||
save_path = status.save_path
|
save_path = status.save_path
|
||||||
torrent_name = status.name
|
torrent_name = status.name
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
consoleLog("Error: torrent handle already invalid", True)
|
consoleLog("Error: torrent handle already invalid", True)
|
||||||
with state.downloads_lock:
|
continue
|
||||||
state.active_downloads.pop(magnet_link, None)
|
|
||||||
remove_download_log(magnet_link)
|
|
||||||
return
|
|
||||||
download_path = os.path.join(save_path, torrent_name)
|
download_path = os.path.join(save_path, torrent_name)
|
||||||
|
|
||||||
|
items_to_remove.append((magnet_link, magnetdl, torrent_name, download_path))
|
||||||
|
|
||||||
|
if not items_to_remove:
|
||||||
|
return
|
||||||
|
|
||||||
|
names = [item[2] for item in items_to_remove]
|
||||||
|
|
||||||
confirm = QMessageBox.question(
|
confirm = QMessageBox.question(
|
||||||
self.main_window, "Delete Files",
|
self.main_window,
|
||||||
f"Are you sure you want to delete the downloaded files of '{torrent_name}'? This action cannot be undone.",
|
"Delete Files",
|
||||||
|
f"Are you sure you want to delete {len(names)} download(s)?\n\n" + "\n".join(names),
|
||||||
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No
|
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No
|
||||||
)
|
)
|
||||||
if confirm == QMessageBox.StandardButton.Yes:
|
|
||||||
|
if confirm != QMessageBox.StandardButton.Yes:
|
||||||
|
return
|
||||||
|
|
||||||
|
for magnet_link, magnetdl, torrent_name, download_path in items_to_remove:
|
||||||
with state.downloads_lock:
|
with state.downloads_lock:
|
||||||
state.active_downloads.pop(magnet_link, None)
|
state.active_downloads.pop(magnet_link, None)
|
||||||
|
|
||||||
remove_download_log(magnet_link)
|
remove_download_log(magnet_link)
|
||||||
|
|
||||||
if state.dl_session:
|
if state.dl_session:
|
||||||
try:
|
try:
|
||||||
state.dl_session.remove_torrent(magnetdl)
|
state.dl_session.remove_torrent(magnetdl)
|
||||||
time.sleep(0.5)
|
time.sleep(0.5)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
consoleLog(f"Exception while removing download from LibTorrent: {e}")
|
consoleLog(f"Exception while removing download from LibTorrent: {e}")
|
||||||
|
|
||||||
if download_path and os.path.exists(download_path):
|
if download_path and os.path.exists(download_path):
|
||||||
last_error = None
|
last_error = None
|
||||||
for attempt in range(3):
|
for attempt in range(3):
|
||||||
@@ -146,6 +182,7 @@ class ContextMenu_Downloads:
|
|||||||
last_error = e
|
last_error = e
|
||||||
if attempt < 2:
|
if attempt < 2:
|
||||||
time.sleep(0.5)
|
time.sleep(0.5)
|
||||||
|
|
||||||
if last_error:
|
if last_error:
|
||||||
consoleLog(f"Error deleting files: {last_error}", True)
|
consoleLog(f"Error deleting files: {last_error}", True)
|
||||||
else:
|
else:
|
||||||
|
|||||||
Reference in New Issue
Block a user