| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 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 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|