LCOV - code coverage report
Current view: top level - src/libvalent/contacts - valent-contacts-adapter.c (source / functions) Coverage Total Hit
Test: Code Coverage Lines: 92.2 % 51 47
Test Date: 2024-04-23 06:02:46 Functions: 90.9 % 11 10
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-contacts-adapter"
       5                 :             : 
       6                 :             : #include "config.h"
       7                 :             : 
       8                 :             : #include <gio/gio.h>
       9                 :             : #include <libvalent-core.h>
      10                 :             : 
      11                 :             : #include "valent-contact-store.h"
      12                 :             : #include "valent-contacts-adapter.h"
      13                 :             : 
      14                 :             : 
      15                 :             : /**
      16                 :             :  * ValentContactsAdapter:
      17                 :             :  *
      18                 :             :  * An abstract base class for address book providers.
      19                 :             :  *
      20                 :             :  * `ValentContactsAdapter` is a base class for plugins that provide an
      21                 :             :  * interface to manage address books. This usually means monitoring and
      22                 :             :  * querying [class@Valent.ContactStore] instances.
      23                 :             :  *
      24                 :             :  * ## `.plugin` File
      25                 :             :  *
      26                 :             :  * Implementations may define the following extra fields in the `.plugin` file:
      27                 :             :  *
      28                 :             :  * - `X-ContactsAdapterPriority`
      29                 :             :  *
      30                 :             :  *     An integer indicating the adapter priority. The implementation with the
      31                 :             :  *     lowest value will be used as the primary adapter.
      32                 :             :  *
      33                 :             :  * Since: 1.0
      34                 :             :  */
      35                 :             : 
      36                 :             : typedef struct
      37                 :             : {
      38                 :             :   GPtrArray      *stores;
      39                 :             : } ValentContactsAdapterPrivate;
      40                 :             : 
      41                 :             : static void   g_list_model_iface_init (GListModelInterface *iface);
      42                 :             : 
      43   [ +  +  +  - ]:         510 : G_DEFINE_ABSTRACT_TYPE_WITH_CODE (ValentContactsAdapter, valent_contacts_adapter, VALENT_TYPE_EXTENSION,
      44                 :             :                                   G_ADD_PRIVATE (ValentContactsAdapter)
      45                 :             :                                   G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, g_list_model_iface_init))
      46                 :             : 
      47                 :             : /**
      48                 :             :  * ValentContactsAdapterClass:
      49                 :             :  *
      50                 :             :  * The virtual function table for `ValentContactsAdapter`.
      51                 :             :  */
      52                 :             : 
      53                 :             : 
      54                 :             : /*
      55                 :             :  * GListModel
      56                 :             :  */
      57                 :             : static gpointer
      58                 :          12 : valent_contacts_adapter_get_item (GListModel   *list,
      59                 :             :                                   unsigned int  position)
      60                 :             : {
      61                 :          12 :   ValentContactsAdapter *self = VALENT_CONTACTS_ADAPTER (list);
      62                 :          12 :   ValentContactsAdapterPrivate *priv = valent_contacts_adapter_get_instance_private (self);
      63                 :             : 
      64         [ +  - ]:          12 :   g_assert (VALENT_IS_CONTACTS_ADAPTER (self));
      65                 :             : 
      66         [ +  - ]:          12 :   if G_UNLIKELY (position >= priv->stores->len)
      67                 :             :     return NULL;
      68                 :             : 
      69                 :          12 :   return g_object_ref (g_ptr_array_index (priv->stores, position));
      70                 :             : }
      71                 :             : 
      72                 :             : static GType
      73                 :           0 : valent_contacts_adapter_get_item_type (GListModel *list)
      74                 :             : {
      75                 :           0 :   return VALENT_TYPE_CONTACTS_ADAPTER;
      76                 :             : }
      77                 :             : 
      78                 :             : static unsigned int
      79                 :          14 : valent_contacts_adapter_get_n_items (GListModel *list)
      80                 :             : {
      81                 :          14 :   ValentContactsAdapter *self = VALENT_CONTACTS_ADAPTER (list);
      82                 :          14 :   ValentContactsAdapterPrivate *priv = valent_contacts_adapter_get_instance_private (self);
      83                 :             : 
      84         [ +  - ]:          14 :   g_assert (VALENT_IS_CONTACTS_ADAPTER (self));
      85                 :             : 
      86                 :          14 :   return priv->stores->len;
      87                 :             : }
      88                 :             : 
      89                 :             : static void
      90                 :          66 : g_list_model_iface_init (GListModelInterface *iface)
      91                 :             : {
      92                 :          66 :   iface->get_item = valent_contacts_adapter_get_item;
      93                 :          66 :   iface->get_item_type = valent_contacts_adapter_get_item_type;
      94                 :          66 :   iface->get_n_items = valent_contacts_adapter_get_n_items;
      95                 :          66 : }
      96                 :             : 
      97                 :             : /*
      98                 :             :  * GObject
      99                 :             :  */
     100                 :             : static void
     101                 :           3 : valent_contacts_adapter_finalize (GObject *object)
     102                 :             : {
     103                 :           3 :   ValentContactsAdapter *self = VALENT_CONTACTS_ADAPTER (object);
     104                 :           3 :   ValentContactsAdapterPrivate *priv = valent_contacts_adapter_get_instance_private (self);
     105                 :             : 
     106         [ +  - ]:           3 :   g_clear_pointer (&priv->stores, g_ptr_array_unref);
     107                 :             : 
     108                 :           3 :   G_OBJECT_CLASS (valent_contacts_adapter_parent_class)->finalize (object);
     109                 :           3 : }
     110                 :             : 
     111                 :             : static void
     112                 :          66 : valent_contacts_adapter_class_init (ValentContactsAdapterClass *klass)
     113                 :             : {
     114                 :          66 :   GObjectClass *object_class = G_OBJECT_CLASS (klass);
     115                 :             : 
     116                 :          66 :   object_class->finalize = valent_contacts_adapter_finalize;
     117                 :             : }
     118                 :             : 
     119                 :             : static void
     120                 :           9 : valent_contacts_adapter_init (ValentContactsAdapter *adapter)
     121                 :             : {
     122                 :           9 :   ValentContactsAdapterPrivate *priv = valent_contacts_adapter_get_instance_private (adapter);
     123                 :             : 
     124                 :           9 :   priv->stores = g_ptr_array_new_with_free_func (g_object_unref);
     125                 :           9 : }
     126                 :             : 
     127                 :             : /**
     128                 :             :  * valent_contacts_adapter_store_added:
     129                 :             :  * @adapter: a `ValentContactsAdapter`
     130                 :             :  * @store: a `ValentContactStore`
     131                 :             :  *
     132                 :             :  * Called when @store has been added to @adapter.
     133                 :             :  *
     134                 :             :  * This method should only be called by implementations of
     135                 :             :  * [class@Valent.ContactsAdapter]. @adapter will hold a reference on @store and
     136                 :             :  * emit [signal@Gio.ListModel::items-changed].
     137                 :             :  *
     138                 :             :  * Since: 1.0
     139                 :             :  */
     140                 :             : void
     141                 :          11 : valent_contacts_adapter_store_added (ValentContactsAdapter *adapter,
     142                 :             :                                      ValentContactStore    *store)
     143                 :             : {
     144                 :          11 :   ValentContactsAdapterPrivate *priv = valent_contacts_adapter_get_instance_private (adapter);
     145                 :          11 :   unsigned int position = 0;
     146                 :             : 
     147         [ +  - ]:          11 :   g_return_if_fail (VALENT_IS_CONTACTS_ADAPTER (adapter));
     148         [ -  + ]:          11 :   g_return_if_fail (VALENT_IS_CONTACT_STORE (store));
     149                 :             : 
     150                 :          11 :   position = priv->stores->len;
     151                 :          11 :   g_ptr_array_add (priv->stores, g_object_ref (store));
     152                 :          11 :   g_list_model_items_changed (G_LIST_MODEL (adapter), position, 0, 1);
     153                 :             : }
     154                 :             : 
     155                 :             : /**
     156                 :             :  * valent_contacts_adapter_store_removed:
     157                 :             :  * @adapter: a `ValentContactsAdapter`
     158                 :             :  * @store: a `ValentContactStore`
     159                 :             :  *
     160                 :             :  * Called when @store has been removed from @adapter.
     161                 :             :  *
     162                 :             :  * This method should only be called by implementations of
     163                 :             :  * [class@Valent.ContactsAdapter]. @adapter will drop its reference on @store
     164                 :             :  * and emit [signal@Gio.ListModel::items-changed].
     165                 :             :  *
     166                 :             :  * Since: 1.0
     167                 :             :  */
     168                 :             : void
     169                 :           2 : valent_contacts_adapter_store_removed (ValentContactsAdapter *adapter,
     170                 :             :                                        ValentContactStore    *store)
     171                 :             : {
     172                 :           2 :   ValentContactsAdapterPrivate *priv = valent_contacts_adapter_get_instance_private (adapter);
     173                 :           2 :   g_autoptr (ValentContactStore) item = NULL;
     174                 :           2 :   unsigned int position = 0;
     175                 :             : 
     176         [ +  - ]:           2 :   g_return_if_fail (VALENT_IS_CONTACTS_ADAPTER (adapter));
     177         [ -  + ]:           2 :   g_return_if_fail (VALENT_IS_CONTACT_STORE (store));
     178                 :             : 
     179         [ -  + ]:           2 :   if (!g_ptr_array_find (priv->stores, store, &position))
     180                 :             :     {
     181                 :           0 :       g_warning ("No such store \"%s\" found in \"%s\"",
     182                 :             :                  G_OBJECT_TYPE_NAME (store),
     183                 :             :                  G_OBJECT_TYPE_NAME (adapter));
     184                 :           0 :       return;
     185                 :             :     }
     186                 :             : 
     187                 :           2 :   item = g_ptr_array_steal_index (priv->stores, position);
     188         [ +  - ]:           2 :   g_list_model_items_changed (G_LIST_MODEL (adapter), position, 1, 0);
     189                 :             : }
     190                 :             : 
        

Generated by: LCOV version 2.0-1