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

           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                 :            :  * GObject
     130                 :            :  */
     131                 :            : static void
     132                 :          4 : valent_ping_plugin_constructed (GObject *object)
     133                 :            : {
     134                 :          4 :   ValentDevicePlugin *plugin = VALENT_DEVICE_PLUGIN (object);
     135                 :            : 
     136                 :          4 :   g_action_map_add_action_entries (G_ACTION_MAP (plugin),
     137                 :            :                                    actions,
     138                 :            :                                    G_N_ELEMENTS (actions),
     139                 :            :                                    plugin);
     140                 :          4 :   valent_device_plugin_set_menu_action (plugin,
     141                 :            :                                         "device.ping.ping",
     142                 :          4 :                                         _("Ping"),
     143                 :            :                                         "dialog-information-symbolic");
     144                 :            : 
     145                 :          4 :   G_OBJECT_CLASS (valent_ping_plugin_parent_class)->constructed (object);
     146                 :          4 : }
     147                 :            : 
     148                 :            : static void
     149                 :          8 : valent_ping_plugin_dispose (GObject *object)
     150                 :            : {
     151                 :          8 :   ValentDevicePlugin *plugin = VALENT_DEVICE_PLUGIN (object);
     152                 :            : 
     153                 :          8 :   valent_device_plugin_set_menu_item (plugin, "device.ping.ping", NULL);
     154                 :            : 
     155                 :          8 :   G_OBJECT_CLASS (valent_ping_plugin_parent_class)->dispose (object);
     156                 :          8 : }
     157                 :            : 
     158                 :            : static void
     159                 :          1 : valent_ping_plugin_class_init (ValentPingPluginClass *klass)
     160                 :            : {
     161                 :          1 :   GObjectClass *object_class = G_OBJECT_CLASS (klass);
     162                 :          1 :   ValentDevicePluginClass *plugin_class = VALENT_DEVICE_PLUGIN_CLASS (klass);
     163                 :            : 
     164                 :          1 :   object_class->constructed = valent_ping_plugin_constructed;
     165                 :          1 :   object_class->dispose = valent_ping_plugin_dispose;
     166                 :            : 
     167                 :          1 :   plugin_class->handle_packet = valent_ping_plugin_handle_packet;
     168                 :          1 :   plugin_class->update_state = valent_ping_plugin_update_state;
     169                 :            : }
     170                 :            : 
     171                 :            : static void
     172                 :          4 : valent_ping_plugin_init (ValentPingPlugin *self)
     173                 :            : {
     174                 :          4 : }
     175                 :            : 

Generated by: LCOV version 1.14