Debian Codegraph @

src/ui/stats_dialog.rs

raw ◀ ▶
1 Debian patch touch this file
1 // Fragments - stats_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 adw::prelude::*;
18 use adw::subclass::prelude::*;
19 use glib::{clone, subclass, BindingFlags};
20 use gtk::{glib, CompositeTemplate};
21
22 use crate::backend::FrgConnectionManager;
23 use crate::i18n::{i18n_f, ni18n_f};
24 use crate::utils;
25
26 mod imp {
27 use super::*;
28
29 #[derive(Debug, Default, CompositeTemplate)]
30 #[template(resource = "/de/haeckerfelix/Fragments/gtk/stats_dialog.ui")]
31 pub struct FrgStatsDialog {
32 #[template_child]
33 remote_notice_group: TemplateChild<adw::PreferencesGroup>,
34 #[template_child]
35 remote_notice_row: TemplateChild<adw::ActionRow>,
36
37 #[template_child]
38 version_label: TemplateChild<gtk::Label>,
39
40 #[template_child]
41 download_speed_label: TemplateChild<gtk::Label>,
42 #[template_child]
43 upload_speed_label: TemplateChild<gtk::Label>,
44
45 #[template_child]
46 torrents_label: TemplateChild<gtk::Label>,
47 #[template_child]
48 active_torrents_label: TemplateChild<gtk::Label>,
49 #[template_child]
50 paused_torrents_label: TemplateChild<gtk::Label>,
51 #[template_child]
52 downloaded_torrents_label: TemplateChild<gtk::Label>,
53
54 #[template_child]
55 current_time_active_label: TemplateChild<gtk::Label>,
56 #[template_child]
57 current_downloaded_label: TemplateChild<gtk::Label>,
58 #[template_child]
59 current_uploaded_label: TemplateChild<gtk::Label>,
60 #[template_child]
61 current_files_added_label: TemplateChild<gtk::Label>,
62
63 #[template_child]
64 cumulative_time_active_label: TemplateChild<gtk::Label>,
65 #[template_child]
66 cumulative_downloaded_label: TemplateChild<gtk::Label>,
67 #[template_child]
68 cumulative_uploaded_label: TemplateChild<gtk::Label>,
69 #[template_child]
70 cumulative_files_added_label: TemplateChild<gtk::Label>,
71 #[template_child]
72 cumulative_sessions_label: TemplateChild<gtk::Label>,
73 }
74
75 #[glib::object_subclass]
76 impl ObjectSubclass for FrgStatsDialog {
77 const NAME: &'static str = "FrgStatsDialog";
78 type ParentType = adw::Dialog;
79 type Type = super::FrgStatsDialog;
80
81 fn class_init(klass: &mut Self::Class) {
82 klass.bind_template();
83 }
84
85 fn instance_init(obj: &subclass::InitializingObject<Self>) {
86 obj.init_template();
87 }
88 }
89
90 impl ObjectImpl for FrgStatsDialog {
91 fn constructed(&self) {
92 self.parent_constructed();
93
•94 FrgConnectionManager::default().connect_current_connection_notify(clone!(
95 #[weak(rename_to = this)]
96 self,
97 move |_| {
98 this.update_remote_notice();
•99 }
100 ));
101
102 self.bind_properties();
103 self.update_format_strings();
104 self.update_remote_notice();
105 }
106 }
107
108 impl WidgetImpl for FrgStatsDialog {}
109
110 impl AdwDialogImpl for FrgStatsDialog {}
111
112 impl FrgStatsDialog {
113 fn bind_properties(&self) {
114 let client = FrgConnectionManager::default().client();
115 let session = client.session();
116 let stats = client.session_stats();
117 let current = stats.current_stats();
118 let cumulative = stats.cumulative_stats();
119
120 session
121 .bind_property("version", &self.version_label.get(), "label")
122 .flags(BindingFlags::SYNC_CREATE)
123 .build();
124
125 stats
126 .bind_property("torrent-count", &self.torrents_label.get(), "label")
127 .flags(BindingFlags::SYNC_CREATE)
128 .build();
129
130 stats
131 .bind_property(
132 "active-torrent-count",
133 &self.active_torrents_label.get(),
134 "label",
135 )
136 .flags(BindingFlags::SYNC_CREATE)
137 .build();
138
139 stats
140 .bind_property(
141 "paused-torrent-count",
142 &self.paused_torrents_label.get(),
143 "label",
144 )
145 .flags(BindingFlags::SYNC_CREATE)
146 .build();
147
148 stats
149 .bind_property(
150 "downloaded-torrent-count",
151 &self.downloaded_torrents_label.get(),
152 "label",
153 )
154 .flags(BindingFlags::SYNC_CREATE)
155 .build();
156
157 stats
158 .bind_property(
159 "downloaded-torrent-count",
160 &self.downloaded_torrents_label.get(),
161 "label",
162 )
163 .flags(BindingFlags::SYNC_CREATE)
164 .build();
165
166 current
167 .bind_property(
168 "files-added",
169 &self.current_files_added_label.get(),
170 "label",
171 )
172 .flags(BindingFlags::SYNC_CREATE)
173 .build();
174
175 cumulative
176 .bind_property(
177 "files-added",
178 &self.cumulative_files_added_label.get(),
179 "label",
180 )
181 .flags(BindingFlags::SYNC_CREATE)
182 .build();
183
•184 stats.connect_download_speed_notify(clone!(
185 #[weak(rename_to = this)]
186 self,
187 move |_| {
188 this.update_format_strings();
189 }
190 ));
191
192 stats.connect_upload_speed_notify(clone!(
193 #[weak(rename_to = this)]
194 self,
195 move |_| {
196 this.update_format_strings();
197 }
198 ));
199
200 current.connect_seconds_active_notify(clone!(
201 #[weak(rename_to = this)]
202 self,
203 move |_| {
204 this.update_format_strings();
205 }
206 ));
207
208 current.connect_downloaded_bytes_notify(clone!(
209 #[weak(rename_to = this)]
210 self,
211 move |_| {
212 this.update_format_strings();
213 }
214 ));
215
216 current.connect_uploaded_bytes_notify(clone!(
217 #[weak(rename_to = this)]
218 self,
219 move |_| {
220 this.update_format_strings();
221 }
222 ));
223
224 cumulative.connect_seconds_active_notify(clone!(
225 #[weak(rename_to = this)]
226 self,
227 move |_| {
228 this.update_format_strings();
229 }
230 ));
231
232 cumulative.connect_downloaded_bytes_notify(clone!(
233 #[weak(rename_to = this)]
234 self,
235 move |_| {
236 this.update_format_strings();
237 }
238 ));
239
240 cumulative.connect_uploaded_bytes_notify(clone!(
241 #[weak(rename_to = this)]
242 self,
243 move |_| {
244 this.update_format_strings();
245 }
246 ));
247
248 cumulative.connect_session_count_notify(clone!(
249 #[weak(rename_to = this)]
250 self,
251 move |_| {
252 this.update_format_strings();
253 }
254 ));
255 }
256
257 fn update_format_strings(&self) {
258 let stats = FrgConnectionManager::default().client().session_stats();
259 let current = stats.current_stats();
260 let cumulative = stats.cumulative_stats();
261
262 let speed = utils::format_speed(stats.download_speed());
263 self.download_speed_label.set_label(&speed);
264
265 let speed = utils::format_speed(stats.upload_speed());
266 self.upload_speed_label.set_label(&speed);
267
268 // Current
269 let time = utils::format_time(current.seconds_active().try_into().unwrap(), false);
270 self.current_time_active_label.set_label(&time);
271
272 let size = utils::format_size(current.downloaded_bytes());
273 self.current_downloaded_label.set_label(&size);
274
275 let size = utils::format_size(current.uploaded_bytes());
276 self.current_uploaded_label.set_label(&size);
277
278 // Cumulative
279 let time = utils::format_time(cumulative.seconds_active().try_into().unwrap(), false);
280 self.cumulative_time_active_label.set_label(&time);
281
282 let size = utils::format_size(cumulative.downloaded_bytes());
283 self.cumulative_downloaded_label.set_label(&size);
284
285 let size = utils::format_size(cumulative.uploaded_bytes());
286 self.cumulative_uploaded_label.set_label(&size);
287
288 let started = cumulative.session_count();
289 let text = ni18n_f(
290 "{} time",
291 "{} times",
292 started.try_into().unwrap_or(2),
293 &[&started.to_string()],
294 );
295 self.cumulative_sessions_label.set_label(&text);
296 }
297
298 fn update_remote_notice(&self) {
299 let cm = FrgConnectionManager::default();
300
301 let is_fragments = cm.current_connection().is_fragments();
302 let title = cm.current_connection().title();
303
304 self.remote_notice_group.set_visible(!is_fragments);
305 self.remote_notice_row
306 .set_title(&i18n_f("You are connected with “{}”", &[&title]));
307 }
308 }
309 }
310
311 glib::wrapper! {
312 pub struct FrgStatsDialog(ObjectSubclass<imp::FrgStatsDialog>)
•313 @extends gtk::Widget, adw::Dialog,
314 @implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget;
315 }
316
317 impl FrgStatsDialog {
318 pub fn new() -> Self {
319 glib::Object::new()
320 }
321 }
322
323 impl Default for FrgStatsDialog {
324 fn default() -> Self {
325 Self::new()
326 }
327 }