transmission_gobject/
session_stats.rs

1use std::cell::Cell;
2
3use glib::Properties;
4use glib::object::ObjectExt;
5use glib::subclass::prelude::{ObjectSubclass, *};
6use transmission_client::SessionStats;
7
8use crate::TrSessionStatsDetails;
9
10mod imp {
11    use super::*;
12
13    #[derive(Debug, Default, Properties)]
14    #[properties(wrapper_type = super::TrSessionStats)]
15    pub struct TrSessionStats {
16        #[property(get, minimum = 1, default_value = 1)]
17        pub torrent_count: Cell<i32>,
18        #[property(get, minimum = 1, default_value = 1)]
19        pub active_torrent_count: Cell<i32>,
20        #[property(get, minimum = 1, default_value = 1)]
21        pub paused_torrent_count: Cell<i32>,
22        #[property(get, minimum = 1, default_value = 1)]
23        pub downloaded_torrent_count: Cell<i32>,
24        #[property(get, minimum = 1, default_value = 1)]
25        pub download_speed: Cell<i32>,
26        #[property(get, minimum = 1, default_value = 1)]
27        pub upload_speed: Cell<i32>,
28
29        #[property(get)]
30        pub cumulative_stats: TrSessionStatsDetails,
31        #[property(get)]
32        pub current_stats: TrSessionStatsDetails,
33    }
34
35    #[glib::object_subclass]
36    impl ObjectSubclass for TrSessionStats {
37        const NAME: &'static str = "TrSessionStats";
38        type ParentType = glib::Object;
39        type Type = super::TrSessionStats;
40    }
41
42    #[glib::derived_properties]
43    impl ObjectImpl for TrSessionStats {}
44}
45
46glib::wrapper! {
47    pub struct TrSessionStats(ObjectSubclass<imp::TrSessionStats>);
48}
49
50impl TrSessionStats {
51    pub(crate) fn refresh_values(&self, rpc_stats: SessionStats, download_count: i32) {
52        let imp = self.imp();
53
54        // torrent_count
55        if imp.torrent_count.get() != rpc_stats.torrent_count {
56            imp.torrent_count.set(rpc_stats.torrent_count);
57            self.notify_torrent_count();
58        }
59
60        // active_torrent_count
61        if imp.active_torrent_count.get() != rpc_stats.active_torrent_count {
62            imp.active_torrent_count.set(rpc_stats.active_torrent_count);
63            self.notify_active_torrent_count();
64        }
65
66        // paused_torrent_count
67        if imp.paused_torrent_count.get() != rpc_stats.paused_torrent_count {
68            imp.paused_torrent_count.set(rpc_stats.paused_torrent_count);
69            self.notify_paused_torrent_count();
70        }
71
72        // downloaded_torrent_count
73        if imp.downloaded_torrent_count.get() != download_count {
74            imp.downloaded_torrent_count.set(download_count);
75            self.notify_downloaded_torrent_count();
76        }
77
78        // download_speed
79        if imp.download_speed.get() != rpc_stats.download_speed {
80            imp.download_speed.set(rpc_stats.download_speed);
81            self.notify_download_speed();
82        }
83
84        // upload_speed
85        if imp.upload_speed.get() != rpc_stats.upload_speed {
86            imp.upload_speed.set(rpc_stats.upload_speed);
87            self.notify_upload_speed();
88        }
89
90        imp.cumulative_stats
91            .refresh_values(rpc_stats.cumulative_stats);
92        imp.current_stats.refresh_values(rpc_stats.current_stats);
93    }
94}
95
96impl Default for TrSessionStats {
97    fn default() -> Self {
98        glib::Object::new()
99    }
100}