|
| 1 | + /*- |
| 2 | + * Copyright 2021 Alexander Mikhaylenko <alexm@gnome.org> |
| 3 | + * |
| 4 | + * This library is free software; you can redistribute it and/or |
| 5 | + * modify it under the terms of the GNU Library General Public |
| 6 | + * License as published by the Free Software Foundation; either |
| 7 | + * version 2 of the License, or (at your option) any later version. |
| 8 | + * |
| 9 | + * This library 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 GNU |
| 12 | + * Library General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Library General Public |
| 15 | + * License along with this library; if not, write to the |
| 16 | + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor |
| 17 | + * Boston, MA 02110-1335 USA. |
| 18 | + */ |
| 19 | + |
| 20 | +[DBus (name = "io.elementary.pantheon.AccountsService")] |
| 21 | +private interface Pantheon.AccountsService : Object { |
| 22 | + public abstract int prefers_color_scheme { owned get; set; } |
| 23 | +} |
| 24 | + |
| 25 | +[DBus (name = "org.freedesktop.Accounts")] |
| 26 | +interface FDO.Accounts : Object { |
| 27 | + public abstract string find_user_by_name (string username) throws GLib.Error; |
| 28 | +} |
| 29 | + |
| 30 | +private class AccountsServiceMonitor : GLib.Object { |
| 31 | + private FDO.Accounts? accounts_service = null; |
| 32 | + private Pantheon.AccountsService? pantheon_act = null; |
| 33 | + private string user_path; |
| 34 | + |
| 35 | + public int32 color_scheme { get; set; } |
| 36 | + |
| 37 | + construct { |
| 38 | + setup_user_path (); |
| 39 | + setup_prefers_color_scheme (); |
| 40 | + } |
| 41 | + |
| 42 | + private void setup_user_path () { |
| 43 | + try { |
| 44 | + accounts_service = GLib.Bus.get_proxy_sync ( |
| 45 | + GLib.BusType.SYSTEM, |
| 46 | + "org.freedesktop.Accounts", |
| 47 | + "/org/freedesktop/Accounts" |
| 48 | + ); |
| 49 | + |
| 50 | + user_path = accounts_service.find_user_by_name (GLib.Environment.get_user_name ()); |
| 51 | + } catch (Error e) { |
| 52 | + critical (e.message); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + private void setup_prefers_color_scheme () { |
| 57 | + try { |
| 58 | + pantheon_act = GLib.Bus.get_proxy_sync ( |
| 59 | + GLib.BusType.SYSTEM, |
| 60 | + "org.freedesktop.Accounts", |
| 61 | + user_path, |
| 62 | + GLib.DBusProxyFlags.GET_INVALIDATED_PROPERTIES |
| 63 | + ); |
| 64 | + |
| 65 | + color_scheme = pantheon_act.prefers_color_scheme; |
| 66 | + |
| 67 | + ((GLib.DBusProxy) pantheon_act).g_properties_changed.connect ((changed, invalid) => { |
| 68 | + var value = changed.lookup_value ("PrefersColorScheme", new VariantType ("i")); |
| 69 | + if (value != null) { |
| 70 | + color_scheme = value.get_int32 (); |
| 71 | + } |
| 72 | + }); |
| 73 | + } catch (Error e) { |
| 74 | + critical (e.message); |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +[DBus (name = "org.freedesktop.impl.portal.Settings")] |
| 80 | +public class SettingsDaemon.Settings : GLib.Object { |
| 81 | + private GLib.DBusConnection connection; |
| 82 | + |
| 83 | + public uint32 version { |
| 84 | + get { return 1; } |
| 85 | + } |
| 86 | + |
| 87 | + public signal void setting_changed (string namespace, string key, GLib.Variant value); |
| 88 | + |
| 89 | + private AccountsServiceMonitor monitor; |
| 90 | + |
| 91 | + public Settings (GLib.DBusConnection connection) { |
| 92 | + this.connection = connection; |
| 93 | + } |
| 94 | + |
| 95 | + construct { |
| 96 | + monitor = new AccountsServiceMonitor (); |
| 97 | + monitor.notify["color-scheme"].connect (() => { |
| 98 | + var color_scheme = new Variant.uint32 (monitor.color_scheme); |
| 99 | + setting_changed ("org.freedesktop.appearance", "color-scheme", get_color_scheme ()); |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + private bool namespace_matches (string namespace, string[] patterns) { |
| 104 | + foreach (var pattern in patterns) { |
| 105 | + if (pattern[0] == '\0') { |
| 106 | + return true; |
| 107 | + } |
| 108 | + |
| 109 | + if (pattern == namespace) { |
| 110 | + return true; |
| 111 | + } |
| 112 | + |
| 113 | + int pattern_len = pattern.length; |
| 114 | + if (pattern[pattern_len - 1] == '*' && Posix.strncmp (namespace, pattern, pattern_len - 1) == 0) { |
| 115 | + return true; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return patterns.length == 0; |
| 120 | + } |
| 121 | + |
| 122 | + private GLib.Variant get_color_scheme () { |
| 123 | + return new GLib.Variant.uint32 (monitor.color_scheme); |
| 124 | + } |
| 125 | + |
| 126 | + public async void read_all (string[] namespaces, out GLib.Variant value) throws GLib.DBusError, GLib.IOError { |
| 127 | + var builder = new GLib.VariantBuilder (new GLib.VariantType ("(a{sa{sv}})")); |
| 128 | + |
| 129 | + builder.open (new GLib.VariantType ("a{sa{sv}}")); |
| 130 | + |
| 131 | + if (namespace_matches ("org.freedesktop.appearance", namespaces)) { |
| 132 | + var dict = new VariantDict (); |
| 133 | + |
| 134 | + dict.insert_value ("color-scheme", get_color_scheme ()); |
| 135 | + |
| 136 | + builder.add ("{sa{sv}}", "org.freedesktop.appearance", dict.end ()); |
| 137 | + } |
| 138 | + |
| 139 | + builder.close (); |
| 140 | + |
| 141 | + value = builder.end (); |
| 142 | + } |
| 143 | + |
| 144 | + public async void read (string namespace, string key, out GLib.Variant value) throws GLib.DBusError, GLib.Error { |
| 145 | + debug ("Read %s %s", namespace, key); |
| 146 | + |
| 147 | + if (namespace == "org.freedesktop.appearance" && key == "color-scheme") { |
| 148 | + value = get_color_scheme (); |
| 149 | + return; |
| 150 | + } |
| 151 | + |
| 152 | + debug ("Attempted to read unknown namespace/key pair: %s %s", namespace, key); |
| 153 | + |
| 154 | + throw XdgDesktopPortal.create_error (NOT_FOUND, "Requested setting not found"); |
| 155 | + } |
| 156 | +} |
0 commit comments