LCOV - code coverage report
Current view: top level - src/plugins/findmyphone - valent-findmyphone-plugin.c (source / functions) Coverage Total Hit
Test: Code Coverage Lines: 74.6 % 59 44
Test Date: 2024-04-16 07:28:14 Functions: 80.0 % 10 8
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 42.3 % 26 11

             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-findmyphone-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-findmyphone-plugin.h"
      14                 :             : #include "valent-findmyphone-ringer.h"
      15                 :             : 
      16                 :             : 
      17                 :             : struct _ValentFindmyphonePlugin
      18                 :             : {
      19                 :             :   ValentDevicePlugin       parent_instance;
      20                 :             : 
      21                 :             :   ValentFindmyphoneRinger *ringer;
      22                 :             :   ValentSession           *session;
      23                 :             : };
      24                 :             : 
      25   [ +  +  +  - ]:          13 : G_DEFINE_FINAL_TYPE (ValentFindmyphonePlugin, valent_findmyphone_plugin, VALENT_TYPE_DEVICE_PLUGIN)
      26                 :             : 
      27                 :             : 
      28                 :             : static void
      29                 :           0 : valent_findmyphone_plugin_handle_findmyphone_request (ValentFindmyphonePlugin *self)
      30                 :             : {
      31         [ #  # ]:           0 :   g_assert (VALENT_IS_FINDMYPHONE_PLUGIN (self));
      32                 :             : 
      33                 :           0 :   valent_session_set_locked (self->session, FALSE);
      34                 :           0 :   valent_findmyphone_ringer_toggle (self->ringer, self);
      35                 :           0 : }
      36                 :             : 
      37                 :             : /*
      38                 :             :  * GActions
      39                 :             :  */
      40                 :             : static void
      41                 :           1 : ring_action (GSimpleAction *action,
      42                 :             :              GVariant      *parameter,
      43                 :             :              gpointer       user_data)
      44                 :             : {
      45                 :           1 :   ValentFindmyphonePlugin *self = VALENT_FINDMYPHONE_PLUGIN (user_data);
      46                 :           2 :   g_autoptr (JsonNode) packet = NULL;
      47                 :             : 
      48         [ +  - ]:           1 :   g_assert (VALENT_IS_FINDMYPHONE_PLUGIN (self));
      49                 :             : 
      50                 :           1 :   packet = valent_packet_new ("kdeconnect.findmyphone.request");
      51         [ +  - ]:           1 :   valent_device_plugin_queue_packet (VALENT_DEVICE_PLUGIN (self), packet);
      52                 :           1 : }
      53                 :             : 
      54                 :             : static const GActionEntry actions[] = {
      55                 :             :     {"ring", ring_action, NULL, NULL, NULL}
      56                 :             : };
      57                 :             : 
      58                 :             : /*
      59                 :             :  * ValentDevicePlugin
      60                 :             :  */
      61                 :             : static void
      62                 :           9 : valent_findmyphone_plugin_update_state (ValentDevicePlugin *plugin,
      63                 :             :                                         ValentDeviceState   state)
      64                 :             : {
      65                 :           9 :   ValentFindmyphonePlugin *self = VALENT_FINDMYPHONE_PLUGIN (plugin);
      66                 :           9 :   gboolean available;
      67                 :             : 
      68         [ +  - ]:           9 :   g_assert (VALENT_IS_FINDMYPHONE_PLUGIN (self));
      69                 :             : 
      70                 :           9 :   available = (state & VALENT_DEVICE_STATE_CONNECTED) != 0 &&
      71                 :             :               (state & VALENT_DEVICE_STATE_PAIRED) != 0;
      72                 :             : 
      73                 :             :   /* Stop any ringing */
      74   [ +  +  -  + ]:           9 :   if (!available && valent_findmyphone_ringer_is_owner (self->ringer, self))
      75                 :           0 :     valent_findmyphone_ringer_hide (self->ringer);
      76                 :             : 
      77                 :           9 :   valent_extension_toggle_actions (VALENT_EXTENSION (plugin), available);
      78                 :           9 : }
      79                 :             : 
      80                 :             : static void
      81                 :           0 : valent_findmyphone_plugin_handle_packet (ValentDevicePlugin *plugin,
      82                 :             :                                          const char         *type,
      83                 :             :                                          JsonNode           *packet)
      84                 :             : {
      85                 :           0 :   ValentFindmyphonePlugin *self = VALENT_FINDMYPHONE_PLUGIN (plugin);
      86                 :             : 
      87         [ #  # ]:           0 :   g_assert (VALENT_IS_FINDMYPHONE_PLUGIN (self));
      88         [ #  # ]:           0 :   g_assert (type != NULL);
      89         [ #  # ]:           0 :   g_assert (VALENT_IS_PACKET (packet));
      90                 :             : 
      91         [ #  # ]:           0 :   if (g_str_equal (type, "kdeconnect.findmyphone.request"))
      92                 :           0 :     valent_findmyphone_plugin_handle_findmyphone_request (self);
      93                 :             :   else
      94                 :           0 :     g_assert_not_reached ();
      95                 :           0 : }
      96                 :             : 
      97                 :             : /*
      98                 :             :  * ValentObject
      99                 :             :  */
     100                 :             : static void
     101                 :           8 : valent_findmyphone_plugin_destroy (ValentObject *object)
     102                 :             : {
     103                 :           8 :   ValentFindmyphonePlugin *self = VALENT_FINDMYPHONE_PLUGIN (object);
     104                 :           8 :   ValentDevicePlugin *plugin = VALENT_DEVICE_PLUGIN (object);
     105                 :             : 
     106                 :             :   /* Release the ringer singleton */
     107         [ +  + ]:           8 :   g_clear_pointer (&self->ringer, valent_findmyphone_ringer_release);
     108                 :           8 :   self->session = NULL;
     109                 :             : 
     110                 :           8 :   valent_device_plugin_set_menu_item (plugin, "device.findmyphone.ring", NULL);
     111                 :             : 
     112                 :           8 :   VALENT_OBJECT_CLASS (valent_findmyphone_plugin_parent_class)->destroy (object);
     113                 :           8 : }
     114                 :             : 
     115                 :             : /*
     116                 :             :  * GObject
     117                 :             :  */
     118                 :             : static void
     119                 :           4 : valent_findmyphone_plugin_constructed (GObject *object)
     120                 :             : {
     121                 :           4 :   ValentDevicePlugin *plugin = VALENT_DEVICE_PLUGIN (object);
     122                 :           4 :   ValentFindmyphonePlugin *self = VALENT_FINDMYPHONE_PLUGIN (object);
     123                 :             : 
     124                 :           4 :   g_action_map_add_action_entries (G_ACTION_MAP (plugin),
     125                 :             :                                    actions,
     126                 :             :                                    G_N_ELEMENTS (actions),
     127                 :             :                                    plugin);
     128                 :           4 :   valent_device_plugin_set_menu_action (plugin,
     129                 :             :                                         "device.findmyphone.ring",
     130                 :           4 :                                         _("Ring"),
     131                 :             :                                         "phonelink-ring-symbolic");
     132                 :             : 
     133                 :             :   /* Acquire the ringer singleton and ensure the ValentSession component is
     134                 :             :    * prepared. */
     135                 :           4 :   self->ringer = valent_findmyphone_ringer_acquire ();
     136                 :           4 :   self->session = valent_session_get_default ();
     137                 :             : 
     138                 :           4 :   G_OBJECT_CLASS (valent_findmyphone_plugin_parent_class)->constructed (object);
     139                 :           4 : }
     140                 :             : 
     141                 :             : static void
     142                 :           1 : valent_findmyphone_plugin_class_init (ValentFindmyphonePluginClass *klass)
     143                 :             : {
     144                 :           1 :   GObjectClass *object_class = G_OBJECT_CLASS (klass);
     145                 :           1 :   ValentObjectClass *vobject_class = VALENT_OBJECT_CLASS (klass);
     146                 :           1 :   ValentDevicePluginClass *plugin_class = VALENT_DEVICE_PLUGIN_CLASS (klass);
     147                 :             : 
     148                 :           1 :   object_class->constructed = valent_findmyphone_plugin_constructed;
     149                 :             : 
     150                 :           1 :   vobject_class->destroy = valent_findmyphone_plugin_destroy;
     151                 :             : 
     152                 :           1 :   plugin_class->handle_packet = valent_findmyphone_plugin_handle_packet;
     153                 :           1 :   plugin_class->update_state = valent_findmyphone_plugin_update_state;
     154                 :             : }
     155                 :             : 
     156                 :             : static void
     157                 :           4 : valent_findmyphone_plugin_init (ValentFindmyphonePlugin *self)
     158                 :             : {
     159                 :           4 : }
     160                 :             : 
        

Generated by: LCOV version 2.0-1