feat: cancel a download from the TUI, the CLI, and the daemon
Closes #54. A download can now be cancelled whether it is queued, in progress, or paused. Cancelled is a terminal status: the store refuses to overwrite it, the status channel relays nothing after it, the partial file is deleted, and the transfer thread releases its socket at its next one-second poll instead of the 30s read deadline. A queued download that is cancelled declines the peer's later offer with TransferResponse "Cancelled", and an inbound file connection for it is refused. A fresh download of the same file evicts the dead row so it is not shadowed, and a cancelled transfer is not re-queued after a restart. Surfaces: Client::cancel_download, download.cancel over RPC, `transfer cancel <user> <path>` on the CLI (which also stops an upload by that name), and x in the transfers pane, where d dismisses the row.
This commit is contained in:
@@ -1,18 +1,10 @@
|
||||
use super::MainTui;
|
||||
use crate::models::{DownloadEntry, FileDisplayData};
|
||||
use crate::ui::panes::{SelectedTransfer, selected_transfer};
|
||||
use soulseek_rs::types::UploadStatus;
|
||||
use soulseek_rs::{DownloadStatus, types::Download};
|
||||
use std::{sync::mpsc, thread};
|
||||
|
||||
/// Done, given up, or ran out of time: the row is all that is left.
|
||||
const fn is_finished(download: &Download) -> bool {
|
||||
matches!(
|
||||
download.status,
|
||||
DownloadStatus::Completed
|
||||
| DownloadStatus::Failed(_)
|
||||
| DownloadStatus::TimedOut
|
||||
)
|
||||
}
|
||||
|
||||
impl MainTui {
|
||||
/// Channel for queued downloads, created on first use.
|
||||
pub(super) fn downloads_sender(
|
||||
@@ -73,7 +65,7 @@ impl MainTui {
|
||||
// session holds, so dropping them only here would last until the next
|
||||
// poll put them straight back.
|
||||
for entry in &self.state.downloads {
|
||||
if is_finished(&entry.download) {
|
||||
if entry.download.is_finished() {
|
||||
self.client.remove_download(
|
||||
&entry.download.username,
|
||||
&entry.download.filename,
|
||||
@@ -82,7 +74,7 @@ impl MainTui {
|
||||
}
|
||||
self.state
|
||||
.downloads
|
||||
.retain(|entry| !is_finished(&entry.download));
|
||||
.retain(|entry| !entry.download.is_finished());
|
||||
let len = self.state.downloads.len();
|
||||
if len == 0 {
|
||||
self.state.downloads_table_state.select(None);
|
||||
@@ -139,7 +131,7 @@ impl MainTui {
|
||||
// A finished transfer may exist only in this window (restored from
|
||||
// an earlier run), so the session's "nothing to forget" is not a
|
||||
// reason to keep the row.
|
||||
DownloadStatus::Completed => {
|
||||
DownloadStatus::Completed | DownloadStatus::Cancelled => {
|
||||
self.client
|
||||
.remove_download(&download.username, &download.filename);
|
||||
true
|
||||
@@ -214,29 +206,43 @@ impl MainTui {
|
||||
self.state.results_selected_indices.clear();
|
||||
}
|
||||
|
||||
/// Cancel the selected transfer when it is an upload row (uploads are
|
||||
/// listed after the downloads in the shared pane).
|
||||
pub(super) fn cancel_selected_upload(&self) {
|
||||
let Some(index) = self.state.downloads_table_state.selected() else {
|
||||
return;
|
||||
};
|
||||
let Some(upload) = self
|
||||
.state
|
||||
.uploads
|
||||
.get(index.wrapping_sub(self.state.downloads.len()))
|
||||
else {
|
||||
return;
|
||||
};
|
||||
if upload.status == soulseek_rs::types::UploadStatus::InProgress
|
||||
&& !self
|
||||
.client
|
||||
.cancel_upload(&upload.username, &upload.filename)
|
||||
{
|
||||
soulseek_rs::warn!(
|
||||
"No in-progress upload of {} to {} to cancel",
|
||||
upload.filename,
|
||||
upload.username
|
||||
);
|
||||
pub(super) fn cancel_selected_transfer(&self) {
|
||||
let selected = selected_transfer(
|
||||
self.state.downloads_table_state.selected(),
|
||||
&self.state.downloads,
|
||||
&self.state.uploads,
|
||||
);
|
||||
match selected {
|
||||
Some(SelectedTransfer::Download(entry))
|
||||
if !entry.download.is_finished() =>
|
||||
{
|
||||
let download = &entry.download;
|
||||
if !self
|
||||
.client
|
||||
.cancel_download(&download.username, &download.filename)
|
||||
{
|
||||
soulseek_rs::warn!(
|
||||
"No download of {} from {} to cancel",
|
||||
download.filename,
|
||||
download.username
|
||||
);
|
||||
}
|
||||
}
|
||||
Some(SelectedTransfer::Upload(upload))
|
||||
if upload.status == UploadStatus::InProgress =>
|
||||
{
|
||||
let cancelled = self
|
||||
.client
|
||||
.cancel_upload(&upload.username, &upload.filename);
|
||||
if !cancelled {
|
||||
soulseek_rs::warn!(
|
||||
"No in-progress upload of {} to {} to cancel",
|
||||
upload.filename,
|
||||
upload.username
|
||||
);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -422,7 +422,7 @@ impl MainTui {
|
||||
cycle(&mut self.state.downloads_table_state, rows, true);
|
||||
}
|
||||
KeyCode::Char('x') => {
|
||||
self.cancel_selected_upload();
|
||||
self.cancel_selected_transfer();
|
||||
}
|
||||
KeyCode::Char('p') => {
|
||||
self.toggle_selected_download_pause();
|
||||
|
||||
@@ -289,6 +289,8 @@ mod tests {
|
||||
download_dir_set: std::sync::Mutex<Option<String>>,
|
||||
/// What `set_shared_directories` was last asked for, if anything.
|
||||
shares_set: std::sync::Mutex<Option<Vec<String>>>,
|
||||
download_cancelled: std::sync::Mutex<Option<(String, String)>>,
|
||||
upload_cancelled: std::sync::Mutex<Option<(String, String)>>,
|
||||
}
|
||||
|
||||
fn queued(username: &str, filename: &str) -> soulseek_rs::types::Download {
|
||||
@@ -432,8 +434,15 @@ mod tests {
|
||||
fn take_upload_events(&self) -> Vec<soulseek_rs::UploadInfo> {
|
||||
Vec::new()
|
||||
}
|
||||
fn cancel_upload(&self, _u: &str, _f: &str) -> bool {
|
||||
false
|
||||
fn cancel_upload(&self, u: &str, f: &str) -> bool {
|
||||
*self.upload_cancelled.lock().expect("not poisoned") =
|
||||
Some((u.to_string(), f.to_string()));
|
||||
true
|
||||
}
|
||||
fn cancel_download(&self, u: &str, f: &str) -> bool {
|
||||
*self.download_cancelled.lock().expect("not poisoned") =
|
||||
Some((u.to_string(), f.to_string()));
|
||||
true
|
||||
}
|
||||
fn set_upload_slots(&self, _slots: usize) {}
|
||||
fn check_privileges(&self) -> soulseek_rs::Result<()> {
|
||||
@@ -558,6 +567,106 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn x_cancels_the_highlighted_download() {
|
||||
let session = Arc::new(TalkativeSession::default());
|
||||
let mut tui = attach(session.clone());
|
||||
tui.state.downloads.push(crate::models::DownloadEntry {
|
||||
download: at_status(
|
||||
"bob",
|
||||
"song.mp3",
|
||||
soulseek_rs::DownloadStatus::InProgress {
|
||||
bytes_downloaded: 1,
|
||||
total_bytes: 4096,
|
||||
speed_bytes_per_sec: 1.0,
|
||||
},
|
||||
),
|
||||
receiver: None,
|
||||
});
|
||||
tui.state.downloads_table_state.select(Some(0));
|
||||
tui.state.focused_pane = FocusedPane::Downloads;
|
||||
|
||||
tui.handle_key_event(key('x'));
|
||||
|
||||
assert_eq!(
|
||||
*session.download_cancelled.lock().expect("not poisoned"),
|
||||
Some(("bob".to_string(), "song.mp3".to_string()))
|
||||
);
|
||||
assert_eq!(
|
||||
*session.upload_cancelled.lock().expect("not poisoned"),
|
||||
None
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn d_clears_a_cancelled_row() {
|
||||
let mut tui = with_session(TalkativeSession::default());
|
||||
tui.state.downloads.push(crate::models::DownloadEntry {
|
||||
download: at_status(
|
||||
"bob",
|
||||
"gone.mp3",
|
||||
soulseek_rs::DownloadStatus::Cancelled,
|
||||
),
|
||||
receiver: None,
|
||||
});
|
||||
tui.state.downloads_table_state.select(Some(0));
|
||||
tui.state.focused_pane = FocusedPane::Downloads;
|
||||
|
||||
tui.handle_key_event(key('d'));
|
||||
|
||||
assert!(tui.state.downloads.is_empty(), "the row goes");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn x_on_a_finished_download_asks_nothing() {
|
||||
let session = Arc::new(TalkativeSession::default());
|
||||
let mut tui = attach(session.clone());
|
||||
tui.state.downloads.push(crate::models::DownloadEntry {
|
||||
download: completed("bob", "done.mp3"),
|
||||
receiver: None,
|
||||
});
|
||||
tui.state.downloads_table_state.select(Some(0));
|
||||
tui.state.focused_pane = FocusedPane::Downloads;
|
||||
|
||||
tui.handle_key_event(key('x'));
|
||||
|
||||
assert_eq!(
|
||||
*session.download_cancelled.lock().expect("not poisoned"),
|
||||
None
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn x_on_an_upload_row_still_cancels_the_upload() {
|
||||
let session = Arc::new(TalkativeSession::default());
|
||||
let mut tui = attach(session.clone());
|
||||
tui.state.downloads.push(crate::models::DownloadEntry {
|
||||
download: queued("bob", "song.mp3"),
|
||||
receiver: None,
|
||||
});
|
||||
tui.state.uploads.push(soulseek_rs::types::UploadInfo {
|
||||
username: "alice".to_string(),
|
||||
filename: "served.mp3".to_string(),
|
||||
size: 10,
|
||||
bytes_sent: 1,
|
||||
speed_bytes_per_sec: 1.0,
|
||||
status: soulseek_rs::types::UploadStatus::InProgress,
|
||||
});
|
||||
tui.state.downloads_table_state.select(Some(1));
|
||||
tui.state.focused_pane = FocusedPane::Downloads;
|
||||
|
||||
tui.handle_key_event(key('x'));
|
||||
|
||||
assert_eq!(
|
||||
*session.upload_cancelled.lock().expect("not poisoned"),
|
||||
Some(("alice".to_string(), "served.mp3".to_string()))
|
||||
);
|
||||
assert_eq!(
|
||||
*session.download_cancelled.lock().expect("not poisoned"),
|
||||
None
|
||||
);
|
||||
}
|
||||
|
||||
fn with_session(session: TalkativeSession) -> MainTui {
|
||||
attach(Arc::new(session))
|
||||
}
|
||||
|
||||
@@ -392,6 +392,7 @@ impl MainTui {
|
||||
FocusedPane::Downloads => {
|
||||
vec![
|
||||
("p", "pause/resume"),
|
||||
("x", "cancel"),
|
||||
("r", "retry failed"),
|
||||
("d", "delete queued/done"),
|
||||
("c", "clear finished"),
|
||||
|
||||
Reference in New Issue
Block a user