LCOV - code coverage report
Current view: top level - src/plugins/contacts - valent-contacts-preferences.c (source / functions) Coverage Total Hit
Test: Code Coverage Lines: 71.9 % 114 82
Test Date: 2024-04-16 07:28:14 Functions: 83.3 % 12 10
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 26.8 % 56 15

             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-contacts-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-contacts-preferences.h"
      14                 :             : 
      15                 :             : 
      16                 :             : struct _ValentContactsPreferences
      17                 :             : {
      18                 :             :   ValentDevicePreferencesGroup  parent_instance;
      19                 :             : 
      20                 :             :   GHashTable                   *local_stores;
      21                 :             : 
      22                 :             :   /* template */
      23                 :             :   AdwExpanderRow               *local_sync;
      24                 :             :   GtkListBox                   *local_list;
      25                 :             :   GtkSwitch                    *remote_sync;
      26                 :             : };
      27                 :             : 
      28   [ +  +  +  - ]:           8 : G_DEFINE_FINAL_TYPE (ValentContactsPreferences, valent_contacts_preferences, VALENT_TYPE_DEVICE_PREFERENCES_GROUP)
      29                 :             : 
      30                 :             : 
      31                 :             : static void
      32                 :           0 : on_local_sync (GtkListBox                   *box,
      33                 :             :                GtkListBoxRow                *row,
      34                 :             :                ValentDevicePreferencesGroup *group)
      35                 :             : {
      36                 :           0 :   GSettings *settings;
      37                 :           0 :   g_autofree char *local_uid = NULL;
      38                 :           0 :   const char *uid;
      39                 :             : 
      40   [ #  #  #  #  :           0 :   g_assert (GTK_IS_LIST_BOX (box));
             #  #  #  # ]
      41   [ #  #  #  #  :           0 :   g_assert (GTK_IS_LIST_BOX_ROW (row));
             #  #  #  # ]
      42         [ #  # ]:           0 :   g_assert (VALENT_IS_DEVICE_PREFERENCES_GROUP (group));
      43                 :             : 
      44                 :           0 :   settings = valent_device_preferences_group_get_settings (group);
      45                 :           0 :   local_uid = g_settings_get_string (settings, "local-uid");
      46                 :           0 :   uid = gtk_widget_get_name (GTK_WIDGET (row));
      47                 :             : 
      48         [ #  # ]:           0 :   if (g_strcmp0 (local_uid, uid) == 0)
      49                 :           0 :     g_settings_reset (settings, "local-uid");
      50                 :             :   else
      51                 :           0 :     g_settings_set_string (settings, "local-uid", uid);
      52                 :             : 
      53                 :           0 :   gtk_list_box_invalidate_filter (box);
      54                 :           0 : }
      55                 :             : 
      56                 :             : static void
      57                 :           0 : on_store_selected (AdwActionRow              *row,
      58                 :             :                    ValentContactsPreferences *self)
      59                 :             : {
      60                 :           0 :   ValentDevicePreferencesGroup *group = VALENT_DEVICE_PREFERENCES_GROUP (self);
      61                 :           0 :   GSettings *settings;
      62                 :           0 :   GHashTableIter iter;
      63                 :           0 :   gpointer store, store_row;
      64                 :             : 
      65         [ #  # ]:           0 :   g_assert (ADW_IS_ACTION_ROW (row));
      66         [ #  # ]:           0 :   g_assert (VALENT_IS_CONTACTS_PREFERENCES (self));
      67                 :             : 
      68                 :           0 :   settings = valent_device_preferences_group_get_settings (group);
      69                 :             : 
      70                 :           0 :   g_hash_table_iter_init (&iter, self->local_stores);
      71                 :             : 
      72         [ #  # ]:           0 :   while (g_hash_table_iter_next (&iter, &store, &store_row))
      73                 :             :     {
      74                 :           0 :       GtkWidget *check = g_object_get_data (G_OBJECT (store_row), "select");
      75                 :             : 
      76         [ #  # ]:           0 :       if (row == store_row)
      77                 :             :         {
      78                 :           0 :           const char *local_uid;
      79                 :             : 
      80                 :           0 :           local_uid = valent_contact_store_get_uid (store);
      81                 :           0 :           g_settings_set_string (settings, "local-uid", local_uid);
      82                 :             :         }
      83                 :             : 
      84                 :           0 :       gtk_widget_set_visible (check, (row == store_row));
      85                 :             :     }
      86                 :           0 : }
      87                 :             : 
      88                 :             : static GtkWidget *
      89                 :           1 : valent_contacts_preferences_create_row_func (gpointer item,
      90                 :             :                                              gpointer user_data)
      91                 :             : {
      92                 :           1 :   ValentContactsPreferences *self = VALENT_CONTACTS_PREFERENCES (user_data);
      93                 :           1 :   ValentDevicePreferencesGroup *group = VALENT_DEVICE_PREFERENCES_GROUP (self);
      94                 :           1 :   ValentContactStore *store = VALENT_CONTACT_STORE (item);
      95                 :           1 :   ValentContext *context = NULL;
      96                 :           1 :   const char *device_id = NULL;
      97                 :           1 :   GSettings *settings;
      98                 :           1 :   GtkWidget *row;
      99                 :           1 :   GtkWidget *image;
     100                 :           1 :   const char *icon_name;
     101                 :           1 :   const char *uid;
     102                 :           2 :   g_autofree char *local_uid = NULL;
     103                 :             : 
     104         [ +  - ]:           1 :   g_assert (VALENT_IS_CONTACT_STORE (store));
     105         [ -  + ]:           1 :   g_assert (VALENT_IS_CONTACTS_PREFERENCES (self));
     106                 :             : 
     107                 :             :   /* FIXME: select an icon name for the addressbook type */
     108                 :           1 :   context = valent_device_preferences_group_get_context (group);
     109                 :           1 :   device_id = valent_context_get_id (context);
     110                 :           1 :   uid = valent_contact_store_get_uid (store);
     111                 :             : 
     112         [ +  - ]:           1 :   if (g_strcmp0 (device_id, uid) == 0)
     113                 :             :     icon_name = APPLICATION_ID"-symbolic";
     114                 :             :   else
     115                 :           1 :     icon_name = "x-office-address-book";
     116                 :             : 
     117                 :             :   /* Row */
     118                 :           1 :   row = g_object_new (ADW_TYPE_ACTION_ROW,
     119                 :             :                       "activatable", TRUE,
     120                 :             :                       "title",       valent_contact_store_get_name (store),
     121                 :             :                       NULL);
     122                 :           1 :   image = g_object_new (GTK_TYPE_IMAGE,
     123                 :             :                         "icon-name", icon_name,
     124                 :             :                         "icon-size", GTK_ICON_SIZE_NORMAL,
     125                 :             :                         NULL);
     126                 :           1 :   adw_action_row_add_prefix (ADW_ACTION_ROW (row), image);
     127                 :             : 
     128                 :           1 :   g_signal_connect_object (G_OBJECT (row),
     129                 :             :                            "activated",
     130                 :             :                            G_CALLBACK (on_store_selected),
     131                 :             :                            self, 0);
     132                 :             : 
     133                 :             :   /* Check */
     134                 :           1 :   settings = valent_device_preferences_group_get_settings (group);
     135                 :           1 :   local_uid = g_settings_get_string (settings, "local-uid");
     136                 :           1 :   image = g_object_new (GTK_TYPE_IMAGE,
     137                 :             :                         "icon-name", "object-select-symbolic",
     138                 :             :                         "icon-size", GTK_ICON_SIZE_NORMAL,
     139                 :           1 :                         "visible",   (g_strcmp0 (local_uid, uid) == 0),
     140                 :             :                         NULL);
     141                 :           1 :   adw_action_row_add_suffix (ADW_ACTION_ROW (row), image);
     142                 :           1 :   g_object_set_data (G_OBJECT (row), "select", image);
     143                 :             : 
     144                 :           1 :   g_object_bind_property (store, "name",
     145                 :             :                           row,   "title",
     146                 :             :                           G_BINDING_SYNC_CREATE);
     147                 :             : 
     148                 :           1 :   return row;
     149                 :             : }
     150                 :             : 
     151                 :             : static GtkListBox *
     152                 :           1 : _adw_expander_row_get_list (AdwExpanderRow *row)
     153                 :             : {
     154                 :           1 :   GtkWidget *box;
     155                 :           1 :   GtkWidget *child;
     156                 :             : 
     157                 :             :   /* First child is a GtkBox */
     158                 :           1 :   box = gtk_widget_get_first_child (GTK_WIDGET (row));
     159                 :             : 
     160                 :             :   /* The GtkBox contains the primary AdwActionRow and a GtkRevealer */
     161                 :           1 :   for (child = gtk_widget_get_first_child (box);
     162         [ +  - ]:           2 :        child != NULL;
     163                 :           1 :        child = gtk_widget_get_next_sibling (child))
     164                 :             :     {
     165   [ +  -  +  +  :           2 :       if (GTK_IS_REVEALER (child))
                   +  - ]
     166                 :             :         break;
     167                 :             :     }
     168                 :             : 
     169                 :             :   /* The GtkRevealer's child is the GtkListBox */
     170   [ +  -  +  -  :           1 :   if (GTK_IS_REVEALER (child))
             -  +  -  - ]
     171                 :           1 :     return GTK_LIST_BOX (gtk_revealer_get_child (GTK_REVEALER (child)));
     172                 :             : 
     173                 :             :   return NULL;
     174                 :             : }
     175                 :             : 
     176                 :             : 
     177                 :             : /*
     178                 :             :  * GObject
     179                 :             :  */
     180                 :             : static void
     181                 :           1 : valent_contacts_preferences_constructed (GObject *object)
     182                 :             : {
     183                 :           1 :   ValentContactsPreferences *self = VALENT_CONTACTS_PREFERENCES (object);
     184                 :           1 :   ValentDevicePreferencesGroup *group = VALENT_DEVICE_PREFERENCES_GROUP (self);
     185                 :           1 :   GSettings *settings;
     186                 :           1 :   ValentContacts *contacts;
     187                 :             : 
     188                 :           1 :   settings = valent_device_preferences_group_get_settings (group);
     189                 :           1 :   g_settings_bind (settings,          "remote-sync",
     190                 :           1 :                    self->remote_sync, "active",
     191                 :             :                    G_SETTINGS_BIND_DEFAULT);
     192                 :           1 :   g_settings_bind (settings,         "local-sync",
     193                 :           1 :                    self->local_sync, "enable-expansion",
     194                 :             :                    G_SETTINGS_BIND_DEFAULT);
     195                 :             : 
     196                 :           1 :   contacts = valent_contacts_get_default ();
     197                 :           1 :   gtk_list_box_bind_model (self->local_list,
     198                 :             :                            G_LIST_MODEL (contacts),
     199                 :             :                            valent_contacts_preferences_create_row_func,
     200                 :             :                            self, NULL);
     201                 :             : 
     202                 :           1 :   G_OBJECT_CLASS (valent_contacts_preferences_parent_class)->constructed (object);
     203                 :           1 : }
     204                 :             : 
     205                 :             : static void
     206                 :           1 : valent_contacts_preferences_dispose (GObject *object)
     207                 :             : {
     208                 :           1 :   GtkWidget *widget = GTK_WIDGET (object);
     209                 :             : 
     210                 :           1 :   gtk_widget_dispose_template (widget, VALENT_TYPE_CONTACTS_PREFERENCES);
     211                 :             : 
     212                 :           1 :   G_OBJECT_CLASS (valent_contacts_preferences_parent_class)->dispose (object);
     213                 :           1 : }
     214                 :             : 
     215                 :             : static void
     216                 :           1 : valent_contacts_preferences_finalize (GObject *object)
     217                 :             : {
     218                 :           1 :   ValentContactsPreferences *self = VALENT_CONTACTS_PREFERENCES (object);
     219                 :             : 
     220         [ +  - ]:           1 :   g_clear_pointer (&self->local_stores, g_hash_table_unref);
     221                 :             : 
     222                 :           1 :   G_OBJECT_CLASS (valent_contacts_preferences_parent_class)->finalize (object);
     223                 :           1 : }
     224                 :             : 
     225                 :             : static void
     226                 :           2 : valent_contacts_preferences_class_init (ValentContactsPreferencesClass *klass)
     227                 :             : {
     228                 :           2 :   GObjectClass *object_class = G_OBJECT_CLASS (klass);
     229                 :           2 :   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
     230                 :             : 
     231                 :           2 :   object_class->constructed = valent_contacts_preferences_constructed;
     232                 :           2 :   object_class->dispose = valent_contacts_preferences_dispose;
     233                 :           2 :   object_class->finalize = valent_contacts_preferences_finalize;
     234                 :             : 
     235                 :           2 :   gtk_widget_class_set_template_from_resource (widget_class, "/plugins/contacts/valent-contacts-preferences.ui");
     236                 :           2 :   gtk_widget_class_bind_template_child (widget_class, ValentContactsPreferences, local_sync);
     237                 :           2 :   gtk_widget_class_bind_template_child (widget_class, ValentContactsPreferences, remote_sync);
     238                 :             : 
     239                 :           2 :   gtk_widget_class_bind_template_callback (widget_class, on_local_sync);
     240                 :           2 : }
     241                 :             : 
     242                 :             : static void
     243                 :           1 : valent_contacts_preferences_init (ValentContactsPreferences *self)
     244                 :             : {
     245                 :           1 :   gtk_widget_init_template (GTK_WIDGET (self));
     246                 :             : 
     247                 :           1 :   self->local_list = _adw_expander_row_get_list (self->local_sync);
     248                 :           1 :   self->local_stores = g_hash_table_new (NULL, NULL);
     249                 :           1 : }
     250                 :             : 
        

Generated by: LCOV version 2.0-1