gdbusobjectmanagerclient.c 65.6 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
/* GDBus - GLib D-Bus Library
 *
 * Copyright (C) 2008-2010 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: David Zeuthen <davidz@redhat.com>
 */

#include "config.h"

#include "gdbusobjectmanager.h"
#include "gdbusobjectmanagerclient.h"
#include "gdbusobject.h"
#include "gdbusprivate.h"
#include "gio-marshal.h"
#include "gioenumtypes.h"
#include "ginitable.h"
#include "gasyncresult.h"
#include "gsimpleasyncresult.h"
#include "gasyncinitable.h"
#include "gdbusconnection.h"
#include "gdbusutils.h"
#include "gdbusobject.h"
#include "gdbusobjectproxy.h"
#include "gdbusproxy.h"
#include "gdbusinterface.h"

#include "glibintl.h"

/**
 * SECTION:gdbusobjectmanagerclient
 * @short_description: Client-side object manager
 * @include: gio/gio.h
 *
 * #GDBusObjectManagerClient is used to create, monitor and delete object
 * proxies for remote objects exported by a #GDBusObjectManagerServer (or any
 * code implementing the <ulink
 * url="http://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces-objectmanager">org.freedesktop.DBus.ObjectManager</ulink>
 * interface).
 *
 * Once an instance of this type has been created, you can connect to
 * the #GDBusObjectManager::object-added and
 * #GDBusObjectManager::object-removed signals and inspect the
 * #GDBusObjectProxy objects returned by
 * g_dbus_object_manager_get_objects().
 *
 * If the name for a #GDBusObjectManagerClient is not owned by anyone at
 * object construction time, the default behavior is to request the
 * message bus to launch an owner for the name. This behavior can be
 * disabled using the %G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_DO_NOT_AUTO_START
 * flag. It's also worth noting that this only works if the name of
 * interest is activatable in the first place. E.g. in some cases it
 * is not possible to launch an owner for the requested name. In this
 * case, #GDBusObjectManagerClient object construction still succeeds but
 * there will be no object proxies
 * (e.g. g_dbus_object_manager_get_objects() returns the empty list) and
 * the #GDBusObjectManagerClient:name-owner property is %NULL.
 *
 * The owner of the requested name can come and go (for example
 * consider a system service being restarted) – #GDBusObjectManagerClient
 * handles this case too; simply connect to the #GObject::notify
 * signal to watch for changes on the #GDBusObjectManagerClient:name-owner
 * property. When the name owner vanishes, the behavior is that
 * #GDBusObjectManagerClient:name-owner is set to %NULL (this includes
 * emission of the #GObject::notify signal) and then
 * #GDBusObjectManager::object-removed signals are synthesized
 * for all currently existing object proxies. Since
 * #GDBusObjectManagerClient:name-owner is %NULL when this happens, you can
 * use this information to disambiguate a synthesized signal from a
 * genuine signal caused by object removal on the remote
 * #GDBusObjectManager. Similarly, when a new name owner appears,
 * #GDBusObjectManager::object-added signals are synthesized
 * while #GDBusObjectManagerClient:name-owner is still %NULL. Only when all
 * object proxies have been added, the #GDBusObjectManagerClient:name-owner
 * is set to the new name owner (this includes emission of the
 * #GObject::notify signal).  Furthermore, you are guaranteed that
 * #GDBusObjectManagerClient:name-owner will alternate between a name owner
 * (e.g. <literal>:1.42</literal>) and %NULL even in the case where
 * the name of interest is atomically replaced
 *
 * Ultimately, #GDBusObjectManagerClient is used to obtain #GDBusProxy
 * instances. All signals (including the
 * <literal>org.freedesktop.DBus.Properties::PropertiesChanged</literal>
 * signal) delivered to #GDBusProxy instances are guaranteed to
 * originate from the name owner. This guarantee along with the
 * behavior described above, means that certain race conditions
 * including the <emphasis><quote>half the proxy is from the old owner
 * and the other half is from the new owner</quote></emphasis> problem
 * cannot happen.
 *
 * To avoid having the application connect to signals on the returned
 * #GDBusObjectProxy and #GDBusProxy objects, the
 * #GDBusObject::interface-added,
 * #GDBusObject::interface-removed,
 * #GDBusProxy::g-properties-changed and
 * #GDBusProxy::g-signal signals
 * are also emitted on the #GDBusObjectManagerClient instance managing these
 * objects. The signals emitted are
 * #GDBusObjectManager::interface-added,
 * #GDBusObjectManager::interface-removed,
 * #GDBusObjectManagerClient::interface-proxy-properties-changed and
 * #GDBusObjectManagerClient::interface-proxy-signal.
 *
 * Note that all callbacks and signals are emitted in the
 * <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
 * that the #GDBusObjectManagerClient object was constructed
 * in. Additionally, the #GDBusObjectProxy and #GDBusProxy objects
 * originating from the #GDBusObjectManagerClient object will be created in
 * the same context and, consequently, will deliver signals in the
 * same main loop.
 */

struct _GDBusObjectManagerClientPrivate
{
  GBusType bus_type;
  GDBusConnection *connection;
  gchar *object_path;
  gchar *name;
  gchar *name_owner;
  GDBusObjectManagerClientFlags flags;

  GDBusProxy *control_proxy;

  GHashTable *map_object_path_to_object_proxy;

  guint signal_subscription_id;
  gchar *match_rule;

  GDBusProxyTypeFunc get_proxy_type_func;
  gpointer get_proxy_type_user_data;
145
  GDestroyNotify get_proxy_type_destroy_notify;
146 147 148 149 150 151 152 153 154 155 156 157
};

enum
{
  PROP_0,
  PROP_BUS_TYPE,
  PROP_CONNECTION,
  PROP_FLAGS,
  PROP_OBJECT_PATH,
  PROP_NAME,
  PROP_NAME_OWNER,
  PROP_GET_PROXY_TYPE_FUNC,
158 159
  PROP_GET_PROXY_TYPE_USER_DATA,
  PROP_GET_PROXY_TYPE_DESTROY_NOTIFY
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
};

enum
{
  INTERFACE_PROXY_SIGNAL_SIGNAL,
  INTERFACE_PROXY_PROPERTIES_CHANGED_SIGNAL,
  LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

static void initable_iface_init       (GInitableIface *initable_iface);
static void async_initable_iface_init (GAsyncInitableIface *async_initable_iface);
static void dbus_object_manager_interface_init (GDBusObjectManagerIface *iface);

G_DEFINE_TYPE_WITH_CODE (GDBusObjectManagerClient, g_dbus_object_manager_client, G_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
                         G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init)
                         G_IMPLEMENT_INTERFACE (G_TYPE_DBUS_OBJECT_MANAGER, dbus_object_manager_interface_init));

static void maybe_unsubscribe_signals (GDBusObjectManagerClient *manager);

static void on_control_proxy_g_signal (GDBusProxy   *proxy,
                                       const gchar  *sender_name,
                                       const gchar  *signal_name,
                                       GVariant     *parameters,
                                       gpointer      user_data);

static void process_get_all_result (GDBusObjectManagerClient *manager,
                                    GVariant          *value,
                                    const gchar       *name_owner);

