LCOV - code coverage report
Current view: top level - src/libvalent/ui - valent-plugin-row.c (source / functions) Coverage Total Hit
Test: Code Coverage Lines: 89.4 % 66 59
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: 54.2 % 24 13

             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-plugin-row"
       5                 :             : 
       6                 :             : #include "config.h"
       7                 :             : 
       8                 :             : #include <adwaita.h>
       9                 :             : #include <libvalent-core.h>
      10                 :             : 
      11                 :             : #include "valent-plugin-row.h"
      12                 :             : 
      13                 :             : 
      14                 :             : /**
      15                 :             :  * ValentPluginRow:
      16                 :             :  *
      17                 :             :  * A [class@Gtk.ListBoxRow] used to toggle plugins.
      18                 :             :  */
      19                 :             : 
      20                 :             : struct _ValentPluginRow
      21                 :             : {
      22                 :             :   AdwExpanderRow  parent;
      23                 :             : 
      24                 :             :   ValentContext  *context;
      25                 :             :   PeasPluginInfo *plugin_info;
      26                 :             :   GSettings      *settings;
      27                 :             : 
      28                 :             :   /* template */
      29                 :             :   GtkSwitch      *plugin_enabled;
      30                 :             : };
      31                 :             : 
      32   [ +  +  +  - ]:          22 : G_DEFINE_FINAL_TYPE (ValentPluginRow, valent_plugin_row, ADW_TYPE_EXPANDER_ROW)
      33                 :             : 
      34                 :             : enum {
      35                 :             :   PROP_0,
      36                 :             :   PROP_CONTEXT,
      37                 :             :   PROP_PLUGIN_INFO,
      38                 :             :   N_PROPERTIES
      39                 :             : };
      40                 :             : 
      41                 :             : static GParamSpec *properties[N_PROPERTIES] = { NULL, };
      42                 :             : 
      43                 :             : static char *
      44                 :           6 : strv_to_str (GObject            *object,
      45                 :             :              const char * const *strv)
      46                 :             : {
      47                 :           6 :   return g_strjoinv (", ", (GStrv)strv);
      48                 :             : }
      49                 :             : 
      50                 :             : static void
      51                 :           6 : valent_plugin_row_set_context (ValentPluginRow *self,
      52                 :             :                                ValentContext   *context)
      53                 :             : {
      54         [ +  - ]:           6 :   g_assert (VALENT_IS_PLUGIN_ROW (self));
      55   [ +  -  -  + ]:           6 :   g_assert (context == NULL || VALENT_IS_CONTEXT (context));
      56                 :             : 
      57         [ +  - ]:           6 :   if (!g_set_object (&self->context, context))
      58                 :             :     return;
      59                 :             : 
      60                 :           6 :   self->settings = valent_context_create_settings (self->context,
      61                 :             :                                                    "ca.andyholmes.Valent.Plugin");
      62                 :           6 :   g_settings_bind (self->settings,       "enabled",
      63                 :           6 :                    self->plugin_enabled, "active",
      64                 :             :                    G_SETTINGS_BIND_DEFAULT);
      65                 :           6 :   gtk_switch_set_active (self->plugin_enabled,
      66                 :             :                          g_settings_get_boolean (self->settings, "enabled"));
      67                 :             : }
      68                 :             : 
      69                 :             : /*
      70                 :             :  * GObject
      71                 :             :  */
      72                 :             : static void
      73                 :           6 : valent_plugin_row_dispose (GObject *object)
      74                 :             : {
      75                 :           6 :   ValentPluginRow *self = VALENT_PLUGIN_ROW (object);
      76                 :             : 
      77                 :           6 :   gtk_widget_dispose_template (GTK_WIDGET (self), VALENT_TYPE_PLUGIN_ROW);
      78                 :             : 
      79                 :           6 :   G_OBJECT_CLASS (valent_plugin_row_parent_class)->dispose (object);
      80                 :           6 : }
      81                 :             : 
      82                 :             : static void
      83                 :           6 : valent_plugin_row_finalize (GObject *object)
      84                 :             : {
      85                 :           6 :   ValentPluginRow *self = VALENT_PLUGIN_ROW (object);
      86                 :             : 
      87         [ +  - ]:           6 :   g_clear_object (&self->context);
      88         [ +  - ]:           6 :   g_clear_object (&self->plugin_info);
      89         [ +  - ]:           6 :   g_clear_object (&self->settings);
      90                 :             : 
      91                 :           6 :   G_OBJECT_CLASS (valent_plugin_row_parent_class)->finalize (object);
      92                 :           6 : }
      93                 :             : 
      94                 :             : static void
      95                 :         120 : valent_plugin_row_get_property (GObject    *object,
      96                 :             :                                 guint       prop_id,
      97                 :             :                                 GValue     *value,
      98                 :             :                                 GParamSpec *pspec)
      99                 :             : {
     100                 :         120 :   ValentPluginRow *self = VALENT_PLUGIN_ROW (object);
     101                 :             : 
     102      [ -  +  - ]:         120 :   switch (prop_id)
     103                 :             :     {
     104                 :           0 :     case PROP_CONTEXT:
     105                 :           0 :       g_value_set_object (value, self->context);
     106                 :           0 :       break;
     107                 :             : 
     108                 :         120 :     case PROP_PLUGIN_INFO:
     109                 :         120 :       g_value_set_object (value, self->plugin_info);
     110                 :         120 :       break;
     111                 :             : 
     112                 :           0 :     default:
     113                 :           0 :       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     114                 :             :     }
     115                 :         120 : }
     116                 :             : 
     117                 :             : static void
     118                 :          12 : valent_plugin_row_set_property (GObject      *object,
     119                 :             :                                 guint         prop_id,
     120                 :             :                                 const GValue *value,
     121                 :             :                                 GParamSpec   *pspec)
     122                 :             : {
     123                 :          12 :   ValentPluginRow *self = VALENT_PLUGIN_ROW (object);
     124                 :             : 
     125      [ +  +  - ]:          12 :   switch (prop_id)
     126                 :             :     {
     127                 :           6 :     case PROP_CONTEXT:
     128                 :           6 :       valent_plugin_row_set_context (self, g_value_get_object (value));
     129                 :           6 :       break;
     130                 :             : 
     131                 :           6 :     case PROP_PLUGIN_INFO:
     132                 :           6 :       self->plugin_info = g_value_dup_object (value);
     133                 :           6 :       break;
     134                 :             : 
     135                 :           0 :     default:
     136                 :           0 :       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     137                 :             :     }
     138                 :          12 : }
     139                 :             : 
     140                 :             : static void
     141                 :           2 : valent_plugin_row_class_init (ValentPluginRowClass *klass)
     142                 :             : {
     143                 :           2 :   GObjectClass *object_class = G_OBJECT_CLASS (klass);
     144                 :           2 :   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
     145                 :             : 
     146                 :           2 :   object_class->dispose = valent_plugin_row_dispose;
     147                 :           2 :   object_class->finalize = valent_plugin_row_finalize;
     148                 :           2 :   object_class->get_property = valent_plugin_row_get_property;
     149                 :           2 :   object_class->set_property = valent_plugin_row_set_property;
     150                 :             : 
     151                 :             :   /**
     152                 :             :    * ValentPluginRow:context:
     153                 :             :    *
     154                 :             :    * The [class@Valent.Context] for the [class@Valent.Extension].
     155                 :             :    */
     156                 :           4 :   properties [PROP_CONTEXT] =
     157                 :           2 :     g_param_spec_object ("context", NULL, NULL,
     158                 :             :                          VALENT_TYPE_CONTEXT,
     159                 :             :                          (G_PARAM_READWRITE |
     160                 :             :                           G_PARAM_CONSTRUCT_ONLY |
     161                 :             :                           G_PARAM_EXPLICIT_NOTIFY |
     162                 :             :                           G_PARAM_STATIC_STRINGS));
     163                 :             : 
     164                 :             :   /**
     165                 :             :    * ValentPluginRow:plugin-info:
     166                 :             :    *
     167                 :             :    * The [class@Peas.PluginInfo] describing the plugin.
     168                 :             :    */
     169                 :           4 :   properties [PROP_PLUGIN_INFO] =
     170                 :           2 :     g_param_spec_object ("plugin-info", NULL, NULL,
     171                 :             :                          PEAS_TYPE_PLUGIN_INFO,
     172                 :             :                          (G_PARAM_READWRITE |
     173                 :             :                           G_PARAM_CONSTRUCT_ONLY |
     174                 :             :                           G_PARAM_STATIC_STRINGS));
     175                 :             : 
     176                 :           2 :   g_object_class_install_properties (object_class, N_PROPERTIES, properties);
     177                 :             : 
     178                 :           2 :   gtk_widget_class_set_template_from_resource (widget_class, "/ca/andyholmes/Valent/ui/valent-plugin-row.ui");
     179                 :           2 :   gtk_widget_class_bind_template_child (widget_class, ValentPluginRow, plugin_enabled);
     180                 :           2 :   gtk_widget_class_bind_template_callback (widget_class, strv_to_str);
     181                 :           2 : }
     182                 :             : 
     183                 :             : static void
     184                 :           6 : valent_plugin_row_init (ValentPluginRow *self)
     185                 :             : {
     186                 :           6 :   gtk_widget_init_template (GTK_WIDGET (self));
     187                 :           6 : }
        

Generated by: LCOV version 2.0-1