Debian Codegraph @

src/ui/file_row.rs

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 // Fragments - file_row.rs
2 // Copyright (C) 2022-2023 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()), &[])
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!(@weak self as this => move|_,_|{
146 this.update_progressbar();
147 this.update_subtitle();
148 }),
149 );
150 self.signal_handlers.borrow_mut().push(id);
151
152 let id = file.connect_notify_local(
153 Some("wanted"),
154 clone!(@weak self as this => move|_,_|{
155 this.update_progressbar();
156 this.update_subtitle();
157 }),
158 );
159 self.signal_handlers.borrow_mut().push(id);
160
161 self.update_progressbar();
162 self.update_subtitle();
163 }
164 }
165
166 fn update_progressbar(&self) {
167 let file = self.obj().file().unwrap();
168 let progressbar = &self.progressbar;
169
170 let mut percentage = file.bytes_completed() as f64 / file.length() as f64;
171 if !percentage.is_normal() {
172 percentage = 0.0;
173 }
174
175 if percentage > 0.0 || file.wanted() {
176 self.stack.set_visible_child_name("progress");
177 progressbar.set_fraction(percentage);
178 } else {
179 self.stack.set_visible_child_name("no-progress");
180 }
181
182 if file.wanted() {
183 self.obj().remove_css_class("inactive");
184 } else {
185 self.obj().add_css_class("inactive");
186 }
187 }
188
189 fn update_subtitle(&self) {
190 let file = self.obj().file().unwrap();
191
192 let length = utils::format_size(file.length());
193 let bytes_completed = utils::format_size(file.bytes_completed());
194 let items = file.related().n_items();
195
196 #[rustfmt::skip]
197 let subtitle = if file.bytes_completed() > 0 || file.wanted() {
198 if file.is_folder() {
199 ni18n_f("{} / {}, {} item", "{} / {}, {} items",items, &[&bytes_completed, &length, &items.to_string()])
200 } else {
201 i18n_f("{} / {}", &[&bytes_completed, &length])
202 }
203 } else if file.is_folder() {
204 ni18n_f("{}, {} item", "{}, {} items", items, &[&length, &items.to_string()])
205 } else {
206 length
207 };
208
209 self.subtitle_label.set_text(&subtitle);
210 }
211 }
212 }
213
214 glib::wrapper! {
215 pub struct FrgFileRow(ObjectSubclass<imp::FrgFileRow>)
216 @extends gtk::Widget, adw::Bin;
217 }
218
219 impl FrgFileRow {
220 pub fn new() -> Self {
221 glib::Object::new()
222 }
223 }
224
225 impl Default for FrgFileRow {
226 fn default() -> Self {
227 Self::new()
228 }
229 }