Debian Codegraph @

src/ui/torrent_row.rs

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