calls-enumerate.c 6.14 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 109 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
/*
 * 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.h"
#include "calls-origin.h"
#include "calls-call.h"

#include <glib/gi18n.h>



typedef void (*CallAddedCallback) (gpointer     user_data,
                                   CallsCall   *call,
                                   CallsOrigin *origin);

static void
enum_call_added_cb (CallsOrigin          *origin,
                    CallsCall            *call,
                    CallsEnumerateParams *params)
{
  // Call call-added signal
  if (calls_enumerate_params_get_enumerating (params))
    {
      CallAddedCallback call_added_cb;

      call_added_cb = (CallAddedCallback)
        calls_enumerate_params_lookup (params,
                                       CALLS_TYPE_ORIGIN,
                                       "call-added");
      if (call_added_cb)
        {
          gpointer user_data = calls_enumerate_params_get_user_data (params);
          call_added_cb (user_data, call, origin);
        }
    }

  // Connect user's callbacks
  calls_enumerate_params_connect (params, G_OBJECT (call));
}


static void
enum_origin_calls (CallsOrigin          *origin,
                   CallsEnumerateParams *params)
{
  GList *calls, *node;

  calls = calls_origin_get_calls (origin);

  for (node = calls; node; node = node->next)
    {
      enum_call_added_cb (origin,
                          CALLS_CALL (node->data),
                          params);
    }

  g_list_free (calls);
}


typedef void (*OriginAddedCallback) (gpointer       user_data,
                                     CallsOrigin   *origin,
                                     CallsProvider *provider);

static void
enum_origin_added_cb (CallsProvider        *provider,
                      CallsOrigin          *origin,
                      CallsEnumerateParams *params)
{
  gboolean add_callback;

  // Call origin-added signal
  if (calls_enumerate_params_get_enumerating (params))
    {
      OriginAddedCallback origin_added_cb;

      origin_added_cb = (OriginAddedCallback)
        calls_enumerate_params_lookup (params,
                                           CALLS_TYPE_PROVIDER,
                                           "origin-added");
      if (origin_added_cb)
        {
          gpointer user_data = calls_enumerate_params_get_user_data (params);
          origin_added_cb (user_data, origin, provider);
        }
    }

  // Connect user's callbacks
  calls_enumerate_params_connect (params, G_OBJECT (origin));

  // We add a callback for ourselves if we have to set callbacks on
  // anything lower in the hierarchy in future
  add_callback =
    calls_enumerate_params_have_callbacks (params, CALLS_TYPE_CALL);

  if (add_callback)
    {
      g_object_ref (params);
      g_signal_connect_data (origin,
                             "call-added",
                             G_CALLBACK (enum_call_added_cb),
                             params,
                             (GClosureNotify)g_object_unref,
                             0);

    }

  // We enumerate if we've added callbacks and if there's the specific
  // "call-added" callback for this level
  if (add_callback
      ||
      calls_enumerate_params_lookup (params, CALLS_TYPE_ORIGIN,
                                     "call-added"))
    {
      enum_origin_calls (origin, params);
    }
}


static void
enum_provider_origins (CallsProvider        *provider,
                       CallsEnumerateParams *params)
{
  GList *origins, *node;

  origins = calls_provider_get_origins (provider);

  for (node = origins; node; node = node->next)
    {
      enum_origin_added_cb (provider,
                            CALLS_ORIGIN (node->data),
                            params);
    }

  g_list_free (origins);
}


/**
 * calls_enumerateerate:
 * @provider: a #CallsProvider
 * @params: a #CallsEnumerateParams containing callbacks and state
 *
 * Enumerate all of the #CallsOrigin objects in the #CallsProvider and
 * then all of the #CallsCall objects in those #CallsOrigin objects.
 * For any callbacks stored in @params, connect them to the
 * enumerated objects and connect them to any future objects that
 * appear.  Call the "origin-added" callback for a #CallsProvider and
 * "call-added" for a #CallsOrigin if the target objects are
 * enumerated.
 */
void
calls_enumerate (CallsProvider        *provider,
                 CallsEnumerateParams *params)
{
  gboolean add_callback;

  // Connect user's callbacks
  calls_enumerate_params_connect (params, G_OBJECT (provider));

  // We add a callback for ourselves if we have to set callbacks on
  // anything lower in the hierarchy in future
  add_callback =
    calls_enumerate_params_have_callbacks (params, CALLS_TYPE_ORIGIN)
    ||
    calls_enumerate_params_have_callbacks (params, CALLS_TYPE_CALL);

  if (add_callback)
    {
      g_object_ref (params);
      g_signal_connect_data (provider,
                             "origin-added",
                             G_CALLBACK (enum_origin_added_cb),
                             params,
                             (GClosureNotify)g_object_unref,
                             0);

    }

  // We enumerate if we've added callbacks and if there's the specific
  // "origin-added" callback for this level
  if (add_callback ||
      calls_enumerate_params_lookup (params, CALLS_TYPE_PROVIDER,
                                     "origin-added"))
    {
      enum_provider_origins (provider, params);
    }

  calls_enumerate_params_set_enumerating (params, FALSE);
}