static void
g_dbus_object_manager_client_finalize (GObject *object)
{
  GDBusObjectManagerClient *manager = G_DBUS_OBJECT_MANAGER_CLIENT (object);

  maybe_unsubscribe_signals (manager);

  g_hash_table_unref (manager->priv->map_object_path_to_object_proxy);

  if (manager->priv->control_proxy != NULL)
    {
      g_warn_if_fail (g_signal_handlers_disconnect_by_func (manager->priv->control_proxy,
                                                            on_control_proxy_g_signal,
                                                            manager) == 1);
      g_object_unref (manager->priv->control_proxy);
    }
  g_object_unref (manager->priv->connection);
  g_free (manager->priv->object_path);
  g_free (manager->priv->name);
  g_free (manager->priv->name_owner);

213 214 215
  if (manager->priv->get_proxy_type_destroy_notify != NULL)
    manager->priv->get_proxy_type_destroy_notify (manager->priv->get_proxy_type_user_data);

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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
  if (G_OBJECT_CLASS (g_dbus_object_manager_client_parent_class)->finalize != NULL)
    G_OBJECT_CLASS (g_dbus_object_manager_client_parent_class)->finalize (object);
}

static void
g_dbus_object_manager_client_get_property (GObject    *_object,
                                    guint       prop_id,
                                    GValue     *value,
                                    GParamSpec *pspec)
{
  GDBusObjectManagerClient *manager = G_DBUS_OBJECT_MANAGER_CLIENT (_object);

  switch (prop_id)
    {
    case PROP_CONNECTION:
      g_value_set_object (value, g_dbus_object_manager_client_get_connection (manager));
      break;

    case PROP_OBJECT_PATH:
      g_value_set_string (value, g_dbus_object_manager_get_object_path (G_DBUS_OBJECT_MANAGER (manager)));
      break;

    case PROP_NAME:
      g_value_set_string (value, g_dbus_object_manager_client_get_name (manager));
      break;

    case PROP_FLAGS:
      g_value_set_flags (value, g_dbus_object_manager_client_get_flags (manager));
      break;

    case PROP_NAME_OWNER:
      g_value_take_string (value, g_dbus_object_manager_client_get_name_owner (manager));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (_object, prop_id, pspec);
      break;
    }
}

static void
g_dbus_object_manager_client_set_property (GObject       *_object,
                                    guint          prop_id,
                                    const GValue  *value,
                                    GParamSpec    *pspec)
{
  GDBusObjectManagerClient *manager = G_DBUS_OBJECT_MANAGER_CLIENT (_object);

  switch (prop_id)
    {
    case PROP_BUS_TYPE:
      manager->priv->bus_type = g_value_get_enum (value);
      break;

    case PROP_CONNECTION:
      if (g_value_get_object (value) != NULL)
        {
          g_assert (manager->priv->connection == NULL);
          g_assert (G_IS_DBUS_CONNECTION (g_value_get_object (value)));
          manager->priv->connection = g_value_dup_object (value);
        }
      break;

    case PROP_OBJECT_PATH:
      g_assert (manager->priv->object_path == NULL);
      g_assert (g_variant_is_object_path (g_value_get_string (value)));
      manager->priv->object_path = g_value_dup_string (value);
      break;

    case PROP_NAME:
      g_assert (manager->priv->name == NULL);
      g_assert (g_dbus_is_name (g_value_get_string (value)));
      manager->priv->name = g_value_dup_string (value);
      break;

    case PROP_FLAGS:
      manager->priv->flags = g_value_get_flags (value);
      break;

    case PROP_GET_PROXY_TYPE_FUNC:
      manager->priv->get_proxy_type_func = g_value_get_pointer (value);
      break;

    case PROP_GET_PROXY_TYPE_USER_DATA:
      manager->priv->get_proxy_type_user_data = g_value_get_pointer (value);
      break;

303 304 305 306
    case PROP_GET_PROXY_TYPE_DESTROY_NOTIFY:
      manager->priv->get_proxy_type_destroy_notify = g_value_get_pointer (value);
      break;

307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (_object, prop_id, pspec);
      break;
    }
}

