LCOV - code coverage report
Current view: top level - glib/gio - gpowerprofilemonitorportal.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 26 60 43.3 %
Date: 2024-04-23 05:16:05 Functions: 9 11 81.8 %
Branches: 6 22 27.3 %

           Branch data     Line data    Source code
       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   [ +  +  +  -  :        231 : 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                 :          1 : g_power_profile_monitor_portal_init (GPowerProfileMonitorPortal *portal)
      64                 :            : {
      65                 :          1 : }
      66                 :            : 
      67                 :            : static void
      68                 :          0 : proxy_properties_changed (GDBusProxy *proxy,
      69                 :            :                           GVariant   *changed_properties,
      70                 :            :                           GStrv       invalidated_properties,
      71                 :            :                           gpointer    user_data)
      72                 :            : {
      73                 :          0 :   GPowerProfileMonitorPortal *ppm = user_data;
      74                 :            :   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                 :            : }
      85                 :            : 
      86                 :            : static void
      87                 :          0 : g_power_profile_monitor_portal_get_property (GObject    *object,
      88                 :            :                                              guint       prop_id,
      89                 :            :                                              GValue     *value,
      90                 :            :                                              GParamSpec *pspec)
      91                 :            : {
      92                 :          0 :   GPowerProfileMonitorPortal *ppm = G_POWER_PROFILE_MONITOR_PORTAL (object);
      93                 :            : 
      94         [ #  # ]:          0 :   switch ((GPowerProfileMonitorPortalProperty) prop_id)
      95                 :            :     {
      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                 :            :     }
     103                 :          0 : }
     104                 :            : 
     105                 :            : static gboolean
     106                 :          1 : g_power_profile_monitor_portal_initable_init (GInitable     *initable,
     107                 :            :                                               GCancellable  *cancellable,
     108                 :            :                                               GError       **error)
     109                 :            : {
     110                 :          1 :   GPowerProfileMonitorPortal *ppm = G_POWER_PROFILE_MONITOR_PORTAL (initable);
     111                 :            :   GDBusProxy *proxy;
     112                 :            :   gchar *name_owner;
     113                 :          1 :   GVariant *power_saver_enabled_v = NULL;
     114                 :            : 
     115         [ +  - ]:          1 :   if (!glib_should_use_portal ())
     116                 :            :     {
     117                 :          1 :       g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Not using portals");
     118                 :          1 :       return FALSE;
     119                 :            :     }
     120                 :            : 
     121                 :          0 :   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         [ #  # ]:          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                 :            :     {
     136                 :          0 :       g_object_unref (proxy);
     137                 :          0 :       g_set_error (error,
     138                 :            :                    G_DBUS_ERROR,
     139                 :            :                    G_DBUS_ERROR_NAME_HAS_NO_OWNER,
     140                 :            :                    "Desktop portal not found");
     141                 :          0 :       return FALSE;
     142                 :            :     }
     143                 :            : 
     144                 :          0 :   g_free (name_owner);
     145                 :            : 
     146                 :          0 :   ppm->signal_id = g_signal_connect (proxy, "g-properties-changed",
     147                 :            :                                      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                 :            : }
     159                 :            : 
     160                 :            : static void
     161                 :          1 : g_power_profile_monitor_portal_finalize (GObject *object)
     162                 :            : {
     163                 :          1 :   GPowerProfileMonitorPortal *ppm = G_POWER_PROFILE_MONITOR_PORTAL (object);
     164                 :            : 
     165                 :          1 :   g_clear_signal_handler (&ppm->signal_id, ppm->proxy);
     166                 :          1 :   g_clear_object (&ppm->proxy);
     167                 :            : 
     168                 :          1 :   G_OBJECT_CLASS (g_power_profile_monitor_portal_parent_class)->finalize (object);
     169                 :          1 : }
     170                 :            : 
     171                 :            : static void
     172                 :          1 : g_power_profile_monitor_portal_class_init (GPowerProfileMonitorPortalClass *nl_class)
     173                 :            : {
     174                 :          1 :   GObjectClass *gobject_class = G_OBJECT_CLASS (nl_class);
     175                 :            : 
     176                 :          1 :   gobject_class->get_property = g_power_profile_monitor_portal_get_property;
     177                 :          1 :   gobject_class->finalize  = g_power_profile_monitor_portal_finalize;
     178                 :            : 
     179                 :          1 :   g_object_class_override_property (gobject_class, PROP_POWER_SAVER_ENABLED, "power-saver-enabled");
     180                 :          1 : }
     181                 :            : 
     182                 :            : static void
     183                 :          1 : g_power_profile_monitor_portal_iface_init (GPowerProfileMonitorInterface *monitor_iface)
     184                 :            : {
     185                 :          1 : }
     186                 :            : 
     187                 :            : static void
     188                 :          1 : g_power_profile_monitor_portal_initable_iface_init (GInitableIface *iface)
     189                 :            : {
     190                 :          1 :   iface->init = g_power_profile_monitor_portal_initable_init;
     191                 :          1 : }

Generated by: LCOV version 1.14