LCOV - code coverage report
Current view: top level - src/plugins/lock - valent-lock-plugin.c (source / functions) Hit Total Coverage
Test: Code Coverage Lines: 105 106 99.1 %
Date: 2023-06-06 02:20:50 Functions: 15 15 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 34 56 60.7 %

           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-lock-plugin"
       5                 :            : 
       6                 :            : #include "config.h"
       7                 :            : 
       8                 :            : #include <glib/gi18n.h>
       9                 :            : #include <gio/gio.h>
      10                 :            : #include <json-glib/json-glib.h>
      11                 :            : #include <valent.h>
      12                 :            : 
      13                 :            : #include "valent-lock-plugin.h"
      14                 :            : 
      15                 :            : 
      16                 :            : struct _ValentLockPlugin
      17                 :            : {
      18                 :            :   ValentDevicePlugin  parent_instance;
      19                 :            : 
      20                 :            :   ValentSession      *session;
      21                 :            :   unsigned long       session_changed_id;
      22                 :            : 
      23                 :            :   gboolean            remote_locked;
      24                 :            : };
      25                 :            : 
      26   [ +  +  +  - ]:         42 : G_DEFINE_FINAL_TYPE (ValentLockPlugin, valent_lock_plugin, VALENT_TYPE_DEVICE_PLUGIN)
      27                 :            : 
      28                 :            : static void valent_lock_plugin_request_state (ValentLockPlugin *self);
      29                 :            : static void valent_lock_plugin_send_state    (ValentLockPlugin *self);
      30                 :            : 
      31                 :            : 
      32                 :            : /*
      33                 :            :  * Local Lock
      34                 :            :  */
      35                 :            : static void
      36                 :          3 : valent_lock_plugin_send_state (ValentLockPlugin *self)
      37                 :            : {
      38                 :          6 :   g_autoptr (JsonBuilder) builder = NULL;
      39         [ -  + ]:          3 :   g_autoptr (JsonNode) packet = NULL;
      40                 :          3 :   gboolean state;
      41                 :            : 
      42         [ +  - ]:          3 :   g_assert (VALENT_IS_LOCK_PLUGIN (self));
      43                 :            : 
      44                 :          3 :   state = valent_session_get_locked (self->session);
      45                 :            : 
      46                 :          3 :   valent_packet_init (&builder, "kdeconnect.lock");
      47                 :          3 :   json_builder_set_member_name (builder, "isLocked");
      48                 :          3 :   json_builder_add_boolean_value (builder, state);
      49                 :          3 :   packet = valent_packet_end (&builder);
      50                 :            : 
      51         [ +  - ]:          3 :   valent_device_plugin_queue_packet (VALENT_DEVICE_PLUGIN (self), packet);
      52                 :          3 : }
      53                 :            : 
      54                 :            : static void
      55                 :          3 : valent_lock_plugin_handle_lock_request (ValentLockPlugin *self,
      56                 :            :                                         JsonNode         *packet)
      57                 :            : {
      58                 :          3 :   gboolean state;
      59                 :            : 
      60         [ +  - ]:          3 :   g_assert (VALENT_IS_LOCK_PLUGIN (self));
      61         [ -  + ]:          3 :   g_assert (VALENT_IS_PACKET (packet));
      62                 :            : 
      63         [ +  + ]:          3 :   if (valent_packet_check_field (packet, "requestLocked"))
      64                 :          1 :     valent_lock_plugin_send_state (self);
      65                 :            : 
      66         [ +  + ]:          3 :   if (valent_packet_get_boolean (packet, "setLocked", &state))
      67                 :          2 :     valent_session_set_locked (self->session, state);
      68                 :          3 : }
      69                 :            : 
      70                 :            : /*
      71                 :            :  * Remote Lock
      72                 :            :  */
      73                 :            : static void
      74                 :          9 : valent_lock_plugin_update_actions (ValentLockPlugin *self)
      75                 :            : {
      76                 :          9 :   GAction *action;
      77                 :            : 
      78                 :          9 :   action = g_action_map_lookup_action (G_ACTION_MAP (self), "state");
      79                 :          9 :   g_simple_action_set_state (G_SIMPLE_ACTION (action),
      80                 :            :                              g_variant_new_boolean (self->remote_locked));
      81                 :          9 : }
      82                 :            : 
      83                 :            : static void
      84                 :          5 : valent_lock_plugin_handle_lock (ValentLockPlugin *self,
      85                 :            :                                 JsonNode         *packet)
      86                 :            : {
      87         [ +  - ]:          5 :   g_assert (VALENT_IS_LOCK_PLUGIN (self));
      88         [ -  + ]:          5 :   g_assert (VALENT_IS_PACKET (packet));
      89                 :            : 
      90         [ +  - ]:          5 :   if (valent_packet_get_boolean (packet, "isLocked", &self->remote_locked))
      91                 :          5 :     valent_lock_plugin_update_actions (self);
      92                 :          5 : }
      93                 :            : 
      94                 :            : static void
      95                 :          4 : valent_lock_plugin_request_state (ValentLockPlugin *self)
      96                 :            : {
      97                 :          8 :   g_autoptr (JsonBuilder) builder = NULL;
      98         [ -  + ]:          4 :   g_autoptr (JsonNode) packet = NULL;
      99                 :            : 
     100                 :          4 :   valent_packet_init (&builder, "kdeconnect.lock.request");
     101                 :          4 :   json_builder_set_member_name (builder, "requestLocked");
     102                 :          4 :   json_builder_add_boolean_value (builder, TRUE);
     103                 :          4 :   packet = valent_packet_end (&builder);
     104                 :            : 
     105         [ +  - ]:          4 :   valent_device_plugin_queue_packet (VALENT_DEVICE_PLUGIN (self), packet);
     106                 :          4 : }
     107                 :            : 
     108                 :            : static void
     109                 :          2 : valent_lock_plugin_set_state (ValentLockPlugin *self,
     110                 :            :                               gboolean          state)
     111                 :            : {
     112                 :          4 :   g_autoptr (JsonBuilder) builder = NULL;
     113         [ -  + ]:          2 :   g_autoptr (JsonNode) packet = NULL;
     114                 :            : 
     115                 :          2 :   valent_packet_init (&builder, "kdeconnect.lock.request");
     116                 :          2 :   json_builder_set_member_name (builder, "setLocked");
     117                 :          2 :   json_builder_add_boolean_value (builder, state);
     118                 :          2 :   packet = valent_packet_end (&builder);
     119                 :            : 
     120         [ +  - ]:          2 :   valent_device_plugin_queue_packet (VALENT_DEVICE_PLUGIN (self), packet);
     121                 :          2 : }
     122                 :            : 
     123                 :            : /*
     124                 :            :  * GActions
     125                 :            :  */
     126                 :            : static void
     127                 :          2 : state_action (GSimpleAction *action,
     128                 :            :               GVariant      *parameter,
     129                 :            :               gpointer       user_data)
     130                 :            : {
     131                 :          2 :   ValentLockPlugin *self = VALENT_LOCK_PLUGIN (user_data);
     132                 :          2 :   gboolean lock = FALSE;
     133                 :            : 
     134         [ +  - ]:          2 :   g_assert (VALENT_IS_LOCK_PLUGIN (self));
     135                 :            : 
     136                 :          2 :   lock = g_variant_get_boolean (parameter);
     137                 :            : 
     138         [ +  - ]:          2 :   if (self->remote_locked != lock)
     139                 :          2 :     valent_lock_plugin_set_state (self, lock);
     140                 :          2 : }
     141                 :            : 
     142                 :            : static const GActionEntry actions[] = {
     143                 :            :     {"state", NULL, NULL, "false", state_action},
     144                 :            : };
     145                 :            : 
     146                 :            : /*
     147                 :            :  * ValentDevicePlugin
     148                 :            :  */
     149                 :            : static void
     150                 :         15 : valent_lock_plugin_update_state (ValentDevicePlugin *plugin,
     151                 :            :                                  ValentDeviceState   state)
     152                 :            : {
     153                 :         15 :   ValentLockPlugin *self = VALENT_LOCK_PLUGIN (plugin);
     154                 :         15 :   gboolean available;
     155                 :            : 
     156         [ +  - ]:         15 :   g_assert (VALENT_IS_LOCK_PLUGIN (self));
     157                 :            : 
     158                 :         15 :   available = (state & VALENT_DEVICE_STATE_CONNECTED) != 0 &&
     159                 :            :               (state & VALENT_DEVICE_STATE_PAIRED) != 0;
     160                 :            : 
     161         [ +  + ]:         15 :   if (available)
     162                 :            :     {
     163         [ +  - ]:          4 :       if (self->session_changed_id == 0)
     164                 :            :         {
     165                 :          4 :           self->session_changed_id =
     166                 :          4 :             g_signal_connect_object (self->session,
     167                 :            :                                      "notify::locked",
     168                 :            :                                      G_CALLBACK (valent_lock_plugin_send_state),
     169                 :            :                                      self,
     170                 :            :                                      G_CONNECT_SWAPPED);
     171                 :            :         }
     172                 :            : 
     173                 :          4 :       valent_extension_toggle_actions (VALENT_EXTENSION (plugin), available);
     174                 :          4 :       valent_lock_plugin_update_actions (self);
     175                 :          4 :       valent_lock_plugin_request_state (self);
     176                 :            :     }
     177                 :            :   else
     178                 :            :     {
     179         [ +  + ]:         11 :       g_clear_signal_handler (&self->session_changed_id, self->session);
     180                 :         11 :       valent_extension_toggle_actions (VALENT_EXTENSION (plugin), available);
     181                 :            :     }
     182                 :         15 : }
     183                 :            : 
     184                 :            : static void
     185                 :          8 : valent_lock_plugin_handle_packet (ValentDevicePlugin *plugin,
     186                 :            :                                   const char         *type,
     187                 :            :                                   JsonNode           *packet)
     188                 :            : {
     189                 :          8 :   ValentLockPlugin *self = VALENT_LOCK_PLUGIN (plugin);
     190                 :            : 
     191         [ +  - ]:          8 :   g_assert (VALENT_IS_LOCK_PLUGIN (self));
     192         [ -  + ]:          8 :   g_assert (type != NULL);
     193         [ -  + ]:          8 :   g_assert (VALENT_IS_PACKET (packet));
     194                 :            : 
     195         [ +  + ]:          8 :   if (g_str_equal (type, "kdeconnect.lock"))
     196                 :          5 :     valent_lock_plugin_handle_lock (self, packet);
     197         [ +  - ]:          3 :   else if (g_str_equal (type, "kdeconnect.lock.request"))
     198                 :          3 :     valent_lock_plugin_handle_lock_request (self, packet);
     199                 :            :   else
     200                 :          0 :     g_assert_not_reached ();
     201                 :          8 : }
     202                 :            : 
     203                 :            : /*
     204                 :            :  * GObject
     205                 :            :  */
     206                 :            : static void
     207                 :          5 : valent_lock_plugin_constructed (GObject *object)
     208                 :            : {
     209                 :          5 :   ValentLockPlugin *self = VALENT_LOCK_PLUGIN (object);
     210                 :          5 :   ValentDevicePlugin *plugin = VALENT_DEVICE_PLUGIN (object);
     211                 :            : 
     212                 :          5 :   self->session = valent_session_get_default ();
     213                 :            : 
     214                 :          5 :   g_action_map_add_action_entries (G_ACTION_MAP (plugin),
     215                 :            :                                    actions,
     216                 :            :                                    G_N_ELEMENTS (actions),
     217                 :            :                                    plugin);
     218                 :            : 
     219                 :          5 :   G_OBJECT_CLASS (valent_lock_plugin_parent_class)->constructed (object);
     220                 :          5 : }
     221                 :            : 
     222                 :            : static void
     223                 :         10 : valent_lock_plugin_dispose (GObject *object)
     224                 :            : {
     225                 :         10 :   ValentLockPlugin *self = VALENT_LOCK_PLUGIN (object);
     226                 :            : 
     227         [ -  + ]:         10 :   g_clear_signal_handler (&self->session_changed_id, self->session);
     228                 :            : 
     229                 :         10 :   G_OBJECT_CLASS (valent_lock_plugin_parent_class)->dispose (object);
     230                 :         10 : }
     231                 :            : 
     232                 :            : static void
     233                 :          2 : valent_lock_plugin_class_init (ValentLockPluginClass *klass)
     234                 :            : {
     235                 :          2 :   GObjectClass *object_class = G_OBJECT_CLASS (klass);
     236                 :          2 :   ValentDevicePluginClass *plugin_class = VALENT_DEVICE_PLUGIN_CLASS (klass);
     237                 :            : 
     238                 :          2 :   object_class->constructed = valent_lock_plugin_constructed;
     239                 :          2 :   object_class->dispose = valent_lock_plugin_dispose;
     240                 :            : 
     241                 :          2 :   plugin_class->handle_packet = valent_lock_plugin_handle_packet;
     242                 :          2 :   plugin_class->update_state = valent_lock_plugin_update_state;
     243                 :            : }
     244                 :            : 
     245                 :            : static void
     246                 :          5 : valent_lock_plugin_init (ValentLockPlugin *self)
     247                 :            : {
     248                 :          5 : }
     249                 :            : 

Generated by: LCOV version 1.14