static void
g_dbus_object_manager_client_class_init (GDBusObjectManagerClientClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->finalize     = g_dbus_object_manager_client_finalize;
  gobject_class->set_property = g_dbus_object_manager_client_set_property;
  gobject_class->get_property = g_dbus_object_manager_client_get_property;

  /**
   * GDBusObjectManagerClient:connection:
   *
   * The #GDBusConnection to use.
326 327
   *
   * Since: 2.30
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
   */
  g_object_class_install_property (gobject_class,
                                   PROP_CONNECTION,
                                   g_param_spec_object ("connection",
                                                        "Connection",
                                                        "The connection to use",
                                                        G_TYPE_DBUS_CONNECTION,
                                                        G_PARAM_READABLE |
                                                        G_PARAM_WRITABLE |
                                                        G_PARAM_CONSTRUCT_ONLY |
                                                        G_PARAM_STATIC_STRINGS));

  /**
   * GDBusObjectManagerClient:bus-type:
   *
   * If this property is not %G_BUS_TYPE_NONE, then
   * #GDBusObjectManagerClient:connection must be %NULL and will be set to the
   * #GDBusConnection obtained by calling g_bus_get() with the value
   * of this property.
347 348
   *
   * Since: 2.30
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
   */
  g_object_class_install_property (gobject_class,
                                   PROP_BUS_TYPE,
                                   g_param_spec_enum ("bus-type",
                                                      "Bus Type",
                                                      "The bus to connect to, if any",
                                                      G_TYPE_BUS_TYPE,
                                                      G_BUS_TYPE_NONE,
                                                      G_PARAM_WRITABLE |
                                                      G_PARAM_CONSTRUCT_ONLY |
                                                      G_PARAM_STATIC_NAME |
                                                      G_PARAM_STATIC_BLURB |
                                                      G_PARAM_STATIC_NICK));

  /**
   * GDBusObjectManagerClient:flags:
   *
   * Flags from the #GDBusObjectManagerClientFlags enumeration.
367 368
   *
   * Since: 2.30
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
   */
  g_object_class_install_property (gobject_class,
                                   PROP_FLAGS,
                                   g_param_spec_flags ("flags",
                                                       "Flags",
                                                       "Flags for the proxy manager",
                                                       G_TYPE_DBUS_OBJECT_MANAGER_CLIENT_FLAGS,
                                                       G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,
                                                       G_PARAM_READABLE |
                                                       G_PARAM_WRITABLE |
                                                       G_PARAM_CONSTRUCT_ONLY |
                                                       G_PARAM_STATIC_NAME |
                                                       G_PARAM_STATIC_BLURB |
                                                       G_PARAM_STATIC_NICK));

  /**
   * GDBusObjectManagerClient:object-path:
   *
   * The object path the manager is for.
388 389
   *
   * Since: 2.30
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
   */
  g_object_class_install_property (gobject_class,
                                   PROP_OBJECT_PATH,
                                   g_param_spec_string ("object-path",
                                                        "Object Path",
                                                        "The object path of the control object",
                                                        NULL,
                                                        G_PARAM_READABLE |
                                                        G_PARAM_WRITABLE |
                                                        G_PARAM_CONSTRUCT_ONLY |
                                                        G_PARAM_STATIC_STRINGS));

  /**
   * GDBusObjectManagerClient:name:
   *
   * The well-known name or unique name that the manager is for.
406 407
   *
   * Since: 2.30
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
   */
  g_object_class_install_property (gobject_class,
                                   PROP_NAME,
                                   g_param_spec_string ("name",
                                                        "Name",
                                                        "Name that the manager is for",
                                                        NULL,
                                                        G_PARAM_READABLE |
                                                        G_PARAM_WRITABLE |
                                                        G_PARAM_CONSTRUCT_ONLY |
                                                        G_PARAM_STATIC_STRINGS));

  /**
   * GDBusObjectManagerClient:name-owner:
   *
   * The unique name that owns #GDBusObjectManagerClient:name or %NULL if
   * no-one is currently owning the name. Connect to the
   * #GObject::notify signal to track changes to this property.
426 427
   *
   * Since: 2.30
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
   */
  g_object_class_install_property (gobject_class,
                                   PROP_NAME_OWNER,
                                   g_param_spec_string ("name-owner",
                                                        "Name Owner",
                                                        "The owner of the name we are watching",
                                                        NULL,
                                                        G_PARAM_READABLE |
                                                        G_PARAM_STATIC_STRINGS));

  /**
   * GDBusObjectManagerClient:get-proxy-type-func:
   *
   * The #GDBusProxyTypeFunc to use when determining what #GType to
   * use for interface proxies or %NULL.
443 444
   *
   * Since: 2.30
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
   */
  g_object_class_install_property (gobject_class,
                                   PROP_GET_PROXY_TYPE_FUNC,
                                   g_param_spec_pointer ("get-proxy-type-func",
                                                         "GDBusProxyTypeFunc Function Pointer",
                                                         "The GDBusProxyTypeFunc pointer to use",
                                                         G_PARAM_READABLE |
                                                         G_PARAM_WRITABLE |
                                                         G_PARAM_CONSTRUCT_ONLY |
                                                         G_PARAM_STATIC_STRINGS));

  /**
   * GDBusObjectManagerClient:get-proxy-type-user-data:
   *
   * The #gpointer user_data to pass to #GDBusObjectManagerClient:get-proxy-type-func.
460 461
   *
   * Since: 2.30
462 463 464 465 466 467 468 469 470 471 472
   */
  g_object_class_install_property (gobject_class,
                                   PROP_GET_PROXY_TYPE_USER_DATA,
                                   g_param_spec_pointer ("get-proxy-type-user-data",
                                                         "GDBusProxyTypeFunc User Data",
                                                         "The GDBusProxyTypeFunc user_data",
                                                         G_PARAM_READABLE |
                                                         G_PARAM_WRITABLE |
                                                         G_PARAM_CONSTRUCT_ONLY |
                                                         G_PARAM_STATIC_STRINGS));

473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
  /**
   * GDBusObjectManagerClient:get-proxy-type-destroy-notify:
   *
   * A #GDestroyNotify for the #gpointer user_data in #GDBusObjectManagerClient:get-proxy-type-user-data.
   *
   * Since: 2.30
   */
  g_object_class_install_property (gobject_class,
                                   PROP_GET_PROXY_TYPE_DESTROY_NOTIFY,
                                   g_param_spec_pointer ("get-proxy-type-destroy-notify",
                                                         "GDBusProxyTypeFunc user data free function",
                                                         "The GDBusProxyTypeFunc user data free function",
                                                         G_PARAM_READABLE |
                                                         G_PARAM_WRITABLE |
                                                         G_PARAM_CONSTRUCT_ONLY |
                                                         G_PARAM_STATIC_STRINGS));

490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
  /**
   * GDBusObjectManagerClient::interface-proxy-signal:
   * @manager: The #GDBusObjectManagerClient emitting the signal.
   * @object_proxy: The #GDBusObjectProxy on which an interface is emitting a D-Bus signal.
   * @interface_proxy: The #GDBusProxy that is emitting a D-Bus signal.
   * @sender_name: The sender of the signal or NULL if the connection is not a bus connection.
   * @signal_name: The signal name.
   * @parameters: A #GVariant tuple with parameters for the signal.
   *
   * Emitted when a D-Bus signal is received on @interface_proxy.
   *
   * This signal exists purely as a convenience to avoid having to
   * connect signals to all interface proxies managed by @manager.
   *
   * This signal is emitted in the
   * <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
   * that @manager was constructed in.
507 508
   *
   * Since: 2.30
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
   */
  signals[INTERFACE_PROXY_SIGNAL_SIGNAL] =
    g_signal_new ("interface-proxy-signal",
                  G_TYPE_DBUS_OBJECT_MANAGER_CLIENT,
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GDBusObjectManagerClientClass, interface_proxy_signal),
                  NULL,
                  NULL,
                  _gio_marshal_VOID__OBJECT_OBJECT_STRING_STRING_VARIANT,
                  G_TYPE_NONE,
                  5,
                  G_TYPE_DBUS_OBJECT_PROXY,
                  G_TYPE_DBUS_PROXY,
                  G_TYPE_STRING,
                  G_TYPE_STRING,
                  G_TYPE_VARIANT);

  /**
   * GDBusObjectManagerClient::interface-proxy-properties-changed:
   * @manager: The #GDBusObjectManagerClient emitting the signal.
   * @object_proxy: The #GDBusObjectProxy on which an interface has properties that are changing.
   * @interface_proxy: The #GDBusProxy that has properties that are changing.
   * @changed_properties: A #GVariant containing the properties that changed.
   * @invalidated_properties: A %NULL terminated array of properties that was invalidated.
   *
   * Emitted when one or more D-Bus properties on proxy changes. The
   * local cache has already been updated when this signal fires. Note
   * that both @changed_properties and @invalidated_properties are
   * guaranteed to never be %NULL (either may be empty though).
   *
   * This signal exists purely as a convenience to avoid having to
   * connect signals to all interface proxies managed by @manager.
   *
   * This signal is emitted in the
   * <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
   * that @manager was constructed in.
545 546
   *
   * Since: 2.30
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
   */
  signals[INTERFACE_PROXY_PROPERTIES_CHANGED_SIGNAL] =
    g_signal_new ("interface-proxy-properties-changed",
                  G_TYPE_DBUS_OBJECT_MANAGER_CLIENT,
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GDBusObjectManagerClientClass, interface_proxy_properties_changed),
                  NULL,
                  NULL,
                  _gio_marshal_VOID__OBJECT_OBJECT_VARIANT_BOXED,
                  G_TYPE_NONE,
                  4,
                  G_TYPE_DBUS_OBJECT_PROXY,
                  G_TYPE_DBUS_PROXY,
                  G_TYPE_VARIANT,
                  G_TYPE_STRV);

  g_type_class_add_private (klass, sizeof (GDBusObjectManagerClientPrivate));
}

static void
g_dbus_object_manager_client_init (GDBusObjectManagerClient *manager)
{
  manager->priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
                                               G_TYPE_DBUS_OBJECT_MANAGER_CLIENT,
                                               GDBusObjectManagerClientPrivate);
  manager->priv->map_object_path_to_object_proxy = g_hash_table_new_full (g_str_hash,
                                                                          g_str_equal,
                                                                          g_free,
                                                                          (GDestroyNotify) g_object_unref);
}

/* ---------------------------------------------------------------------------------------------------- */

