From 634f3916253f113b98bbd6abd94f42d62c02c34e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Corentin=20No=C3=ABl?= Date: Sun, 2 Jun 2019 21:29:21 +0200 Subject: [PATCH] Get the time format from the new key and make it more efficient --- src/LanguagesFormat.vala | 61 +++++++++++++++++++++++++++++----------- 1 file changed, 45 insertions(+), 16 deletions(-) diff --git a/src/LanguagesFormat.vala b/src/LanguagesFormat.vala index 296485c7e..9bbb89c17 100644 --- a/src/LanguagesFormat.vala +++ b/src/LanguagesFormat.vala @@ -16,6 +16,50 @@ // namespace Maya.Settings { + [DBus (name = "io.elementary.greeter.AccountsService")] + interface Greeter.AccountsService : Object { + public abstract string time_format { owned get; set; } + } + + [DBus (name = "org.freedesktop.Accounts")] + interface FDO.Accounts : Object { + public abstract string find_user_by_name (string username) throws GLib.Error; + } + + public class TimeFormatHolder : Object { + private static TimeFormatHolder instance; + public static unowned TimeFormatHolder get_instance () { + if (instance == null) { + instance = new TimeFormatHolder (); + } + + return instance; + } + + public bool is_12h { get; private set; default = false; } + private Greeter.AccountsService? greeter_act = null; + + construct { + try { + var accounts_service = GLib.Bus.get_proxy_sync (GLib.BusType.SYSTEM, + "org.freedesktop.Accounts", + "/org/freedesktop/Accounts"); + var user_path = accounts_service.find_user_by_name (GLib.Environment.get_user_name ()); + greeter_act = GLib.Bus.get_proxy_sync (GLib.BusType.SYSTEM, + "org.freedesktop.Accounts", + user_path, + GLib.DBusProxyFlags.GET_INVALIDATED_PROPERTIES); + is_12h = greeter_act.time_format == "12h" ? true : false; + } catch (Error e) { + critical (e.message); + var setting = new GLib.Settings ("org.gnome.desktop.interface"); + GLib.Variant? clockformat = setting.get_user_value ("clock-format"); + if (clockformat != null) { + is_12h = clockformat.get_string ().contains ("12h"); + } + } + } + } public string DateFormat () { return _("%B %e, %Y"); @@ -26,22 +70,7 @@ namespace Maya.Settings { } public string TimeFormat () { - // If AM/PM doesn't exist, use 24h. - if (Posix.nl_langinfo (Posix.NLItem.AM_STR) == null || Posix.nl_langinfo (Posix.NLItem.AM_STR) == "") { - return Granite.DateTime.get_default_time_format (false); - } - - // If AM/PM exists, assume it is the default time format and check for format override. - var setting = new GLib.Settings ("org.gnome.desktop.interface"); - var clockformat = setting.get_user_value ("clock-format"); - if (clockformat == null) - return Granite.DateTime.get_default_time_format (true); - - if (clockformat.get_string ().contains ("12h")) { - return Granite.DateTime.get_default_time_format (true); - } else { - return Granite.DateTime.get_default_time_format (false); - } + return Granite.DateTime.get_default_time_format (TimeFormatHolder.get_instance ().is_12h); } }