LCOV - code coverage report
Current view: top level - src/plugins/gnome - valent-preferences-command-editor.c (source / functions) Coverage Total Hit
Test: Code Coverage Lines: 93.4 % 121 113
Test Date: 2025-04-18 23:22:27 Functions: 94.1 % 17 16
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 54.7 % 64 35

             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-runcommand-editor"
       5                 :             : 
       6                 :             : #include "config.h"
       7                 :             : 
       8                 :             : #include <adwaita.h>
       9                 :             : #include <glib/gi18n.h>
      10                 :             : #include <gio/gio.h>
      11                 :             : #include <gtk/gtk.h>
      12                 :             : #include <valent.h>
      13                 :             : 
      14                 :             : #include "valent-preferences-command-editor.h"
      15                 :             : 
      16                 :             : struct _ValentPreferencesCommandEditor
      17                 :             : {
      18                 :             :   AdwDialog    parent_instance;
      19                 :             : 
      20                 :             :   char        *uuid;
      21                 :             :   GVariant    *command;
      22                 :             : 
      23                 :             :   /* template */
      24                 :             :   AdwEntryRow *argv_entry;
      25                 :             :   AdwEntryRow *name_entry;
      26                 :             : };
      27                 :             : 
      28   [ +  +  +  - ]:          15 : G_DEFINE_TYPE (ValentPreferencesCommandEditor, valent_preferences_command_editor, ADW_TYPE_DIALOG)
      29                 :             : 
      30                 :             : typedef enum {
      31                 :             :   PROP_COMMAND = 1,
      32                 :             :   PROP_UUID,
      33                 :             : } ValentPreferencesCommandEditorProperty;
      34                 :             : 
      35                 :             : static GParamSpec *properties[PROP_UUID + 1] = { NULL, };
      36                 :             : 
      37                 :             : 
      38                 :             : static void
      39                 :           0 : on_entry_activated (AdwEntryRow                    *entry,
      40                 :             :                     ValentPreferencesCommandEditor *self)
      41                 :             : {
      42         [ #  # ]:           0 :   g_assert (VALENT_IS_PREFERENCES_COMMAND_EDITOR (self));
      43                 :             : 
      44                 :           0 :   gtk_widget_activate_action (GTK_WIDGET (self), "editor.save", NULL);
      45                 :           0 : }
      46                 :             : 
      47                 :             : static void
      48                 :           4 : on_entry_changed (AdwEntryRow                    *entry,
      49                 :             :                   ValentPreferencesCommandEditor *self)
      50                 :             : {
      51                 :           4 :   const char *argv_text;
      52                 :           4 :   const char *name_text;
      53                 :             : 
      54   [ +  -  +  - ]:           4 :   g_assert (entry == NULL || ADW_IS_ENTRY_ROW (entry));
      55         [ -  + ]:           4 :   g_assert (VALENT_IS_PREFERENCES_COMMAND_EDITOR (self));
      56                 :             : 
      57                 :           4 :   argv_text = gtk_editable_get_text (GTK_EDITABLE (self->argv_entry));
      58                 :           4 :   name_text = gtk_editable_get_text (GTK_EDITABLE (self->name_entry));
      59                 :             : 
      60   [ +  -  +  +  :           4 :   if (argv_text != NULL && *argv_text != '\0' &&
                   +  - ]
      61         [ +  + ]:           2 :       name_text != NULL && *name_text != '\0')
      62                 :           1 :     gtk_widget_action_set_enabled (GTK_WIDGET (self), "editor.save", TRUE);
      63                 :             :   else
      64                 :           3 :     gtk_widget_action_set_enabled (GTK_WIDGET (self), "editor.save", FALSE);
      65                 :           4 : }
      66                 :             : 
      67                 :             : /*
      68                 :             :  * GAction
      69                 :             :  */
      70                 :             : static void
      71                 :           1 : editor_remove_action (GtkWidget  *widget,
      72                 :             :                       const char *action_name,
      73                 :             :                       GVariant   *parameter)
      74                 :             : {
      75                 :           1 :   ValentPreferencesCommandEditor *self = VALENT_PREFERENCES_COMMAND_EDITOR (widget);
      76                 :             : 
      77                 :           1 :   gtk_editable_set_text (GTK_EDITABLE (self->name_entry), "");
      78                 :           1 :   gtk_editable_set_text (GTK_EDITABLE (self->argv_entry), "");
      79                 :             : 
      80         [ +  - ]:           1 :   g_clear_pointer (&self->command, g_variant_unref);
      81                 :           1 :   g_object_notify_by_pspec (G_OBJECT (widget), properties [PROP_COMMAND]);
      82                 :           1 : }
      83                 :             : 
      84                 :             : static void
      85                 :           1 : editor_save_action (GtkWidget  *widget,
      86                 :             :                     const char *action_name,
      87                 :             :                     GVariant   *parameter)
      88                 :             : {
      89                 :           1 :   ValentPreferencesCommandEditor *self = VALENT_PREFERENCES_COMMAND_EDITOR (widget);
      90                 :           1 :   GVariant *command = NULL;
      91                 :           1 :   const char *name_text = NULL;
      92                 :           1 :   const char *argv_text = NULL;
      93                 :             : 
      94         [ +  - ]:           1 :   g_assert (VALENT_IS_PREFERENCES_COMMAND_EDITOR (self));
      95                 :             : 
      96                 :           1 :   name_text = gtk_editable_get_text (GTK_EDITABLE (self->name_entry));
      97                 :           1 :   argv_text = gtk_editable_get_text (GTK_EDITABLE (self->argv_entry));
      98                 :             : 
      99   [ +  -  +  -  :           1 :   g_return_if_fail (argv_text != NULL && *argv_text != '\0' &&
             +  -  -  + ]
     100                 :             :                     name_text != NULL && *name_text != '\0');
     101                 :             : 
     102                 :           1 :   command = g_variant_new_parsed ("{"
     103                 :             :                                     "'name': <%s>, "
     104                 :             :                                     "'command': <%s>"
     105                 :             :                                   "}",
     106                 :             :                                   name_text,
     107                 :             :                                   argv_text);
     108                 :             : 
     109         [ +  - ]:           1 :   g_clear_pointer (&self->command, g_variant_unref);
     110                 :           1 :   self->command = g_variant_ref_sink (command);
     111                 :           1 :   g_object_notify_by_pspec (G_OBJECT (widget), properties [PROP_COMMAND]);
     112                 :             : }
     113                 :             : 
     114                 :             : /*
     115                 :             :  * GObject
     116                 :             :  */
     117                 :             : static void
     118                 :           1 : valent_preferences_command_editor_dispose (GObject *object)
     119                 :             : {
     120                 :           1 :   GtkWidget *widget = GTK_WIDGET (object);
     121                 :             : 
     122                 :           1 :   gtk_widget_dispose_template (widget, VALENT_TYPE_PREFERENCES_COMMAND_EDITOR);
     123                 :             : 
     124                 :           1 :   G_OBJECT_CLASS (valent_preferences_command_editor_parent_class)->dispose (object);
     125                 :           1 : }
     126                 :             : 
     127                 :             : static void
     128                 :           1 : valent_preferences_command_editor_finalize (GObject *object)
     129                 :             : {
     130                 :           1 :   ValentPreferencesCommandEditor *self = VALENT_PREFERENCES_COMMAND_EDITOR (object);
     131                 :             : 
     132         [ -  + ]:           1 :   g_clear_pointer (&self->command, g_variant_unref);
     133         [ +  - ]:           1 :   g_clear_pointer (&self->uuid, g_free);
     134                 :             : 
     135                 :           1 :   G_OBJECT_CLASS (valent_preferences_command_editor_parent_class)->finalize (object);
     136                 :           1 : }
     137                 :             : 
     138                 :             : static void
     139                 :           2 : valent_preferences_command_editor_get_property (GObject    *object,
     140                 :             :                                                 guint       prop_id,
     141                 :             :                                                 GValue     *value,
     142                 :             :                                                 GParamSpec *pspec)
     143                 :             : {
     144                 :           2 :   ValentPreferencesCommandEditor *self = VALENT_PREFERENCES_COMMAND_EDITOR (object);
     145                 :             : 
     146      [ +  +  - ]:           2 :   switch ((ValentPreferencesCommandEditorProperty)prop_id)
     147                 :             :     {
     148                 :           1 :     case PROP_COMMAND:
     149                 :           1 :       g_value_set_variant (value, self->command);
     150                 :           1 :       break;
     151                 :             : 
     152                 :           1 :     case PROP_UUID:
     153                 :           1 :       g_value_set_string (value, self->uuid);
     154                 :           1 :       break;
     155                 :             : 
     156                 :           0 :     default:
     157                 :           0 :       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     158                 :             :     }
     159                 :           2 : }
     160                 :             : 
     161                 :             : static void
     162                 :           2 : valent_preferences_command_editor_set_property (GObject      *object,
     163                 :             :                                                 guint         prop_id,
     164                 :             :                                                 const GValue *value,
     165                 :             :                                                 GParamSpec   *pspec)
     166                 :             : {
     167                 :           2 :   ValentPreferencesCommandEditor *self = VALENT_PREFERENCES_COMMAND_EDITOR (object);
     168                 :             : 
     169      [ +  +  - ]:           2 :   switch ((ValentPreferencesCommandEditorProperty)prop_id)
     170                 :             :     {
     171                 :           1 :     case PROP_COMMAND:
     172                 :           1 :       valent_preferences_command_editor_set_command (self, g_value_get_variant (value));
     173                 :           1 :       break;
     174                 :             : 
     175                 :           1 :     case PROP_UUID:
     176                 :           1 :       valent_preferences_command_editor_set_uuid (self, g_value_get_string (value));
     177                 :           1 :       break;
     178                 :             : 
     179                 :           0 :     default:
     180                 :           0 :       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     181                 :             :     }
     182                 :           2 : }
     183                 :             : 
     184                 :             : static void
     185                 :           1 : valent_preferences_command_editor_class_init (ValentPreferencesCommandEditorClass *klass)
     186                 :             : {
     187                 :           1 :   GObjectClass *object_class = G_OBJECT_CLASS (klass);
     188                 :           1 :   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
     189                 :             : 
     190                 :           1 :   object_class->dispose = valent_preferences_command_editor_dispose;
     191                 :           1 :   object_class->finalize = valent_preferences_command_editor_finalize;
     192                 :           1 :   object_class->get_property = valent_preferences_command_editor_get_property;
     193                 :           1 :   object_class->set_property = valent_preferences_command_editor_set_property;
     194                 :             : 
     195                 :           2 :   properties [PROP_COMMAND] =
     196                 :           1 :     g_param_spec_variant ("command", NULL, NULL,
     197                 :             :                           G_VARIANT_TYPE_VARDICT,
     198                 :             :                           NULL,
     199                 :             :                          (G_PARAM_READWRITE |
     200                 :             :                           G_PARAM_CONSTRUCT |
     201                 :             :                           G_PARAM_EXPLICIT_NOTIFY |
     202                 :             :                           G_PARAM_STATIC_STRINGS));
     203                 :             : 
     204                 :           2 :   properties [PROP_UUID] =
     205                 :           1 :     g_param_spec_string ("uuid", NULL, NULL,
     206                 :             :                          "",
     207                 :             :                          (G_PARAM_READWRITE |
     208                 :             :                           G_PARAM_CONSTRUCT |
     209                 :             :                           G_PARAM_EXPLICIT_NOTIFY |
     210                 :             :                           G_PARAM_STATIC_STRINGS));
     211                 :             : 
     212                 :           1 :   g_object_class_install_properties (object_class, G_N_ELEMENTS (properties), properties);
     213                 :             : 
     214                 :           1 :   gtk_widget_class_set_template_from_resource (widget_class, "/plugins/gnome/valent-preferences-command-editor.ui");
     215                 :           1 :   gtk_widget_class_bind_template_child (widget_class, ValentPreferencesCommandEditor, argv_entry);
     216                 :           1 :   gtk_widget_class_bind_template_child (widget_class, ValentPreferencesCommandEditor, name_entry);
     217                 :           1 :   gtk_widget_class_bind_template_callback (widget_class, on_entry_activated);
     218                 :           1 :   gtk_widget_class_bind_template_callback (widget_class, on_entry_changed);
     219                 :             : 
     220                 :           1 :   gtk_widget_class_install_action (widget_class, "editor.remove", NULL, editor_remove_action);
     221                 :           1 :   gtk_widget_class_install_action (widget_class, "editor.save", NULL, editor_save_action);
     222                 :           1 : }
     223                 :             : 
     224                 :             : static void
     225                 :           1 : valent_preferences_command_editor_init (ValentPreferencesCommandEditor *self)
     226                 :             : {
     227                 :           1 :   gtk_widget_init_template (GTK_WIDGET (self));
     228                 :           1 : }
     229                 :             : 
     230                 :             : GVariant *
     231                 :           2 : valent_preferences_command_editor_get_command (ValentPreferencesCommandEditor *editor)
     232                 :             : {
     233         [ +  - ]:           2 :   g_return_val_if_fail (VALENT_IS_PREFERENCES_COMMAND_EDITOR (editor), NULL);
     234                 :             : 
     235                 :           2 :   return editor->command;
     236                 :             : }
     237                 :             : 
     238                 :             : void
     239                 :           1 : valent_preferences_command_editor_set_command (ValentPreferencesCommandEditor *editor,
     240                 :             :                                                GVariant                       *command)
     241                 :             : {
     242                 :           1 :   const char *name_text = "";
     243                 :           1 :   const char *argv_text = "";
     244                 :             : 
     245         [ +  - ]:           1 :   g_return_if_fail (VALENT_IS_PREFERENCES_COMMAND_EDITOR (editor));
     246   [ +  -  -  + ]:           1 :   g_return_if_fail (command == NULL || g_variant_is_of_type (command, G_VARIANT_TYPE_VARDICT));
     247                 :             : 
     248         [ -  + ]:           1 :   g_clear_pointer (&editor->command, g_variant_unref);
     249         [ +  - ]:           1 :   if (command != NULL)
     250                 :             :     {
     251                 :           1 :       editor->command = g_variant_ref_sink (command);
     252                 :           1 :       g_variant_lookup (editor->command, "name", "&s", &name_text);
     253                 :           1 :       g_variant_lookup (editor->command, "command", "&s", &argv_text);
     254                 :             :     }
     255                 :             : 
     256                 :           1 :   gtk_editable_set_text (GTK_EDITABLE (editor->name_entry), name_text);
     257                 :           1 :   gtk_editable_set_text (GTK_EDITABLE (editor->argv_entry), argv_text);
     258                 :           1 :   gtk_widget_action_set_enabled (GTK_WIDGET (editor), "editor.remove",
     259                 :           1 :                                  editor->command != NULL);
     260                 :             : 
     261                 :           1 :   g_object_notify_by_pspec (G_OBJECT (editor), properties [PROP_COMMAND]);
     262                 :             : }
     263                 :             : 
     264                 :             : const char *
     265                 :           2 : valent_preferences_command_editor_get_uuid (ValentPreferencesCommandEditor *editor)
     266                 :             : {
     267         [ +  - ]:           2 :   g_return_val_if_fail (VALENT_IS_PREFERENCES_COMMAND_EDITOR (editor), NULL);
     268                 :             : 
     269                 :           2 :   return editor->uuid;
     270                 :             : }
     271                 :             : 
     272                 :             : void
     273                 :           1 : valent_preferences_command_editor_set_uuid (ValentPreferencesCommandEditor *editor,
     274                 :             :                                             const char                     *uuid)
     275                 :             : {
     276         [ +  - ]:           1 :   g_return_if_fail (VALENT_IS_PREFERENCES_COMMAND_EDITOR (editor));
     277         [ -  + ]:           1 :   g_return_if_fail (uuid != NULL);
     278                 :             : 
     279         [ +  - ]:           1 :   if (g_set_str (&editor->uuid, uuid))
     280                 :           1 :     g_object_notify_by_pspec (G_OBJECT (editor), properties [PROP_UUID]);
     281                 :             : }
     282                 :             : 
        

Generated by: LCOV version 2.0-1