/**
 * g_dbus_object_manager_client_new_sync:
 * @connection: A #GDBusConnection.
 * @flags: Zero or more flags from the #GDBusObjectManagerClientFlags enumeration.
 * @name: The owner of the control object (unique or well-known name).
 * @object_path: The object path of the control object.
 * @get_proxy_type_func: A #GDBusProxyTypeFunc function or %NULL to always construct #GDBusProxy proxies.
 * @get_proxy_type_user_data: User data to pass to @get_proxy_type_func.
588
 * @get_proxy_type_destroy_notify: (allow-none): Free function for @get_proxy_type_user_data or %NULL.
589 590 591 592 593 594 595 596 597
 * @cancellable: A #GCancellable or %NULL
 * @error: Return location for error or %NULL.
 *
 * Creates a new #GDBusObjectManagerClient object.
 *
 * This is a synchronous failable constructor - the calling thread is
 * blocked until a reply is received. See g_dbus_object_manager_client_new()
 * for the asynchronous version.
 *
598 599 600
 * Returns: (transfer full) (type GDBusObjectManagerClient): A
 *   #GDBusObjectManagerClient object or %NULL if @error is set. Free
 *   with g_object_unref().
601 602
 *
 * Since: 2.30
603 604 605 606 607 608 609 610
 */
GDBusObjectManager *
g_dbus_object_manager_client_new_sync (GDBusConnection               *connection,
                                       GDBusObjectManagerClientFlags  flags,
                                       const gchar                   *name,
                                       const gchar                   *object_path,
                                       GDBusProxyTypeFunc             get_proxy_type_func,
                                       gpointer                       get_proxy_type_user_data,
611
                                       GDestroyNotify                 get_proxy_type_destroy_notify,
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
                                       GCancellable                  *cancellable,
                                       GError                       **error)
{
  GInitable *initable;

  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
  g_return_val_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) ||
                        g_dbus_is_name (name), NULL);
  g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

  initable = g_initable_new (G_TYPE_DBUS_OBJECT_MANAGER_CLIENT,
                             cancellable,
                             error,
                             "connection", connection,
                             "flags", flags,
                             "name", name,
                             "object-path", object_path,
                             "get-proxy-type-func", get_proxy_type_func,
                             "get-proxy-type-user-data", get_proxy_type_user_data,
632
                             "get-proxy-type-destroy-notify", get_proxy_type_destroy_notify,
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
                             NULL);
  if (initable != NULL)
    return G_DBUS_OBJECT_MANAGER (initable);
  else
    return NULL;
}

/**
 * g_dbus_object_manager_client_new:
 * @connection: A #GDBusConnection.
 * @flags: Zero or more flags from the #GDBusObjectManagerClientFlags enumeration.
 * @name: The owner of the control object (unique or well-known name).
 * @object_path: The object path of the control object.
 * @get_proxy_type_func: A #GDBusProxyTypeFunc function or %NULL to always construct #GDBusProxy proxies.
 * @get_proxy_type_user_data: User data to pass to @get_proxy_type_func.
648
 * @get_proxy_type_destroy_notify: (allow-none): Free function for @get_proxy_type_user_data or %NULL.
649 650 651 652 653 654 655 656 657 658 659 660
 * @cancellable: A #GCancellable or %NULL
 * @callback: A #GAsyncReadyCallback to call when the request is satisfied.
 * @user_data: The data to pass to @callback.
 *
 * Asynchronously creates a new #GDBusObjectManagerClient object.
 *
 * This is an asynchronous failable constructor. When the result is
 * ready, @callback will be invoked in the
 * <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
 * of the thread you are calling this method from. You can
 * then call g_dbus_object_manager_client_new_finish() to get the result. See
 * g_dbus_object_manager_client_new_sync() for the synchronous version.
661 662
 *
 * Since: 2.30
663 664 665 666 667 668 669 670
 */
void
g_dbus_object_manager_client_new (GDBusConnection               *connection,
                                  GDBusObjectManagerClientFlags  flags,
                                  const gchar                   *name,
                                  const gchar                   *object_path,
                                  GDBusProxyTypeFunc             get_proxy_type_func,
                                  gpointer                       get_proxy_type_user_data,
671
                                  GDestroyNotify                 get_proxy_type_destroy_notify,
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
                                  GCancellable                  *cancellable,
                                  GAsyncReadyCallback            callback,
                                  gpointer                       user_data)
{
  g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
  g_return_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) ||
                        g_dbus_is_name (name));
  g_return_if_fail (g_variant_is_object_path (object_path));

  g_async_initable_new_async (G_TYPE_DBUS_OBJECT_MANAGER_CLIENT,
                              G_PRIORITY_DEFAULT,
                              cancellable,
                              callback,
                              user_data,
                              "connection", connection,
                              "flags", flags,
                              "name", name,
                              "object-path", object_path,
                              "get-proxy-type-func", get_proxy_type_func,
                              "get-proxy-type-user-data", get_proxy_type_user_data,
692
                              "get-proxy-type-destroy-notify", get_proxy_type_destroy_notify,
693 694 695 696 697 698 699 700 701 702
                              NULL);
}

/**
 * g_dbus_object_manager_client_new_finish:
 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_object_manager_client_new().
 * @error: Return location for error or %NULL.
 *
 * Finishes an operation started with g_dbus_object_manager_client_new().
 *
703 704 705
 * Returns: (transfer full) (type GDBusObjectManagerClient): A
 *   #GDBusObjectManagerClient object or %NULL if @error is set. Free
 *   with g_object_unref().
706 707
 *
 * Since: 2.30
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
 */
GDBusObjectManager *
g_dbus_object_manager_client_new_finish (GAsyncResult   *res,
                                 GError        **error)
{
  GObject *object;
  GObject *source_object;

  source_object = g_async_result_get_source_object (res);
  g_assert (source_object != NULL);

  object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
                                        res,
                                        error);
  g_object_unref (source_object);

  if (object != NULL)
    return G_DBUS_OBJECT_MANAGER (object);
  else
    return NULL;
}

/* ---------------------------------------------------------------------------------------------------- */

/**
 * g_dbus_object_manager_client_new_for_bus_sync:
 * @bus_type: A #GBusType.
 * @flags: Zero or more flags from the #GDBusObjectManagerClientFlags enumeration.
 * @name: The owner of the control object (unique or well-known name).
 * @object_path: The object path of the control object.
 * @get_proxy_type_func: A #GDBusProxyTypeFunc function or %NULL to always construct #GDBusProxy proxies.
 * @get_proxy_type_user_data: User data to pass to @get_proxy_type_func.
740
 * @get_proxy_type_destroy_notify: (allow-none): Free function for @get_proxy_type_user_data or %NULL.
741 742 743 744 745 746 747 748 749 750
 * @cancellable: A #GCancellable or %NULL
 * @error: Return location for error or %NULL.
 *
 * Like g_dbus_object_manager_client_new_sync() but takes a #GBusType instead
 * of a #GDBusConnection.
 *
 * This is a synchronous failable constructor - the calling thread is
 * blocked until a reply is received. See g_dbus_object_manager_client_new_for_bus()
 * for the asynchronous version.
 *
751 752 753
 * Returns: (transfer full) (type GDBusObjectManagerClient): A
 *   #GDBusObjectManagerClient object or %NULL if @error is set. Free
 *   with g_object_unref().
754 755
 *
 * Since: 2.30
756 757 758 759 760 761 762 763
 */
