Debian Codegraph @

podcasts-gtk/src/settings.rs

raw symbols SCIP
1 // prefs.rs
2 //
3 // Copyright 2018 Measly Twerp <[email protected]>
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // SPDX-License-Identifier: GPL-3.0-or-later
19 use gio::{Settings, prelude::SettingsExt};
20 use gtk::gio;
21 use gtk::prelude::GtkWindowExt;
22
23 use chrono::Duration;
24 use chrono::prelude::*;
25
26 pub(crate) struct WindowGeometry {
27 width: i32,
28 height: i32,
29 is_maximized: bool,
30 }
31
32 impl WindowGeometry {
33 pub(crate) fn from_window(window: &adw::ApplicationWindow) -> WindowGeometry {
34 let size = window.default_size();
35 let width = size.0;
36 let height = size.1;
37 let is_maximized = window.is_maximized();
38
39 WindowGeometry {
40 width,
41 height,
42 is_maximized,
43 }
44 }
45
46 pub(crate) fn from_settings(settings: &gio::Settings) -> WindowGeometry {
47 let width = settings.int("persist-window-geometry-width");
48 let height = settings.int("persist-window-geometry-height");
49 let is_maximized = settings.boolean("persist-window-geometry-maximized");
50
51 WindowGeometry {
52 width,
53 height,
54 is_maximized,
55 }
56 }
57
58 pub(crate) fn apply(&self, window: &adw::ApplicationWindow) {
59 if self.width > 0 && self.height > 0 {
60 window.set_default_size(self.width, self.height);
61 }
62
63 if self.is_maximized {
64 window.maximize();
65 }
66 }
67
68 pub(crate) fn write(&self, settings: &gio::Settings) {
69 settings
70 .set_int("persist-window-geometry-width", self.width)
71 .unwrap();
72 settings
73 .set_int("persist-window-geometry-height", self.height)
74 .unwrap();
75 settings
76 .set_boolean("persist-window-geometry-maximized", self.is_maximized)
77 .unwrap();
78 }
79 }
80
81 pub(crate) fn get_refresh_interval(settings: &Settings) -> Duration {
82 let time = i64::from(settings.int("refresh-interval-time"));
83 let period = settings.string("refresh-interval-period");
84
85 time_period_to_duration(time, period.as_str())
86 }
87
88 pub(crate) fn get_cleanup_date(settings: &Settings) -> DateTime<Utc> {
89 let time = i64::from(settings.int("cleanup-age-time"));
90 let period = settings.string("cleanup-age-period");
91 let duration = time_period_to_duration(time, period.as_str());
92
93 Utc::now() - duration
94 }
95
96 pub(crate) fn time_period_to_duration(time: i64, period: &str) -> Duration {
97 match period {
98 "weeks" => Duration::weeks(time),
99 "days" => Duration::days(time),
100 "hours" => Duration::hours(time),
101 "minutes" => Duration::minutes(time),
102 _ => Duration::seconds(time),
103 }
104 }
105
106 #[test]
107 fn test_time_period_to_duration() {
108 let time = 2;
109 let week = 604800 * time;
110 let day = 86400 * time;
111 let hour = 3600 * time;
112 let minute = 60 * time;
113
114 assert_eq!(week, time_period_to_duration(time, "weeks").num_seconds());
115 assert_eq!(day, time_period_to_duration(time, "days").num_seconds());
116 assert_eq!(hour, time_period_to_duration(time, "hours").num_seconds());
117 assert_eq!(
118 minute,
119 time_period_to_duration(time, "minutes").num_seconds()
120 );
121 assert_eq!(time, time_period_to_duration(time, "seconds").num_seconds());
122 }
123
124 // #[test]
125 // fn test_apply_window_geometry() {
126 // gtk::init().expect("Error initializing gtk.");
127
128 // let window = gtk::Window::new(gtk::WindowType::Toplevel);
129 // let _geometry = WindowGeometry {
130 // left: 0,
131 // top: 0,
132 // width: 100,
133 // height: 100,
134 // is_maximized: true
135 // };
136
137 // assert!(!window.is_maximized());
138
139 // window.show();
140 // window.activate();
141 // geometry.apply(&window);
142
143 // assert!(window.is_maximized());
144 // }