LCOV - code coverage report
Current view: top level - src/libvalent/session - valent-session.c (source / functions) Coverage Total Hit
Test: Code Coverage Lines: 95.3 % 86 82
Test Date: 2024-04-23 06:02:46 Functions: 100.0 % 14 14
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 64.9 % 37 24

             Branch data     Line data    Source code
       1                 :             : // SPDX-License-Identifier: GPL-3.0-or-later
       2                 :             : // SPDX-FileCopyrightText: Andy Holmes <andrew.g.r.holmes@gmail.com>
       3                 :             : 
       4                 :             : #define G_LOG_DOMAIN "valent-session"
       5                 :             : 
       6                 :             : #include "config.h"
       7                 :             : 
       8                 :             : #include <gio/gio.h>
       9                 :             : #include <libpeas.h>
      10                 :             : #include <libvalent-core.h>
      11                 :             : 
      12                 :             : #include "valent-session.h"
      13                 :             : #include "valent-session-adapter.h"
      14                 :             : 
      15                 :             : 
      16                 :             : /**
      17                 :             :  * ValentSession:
      18                 :             :  *
      19                 :             :  * A class for monitoring the session state.
      20                 :             :  *
      21                 :             :  * `ValentSession` is an abstraction of session managers, intended for use by
      22                 :             :  * [class@Valent.DevicePlugin] implementations.
      23                 :             :  *
      24                 :             :  * Plugins can implement [class@Valent.SessionAdapter] to provide an interface
      25                 :             :  * to monitor and control the session state.
      26                 :             :  *
      27                 :             :  * Since: 1.0
      28                 :             :  */
      29                 :             : 
      30                 :             : struct _ValentSession
      31                 :             : {
      32                 :             :   ValentComponent       parent_instance;
      33                 :             : 
      34                 :             :   ValentSessionAdapter *default_adapter;
      35                 :             : };
      36                 :             : 
      37   [ +  +  +  - ]:         208 : G_DEFINE_FINAL_TYPE (ValentSession, valent_session, VALENT_TYPE_COMPONENT)
      38                 :             : 
      39                 :             : enum {
      40                 :             :   PROP_0,
      41                 :             :   PROP_ACTIVE,
      42                 :             :   PROP_LOCKED,
      43                 :             :   N_PROPERTIES
      44                 :             : };
      45                 :             : 
      46                 :             : static GParamSpec *properties[N_PROPERTIES] = { NULL, };
      47                 :             : 
      48                 :             : static ValentSession *default_adapter = NULL;
      49                 :             : 
      50                 :             : 
      51                 :             : static void
      52                 :           2 : on_active_changed (ValentSessionAdapter *adapter,
      53                 :             :                    GParamSpec           *pspec,
      54                 :             :                    ValentSession        *self)
      55                 :             : {
      56                 :           2 :   VALENT_ENTRY;
      57                 :             : 
      58         [ +  - ]:           2 :   if (self->default_adapter == adapter)
      59                 :           2 :     g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_ACTIVE]);
      60                 :             : 
      61                 :           2 :   VALENT_EXIT;
      62                 :             : }
      63                 :             : 
      64                 :             : static void
      65                 :           6 : on_locked_changed (ValentSessionAdapter *adapter,
      66                 :             :                    GParamSpec           *pspec,
      67                 :             :                    ValentSession        *self)
      68                 :             : {
      69                 :           6 :   VALENT_ENTRY;
      70                 :             : 
      71         [ +  - ]:           6 :   if (self->default_adapter == adapter)
      72                 :           6 :     g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_LOCKED]);
      73                 :             : 
      74                 :           6 :   VALENT_EXIT;
      75                 :             : }
      76                 :             : 
      77                 :             : /*
      78                 :             :  * ValentComponent
      79                 :             :  */
      80                 :             : static void
      81                 :           8 : valent_session_bind_preferred (ValentComponent *component,
      82                 :             :                                GObject         *extension)
      83                 :             : {
      84                 :           8 :   ValentSession *self = VALENT_SESSION (component);
      85                 :           8 :   ValentSessionAdapter *adapter = VALENT_SESSION_ADAPTER (extension);
      86                 :             : 
      87                 :           8 :   VALENT_ENTRY;
      88                 :             : 
      89         [ +  - ]:           8 :   g_assert (VALENT_IS_SESSION (self));
      90   [ +  +  -  + ]:           8 :   g_assert (adapter == NULL || VALENT_IS_SESSION_ADAPTER (adapter));
      91                 :             : 
      92         [ +  + ]:           8 :   if (self->default_adapter != NULL)
      93                 :             :     {
      94                 :           1 :       g_signal_handlers_disconnect_by_func (self->default_adapter,
      95                 :             :                                             self,
      96                 :             :                                             on_active_changed);
      97                 :           1 :       g_signal_handlers_disconnect_by_func (self->default_adapter,
      98                 :             :                                             self,
      99                 :             :                                             on_locked_changed);
     100                 :           1 :       self->default_adapter = NULL;
     101                 :             :     }
     102                 :             : 
     103         [ +  + ]:           8 :   if (adapter != NULL)
     104                 :             :     {
     105                 :           7 :       self->default_adapter = adapter;
     106                 :           7 :       g_signal_connect_object (self->default_adapter,
     107                 :             :                                "notify::active",
     108                 :             :                                G_CALLBACK (on_active_changed),
     109                 :             :                                self, 0);
     110                 :           7 :       g_signal_connect_object (self->default_adapter,
     111                 :             :                                "notify::locked",
     112                 :             :                                G_CALLBACK (on_locked_changed),
     113                 :             :                                self, 0);
     114                 :             :     }
     115                 :             : 
     116                 :           8 :   VALENT_EXIT;
     117                 :             : }
     118                 :             : 
     119                 :             : /*
     120                 :             :  * GObject
     121                 :             :  */
     122                 :             : static void
     123                 :           2 : valent_session_get_property (GObject    *object,
     124                 :             :                              guint       prop_id,
     125                 :             :                              GValue     *value,
     126                 :             :                              GParamSpec *pspec)
     127                 :             : {
     128                 :           2 :   ValentSession *self = VALENT_SESSION (object);
     129                 :             : 
     130      [ +  +  - ]:           2 :   switch (prop_id)
     131                 :             :     {
     132                 :           1 :     case PROP_ACTIVE:
     133                 :           1 :       g_value_set_boolean (value, valent_session_get_active (self));
     134                 :           1 :       break;
     135                 :             : 
     136                 :           1 :     case PROP_LOCKED:
     137                 :           1 :       g_value_set_boolean (value, valent_session_get_locked (self));
     138                 :           1 :       break;
     139                 :             : 
     140                 :           0 :     default:
     141                 :           0 :       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     142                 :             :     }
     143                 :           2 : }
     144                 :             : 
     145                 :             : static void
     146                 :           1 : valent_session_set_property (GObject      *object,
     147                 :             :                              guint         prop_id,
     148                 :             :                              const GValue *value,
     149                 :             :                              GParamSpec   *pspec)
     150                 :             : {
     151                 :           1 :   ValentSession *self = VALENT_SESSION (object);
     152                 :             : 
     153         [ +  - ]:           1 :   switch (prop_id)
     154                 :             :     {
     155                 :           1 :     case PROP_LOCKED:
     156                 :           1 :       valent_session_set_locked (self, g_value_get_boolean (value));
     157                 :           1 :       break;
     158                 :             : 
     159                 :           0 :     default:
     160                 :           0 :       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     161                 :             :     }
     162                 :           1 : }
     163                 :             : 
     164                 :             : static void
     165                 :           6 : valent_session_class_init (ValentSessionClass *klass)
     166                 :             : {
     167                 :           6 :   GObjectClass *object_class = G_OBJECT_CLASS (klass);
     168                 :           6 :   ValentComponentClass *component_class = VALENT_COMPONENT_CLASS (klass);
     169                 :             : 
     170                 :           6 :   object_class->get_property = valent_session_get_property;
     171                 :           6 :   object_class->set_property = valent_session_set_property;
     172                 :             : 
     173                 :           6 :   component_class->bind_preferred = valent_session_bind_preferred;
     174                 :             : 
     175                 :             :   /**
     176                 :             :    * ValentSession:active: (getter get_active)
     177                 :             :    *
     178                 :             :    * Whether the session is active.
     179                 :             :    *
     180                 :             :    * Since: 1.0
     181                 :             :    */
     182                 :          12 :   properties [PROP_ACTIVE] =
     183                 :           6 :     g_param_spec_boolean ("active", NULL, NULL,
     184                 :             :                           FALSE,
     185                 :             :                           (G_PARAM_READABLE |
     186                 :             :                            G_PARAM_EXPLICIT_NOTIFY |
     187                 :             :                            G_PARAM_STATIC_STRINGS));
     188                 :             : 
     189                 :             :   /**
     190                 :             :    * ValentSession:locked: (getter get_locked) (setter set_locked)
     191                 :             :    *
     192                 :             :    * Whether the session is locked.
     193                 :             :    *
     194                 :             :    * Since: 1.0
     195                 :             :    */
     196                 :          12 :   properties [PROP_LOCKED] =
     197                 :           6 :     g_param_spec_boolean ("locked", NULL, NULL,
     198                 :             :                           FALSE,
     199                 :             :                           (G_PARAM_READWRITE |
     200                 :             :                            G_PARAM_EXPLICIT_NOTIFY |
     201                 :             :                            G_PARAM_STATIC_STRINGS));
     202                 :             : 
     203                 :           6 :   g_object_class_install_properties (object_class, N_PROPERTIES, properties);
     204                 :           6 : }
     205                 :             : 
     206                 :             : static void
     207                 :           6 : valent_session_init (ValentSession *self)
     208                 :             : {
     209                 :           6 : }
     210                 :             : 
     211                 :             : /**
     212                 :             :  * valent_session_get_default:
     213                 :             :  *
     214                 :             :  * Get the default [class@Valent.Session].
     215                 :             :  *
     216                 :             :  * Returns: (transfer none) (nullable): a `ValentSession`
     217                 :             :  *
     218                 :             :  * Since: 1.0
     219                 :             :  */
     220                 :             : ValentSession *
     221                 :          13 : valent_session_get_default (void)
     222                 :             : {
     223         [ +  + ]:          13 :   if (default_adapter == NULL)
     224                 :             :     {
     225                 :           6 :       default_adapter = g_object_new (VALENT_TYPE_SESSION,
     226                 :             :                                       "plugin-domain", "session",
     227                 :             :                                       "plugin-type",   VALENT_TYPE_SESSION_ADAPTER,
     228                 :             :                                       NULL);
     229                 :             : 
     230                 :           6 :       g_object_add_weak_pointer (G_OBJECT (default_adapter),
     231                 :             :                                  (gpointer)&default_adapter);
     232                 :             :     }
     233                 :             : 
     234                 :          13 :   return default_adapter;
     235                 :             : }
     236                 :             : 
     237                 :             : /**
     238                 :             :  * valent_session_get_active: (get-property active)
     239                 :             :  * @session: a `ValentSession`
     240                 :             :  *
     241                 :             :  * Get the active state of the primary [class@Valent.SessionAdapter].
     242                 :             :  *
     243                 :             :  * Returns: %TRUE if the session is active, or %FALSE if not
     244                 :             :  *
     245                 :             :  * Since: 1.0
     246                 :             :  */
     247                 :             : gboolean
     248                 :           4 : valent_session_get_active (ValentSession *session)
     249                 :             : {
     250                 :           4 :   gboolean ret = FALSE;
     251                 :             : 
     252                 :           4 :   VALENT_ENTRY;
     253                 :             : 
     254         [ +  - ]:           4 :   g_return_val_if_fail (VALENT_IS_SESSION (session), FALSE);
     255                 :             : 
     256         [ -  + ]:           4 :   if G_LIKELY (session->default_adapter != NULL)
     257                 :           4 :     ret = valent_session_adapter_get_active (session->default_adapter);
     258                 :             : 
     259                 :           4 :   VALENT_RETURN (ret);
     260                 :             : }
     261                 :             : 
     262                 :             : /**
     263                 :             :  * valent_session_get_locked: (get-property locked)
     264                 :             :  * @session: a `ValentSession`
     265                 :             :  *
     266                 :             :  * Get the locked state of the primary [class@Valent.SessionAdapter].
     267                 :             :  *
     268                 :             :  * Returns: %TRUE if the session is locked, or %FALSE if unlocked
     269                 :             :  *
     270                 :             :  * Since: 1.0
     271                 :             :  */
     272                 :             : gboolean
     273                 :           8 : valent_session_get_locked (ValentSession *session)
     274                 :             : {
     275                 :           8 :   gboolean ret = FALSE;
     276                 :             : 
     277                 :           8 :   VALENT_ENTRY;
     278                 :             : 
     279         [ +  - ]:           8 :   g_return_val_if_fail (VALENT_IS_SESSION (session), FALSE);
     280                 :             : 
     281         [ -  + ]:           8 :   if G_LIKELY (session->default_adapter != NULL)
     282                 :           8 :     ret = valent_session_adapter_get_locked (session->default_adapter);
     283                 :             : 
     284                 :           8 :   VALENT_RETURN (ret);
     285                 :             : }
     286                 :             : 
     287                 :             : /**
     288                 :             :  * valent_session_set_locked: (set-property locked)
     289                 :             :  * @session: a `ValentSession`
     290                 :             :  * @state: %TRUE to lock, or %FALSE to unlock
     291                 :             :  *
     292                 :             :  * Set the locked state of the primary [class@Valent.SessionAdapter].
     293                 :             :  *
     294                 :             :  * Since: 1.0
     295                 :             :  */
     296                 :             : void
     297                 :           4 : valent_session_set_locked (ValentSession *session,
     298                 :             :                            gboolean       state)
     299                 :             : {
     300                 :           4 :   VALENT_ENTRY;
     301                 :             : 
     302         [ +  - ]:           4 :   g_return_if_fail (VALENT_IS_SESSION (session));
     303                 :             : 
     304         [ +  - ]:           4 :   if G_LIKELY (session->default_adapter != NULL)
     305                 :           4 :     valent_session_adapter_set_locked (session->default_adapter, state);
     306                 :             : 
     307                 :           4 :   VALENT_EXIT;
     308                 :             : }
     309                 :             : 
        

Generated by: LCOV version 2.0-1