GDBusObjectManager *
g_dbus_object_manager_client_new_for_bus_sync (GBusType                       bus_type,
                                               GDBusObjectManagerClientFlags  flags,
                                               const gchar                   *name,
                                               const gchar                   *object_path,
                                               GDBusProxyTypeFunc             get_proxy_type_func,
                                               gpointer                       get_proxy_type_user_data,
764
                                               GDestroyNotify                 get_proxy_type_destroy_notify,
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
                                               GCancellable                  *cancellable,
                                               GError                       **error)
{
  GInitable *initable;

  g_return_val_if_fail (bus_type != G_BUS_TYPE_NONE, NULL);
  g_return_val_if_fail (g_dbus_is_name (name), NULL);
  g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

  initable = g_initable_new (G_TYPE_DBUS_OBJECT_MANAGER_CLIENT,
                             cancellable,
                             error,
                             "bus-type", bus_type,
                             "flags", flags,
                             "name", name,
                             "object-path", object_path,
                             "get-proxy-type-func", get_proxy_type_func,
                             "get-proxy-type-user-data", get_proxy_type_user_data,
784
                             "get-proxy-type-destroy-notify", get_proxy_type_destroy_notify,
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
                             NULL);
  if (initable != NULL)
    return G_DBUS_OBJECT_MANAGER (initable);
  else
    return NULL;
}

/**
 * g_dbus_object_manager_client_new_for_bus:
 * @bus_type: A #GBusType.
 * @flags: Zero or more flags from the #GDBusObjectManagerClientFlags enumeration.
 * @name: The owner of the control object (unique or well-known name).
 * @object_path: The object path of the control object.
 * @get_proxy_type_func: A #GDBusProxyTypeFunc function or %NULL to always construct #GDBusProxy proxies.
 * @get_proxy_type_user_data: User data to pass to @get_proxy_type_func.
800
 * @get_proxy_type_destroy_notify: (allow-none): Free function for @get_proxy_type_user_data or %NULL.
801 802 803 804 805 806 807 808 809 810 811 812 813
 * @cancellable: A #GCancellable or %NULL
 * @callback: A #GAsyncReadyCallback to call when the request is satisfied.
 * @user_data: The data to pass to @callback.
 *
 * Like g_dbus_object_manager_client_new() but takes a #GBusType instead of a
 * #GDBusConnection.
 *
 * This is an asynchronous failable constructor. When the result is
 * ready, @callback will be invoked in the
 * <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
 * of the thread you are calling this method from. You can
 * then call g_dbus_object_manager_client_new_for_bus_finish() to get the result. See
 * g_dbus_object_manager_client_new_for_bus_sync() for the synchronous version.
814 815
 *
 * Since: 2.30
816 817 818 819 820 821 822 823
 */
void
g_dbus_object_manager_client_new_for_bus (GBusType                       bus_type,
                                          GDBusObjectManagerClientFlags  flags,
                                          const gchar                   *name,
                                          const gchar                   *object_path,
                                          GDBusProxyTypeFunc             get_proxy_type_func,
                                          gpointer                       get_proxy_type_user_data,
824
                                          GDestroyNotify                 get_proxy_type_destroy_notify,
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
                                          GCancellable                  *cancellable,
                                          GAsyncReadyCallback            callback,
                                          gpointer                       user_data)
{
  g_return_if_fail (bus_type != G_BUS_TYPE_NONE);
  g_return_if_fail (g_dbus_is_name (name));
  g_return_if_fail (g_variant_is_object_path (object_path));

  g_async_initable_new_async (G_TYPE_DBUS_OBJECT_MANAGER_CLIENT,
                              G_PRIORITY_DEFAULT,
                              cancellable,
                              callback,
                              user_data,
                              "bus-type", bus_type,
                              "flags", flags,
                              "name", name,
                              "object-path", object_path,
                              "get-proxy-type-func", get_proxy_type_func,
                              "get-proxy-type-user-data", get_proxy_type_user_data,
844
                              "get-proxy-type-destroy-notify", get_proxy_type_destroy_notify,
845 846 847 848 849 850 851 852 853 854
                              NULL);
}

/**
 * g_dbus_object_manager_client_new_for_bus_finish:
 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_object_manager_client_new_for_bus().
 * @error: Return location for error or %NULL.
 *
 * Finishes an operation started with g_dbus_object_manager_client_new_for_bus().
 *
855 856 857
 * Returns: (transfer full) (type GDBusObjectManagerClient): A
 *   #GDBusObjectManagerClient object or %NULL if @error is set. Free
 *   with g_object_unref().
858 859
 *
 * Since: 2.30
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
 */
GDBusObjectManager *
g_dbus_object_manager_client_new_for_bus_finish (GAsyncResult   *res,
                                                 GError        **error)
{
  GObject *object;
  GObject *source_object;

  source_object = g_async_result_get_source_object (res);
  g_assert (source_object != NULL);

  object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
                                        res,
                                        error);
  g_object_unref (source_object);

  if (object != NULL)
    return G_DBUS_OBJECT_MANAGER (object);
  else
    return NULL;
}

/* ---------------------------------------------------------------------------------------------------- */

/**
 * g_dbus_object_manager_client_get_connection:
 * @manager: A #GDBusObjectManagerClient
 *
 * Gets the #GDBusConnection used by @manager.
 *
890 891
 * Returns: (transfer none): A #GDBusConnection object. Do not free,
 *   the object belongs to @manager.
892 893
 *
 * Since: 2.30
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
 */
GDBusConnection *
g_dbus_object_manager_client_get_connection (GDBusObjectManagerClient *manager)
{
  g_return_val_if_fail (G_IS_DBUS_OBJECT_MANAGER_CLIENT (manager), NULL);
  return manager->priv->connection;
}

/**
 * g_dbus_object_manager_client_get_name:
 * @manager: A #GDBusObjectManagerClient
 *
 * Gets the name that @manager is for.
 *
 * Returns: A unique or well-known name. Do not free, the string
 * belongs to @manager.
910 911
 *
 * Since: 2.30
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
 */
const gchar *
g_dbus_object_manager_client_get_name (GDBusObjectManagerClient *manager)
{
  g_return_val_if_fail (G_IS_DBUS_OBJECT_MANAGER_CLIENT (manager), NULL);
  return manager->priv->name;
}

/**
 * g_dbus_object_manager_client_get_flags:
 * @manager: A #GDBusObjectManagerClient
 *
 * Gets the flags that @manager was constructed with.
 *
 * Returns: Zero of more flags from the #GDBusObjectManagerClientFlags
 * enumeration.
928 929
 *
 * Since: 2.30
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
 */
GDBusObjectManagerClientFlags
g_dbus_object_manager_client_get_flags (GDBusObjectManagerClient *manager)
{
  g_return_val_if_fail (G_IS_DBUS_OBJECT_MANAGER_CLIENT (manager), G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE);
  return manager->priv->flags;
}

/**
 * g_dbus_object_manager_client_get_name_owner:
 * @manager: A #GDBusObjectManagerClient.
 *
 * The unique name that owns the name that @manager is for or %NULL if
 * no-one currently owns that name. You can connect to the
 * #GObject::notify signal to track changes to the
 * #GDBusObjectManagerClient:name-owner property.
 *
 * Returns: The name owner or %NULL if no name owner exists. Free with
 * g_free().
949 950
 *
 * Since: 2.30
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657
 */
gchar *
g_dbus_object_manager_client_get_name_owner (GDBusObjectManagerClient *manager)
{
  g_return_val_if_fail (G_IS_DBUS_OBJECT_MANAGER_CLIENT (manager), NULL);
  return g_strdup (manager->priv->name_owner);
}

/* ---------------------------------------------------------------------------------------------------- */

/* signal handler for all objects we manage - we dispatch signals
 * from here to the objects
 */
