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