From fb8f804e09c4505773e0324302c565df02ba250d Mon Sep 17 00:00:00 2001 From: peijiankang Date: Mon, 18 Sep 2023 10:55:57 +0800 Subject: [PATCH] Addition of FormatsLocale property and SetFormatsLocale --- data/org.freedesktop.Accounts.User.xml | 45 +++++++++++++++++++ src/libaccountsservice/act-user.c | 60 ++++++++++++++++++++++++++ src/libaccountsservice/act-user.h | 3 ++ src/user.c | 57 ++++++++++++++++++++++++ 4 files changed, 165 insertions(+) diff --git a/data/org.freedesktop.Accounts.User.xml b/data/org.freedesktop.Accounts.User.xml index 7a8a915..c750878 100644 --- a/data/org.freedesktop.Accounts.User.xml +++ b/data/org.freedesktop.Accounts.User.xml @@ -191,6 +191,41 @@ + + + + + + The new regional formats, as a locale specification like "de_DE.UTF-8". + + + + + + + Sets the users regional formats. + + + + The caller needs one of the following PolicyKit authorizations: + + + org.freedesktop.accounts.change-own-user-data + To change regional formats + + + org.freedesktop.accounts.user-administration + To change egional formats of another user + + + + + if the caller lacks the appropriate PolicyKit authorization + if the operation failed + + + + @@ -871,6 +906,16 @@ + + + + + The users regional formats, as a locale specification like "de_DE.UTF-8". + + + + + diff --git a/src/libaccountsservice/act-user.c b/src/libaccountsservice/act-user.c index 9125d43..daf634c 100644 --- a/src/libaccountsservice/act-user.c +++ b/src/libaccountsservice/act-user.c @@ -93,6 +93,7 @@ enum PROP_BACKGROUND_FILE, PROP_ICON_FILE, PROP_LANGUAGE, + PROP_FORMATS_LOCALE, PROP_X_SESSION, PROP_IS_LOADED }; @@ -273,6 +274,7 @@ act_user_class_init (ActUserClass *class) "The real name to display for this user.", NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); + g_object_class_install_property (gobject_class, PROP_BACKGROUND_FILE, g_param_spec_string ("background-file", @@ -406,6 +408,15 @@ act_user_class_init (ActUserClass *class) "User's locale.", NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); + + g_object_class_install_property (gobject_class, + PROP_FORMATS_LOCALE, + g_param_spec_string ("formats-locale", + "Regional Formats", + "User's regional formats.", + NULL, + G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); + g_object_class_install_property (gobject_class, PROP_X_SESSION, g_param_spec_string ("x-session", @@ -1086,6 +1097,25 @@ act_user_get_language (ActUser *user) return accounts_user_get_language (user->accounts_proxy); } +/** + * act_user_get_formats_locale: + * @user: a #ActUser + * + * Returns the path to the configured formats locale of @user. + * + * Returns: (transfer none): a path to an icon + */ +const char * +act_user_get_formats_locale (ActUser *user) +{ + g_return_val_if_fail (ACT_IS_USER (user), NULL); + + if (user->accounts_proxy == NULL) + return NULL; + + return accounts_user_get_formats_locale (user->accounts_proxy); +} + /** * act_user_get_x_session: * @user: a #ActUser @@ -1440,6 +1470,36 @@ act_user_set_password_expiration_policy (ActUser *user, } } +/** + * act_user_set_formats_locale: + * @user: the user object to alter. + * @formats_locale: a locale (e.g. en_US.utf8) + * + * Assigns a new formats locale for @user. + * + * Note this function is synchronous and ignores errors. + **/ +void +act_user_set_formats_locale (ActUser *user, + const char *formats_locale) +{ + g_autoptr(GError) error = NULL; + + g_return_if_fail (ACT_IS_USER (user)); + g_return_if_fail (formats_locale != NULL); + g_return_if_fail (ACCOUNTS_IS_USER (user->accounts_proxy)); + + if (!accounts_user_call_set_formats_locale_sync (user->accounts_proxy, + formats_locale, + G_DBUS_CALL_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION, + -1, + NULL, + &error)) { + g_warning ("SetFormatsLocale for formats_locale %s call failed: %s", formats_locale, error->message); + return; + } +} + /** * act_user_set_email: * @user: the user object to alter. diff --git a/src/libaccountsservice/act-user.h b/src/libaccountsservice/act-user.h index 82019f5..4281553 100644 --- a/src/libaccountsservice/act-user.h +++ b/src/libaccountsservice/act-user.h @@ -72,6 +72,7 @@ gboolean act_user_is_nonexistent (ActUser *user); const char *act_user_get_background_file (ActUser *user); const char *act_user_get_icon_file (ActUser *user); const char *act_user_get_language (ActUser *user); +const char *act_user_get_formats_locale (ActUser *user); const char * const *act_user_get_languages (ActUser *user); const char *act_user_get_x_session (ActUser *user); const char *act_user_get_session (ActUser *user); @@ -101,6 +102,8 @@ void act_user_set_user_expiration_policy (ActUser *user, void act_user_set_language (ActUser *user, const char *language); +void act_user_set_formats_locale (ActUser *user, + const char *formats_locale); void act_user_set_background_file (ActUser *user, const char *background_file); void act_user_set_languages (ActUser *user, diff --git a/src/user.c b/src/user.c index 16a1cbd..9b5cf7d 100644 --- a/src/user.c +++ b/src/user.c @@ -545,6 +545,11 @@ user_update_from_keyfile (User *user, accounts_user_set_language (ACCOUNTS_USER (user), s); g_clear_pointer (&s, g_free); } + s = g_key_file_get_string (keyfile, "User", "FormatsLocale", NULL); + if (s != NULL) { + accounts_user_set_formats_locale (ACCOUNTS_USER (user), s); + g_clear_pointer (&s, g_free); + } s = g_key_file_get_string (keyfile, "User", "XSession", NULL); if (s != NULL) { @@ -672,6 +677,9 @@ user_save_to_keyfile (User *user, if (accounts_user_get_session_type (ACCOUNTS_USER (user))) g_key_file_set_string (keyfile, "User", "SessionType", accounts_user_get_session_type (ACCOUNTS_USER (user))); + if (accounts_user_get_formats_locale (ACCOUNTS_USER (user))) + g_key_file_set_string (keyfile, "User", "FormatsLocale", accounts_user_get_formats_locale (ACCOUNTS_USER (user))); + if (accounts_user_get_xsession (ACCOUNTS_USER (user))) g_key_file_set_string (keyfile, "User", "XSession", accounts_user_get_xsession (ACCOUNTS_USER (user))); @@ -1583,6 +1591,54 @@ user_set_session_type (AccountsUser *auser, return TRUE; } +static void +user_change_formats_locale_authorized_cb (Daemon *daemon, + User *user, + GDBusMethodInvocation *context, + gpointer user_data) + +{ + const gchar *formats_locale = user_data; + + if (g_strcmp0 (accounts_user_get_formats_locale (ACCOUNTS_USER (user)), formats_locale) != 0) { + accounts_user_set_formats_locale (ACCOUNTS_USER (user), formats_locale); + + save_extra_data (user); + } + + accounts_user_complete_set_formats_locale (ACCOUNTS_USER (user), context); +} + +static gboolean +user_set_formats_locale (AccountsUser *auser, + GDBusMethodInvocation *context, + const gchar *formats_locale) +{ + User *user = (User*)auser; + int uid; + const gchar *action_id; + + if (!get_caller_uid (context, &uid)) { + throw_error (context, ERROR_FAILED, "identifying caller failed"); + return FALSE; + } + + if (accounts_user_get_uid (ACCOUNTS_USER (user)) == (uid_t) uid) + action_id = "org.freedesktop.accounts.change-own-user-data"; + else + action_id = "org.freedesktop.accounts.user-administration"; + + daemon_local_check_auth (user->daemon, + user, + action_id, + user_change_formats_locale_authorized_cb, + context, + g_strdup (formats_locale), + (GDestroyNotify) g_free); + + return TRUE; +} + static void user_change_x_session_authorized_cb (Daemon *daemon, User *user, @@ -2802,6 +2858,7 @@ user_accounts_user_iface_init (AccountsUserIface *iface) iface->handle_set_automatic_login = user_set_automatic_login; iface->handle_set_background_file = user_set_background_file; iface->handle_set_email = user_set_email; + iface->handle_set_formats_locale = user_set_formats_locale; iface->handle_set_home_directory = user_set_home_directory; iface->handle_set_icon_file = user_set_icon_file; iface->handle_set_language = user_set_language; -- 2.27.0