static void
signal_cb (GDBusConnection *connection,
           const gchar     *sender_name,
           const gchar     *object_path,
           const gchar     *interface_name,
           const gchar     *signal_name,
           GVariant        *parameters,
           gpointer         user_data)
{
  GDBusObjectManagerClient *manager = G_DBUS_OBJECT_MANAGER_CLIENT (user_data);
  GDBusObjectProxy *object_proxy;
  GDBusInterface *interface;

  object_proxy = g_hash_table_lookup (manager->priv->map_object_path_to_object_proxy, object_path);
  if (object_proxy == NULL)
    goto out;

  //g_debug ("yay, signal_cb %s %s: %s\n", signal_name, object_path, g_variant_print (parameters, TRUE));

  if (g_strcmp0 (interface_name, "org.freedesktop.DBus.Properties") == 0)
    {
      if (g_strcmp0 (signal_name, "PropertiesChanged") == 0)
        {
          const gchar *interface_name;
          GVariant *changed_properties;
          const gchar **invalidated_properties;

          g_variant_get (parameters,
                         "(&s@a{sv}^a&s)",
                         &interface_name,
                         &changed_properties,
                         &invalidated_properties);

          interface = g_dbus_object_get_interface (G_DBUS_OBJECT (object_proxy), interface_name);
          if (interface != NULL)
            {
              GVariantIter property_iter;
              const gchar *property_name;
              GVariant *property_value;
              guint n;

              /* update caches... */
              g_variant_iter_init (&property_iter, changed_properties);
              while (g_variant_iter_next (&property_iter,
                                          "{&sv}",
                                          &property_name,
                                          &property_value))
                {
                  g_dbus_proxy_set_cached_property (G_DBUS_PROXY (interface),
                                                    property_name,
                                                    property_value);
                  g_variant_unref (property_value);
                }

              for (n = 0; invalidated_properties[n] != NULL; n++)
                {
                  g_dbus_proxy_set_cached_property (G_DBUS_PROXY (interface),
                                                    invalidated_properties[n],
                                                    NULL);
                }
              /* ... and then synthesize the signal */
              g_signal_emit (manager,
                             signals[INTERFACE_PROXY_PROPERTIES_CHANGED_SIGNAL],
                             0,
                             object_proxy,
                             interface,
                             changed_properties,
                             invalidated_properties);
              g_signal_emit_by_name (interface,
                                     "g-properties-changed",
                                     changed_properties,
                                     invalidated_properties);
              g_object_unref (interface);
            }
          g_variant_unref (changed_properties);
          g_free (invalidated_properties);
        }
    }
  else
    {
      /* regular signal - just dispatch it */
      interface = g_dbus_object_get_interface (G_DBUS_OBJECT (object_proxy), interface_name);
      if (interface != NULL)
        {
          g_signal_emit (manager,
                         signals[INTERFACE_PROXY_SIGNAL_SIGNAL],
                         0,
                         object_proxy,
                         interface,
                         sender_name,
                         signal_name,
                         parameters);
          g_signal_emit_by_name (interface,
                                 "g-signal",
                                 sender_name,
                                 signal_name,
                                 parameters);
          g_object_unref (interface);
        }
    }

 out:
  ;
}

static void
subscribe_signals (GDBusObjectManagerClient *manager,
                   const gchar *name_owner)
{
  GError *error;
  GVariant *ret;

  g_return_if_fail (G_IS_DBUS_OBJECT_MANAGER_CLIENT (manager));
  g_return_if_fail (manager->priv->signal_subscription_id == 0);
  g_return_if_fail (g_dbus_is_unique_name (name_owner));

  /* the bus daemon may not implement path_prefix so gracefully
   * handle this by using a fallback
   */
  manager->priv->match_rule = g_strdup_printf ("type='signal',sender='%s',path_namespace='%s'",
                                               name_owner,
                                               manager->priv->object_path);

  error = NULL;
  ret = g_dbus_connection_call_sync (manager->priv->connection,
                                     "org.freedesktop.DBus",
                                     "/org/freedeskop/DBus",
                                     "org.freedesktop.DBus",
                                     "AddMatch",
                                     g_variant_new ("(s)",
                                                    manager->priv->match_rule),
                                     NULL, /* reply_type */
                                     G_DBUS_CALL_FLAGS_NONE,
                                     -1, /* default timeout */
                                     NULL, /* TODO: Cancellable */
                                     &error);
  if (ret != NULL)
    {
      /* yay, bus daemon supports path_namespace */
      g_variant_unref (ret);

      /* still need to ask GDBusConnection for the callbacks */
      manager->priv->signal_subscription_id =
        g_dbus_connection_signal_subscribe (manager->priv->connection,
                                            name_owner,
                                            NULL, /* interface */
                                            NULL, /* member */
                                            NULL, /* path - TODO: really want wilcard support here */
                                            NULL, /* arg0 */
                                            G_DBUS_SIGNAL_FLAGS_NONE |
                                            G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE,
                                            signal_cb,
                                            manager,
                                            NULL); /* user_data_free_func */

    }
  else
    {
      /* TODO: we could report this to the user
      g_warning ("Message bus daemon does not support path_namespace: %s (%s %d)",
                 error->message,
                 g_quark_to_string (error->domain),
                 error->code);
      */

      g_error_free (error);

      /* no need to call RemoveMatch when done since it didn't work */
      g_free (manager->priv->match_rule);
      manager->priv->match_rule = NULL;

      /* Fallback is to subscribe to *all* signals from the name owner which
       * is rather wasteful. It's probably not a big practical problem because
       * users typically want all objects that the name owner supplies.
       */
      manager->priv->signal_subscription_id =
        g_dbus_connection_signal_subscribe (manager->priv->connection,
                                            name_owner,
                                            NULL, /* interface */
                                            NULL, /* member */
                                            NULL, /* path - TODO: really want wilcard support here */
                                            NULL, /* arg0 */
                                            G_DBUS_SIGNAL_FLAGS_NONE,
                                            signal_cb,
                                            manager,
                                            NULL); /* user_data_free_func */
    }
}

static void
maybe_unsubscribe_signals (GDBusObjectManagerClient *manager)
{
  g_return_if_fail (G_IS_DBUS_OBJECT_MANAGER_CLIENT (manager));

  if (manager->priv->signal_subscription_id > 0)
    {
      g_dbus_connection_signal_unsubscribe (manager->priv->connection,
                                            manager->priv->signal_subscription_id);
      manager->priv->signal_subscription_id = 0;
    }

  if (manager->priv->match_rule != NULL)
    {
      /* Since the AddMatch call succeeded this is guaranteed to not
       * fail - therefore, don't bother checking the return value
       */
      g_dbus_connection_call (manager->priv->connection,
                              "org.freedesktop.DBus",
                              "/org/freedeskop/DBus",
                              "org.freedesktop.DBus",
                              "RemoveMatch",
                              g_variant_new ("(s)",
                                             manager->priv->match_rule),
                              NULL, /* reply_type */
                              G_DBUS_CALL_FLAGS_NONE,
                              -1, /* default timeout */
                              NULL, /* GCancellable */
                              NULL, /* GAsyncReadyCallback */
                              NULL); /* user data */
      g_free (manager->priv->match_rule);
      manager->priv->match_rule = NULL;
    }

}

/* ---------------------------------------------------------------------------------------------------- */

