| 1 |
// Fragments - torrent_dialog.rs |
| •2 |
// Copyright (C) 2022-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::cell::OnceCell; |
| 18 |
|
| 19 |
use adw::prelude::*; |
| 20 |
use adw::subclass::dialog::*; |
| 21 |
use adw::subclass::prelude::*; |
| 22 |
use gio::SimpleAction; |
| 23 |
use glib::{clone, subclass, Properties, VariantTy}; |
| 24 |
use gtk::{gio, glib, CompositeTemplate}; |
| 25 |
use transmission_gobject::{TrFile, TrTorrent, TrTorrentStatus}; |
| 26 |
|
| 27 |
use super::{FrgFilePage, FrgFileRow, FrgFolderPage, FrgStatusHeader}; |
| 28 |
use crate::backend::FrgConnectionManager; |
| 29 |
use crate::i18n::ni18n_f; |
| 30 |
use crate::model::{FrgFileSorter, FrgFileSorting}; |
| 31 |
use crate::{actions, utils}; |
| 32 |
|
| 33 |
mod imp { |
| 34 |
use super::*; |
| 35 |
|
| 36 |
#[derive(Debug, Default, CompositeTemplate, Properties)] |
| 37 |
#[template(resource = "/de/haeckerfelix/Fragments/gtk/torrent_dialog.ui")] |
| 38 |
#[properties(wrapper_type = super::FrgTorrentDialog)] |
| 39 |
pub struct FrgTorrentDialog { |
| 40 |
#[template_child] |
| 41 |
pub toast_overlay: TemplateChild<adw::ToastOverlay>, |
| 42 |
#[template_child] |
| 43 |
navigation_view: TemplateChild<adw::NavigationView>, |
| 44 |
|
| 45 |
#[template_child] |
| 46 |
status_header: TemplateChild<FrgStatusHeader>, |
| 47 |
|
| 48 |
#[template_child] |
| 49 |
location_box: TemplateChild<gtk::Box>, |
| 50 |
#[template_child] |
| 51 |
location_label: TemplateChild<gtk::Label>, |
| 52 |
|
| 53 |
#[template_child] |
| 54 |
error_group: TemplateChild<adw::PreferencesGroup>, |
| 55 |
#[template_child] |
| 56 |
error_row: TemplateChild<adw::ActionRow>, |
| 57 |
|
| 58 |
#[template_child] |
| 59 |
button_stack: TemplateChild<gtk::Stack>, |
| 60 |
#[template_child] |
| 61 |
pause_button: TemplateChild<gtk::Button>, |
| 62 |
#[template_child] |
| 63 |
continue_button: TemplateChild<gtk::Button>, |
| 64 |
#[template_child] |
| 65 |
open_button: TemplateChild<gtk::Button>, |
| 66 |
#[template_child] |
| 67 |
more_button: TemplateChild<gtk::MenuButton>, |
| 68 |
|
| 69 |
#[template_child] |
| 70 |
download_image: TemplateChild<gtk::Image>, |
| 71 |
#[template_child] |
| 72 |
upload_image: TemplateChild<gtk::Image>, |
| 73 |
#[template_child] |
| 74 |
download_speed_label: TemplateChild<gtk::Label>, |
| 75 |
#[template_child] |
| 76 |
upload_speed_label: TemplateChild<gtk::Label>, |
| 77 |
#[template_child] |
| 78 |
seeders_label: TemplateChild<gtk::Label>, |
| 79 |
#[template_child] |
| 80 |
leechers_label: TemplateChild<gtk::Label>, |
| 81 |
#[template_child] |
| 82 |
downloaded_label: TemplateChild<gtk::Label>, |
| 83 |
#[template_child] |
| 84 |
uploaded_label: TemplateChild<gtk::Label>, |
| 85 |
#[template_child] |
| 86 |
metadata_loading_group: TemplateChild<adw::PreferencesGroup>, |
| 87 |
#[template_child] |
| 88 |
metadata_percentage_label: TemplateChild<gtk::Label>, |
| 89 |
#[template_child] |
| 90 |
file_listbox: TemplateChild<gtk::ListBox>, |
| 91 |
#[template_child] |
| 92 |
all_files_group: TemplateChild<adw::PreferencesGroup>, |
| 93 |
#[template_child] |
| 94 |
all_files_row: TemplateChild<adw::ActionRow>, |
| 95 |
|
| 96 |
#[property(get, set, construct_only)] |
| 97 |
torrent: OnceCell<TrTorrent>, |
| 98 |
|
| 99 |
view_actions: gio::SimpleActionGroup, |
| 100 |
files_sorter: FrgFileSorter, |
| 101 |
} |
| 102 |
|
| 103 |
#[glib::object_subclass] |
| 104 |
impl ObjectSubclass for FrgTorrentDialog { |
| 105 |
const NAME: &'static str = "FrgTorrentDialog"; |
| 106 |
type ParentType = adw::Dialog; |
| 107 |
type Type = super::FrgTorrentDialog; |
| 108 |
|
| 109 |
fn class_init(klass: &mut Self::Class) { |
| 110 |
klass.bind_template(); |
| 111 |
} |
| 112 |
|
| 113 |
fn instance_init(obj: &subclass::InitializingObject<Self>) { |
| 114 |
obj.init_template(); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
#[glib::derived_properties] |
| 119 |
impl ObjectImpl for FrgTorrentDialog { |
| 120 |
fn constructed(&self) { |
| 121 |
self.parent_constructed(); |
| 122 |
let obj: &super::FrgTorrentDialog = &self.obj(); |
| 123 |
let torrent = obj.torrent(); |
| 124 |
|
| 125 |
// Pull extra information from transmission daemon so we can display them in the |
| 126 |
// dialog. Those aren't getting retrieved by default to avoid unnecessary |
| 127 |
// deserialization. |
| 128 |
torrent.set_update_extra_info(true); |
| 129 |
|
| 130 |
// Bind torrent values |
| 131 |
self.bind_property("name", obj, "title"); |
| 132 |
self.bind_property("primary-mime-type", &self.status_header.get(), "mimetype"); |
| 133 |
self.bind_property("name", &self.status_header.get(), "title"); |
| 134 |
self.bind_property("size", &self.status_header.get(), "total-bytes"); |
| 135 |
self.bind_property("downloaded", &self.status_header.get(), "downloaded-bytes"); |
| 136 |
self.bind_property("download-dir", &self.location_label.get(), "label"); |
| 137 |
self.bind_property("leechers", &self.leechers_label.get(), "label"); |
| 138 |
|
| 139 |
// Setup `torrent` specific actions |
| 140 |
actions::install_torrent_actions(&torrent, obj.clone().upcast()); |
| 141 |
|
| 142 |
// "More" menu button |
| 143 |
let builder = |
| 144 |
gtk::Builder::from_resource("/de/haeckerfelix/Fragments/gtk/torrent_menu.ui"); |
| 145 |
let menu: gio::MenuModel = builder.object("torrent_more_menu").unwrap(); |
| 146 |
self.more_button.set_menu_model(Some(&menu)); |
| 147 |
|
| 148 |
// Files metadata |
| 149 |
torrent |
| 150 |
.bind_property( |
| 151 |
"metadata-percent-complete", |
| 152 |
&self.metadata_percentage_label.get(), |
| 153 |
"label", |
| 154 |
) |
| 155 |
.transform_to(|_, p: f32| Some(format!("{:.0}%", p * 100.0))) |
| 156 |
.sync_create() |
| 157 |
.build(); |
| 158 |
|
| 159 |
torrent |
| 160 |
.bind_property( |
| 161 |
"metadata-percent-complete", |
| 162 |
&self.metadata_loading_group.get(), |
| 163 |
"visible", |
| 164 |
) |
| 165 |
.transform_to(|_, p: f32| Some(p != 1.0)) |
| 166 |
.sync_create() |
| 167 |
.build(); |
| 168 |
|
| 169 |
// Files listbox |
| 170 |
if torrent.files().is_ready() { |
| 171 |
self.show_overview_files(); |
| 172 |
} |
| •173 |
torrent.files().connect_is_ready_notify(clone!( |
| 174 |
#[weak(rename_to = this)] |
| 175 |
self, |
| 176 |
move |_| { |
| 177 |
this.show_overview_files(); |
| •178 |
} |
| 179 |
)); |
| 180 |
|
| 181 |
// Files listbox row activation |
| •182 |
self.file_listbox.connect_row_activated(clone!( |
| 183 |
#[weak(rename_to = this)] |
| 184 |
self, |
| 185 |
move |_, row| { |
| 186 |
let row = row |
| 187 |
.downcast_ref::<adw::PreferencesRow>() |
| 188 |
.unwrap() |
| 189 |
.child() |
| 190 |
.unwrap(); |
| 191 |
let file_row = row.downcast_ref::<FrgFileRow>().unwrap(); |
| 192 |
let file = file_row.file(); |
| 193 |
|
| 194 |
if let Some(file) = file { |
| 195 |
if file.is_folder() { |
| 196 |
this.show_folder(&file); |
| 197 |
} else { |
| 198 |
this.show_file(&file); |
| 199 |
} |
| 200 |
} |
| •201 |
} |
| 202 |
)); |
| 203 |
|
| 204 |
// Setup view actions |
| 205 |
obj.insert_action_group("view", Some(&self.view_actions)); |
| 206 |
let mut action; |
| 207 |
|
| 208 |
// view.sorting |
| 209 |
action = SimpleAction::new_stateful( |
| 210 |
"sorting", |
| 211 |
VariantTy::STRING.into(), |
| 212 |
&"name".to_variant(), |
| 213 |
); |
| •214 |
action.connect_change_state(clone!( |
| 215 |
#[weak(rename_to = files_sorter)] |
| 216 |
self.files_sorter, |
| 217 |
move |action, state| { |
| 218 |
if let Some(state) = state { |
| 219 |
action.set_state(state); |
| 220 |
|
| 221 |
let sorter: FrgFileSorting = state.str().unwrap().into(); |
| 222 |
files_sorter.set_sorting(sorter); |
| 223 |
} |
| •224 |
} |
| 225 |
)); |
| 226 |
self.view_actions.add_action(&action); |
| 227 |
|
| 228 |
// view.order |
| 229 |
action = SimpleAction::new_stateful( |
| 230 |
"order", |
| 231 |
VariantTy::STRING.into(), |
| 232 |
&"ascending".to_variant(), |
| 233 |
); |
| •234 |
action.connect_change_state(clone!( |
| 235 |
#[weak(rename_to = files_sorter)] |
| 236 |
self.files_sorter, |
| 237 |
move |action, state| { |
| 238 |
if let Some(state) = state { |
| 239 |
action.set_state(state); |
| 240 |
|
| 241 |
let descending = state.str().unwrap() == "descending"; |
| 242 |
files_sorter.set_descending(descending); |
| 243 |
} |
| •244 |
} |
| 245 |
)); |
| 246 |
self.view_actions.add_action(&action); |
| 247 |
|
| 248 |
// view.folder-before-files |
| 249 |
action = SimpleAction::new_stateful("folder-before-files", None, &true.to_variant()); |
| •250 |
action.connect_change_state(clone!( |
| 251 |
#[weak(rename_to = files_sorter)] |
| 252 |
self.files_sorter, |
| 253 |
move |action, state| { |
| 254 |
if let Some(state) = state { |
| 255 |
action.set_state(state); |
| 256 |
|
| 257 |
let value = state.get::<bool>().unwrap(); |
| 258 |
files_sorter.set_folders_before_files(value); |
| 259 |
} |
| •260 |
} |
| 261 |
)); |
| 262 |
self.view_actions.add_action(&action); |
| 263 |
|
| 264 |
// view.show-all |
| 265 |
action = SimpleAction::new("show-all", None); |
| •266 |
action.connect_activate(clone!( |
| 267 |
#[weak(rename_to = this)] |
| 268 |
self, |
| 269 |
move |_, _| { |
| 270 |
if let Some(top_level) = this.obj().torrent().files().top_level() { |
| 271 |
this.show_folder(&top_level); |
| 272 |
} else { |
| 273 |
warn!("Unable to show contents, no top level file available"); |
| 274 |
} |
| 275 |
} |
| •276 |
)); |
| 277 |
self.view_actions.add_action(&action); |
| 278 |
|
| 279 |
// view.show-folder |
| 280 |
action = SimpleAction::new("show-folder", VariantTy::STRING.into()); |
| •281 |
action.connect_activate(clone!( |
| 282 |
#[weak(rename_to = this)] |
| 283 |
self, |
| 284 |
move |_, param| { |
| 285 |
if let Some(param) = param { |
| 286 |
let name = param.get::<String>().unwrap(); |
| 287 |
let folder = this.obj().torrent().files().file_by_name(&name).unwrap(); |
| 288 |
this.show_folder(&folder); |
| 289 |
} |
| 290 |
} |
| •291 |
)); |
| 292 |
self.view_actions.add_action(&action); |
| 293 |
|
| 294 |
// view.show-file |
| 295 |
action = SimpleAction::new("show-file", VariantTy::STRING.into()); |
| •296 |
action.connect_activate(clone!( |
| 297 |
#[weak(rename_to = this)] |
| 298 |
self, |
| 299 |
move |_, param| { |
| 300 |
if let Some(param) = param { |
| 301 |
let name = param.get::<String>().unwrap(); |
| 302 |
let file = this.obj().torrent().files().file_by_name(&name).unwrap(); |
| 303 |
this.show_file(&file); |
| 304 |
} |
| 305 |
} |
| •306 |
)); |
| 307 |
self.view_actions.add_action(&action); |
| 308 |
|
| 309 |
// Update dialog on torrent changes |
| 310 |
torrent.connect_notify_local( |
| 311 |
None, |
| •312 |
clone!( |
| 313 |
#[weak(rename_to = this)] |
| 314 |
self, |
| 315 |
move |_, _| { |
| 316 |
this.update_labels(); |
| 317 |
this.update_widgets(); |
| 318 |
} |
| 319 |
), |
| 320 |
); |
| 321 |
|
| 322 |
self.update_labels(); |
| 323 |
self.update_widgets(); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
impl WidgetImpl for FrgTorrentDialog {} |
| 328 |
|
| 329 |
impl AdwDialogImpl for FrgTorrentDialog { |
| 330 |
fn closed(&self) { |
| 331 |
self.obj().torrent().set_update_extra_info(false); |
| 332 |
self.parent_closed(); |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
impl FrgTorrentDialog { |
| 337 |
fn show_folder(&self, folder: &TrFile) { |
| 338 |
let page = FrgFolderPage::new(folder, &self.files_sorter); |
| 339 |
self.navigation_view.push(&page); |
| 340 |
} |
| 341 |
|
| 342 |
fn show_file(&self, file: &TrFile) { |
| 343 |
let page = FrgFilePage::new(file); |
| 344 |
self.navigation_view.push(&page); |
| 345 |
} |
| 346 |
|
| 347 |
fn show_overview_files(&self) { |
| 348 |
let torrent = self.obj().torrent(); |
| 349 |
|
| 350 |
let top_level = if let Some(top_level) = torrent.files().top_level() { |
| 351 |
top_level |
| 352 |
} else { |
| 353 |
warn!("Unable display files in overview page, no top level available"); |
| 354 |
return; |
| 355 |
}; |
| 356 |
|
| 357 |
// When a torrent has multiple files, the top level entry is always a folder |
| 358 |
if top_level.is_folder() { |
| 359 |
self.file_listbox.set_visible(true); |
| 360 |
let files = top_level.related(); |
| 361 |
|
| 362 |
// We has to limit the files which are getting displayed in a |
| 363 |
// regular listbox since it doesn't scale like a listview |
| 364 |
let mut max_files = files.n_items(); |
| 365 |
if max_files > 8 { |
| 366 |
max_files = 8; |
| 367 |
let total = torrent.files().n_items(); |
| 368 |
|
| 369 |
// Translators: {} is *always* a number greater than 8 |
| 370 |
let text = ni18n_f( |
| 371 |
"Show All", |
| 372 |
"Show All {} Items", |
| 373 |
total, |
| 374 |
&[&total.to_string()], |
| 375 |
); |
| 376 |
|
| 377 |
// Show row which allows to show all files using a listview |
| 378 |
self.all_files_row.set_title(&text); |
| 379 |
self.all_files_group.set_visible(true); |
| 380 |
self.obj().set_content_height(1200); |
| 381 |
} |
| 382 |
|
| 383 |
let sorter = FrgFileSorter::new(); |
| 384 |
let files_sorted = gtk::SortListModel::new(Some(files), Some(sorter)); |
| 385 |
|
| 386 |
// Add some files to the simple listbox |
| 387 |
for i in 0..max_files { |
| 388 |
let object = files_sorted.item(i).unwrap(); |
| 389 |
let file = object.downcast_ref::<TrFile>().unwrap(); |
| 390 |
|
| 391 |
let file_row = FrgFileRow::new(); |
| 392 |
file_row.set_file(file); |
| 393 |
|
| 394 |
let row = adw::PreferencesRow::new(); |
| 395 |
row.set_child(Some(&file_row)); |
| 396 |
|
| 397 |
self.file_listbox.append(&row); |
| 398 |
} |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
fn update_labels(&self) { |
| 403 |
let torrent = self.obj().torrent(); |
| 404 |
|
| 405 |
let eta = utils::eta_text(&torrent); |
| 406 |
self.status_header.set_eta(eta); |
| 407 |
|
| 408 |
let downloaded = utils::format_size(torrent.downloaded()); |
| 409 |
self.downloaded_label.set_text(&downloaded); |
| 410 |
|
| 411 |
let uploaded = utils::format_size(torrent.uploaded()); |
| 412 |
self.uploaded_label.set_text(&uploaded); |
| 413 |
|
| 414 |
let download_speed = utils::format_speed(torrent.download_speed()); |
| 415 |
self.download_speed_label.set_text(&download_speed); |
| 416 |
|
| 417 |
let upload_speed = utils::format_speed(torrent.upload_speed()); |
| 418 |
self.upload_speed_label.set_text(&upload_speed); |
| 419 |
|
| 420 |
let seeders = torrent.seeders().to_string(); |
| 421 |
let seeders_active = torrent.seeders_active().to_string(); |
| 422 |
// Translators: First {} corresponds to the number of seeders, second {} to the |
| 423 |
// number of active seeders |
| 424 |
let seeders_text = ni18n_f( |
| 425 |
"{} ({} active)", |
| 426 |
"{} ({} active)", |
| 427 |
torrent.seeders_active().try_into().unwrap_or(0), |
| 428 |
&[&seeders, &seeders_active], |
| 429 |
); |
| 430 |
self.seeders_label.set_text(&seeders_text); |
| 431 |
} |
| 432 |
|
| 433 |
fn update_widgets(&self) { |
| 434 |
let session = FrgConnectionManager::default().client().session(); |
| 435 |
let torrent = self.obj().torrent(); |
| 436 |
|
| 437 |
// Location label |
| 438 |
// Only show the location info when it differs from the standard download |
| 439 |
let session_download_dir = if let Some(dir) = session.download_dir() { |
| 440 |
dir.path().unwrap().to_str().unwrap().to_string() |
| 441 |
} else { |
| 442 |
String::new() |
| 443 |
}; |
| 444 |
let show_location = torrent.download_dir() != session_download_dir; |
| 445 |
self.location_box.set_visible(show_location); |
| 446 |
|
| 447 |
// Action button row |
| 448 |
let button_widget = match torrent.status() { |
| 449 |
TrTorrentStatus::Stopped => self.continue_button.get(), |
| 450 |
_ => self.pause_button.get(), |
| 451 |
}; |
| 452 |
self.button_stack.set_visible_child(&button_widget); |
| 453 |
|
| 454 |
// Error message row |
| 455 |
if torrent.error() != 0 { |
| 456 |
self.error_group.set_visible(true); |
| 457 |
self.error_row.set_subtitle(&torrent.error_string()); |
| 458 |
} else { |
| 459 |
self.error_group.set_visible(false); |
| 460 |
} |
| 461 |
|
| 462 |
// Status boxes |
| 463 |
let is_downloading = torrent.download_speed() != 0; |
| 464 |
let is_uploading = torrent.upload_speed() != 0; |
| 465 |
|
| 466 |
if is_downloading { |
| 467 |
self.download_image.add_css_class("active"); |
| 468 |
} else { |
| 469 |
self.download_image.remove_css_class("active"); |
| 470 |
} |
| 471 |
|
| 472 |
if is_uploading { |
| 473 |
self.upload_image.add_css_class("active"); |
| 474 |
} else { |
| 475 |
self.upload_image.remove_css_class("active"); |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
fn bind_property<T: IsA<gtk::Widget>>( |
| 480 |
&self, |
| 481 |
prop_name: &str, |
| 482 |
widget: &T, |
| 483 |
widget_prop_name: &str, |
| 484 |
) { |
| 485 |
self.obj() |
| 486 |
.torrent() |
| 487 |
.bind_property(prop_name, widget, widget_prop_name) |
| 488 |
.sync_create() |
| 489 |
.build(); |
| 490 |
} |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
glib::wrapper! { |
| 495 |
pub struct FrgTorrentDialog(ObjectSubclass<imp::FrgTorrentDialog>) |
| •496 |
@extends gtk::Widget, adw::Dialog, |
| 497 |
@implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget; |
| 498 |
} |
| 499 |
|
| 500 |
impl FrgTorrentDialog { |
| 501 |
pub fn new(torrent: &TrTorrent) -> Self { |
| 502 |
glib::Object::builder().property("torrent", torrent).build() |
| 503 |
} |
| 504 |
|
| 505 |
pub fn toast_overlay(&self) -> adw::ToastOverlay { |
| 506 |
self.imp().toast_overlay.get() |
| 507 |
} |
| 508 |
} |