| 1 |
// Fragments - file_row.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::RefCell; |
| 18 |
|
| 19 |
use adw::subclass::prelude::*; |
| 20 |
use glib::{clone, subclass, Properties}; |
| 21 |
use gtk::prelude::*; |
| 22 |
use gtk::{gio, glib, CompositeTemplate}; |
| 23 |
use transmission_gobject::TrFile; |
| 24 |
|
| 25 |
use crate::i18n::{i18n_f, ni18n_f}; |
| 26 |
use crate::{actions, utils}; |
| 27 |
|
| 28 |
mod imp { |
| 29 |
use super::*; |
| 30 |
|
| 31 |
#[derive(Debug, Default, CompositeTemplate, Properties)] |
| 32 |
#[template(resource = "/de/haeckerfelix/Fragments/gtk/file_row.ui")] |
| 33 |
#[properties(wrapper_type = super::FrgFileRow)] |
| 34 |
pub struct FrgFileRow { |
| 35 |
#[template_child] |
| 36 |
mimetype_image: TemplateChild<gtk::Image>, |
| 37 |
#[template_child] |
| 38 |
stack: TemplateChild<gtk::Stack>, |
| 39 |
#[template_child] |
| 40 |
title_label: TemplateChild<gtk::Label>, |
| 41 |
#[template_child] |
| 42 |
subtitle_label: TemplateChild<gtk::Label>, |
| 43 |
#[template_child] |
| 44 |
progressbar: TemplateChild<gtk::ProgressBar>, |
| 45 |
#[template_child] |
| 46 |
checkbutton: TemplateChild<gtk::CheckButton>, |
| 47 |
#[template_child] |
| 48 |
folder_arrow_image: TemplateChild<gtk::Image>, |
| 49 |
|
| 50 |
#[property(get, set=Self::set_file)] |
| 51 |
file: RefCell<Option<TrFile>>, |
| 52 |
|
| 53 |
bindings: RefCell<Vec<glib::Binding>>, |
| 54 |
signal_handlers: RefCell<Vec<glib::SignalHandlerId>>, |
| 55 |
} |
| 56 |
|
| 57 |
#[glib::object_subclass] |
| 58 |
impl ObjectSubclass for FrgFileRow { |
| 59 |
const NAME: &'static str = "FrgFileRow"; |
| 60 |
type ParentType = adw::Bin; |
| 61 |
type Type = super::FrgFileRow; |
| 62 |
|
| 63 |
fn class_init(klass: &mut Self::Class) { |
| 64 |
klass.bind_template(); |
| 65 |
} |
| 66 |
|
| 67 |
fn instance_init(obj: &subclass::InitializingObject<Self>) { |
| 68 |
obj.init_template(); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
#[glib::derived_properties] |
| 73 |
impl ObjectImpl for FrgFileRow {} |
| 74 |
|
| 75 |
impl WidgetImpl for FrgFileRow {} |
| 76 |
|
| 77 |
impl BinImpl for FrgFileRow {} |
| 78 |
|
| 79 |
impl FrgFileRow { |
| 80 |
pub fn set_file(&self, file: Option<&TrFile>) { |
| 81 |
// Drop all bindings from the previous file |
| 82 |
while let Some(binding) = self.bindings.borrow_mut().pop() { |
| 83 |
binding.unbind(); |
| 84 |
} |
| 85 |
while let Some(id) = self.signal_handlers.borrow_mut().pop() { |
| 86 |
if let Some(file) = self.obj().file() { |
| 87 |
file.disconnect(id); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
self.file.replace(file.cloned()); |
| 92 |
self.update_widgets(); |
| 93 |
} |
| 94 |
|
| 95 |
fn update_widgets(&self) { |
| 96 |
if let Some(file) = self.obj().file() { |
| 97 |
// Actions |
| 98 |
actions::install_file_actions(&file, self.obj().clone().upcast()); |
| 99 |
|
| 100 |
// Title |
| 101 |
self.title_label.set_label(&file.title()); |
| 102 |
self.obj().set_tooltip_text(Some(&file.name())); |
| 103 |
|
| 104 |
// Row icon |
| 105 |
if file.is_folder() { |
| 106 |
self.mimetype_image.set_icon_name(Some("folder-symbolic")); |
| 107 |
self.folder_arrow_image.set_visible(true); |
| 108 |
} else { |
| •109 |
let mimetype = gio::content_type_guess(Some(&file.name()), None) |
| 110 |
.0 |
| 111 |
.to_string(); |
| 112 |
utils::set_mimetype_image(&mimetype, &self.mimetype_image.get()) |
| 113 |
} |
| 114 |
|
| 115 |
// Wanted checkbox |
| 116 |
let binding = file |
| 117 |
.bind_property("wanted", &self.checkbutton.get(), "active") |
| 118 |
.sync_create() |
| 119 |
.bidirectional() |
| 120 |
.build(); |
| 121 |
self.bindings.borrow_mut().push(binding); |
| 122 |
|
| 123 |
let binding = file |
| 124 |
.bind_property("bytes-completed", &self.checkbutton.get(), "sensitive") |
| 125 |
.transform_to(|b, bytes: i64| { |
| 126 |
Some(bytes != b.source().unwrap().downcast::<TrFile>().unwrap().length()) |
| 127 |
}) |
| 128 |
.sync_create() |
| 129 |
.build(); |
| 130 |
self.bindings.borrow_mut().push(binding); |
| 131 |
|
| 132 |
let binding = file |
| 133 |
.bind_property( |
| 134 |
"wanted-inconsistent", |
| 135 |
&self.checkbutton.get(), |
| 136 |
"inconsistent", |
| 137 |
) |
| 138 |
.sync_create() |
| 139 |
.build(); |
| 140 |
self.bindings.borrow_mut().push(binding); |
| 141 |
|
| 142 |
// Progress & Subtitle |
| 143 |
let id = file.connect_notify_local( |
| 144 |
Some("bytes-completed"), |
| •145 |
clone!( |
| 146 |
#[weak(rename_to = this)] |
| 147 |
self, |
| 148 |
move |_, _| { |
| 149 |
this.update_progressbar(); |
| 150 |
this.update_subtitle(); |
| 151 |
} |
| 152 |
), |
| 153 |
); |
| 154 |
self.signal_handlers.borrow_mut().push(id); |
| 155 |
|
| 156 |
let id = file.connect_notify_local( |
| 157 |
Some("wanted"), |
| •158 |
clone!( |
| 159 |
#[weak(rename_to = this)] |
| 160 |
self, |
| 161 |
move |_, _| { |
| 162 |
this.update_progressbar(); |
| 163 |
this.update_subtitle(); |
| 164 |
} |
| 165 |
), |
| 166 |
); |
| 167 |
self.signal_handlers.borrow_mut().push(id); |
| 168 |
|
| 169 |
self.update_progressbar(); |
| 170 |
self.update_subtitle(); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
fn update_progressbar(&self) { |
| 175 |
let file = self.obj().file().unwrap(); |
| 176 |
let progressbar = &self.progressbar; |
| 177 |
|
| 178 |
let mut percentage = file.bytes_completed() as f64 / file.length() as f64; |
| 179 |
if !percentage.is_normal() { |
| 180 |
percentage = 0.0; |
| 181 |
} |
| 182 |
|
| 183 |
if percentage > 0.0 || file.wanted() { |
| 184 |
self.stack.set_visible_child_name("progress"); |
| 185 |
progressbar.set_fraction(percentage); |
| 186 |
} else { |
| 187 |
self.stack.set_visible_child_name("no-progress"); |
| 188 |
} |
| 189 |
|
| 190 |
if file.wanted() { |
| 191 |
self.obj().remove_css_class("inactive"); |
| 192 |
} else { |
| 193 |
self.obj().add_css_class("inactive"); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
fn update_subtitle(&self) { |
| 198 |
let file = self.obj().file().unwrap(); |
| 199 |
|
| 200 |
let length = utils::format_size(file.length()); |
| 201 |
let bytes_completed = utils::format_size(file.bytes_completed()); |
| 202 |
let items = file.related().n_items(); |
| 203 |
|
| 204 |
#[rustfmt::skip] |
| 205 |
let subtitle = if file.bytes_completed() > 0 || file.wanted() { |
| 206 |
if file.is_folder() { |
| 207 |
ni18n_f("{} / {}, {} item", "{} / {}, {} items",items, &[&bytes_completed, &length, &items.to_string()]) |
| 208 |
} else { |
| 209 |
i18n_f("{} / {}", &[&bytes_completed, &length]) |
| 210 |
} |
| 211 |
} else if file.is_folder() { |
| 212 |
ni18n_f("{}, {} item", "{}, {} items", items, &[&length, &items.to_string()]) |
| 213 |
} else { |
| 214 |
length |
| 215 |
}; |
| 216 |
|
| 217 |
self.subtitle_label.set_text(&subtitle); |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
glib::wrapper! { |
| 223 |
pub struct FrgFileRow(ObjectSubclass<imp::FrgFileRow>) |
| •224 |
@extends gtk::Widget, adw::Bin, |
| 225 |
@implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget; |
| 226 |
} |
| 227 |
|
| 228 |
impl FrgFileRow { |
| 229 |
pub fn new() -> Self { |
| 230 |
glib::Object::new() |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
impl Default for FrgFileRow { |
| 235 |
fn default() -> Self { |
| 236 |
Self::new() |
| 237 |
} |
| 238 |
} |