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 {
65 }
66
67 static void
68 proxy_properties_changed (GDBusProxy *proxy,
69 GVariant *changed_properties,
70 GStrv invalidated_properties,
71 gpointer user_data)
72 {
73 GPowerProfileMonitorPortal *ppm = user_data;
74 gboolean power_saver_enabled;
75
76 if (!g_variant_lookup (changed_properties, "power-saver-enabled", "b", &power_saver_enabled))
77 return;
78
79 if (power_saver_enabled == ppm->power_saver_enabled)
80 return;
81
82 ppm->power_saver_enabled = power_saver_enabled;
83 g_object_notify (G_OBJECT (ppm), "power-saver-enabled");
84 }
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 {
92 GPowerProfileMonitorPortal *ppm = G_POWER_PROFILE_MONITOR_PORTAL (object);
93
94 switch ((GPowerProfileMonitorPortalProperty) prop_id)
95 {
96 case PROP_POWER_SAVER_ENABLED:
97 g_value_set_boolean (value, ppm->power_saver_enabled);
98 break;
99
100 default:
101 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
102 }
103 }
104
105 static gboolean
106 g_power_profile_monitor_portal_initable_init (GInitable *initable,
107 GCancellable *cancellable,
108 GError **error)
109 {
110 GPowerProfileMonitorPortal *ppm = G_POWER_PROFILE_MONITOR_PORTAL (initable);
111 GDBusProxy *proxy;
112 gchar *name_owner;
113 GVariant *power_saver_enabled_v = NULL;
114
115 if (!glib_should_use_portal ())
116 {
117 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Not using portals");
118 return FALSE;
119 }
120
121 proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
122 G_DBUS_PROXY_FLAGS_NONE,
123 NULL,
124 "org.freedesktop.portal.Desktop",
125 "/org/freedesktop/portal/desktop",
126 "org.freedesktop.portal.PowerProfileMonitor",
127 cancellable,
128 error);
129 if (!proxy)
130 return FALSE;
131
132 name_owner = g_dbus_proxy_get_name_owner (proxy);
133
134 if (name_owner == NULL)
135 {
136 g_object_unref (proxy);
137 g_set_error (error,
138 G_DBUS_ERROR,
139 G_DBUS_ERROR_NAME_HAS_NO_OWNER,
140 "Desktop portal not found");
141 return FALSE;
142 }
143
144 g_free (name_owner);
145
146 ppm->signal_id = g_signal_connect (proxy, "g-properties-changed",
147 G_CALLBACK (proxy_properties_changed), ppm);
148
149 power_saver_enabled_v = g_dbus_proxy_get_cached_property (proxy, "power-saver-enabled");
150 if (power_saver_enabled_v != NULL &&
151 g_variant_is_of_type (power_saver_enabled_v, G_VARIANT_TYPE_BOOLEAN))
152 ppm->power_saver_enabled = g_variant_get_boolean (power_saver_enabled_v);
153 g_clear_pointer (&power_saver_enabled_v, g_variant_unref);
154
155 ppm->proxy = g_steal_pointer (&proxy);
156
157 return TRUE;
158 }
159
160 static void
161 g_power_profile_monitor_portal_finalize (GObject *object)
162 {
163 GPowerProfileMonitorPortal *ppm = G_POWER_PROFILE_MONITOR_PORTAL (object);
164
165 g_clear_signal_handler (&ppm->signal_id, ppm->proxy);
166 g_clear_object (&ppm->proxy);
167
168 G_OBJECT_CLASS (g_power_profile_monitor_portal_parent_class)->finalize (object);
169 }
170
171 static void
172 g_power_profile_monitor_portal_class_init (GPowerProfileMonitorPortalClass *nl_class)
173 {
174 GObjectClass *gobject_class = G_OBJECT_CLASS (nl_class);
175
176 gobject_class->get_property = g_power_profile_monitor_portal_get_property;
177 gobject_class->finalize = g_power_profile_monitor_portal_finalize;
178
179 g_object_class_override_property (gobject_class, PROP_POWER_SAVER_ENABLED, "power-saver-enabled");
180 }
181
182 static void
183 g_power_profile_monitor_portal_iface_init (GPowerProfileMonitorInterface *monitor_iface)
184 {
185 }
186
187 static void
188 g_power_profile_monitor_portal_initable_iface_init (GInitableIface *iface)
189 {
190 iface->init = g_power_profile_monitor_portal_initable_init;
191 }