Blame gio/gdbusmenumodel.c

Packit ae235b
/*
Packit ae235b
 * Copyright © 2011 Canonical Ltd.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful, but
Packit ae235b
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: Ryan Lortie <desrt@desrt.ca>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include "gdbusmenumodel.h"
Packit ae235b
Packit ae235b
#include "gmenumodel.h"
Packit ae235b
Packit ae235b
/* Prelude {{{1 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gdbusmenumodel
Packit ae235b
 * @title: GDBusMenuModel
Packit ae235b
 * @short_description: A D-Bus GMenuModel implementation
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 * @see_also: [GMenuModel Exporter][gio-GMenuModel-exporter]
Packit ae235b
 *
Packit ae235b
 * #GDBusMenuModel is an implementation of #GMenuModel that can be used
Packit ae235b
 * as a proxy for a menu model that is exported over D-Bus with
Packit ae235b
 * g_dbus_connection_export_menu_model().
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GDBusMenuModel:
Packit ae235b
 *
Packit ae235b
 * #GDBusMenuModel is an opaque data structure and can only be accessed
Packit ae235b
 * using the following functions.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * There are 3 main (quasi-)classes involved here:
Packit ae235b
 *
Packit ae235b
 *   - GDBusMenuPath
Packit ae235b
 *   - GDBusMenuGroup
Packit ae235b
 *   - GDBusMenuModel
Packit ae235b
 *
Packit ae235b
 * Each of these classes exists as a parameterised singleton keyed to a
Packit ae235b
 * particular thing:
Packit ae235b
 *
Packit ae235b
 *   - GDBusMenuPath represents a D-Bus object path on a particular
Packit ae235b
 *     unique bus name on a particular GDBusConnection and in a
Packit ae235b
 *     particular GMainContext.
Packit ae235b
 *
Packit ae235b
 *   - GDBusMenuGroup represents a particular group on a particular
Packit ae235b
 *     GDBusMenuPath.
Packit ae235b
 *
Packit ae235b
 *   - GDBusMenuModel represents a particular menu within a particular
Packit ae235b
 *     GDBusMenuGroup.
Packit ae235b
 *
Packit ae235b
 * There are also two (and a half) utility structs:
Packit ae235b
 *
Packit ae235b
 *  - PathIdentifier and ConstPathIdentifier
Packit ae235b
 *  - GDBusMenuModelItem
Packit ae235b
 *
Packit ae235b
 * PathIdentifier is the 4-tuple of (GMainContext, GDBusConnection,
Packit ae235b
 * unique name, object path) that uniquely identifies a particular
Packit ae235b
 * GDBusMenuPath.  It holds ownership on each of these things, so we
Packit ae235b
 * have a ConstPathIdentifier variant that does not.
Packit ae235b
 *
Packit ae235b
 * We have a 3-level hierarchy of hashtables:
Packit ae235b
 *
Packit ae235b
 *   - a global hashtable (g_dbus_menu_paths) maps from PathIdentifier
Packit ae235b
 *     to GDBusMenuPath
Packit ae235b
 *
Packit ae235b
 *   - each GDBusMenuPath has a hashtable mapping from guint (group
Packit ae235b
 *     number) to GDBusMenuGroup
Packit ae235b
 *
Packit ae235b
 *   - each GDBusMenuGroup has a hashtable mapping from guint (menu
Packit ae235b
 *     number) to GDBusMenuModel.
Packit ae235b
 *
Packit ae235b
 * In this way, each quintuplet of (connection, bus name, object path,
Packit ae235b
 * group id, menu id) maps to a single GDBusMenuModel instance that can be
Packit ae235b
 * located via 3 hashtable lookups.
Packit ae235b
 *
Packit ae235b
 * All of the 3 classes are refcounted (GDBusMenuPath and
Packit ae235b
 * GDBusMenuGroup manually, and GDBusMenuModel by virtue of being a
Packit ae235b
 * GObject).  The hashtables do not hold references -- rather, when the
Packit ae235b
 * last reference is dropped, the object is removed from the hashtable.
Packit ae235b
 *
Packit ae235b
 * The hard references go in the other direction: GDBusMenuModel is created
Packit ae235b
 * as the user requests it and only exists as long as the user holds a
Packit ae235b
 * reference on it.  GDBusMenuModel holds a reference on the GDBusMenuGroup
Packit ae235b
 * from which it came. GDBusMenuGroup holds a reference on
Packit ae235b
 * GDBusMenuPath.
Packit ae235b
 *
Packit ae235b
 * In addition to refcounts, each object has an 'active' variable (ints
Packit ae235b
 * for GDBusMenuPath and GDBusMenuGroup, boolean for GDBusMenuModel).
Packit ae235b
 *
Packit ae235b
 *   - GDBusMenuModel is inactive when created and becomes active only when
Packit ae235b
 *     first queried for information.  This prevents extra work from
Packit ae235b
 *     happening just by someone acquiring a GDBusMenuModel (and not
Packit ae235b
 *     actually trying to display it yet).
Packit ae235b
 *
Packit ae235b
 *   - The active count on GDBusMenuGroup is equal to the number of
Packit ae235b
 *     GDBusMenuModel instances in that group that are active.  When the
Packit ae235b
 *     active count transitions from 0 to 1, the group calls the 'Start'
Packit ae235b
 *     method on the service to begin monitoring that group.  When it
Packit ae235b
 *     drops from 1 to 0, the group calls the 'End' method to stop
Packit ae235b
 *     monitoring.
Packit ae235b
 *
Packit ae235b
 *   - The active count on GDBusMenuPath is equal to the number of
Packit ae235b
 *     GDBusMenuGroup instances on that path with a non-zero active
Packit ae235b
 *     count.  When the active count transitions from 0 to 1, the path
Packit ae235b
 *     sets up a signal subscription to monitor any changes.  The signal
Packit ae235b
 *     subscription is taken down when the active count transitions from
Packit ae235b
 *     1 to 0.
Packit ae235b
 *
Packit ae235b
 * When active, GDBusMenuPath gets incoming signals when changes occur.
Packit ae235b
 * If the change signal mentions a group for which we currently have an
Packit ae235b
 * active GDBusMenuGroup, the change signal is passed along to that
Packit ae235b
 * group.  If the group is inactive, the change signal is ignored.
Packit ae235b
 *
Packit ae235b
 * Most of the "work" occurs in GDBusMenuGroup.  In addition to the
Packit ae235b
 * hashtable of GDBusMenuModel instances, it keeps a hashtable of the actual
Packit ae235b
 * menu contents, each encoded as GSequence of GDBusMenuModelItem.  It
Packit ae235b
 * initially populates this table with the results of the "Start" method
Packit ae235b
 * call and then updates it according to incoming change signals.  If
Packit ae235b
 * the change signal mentions a menu for which we current have an active
Packit ae235b
 * GDBusMenuModel, the change signal is passed along to that model.  If the
Packit ae235b
 * model is inactive, the change signal is ignored.
Packit ae235b
 *
Packit ae235b
 * GDBusMenuModelItem is just a pair of hashtables, one for the attributes
Packit ae235b
 * and one for the links of the item.  Both map strings to GVariant
Packit ae235b
 * instances.  In the case of links, the GVariant has type '(uu)' and is
Packit ae235b
 * turned into a GDBusMenuModel at the point that the user pulls it through
Packit ae235b
 * the API.
Packit ae235b
 *
Packit ae235b
 * Following the "empty is the same as non-existent" rule, the hashtable
Packit ae235b
 * of GSequence of GDBusMenuModelItem holds NULL for empty menus.
Packit ae235b
 *
Packit ae235b
 * GDBusMenuModel contains very little functionality of its own.  It holds a
Packit ae235b
 * (weak) reference to the GSequence of GDBusMenuModelItem contained in the
Packit ae235b
 * GDBusMenuGroup.  It uses this GSequence to implement the GMenuModel
Packit ae235b
 * interface.  It also emits the "items-changed" signal if it is active
Packit ae235b
 * and it was told that the contents of the GSequence changed.
Packit ae235b
 */
