| 1 |
// Fragments - actions.rs |
| •2 |
// Copyright (C) 2023-2025 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!( |
| 42 |
#[weak] |
| 43 |
torrent, |
| 44 |
move |_, _| { |
| 45 |
let fut = async move { |
| 46 |
torrent.start(false).await.expect("Unable to start torrent"); |
| 47 |
}; |
| 48 |
glib::spawn_future_local(fut); |
| 49 |
} |
| 50 |
)); |
| 51 |
actions.add_action(&continue_action); |
| 52 |
|
| 53 |
// torrent.pause |
| 54 |
let pause_action = SimpleAction::new("pause", None); |
| •55 |
pause_action.connect_activate(clone!( |
| 56 |
#[weak] |
| 57 |
torrent, |
| 58 |
move |_, _| { |
| 59 |
let fut = async move { |
| 60 |
torrent.stop().await.expect("Unable to stop torrent"); |
| 61 |
}; |
| 62 |
glib::spawn_future_local(fut); |
| 63 |
} |
| 64 |
)); |
| 65 |
actions.add_action(&pause_action); |
| 66 |
|
| 67 |
// Disable `continue`/`pause` action depending on the torrent status |
| •68 |
torrent.connect_status_notify(clone!( |
| 69 |
#[weak] |
| 70 |
torrent, |
| 71 |
#[weak] |
| 72 |
continue_action, |
| 73 |
#[weak] |
| 74 |
pause_action, |
| 75 |
move |_| { |
| 76 |
update_status_action(&torrent, &continue_action, &pause_action); |
| •77 |
} |
| 78 |
)); |
| 79 |
update_status_action(torrent, &continue_action, &pause_action); |
| 80 |
|
| 81 |
// torrent.remove |
| 82 |
let remove_action = SimpleAction::new("remove", None); |
| •83 |
remove_action.connect_activate(clone!( |
| 84 |
#[weak] |
| 85 |
torrent, |
| 86 |
#[weak] |
| 87 |
widget, |
| 88 |
move |_, _| { |
| 89 |
show_remove_torrent_dialog(&torrent, widget); |
| 90 |
} |
| 91 |
)); |
| 92 |
actions.add_action(&remove_action); |
| 93 |
|
| 94 |
// torrent.copy-magnet |
| 95 |
let copy_magnet_action = SimpleAction::new("copy-magnet", None); |
| •96 |
copy_magnet_action.connect_activate(clone!( |
| 97 |
#[weak] |
| 98 |
torrent, |
| 99 |
#[weak] |
| 100 |
widget, |
| 101 |
move |_, _| { |
| 102 |
let display = gdk::Display::default().unwrap(); |
| 103 |
let clipboard = display.clipboard(); |
| 104 |
|
| 105 |
let content = gdk::ContentProvider::for_value(&torrent.magnet_link().to_value()); |
| 106 |
if let Err(err) = clipboard.set_content(Some(&content)) { |
| 107 |
utils::inapp_notification( |
| 108 |
&i18n("Unable to copy magnet link to clipboard"), |
| 109 |
Some(&widget), |
| 110 |
); |
| 111 |
warn!( |
| 112 |
"Unable to copy magnet link to clipboard: {}", |
| 113 |
err.to_string() |
| 114 |
); |
| 115 |
} |
| 116 |
|
| •117 |
utils::inapp_notification(&i18n("Copied magnet link to clipboard"), Some(&widget)); |
| 118 |
} |
| 119 |
)); |
| 120 |
actions.add_action(©_magnet_action); |
| 121 |
|
| 122 |
// torrent.manual-update |
| 123 |
let manual_update_action = SimpleAction::new("manual-update", None); |
| •124 |
manual_update_action.connect_activate(clone!( |
| 125 |
#[weak] |
| •126 |
torrent, |
| 127 |
move |_, _| { |
| 128 |
let fut = async move { |
| 129 |
torrent |
| 130 |
.reannounce() |
| 131 |
.await |
| 132 |
.expect("Unable to reannounce torrent"); |
| 133 |
}; |
| 134 |
glib::spawn_future_local(fut); |
| 135 |
} |
| 136 |
)); |
| 137 |
actions.add_action(&manual_update_action); |
| 138 |
|
| 139 |
// torrent.queue-up |
| 140 |
let queue_up_action = SimpleAction::new("queue-up", None); |
| •141 |
queue_up_action.connect_activate(clone!( |
| 142 |
#[weak] |
| 143 |
torrent, |
| 144 |
move |_, _| { |
| 145 |
move_queue(false, torrent); |
| 146 |
} |
| 147 |
)); |
| 148 |
actions.add_action(&queue_up_action); |
| 149 |
|
| 150 |
// torrent.queue-down |
| 151 |
let queue_down_action = SimpleAction::new("queue-down", None); |
| •152 |
queue_down_action.connect_activate(clone!( |
| 153 |
#[weak] |
| 154 |
torrent, |
| 155 |
move |_, _| { |
| 156 |
move_queue(true, torrent); |
| 157 |
} |
| 158 |
)); |
| 159 |
actions.add_action(&queue_down_action); |
| 160 |
|
| 161 |
// Disable queue actions for non queued torrents |
| •162 |
torrent.connect_status_notify(clone!( |
| 163 |
#[weak] |
| 164 |
torrent, |
| 165 |
#[weak] |
| 166 |
queue_up_action, |
| 167 |
#[weak] |
| 168 |
queue_down_action, |
| 169 |
move |_| { |
| 170 |
update_queue_action(&torrent, &queue_up_action, &queue_down_action); |
| •171 |
} |
| 172 |
)); |
| 173 |
update_queue_action(torrent, &queue_up_action, &queue_down_action); |
| 174 |
|
| 175 |
// torrent.show-dialog |
| 176 |
let show_dialog_action = SimpleAction::new("show-dialog", None); |
| •177 |
show_dialog_action.connect_activate(clone!( |
| 178 |
#[weak] |
| 179 |
torrent, |
| 180 |
#[weak] |
| 181 |
widget, |
| 182 |
move |_, _| { |
| 183 |
let dialog = FrgTorrentDialog::new(&torrent); |
| 184 |
dialog.present(Some(&widget)); |
| 185 |
} |
| 186 |
)); |
| 187 |
actions.add_action(&show_dialog_action); |
| 188 |
|
| 189 |
// torrent.open |
| 190 |
let open_action = SimpleAction::new("open", None); |
| •191 |
open_action.connect_activate(clone!( |
| 192 |
#[weak] |
| 193 |
torrent, |
| 194 |
#[weak] |
| 195 |
widget, |
| 196 |
move |_, _| { |
| 197 |
let name = if let Some(top_level) = torrent.files().top_level() { |
| 198 |
top_level.name() |
| 199 |
} else { |
| 200 |
warn!("Top level file is not available, falling back to torrent name for opening"); |
| 201 |
torrent.name() |
| 202 |
}; |
| 203 |
|
| 204 |
let path = Path::new(&torrent.download_dir()).join(name); |
| 205 |
open(path, &widget.native().unwrap(), false, Some(widget)); |
| 206 |
} |
| 207 |
)); |
| 208 |
actions.add_action(&open_action); |
| 209 |
|
| 210 |
// Only downloaded torrents can be opened |
| •211 |
torrent.connect_downloaded_notify(clone!( |
| 212 |
#[weak] |
| 213 |
torrent, |
| 214 |
#[weak] |
| 215 |
open_action, |
| 216 |
move |_| { |
| 217 |
update_torrent_open_action(&torrent, &open_action); |
| 218 |
} |
| 219 |
)); |
| 220 |
update_torrent_open_action(torrent, &open_action); |
| 221 |
|
| 222 |
// torrent.set-location |
| 223 |
let set_location_action = SimpleAction::new("set-location", None); |
| •224 |
set_location_action.connect_activate(clone!( |
| 225 |
#[weak] |
| 226 |
torrent, |
| 227 |
#[weak] |
| 228 |
widget, |
| 229 |
move |_, _| { |
| 230 |
show_set_torrent_location(&torrent, &widget); |
| 231 |
} |
| 232 |
)); |
| 233 |
set_location_action.set_enabled(current_connection.is_fragments()); |
| 234 |
actions.add_action(&set_location_action); |
| 235 |
|
| 236 |
// Disable filesystem actions when connected with a remote session |
| •237 |
current_connection.connect_is_fragments_notify(clone!( |
| 238 |
#[weak] |
| 239 |
torrent, |
| 240 |
#[weak] |
| 241 |
open_action, |
| 242 |
#[weak] |
| 243 |
set_location_action, |
| 244 |
move |c| { |
| 245 |
set_location_action.set_enabled(c.is_fragments()); |
| 246 |
update_torrent_open_action(&torrent, &open_action); |
| •247 |
} |
| 248 |
)); |
| 249 |
} |
| 250 |
|
| 251 |
pub fn install_file_actions(file: &TrFile, widget: gtk::Widget) { |
| 252 |
let current_connection = FrgConnectionManager::default().current_connection(); |
| 253 |
|
| 254 |
let actions = SimpleActionGroup::new(); |
| 255 |
widget.insert_action_group("file", Some(&actions)); |
| 256 |
|
| 257 |
// file.open |
| 258 |
let open_action = SimpleAction::new("open", None); |
| •259 |
open_action.connect_activate(clone!( |
| 260 |
#[weak] |
| 261 |
file, |
| 262 |
#[weak] |
| 263 |
widget, |
| 264 |
move |_, _| { |
| 265 |
let path = Path::new(&file.torrent().download_dir()).join(file.name()); |
| 266 |
open(path, &widget.native().unwrap(), false, Some(widget)); |
| 267 |
} |
| 268 |
)); |
| 269 |
actions.add_action(&open_action); |
| 270 |
|
| 271 |
// file.open-containing-folder |
| 272 |
let open_containing_action = SimpleAction::new("open-containing-folder", None); |
| •273 |
open_containing_action.connect_activate(clone!( |
| 274 |
#[weak] |
| 275 |
file, |
| 276 |
#[weak] |
| 277 |
widget, |
| 278 |
move |_, _| { |
| 279 |
let path = Path::new(&file.torrent().download_dir()).join(file.name()); |
| 280 |
open(path, &widget.native().unwrap(), true, Some(widget)); |
| 281 |
} |
| 282 |
)); |
| 283 |
actions.add_action(&open_containing_action); |
| 284 |
|
| 285 |
// Only downloaded files can be opened |
| •286 |
file.connect_bytes_completed_notify(clone!( |
| 287 |
#[weak] |
| 288 |
file, |
| 289 |
#[weak] |
| 290 |
actions, |
| 291 |
move |_| { |
| 292 |
update_file_open_actions(&file, &actions); |
| 293 |
} |
| 294 |
)); |
| 295 |
update_file_open_actions(file, &actions); |
| 296 |
|
| 297 |
// Disable filesystem actions when connected with a remote session |
| •298 |
current_connection.connect_is_fragments_notify(clone!( |
| 299 |
#[weak] |
| 300 |
file, |
| 301 |
#[weak] |
| 302 |
actions, |
| 303 |
move |c| { |
| 304 |
open_containing_action.set_enabled(c.is_fragments()); |
| 305 |
update_file_open_actions(&file, &actions); |
| •306 |
} |
| 307 |
)); |
| 308 |
} |
| 309 |
|
| 310 |
fn move_queue(down: bool, torrent: TrTorrent) { |
| •311 |
let fut = clone!( |
| 312 |
#[weak] |
| 313 |
torrent, |
| 314 |
async move { |
| 315 |
let pos = if down { |
| 316 |
torrent.download_queue_position() + 1 |
| 317 |
} else { |
| 318 |
torrent.download_queue_position() - 1 |
| 319 |
}; |
| 320 |
torrent |
| 321 |
.set_download_queue_position(pos) |
| 322 |
.await |
| 323 |
.expect("Unable to set download queue position"); |
| 324 |
} |
| 325 |
); |
| 326 |
glib::spawn_future_local(fut); |
| 327 |
} |
| 328 |
|
| 329 |
fn update_status_action( |
| 330 |
torrent: &TrTorrent, |
| 331 |
continue_action: &SimpleAction, |
| 332 |
pause_action: &SimpleAction, |
| 333 |
) { |
| 334 |
match torrent.status() { |
| 335 |
TrTorrentStatus::Stopped => { |
| 336 |
continue_action.set_enabled(true); |
| 337 |
pause_action.set_enabled(false); |
| 338 |
} |
| 339 |
_ => { |
| 340 |
continue_action.set_enabled(false); |
| 341 |
pause_action.set_enabled(true); |
| 342 |
} |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
fn update_queue_action( |
| 347 |
torrent: &TrTorrent, |
| 348 |
queue_up_action: &SimpleAction, |
| 349 |
queue_down_action: &SimpleAction, |
| 350 |
) { |
| 351 |
let enable = matches!(torrent.status(), TrTorrentStatus::DownloadWait); |
| 352 |
queue_up_action.set_enabled(enable); |
| 353 |
queue_down_action.set_enabled(enable); |
| 354 |
} |
| 355 |
|
| 356 |
fn update_torrent_open_action(torrent: &TrTorrent, action: &SimpleAction) { |
| 357 |
let current_connection = FrgConnectionManager::default().current_connection(); |
| 358 |
let is_fragments = current_connection.is_fragments(); |
| 359 |
let is_downloaded = torrent.size() == torrent.downloaded(); |
| 360 |
|
| 361 |
action.set_enabled(is_fragments && is_downloaded); |
| 362 |
} |
| 363 |
|
| 364 |
fn update_file_open_actions(file: &TrFile, actions: &SimpleActionGroup) { |
| 365 |
let current_connection = FrgConnectionManager::default().current_connection(); |
| 366 |
let is_fragments = current_connection.is_fragments(); |
| 367 |
let is_downloaded = file.length() == file.bytes_completed(); |
| 368 |
|
| 369 |
for action_name in actions.list_actions() { |
| 370 |
let action: SimpleAction = actions |
| 371 |
.lookup_action(&action_name) |
| 372 |
.unwrap() |
| 373 |
.downcast() |
| 374 |
.unwrap(); |
| 375 |
action.set_enabled(is_fragments && is_downloaded); |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
fn show_remove_torrent_dialog(torrent: &TrTorrent, parent: gtk::Widget) { |
| 380 |
let heading = i18n("Remove Torrent?"); |
| 381 |
let body = |
| 382 |
i18n("Once removed, continuing the transfer will require the torrent file or magnet link."); |
| 383 |
|
| 384 |
let dialog = adw::AlertDialog::new(Some(&heading), Some(&body)); |
| 385 |
|
| 386 |
dialog.add_response("cancel", &i18n("_Cancel")); |
| 387 |
dialog.set_default_response(Some("cancel")); |
| 388 |
dialog.add_response("remove", &i18n("_Remove")); |
| 389 |
dialog.set_response_appearance("remove", adw::ResponseAppearance::Destructive); |
| 390 |
|
| 391 |
// Check button |
| 392 |
let check_button = gtk::CheckButton::with_label(&i18n("Remove downloaded data as well")); |
| 393 |
check_button.set_halign(gtk::Align::Center); |
| 394 |
dialog.set_extra_child(Some(&check_button)); |
| 395 |
|
| •396 |
dialog.connect_response( |
| 397 |
None, |
| 398 |
clone!( |
| 399 |
#[strong] |
| 400 |
dialog, |
| 401 |
#[weak] |
| 402 |
torrent, |
| 403 |
#[weak] |
| 404 |
parent, |
| 405 |
#[weak] |
| 406 |
check_button, |
| 407 |
move |_, resp| { |
| 408 |
if resp == "remove" { |
| 409 |
let fut = async move { |
| 410 |
torrent |
| 411 |
.remove(check_button.is_active()) |
| 412 |
.await |
| 413 |
.expect("Unable to remove torrent"); |
| •414 |
}; |
| 415 |
|
| 416 |
if let Ok(dialog) = parent.downcast::<adw::Dialog>() { |
| 417 |
dialog.close(); |
| 418 |
} |
| 419 |
|
| •420 |
glib::spawn_future_local(fut); |
| 421 |
} |
| 422 |
|
| •423 |
dialog.close(); |
| 424 |
} |
| •425 |
), |
| 426 |
); |
| 427 |
|
| 428 |
dialog.present(Some(&parent)); |
| 429 |
} |
| 430 |
|
| 431 |
// TODO: Consider rewriting this as async to get rid of the nested closures |
| 432 |
fn show_set_torrent_location(torrent: &TrTorrent, parent: >k::Widget) { |
| 433 |
let title = i18n("Set Torrent Location"); |
| 434 |
|
| 435 |
let dialog = gtk::FileDialog::new(); |
| 436 |
dialog.set_title(&title); |
| 437 |
dialog.set_accept_label(Some(&i18n("_Select"))); |
| 438 |
|
| 439 |
let root = parent.root().unwrap().downcast::<gtk::Window>().unwrap(); |
| •440 |
dialog.select_folder(Some(&root), gio::Cancellable::NONE, clone!(#[weak] torrent, #[weak] parent, #[strong(rename_to = _dialog)] dialog, move |result| { |
| 441 |
match result { |
| 442 |
Ok(folder) => { |
| 443 |
debug!("Selected torrent directory: {:?}", folder.path()); |
| 444 |
|
| 445 |
if torrent.error() == 0 { |
| 446 |
let heading = i18n("Move Data?"); |
| 447 |
let body = i18n("In order to continue using the torrent, the data must be available at the new location."); |
| 448 |
|
| 449 |
let dialog = adw::AlertDialog::new(Some(&heading), Some(&body)); |
| 450 |
dialog.add_response("keep-data", &i18n("_Only Change Location")); |
| 451 |
dialog.add_response("move-data", &i18n("_Move Data to New Location")); |
| 452 |
dialog.set_default_response(Some("move-data")); |
| 453 |
dialog.set_response_appearance("move-data", adw::ResponseAppearance::Suggested); |
| 454 |
|
| •455 |
dialog.present(Some(&parent)); |
| 456 |
dialog.connect_response( |
| 457 |
None, |
| •458 |
clone!(#[strong(rename_to = _dialog)] dialog, #[strong] torrent, #[strong] folder, move |_ , resp| { |
| 459 |
let resp = resp.to_string(); |
| •460 |
let fut = clone!(#[strong] resp, #[strong] torrent, #[strong] folder, async move { |
| 461 |
let _ = torrent.set_location(folder, resp == "move-data").await; |
| 462 |
}); |
| 463 |
|
| 464 |
glib::spawn_future_local(fut); |
| 465 |
}), |
| 466 |
); |
| 467 |
} else { |
| 468 |
let fut = async move { |
| 469 |
// If there is a problem with the torrent, it is most likely because the local |
| 470 |
// downloaded file is no longer available. Therefore, do not offer the option |
| 471 |
// to move the data in the first place. |
| 472 |
let _ = torrent.set_location(folder, false).await; |
| 473 |
// Automatically start torrent again so the error message disappears (hopefully) |
| 474 |
let _ = torrent.start(false).await; |
| 475 |
}; |
| 476 |
glib::spawn_future_local(fut); |
| 477 |
} |
| 478 |
} |
| 479 |
Err(err) => { |
| 480 |
warn!("Selected directory could not be accessed {err}"); |
| 481 |
} |
| 482 |
} |
| 483 |
})); |
| 484 |
} |
| 485 |
|
| 486 |
fn open( |
| 487 |
path: PathBuf, |
| 488 |
native: >k::Native, |
| 489 |
open_containing_folder: bool, |
| 490 |
widget: Option<gtk::Widget>, |
| 491 |
) { |
| •492 |
let fut = clone!( |
| 493 |
#[strong] |
| 494 |
path, |
| 495 |
#[weak] |
| 496 |
native, |
| 497 |
#[strong] |
| 498 |
open_containing_folder, |
| 499 |
#[strong] |
| 500 |
widget, |
| 501 |
async move { |
| 502 |
debug!( |
| 503 |
"Opening: {:?} (containing folder: {})", |
| 504 |
path, open_containing_folder |
| 505 |
); |
| 506 |
match File::open(&path) { |
| 507 |
Ok(file) => { |
| 508 |
let identifier = WindowIdentifier::from_native(&native).await; |
| 509 |
|
| 510 |
let res = if open_containing_folder || path.is_dir() { |
| 511 |
OpenDirectoryRequest::default() |
| 512 |
.identifier(identifier) |
| 513 |
.send(&file.as_fd()) |
| 514 |
.await |
| 515 |
} else { |
| 516 |
OpenFileRequest::default() |
| 517 |
.identifier(identifier) |
| 518 |
.ask(true) |
| 519 |
.send_file(&file.as_fd()) |
| 520 |
.await |
| 521 |
}; |
| 522 |
|
| •523 |
if let Err(err) = res { |
| 524 |
warn!("Could not open folder / file: {}", err.to_string()); |
| 525 |
utils::inapp_notification( |
| 526 |
&i18n("Unable to open file / folder"), |
| 527 |
widget.as_ref(), |
| 528 |
); |
| 529 |
} |
| 530 |
} |
| 531 |
Err(err) => { |
| 532 |
warn!("Could not open file: {}", err.to_string()); |
| 533 |
utils::inapp_notification(&i18n("Could not open file"), widget.as_ref()); |
| 534 |
} |
| 535 |
} |
| 536 |
} |
| •537 |
); |
| 538 |
glib::spawn_future_local(fut); |
| 539 |
} |