LCOV - code coverage report
Current view: top level - src/plugins/ping - valent-ping-plugin.c (source / functions) Coverage Total Hit
Test: Code Coverage Lines: 100.0 % 70 70
Test Date: 2024-04-23 06:02:46 Functions: 100.0 % 11 11
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 61.1 % 36 22

             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-ping-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-ping-plugin.h"
      14                 :             : 
      15                 :             : 
      16                 :             : struct _ValentPingPlugin
      17                 :             : {
      18                 :             :   ValentDevicePlugin  parent_instance;
      19                 :             : };
      20                 :             : 
      21   [ +  +  +  - ]:          24 : G_DEFINE_FINAL_TYPE (ValentPingPlugin, valent_ping_plugin, VALENT_TYPE_DEVICE_PLUGIN)
      22                 :             : 
      23                 :             : 
      24                 :             : static void
      25                 :           2 : valent_ping_plugin_handle_ping (ValentPingPlugin *self,
      26                 :             :                                 JsonNode         *packet)
      27                 :             : {
      28                 :           4 :   g_autoptr (GNotification) notification = NULL;
      29                 :           2 :   const char *message;
      30                 :           2 :   ValentDevice *device;
      31                 :             : 
      32         [ +  - ]:           2 :   g_assert (VALENT_IS_PING_PLUGIN (self));
      33         [ -  + ]:           2 :   g_assert (VALENT_IS_PACKET (packet));
      34                 :             : 
      35                 :             :   /* Check for the optional message */
      36         [ +  + ]:           2 :   if (!valent_packet_get_string (packet, "message", &message))
      37                 :           1 :     message = _("Ping!");
      38                 :             : 
      39                 :             :   /* Show a notification */
      40                 :           2 :   device = valent_extension_get_object (VALENT_EXTENSION (self));
      41                 :           2 :   notification = g_notification_new (valent_device_get_name (device));
      42                 :           2 :   g_notification_set_body (notification, message);
      43         [ +  - ]:           2 :   valent_device_plugin_show_notification (VALENT_DEVICE_PLUGIN (self),
      44                 :             :                                           "ping",
      45                 :             :                                           notification);
      46                 :           2 : }
      47                 :             : 
      48                 :             : static void
      49                 :           2 : valent_ping_plugin_send_ping (ValentPingPlugin *self,
      50                 :             :                               const char       *message)
      51                 :             : {
      52                 :           4 :   g_autoptr (JsonBuilder) builder = NULL;
      53         [ -  + ]:           2 :   g_autoptr (JsonNode) packet = NULL;
      54                 :             : 
      55         [ +  - ]:           2 :   g_assert (VALENT_IS_PING_PLUGIN (self));
      56                 :             : 
      57                 :           2 :   valent_packet_init (&builder, "kdeconnect.ping");
      58                 :             : 
      59   [ +  +  +  - ]:           2 :   if (message != NULL && *message != '\0')
      60                 :             :     {
      61                 :           1 :       json_builder_set_member_name (builder, "message");
      62                 :           1 :       json_builder_add_string_value (builder, message);
      63                 :             :     }
      64                 :             : 
      65                 :           2 :   packet = valent_packet_end (&builder);
      66                 :             : 
      67         [ +  - ]:           2 :   valent_device_plugin_queue_packet (VALENT_DEVICE_PLUGIN (self), packet);
      68                 :           2 : }
      69                 :             : 
      70                 :             : /*
      71                 :             :  * GActions
      72                 :             :  */
      73                 :             : static void
      74                 :           2 : ping_action (GSimpleAction *action,
      75                 :             :              GVariant      *parameter,
      76                 :             :              gpointer       user_data)
      77                 :             : {
      78                 :           2 :   ValentPingPlugin *self = VALENT_PING_PLUGIN (user_data);
      79                 :           2 :   const char *message = NULL;
      80                 :             : 
      81         [ +  - ]:           2 :   g_assert (VALENT_IS_PING_PLUGIN (self));
      82                 :             : 
      83         [ +  + ]:           2 :   if (parameter != NULL)
      84                 :           1 :     message = g_variant_get_string (parameter, NULL);
      85                 :             : 
      86                 :           2 :   valent_ping_plugin_send_ping (self, message);
      87                 :           2 : }
      88                 :             : 
      89                 :             : static const GActionEntry actions[] = {
      90                 :             :     {"ping",    ping_action, NULL, NULL, NULL},
      91                 :             :     {"message", ping_action, "s",  NULL, NULL}
      92                 :             : };
      93                 :             : 
      94                 :             : /*
      95                 :             :  * ValentDevicePlugin
      96                 :             :  */
      97                 :             : static void
      98                 :          13 : valent_ping_plugin_update_state (ValentDevicePlugin *plugin,
      99                 :             :                                  ValentDeviceState   state)
     100                 :             : {
     101                 :          13 :   gboolean available;
     102                 :             : 
     103         [ +  - ]:          13 :   g_assert (VALENT_IS_PING_PLUGIN (plugin));
     104                 :             : 
     105                 :          13 :   available = (state & VALENT_DEVICE_STATE_CONNECTED) != 0 &&
     106                 :             :               (state & VALENT_DEVICE_STATE_PAIRED) != 0;
     107                 :             : 
     108                 :          13 :   valent_extension_toggle_actions (VALENT_EXTENSION (plugin), available);
     109                 :          13 : }
     110                 :             : 
     111                 :             : static void
     112                 :           2 : valent_ping_plugin_handle_packet (ValentDevicePlugin *plugin,
     113                 :             :                                   const char         *type,
     114                 :             :                                   JsonNode           *packet)
     115                 :             : {
     116                 :           2 :   ValentPingPlugin *self = VALENT_PING_PLUGIN (plugin);
     117                 :             : 
     118         [ +  - ]:           2 :   g_assert (VALENT_IS_PING_PLUGIN (self));
     119         [ -  + ]:           2 :   g_assert (type != NULL);
     120         [ -  + ]:           2 :   g_assert (VALENT_IS_PACKET (packet));
     121                 :             : 
     122         [ +  - ]:           2 :   if (g_str_equal (type, "kdeconnect.ping"))
     123                 :           2 :     valent_ping_plugin_handle_ping (self, packet);
     124                 :             :   else
     125                 :           2 :     g_assert_not_reached ();
     126                 :           2 : }
     127                 :             : 
     128                 :             : /*
     129                 :             :  * ValentObject
     130                 :             :  */
     131                 :             : static void
     132                 :           8 : valent_ping_plugin_destroy (ValentObject *object)
     133                 :             : {
     134                 :           8 :   ValentDevicePlugin *plugin = VALENT_DEVICE_PLUGIN (object);
     135                 :             : 
     136                 :           8 :   valent_device_plugin_set_menu_item (plugin, "device.ping.ping", NULL);
     137                 :             : 
     138                 :           8 :   VALENT_OBJECT_CLASS (valent_ping_plugin_parent_class)->destroy (object);
     139                 :           8 : }
     140                 :             : 
     141                 :             : /*
     142                 :             :  * GObject
     143                 :             :  */
     144                 :             : static void
     145                 :           4 : valent_ping_plugin_constructed (GObject *object)
     146                 :             : {
     147                 :           4 :   ValentDevicePlugin *plugin = VALENT_DEVICE_PLUGIN (object);
     148                 :             : 
     149                 :           4 :   g_action_map_add_action_entries (G_ACTION_MAP (plugin),
     150                 :             :                                    actions,
     151                 :             :                                    G_N_ELEMENTS (actions),
     152                 :             :                                    plugin);
     153                 :           4 :   valent_device_plugin_set_menu_action (plugin,
     154                 :             :                                         "device.ping.ping",
     155                 :           4 :                                         _("Ping"),
     156                 :             :                                         "dialog-information-symbolic");
     157                 :             : 
     158                 :           4 :   G_OBJECT_CLASS (valent_ping_plugin_parent_class)->constructed (object);
     159                 :           4 : }
     160                 :             : 
     161                 :             : static void
     162                 :           1 : valent_ping_plugin_class_init (ValentPingPluginClass *klass)
     163                 :             : {
     164                 :           1 :   GObjectClass *object_class = G_OBJECT_CLASS (klass);
     165                 :           1 :   ValentObjectClass *vobject_class = VALENT_OBJECT_CLASS (klass);
     166                 :           1 :   ValentDevicePluginClass *plugin_class = VALENT_DEVICE_PLUGIN_CLASS (klass);
     167                 :             : 
     168                 :           1 :   object_class->constructed = valent_ping_plugin_constructed;
     169                 :             : 
     170                 :           1 :   vobject_class->destroy = valent_ping_plugin_destroy;
     171                 :             : 
     172                 :           1 :   plugin_class->handle_packet = valent_ping_plugin_handle_packet;
     173                 :           1 :   plugin_class->update_state = valent_ping_plugin_update_state;
     174                 :             : }
     175                 :             : 
     176                 :             : static void
     177                 :           4 : valent_ping_plugin_init (ValentPingPlugin *self)
     178                 :             : {
     179                 :           4 : }
     180                 :             : 
        

Generated by: LCOV version 2.0-1