static void
on_notify_g_name_owner (GObject    *object,
                        GParamSpec *pspec,
                        gpointer    user_data)
{
  GDBusObjectManagerClient *manager = G_DBUS_OBJECT_MANAGER_CLIENT (user_data);
  gchar *old_name_owner;
  gchar *new_name_owner;

  old_name_owner = manager->priv->name_owner;
  new_name_owner = g_dbus_proxy_get_name_owner (manager->priv->control_proxy);
  manager->priv->name_owner = NULL;

  if (g_strcmp0 (old_name_owner, new_name_owner) != 0)
    {
      GList *l;
      GList *proxies;

      /* do the :name-owner notify with a NULL name - this way the user knows
       * the ::object-proxy-removed following is because the name owner went
       * away
       */
      g_object_notify (G_OBJECT (manager), "name-owner");

      /* remote manager changed; nuke all local proxies  */
      proxies = g_hash_table_get_values (manager->priv->map_object_path_to_object_proxy);
      g_list_foreach (proxies, (GFunc) g_object_ref, NULL);
      g_hash_table_remove_all (manager->priv->map_object_path_to_object_proxy);
      for (l = proxies; l != NULL; l = l->next)
        {
          GDBusObjectProxy *object_proxy = G_DBUS_OBJECT_PROXY (l->data);
          g_signal_emit_by_name (manager, "object-removed", object_proxy);
        }
      g_list_foreach (proxies, (GFunc) g_object_unref, NULL);
      g_list_free (proxies);

      /* nuke local filter */
      maybe_unsubscribe_signals (manager);
    }

  if (new_name_owner != NULL)
    {
      GError *error;
      GVariant *value;

      //g_debug ("repopulating for %s", new_name_owner);

      /* TODO: do this async! */
      subscribe_signals (manager,
                         new_name_owner);
      error = NULL;
      value = g_dbus_proxy_call_sync (manager->priv->control_proxy,
                                      "GetManagedObjects",
                                      NULL, /* parameters */
                                      G_DBUS_CALL_FLAGS_NONE,
                                      -1,
                                      NULL,
                                      &error);
      if (value == NULL)
        {
          maybe_unsubscribe_signals (manager);
          g_warning ("Error calling GetManagedObjects() when name owner %s for name %s came back: %s",
                     new_name_owner,
                     manager->priv->name,
                     error->message);
          g_error_free (error);
        }
      else
        {
          process_get_all_result (manager, value, new_name_owner);
          g_variant_unref (value);
        }

      /* do the :name-owner notify *AFTER* emitting ::object-proxy-added signals - this
       * way the user knows that the signals were emitted because the name owner came back
       */
      manager->priv->name_owner = new_name_owner;
      g_object_notify (G_OBJECT (manager), "name-owner");

    }
  g_free (old_name_owner);
}

static gboolean
initable_init (GInitable     *initable,
               GCancellable  *cancellable,
               GError       **error)
{
  GDBusObjectManagerClient *manager = G_DBUS_OBJECT_MANAGER_CLIENT (initable);
  gboolean ret;
  GVariant *value;
  GDBusProxyFlags proxy_flags;

  ret = FALSE;

  if (manager->priv->bus_type != G_BUS_TYPE_NONE)
    {
      g_assert (manager->priv->connection == NULL);
      manager->priv->connection = g_bus_get_sync (manager->priv->bus_type, cancellable, error);
      if (manager->priv->connection == NULL)
        goto out;
    }

  proxy_flags = G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES;
  if (manager->priv->flags & G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_DO_NOT_AUTO_START)
    proxy_flags |= G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START;;

  manager->priv->control_proxy = g_dbus_proxy_new_sync (manager->priv->connection,
                                                        proxy_flags,
                                                        NULL, /* GDBusInterfaceInfo* */
                                                        manager->priv->name,
                                                        manager->priv->object_path,
                                                        "org.freedesktop.DBus.ObjectManager",
                                                        cancellable,
                                                        error);
  if (manager->priv->control_proxy == NULL)
    goto out;

  g_signal_connect (G_OBJECT (manager->priv->control_proxy),
                    "notify::g-name-owner",
                    G_CALLBACK (on_notify_g_name_owner),
                    manager);

  manager->priv->name_owner = g_dbus_proxy_get_name_owner (manager->priv->control_proxy);
  if (manager->priv->name_owner == NULL)
    {
      /* it's perfectly fine if there's no name owner.. we're just going to
       * wait until one is ready
       */
    }
  else
    {
      /* yay, we have a name owner */
      g_signal_connect (manager->priv->control_proxy,
                        "g-signal",
                        G_CALLBACK (on_control_proxy_g_signal),
                        manager);
      subscribe_signals (manager,
                         manager->priv->name_owner);
      value = g_dbus_proxy_call_sync (manager->priv->control_proxy,
                                      "GetManagedObjects",
                                      NULL, /* parameters */
                                      G_DBUS_CALL_FLAGS_NONE,
                                      -1,
                                      cancellable,
                                      error);
      if (value == NULL)
        {
          maybe_unsubscribe_signals (manager);
          g_warn_if_fail (g_signal_handlers_disconnect_by_func (manager->priv->control_proxy,
                                                                on_control_proxy_g_signal,
                                                                manager) == 1);
          g_object_unref (manager->priv->control_proxy);
          manager->priv->control_proxy = NULL;
          goto out;
        }

      process_get_all_result (manager, value, manager->priv->name_owner);
      g_variant_unref (value);
    }

  ret = TRUE;

 out:
  return ret;
}

static void
initable_iface_init (GInitableIface *initable_iface)
{
  initable_iface->init = initable_init;
}

static void
async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
{
  /* for now, just use default: run GInitable code in thread */
}

/* ---------------------------------------------------------------------------------------------------- */

static void
add_interfaces (GDBusObjectManagerClient *manager,
                const gchar       *object_path,
                GVariant          *ifaces_and_properties,
                const gchar       *name_owner)
{
  GDBusObjectProxy *op;
  gboolean added;
  GVariantIter iter;
  const gchar *interface_name;
  GVariant *properties;

  g_return_if_fail (g_dbus_is_unique_name (name_owner));

  added = FALSE;
  op = g_hash_table_lookup (manager->priv->map_object_path_to_object_proxy, object_path);
  if (op == NULL)
    {
      op = _g_dbus_object_proxy_new (manager->priv->connection, object_path);
      added = TRUE;
    }

  g_variant_iter_init (&iter, ifaces_and_properties);
  while (g_variant_iter_next (&iter,
                              "{&s@a{sv}}",
                              &interface_name,
                              &properties))
    {
      GDBusProxy *interface_proxy;
      GError *error;
      GType interface_proxy_type;

      if (manager->priv->get_proxy_type_func != NULL)
        {
          interface_proxy_type = manager->priv->get_proxy_type_func (manager,
                                                                     object_path,
                                                                     interface_name,
                                                                     manager->priv->get_proxy_type_user_data);
          g_warn_if_fail (g_type_is_a (interface_proxy_type, G_TYPE_DBUS_PROXY));
        }
      else
        {
          interface_proxy_type = G_TYPE_DBUS_PROXY;
        }

      /* this is fine - there is no blocking IO because we pass DO_NOT_LOAD_PROPERTIES and
       * DO_NOT_CONNECT_SIGNALS and use a unique name
       */
      error = NULL;
      interface_proxy = g_initable_new (interface_proxy_type,
                                        NULL, /* GCancellable */
                                        &error,
                                        "g-connection", manager->priv->connection,
                                        "g-flags", G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
                                                   G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
                                        "g-name", name_owner,
                                        "g-object-path", object_path,
                                        "g-interface-name", interface_name,
                                        NULL);
      if (interface_proxy == NULL)
        {
          g_warning ("%s: Error constructing proxy for path %s and interface %s: %s",
                     G_STRLOC,
                     object_path,
                     interface_name,
                     error->message);
          g_error_free (error);
        }
      else
        {
          GVariantIter property_iter;
          const gchar *property_name;
          GVariant *property_value;

          /* associate the interface proxy with the object */
          g_dbus_interface_set_object (G_DBUS_INTERFACE (interface_proxy),
                                       G_DBUS_OBJECT (op));

          g_variant_iter_init (&property_iter, properties);
          while (g_variant_iter_next (&property_iter,
                                      "{&sv}",
                                      &property_name,
                                      &property_value))
            {
              g_dbus_proxy_set_cached_property (interface_proxy,
                                                property_name,
                                                property_value);
              g_variant_unref (property_value);
            }

          _g_dbus_object_proxy_add_interface (op, interface_proxy);
          if (!added)
            g_signal_emit_by_name (manager, "interface-added", op, interface_proxy);
          g_object_unref (interface_proxy);
        }
      g_variant_unref (properties);
    }

  if (added)
    {
      g_hash_table_insert (manager->priv->map_object_path_to_object_proxy,
                           g_strdup (object_path),
                           op);
      g_signal_emit_by_name (manager, "object-added", op);
    }
}

