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