From be2cabb0d49b0ca4d95c49a0e55faa100b83e512 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Imobach=20Gonz=C3=A1lez=20Sosa?= Date: Wed, 29 Nov 2023 07:05:11 +0000 Subject: [PATCH] Read the list of keymaps once --- rust/agama-dbus-server/src/locale.rs | 14 +++++--- rust/agama-dbus-server/src/locale/keyboard.rs | 33 ++++++++++++++++++- rust/agama-dbus-server/src/locale/locale.rs | 1 + rust/agama-dbus-server/src/locale/timezone.rs | 1 + 4 files changed, 43 insertions(+), 6 deletions(-) diff --git a/rust/agama-dbus-server/src/locale.rs b/rust/agama-dbus-server/src/locale.rs index d94b102ba9..01e734dbe6 100644 --- a/rust/agama-dbus-server/src/locale.rs +++ b/rust/agama-dbus-server/src/locale.rs @@ -6,7 +6,7 @@ mod timezone; use crate::error::Error; use agama_locale_data::KeymapId; use anyhow::Context; -use keyboard::{get_keymaps, Keymap}; +use keyboard::KeymapsDatabase; use locale::LocalesDatabase; use std::process::Command; use timezone::TimezonesDatabase; @@ -18,7 +18,7 @@ pub struct Locale { locales: Vec, locales_db: LocalesDatabase, keymap: KeymapId, - keymaps: Vec, + keymaps_db: KeymapsDatabase, ui_locale: String, } @@ -88,7 +88,8 @@ impl Locale { /// * The name of the keyboard in language set by the UILocale property. fn list_keymaps(&self) -> Result, Error> { let keymaps = self - .keymaps + .keymaps_db + .entries() .iter() .map(|k| (k.id.to_string(), k.localized_description())) .collect(); @@ -106,7 +107,7 @@ impl Locale { .parse() .map_err(|_e| zbus::fdo::Error::InvalidArgs("Invalid keymap".to_string()))?; - if !self.keymaps.iter().any(|k| k.id == keymap_id) { + if !self.keymaps_db.exists(&keymap_id) { return Err(zbus::fdo::Error::Failed( "Invalid keyboard value".to_string(), )); @@ -184,13 +185,16 @@ impl Locale { timezones_db.read(&locale)?; let default_timezone = timezones_db.entries().get(0).unwrap(); + let mut keymaps_db = KeymapsDatabase::new(); + keymaps_db.read()?; + let locale = Self { keymap: "us".parse().unwrap(), timezone: default_timezone.code.to_string(), locales: vec![default_locale.code.to_string()], locales_db, timezones_db, - keymaps: get_keymaps()?, + keymaps_db, ui_locale: locale.to_string(), }; diff --git a/rust/agama-dbus-server/src/locale/keyboard.rs b/rust/agama-dbus-server/src/locale/keyboard.rs index 449b632f87..8a286bf4f6 100644 --- a/rust/agama-dbus-server/src/locale/keyboard.rs +++ b/rust/agama-dbus-server/src/locale/keyboard.rs @@ -21,11 +21,42 @@ impl Keymap { } } +/// Represents the keymaps database. +/// +/// The list of supported keymaps is read from `systemd-localed` and the +/// descriptions from the X Keyboard Configuraiton Database (see +/// `agama_locale_data::XkbConfigRegistry`). +#[derive(Default)] +pub struct KeymapsDatabase { + keymaps: Vec, +} + +impl KeymapsDatabase { + pub fn new() -> Self { + Self::default() + } + + /// Reads the list of keymaps. + pub fn read(&mut self) -> anyhow::Result<()> { + self.keymaps = get_keymaps()?; + Ok(()) + } + + pub fn exists(&self, id: &KeymapId) -> bool { + self.keymaps.iter().any(|k| &k.id == id) + } + + /// Returns the list of keymaps. + pub fn entries(&self) -> &Vec { + &self.keymaps + } +} + /// Returns the list of keymaps to offer. /// /// It only includes the keyboards supported by `localectl` but getting /// the description from the X Keyboard Configuration Database. -pub fn get_keymaps() -> anyhow::Result> { +fn get_keymaps() -> anyhow::Result> { let mut keymaps: Vec = vec![]; let xkb_descriptions = get_keymap_descriptions(); let keymap_ids = get_localectl_keymaps()?; diff --git a/rust/agama-dbus-server/src/locale/locale.rs b/rust/agama-dbus-server/src/locale/locale.rs index fb64845ffa..3dbeb0292e 100644 --- a/rust/agama-dbus-server/src/locale/locale.rs +++ b/rust/agama-dbus-server/src/locale/locale.rs @@ -62,6 +62,7 @@ impl LocalesDatabase { false } + /// Returns the list of locales. pub fn entries(&self) -> &Vec { &self.locales } diff --git a/rust/agama-dbus-server/src/locale/timezone.rs b/rust/agama-dbus-server/src/locale/timezone.rs index 74c9aa6671..8b60197a45 100644 --- a/rust/agama-dbus-server/src/locale/timezone.rs +++ b/rust/agama-dbus-server/src/locale/timezone.rs @@ -35,6 +35,7 @@ impl TimezonesDatabase { self.timezones.iter().any(|t| &t.code == timezone) } + /// Returns the list of timezones. pub fn entries(&self) -> &Vec { &self.timezones }