static void
remove_interfaces (GDBusObjectManagerClient   *manager,
                   const gchar         *object_path,
                   const gchar *const  *interface_names)
{
  GDBusObjectProxy *op;
  GList *interfaces;
  guint n;
  guint num_interfaces;
  guint num_interfaces_to_remove;

  op = g_hash_table_lookup (manager->priv->map_object_path_to_object_proxy, object_path);
  if (op == NULL)
    {
      g_warning ("%s: Processing InterfaceRemoved signal for path %s but no object proxy exists",
                 G_STRLOC,
                 object_path);
      goto out;
    }

  interfaces = g_dbus_object_get_interfaces (G_DBUS_OBJECT (op));
  num_interfaces = g_list_length (interfaces);
  g_list_foreach (interfaces, (GFunc) g_object_unref, NULL);
  g_list_free (interfaces);

  num_interfaces_to_remove = g_strv_length ((gchar **) interface_names);

  /* see if we are going to completety remove the object */
  if (num_interfaces_to_remove == num_interfaces)
    {
      g_object_ref (op);
      g_warn_if_fail (g_hash_table_remove (manager->priv->map_object_path_to_object_proxy, object_path));
      g_signal_emit_by_name (manager, "object-removed", op);
      g_object_unref (op);
    }
  else
    {
      for (n = 0; interface_names != NULL && interface_names[n] != NULL; n++)
        {
          GDBusInterface *interface;
          interface = g_dbus_object_get_interface (G_DBUS_OBJECT (op), interface_names[n]);
          _g_dbus_object_proxy_remove_interface (op, interface_names[n]);
          if (interface != NULL)
            {
              g_signal_emit_by_name (manager, "interface-removed", op, interface);
              g_object_unref (interface);
            }
        }
    }
 out:
  ;
}

static void
process_get_all_result (GDBusObjectManagerClient *manager,
                        GVariant          *value,
                        const gchar       *name_owner)
{
  GVariant *arg0;
  const gchar *object_path;
  GVariant *ifaces_and_properties;
  GVariantIter iter;

  g_return_if_fail (g_dbus_is_unique_name (name_owner));

  arg0 = g_variant_get_child_value (value, 0);
  g_variant_iter_init (&iter, arg0);
  while (g_variant_iter_next (&iter,
                              "{&o@a{sa{sv}}}",
                              &object_path,
                              &ifaces_and_properties))
    {
      add_interfaces (manager, object_path, ifaces_and_properties, name_owner);
      g_variant_unref (ifaces_and_properties);
    }
  g_variant_unref (arg0);
}

static void
on_control_proxy_g_signal (GDBusProxy   *proxy,
                           const gchar  *sender_name,
                           const gchar  *signal_name,
                           GVariant     *parameters,
                           gpointer      user_data)
{
  GDBusObjectManagerClient *manager = G_DBUS_OBJECT_MANAGER_CLIENT (user_data);
  const gchar *object_path;

  //g_debug ("yay, g_signal %s: %s\n", signal_name, g_variant_print (parameters, TRUE));

  if (g_strcmp0 (signal_name, "InterfacesAdded") == 0)
    {
      GVariant *ifaces_and_properties;
      g_variant_get (parameters,
                     "(&o@a{sa{sv}})",
                     &object_path,
                     &ifaces_and_properties);
      add_interfaces (manager, object_path, ifaces_and_properties, manager->priv->name_owner);
      g_variant_unref (ifaces_and_properties);
    }
  else if (g_strcmp0 (signal_name, "InterfacesRemoved") == 0)
    {
      const gchar **ifaces;
      g_variant_get (parameters,
                     "(&o^a&s)",
                     &object_path,
                     &ifaces);
      remove_interfaces (manager, object_path, ifaces);
      g_free (ifaces);
    }
}

/* ---------------------------------------------------------------------------------------------------- */

static const gchar *
g_dbus_object_manager_client_get_object_path (GDBusObjectManager *_manager)
{
  GDBusObjectManagerClient *manager = G_DBUS_OBJECT_MANAGER_CLIENT (_manager);
  return manager->priv->object_path;
}

static GDBusObject *
g_dbus_object_manager_client_get_object (GDBusObjectManager *_manager,
                                         const gchar        *object_path)
{
  GDBusObjectManagerClient *manager = G_DBUS_OBJECT_MANAGER_CLIENT (_manager);
  GDBusObject *ret;

  ret = g_hash_table_lookup (manager->priv->map_object_path_to_object_proxy, object_path);
  if (ret != NULL)
    g_object_ref (ret);
  return ret;
}

static GDBusInterface *
g_dbus_object_manager_client_get_interface  (GDBusObjectManager  *_manager,
                                             const gchar         *object_path,
                                             const gchar         *interface_name)
{
  GDBusInterface *ret;
  GDBusObject *object;

  ret = NULL;

  object = g_dbus_object_manager_get_object (_manager, object_path);
  if (object == NULL)
    goto out;

  ret = g_dbus_object_get_interface (object, interface_name);
  g_object_unref (object);

 out:
  return ret;
}

static GList *
g_dbus_object_manager_client_get_objects (GDBusObjectManager *_manager)
{
  GDBusObjectManagerClient *manager = G_DBUS_OBJECT_MANAGER_CLIENT (_manager);
  GList *ret;

  g_return_val_if_fail (G_IS_DBUS_OBJECT_MANAGER_CLIENT (manager), NULL);

  ret = g_hash_table_get_values (manager->priv->map_object_path_to_object_proxy);
  g_list_foreach (ret, (GFunc) g_object_ref, NULL);
  return ret;
}


static void
dbus_object_manager_interface_init (GDBusObjectManagerIface *iface)
{
  iface->get_object_path = g_dbus_object_manager_client_get_object_path;
  iface->get_objects     = g_dbus_object_manager_client_get_objects;
  iface->get_object      = g_dbus_object_manager_client_get_object;
  iface->get_interface   = g_dbus_object_manager_client_get_interface;
}

/* ---------------------------------------------------------------------------------------------------- */