Debian Codegraph @

src/ui/torrent_row.rs

raw ◀ ▶
1 Debian patch touch this file
•1 // Fragments - torrent_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::OnceCell;
18
19 use glib::{clone, subclass, Properties};
20 use gtk::prelude::*;
21 use gtk::subclass::prelude::*;
22 use gtk::{gdk, gio, glib, CompositeTemplate};
23 use transmission_gobject::{TrTorrent, TrTorrentStatus};
24
25 use crate::i18n::i18n_f;
26 use crate::{actions, utils};
27
28 mod imp {
29 use super::*;
30
31 #[derive(Debug, CompositeTemplate, Default, Properties)]
32 #[template(resource = "/de/haeckerfelix/Fragments/gtk/torrent_row.ui")]
33 #[properties(wrapper_type = super::FrgTorrentRow)]
34 pub struct FrgTorrentRow {
35 #[template_child]
36 torrent_name_label: TemplateChild<gtk::Label>,
37 #[template_child]
38 description_label: TemplateChild<gtk::Label>,
39 #[template_child]
40 eta_label: TemplateChild<gtk::Label>,
41 #[template_child]
42 queue_position_label: TemplateChild<gtk::Label>,
43 #[template_child]
44 progress_bar: TemplateChild<gtk::ProgressBar>,
45 #[template_child]
46 queue_box: TemplateChild<gtk::Box>,
47 #[template_child]
48 index_stack: TemplateChild<gtk::Stack>,
49 #[template_child]
50 action_stack: TemplateChild<gtk::Stack>,
51
52 popover_menu: OnceCell<gtk::PopoverMenu>,
53
54 #[property(get, set, construct_only)]
55 torrent: OnceCell<TrTorrent>,
56 }
57
58 #[glib::object_subclass]
59 impl ObjectSubclass for FrgTorrentRow {
60 const NAME: &'static str = "FrgTorrentRow";
61 type ParentType = gtk::ListBoxRow;
62 type Type = super::FrgTorrentRow;
63
64 fn class_init(klass: &mut Self::Class) {
65 klass.bind_template();
66 }
67
68 fn instance_init(obj: &subclass::InitializingObject<Self>) {
69 obj.init_template();
70 }
71 }
72
73 #[glib::derived_properties]
74 impl ObjectImpl for FrgTorrentRow {
75 fn constructed(&self) {
76 self.parent_constructed();
77 let obj = self.obj();
78
79 actions::install_torrent_actions(&obj.torrent(), obj.clone().upcast());
80
81 // Right-click popover menu
82 let builder =
83 gtk::Builder::from_resource("/de/haeckerfelix/Fragments/gtk/torrent_menu.ui");
84 let menu: gio::MenuModel = builder.object("torrent_menu").unwrap();
85
86 let popover_menu = gtk::PopoverMenu::from_model(Some(&menu));
87 popover_menu.set_parent(self.obj().upcast_ref::<gtk::Widget>());
88 popover_menu.set_has_arrow(false);
89
90 self.popover_menu.set(popover_menu).unwrap();
91
92 // Binding torrent values
93 self.bind_property("name", self.torrent_name_label.get(), "label");
94 self.bind_property("progress", self.progress_bar.get(), "fraction");
95
96 self.obj().torrent().connect_notify_local(
97 None,
•98 clone!(
99 #[weak(rename_to = this)]
100 self,
101 move |_, _| {
102 this.update_labels();
103 this.update_widgets();
104 }
105 ),
106 );
107
108 // Shift+F10 / `Menu` button to open context menu
109 let trigger = gtk::AlternativeTrigger::new(
110 gtk::KeyvalTrigger::new(gdk::Key::F10, gdk::ModifierType::SHIFT_MASK),
111 gtk::KeyvalTrigger::new(gdk::Key::Menu, gdk::ModifierType::empty()),
112 );
•113 let action = gtk::CallbackAction::new(clone!(
114 #[weak(rename_to = this)]
115 self,
116 #[upgrade_or]
117 glib::Propagation::Stop,
118 move |_, _| {
119 this.show_context_menu(None::<&gtk::Gesture>, 40.0, 40.0);
120 glib::Propagation::Stop
•121 }
122 ));
123 let shortcut = gtk::Shortcut::new(Some(trigger), Some(action));
124
125 let controller = gtk::ShortcutController::new();
126 controller.add_shortcut(shortcut);
127 self.obj().add_controller(controller);
128
129 // Right click to open context menu
130 let controller = gtk::GestureClick::new();
131 controller.set_button(gdk::BUTTON_SECONDARY);
•132 controller.connect_pressed(clone!(
133 #[weak(rename_to = this)]
134 self,
135 move |c, _, x, y| this.show_context_menu(Some(c), x, y)
136 ));
137 self.obj().add_controller(controller);
138
139 // Touch long-press to open context menu
140 let controller = gtk::GestureLongPress::new();
141 controller.set_touch_only(true);
•142 controller.connect_pressed(clone!(
143 #[weak(rename_to = this)]
144 self,
145 move |c, x, y| this.show_context_menu(Some(c), x, y)
146 ));
147 self.obj().add_controller(controller);
148
149 self.update_labels();
150 self.update_widgets();
151 }
152
153 fn dispose(&self) {
154 self.popover_menu.get().unwrap().unparent();
155 }
156 }
157
158 impl WidgetImpl for FrgTorrentRow {}
159
160 impl ListBoxRowImpl for FrgTorrentRow {}
161
162 impl FrgTorrentRow {
163 fn show_context_menu<G>(&self, controller: Option<&G>, x: f64, y: f64)
164 where
165 G: IsA<gtk::Gesture>,
166 {
167 let popover_menu = self.popover_menu.get().unwrap();
168
169 if let Some(controller) = controller {
170 controller.set_state(gtk::EventSequenceState::Claimed);
171 }
172
173 let coords = gdk::Rectangle::new(x as i32, y as i32, 0, 0);
174 popover_menu.set_pointing_to(Some(&coords));
175 popover_menu.set_halign(gtk::Align::Start);
176 popover_menu.popup();
177 }
178
179 fn update_labels(&self) {
180 let torrent = self.obj().torrent();
181
182 let eta = utils::eta_text(&torrent);
183 let description = if torrent.error() == 0 {
184 self.description_text()
185 } else {
186 torrent.error_string()
187 };
188 let pos = (torrent.download_queue_position() + 1).to_string();
189
190 self.eta_label.set_text(&eta);
191 self.description_label.set_text(&description);
192 self.queue_position_label.set_text(&pos);
193 }
194
195 fn update_widgets(&self) {
196 self.queue_box.set_visible(false);
197
198 self.obj().remove_css_class("inactive");
199 self.description_label.remove_css_class("error");
200
201 let index_name: String;
202 let action_name: String;
203
204 match self.obj().torrent().status() {
205 TrTorrentStatus::Download => {
206 index_name = "download".into();
207 action_name = "pause".into();
208 }
209 TrTorrentStatus::DownloadWait => {
210 index_name = "queued".into();
211 action_name = "pause".into();
212
213 self.obj().add_css_class("inactive");
214 self.queue_box.set_visible(true);
215 }
216 TrTorrentStatus::Check | TrTorrentStatus::CheckWait => {
217 index_name = "check".into();
218 action_name = "pause".into();
219
220 self.obj().add_css_class("inactive");
221 }
222 TrTorrentStatus::Stopped => {
223 index_name = "stopped".into();
224 action_name = "continue".into();
225
226 self.obj().add_css_class("inactive");
227 }
228 TrTorrentStatus::Seed | TrTorrentStatus::SeedWait => {
229 index_name = "upload".into();
230 action_name = "remove".into();
231 }
232 }
233
234 if self.obj().torrent().error() != 0 {
235 self.description_label.add_css_class("error");
236 }
237
238 self.index_stack.set_visible_child_name(&index_name);
239 self.action_stack.set_visible_child_name(&action_name);
240 }
241
242 fn description_text(&self) -> String {
243 let torrent = self.obj().torrent();
244
245 let downloaded = utils::format_size(torrent.downloaded());
246 let uploaded = utils::format_size(torrent.uploaded());
247 let download_speed = utils::format_speed(torrent.download_speed());
248 let upload_speed = utils::format_speed(torrent.upload_speed());
249 let size = utils::format_size(torrent.size());
250
251 if torrent.downloaded() == torrent.size() {
252 // Translators: First {} is the amount uploaded, second {} is the
253 // uploading speed
254 // Displayed under the torrent name in the main window when torrent
255 // download is finished
256 i18n_f("{} uploaded · {}", &[&uploaded, &upload_speed])
257 } else if torrent.status() == TrTorrentStatus::Stopped
258 || torrent.status() == TrTorrentStatus::DownloadWait
259 {
260 // Translators: First {} is the amount downloaded, second {} is the
261 // total torrent size
262 // Displayed under the torrent name in the main window when torrent
263 // is stopped
264 i18n_f("{} of {}", &[&downloaded, &size])
265 } else {
266 // Translators: First {} is the amount downloaded, second {} is the
267 // total torrent size, third {} is the download speed
268 // Displayed under the torrent name in the main window when torrent
269 // is downloading
270 i18n_f("{} of {} · {}", &[&downloaded, &size, &download_speed])
271 }
272 }
273
274 fn bind_property<T: IsA<gtk::Widget>>(
275 &self,
276 prop_name: &str,
277 widget: T,
278 widget_prop_name: &str,
279 ) {
280 self.obj()
281 .torrent()
282 .bind_property(prop_name, &widget, widget_prop_name)
283 .flags(glib::BindingFlags::SYNC_CREATE)
284 .build();
285 }
286 }
287 }
288
289 glib::wrapper! {
290 pub struct FrgTorrentRow(
291 ObjectSubclass<imp::FrgTorrentRow>)
•292 @extends gtk::Widget, gtk::ListBoxRow,
293 @implements gtk::Actionable, gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget;
294 }
295
296 impl FrgTorrentRow {
297 pub fn new(torrent: &TrTorrent) -> Self {
298 glib::Object::builder().property("torrent", torrent).build()
299 }
300 }