/src/glib/gio/gpowerprofilemonitorportal.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* GIO - GLib Input, Output and Streaming Library |
2 | | * |
3 | | * Copyright 2021 Red Hat, Inc. |
4 | | * |
5 | | * SPDX-License-Identifier: LGPL-2.1-or-later |
6 | | * |
7 | | * This library is free software; you can redistribute it and/or |
8 | | * modify it under the terms of the GNU Lesser General Public |
9 | | * License as published by the Free Software Foundation; either |
10 | | * version 2.1 of the License, or (at your option) any later version. |
11 | | * |
12 | | * This library is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | | * Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General |
18 | | * Public License along with this library; if not, see <http://www.gnu.org/licenses/>. |
19 | | */ |
20 | | |
21 | | #include "config.h" |
22 | | |
23 | | #include "gpowerprofilemonitor.h" |
24 | | #include "gpowerprofilemonitorportal.h" |
25 | | #include "gdbuserror.h" |
26 | | #include "gdbusproxy.h" |
27 | | #include "ginitable.h" |
28 | | #include "gioerror.h" |
29 | | #include "giomodule-priv.h" |
30 | | #include "gportalsupport.h" |
31 | | |
32 | | #define G_POWER_PROFILE_MONITOR_PORTAL_GET_INITABLE_IFACE(o) (G_TYPE_INSTANCE_GET_INTERFACE ((o), G_TYPE_INITABLE, GInitable)) |
33 | | |
34 | | static void g_power_profile_monitor_portal_iface_init (GPowerProfileMonitorInterface *iface); |
35 | | static void g_power_profile_monitor_portal_initable_iface_init (GInitableIface *iface); |
36 | | |
37 | | typedef enum |
38 | | { |
39 | | PROP_POWER_SAVER_ENABLED = 1, |
40 | | } GPowerProfileMonitorPortalProperty; |
41 | | |
42 | | struct _GPowerProfileMonitorPortal |
43 | | { |
44 | | GObject parent_instance; |
45 | | |
46 | | GDBusProxy *proxy; |
47 | | gulong signal_id; |
48 | | gboolean power_saver_enabled; |
49 | | }; |
50 | | |
51 | | G_DEFINE_TYPE_WITH_CODE (GPowerProfileMonitorPortal, g_power_profile_monitor_portal, G_TYPE_OBJECT, |
52 | | G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, |
53 | | g_power_profile_monitor_portal_initable_iface_init) |
54 | | G_IMPLEMENT_INTERFACE (G_TYPE_POWER_PROFILE_MONITOR, |
55 | | g_power_profile_monitor_portal_iface_init) |
56 | | _g_io_modules_ensure_extension_points_registered (); |
57 | | g_io_extension_point_implement (G_POWER_PROFILE_MONITOR_EXTENSION_POINT_NAME, |
58 | | g_define_type_id, |
59 | | "portal", |
60 | | 40)) |
61 | | |
62 | | static void |
63 | | g_power_profile_monitor_portal_init (GPowerProfileMonitorPortal *portal) |
64 | 0 | { |
65 | 0 | } |
66 | | |
67 | | static void |
68 | | proxy_properties_changed (GDBusProxy *proxy, |
69 | | GVariant *changed_properties, |
70 | | GStrv invalidated_properties, |
71 | | gpointer user_data) |
72 | 0 | { |
73 | 0 | GPowerProfileMonitorPortal *ppm = user_data; |
74 | 0 | gboolean power_saver_enabled; |
75 | |
|
76 | 0 | if (!g_variant_lookup (changed_properties, "power-saver-enabled", "b", &power_saver_enabled)) |
77 | 0 | return; |
78 | | |
79 | 0 | if (power_saver_enabled == ppm->power_saver_enabled) |
80 | 0 | return; |
81 | | |
82 | 0 | ppm->power_saver_enabled = power_saver_enabled; |
83 | 0 | g_object_notify (G_OBJECT (ppm), "power-saver-enabled"); |
84 | 0 | } |
85 | | |
86 | | static void |
87 | | g_power_profile_monitor_portal_get_property (GObject *object, |
88 | | guint prop_id, |
89 | | GValue *value, |
90 | | GParamSpec *pspec) |
91 | 0 | { |
92 | 0 | GPowerProfileMonitorPortal *ppm = G_POWER_PROFILE_MONITOR_PORTAL (object); |
93 | |
|
94 | 0 | switch ((GPowerProfileMonitorPortalProperty) prop_id) |
95 | 0 | { |
96 | 0 | case PROP_POWER_SAVER_ENABLED: |
97 | 0 | g_value_set_boolean (value, ppm->power_saver_enabled); |
98 | 0 | break; |
99 | | |
100 | 0 | default: |
101 | 0 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
102 | 0 | } |
103 | 0 | } |
104 | | |
105 | | static gboolean |
106 | | g_power_profile_monitor_portal_initable_init (GInitable *initable, |
107 | | GCancellable *cancellable, |
108 | | GError **error) |
109 | 0 | { |
110 | 0 | GPowerProfileMonitorPortal *ppm = G_POWER_PROFILE_MONITOR_PORTAL (initable); |
111 | 0 | GDBusProxy *proxy; |
112 | 0 | gchar *name_owner; |
113 | 0 | GVariant *power_saver_enabled_v = NULL; |
114 | |
|
115 | 0 | if (!glib_should_use_portal ()) |
116 | 0 | { |
117 | 0 | g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Not using portals"); |
118 | 0 | return FALSE; |
119 | 0 | } |
120 | | |
121 | 0 | proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION, |
122 | 0 | G_DBUS_PROXY_FLAGS_NONE, |
123 | 0 | NULL, |
124 | 0 | "org.freedesktop.portal.Desktop", |
125 | 0 | "/org/freedesktop/portal/desktop", |
126 | 0 | "org.freedesktop.portal.PowerProfileMonitor", |
127 | 0 | cancellable, |
128 | 0 | error); |
129 | 0 | if (!proxy) |
130 | 0 | return FALSE; |
131 | | |
132 | 0 | name_owner = g_dbus_proxy_get_name_owner (proxy); |
133 | |
|
134 | 0 | if (name_owner == NULL) |
135 | 0 | { |
136 | 0 | g_object_unref (proxy); |
137 | 0 | g_set_error (error, |
138 | 0 | G_DBUS_ERROR, |
139 | 0 | G_DBUS_ERROR_NAME_HAS_NO_OWNER, |
140 | 0 | "Desktop portal not found"); |
141 | 0 | return FALSE; |
142 | 0 | } |
143 | | |
144 | 0 | g_free (name_owner); |
145 | |
|
146 | 0 | ppm->signal_id = g_signal_connect (proxy, "g-properties-changed", |
147 | 0 | G_CALLBACK (proxy_properties_changed), ppm); |
148 | |
|
149 | 0 | power_saver_enabled_v = g_dbus_proxy_get_cached_property (proxy, "power-saver-enabled"); |
150 | 0 | if (power_saver_enabled_v != NULL && |
151 | 0 | g_variant_is_of_type (power_saver_enabled_v, G_VARIANT_TYPE_BOOLEAN)) |
152 | 0 | ppm->power_saver_enabled = g_variant_get_boolean (power_saver_enabled_v); |
153 | 0 | g_clear_pointer (&power_saver_enabled_v, g_variant_unref); |
154 | |
|
155 | 0 | ppm->proxy = g_steal_pointer (&proxy); |
156 | |
|
157 | 0 | return TRUE; |
158 | 0 | } |
159 | | |
160 | | static void |
161 | | g_power_profile_monitor_portal_finalize (GObject *object) |
162 | 0 | { |
163 | 0 | GPowerProfileMonitorPortal *ppm = G_POWER_PROFILE_MONITOR_PORTAL (object); |
164 | |
|
165 | 0 | g_clear_signal_handler (&ppm->signal_id, ppm->proxy); |
166 | 0 | g_clear_object (&ppm->proxy); |
167 | |
|
168 | 0 | G_OBJECT_CLASS (g_power_profile_monitor_portal_parent_class)->finalize (object); |
169 | 0 | } |
170 | | |
171 | | static void |
172 | | g_power_profile_monitor_portal_class_init (GPowerProfileMonitorPortalClass *nl_class) |
173 | 0 | { |
174 | 0 | GObjectClass *gobject_class = G_OBJECT_CLASS (nl_class); |
175 | |
|
176 | 0 | gobject_class->get_property = g_power_profile_monitor_portal_get_property; |
177 | 0 | gobject_class->finalize = g_power_profile_monitor_portal_finalize; |
178 | |
|
179 | 0 | g_object_class_override_property (gobject_class, PROP_POWER_SAVER_ENABLED, "power-saver-enabled"); |
180 | 0 | } |
181 | | |
182 | | static void |
183 | | g_power_profile_monitor_portal_iface_init (GPowerProfileMonitorInterface *monitor_iface) |
184 | 0 | { |
185 | 0 | } |
186 | | |
187 | | static void |
188 | | g_power_profile_monitor_portal_initable_iface_init (GInitableIface *iface) |
189 | 0 | { |
190 | 0 | iface->init = g_power_profile_monitor_portal_initable_init; |
191 | 0 | } |