calls-enumerate-params.c 5.96 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
/*
 * Copyright (C) 2018 Purism SPC
 *
 * This file is part of Calls.
 *
 * Calls is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Calls is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Calls.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Bob Ham <bob.ham@puri.sm>
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 */

#include "calls-enumerate-params.h"
#include "calls-provider.h"
#include "calls-origin.h"
#include "calls-call.h"

#include <glib/gi18n.h>
#include <glib-object.h>


typedef enum
{
  CALLS_ENUMERATE_PROVIDER,
  CALLS_ENUMERATE_ORIGIN,
  CALLS_ENUMERATE_CALL,
  CALLS_ENUMERATE_LAST
} CallsEnumerateObjectType;


struct _CallsEnumerateParams
{
  GObject parent_instance;

  gboolean    enumerating;
  gpointer    user_data;
  GHashTable *callbacks[3];
};

G_DEFINE_TYPE (CallsEnumerateParams, calls_enumerate_params, G_TYPE_OBJECT);

enum {
  PROP_0,
  PROP_USER_DATA,
  PROP_LAST_PROP,
};
static GParamSpec *props[PROP_LAST_PROP];


static void
calls_enumerate_params_init (CallsEnumerateParams *self)
{
  unsigned i;

  self->enumerating = TRUE;

  for (i = 0; i < CALLS_ENUMERATE_LAST; ++i)
    {
      self->callbacks[i] =
        g_hash_table_new_full (g_str_hash, g_str_equal,
                               g_free, NULL);
    }
}


static void
set_property (GObject      *object,
              guint         property_id,
              const GValue *value,
              GParamSpec   *pspec)
{
  CallsEnumerateParams *self = CALLS_ENUMERATE_PARAMS (object);

  switch (property_id) {
  case PROP_USER_DATA:
    self->user_data = g_value_get_pointer (value);
    break;

  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    break;
  }
}


static void
finalize (GObject *object)
{
  CallsEnumerateParams *self = CALLS_ENUMERATE_PARAMS (object);

  unsigned i;
  for (i = 0; i < CALLS_ENUMERATE_LAST; ++i)
    {
      g_hash_table_unref (self->callbacks[i]);
    }

109
  G_OBJECT_CLASS (calls_enumerate_params_parent_class)->finalize (object);
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
}


static void
calls_enumerate_params_class_init (CallsEnumerateParamsClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->set_property = set_property;
  object_class->finalize = finalize;

  props[PROP_USER_DATA] =
    g_param_spec_pointer ("user-data",
                          _("User data"),
                          _("The pointer to be provided as user data for signal connections"),
                          G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY);

  g_object_class_install_properties (object_class, PROP_LAST_PROP, props);
}


CallsEnumerateParams *
calls_enumerate_params_new (gpointer user_data)
{
  return g_object_new (CALLS_TYPE_ENUMERATE_PARAMS,
                       "user-data", user_data,
                       NULL);
}


gpointer
calls_enumerate_params_get_user_data (CallsEnumerateParams *self)
{
  return self->user_data;
}


gboolean
calls_enumerate_params_get_enumerating (CallsEnumerateParams *self)
{
  return self->enumerating;
}


void
calls_enumerate_params_set_enumerating (CallsEnumerateParams *self,
                                        gboolean              enumerating)
{
  self->enumerating = enumerating;
}


static GHashTable *
lookup_callbacks (CallsEnumerateParams *self,
                  GType                 obj_type)
{
  const GType obj_gtypes[3] =
    {
      CALLS_TYPE_PROVIDER,
      CALLS_TYPE_ORIGIN,
      CALLS_TYPE_CALL
    };
  unsigned i;

  for (i = 0; i < CALLS_ENUMERATE_LAST; ++i)
    {
      if (g_type_is_a (obj_type, obj_gtypes[i]))
        {
          return self->callbacks[i];
        }
    }

  g_error ("Unknown GType `%s' converting to Provider enumeration object type",
           g_type_name (obj_type));
}


gboolean
calls_enumerate_params_have_callbacks (CallsEnumerateParams *self,
                                       GType                 obj_type)
{
  GHashTable * const callbacks = lookup_callbacks (self, obj_type);
  return g_hash_table_size (callbacks) > 0;
}


gboolean
calls_enumerate_params_add (CallsEnumerateParams *self,
                            GType                 obj_type,
                            const gchar          *detail,
                            GCallback             callback)
{
  GHashTable * const callbacks = lookup_callbacks (self, obj_type);
  return g_hash_table_insert (callbacks, g_strdup (detail), callback);
}


struct _CallsEnumerateConnectData
{
  gpointer instance;
  gpointer user_data;
};
typedef struct _CallsEnumerateConnectData CallsEnumerateConnectData;


static void
callbacks_connect (const gchar               *detail,
                   GCallback                  callback,
                   CallsEnumerateConnectData *data)
{
  g_signal_connect_swapped (data->instance,
                            detail,
                            callback,
                            data->user_data);
}


void
calls_enumerate_params_connect (CallsEnumerateParams *self,
                                GObject              *object)
{
  GHashTable * const callbacks =
    lookup_callbacks (self, G_TYPE_FROM_INSTANCE (object));
  CallsEnumerateConnectData data;

  data.instance  = object;
  data.user_data = self->user_data;

  g_hash_table_foreach (callbacks,
                        (GHFunc)callbacks_connect,
                        &data);
}


GCallback
calls_enumerate_params_lookup  (CallsEnumerateParams *self,
                                GType                 obj_type,
                                const gchar          *detail)
{
  GHashTable * const callbacks = lookup_callbacks (self, obj_type);
  return g_hash_table_lookup (callbacks, detail);
}