Packit ae235b
Packit ae235b
typedef struct _GDBusMenuGroup GDBusMenuGroup;
Packit ae235b
typedef struct _GDBusMenuPath GDBusMenuPath;
Packit ae235b
Packit ae235b
static void                     g_dbus_menu_group_changed               (GDBusMenuGroup  *group,
Packit ae235b
                                                                         guint            menu_id,
Packit ae235b
                                                                         gint             position,
Packit ae235b
                                                                         gint             removed,
Packit ae235b
                                                                         GVariant        *added);
Packit ae235b
static void                     g_dbus_menu_model_changed               (GDBusMenuModel  *proxy,
Packit ae235b
                                                                         GSequence       *items,
Packit ae235b
                                                                         gint             position,
Packit ae235b
                                                                         gint             removed,
Packit ae235b
                                                                         gint             added);
Packit ae235b
static GDBusMenuGroup *         g_dbus_menu_group_get_from_path         (GDBusMenuPath   *path,
Packit ae235b
                                                                         guint            group_id);
Packit ae235b
static GDBusMenuModel *         g_dbus_menu_model_get_from_group        (GDBusMenuGroup  *group,
Packit ae235b
                                                                         guint            menu_id);
Packit ae235b
Packit ae235b
/* PathIdentifier {{{1 */
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  GMainContext *context;
Packit ae235b
  GDBusConnection *connection;
Packit ae235b
  gchar *bus_name;
Packit ae235b
  gchar *object_path;
