| 1 |
// Fragments - actions.rs |
| 2 |
// Copyright (C) 2023-2024 Felix Häcker <[email protected]> |
| 3 |
// |
| 4 |
// This program is free software: you can redistribute it and/or modify |
| 5 |
// it under the terms of the GNU General Public License as published by |
| 6 |
// the Free Software Foundation, either version 3 of the License, or |
| 7 |
// (at your option) any later version. |
| 8 |
// |
| 9 |
// This program is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
// |
| 14 |
// You should have received a copy of the GNU General Public License |
| 15 |
// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
use std::fs::File; |
| 18 |
use std::os::fd::AsFd; |
| 19 |
use std::path::{Path, PathBuf}; |
| 20 |
|
| 21 |
use adw::prelude::*; |
| 22 |
use ashpd::desktop::open_uri::{OpenDirectoryRequest, OpenFileRequest}; |
| 23 |
use ashpd::WindowIdentifier; |
| 24 |
use gio::{SimpleAction, SimpleActionGroup}; |
| 25 |
use glib::clone; |
| 26 |
use gtk::{gdk, gio, glib}; |
| 27 |
use transmission_gobject::{TrFile, TrTorrent, TrTorrentStatus}; |
| 28 |
|
| 29 |
use crate::backend::FrgConnectionManager; |
| 30 |
use crate::i18n::i18n; |
| 31 |
use crate::ui::FrgTorrentDialog; |
| 32 |
use crate::utils; |
| 33 |
|
| 34 |
pub fn install_torrent_actions(torrent: &TrTorrent, widget: gtk::Widget) { |
| 35 |
let current_connection = FrgConnectionManager::default().current_connection(); |
| 36 |
let actions = SimpleActionGroup::new(); |
| 37 |
widget.insert_action_group("torrent", Some(&actions)); |
| 38 |
|
| 39 |
// torrent.continue |
| 40 |
let continue_action = SimpleAction::new("continue", None); |
| 41 |
continue_action.connect_activate(clone!(@weak torrent => move |_, _| { |
| 42 |
let fut = async move { |
| 43 |
torrent.start(false).await.expect("Unable to start torrent"); |
| 44 |
}; |
| 45 |
glib::spawn_future_local(fut); |
| 46 |
})); |
| 47 |
actions.add_action(&continue_action); |
| 48 |
|
| 49 |
// torrent.pause |
| 50 |
let pause_action = SimpleAction::new("pause", None); |
| 51 |
pause_action.connect_activate(clone!(@weak torrent => move |_, _| { |
| 52 |
let fut = async move { |
| 53 |
torrent.stop().await.expect("Unable to stop torrent"); |
| 54 |
}; |
| 55 |
glib::spawn_future_local(fut); |
| 56 |
})); |
| 57 |
actions.add_action(&pause_action); |
| 58 |
|
| 59 |
// Disable `continue`/`pause` action depending on the torrent status |
| 60 |
torrent.connect_status_notify( |
| 61 |
clone!(@weak torrent, @weak continue_action, @weak pause_action => move |_|{ |
| 62 |
update_status_action(&torrent, &continue_action, &pause_action); |
| 63 |
}), |
| 64 |
); |
| 65 |
update_status_action(torrent, &continue_action, &pause_action); |
| 66 |
|
| 67 |
// torrent.remove |
| 68 |
let remove_action = SimpleAction::new("remove", None); |
| 69 |
remove_action.connect_activate(clone!(@weak torrent, @weak widget => move |_, _| { |
| 70 |
show_remove_torrent_dialog(&torrent, widget); |
| 71 |
})); |
| 72 |
actions.add_action(&remove_action); |
| 73 |
|
| 74 |
// torrent.copy-magnet |
| 75 |
let copy_magnet_action = SimpleAction::new("copy-magnet", None); |
| 76 |
copy_magnet_action.connect_activate(clone!(@weak torrent, @weak widget => move |_, _| { |
| 77 |
let display = gdk::Display::default().unwrap(); |
| 78 |
let clipboard = display.clipboard(); |
| 79 |
|
| 80 |
let content = gdk::ContentProvider::for_value(&torrent.magnet_link().to_value()); |
| 81 |
if let Err(err) = clipboard.set_content(Some(&content)){ |
| 82 |
utils::inapp_notification(&i18n("Unable to copy magnet link to clipboard"), Some(&widget)); |
| 83 |
warn!("Unable to copy magnet link to clipboard: {}", err.to_string()); |
| 84 |
} |
| 85 |
|
| 86 |
utils::inapp_notification(&i18n("Copied magnet link to clipboard"), Some(&widget)); |
| 87 |
})); |
| 88 |
actions.add_action(©_magnet_action); |
| 89 |
|
| 90 |
// torrent.manual-update |
| 91 |
let manual_update_action = SimpleAction::new("manual-update", None); |
| 92 |
manual_update_action.connect_activate(clone!(@weak torrent => move |_, _| { |
| 93 |
let fut = async move { |
| 94 |
torrent.reannounce().await.expect("Unable to reannounce torrent"); |
| 95 |
}; |
| 96 |
glib::spawn_future_local(fut); |
| 97 |
})); |
| 98 |
actions.add_action(&manual_update_action); |
| 99 |
|
| 100 |
// torrent.queue-up |
| 101 |
let queue_up_action = SimpleAction::new("queue-up", None); |
| 102 |
queue_up_action.connect_activate(clone!(@weak torrent => move |_, _| { |
| 103 |
move_queue(false, torrent); |
| 104 |
})); |
| 105 |
actions.add_action(&queue_up_action); |
| 106 |
|
| 107 |
// torrent.queue-down |
| 108 |
let queue_down_action = SimpleAction::new("queue-down", None); |
| 109 |
queue_down_action.connect_activate(clone!(@weak torrent => move |_, _| { |
| 110 |
move_queue(true, torrent); |
| 111 |
})); |
| 112 |
actions.add_action(&queue_down_action); |
| 113 |
|
| 114 |
// Disable queue actions for non queued torrents |
| 115 |
torrent.connect_status_notify( |
| 116 |
clone!(@weak torrent, @weak queue_up_action, @weak queue_down_action => move |_|{ |
| 117 |
update_queue_action(&torrent, &queue_up_action, &queue_down_action); |
| 118 |
}), |
| 119 |
); |
| 120 |
update_queue_action(torrent, &queue_up_action, &queue_down_action); |
| 121 |
|
| 122 |
// torrent.show-dialog |
| 123 |
let show_dialog_action = SimpleAction::new("show-dialog", None); |
| 124 |
show_dialog_action.connect_activate(clone!(@weak torrent, @weak widget => move |_, _| { |
| 125 |
let dialog = FrgTorrentDialog::new(&torrent); |
| 126 |
dialog.present(Some(&widget)); |
| 127 |
})); |
| 128 |
actions.add_action(&show_dialog_action); |
| 129 |
|
| 130 |
// torrent.open |
| 131 |
let open_action = SimpleAction::new("open", None); |
| 132 |
open_action.connect_activate(clone!(@weak torrent, @weak widget => move |_, _| { |
| 133 |
let name = if let Some(top_level) = torrent.files().top_level() { |
| 134 |
top_level.name() |
| 135 |
} else { |
| 136 |
warn!("Top level file is not available, falling back to torrent name for opening"); |
| 137 |
torrent.name() |
| 138 |
}; |
| 139 |
|
| 140 |
let path = Path::new(&torrent.download_dir()).join(name); |
| 141 |
open(path, &widget.native().unwrap(), false, Some(widget)); |
| 142 |
})); |
| 143 |
actions.add_action(&open_action); |
| 144 |
|
| 145 |
// Only downloaded torrents can be opened |
| 146 |
torrent.connect_downloaded_notify(clone!(@weak torrent, @weak open_action => move |_|{ |
| 147 |
update_torrent_open_action(&torrent, &open_action); |
| 148 |
})); |
| 149 |
update_torrent_open_action(torrent, &open_action); |
| 150 |
|
| 151 |
// torrent.set-location |
| 152 |
let set_location_action = SimpleAction::new("set-location", None); |
| 153 |
set_location_action.connect_activate(clone!(@weak torrent, @weak widget => move |_, _| { |
| 154 |
show_set_torrent_location(&torrent, &widget); |
| 155 |
} |
| 156 |
)); |
| 157 |
set_location_action.set_enabled(current_connection.is_fragments()); |
| 158 |
actions.add_action(&set_location_action); |
| 159 |
|
| 160 |
// Disable filesystem actions when connected with a remote session |
| 161 |
current_connection.connect_is_fragments_notify( |
| 162 |
clone!(@weak torrent, @weak open_action, @weak set_location_action => move |c|{ |
| 163 |
set_location_action.set_enabled(c.is_fragments()); |
| 164 |
update_torrent_open_action(&torrent, &open_action); |
| 165 |
}), |
| 166 |
); |
| 167 |
} |
| 168 |
|
| 169 |
pub fn install_file_actions(file: &TrFile, widget: gtk::Widget) { |
| 170 |
let current_connection = FrgConnectionManager::default().current_connection(); |
| 171 |
|
| 172 |
let actions = SimpleActionGroup::new(); |
| 173 |
widget.insert_action_group("file", Some(&actions)); |
| 174 |
|
| 175 |
// file.open |
| 176 |
let open_action = SimpleAction::new("open", None); |
| 177 |
open_action.connect_activate(clone!(@weak file, @weak widget => move |_, _| { |
| 178 |
let path = Path::new(&file.torrent().download_dir()).join(file.name()); |
| 179 |
open(path, &widget.native().unwrap(), false, Some(widget)); |
| 180 |
})); |
| 181 |
actions.add_action(&open_action); |
| 182 |
|
| 183 |
// file.open-containing-folder |
| 184 |
let open_containing_action = SimpleAction::new("open-containing-folder", None); |
| 185 |
open_containing_action.connect_activate(clone!(@weak file, @weak widget => move |_, _| { |
| 186 |
let path = Path::new(&file.torrent().download_dir()).join(file.name()); |
| 187 |
open(path, &widget.native().unwrap(), true, Some(widget)); |
| 188 |
})); |
| 189 |
actions.add_action(&open_containing_action); |
| 190 |
|
| 191 |
// Only downloaded files can be opened |
| 192 |
file.connect_bytes_completed_notify(clone!(@weak file, @weak actions => move |_|{ |
| 193 |
update_file_open_actions(&file, &actions); |
| 194 |
})); |
| 195 |
update_file_open_actions(file, &actions); |
| 196 |
|
| 197 |
// Disable filesystem actions when connected with a remote session |
| 198 |
current_connection.connect_is_fragments_notify( |
| 199 |
clone!(@weak file, @weak open_action, @weak actions => move |c|{ |
| 200 |
open_containing_action.set_enabled(c.is_fragments()); |
| 201 |
update_file_open_actions(&file, &actions); |
| 202 |
}), |
| 203 |
); |
| 204 |
} |
| 205 |
|
| 206 |
fn move_queue(down: bool, torrent: TrTorrent) { |
| 207 |
let fut = clone!(@weak torrent => async move { |
| 208 |
let pos = if down{ |
| 209 |
torrent.download_queue_position() + 1 |
| 210 |
}else{ |
| 211 |
torrent.download_queue_position() - 1 |
| 212 |
}; |
| 213 |
torrent.set_download_queue_position(pos).await.expect("Unable to set download queue position"); |
| 214 |
}); |
| 215 |
glib::spawn_future_local(fut); |
| 216 |
} |
| 217 |
|
| 218 |
fn update_status_action( |
| 219 |
torrent: &TrTorrent, |
| 220 |
continue_action: &SimpleAction, |
| 221 |
pause_action: &SimpleAction, |
| 222 |
) { |
| 223 |
match torrent.status() { |
| 224 |
TrTorrentStatus::Stopped => { |
| 225 |
continue_action.set_enabled(true); |
| 226 |
pause_action.set_enabled(false); |
| 227 |
} |
| 228 |
_ => { |
| 229 |
continue_action.set_enabled(false); |
| 230 |
pause_action.set_enabled(true); |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
fn update_queue_action( |
| 236 |
torrent: &TrTorrent, |
| 237 |
queue_up_action: &SimpleAction, |
| 238 |
queue_down_action: &SimpleAction, |
| 239 |
) { |
| 240 |
let enable = matches!(torrent.status(), TrTorrentStatus::DownloadWait); |
| 241 |
queue_up_action.set_enabled(enable); |
| 242 |
queue_down_action.set_enabled(enable); |
| 243 |
} |
| 244 |
|
| 245 |
fn update_torrent_open_action(torrent: &TrTorrent, action: &SimpleAction) { |
| 246 |
let current_connection = FrgConnectionManager::default().current_connection(); |
| 247 |
let is_fragments = current_connection.is_fragments(); |
| 248 |
let is_downloaded = torrent.size() == torrent.downloaded(); |
| 249 |
|
| 250 |
action.set_enabled(is_fragments && is_downloaded); |
| 251 |
} |
| 252 |
|
| 253 |
fn update_file_open_actions(file: &TrFile, actions: &SimpleActionGroup) { |
| 254 |
let current_connection = FrgConnectionManager::default().current_connection(); |
| 255 |
let is_fragments = current_connection.is_fragments(); |
| 256 |
let is_downloaded = file.length() == file.bytes_completed(); |
| 257 |
|
| 258 |
for action_name in actions.list_actions() { |
| 259 |
let action: SimpleAction = actions |
| 260 |
.lookup_action(&action_name) |
| 261 |
.unwrap() |
| 262 |
.downcast() |
| 263 |
.unwrap(); |
| 264 |
action.set_enabled(is_fragments && is_downloaded); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
fn show_remove_torrent_dialog(torrent: &TrTorrent, parent: gtk::Widget) { |
| 269 |
let heading = i18n("Remove Torrent?"); |
| 270 |
let body = |
| 271 |
i18n("Once removed, continuing the transfer will require the torrent file or magnet link."); |
| 272 |
|
| 273 |
let dialog = adw::AlertDialog::new(Some(&heading), Some(&body)); |
| 274 |
|
| 275 |
dialog.add_response("cancel", &i18n("_Cancel")); |
| 276 |
dialog.set_default_response(Some("cancel")); |
| 277 |
dialog.add_response("remove", &i18n("_Remove")); |
| 278 |
dialog.set_response_appearance("remove", adw::ResponseAppearance::Destructive); |
| 279 |
|
| 280 |
// Check button |
| 281 |
let check_button = gtk::CheckButton::with_label(&i18n("Remove downloaded data as well")); |
| 282 |
check_button.set_halign(gtk::Align::Center); |
| 283 |
dialog.set_extra_child(Some(&check_button)); |
| 284 |
|
| 285 |
dialog.connect_response(None, |
| 286 |
clone!(@strong dialog, @weak torrent, @weak parent, @weak check_button => move |_ , resp| { |
| 287 |
if resp == "remove" { |
| 288 |
let fut = async move { |
| 289 |
torrent.remove(check_button.is_active()).await.expect("Unable to remove torrent"); |
| 290 |
}; |
| 291 |
|
| 292 |
if let Ok(dialog) = parent.downcast::<adw::Dialog>(){ |
| 293 |
dialog.close(); |
| 294 |
} |
| 295 |
|
| 296 |
glib::spawn_future_local(fut); |
| 297 |
} |
| 298 |
|
| 299 |
dialog.close(); |
| 300 |
}), |
| 301 |
); |
| 302 |
|
| 303 |
dialog.present(Some(&parent)); |
| 304 |
} |
| 305 |
|
| 306 |
// TODO: Consider rewriting this as async to get rid of the nested closures |
| 307 |
fn show_set_torrent_location(torrent: &TrTorrent, parent: >k::Widget) { |
| 308 |
let title = i18n("Set Torrent Location"); |
| 309 |
|
| 310 |
let dialog = gtk::FileDialog::new(); |
| 311 |
dialog.set_title(&title); |
| 312 |
dialog.set_accept_label(Some(&i18n("_Select"))); |
| 313 |
|
| 314 |
let root = parent.root().unwrap().downcast::<gtk::Window>().unwrap(); |
| 315 |
dialog.select_folder(Some(&root), gio::Cancellable::NONE, clone!(@weak torrent, @weak parent, @strong dialog => move |result| { |
| 316 |
match result { |
| 317 |
Ok(folder) => { |
| 318 |
debug!("Selected torrent directory: {:?}", folder.path()); |
| 319 |
|
| 320 |
if torrent.error() == 0 { |
| 321 |
let heading = i18n("Move Data?"); |
| 322 |
let body = i18n("In order to continue using the torrent, the data must be available at the new location."); |
| 323 |
|
| 324 |
let dialog = adw::AlertDialog::new(Some(&heading), Some(&body)); |
| 325 |
dialog.add_response("keep-data", &i18n("_Only Change Location")); |
| 326 |
dialog.add_response("move-data", &i18n("_Move Data to New Location")); |
| 327 |
dialog.set_default_response(Some("move-data")); |
| 328 |
dialog.set_response_appearance("move-data", adw::ResponseAppearance::Suggested); |
| 329 |
|
| 330 |
dialog.present(Some(&parent)); |
| 331 |
dialog.connect_response( |
| 332 |
None, |
| 333 |
clone!(@strong dialog, @strong torrent, @strong folder => move |_ , resp| { |
| 334 |
let resp = resp.to_string(); |
| 335 |
let fut = clone!(@strong resp, @strong torrent, @strong folder => async move { |
| 336 |
let _ = torrent.set_location(folder, resp == "move-data").await; |
| 337 |
}); |
| 338 |
|
| 339 |
glib::spawn_future_local(fut); |
| 340 |
}), |
| 341 |
); |
| 342 |
} else { |
| 343 |
let fut = async move { |
| 344 |
// If there is a problem with the torrent, it is most likely because the local |
| 345 |
// downloaded file is no longer available. Therefore, do not offer the option |
| 346 |
// to move the data in the first place. |
| 347 |
let _ = torrent.set_location(folder, false).await; |
| 348 |
// Automatically start torrent again so the error message disappears (hopefully) |
| 349 |
let _ = torrent.start(false).await; |
| 350 |
}; |
| 351 |
glib::spawn_future_local(fut); |
| 352 |
} |
| 353 |
} |
| 354 |
Err(err) => { |
| 355 |
warn!("Selected directory could not be accessed {err}"); |
| 356 |
} |
| 357 |
} |
| 358 |
})); |
| 359 |
} |
| 360 |
|
| 361 |
fn open( |
| 362 |
path: PathBuf, |
| 363 |
native: >k::Native, |
| 364 |
open_containing_folder: bool, |
| 365 |
widget: Option<gtk::Widget>, |
| 366 |
) { |
| 367 |
let fut = clone!(@strong path, @weak native, @strong open_containing_folder, @strong widget => async move { |
| 368 |
debug!("Opening: {:?} (containing folder: {})", path, open_containing_folder); |
| 369 |
match File::open(&path){ |
| 370 |
Ok(file) => { |
| 371 |
let identifier = WindowIdentifier::from_native(&native).await; |
| 372 |
|
| 373 |
let res = if open_containing_folder || path.is_dir() { |
| 374 |
OpenDirectoryRequest::default().identifier(identifier).send(&file.as_fd()).await |
| 375 |
} else { |
| 376 |
OpenFileRequest::default().identifier(identifier).ask(true).send_file(&file.as_fd()).await |
| 377 |
}; |
| 378 |
|
| 379 |
if let Err(err) = res { |
| 380 |
warn!("Could not open folder / file: {}", err.to_string()); |
| 381 |
utils::inapp_notification(&i18n("Unable to open file / folder"), widget.as_ref()); |
| 382 |
} |
| 383 |
}, |
| 384 |
Err(err) => { |
| 385 |
warn!("Could not open file: {}", err.to_string()); |
| 386 |
utils::inapp_notification(&i18n("Could not open file"), widget.as_ref()); |
| 387 |
} |
| 388 |
} |
| 389 |
}); |
| 390 |
glib::spawn_future_local(fut); |
| 391 |
} |