LCOV - code coverage report
Current view: top level - src/plugins/telephony - valent-telephony-preferences.c (source / functions) Coverage Total Hit
Test: Code Coverage Lines: 73.5 % 68 50
Test Date: 2024-04-23 06:02:46 Functions: 81.8 % 11 9
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 33.3 % 12 4

             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-telephony-preferences"
       5                 :             : 
       6                 :             : #include "config.h"
       7                 :             : 
       8                 :             : #include <glib/gi18n.h>
       9                 :             : #include <adwaita.h>
      10                 :             : #include <gtk/gtk.h>
      11                 :             : #include <valent.h>
      12                 :             : 
      13                 :             : #include "valent-telephony-preferences.h"
      14                 :             : 
      15                 :             : 
      16                 :             : struct _ValentTelephonyPreferences
      17                 :             : {
      18                 :             :   ValentDevicePreferencesGroup  parent_instance;
      19                 :             : 
      20                 :             :   /* template */
      21                 :             :   AdwComboRow                  *ringing_volume;
      22                 :             :   GtkSwitch                    *ringing_pause;
      23                 :             : 
      24                 :             :   AdwComboRow                  *talking_volume;
      25                 :             :   GtkSwitch                    *talking_pause;
      26                 :             :   GtkSwitch                    *talking_microphone;
      27                 :             : };
      28                 :             : 
      29   [ +  +  +  - ]:           7 : G_DEFINE_FINAL_TYPE (ValentTelephonyPreferences, valent_telephony_preferences, VALENT_TYPE_DEVICE_PREFERENCES_GROUP)
      30                 :             : 
      31                 :             : 
      32                 :             : /* Mapping functions for converting our [-1...100] volume range to a boolean,
      33                 :             :  * where -1 generally means "don't change", 0 means mute and any other value is
      34                 :             :  * a volume percentage.
      35                 :             :  * */
      36                 :             : static gboolean
      37                 :           1 : get_mute_volume_boolean (GValue   *value,
      38                 :             :                          GVariant *variant,
      39                 :             :                          gpointer  user_data)
      40                 :             : {
      41                 :           1 :   int volume;
      42                 :             : 
      43                 :           1 :   volume = g_variant_get_int32 (variant);
      44                 :             : 
      45                 :           1 :   g_value_set_boolean (value, (volume == 0));
      46                 :             : 
      47                 :           1 :   return TRUE;
      48                 :             : }
      49                 :             : 
      50                 :             : static GVariant *
      51                 :           0 : set_mute_volume_boolean (const GValue       *value,
      52                 :             :                          const GVariantType *expected_type,
      53                 :             :                          gpointer            user_data)
      54                 :             : {
      55                 :           0 :   gboolean mute_volume;
      56                 :             : 
      57                 :           0 :   mute_volume = g_value_get_boolean (value);
      58                 :             : 
      59         [ #  # ]:           0 :   if (mute_volume)
      60                 :           0 :     return g_variant_new_int32 (0);
      61                 :             :   else
      62                 :           0 :     return g_variant_new_int32 (-1);
      63                 :             : }
      64                 :             : 
      65                 :             : #define VOLUME_NOTHING -1
      66                 :             : #define VOLUME_LOWER   15
      67                 :             : #define VOLUME_MUTE     0
      68                 :             : 
      69                 :             : static gboolean
      70                 :           2 : get_volume (GValue   *value,
      71                 :             :             GVariant *variant,
      72                 :             :             gpointer  user_data)
      73                 :             : {
      74                 :           2 :   int volume;
      75                 :           2 :   guint selected;
      76                 :             : 
      77                 :           2 :   volume = g_variant_get_int32 (variant);
      78                 :             : 
      79         [ +  - ]:           2 :   switch (volume)
      80                 :             :     {
      81                 :             :     case VOLUME_NOTHING:
      82                 :             :       selected = 0;
      83                 :             :       break;
      84                 :             : 
      85                 :             :     case VOLUME_LOWER:
      86                 :             :       selected = 1;
      87                 :             :       break;
      88                 :             : 
      89                 :             :     case VOLUME_MUTE:
      90                 :             :       selected = 2;
      91                 :             :       break;
      92                 :             : 
      93                 :             :     default:
      94                 :             :       selected = GTK_INVALID_LIST_POSITION;
      95                 :             :     }
      96                 :             : 
      97                 :           2 :   g_value_set_uint (value, selected);
      98                 :             : 
      99                 :           2 :   return TRUE;
     100                 :             : }
     101                 :             : 
     102                 :             : static GVariant *
     103                 :           0 : set_volume (const GValue       *value,
     104                 :             :             const GVariantType *expected_type,
     105                 :             :             gpointer            user_data)
     106                 :             : {
     107                 :           0 :   unsigned int selected;
     108                 :             : 
     109                 :           0 :   selected = g_value_get_uint (value);
     110                 :             : 
     111   [ #  #  #  # ]:           0 :   switch (selected)
     112                 :             :     {
     113                 :           0 :     case 0:
     114                 :           0 :       return g_variant_new_int32 (-1);
     115                 :             : 
     116                 :           0 :     case 1:
     117                 :           0 :       return g_variant_new_int32 (15);
     118                 :             : 
     119                 :           0 :     case 2:
     120                 :           0 :       return g_variant_new_int32 (0);
     121                 :             : 
     122                 :           0 :     default:
     123                 :           0 :       return g_variant_new_int32 (-1);
     124                 :             :     }
     125                 :             : }
     126                 :             : 
     127                 :             : 
     128                 :             : /*
     129                 :             :  * GObject
     130                 :             :  */
     131                 :             : static void
     132                 :           1 : valent_telephony_preferences_constructed (GObject *object)
     133                 :             : {
     134                 :           1 :   ValentTelephonyPreferences *self = VALENT_TELEPHONY_PREFERENCES (object);
     135                 :           1 :   ValentDevicePreferencesGroup *group = VALENT_DEVICE_PREFERENCES_GROUP (self);
     136                 :           1 :   GSettings *settings;
     137                 :             : 
     138                 :           1 :   settings = valent_device_preferences_group_get_settings (group);
     139                 :           1 :   g_settings_bind (settings,            "ringing-pause",
     140                 :           1 :                    self->ringing_pause, "active",
     141                 :             :                    G_SETTINGS_BIND_DEFAULT);
     142                 :           1 :   g_settings_bind_with_mapping (settings,             "ringing-volume",
     143                 :           1 :                                 self->ringing_volume, "selected",
     144                 :             :                                 G_SETTINGS_BIND_DEFAULT,
     145                 :             :                                 get_volume,
     146                 :             :                                 set_volume,
     147                 :             :                                 NULL, NULL);
     148                 :           1 :   g_settings_bind_with_mapping (settings,                 "talking-microphone",
     149                 :           1 :                                 self->talking_microphone, "active",
     150                 :             :                                 G_SETTINGS_BIND_DEFAULT,
     151                 :             :                                 get_mute_volume_boolean,
     152                 :             :                                 set_mute_volume_boolean,
     153                 :             :                                 NULL, NULL);
     154                 :           1 :   g_settings_bind_with_mapping (settings,             "talking-volume",
     155                 :           1 :                                 self->talking_volume, "selected",
     156                 :             :                                 G_SETTINGS_BIND_DEFAULT,
     157                 :             :                                 get_volume,
     158                 :             :                                 set_volume,
     159                 :             :                                 NULL, NULL);
     160                 :           1 :   g_settings_bind (settings,            "talking-pause",
     161                 :           1 :                    self->talking_pause, "active",
     162                 :             :                    G_SETTINGS_BIND_DEFAULT);
     163                 :             : 
     164                 :           1 :   G_OBJECT_CLASS (valent_telephony_preferences_parent_class)->constructed (object);
     165                 :           1 : }
     166                 :             : 
     167                 :             : static void
     168                 :           1 : valent_telephony_preferences_dispose (GObject *object)
     169                 :             : {
     170                 :           1 :   GtkWidget *widget = GTK_WIDGET (object);
     171                 :             : 
     172                 :           1 :   gtk_widget_dispose_template (widget, VALENT_TYPE_TELEPHONY_PREFERENCES);
     173                 :             : 
     174                 :           1 :   G_OBJECT_CLASS (valent_telephony_preferences_parent_class)->dispose (object);
     175                 :           1 : }
     176                 :             : 
     177                 :             : static void
     178                 :           2 : valent_telephony_preferences_class_init (ValentTelephonyPreferencesClass *klass)
     179                 :             : {
     180                 :           2 :   GObjectClass *object_class = G_OBJECT_CLASS (klass);
     181                 :           2 :   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
     182                 :             : 
     183                 :           2 :   object_class->constructed = valent_telephony_preferences_constructed;
     184                 :           2 :   object_class->dispose = valent_telephony_preferences_dispose;
     185                 :             : 
     186                 :           2 :   gtk_widget_class_set_template_from_resource (widget_class, "/plugins/telephony/valent-telephony-preferences.ui");
     187                 :           2 :   gtk_widget_class_bind_template_child (widget_class, ValentTelephonyPreferences, ringing_volume);
     188                 :           2 :   gtk_widget_class_bind_template_child (widget_class, ValentTelephonyPreferences, ringing_pause);
     189                 :           2 :   gtk_widget_class_bind_template_child (widget_class, ValentTelephonyPreferences, talking_volume);
     190                 :           2 :   gtk_widget_class_bind_template_child (widget_class, ValentTelephonyPreferences, talking_pause);
     191                 :           2 :   gtk_widget_class_bind_template_child (widget_class, ValentTelephonyPreferences, talking_microphone);
     192                 :           2 : }
     193                 :             : 
     194                 :             : static void
     195                 :           1 : valent_telephony_preferences_init (ValentTelephonyPreferences *self)
     196                 :             : {
     197                 :           1 :   gtk_widget_init_template (GTK_WIDGET (self));
     198                 :           1 : }
     199                 :             : 
        

Generated by: LCOV version 2.0-1