Packit ae235b
} PathIdentifier;
Packit ae235b
Packit ae235b
typedef const struct
Packit ae235b
{
Packit ae235b
  GMainContext *context;
Packit ae235b
  GDBusConnection *connection;
Packit ae235b
  const gchar *bus_name;
Packit ae235b
  const gchar *object_path;
Packit ae235b
} ConstPathIdentifier;
Packit ae235b
Packit ae235b
static guint
Packit ae235b
path_identifier_hash (gconstpointer data)
Packit ae235b
{
Packit ae235b
  ConstPathIdentifier *id = data;
Packit ae235b
Packit ae235b
  return g_str_hash (id->object_path);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
path_identifier_equal (gconstpointer a,
Packit ae235b
                       gconstpointer b)
Packit ae235b
{
Packit ae235b
  ConstPathIdentifier *id_a = a;
Packit ae235b
  ConstPathIdentifier *id_b = b;
Packit ae235b
Packit ae235b
  return id_a->connection == id_b->connection &&
Packit ae235b
         g_strcmp0 (id_a->bus_name, id_b->bus_name) == 0 &&
Packit ae235b
         g_str_equal (id_a->object_path, id_b->object_path);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
path_identifier_free (PathIdentifier *id)
Packit ae235b
{
Packit ae235b
  g_main_context_unref (id->context);
Packit ae235b
  g_object_unref (id->connection);
Packit ae235b
  g_free (id->bus_name);
Packit ae235b
  g_free (id->object_path);
Packit ae235b
Packit ae235b
  g_slice_free (PathIdentifier, id);
Packit ae235b
}
Packit ae235b
Packit ae235b
static PathIdentifier *
Packit ae235b
path_identifier_new (ConstPathIdentifier *cid)
Packit ae235b
{
Packit ae235b
  PathIdentifier *id;
Packit ae235b
Packit ae235b
  id = g_slice_new (PathIdentifier);
Packit ae235b
  id->context = g_main_context_ref (cid->context);
Packit ae235b
  id->connection = g_object_ref (cid->connection);
Packit ae235b
  id->bus_name = g_strdup (cid->bus_name);
Packit ae235b
  id->object_path = g_strdup (cid->object_path);
Packit ae235b
Packit ae235b
  return id;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* GDBusMenuPath {{{1 */
Packit ae235b
Packit ae235b
struct _GDBusMenuPath
Packit ae235b
{
Packit ae235b
  PathIdentifier *id;
Packit ae235b
  gint ref_count;
Packit ae235b
Packit ae235b
  GHashTable *groups;
Packit ae235b
  gint active;
Packit ae235b
  guint watch_id;
Packit ae235b
};
Packit ae235b
Packit ae235b
static GHashTable *g_dbus_menu_paths;
Packit ae235b
Packit ae235b
static GDBusMenuPath *
Packit ae235b
g_dbus_menu_path_ref (GDBusMenuPath *path)
Packit ae235b
{
Packit ae235b
  path->ref_count++;
Packit ae235b
Packit ae235b
  return path;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_dbus_menu_path_unref (GDBusMenuPath *path)
Packit ae235b
{
Packit ae235b
  if (--path->ref_count == 0)
Packit ae235b
    {
Packit ae235b
      g_hash_table_remove (g_dbus_menu_paths, path->id);
Packit ae235b
      g_hash_table_unref (path->groups);
Packit ae235b
      path_identifier_free (path->id);
Packit ae235b
Packit ae235b
      g_slice_free (GDBusMenuPath, path);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_dbus_menu_path_signal (GDBusConnection *connection,
Packit ae235b
                         const gchar     *sender_name,
Packit ae235b
                         const gchar     *object_path,
Packit ae235b
                         const gchar     *interface_name,
Packit ae235b
                         const gchar     *signal_name,
Packit ae235b
                         GVariant        *parameters,
Packit ae235b
                         gpointer         user_data)
Packit ae235b
{
Packit ae235b
  GDBusMenuPath *path = user_data;
Packit ae235b
  GVariantIter *iter;
Packit ae235b
  guint group_id;
Packit ae235b
  guint menu_id;
Packit ae235b
  guint position;
Packit ae235b
  guint removes;
Packit ae235b
  GVariant *adds;
Packit ae235b
Packit ae235b
  if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(a(uuuuaa{sv}))")))
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  g_variant_get (parameters, "(a(uuuuaa{sv}))", &iter);
Packit ae235b
  while (g_variant_iter_loop (iter, "(uuuu@aa{sv})", &group_id, &menu_id, &position, &removes, &adds))
Packit ae235b
    {
Packit ae235b
      GDBusMenuGroup *group;
Packit ae235b
Packit ae235b
      group = g_hash_table_lookup (path->groups, GINT_TO_POINTER (group_id));
Packit ae235b
Packit ae235b
      if (group != NULL)
Packit ae235b
        g_dbus_menu_group_changed (group, menu_id, position, removes, adds);
Packit ae235b
    }
Packit ae235b
  g_variant_iter_free (iter);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_dbus_menu_path_activate (GDBusMenuPath *path)
Packit ae235b
{
Packit ae235b
  if (path->active++ == 0)
Packit ae235b
    path->watch_id = g_dbus_connection_signal_subscribe (path->id->connection, path->id->bus_name,
Packit ae235b
                                                         "org.gtk.Menus", "Changed", path->id->object_path,
Packit ae235b
                                                         NULL, G_DBUS_SIGNAL_FLAGS_NONE,
Packit ae235b
                                                         g_dbus_menu_path_signal, path, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_dbus_menu_path_deactivate (GDBusMenuPath *path)
Packit ae235b
{
Packit ae235b
  if (--path->active == 0)
Packit ae235b
    g_dbus_connection_signal_unsubscribe (path->id->connection, path->watch_id);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GDBusMenuPath *
Packit ae235b
g_dbus_menu_path_get (GMainContext    *context,
Packit ae235b
                      GDBusConnection *connection,
Packit ae235b
                      const gchar     *bus_name,
Packit ae235b
                      const gchar     *object_path)
Packit ae235b
{
Packit ae235b
  ConstPathIdentifier cid = { context, connection, bus_name, object_path };
Packit ae235b
  GDBusMenuPath *path;
Packit ae235b
Packit ae235b
  if (g_dbus_menu_paths == NULL)
Packit ae235b
    g_dbus_menu_paths = g_hash_table_new (path_identifier_hash, path_identifier_equal);
Packit ae235b
Packit ae235b
  path = g_hash_table_lookup (g_dbus_menu_paths, &cid;;
Packit ae235b
Packit ae235b
  if (path == NULL)
Packit ae235b
    {
Packit ae235b
      path = g_slice_new (GDBusMenuPath);
Packit ae235b
      path->id = path_identifier_new (&cid;;
Packit ae235b
      path->groups = g_hash_table_new (NULL, NULL);
Packit ae235b
      path->ref_count = 0;
Packit ae235b
      path->active = 0;
Packit ae235b
Packit ae235b
      g_hash_table_insert (g_dbus_menu_paths, path->id, path);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return g_dbus_menu_path_ref (path);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* GDBusMenuGroup, GDBusMenuModelItem {{{1 */
Packit ae235b
typedef enum
Packit ae235b
{
Packit ae235b
  GROUP_OFFLINE,
Packit ae235b
  GROUP_PENDING,
Packit ae235b
  GROUP_ONLINE
Packit ae235b
} GroupStatus;
Packit ae235b
Packit ae235b
struct _GDBusMenuGroup
Packit ae235b
{
Packit ae235b
  GDBusMenuPath *path;
Packit ae235b
  guint id;
Packit ae235b
Packit ae235b
  GHashTable *proxies; /* uint -> unowned GDBusMenuModel */
Packit ae235b
  GHashTable *menus;   /* uint -> owned GSequence */
Packit ae235b
  gint ref_count;
Packit ae235b
  GroupStatus state;
Packit ae235b
  gint active;
Packit ae235b
};
Packit ae235b
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  GHashTable *attributes;
Packit ae235b
  GHashTable *links;
Packit ae235b
} GDBusMenuModelItem;
Packit ae235b
Packit ae235b
static GDBusMenuGroup *
Packit ae235b
g_dbus_menu_group_ref (GDBusMenuGroup *group)
Packit ae235b
{
Packit ae235b
  group->ref_count++;
Packit ae235b
Packit ae235b
  return group;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_dbus_menu_group_unref (GDBusMenuGroup *group)
Packit ae235b
{
Packit ae235b
  if (--group->ref_count == 0)
Packit ae235b
    {
Packit ae235b
      g_assert (group->state == GROUP_OFFLINE);
Packit ae235b
      g_assert (group->active == 0);
Packit ae235b
Packit ae235b
      g_hash_table_remove (group->path->groups, GINT_TO_POINTER (group->id));
Packit ae235b
      g_hash_table_unref (group->proxies);
Packit ae235b
      g_hash_table_unref (group->menus);
Packit ae235b
Packit ae235b
      g_dbus_menu_path_unref (group->path);
Packit ae235b
Packit ae235b
      g_slice_free (GDBusMenuGroup, group);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_dbus_menu_model_item_free (gpointer data)
Packit ae235b
{
Packit ae235b
  GDBusMenuModelItem *item = data;
Packit ae235b
Packit ae235b
  g_hash_table_unref (item->attributes);
Packit ae235b
  g_hash_table_unref (item->links);
Packit ae235b
Packit ae235b
  g_slice_free (GDBusMenuModelItem, item);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GDBusMenuModelItem *
Packit ae235b
g_dbus_menu_group_create_item (GVariant *description)
Packit ae235b
{
Packit ae235b
  GDBusMenuModelItem *item;
Packit ae235b
  GVariantIter iter;
Packit ae235b
  const gchar *key;
Packit ae235b
  GVariant *value;
Packit ae235b
Packit ae235b
  item = g_slice_new (GDBusMenuModelItem);
Packit ae235b
  item->attributes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_variant_unref);
Packit ae235b
  item->links = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_variant_unref);
Packit ae235b
Packit ae235b
  g_variant_iter_init (&iter, description);
Packit ae235b
  while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
Packit ae235b
    if (key[0] == ':')
Packit ae235b
      /* key + 1 to skip the ':' */
Packit ae235b
      g_hash_table_insert (item->links, g_strdup (key + 1), g_variant_ref (value));
Packit ae235b
    else
Packit ae235b
      g_hash_table_insert (item->attributes, g_strdup (key), g_variant_ref (value));
Packit ae235b
Packit ae235b
  return item;
Packit ae235b
}
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * GDBusMenuGroup can be in three states:
Packit ae235b
 *
Packit ae235b
 * OFFLINE: not subscribed to this group
Packit ae235b
 * PENDING: we made the call to subscribe to this group, but the result
Packit ae235b
 *          has not come back yet
Packit ae235b
 * ONLINE: we are fully subscribed
Packit ae235b
 *
Packit ae235b
 * We can get into some nasty situations where we make a call due to an
Packit ae235b
 * activation request but receive a deactivation request before the call
Packit ae235b
 * returns.  If another activation request occurs then we could risk
Packit ae235b
 * sending a Start request even though one is already in progress.  For
Packit ae235b
 * this reason, we have to carefully consider what to do in each of the
Packit ae235b
 * three states for each of the following situations:
Packit ae235b
 *
Packit ae235b
 *  - activation requested
Packit ae235b
 *  - deactivation requested
Packit ae235b
 *  - Start call finishes
Packit ae235b
 *
Packit ae235b
 * To simplify things a bit, we do not have a callback for the Stop
Packit ae235b
 * call.  We just send it and assume that it takes effect immediately.
Packit ae235b
 *
Packit ae235b
 * Activation requested:
Packit ae235b
 *   OFFLINE: make the Start call and transition to PENDING
Packit ae235b
 *   PENDING: do nothing -- call is already in progress.
Packit ae235b
 *   ONLINE: this should not be possible
Packit ae235b
 *
Packit ae235b
 * Deactivation requested:
Packit ae235b
 *   OFFLINE: this should not be possible
Packit ae235b
 *   PENDING: do nothing -- handle it when the Start call finishes
Packit ae235b
 *   ONLINE: send the Stop call and move to OFFLINE immediately
Packit ae235b
 *
Packit ae235b
 * Start call finishes:
Packit ae235b
 *   OFFLINE: this should not be possible
Packit ae235b
 *   PENDING:
Packit ae235b
 *     If we should be active (ie: active count > 0): move to ONLINE
Packit ae235b
 *     If not: send Stop call and move to OFFLINE immediately
Packit ae235b
 *   ONLINE: this should not be possible
Packit ae235b
 *
Packit ae235b
 * We have to take care with regards to signal subscriptions (ie:
Packit ae235b
 * activation of the GDBusMenuPath).  The signal subscription is always
Packit ae235b
 * established when transitioning from OFFLINE to PENDING and taken down
Packit ae235b
 * when transitioning to OFFLINE (from either PENDING or ONLINE).
Packit ae235b
 *
Packit ae235b
 * Since there are two places where we transition to OFFLINE, we split
Packit ae235b
 * that code out into a separate function.
Packit ae235b
 */
Packit ae235b
static void
Packit ae235b
g_dbus_menu_group_go_offline (GDBusMenuGroup *group)
Packit ae235b
{
Packit ae235b
  g_dbus_menu_path_deactivate (group->path);
Packit ae235b
  g_dbus_connection_call (group->path->id->connection,
Packit ae235b
                          group->path->id->bus_name,
Packit ae235b
                          group->path->id->object_path,
Packit ae235b
                          "org.gtk.Menus", "End",
Packit ae235b
                          g_variant_new_parsed ("([ %u ],)", group->id),
Packit ae235b
                          NULL, G_DBUS_CALL_FLAGS_NONE, -1,
Packit ae235b
                          NULL, NULL, NULL);
Packit ae235b
  group->state = GROUP_OFFLINE;
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_dbus_menu_group_start_ready (GObject      *source_object,
Packit ae235b
                               GAsyncResult *result,
Packit ae235b
                               gpointer      user_data)
Packit ae235b
{
Packit ae235b
  GDBusConnection *connection = G_DBUS_CONNECTION (source_object);
Packit ae235b
  GDBusMenuGroup *group = user_data;
Packit ae235b
  GVariant *reply;
Packit ae235b
Packit ae235b
  g_assert (group->state == GROUP_PENDING);
Packit ae235b
Packit ae235b
  reply = g_dbus_connection_call_finish (connection, result, NULL);
Packit ae235b
Packit ae235b
  if (group->active)
Packit ae235b
    {
Packit ae235b
      group->state = GROUP_ONLINE;
Packit ae235b
Packit ae235b
      /* If we receive no reply, just act like we got an empty reply. */
Packit ae235b
      if (reply)
Packit ae235b
        {
Packit ae235b
          GVariantIter *iter;
Packit ae235b
          GVariant *items;
Packit ae235b
          guint group_id;
Packit ae235b
          guint menu_id;
Packit ae235b
Packit ae235b
          g_variant_get (reply, "(a(uuaa{sv}))", &iter);
Packit ae235b
          while (g_variant_iter_loop (iter, "(uu@aa{sv})", &group_id, &menu_id, &items))
Packit ae235b
            if (group_id == group->id)
Packit ae235b
              g_dbus_menu_group_changed (group, menu_id, 0, 0, items);
Packit ae235b
          g_variant_iter_free (iter);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    g_dbus_menu_group_go_offline (group);
Packit ae235b
Packit ae235b
  if (reply)
Packit ae235b
    g_variant_unref (reply);
Packit ae235b
Packit ae235b
  g_dbus_menu_group_unref (group);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_dbus_menu_group_activate (GDBusMenuGroup *group)
Packit ae235b
{
Packit ae235b
  if (group->active++ == 0)
Packit ae235b
    {
Packit ae235b
      g_assert (group->state != GROUP_ONLINE);
Packit ae235b
Packit ae235b
      if (group->state == GROUP_OFFLINE)
Packit ae235b
        {
Packit ae235b
          g_dbus_menu_path_activate (group->path);
Packit ae235b
Packit ae235b
          g_dbus_connection_call (group->path->id->connection,
Packit ae235b
                                  group->path->id->bus_name,
Packit ae235b
                                  group->path->id->object_path,
Packit ae235b
                                  "org.gtk.Menus", "Start",
Packit ae235b
                                  g_variant_new_parsed ("([ %u ],)", group->id),
Packit ae235b
                                  G_VARIANT_TYPE ("(a(uuaa{sv}))"),
Packit ae235b
                                  G_DBUS_CALL_FLAGS_NONE, -1, NULL,
Packit ae235b
                                  g_dbus_menu_group_start_ready,
Packit ae235b
                                  g_dbus_menu_group_ref (group));
Packit ae235b
          group->state = GROUP_PENDING;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_dbus_menu_group_deactivate (GDBusMenuGroup *group)
Packit ae235b
{
Packit ae235b
  if (--group->active == 0)
Packit ae235b
    {
Packit ae235b
      g_assert (group->state != GROUP_OFFLINE);
Packit ae235b
Packit ae235b
      if (group->state == GROUP_ONLINE)
Packit ae235b
        {
Packit ae235b
          /* We are here because nobody is watching, so just free
Packit ae235b
           * everything and don't bother with the notifications.
Packit ae235b
           */
Packit ae235b
          g_hash_table_remove_all (group->menus);
Packit ae235b
Packit ae235b
          g_dbus_menu_group_go_offline (group);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_dbus_menu_group_changed (GDBusMenuGroup *group,
Packit ae235b
                           guint           menu_id,
Packit ae235b
                           gint            position,
Packit ae235b
                           gint            removed,
Packit ae235b
                           GVariant       *added)
Packit ae235b
{
Packit ae235b
  GSequenceIter *point;
Packit ae235b
  GVariantIter iter;
Packit ae235b
  GDBusMenuModel *proxy;
Packit ae235b
  GSequence *items;
Packit ae235b
  GVariant *item;
Packit ae235b
  gint n_added;
Packit ae235b
Packit ae235b
  /* We could have signals coming to us when we're not active (due to
Packit ae235b
   * some other process having subscribed to this group) or when we're
Packit ae235b
   * pending.  In both of those cases, we want to ignore the signal
Packit ae235b
   * since we'll get our own information when we call "Start" for
Packit ae235b
   * ourselves.
Packit ae235b
   */
Packit ae235b
  if (group->state != GROUP_ONLINE)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  items = g_hash_table_lookup (group->menus, GINT_TO_POINTER (menu_id));
Packit ae235b
Packit ae235b
  if (items == NULL)
Packit ae235b
    {
Packit ae235b
      items = g_sequence_new (g_dbus_menu_model_item_free);
Packit ae235b
      g_hash_table_insert (group->menus, GINT_TO_POINTER (menu_id), items);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  point = g_sequence_get_iter_at_pos (items, position + removed);
Packit ae235b
Packit ae235b
  g_return_if_fail (point != NULL);
Packit ae235b
Packit ae235b
  if (removed)
Packit ae235b
    {
Packit ae235b
      GSequenceIter *start;
Packit ae235b
Packit ae235b
      start = g_sequence_get_iter_at_pos (items, position);
Packit ae235b
      g_sequence_remove_range (start, point);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  n_added = g_variant_iter_init (&iter, added);
Packit ae235b
  while (g_variant_iter_loop (&iter, "@a{sv}", &item))
Packit ae235b
    g_sequence_insert_before (point, g_dbus_menu_group_create_item (item));
Packit ae235b
Packit ae235b
  if (g_sequence_is_empty (items))
Packit ae235b
    {
Packit ae235b
      g_hash_table_remove (group->menus, GINT_TO_POINTER (menu_id));
Packit ae235b
      items = NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if ((proxy = g_hash_table_lookup (group->proxies, GINT_TO_POINTER (menu_id))))
Packit ae235b
    g_dbus_menu_model_changed (proxy, items, position, removed, n_added);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GDBusMenuGroup *
Packit ae235b
g_dbus_menu_group_get_from_path (GDBusMenuPath *path,
Packit ae235b
                                 guint          group_id)
Packit ae235b
{
Packit ae235b
  GDBusMenuGroup *group;
Packit ae235b
Packit ae235b
  group = g_hash_table_lookup (path->groups, GINT_TO_POINTER (group_id));
Packit ae235b
Packit ae235b
  if (group == NULL)
Packit ae235b
    {
Packit ae235b
      group = g_slice_new (GDBusMenuGroup);
Packit ae235b
      group->path = g_dbus_menu_path_ref (path);
Packit ae235b
      group->id = group_id;
Packit ae235b
      group->proxies = g_hash_table_new (NULL, NULL);
Packit ae235b
      group->menus = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify) g_sequence_free);
Packit ae235b
      group->state = GROUP_OFFLINE;
Packit ae235b
      group->active = 0;
Packit ae235b
      group->ref_count = 0;
Packit ae235b
Packit ae235b
      g_hash_table_insert (path->groups, GINT_TO_POINTER (group->id), group);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return g_dbus_menu_group_ref (group);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GDBusMenuGroup *
Packit ae235b
g_dbus_menu_group_get (GMainContext    *context,
Packit ae235b
                       GDBusConnection *connection,
Packit ae235b
                       const gchar     *bus_name,
Packit ae235b
                       const gchar     *object_path,
Packit ae235b
                       guint            group_id)
Packit ae235b
{
Packit ae235b
  GDBusMenuGroup *group;
Packit ae235b
  GDBusMenuPath *path;
Packit ae235b
Packit ae235b
  path = g_dbus_menu_path_get (context, connection, bus_name, object_path);
Packit ae235b
  group = g_dbus_menu_group_get_from_path (path, group_id);
Packit ae235b
  g_dbus_menu_path_unref (path);
Packit ae235b
Packit ae235b
  return group;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* GDBusMenuModel {{{1 */
Packit ae235b
Packit ae235b
typedef GMenuModelClass GDBusMenuModelClass;
Packit ae235b
struct _GDBusMenuModel
Packit ae235b
{
Packit ae235b
  GMenuModel parent;
Packit ae235b
Packit ae235b
  GDBusMenuGroup *group;
Packit ae235b
  guint id;
Packit ae235b
Packit ae235b
  GSequence *items; /* unowned */
Packit ae235b
  gboolean active;
Packit ae235b
};
Packit ae235b
Packit ae235b
G_DEFINE_TYPE (GDBusMenuModel, g_dbus_menu_model, G_TYPE_MENU_MODEL)
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_dbus_menu_model_is_mutable (GMenuModel *model)
Packit ae235b
{
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gint
Packit ae235b
g_dbus_menu_model_get_n_items (GMenuModel *model)
Packit ae235b
{
Packit ae235b
  GDBusMenuModel *proxy = G_DBUS_MENU_MODEL (model);
Packit ae235b
Packit ae235b
  if (!proxy->active)
Packit ae235b
    {
Packit ae235b
      g_dbus_menu_group_activate (proxy->group);
Packit ae235b
      proxy->active = TRUE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return proxy->items ? g_sequence_get_length (proxy->items) : 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_dbus_menu_model_get_item_attributes (GMenuModel  *model,
Packit ae235b
                                       gint         item_index,
Packit ae235b
                                       GHashTable **table)
Packit ae235b
{
Packit ae235b
  GDBusMenuModel *proxy = G_DBUS_MENU_MODEL (model);
Packit ae235b
  GDBusMenuModelItem *item;
Packit ae235b
  GSequenceIter *iter;
Packit ae235b
Packit ae235b
  g_return_if_fail (proxy->active);
Packit ae235b
  g_return_if_fail (proxy->items);
Packit ae235b
Packit ae235b
  iter = g_sequence_get_iter_at_pos (proxy->items, item_index);
Packit ae235b
  g_return_if_fail (iter);
Packit ae235b
Packit ae235b
  item = g_sequence_get (iter);
Packit ae235b
  g_return_if_fail (item);
Packit ae235b
Packit ae235b
  *table = g_hash_table_ref (item->attributes);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_dbus_menu_model_get_item_links (GMenuModel  *model,
Packit ae235b
                                  gint         item_index,
Packit ae235b
                                  GHashTable **table)
Packit ae235b
{
Packit ae235b
  GDBusMenuModel *proxy = G_DBUS_MENU_MODEL (model);
Packit ae235b
  GDBusMenuModelItem *item;
Packit ae235b
  GSequenceIter *iter;
Packit ae235b
Packit ae235b
  g_return_if_fail (proxy->active);
Packit ae235b
  g_return_if_fail (proxy->items);
Packit ae235b
Packit ae235b
  iter = g_sequence_get_iter_at_pos (proxy->items, item_index);
Packit ae235b
  g_return_if_fail (iter);
Packit ae235b
Packit ae235b
  item = g_sequence_get (iter);
Packit ae235b
  g_return_if_fail (item);
Packit ae235b
Packit ae235b
  *table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
Packit ae235b
Packit ae235b
  {
Packit ae235b
    GHashTableIter tmp;
Packit ae235b
    gpointer key;
Packit ae235b
    gpointer value;
Packit ae235b
Packit ae235b
    g_hash_table_iter_init (&tmp, item->links);
Packit ae235b
    while (g_hash_table_iter_next (&tmp, &key, &value))
Packit ae235b
      {
Packit ae235b
        if (g_variant_is_of_type (value, G_VARIANT_TYPE ("(uu)")))
Packit ae235b
          {
Packit ae235b
            guint group_id, menu_id;
Packit ae235b
            GDBusMenuGroup *group;
Packit ae235b
            GDBusMenuModel *link;
Packit ae235b
Packit ae235b
            g_variant_get (value, "(uu)", &group_id, &menu_id);
Packit ae235b
Packit ae235b
            /* save the hash lookup in a relatively common case */
Packit ae235b
            if (proxy->group->id != group_id)
Packit ae235b
              group = g_dbus_menu_group_get_from_path (proxy->group->path, group_id);
Packit ae235b
            else
Packit ae235b
              group = g_dbus_menu_group_ref (proxy->group);
Packit ae235b
Packit ae235b
            link = g_dbus_menu_model_get_from_group (group, menu_id);
Packit ae235b
Packit ae235b
            g_hash_table_insert (*table, g_strdup (key), link);
Packit ae235b
Packit ae235b
            g_dbus_menu_group_unref (group);
Packit ae235b
          }
Packit ae235b
      }
Packit ae235b
  }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_dbus_menu_model_finalize (GObject *object)
Packit ae235b
{
Packit ae235b
  GDBusMenuModel *proxy = G_DBUS_MENU_MODEL (object);
Packit ae235b
Packit ae235b
  if (proxy->active)
Packit ae235b
    g_dbus_menu_group_deactivate (proxy->group);
Packit ae235b
Packit ae235b
  g_hash_table_remove (proxy->group->proxies, GINT_TO_POINTER (proxy->id));
Packit ae235b
  g_dbus_menu_group_unref (proxy->group);
Packit ae235b
Packit ae235b
  G_OBJECT_CLASS (g_dbus_menu_model_parent_class)
Packit ae235b
    ->finalize (object);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_dbus_menu_model_init (GDBusMenuModel *proxy)
Packit ae235b
{
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_dbus_menu_model_class_init (GDBusMenuModelClass *class)
Packit ae235b
{
Packit ae235b
  GObjectClass *object_class = G_OBJECT_CLASS (class);
Packit ae235b
Packit ae235b
  class->is_mutable = g_dbus_menu_model_is_mutable;
Packit ae235b
  class->get_n_items = g_dbus_menu_model_get_n_items;
Packit ae235b
  class->get_item_attributes = g_dbus_menu_model_get_item_attributes;
Packit ae235b
  class->get_item_links = g_dbus_menu_model_get_item_links;
Packit ae235b
Packit ae235b
  object_class->finalize = g_dbus_menu_model_finalize;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_dbus_menu_model_changed (GDBusMenuModel *proxy,
Packit ae235b
                           GSequence      *items,
Packit ae235b
                           gint            position,
Packit ae235b
                           gint            removed,
Packit ae235b
                           gint            added)
Packit ae235b
{
Packit ae235b
  proxy->items = items;
Packit ae235b
Packit ae235b
  if (proxy->active && (removed || added))
Packit ae235b
    g_menu_model_items_changed (G_MENU_MODEL (proxy), position, removed, added);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GDBusMenuModel *
Packit ae235b
g_dbus_menu_model_get_from_group (GDBusMenuGroup *group,
Packit ae235b
                                  guint           menu_id)
Packit ae235b
{
Packit ae235b
  GDBusMenuModel *proxy;
Packit ae235b
Packit ae235b
  proxy = g_hash_table_lookup (group->proxies, GINT_TO_POINTER (menu_id));
Packit ae235b
  if (proxy)
Packit ae235b
    g_object_ref (proxy);
Packit ae235b
Packit ae235b
  if (proxy == NULL)
Packit ae235b
    {
Packit ae235b
      proxy = g_object_new (G_TYPE_DBUS_MENU_MODEL, NULL);
Packit ae235b
      proxy->items = g_hash_table_lookup (group->menus, GINT_TO_POINTER (menu_id));
Packit ae235b
      g_hash_table_insert (group->proxies, GINT_TO_POINTER (menu_id), proxy);
Packit ae235b
      proxy->group = g_dbus_menu_group_ref (group);
Packit ae235b
      proxy->id = menu_id;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return proxy;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_dbus_menu_model_get:
Packit ae235b
 * @connection: a #GDBusConnection
Packit ae235b
 * @bus_name: (nullable): the bus name which exports the menu model
Packit ae235b
 *     or %NULL if @connection is not a message bus connection
Packit ae235b
 * @object_path: the object path at which the menu model is exported
Packit ae235b
 *
Packit ae235b
 * Obtains a #GDBusMenuModel for the menu model which is exported
Packit ae235b
 * at the given @bus_name and @object_path.
Packit ae235b
 *
Packit ae235b
 * The thread default main context is taken at the time of this call.
Packit ae235b
 * All signals on the menu model (and any linked models) are reported
Packit ae235b
 * with respect to this context.  All calls on the returned menu model
Packit ae235b
 * (and linked models) must also originate from this same context, with
Packit ae235b
 * the thread default main context unchanged.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): a #GDBusMenuModel object. Free with
Packit ae235b
 *     g_object_unref().
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 */
Packit ae235b
GDBusMenuModel *
Packit ae235b
g_dbus_menu_model_get (GDBusConnection *connection,
Packit ae235b
                       const gchar     *bus_name,
Packit ae235b
                       const gchar     *object_path)
Packit ae235b
{
Packit ae235b
  GDBusMenuGroup *group;
Packit ae235b
  GDBusMenuModel *proxy;
Packit ae235b
  GMainContext *context;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (bus_name != NULL || g_dbus_connection_get_unique_name (connection) == NULL, NULL);
Packit ae235b
Packit ae235b
  context = g_main_context_get_thread_default ();
Packit ae235b
  if (context == NULL)
Packit ae235b
    context = g_main_context_default ();
Packit ae235b
Packit ae235b
  group = g_dbus_menu_group_get (context, connection, bus_name, object_path, 0);
Packit ae235b
  proxy = g_dbus_menu_model_get_from_group (group, 0);
Packit ae235b
  g_dbus_menu_group_unref (group);
Packit ae235b
Packit ae235b
  return proxy;
Packit ae235b
}
Packit ae235b
Packit ae235b
#if 0
Packit ae235b
static void
Packit ae235b
dump_proxy (gpointer key, gpointer value, gpointer data)
Packit ae235b
{
Packit ae235b
  GDBusMenuModel *proxy = value;
Packit ae235b
Packit ae235b
  g_print ("    menu %d refcount %d active %d\n",
Packit ae235b
           proxy->id, G_OBJECT (proxy)->ref_count, proxy->active);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
dump_group (gpointer key, gpointer value, gpointer data)
Packit ae235b
{
Packit ae235b
  GDBusMenuGroup *group = value;
Packit ae235b
Packit ae235b
  g_print ("  group %d refcount %d state %d active %d\n",
Packit ae235b
           group->id, group->ref_count, group->state, group->active);
Packit ae235b
Packit ae235b
  g_hash_table_foreach (group->proxies, dump_proxy, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
dump_path (gpointer key, gpointer value, gpointer data)
Packit ae235b
{
Packit ae235b
  PathIdentifier *pid = key;
Packit ae235b
  GDBusMenuPath *path = value;
Packit ae235b
Packit ae235b
  g_print ("%s active %d\n", pid->object_path, path->active);
Packit ae235b
  g_hash_table_foreach (path->groups, dump_group, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_dbus_menu_model_dump (void)
Packit ae235b
{
Packit ae235b
  g_hash_table_foreach (g_dbus_menu_paths, dump_path, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
#endif
Packit ae235b
Packit ae235b
/* Epilogue {{{1 */
Packit ae235b
/* vim:set foldmethod=marker: */