Debian Codegraph @

src/ui/stats_dialog.rs

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 // Fragments - stats_dialog.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 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(
95 clone!(@weak self as this => move |_|{
96 this.update_remote_notice();
97 }),
98 );
99
100 self.bind_properties();
101 self.update_format_strings();
102 self.update_remote_notice();
103 }
104 }
105
106 impl WidgetImpl for FrgStatsDialog {}
107
108 impl AdwDialogImpl for FrgStatsDialog {}
109
110 impl FrgStatsDialog {
111 fn bind_properties(&self) {
112 let client = FrgConnectionManager::default().client();
113 let session = client.session();
114 let stats = client.session_stats();
115 let current = stats.current_stats();
116 let cumulative = stats.cumulative_stats();
117
118 session
119 .bind_property("version", &self.version_label.get(), "label")
120 .flags(BindingFlags::SYNC_CREATE)
121 .build();
122
123 stats
124 .bind_property("torrent-count", &self.torrents_label.get(), "label")
125 .flags(BindingFlags::SYNC_CREATE)
126 .build();
127
128 stats
129 .bind_property(
130 "active-torrent-count",
131 &self.active_torrents_label.get(),
132 "label",
133 )
134 .flags(BindingFlags::SYNC_CREATE)
135 .build();
136
137 stats
138 .bind_property(
139 "paused-torrent-count",
140 &self.paused_torrents_label.get(),
141 "label",
142 )
143 .flags(BindingFlags::SYNC_CREATE)
144 .build();
145
146 stats
147 .bind_property(
148 "downloaded-torrent-count",
149 &self.downloaded_torrents_label.get(),
150 "label",
151 )
152 .flags(BindingFlags::SYNC_CREATE)
153 .build();
154
155 stats
156 .bind_property(
157 "downloaded-torrent-count",
158 &self.downloaded_torrents_label.get(),
159 "label",
160 )
161 .flags(BindingFlags::SYNC_CREATE)
162 .build();
163
164 current
165 .bind_property(
166 "files-added",
167 &self.current_files_added_label.get(),
168 "label",
169 )
170 .flags(BindingFlags::SYNC_CREATE)
171 .build();
172
173 cumulative
174 .bind_property(
175 "files-added",
176 &self.cumulative_files_added_label.get(),
177 "label",
178 )
179 .flags(BindingFlags::SYNC_CREATE)
180 .build();
181
182 stats.connect_download_speed_notify(clone!(@weak self as this => move |_|{
183 this.update_format_strings();
184 }));
185
186 stats.connect_upload_speed_notify(clone!(@weak self as this => move |_|{
187 this.update_format_strings();
188 }));
189
190 current.connect_seconds_active_notify(clone!(@weak self as this => move |_|{
191 this.update_format_strings();
192 }));
193
194 current.connect_downloaded_bytes_notify(clone!(@weak self as this => move |_|{
195 this.update_format_strings();
196 }));
197
198 current.connect_uploaded_bytes_notify(clone!(@weak self as this => move |_|{
199 this.update_format_strings();
200 }));
201
202 cumulative.connect_seconds_active_notify(clone!(@weak self as this => move |_|{
203 this.update_format_strings();
204 }));
205
206 cumulative.connect_downloaded_bytes_notify(clone!(@weak self as this => move |_|{
207 this.update_format_strings();
208 }));
209
210 cumulative.connect_uploaded_bytes_notify(clone!(@weak self as this => move |_|{
211 this.update_format_strings();
212 }));
213
214 cumulative.connect_session_count_notify(clone!(@weak self as this => move |_|{
215 this.update_format_strings();
216 }));
217 }
218
219 fn update_format_strings(&self) {
220 let stats = FrgConnectionManager::default().client().session_stats();
221 let current = stats.current_stats();
222 let cumulative = stats.cumulative_stats();
223
224 let speed = utils::format_speed(stats.download_speed());
225 self.download_speed_label.set_label(&speed);
226
227 let speed = utils::format_speed(stats.upload_speed());
228 self.upload_speed_label.set_label(&speed);
229
230 // Current
231 let time = utils::format_time(current.seconds_active().try_into().unwrap(), false);
232 self.current_time_active_label.set_label(&time);
233
234 let size = utils::format_size(current.downloaded_bytes());
235 self.current_downloaded_label.set_label(&size);
236
237 let size = utils::format_size(current.uploaded_bytes());
238 self.current_uploaded_label.set_label(&size);
239
240 // Cumulative
241 let time = utils::format_time(cumulative.seconds_active().try_into().unwrap(), false);
242 self.cumulative_time_active_label.set_label(&time);
243
244 let size = utils::format_size(cumulative.downloaded_bytes());
245 self.cumulative_downloaded_label.set_label(&size);
246
247 let size = utils::format_size(cumulative.uploaded_bytes());
248 self.cumulative_uploaded_label.set_label(&size);
249
250 let started = cumulative.session_count();
251 let text = ni18n_f(
252 "{} time",
253 "{} times",
254 started.try_into().unwrap_or(2),
255 &[&started.to_string()],
256 );
257 self.cumulative_sessions_label.set_label(&text);
258 }
259
260 fn update_remote_notice(&self) {
261 let cm = FrgConnectionManager::default();
262
263 let is_fragments = cm.current_connection().is_fragments();
264 let title = cm.current_connection().title();
265
266 self.remote_notice_group.set_visible(!is_fragments);
267 self.remote_notice_row
268 .set_title(&i18n_f("You are connected with “{}”", &[&title]));
269 }
270 }
271 }
272
273 glib::wrapper! {
274 pub struct FrgStatsDialog(ObjectSubclass<imp::FrgStatsDialog>)
275 @extends gtk::Widget, adw::Dialog;
276 }
277
278 impl FrgStatsDialog {
279 pub fn new() -> Self {
280 glib::Object::new()
281 }
282 }
283
284 impl Default for FrgStatsDialog {
285 fn default() -> Self {
286 Self::new()
287 }
288 }