| 1 | /* |
| 2 | * Copyright 2024 Andrey Kutejko <[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 <http://www.gnu.org/licenses/>. |
| 16 | * |
| 17 | * For more details see the file COPYING. |
| 18 | */ |
| 19 | |
| 20 | use gtkrust-analyzer cargo gtk4 0.11.1 crate/::{giorust-analyzer cargo gio 0.22.2 crate/, glibrust-analyzer cargo glib 0.22.3 crate/, preluderust-analyzer cargo gtk4 0.11.1 prelude/::*}; |
| 21 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/::RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#; |
| 22 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/; |
| 23 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/; |
| 24 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::{Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#, PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#}; |
| 25 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::{SystemTimerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/SystemTime#, UNIX_EPOCHrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/UNIX_EPOCH.}; |
| 26 | |
| 27 | pub fn list_directoryrust-analyzer cargo gnome-robots 50.0.0 utils/list_directory().(directory: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#>> { |
| 28 | let mut files = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 29 | for entry in fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::read_dirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/read_dir().(directory)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). { |
| 30 | files.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(entry?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[DirEntry]path().()); |
| 31 | } |
| 32 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(files) |
| 33 | } |
| 34 | |
| 35 | pub fn nowrust-analyzer cargo gnome-robots 50.0.0 utils/now().() -> u64 { |
| 36 | SystemTimerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/SystemTime#::nowrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[SystemTime]now().() |
| 37 | .duration_sincerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[SystemTime]duration_since().(UNIX_EPOCHrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/UNIX_EPOCH.) |
| 38 | .map_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_or().(0, |t| t.as_secsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]as_secs().()) |
| 39 | } |
| 40 | |
| 41 | pub trait SettingsWarnExtrust-analyzer cargo gnome-robots 50.0.0 utils/SettingsWarnExt# { |
| 42 | fn set_string_or_warnrust-analyzer cargo gnome-robots 50.0.0 utils/SettingsWarnExt#set_string_or_warn().(&self, key: &str, value: &str); |
| 43 | fn set_int_or_warnrust-analyzer cargo gnome-robots 50.0.0 utils/SettingsWarnExt#set_int_or_warn().(&self, key: &str, value: i32); |
| 44 | fn set_boolean_or_warnrust-analyzer cargo gnome-robots 50.0.0 utils/SettingsWarnExt#set_boolean_or_warn().(&self, key: &str, value: bool); |
| 45 | } |
| 46 | |
| 47 | fn warn_failed_setrust-analyzer cargo gnome-robots 50.0.0 utils/warn_failed_set().(key: &str, error: &dyn stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error#) { |
| 48 | glibrust-analyzer cargo glib 0.22.3 crate/::g_warningrust-analyzer cargo glib 0.22.3 log/g_warning!!(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, "Failed to save setting key {}: {}", key, error); |
| 49 | } |
| 50 | |
| 51 | impl SettingsWarnExtrust-analyzer cargo gnome-robots 50.0.0 utils/SettingsWarnExt# for giorust-analyzer cargo gio 0.22.2 crate/::Settingsrust-analyzer cargo gio 0.22.2 auto/settings/Settings# { |
| 52 | fn set_string_or_warnrust-analyzer cargo gnome-robots 50.0.0 utils/impl#[`gio::Settings`][SettingsWarnExt]set_string_or_warn().(&self, key: &str, value: &str) { |
| 53 | if let Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ref error) = self.set_stringrust-analyzer cargo gio 0.22.2 auto/settings/SettingsExt#set_string().(key, value) { |
| 54 | warn_failed_setrust-analyzer cargo gnome-robots 50.0.0 utils/warn_failed_set().(key, error); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | fn set_int_or_warnrust-analyzer cargo gnome-robots 50.0.0 utils/impl#[`gio::Settings`][SettingsWarnExt]set_int_or_warn().(&self, key: &str, value: i32) { |
| 59 | if let Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ref error) = self.set_intrust-analyzer cargo gio 0.22.2 auto/settings/SettingsExt#set_int().(key, value) { |
| 60 | warn_failed_setrust-analyzer cargo gnome-robots 50.0.0 utils/warn_failed_set().(key, error); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | fn set_boolean_or_warnrust-analyzer cargo gnome-robots 50.0.0 utils/impl#[`gio::Settings`][SettingsWarnExt]set_boolean_or_warn().(&self, key: &str, value: bool) { |
| 65 | if let Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ref error) = self.set_booleanrust-analyzer cargo gio 0.22.2 auto/settings/SettingsExt#set_boolean().(key, value) { |
| 66 | warn_failed_setrust-analyzer cargo gnome-robots 50.0.0 utils/warn_failed_set().(key, error); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | pub fn settings_aware_widgetrust-analyzer cargo gnome-robots 50.0.0 utils/settings_aware_widget().<W, F>( |
| 72 | widget: &W, |
| 73 | settings: &giorust-analyzer cargo gio 0.22.2 crate/::Settingsrust-analyzer cargo gio 0.22.2 auto/settings/Settings#, |
| 74 | settings_key: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&str>, |
| 75 | f: F, |
| 76 | ) where |
| 77 | W: IsArust-analyzer cargo glib 0.22.3 object/IsA#<gtkrust-analyzer cargo gtk4 0.11.1 crate/::Widgetrust-analyzer cargo gtk4 0.11.1 auto/widget/Widget#>, |
| 78 | W: glibrust-analyzer cargo glib 0.22.3 crate/::clonerust-analyzer cargo glib 0.22.3 clone/::Downgraderust-analyzer cargo glib 0.22.3 clone/Downgrade#, |
| 79 | <W as glibrust-analyzer cargo glib 0.22.3 crate/::clonerust-analyzer cargo glib 0.22.3 clone/::Downgraderust-analyzer cargo glib 0.22.3 clone/Downgrade#>::Weakrust-analyzer cargo glib 0.22.3 clone/Downgrade#Weak#: glibrust-analyzer cargo glib 0.22.3 crate/::clonerust-analyzer cargo glib 0.22.3 clone/::Upgraderust-analyzer cargo glib 0.22.3 clone/Upgrade#<Strongrust-analyzer cargo glib 0.22.3 clone/Upgrade#Strong# = W>, |
| 80 | F: Fnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/Fn#(&W, &giorust-analyzer cargo gio 0.22.2 crate/::Settingsrust-analyzer cargo gio 0.22.2 auto/settings/Settings#) + 'static, |
| 81 | { |
| 82 | let signal_handler_id = settings.connect_changedrust-analyzer cargo gio 0.22.2 auto/settings/SettingsExt#connect_changed().( |
| 83 | settings_key, |
| 84 | glibrust-analyzer cargo glib 0.22.3 crate/::clonerust-analyzer cargo glib-macros 0.22.2 clone!!( |
| 85 | #[weak] |
| 86 | widget, |
| 87 | move |s, _| (f)(&widget, s) |
| 88 | ), |
| 89 | ); |
| 90 | widget.connect_destroyrust-analyzer cargo gtk4 0.11.1 auto/widget/WidgetExt#connect_destroy().({ |
| 91 | let signal_handler_id = RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]new().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(signal_handler_id)); |
| 92 | let settings = settings.clonerust-analyzer cargo gio 0.22.2 auto/settings/impl#[Settings][Clone]clone().(); |
| 93 | move |_| { |
| 94 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(signal_handler_id) = signal_handler_id.takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]take().() { |
| 95 | settings.disconnectrust-analyzer cargo glib 0.22.3 object/impl#[T][ObjectExt]disconnect().(signal_handler_id); |
| 96 | } |
| 97 | } |
| 98 | }); |
| 99 | } |