feat: impl delete

This commit is contained in:
Kristofers Solo 2024-02-22 11:16:26 +02:00
parent eb43995718
commit ad2cccb908
3 changed files with 54 additions and 47 deletions

View File

@ -1,6 +1,7 @@
use std::{collections::HashSet, path::Path, vec}; use std::{collections::HashSet, path::Path};
use transmission_rpc::types::{Id, Torrent, TorrentAction, TorrentStatus}; use tracing::error;
use transmission_rpc::types::{Torrent, TorrentAction, TorrentStatus};
use super::{types::Selected, Torrents}; use super::{types::Selected, Torrents};
@ -20,77 +21,81 @@ impl Torrents {
_ => TorrentAction::Stop, _ => TorrentAction::Stop,
}; };
if let Some(id) = torrent.id() { if let Some(id) = torrent.id() {
self.client.torrent_action(action, vec![id]).await.unwrap(); if let Err(e) = self.client.torrent_action(action, vec![id]).await {
error!("{:?}", e);
}
} }
} }
} }
pub async fn toggle_all(&mut self) { pub async fn toggle_all(&mut self) {
let torrents: Vec<(Id, TorrentAction)> = self let torrents: Vec<_> = self
.torrents .torrents
.iter() .iter()
.map(|torrent| { .filter_map(|torrent| {
( torrent.id().map(|id| {
torrent.id().unwrap(), (
match torrent.status { id,
Some(TorrentStatus::Stopped) => TorrentAction::StartNow, match torrent.status {
_ => TorrentAction::Stop, Some(TorrentStatus::Stopped) => TorrentAction::StartNow,
}, _ => TorrentAction::Stop,
) },
)
})
}) })
.collect(); .collect();
for (id, action) in torrents { for (id, action) in torrents {
self.client if let Err(e) = self.client.torrent_action(action, vec![id]).await {
.torrent_action(action, vec![id]) error!("{:?}", e);
.await }
.expect("Error toggling torrent");
} }
} }
pub async fn start_all(&mut self) { pub async fn start_all(&mut self) {
self.action_all(TorrentAction::StartNow) if let Err(e) = self.action_all(TorrentAction::StartNow).await {
.await error!("{:?}", e);
.expect("Error starting all torrents"); }
} }
pub async fn stop_all(&mut self) { pub async fn stop_all(&mut self) {
self.action_all(TorrentAction::Stop) if let Err(e) = self.action_all(TorrentAction::Stop).await {
.await error!("{:?}", e);
.expect("Error stopping all torrents"); }
} }
pub async fn move_dir( pub async fn move_dir(&mut self, torrent: &Torrent, location: &Path, move_from: Option<bool>) {
&mut self, if let Some(id) = torrent.id() {
torrent: &Torrent, if let Err(e) = self
location: &Path, .client
move_from: Option<bool>, .torrent_set_location(vec![id], location.to_string_lossy().into(), move_from)
) -> transmission_rpc::types::Result<()> { .await
let id = torrent.id().expect("ID not found"); {
self.client error!("{:?}", e);
.torrent_set_location(vec![id], location.to_string_lossy().into(), move_from) }
.await?; }
Ok(())
} }
pub async fn delete(&mut self, ids: Selected, delete_local_data: bool) { pub async fn delete(&mut self, ids: Selected, delete_local_data: bool) {
self.client if let Err(e) = self
.client
.torrent_remove(ids.into(), delete_local_data) .torrent_remove(ids.into(), delete_local_data)
.await .await
.unwrap(); {
error!("{:?}", e);
}
} }
pub async fn rename( pub async fn rename(&mut self, torrent: &Torrent, name: &Path) {
&mut self, if let (Some(id), Some(old_name)) = (torrent.id(), torrent.name.clone()) {
torrent: &Torrent, if let Err(e) = self
name: &Path, .client
) -> transmission_rpc::types::Result<()> { .torrent_rename_path(vec![id], old_name, name.to_string_lossy().into())
let id = torrent.id().expect("ID not found"); .await
let old_name = torrent.name.clone().unwrap(); {
self.client error!("{:?}", e);
.torrent_rename_path(vec![id], old_name, name.to_string_lossy().into()) }
.await?; }
Ok(())
} }
async fn action_all(&mut self, action: TorrentAction) -> transmission_rpc::types::Result<()> { async fn action_all(&mut self, action: TorrentAction) -> transmission_rpc::types::Result<()> {

View File

@ -13,7 +13,7 @@ pub struct Torrents {
/// Constructs a new instance of [`Torrents`]. /// Constructs a new instance of [`Torrents`].
pub client: TransClient, pub client: TransClient,
pub torrents: Vec<Torrent>, pub torrents: Vec<Torrent>,
pub selected: HashSet<i64>, // Torrent::id pub selected: HashSet<i64>,
pub fields: Option<Vec<TorrentGetField>>, pub fields: Option<Vec<TorrentGetField>>,
} }

View File

@ -21,6 +21,8 @@ pub fn get_action(key_event: KeyEvent) -> Option<Action> {
KeyCode::Char('3') => Some(Action::SwitchTab(2)), KeyCode::Char('3') => Some(Action::SwitchTab(2)),
KeyCode::Char('t') | KeyCode::Enter | KeyCode::Menu => Some(Action::ToggleTorrent), KeyCode::Char('t') | KeyCode::Enter | KeyCode::Menu => Some(Action::ToggleTorrent),
KeyCode::Char('a') => Some(Action::ToggleAll), KeyCode::Char('a') => Some(Action::ToggleAll),
KeyCode::Char('d') => Some(Action::Delete(false)),
KeyCode::Char('D') => Some(Action::Delete(true)),
KeyCode::Char(' ') => Some(Action::Select), KeyCode::Char(' ') => Some(Action::Select),
// Other handlers you could add here. // Other handlers you could add here.
_ => None, _ => None,