gdbusmenumodel.c 28.6 KB
Newer Older
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1 2 3
/*
 * Copyright © 2011 Canonical Ltd.
 *
4 5 6 7
 * 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.1 of the License, or (at your option) any later version.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
8 9 10 11 12 13 14
 *
 * 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
15
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
16 17 18 19
 *
 * Author: Ryan Lortie <desrt@desrt.ca>
 */

20 21
#include "config.h"

22
#include "gdbusmenumodel.h"
Allison Karlitskaya's avatar
Allison Karlitskaya committed
23 24 25 26 27 28

#include "gmenumodel.h"

/* Prelude {{{1 */

/**
29 30
 * SECTION:gdbusmenumodel
 * @title: GDBusMenuModel
Allison Karlitskaya's avatar
Allison Karlitskaya committed
31
 * @short_description: A D-Bus GMenuModel implementation
32
 * @include: gio/gio.h
33
 * @see_also: [GMenuModel Exporter][gio-GMenuModel-exporter]
Allison Karlitskaya's avatar
Allison Karlitskaya committed
34
 *
35
 * #GDBusMenuModel is an implementation of #GMenuModel that can be used
Allison Karlitskaya's avatar
Allison Karlitskaya committed
36
 * as a proxy for a menu model that is exported over D-Bus with
Matthias Clasen's avatar
Matthias Clasen committed
37
 * g_dbus_connection_export_menu_model().
Allison Karlitskaya's avatar
Allison Karlitskaya committed
38 39
 */

40 41 42 43 44 45 46
/**
 * GDBusMenuModel:
 *
 * #GDBusMenuModel is an opaque data structure and can only be accessed
 * using the following functions.
 */

Allison Karlitskaya's avatar
Allison Karlitskaya committed
47 48 49
/*
 * There are 3 main (quasi-)classes involved here:
 *
50 51 52
 *   - GDBusMenuPath
 *   - GDBusMenuGroup
 *   - GDBusMenuModel
Allison Karlitskaya's avatar
Allison Karlitskaya committed
53 54 55 56
 *
 * Each of these classes exists as a parameterised singleton keyed to a
 * particular thing:
 *
57
 *   - GDBusMenuPath represents a D-Bus object path on a particular
58 59
 *     unique bus name on a particular GDBusConnection and in a
 *     particular GMainContext.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
60
 *
61 62
 *   - GDBusMenuGroup represents a particular group on a particular
 *     GDBusMenuPath.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
63
 *
64 65
 *   - GDBusMenuModel represents a particular menu within a particular
 *     GDBusMenuGroup.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
66 67 68 69
 *
 * There are also two (and a half) utility structs:
 *
 *  - PathIdentifier and ConstPathIdentifier
70
 *  - GDBusMenuModelItem
Allison Karlitskaya's avatar
Allison Karlitskaya committed
71
 *
72 73 74 75
 * PathIdentifier is the 4-tuple of (GMainContext, GDBusConnection,
 * unique name, object path) that uniquely identifies a particular
 * GDBusMenuPath.  It holds ownership on each of these things, so we
 * have a ConstPathIdentifier variant that does not.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
76 77 78
 *
 * We have a 3-level hierarchy of hashtables:
 *
79 80
 *   - a global hashtable (g_dbus_menu_paths) maps from PathIdentifier
 *     to GDBusMenuPath
Allison Karlitskaya's avatar
Allison Karlitskaya committed
81
 *
82 83
 *   - each GDBusMenuPath has a hashtable mapping from guint (group
 *     number) to GDBusMenuGroup
Allison Karlitskaya's avatar
Allison Karlitskaya committed
84
 *
85 86
 *   - each GDBusMenuGroup has a hashtable mapping from guint (menu
 *     number) to GDBusMenuModel.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
87 88
 *
 * In this way, each quintuplet of (connection, bus name, object path,
89
 * group id, menu id) maps to a single GDBusMenuModel instance that can be
Allison Karlitskaya's avatar
Allison Karlitskaya committed
90 91
 * located via 3 hashtable lookups.
 *
92 93
 * All of the 3 classes are refcounted (GDBusMenuPath and
 * GDBusMenuGroup manually, and GDBusMenuModel by virtue of being a
Allison Karlitskaya's avatar
Allison Karlitskaya committed
94 95 96
 * GObject).  The hashtables do not hold references -- rather, when the
 * last reference is dropped, the object is removed from the hashtable.
 *
97
 * The hard references go in the other direction: GDBusMenuModel is created
Allison Karlitskaya's avatar
Allison Karlitskaya committed
98
 * as the user requests it and only exists as long as the user holds a
99 100 101
 * reference on it.  GDBusMenuModel holds a reference on the GDBusMenuGroup
 * from which it came. GDBusMenuGroup holds a reference on
 * GDBusMenuPath.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
102 103
 *
 * In addition to refcounts, each object has an 'active' variable (ints
104
 * for GDBusMenuPath and GDBusMenuGroup, boolean for GDBusMenuModel).
Allison Karlitskaya's avatar
Allison Karlitskaya committed
105
 *
106
 *   - GDBusMenuModel is inactive when created and becomes active only when
Allison Karlitskaya's avatar
Allison Karlitskaya committed
107
 *     first queried for information.  This prevents extra work from
108
 *     happening just by someone acquiring a GDBusMenuModel (and not
Allison Karlitskaya's avatar
Allison Karlitskaya committed
109 110
 *     actually trying to display it yet).
 *
111 112
 *   - The active count on GDBusMenuGroup is equal to the number of
 *     GDBusMenuModel instances in that group that are active.  When the
Allison Karlitskaya's avatar
Allison Karlitskaya committed
113 114 115 116 117
 *     active count transitions from 0 to 1, the group calls the 'Start'
 *     method on the service to begin monitoring that group.  When it
 *     drops from 1 to 0, the group calls the 'End' method to stop
 *     monitoring.
 *
118 119
 *   - The active count on GDBusMenuPath is equal to the number of
 *     GDBusMenuGroup instances on that path with a non-zero active
Allison Karlitskaya's avatar
Allison Karlitskaya committed
120 121 122 123 124
 *     count.  When the active count transitions from 0 to 1, the path
 *     sets up a signal subscription to monitor any changes.  The signal
 *     subscription is taken down when the active count transitions from
 *     1 to 0.
 *
125
 * When active, GDBusMenuPath gets incoming signals when changes occur.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
126
 * If the change signal mentions a group for which we currently have an
127
 * active GDBusMenuGroup, the change signal is passed along to that
Allison Karlitskaya's avatar
Allison Karlitskaya committed
128 129
 * group.  If the group is inactive, the change signal is ignored.
 *
130 131 132
 * Most of the "work" occurs in GDBusMenuGroup.  In addition to the
 * hashtable of GDBusMenuModel instances, it keeps a hashtable of the actual
 * menu contents, each encoded as GSequence of GDBusMenuModelItem.  It
Allison Karlitskaya's avatar
Allison Karlitskaya committed
133 134 135
 * initially populates this table with the results of the "Start" method
 * call and then updates it according to incoming change signals.  If
 * the change signal mentions a menu for which we current have an active
136 137
 * GDBusMenuModel, the change signal is passed along to that model.  If the
 * model is inactive, the change signal is ignored.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
138
 *
139
 * GDBusMenuModelItem is just a pair of hashtables, one for the attributes
140 141
 * and one for the links of the item.  Both map strings to GVariant
 * instances.  In the case of links, the GVariant has type '(uu)' and is
142
 * turned into a GDBusMenuModel at the point that the user pulls it through
143
 * the API.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
144 145
 *
 * Following the "empty is the same as non-existent" rule, the hashtable
146
 * of GSequence of GDBusMenuModelItem holds NULL for empty menus.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
147
 *
148 149 150
 * GDBusMenuModel contains very little functionality of its own.  It holds a
 * (weak) reference to the GSequence of GDBusMenuModelItem contained in the
 * GDBusMenuGroup.  It uses this GSequence to implement the GMenuModel
Allison Karlitskaya's avatar
Allison Karlitskaya committed
151 152 153 154
 * interface.  It also emits the "items-changed" signal if it is active
 * and it was told that the contents of the GSequence changed.
 */

155 156
typedef struct _GDBusMenuGroup GDBusMenuGroup;
typedef struct _GDBusMenuPath GDBusMenuPath;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
157

158
static void                     g_dbus_menu_group_changed               (GDBusMenuGroup  *group,
Allison Karlitskaya's avatar
Allison Karlitskaya committed
159 160 161 162
                                                                         guint            menu_id,
                                                                         gint             position,
                                                                         gint             removed,
                                                                         GVariant        *added);
163
static void                     g_dbus_menu_model_changed               (GDBusMenuModel  *proxy,
Allison Karlitskaya's avatar
Allison Karlitskaya committed
164 165 166 167
                                                                         GSequence       *items,
                                                                         gint             position,
                                                                         gint             removed,
                                                                         gint             added);
168
static GDBusMenuGroup *         g_dbus_menu_group_get_from_path         (GDBusMenuPath   *path,
Allison Karlitskaya's avatar
Allison Karlitskaya committed
169
                                                                         guint            group_id);
170
static GDBusMenuModel *         g_dbus_menu_model_get_from_group        (GDBusMenuGroup  *group,
Allison Karlitskaya's avatar
Allison Karlitskaya committed
171 172 173 174 175
                                                                         guint            menu_id);

/* PathIdentifier {{{1 */
typedef struct
{
176
  GMainContext *context;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
177 178 179 180 181 182 183
  GDBusConnection *connection;
  gchar *bus_name;
  gchar *object_path;
} PathIdentifier;

typedef const struct
{
184
  GMainContext *context;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
  GDBusConnection *connection;
  const gchar *bus_name;
  const gchar *object_path;
} ConstPathIdentifier;

static guint
path_identifier_hash (gconstpointer data)
{
  ConstPathIdentifier *id = data;

  return g_str_hash (id->object_path);
}

static gboolean
path_identifier_equal (gconstpointer a,
                       gconstpointer b)
{
  ConstPathIdentifier *id_a = a;
  ConstPathIdentifier *id_b = b;

  return id_a->connection == id_b->connection &&
206
         g_strcmp0 (id_a->bus_name, id_b->bus_name) == 0 &&
Allison Karlitskaya's avatar
Allison Karlitskaya committed
207 208 209 210 211 212
         g_str_equal (id_a->object_path, id_b->object_path);
}

static void
path_identifier_free (PathIdentifier *id)
{
213
  g_main_context_unref (id->context);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
214 215 216 217 218 219 220 221 222 223 224 225 226
  g_object_unref (id->connection);
  g_free (id->bus_name);
  g_free (id->object_path);

  g_slice_free (PathIdentifier, id);
}

static PathIdentifier *
path_identifier_new (ConstPathIdentifier *cid)
{
  PathIdentifier *id;

  id = g_slice_new (PathIdentifier);
227
  id->context = g_main_context_ref (cid->context);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
228 229 230 231 232 233 234
  id->connection = g_object_ref (cid->connection);
  id->bus_name = g_strdup (cid->bus_name);
  id->object_path = g_strdup (cid->object_path);

  return id;
}

235
/* GDBusMenuPath {{{1 */
Allison Karlitskaya's avatar
Allison Karlitskaya committed
236

237
struct _GDBusMenuPath
Allison Karlitskaya's avatar
Allison Karlitskaya committed
238 239 240 241 242 243 244 245 246
{
  PathIdentifier *id;
  gint ref_count;

  GHashTable *groups;
  gint active;
  guint watch_id;
};

247
static GHashTable *g_dbus_menu_paths;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
248

249 250
static GDBusMenuPath *
g_dbus_menu_path_ref (GDBusMenuPath *path)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
251 252 253 254 255 256 257
{
  path->ref_count++;

  return path;
}

static void
258
g_dbus_menu_path_unref (GDBusMenuPath *path)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
259 260 261
{
  if (--path->ref_count == 0)
    {
262
      g_hash_table_remove (g_dbus_menu_paths, path->id);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
263 264 265
      g_hash_table_unref (path->groups);
      path_identifier_free (path->id);

266
      g_slice_free (GDBusMenuPath, path);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
267 268 269 270
    }
}

static void
271 272 273 274 275 276 277 278 279
g_dbus_menu_path_signal (GDBusConnection *connection,
                         const gchar     *sender_name,
                         const gchar     *object_path,
                         const gchar     *interface_name,
                         const gchar     *signal_name,
                         GVariant        *parameters,
                         gpointer         user_data)
{
  GDBusMenuPath *path = user_data;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
280 281 282 283 284 285 286 287 288 289 290 291 292
  GVariantIter *iter;
  guint group_id;
  guint menu_id;
  guint position;
  guint removes;
  GVariant *adds;

  if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(a(uuuuaa{sv}))")))
    return;

  g_variant_get (parameters, "(a(uuuuaa{sv}))", &iter);
  while (g_variant_iter_loop (iter, "(uuuu@aa{sv})", &group_id, &menu_id, &position, &removes, &adds))
    {
293
      GDBusMenuGroup *group;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
294 295 296 297

      group = g_hash_table_lookup (path->groups, GINT_TO_POINTER (group_id));

      if (group != NULL)
298
        g_dbus_menu_group_changed (group, menu_id, position, removes, adds);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
299 300 301 302 303
    }
  g_variant_iter_free (iter);
}

static void
304
g_dbus_menu_path_activate (GDBusMenuPath *path)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
305 306 307 308 309
{
  if (path->active++ == 0)
    path->watch_id = g_dbus_connection_signal_subscribe (path->id->connection, path->id->bus_name,
                                                         "org.gtk.Menus", "Changed", path->id->object_path,
                                                         NULL, G_DBUS_SIGNAL_FLAGS_NONE,
310
                                                         g_dbus_menu_path_signal, path, NULL);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
311 312 313
}

static void
314
g_dbus_menu_path_deactivate (GDBusMenuPath *path)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
315 316 317 318 319
{
  if (--path->active == 0)
    g_dbus_connection_signal_unsubscribe (path->id->connection, path->watch_id);
}

320
static GDBusMenuPath *
321 322
g_dbus_menu_path_get (GMainContext    *context,
                      GDBusConnection *connection,
323 324
                      const gchar     *bus_name,
                      const gchar     *object_path)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
325
{
326
  ConstPathIdentifier cid = { context, connection, bus_name, object_path };
327
  GDBusMenuPath *path;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
328

329 330
  if (g_dbus_menu_paths == NULL)
    g_dbus_menu_paths = g_hash_table_new (path_identifier_hash, path_identifier_equal);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
331

332
  path = g_hash_table_lookup (g_dbus_menu_paths, &cid);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
333 334 335

  if (path == NULL)
    {
336
      path = g_slice_new (GDBusMenuPath);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
337 338 339 340 341
      path->id = path_identifier_new (&cid);
      path->groups = g_hash_table_new (NULL, NULL);
      path->ref_count = 0;
      path->active = 0;

342
      g_hash_table_insert (g_dbus_menu_paths, path->id, path);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
343 344
    }

345
  return g_dbus_menu_path_ref (path);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
346 347
}

348
/* GDBusMenuGroup, GDBusMenuModelItem {{{1 */
Allison Karlitskaya's avatar
Allison Karlitskaya committed
349 350 351 352 353 354 355
typedef enum
{
  GROUP_OFFLINE,
  GROUP_PENDING,
  GROUP_ONLINE
} GroupStatus;

356
struct _GDBusMenuGroup
Allison Karlitskaya's avatar
Allison Karlitskaya committed
357
{
358
  GDBusMenuPath *path;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
359 360
  guint id;

361
  GHashTable *proxies; /* uint -> unowned GDBusMenuModel */
Allison Karlitskaya's avatar
Allison Karlitskaya committed
362 363 364 365 366 367 368 369 370 371
  GHashTable *menus;   /* uint -> owned GSequence */
  gint ref_count;
  GroupStatus state;
  gint active;
};

typedef struct
{
  GHashTable *attributes;
  GHashTable *links;
372
} GDBusMenuModelItem;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
373

374 375
static GDBusMenuGroup *
g_dbus_menu_group_ref (GDBusMenuGroup *group)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
376 377 378 379 380 381 382
{
  group->ref_count++;

  return group;
}

static void
383
g_dbus_menu_group_unref (GDBusMenuGroup *group)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
384 385 386 387 388 389 390 391
{
  if (--group->ref_count == 0)
    {
      g_assert (group->state == GROUP_OFFLINE);
      g_assert (group->active == 0);

      g_hash_table_remove (group->path->groups, GINT_TO_POINTER (group->id));
      g_hash_table_unref (group->proxies);
392
      g_hash_table_unref (group->menus);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
393

394
      g_dbus_menu_path_unref (group->path);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
395

396
      g_slice_free (GDBusMenuGroup, group);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
397 398 399 400
    }
}

static void
401
g_dbus_menu_model_item_free (gpointer data)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
402
{
403
  GDBusMenuModelItem *item = data;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
404 405 406 407

  g_hash_table_unref (item->attributes);
  g_hash_table_unref (item->links);

408
  g_slice_free (GDBusMenuModelItem, item);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
409 410
}

411 412
static GDBusMenuModelItem *
g_dbus_menu_group_create_item (GVariant *description)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
413
{
414
  GDBusMenuModelItem *item;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
415 416 417 418
  GVariantIter iter;
  const gchar *key;
  GVariant *value;

419
  item = g_slice_new (GDBusMenuModelItem);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
420
  item->attributes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_variant_unref);
421
  item->links = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_variant_unref);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
422 423 424 425

  g_variant_iter_init (&iter, description);
  while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
    if (key[0] == ':')
426 427
      /* key + 1 to skip the ':' */
      g_hash_table_insert (item->links, g_strdup (key + 1), g_variant_ref (value));
Allison Karlitskaya's avatar
Allison Karlitskaya committed
428 429 430 431 432 433 434
    else
      g_hash_table_insert (item->attributes, g_strdup (key), g_variant_ref (value));

  return item;
}

/*
435
 * GDBusMenuGroup can be in three states:
Allison Karlitskaya's avatar
Allison Karlitskaya committed
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
 *
 * OFFLINE: not subscribed to this group
 * PENDING: we made the call to subscribe to this group, but the result
 *          has not come back yet
 * ONLINE: we are fully subscribed
 *
 * We can get into some nasty situations where we make a call due to an
 * activation request but receive a deactivation request before the call
 * returns.  If another activation request occurs then we could risk
 * sending a Start request even though one is already in progress.  For
 * this reason, we have to carefully consider what to do in each of the
 * three states for each of the following situations:
 *
 *  - activation requested
 *  - deactivation requested
 *  - Start call finishes
 *
 * To simplify things a bit, we do not have a callback for the Stop
 * call.  We just send it and assume that it takes effect immediately.
 *
 * Activation requested:
 *   OFFLINE: make the Start call and transition to PENDING
 *   PENDING: do nothing -- call is already in progress.
 *   ONLINE: this should not be possible
 *
 * Deactivation requested:
 *   OFFLINE: this should not be possible
 *   PENDING: do nothing -- handle it when the Start call finishes
 *   ONLINE: send the Stop call and move to OFFLINE immediately
 *
 * Start call finishes:
 *   OFFLINE: this should not be possible
 *   PENDING:
 *     If we should be active (ie: active count > 0): move to ONLINE
 *     If not: send Stop call and move to OFFLINE immediately
 *   ONLINE: this should not be possible
 *
 * We have to take care with regards to signal subscriptions (ie:
474
 * activation of the GDBusMenuPath).  The signal subscription is always
Allison Karlitskaya's avatar
Allison Karlitskaya committed
475
 * established when transitioning from OFFLINE to PENDING and taken down
Matthias Clasen's avatar
Matthias Clasen committed
476
 * when transitioning to OFFLINE (from either PENDING or ONLINE).
Allison Karlitskaya's avatar
Allison Karlitskaya committed
477 478 479 480 481
 *
 * Since there are two places where we transition to OFFLINE, we split
 * that code out into a separate function.
 */
static void
482
g_dbus_menu_group_go_offline (GDBusMenuGroup *group)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
483
{
484
  g_dbus_menu_path_deactivate (group->path);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
485 486 487 488 489 490 491 492 493 494 495 496
  g_dbus_connection_call (group->path->id->connection,
                          group->path->id->bus_name,
                          group->path->id->object_path,
                          "org.gtk.Menus", "End",
                          g_variant_new_parsed ("([ %u ],)", group->id),
                          NULL, G_DBUS_CALL_FLAGS_NONE, -1,
                          NULL, NULL, NULL);
  group->state = GROUP_OFFLINE;
}


static void
497 498 499
g_dbus_menu_group_start_ready (GObject      *source_object,
                               GAsyncResult *result,
                               gpointer      user_data)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
500 501
{
  GDBusConnection *connection = G_DBUS_CONNECTION (source_object);
502
  GDBusMenuGroup *group = user_data;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
  GVariant *reply;

  g_assert (group->state == GROUP_PENDING);

  reply = g_dbus_connection_call_finish (connection, result, NULL);

  if (group->active)
    {
      group->state = GROUP_ONLINE;

      /* If we receive no reply, just act like we got an empty reply. */
      if (reply)
        {
          GVariantIter *iter;
          GVariant *items;
          guint group_id;
          guint menu_id;

          g_variant_get (reply, "(a(uuaa{sv}))", &iter);
          while (g_variant_iter_loop (iter, "(uu@aa{sv})", &group_id, &menu_id, &items))
            if (group_id == group->id)
524
              g_dbus_menu_group_changed (group, menu_id, 0, 0, items);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
525 526 527 528
          g_variant_iter_free (iter);
        }
    }
  else
529
    g_dbus_menu_group_go_offline (group);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
530 531 532 533

  if (reply)
    g_variant_unref (reply);

534
  g_dbus_menu_group_unref (group);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
535 536 537
}

static void
538
g_dbus_menu_group_activate (GDBusMenuGroup *group)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
539 540 541 542 543 544 545
{
  if (group->active++ == 0)
    {
      g_assert (group->state != GROUP_ONLINE);

      if (group->state == GROUP_OFFLINE)
        {
546
          g_dbus_menu_path_activate (group->path);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
547 548 549 550 551 552 553 554

          g_dbus_connection_call (group->path->id->connection,
                                  group->path->id->bus_name,
                                  group->path->id->object_path,
                                  "org.gtk.Menus", "Start",
                                  g_variant_new_parsed ("([ %u ],)", group->id),
                                  G_VARIANT_TYPE ("(a(uuaa{sv}))"),
                                  G_DBUS_CALL_FLAGS_NONE, -1, NULL,
555 556
                                  g_dbus_menu_group_start_ready,
                                  g_dbus_menu_group_ref (group));
Allison Karlitskaya's avatar
Allison Karlitskaya committed
557 558 559 560 561 562
          group->state = GROUP_PENDING;
        }
    }
}

static void
563
g_dbus_menu_group_deactivate (GDBusMenuGroup *group)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
564 565 566 567 568 569 570 571 572 573 574 575
{
  if (--group->active == 0)
    {
      g_assert (group->state != GROUP_OFFLINE);

      if (group->state == GROUP_ONLINE)
        {
          /* We are here because nobody is watching, so just free
           * everything and don't bother with the notifications.
           */
          g_hash_table_remove_all (group->menus);

576
          g_dbus_menu_group_go_offline (group);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
577 578 579 580 581
        }
    }
}

static void
582 583 584 585 586
g_dbus_menu_group_changed (GDBusMenuGroup *group,
                           guint           menu_id,
                           gint            position,
                           gint            removed,
                           GVariant       *added)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
587 588 589
{
  GSequenceIter *point;
  GVariantIter iter;
590
  GDBusMenuModel *proxy;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
  GSequence *items;
  GVariant *item;
  gint n_added;

  /* We could have signals coming to us when we're not active (due to
   * some other process having subscribed to this group) or when we're
   * pending.  In both of those cases, we want to ignore the signal
   * since we'll get our own information when we call "Start" for
   * ourselves.
   */
  if (group->state != GROUP_ONLINE)
    return;

  items = g_hash_table_lookup (group->menus, GINT_TO_POINTER (menu_id));

  if (items == NULL)
    {
608
      items = g_sequence_new (g_dbus_menu_model_item_free);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
      g_hash_table_insert (group->menus, GINT_TO_POINTER (menu_id), items);
    }

  point = g_sequence_get_iter_at_pos (items, position + removed);

  g_return_if_fail (point != NULL);

  if (removed)
    {
      GSequenceIter *start;

      start = g_sequence_get_iter_at_pos (items, position);
      g_sequence_remove_range (start, point);
    }

  n_added = g_variant_iter_init (&iter, added);
  while (g_variant_iter_loop (&iter, "@a{sv}", &item))
626
    g_sequence_insert_before (point, g_dbus_menu_group_create_item (item));
Allison Karlitskaya's avatar
Allison Karlitskaya committed
627

628
  if (g_sequence_is_empty (items))
Allison Karlitskaya's avatar
Allison Karlitskaya committed
629 630 631 632 633 634
    {
      g_hash_table_remove (group->menus, GINT_TO_POINTER (menu_id));
      items = NULL;
    }

  if ((proxy = g_hash_table_lookup (group->proxies, GINT_TO_POINTER (menu_id))))
635
    g_dbus_menu_model_changed (proxy, items, position, removed, n_added);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
636 637
}

638 639 640
static GDBusMenuGroup *
g_dbus_menu_group_get_from_path (GDBusMenuPath *path,
                                 guint          group_id)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
641
{
642
  GDBusMenuGroup *group;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
643 644 645 646 647

  group = g_hash_table_lookup (path->groups, GINT_TO_POINTER (group_id));

  if (group == NULL)
    {
648 649
      group = g_slice_new (GDBusMenuGroup);
      group->path = g_dbus_menu_path_ref (path);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
650 651 652 653 654 655 656 657 658 659
      group->id = group_id;
      group->proxies = g_hash_table_new (NULL, NULL);
      group->menus = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify) g_sequence_free);
      group->state = GROUP_OFFLINE;
      group->active = 0;
      group->ref_count = 0;

      g_hash_table_insert (path->groups, GINT_TO_POINTER (group->id), group);
    }

660
  return g_dbus_menu_group_ref (group);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
661 662
}

663
static GDBusMenuGroup *
664 665
g_dbus_menu_group_get (GMainContext    *context,
                       GDBusConnection *connection,
666 667 668
                       const gchar     *bus_name,
                       const gchar     *object_path,
                       guint            group_id)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
669
{
670 671
  GDBusMenuGroup *group;
  GDBusMenuPath *path;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
672

673
  path = g_dbus_menu_path_get (context, connection, bus_name, object_path);
674 675
  group = g_dbus_menu_group_get_from_path (path, group_id);
  g_dbus_menu_path_unref (path);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
676 677 678 679

  return group;
}

680
/* GDBusMenuModel {{{1 */
Allison Karlitskaya's avatar
Allison Karlitskaya committed
681

682 683
typedef GMenuModelClass GDBusMenuModelClass;
struct _GDBusMenuModel
Allison Karlitskaya's avatar
Allison Karlitskaya committed
684 685 686
{
  GMenuModel parent;

687
  GDBusMenuGroup *group;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
688 689 690 691 692 693
  guint id;

  GSequence *items; /* unowned */
  gboolean active;
};

694
G_DEFINE_TYPE (GDBusMenuModel, g_dbus_menu_model, G_TYPE_MENU_MODEL)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
695 696

static gboolean
697
g_dbus_menu_model_is_mutable (GMenuModel *model)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
698 699 700 701 702
{
  return TRUE;
}

static gint
703
g_dbus_menu_model_get_n_items (GMenuModel *model)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
704
{
705
  GDBusMenuModel *proxy = G_DBUS_MENU_MODEL (model);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
706 707 708

  if (!proxy->active)
    {
709
      g_dbus_menu_group_activate (proxy->group);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
710 711 712 713 714 715 716
      proxy->active = TRUE;
    }

  return proxy->items ? g_sequence_get_length (proxy->items) : 0;
}

static void
717 718 719
g_dbus_menu_model_get_item_attributes (GMenuModel  *model,
                                       gint         item_index,
                                       GHashTable **table)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
720
{
721 722
  GDBusMenuModel *proxy = G_DBUS_MENU_MODEL (model);
  GDBusMenuModelItem *item;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
  GSequenceIter *iter;

  g_return_if_fail (proxy->active);
  g_return_if_fail (proxy->items);

  iter = g_sequence_get_iter_at_pos (proxy->items, item_index);
  g_return_if_fail (iter);

  item = g_sequence_get (iter);
  g_return_if_fail (item);

  *table = g_hash_table_ref (item->attributes);
}

static void
738 739 740
g_dbus_menu_model_get_item_links (GMenuModel  *model,
                                  gint         item_index,
                                  GHashTable **table)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
741
{
742 743
  GDBusMenuModel *proxy = G_DBUS_MENU_MODEL (model);
  GDBusMenuModelItem *item;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
744 745 746 747 748 749 750 751 752 753 754
  GSequenceIter *iter;

  g_return_if_fail (proxy->active);
  g_return_if_fail (proxy->items);

  iter = g_sequence_get_iter_at_pos (proxy->items, item_index);
  g_return_if_fail (iter);

  item = g_sequence_get (iter);
  g_return_if_fail (item);

755 756 757 758 759 760 761 762 763 764 765 766 767
  *table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);

  {
    GHashTableIter tmp;
    gpointer key;
    gpointer value;

    g_hash_table_iter_init (&tmp, item->links);
    while (g_hash_table_iter_next (&tmp, &key, &value))
      {
        if (g_variant_is_of_type (value, G_VARIANT_TYPE ("(uu)")))
          {
            guint group_id, menu_id;
768 769
            GDBusMenuGroup *group;
            GDBusMenuModel *link;
770 771 772 773 774

            g_variant_get (value, "(uu)", &group_id, &menu_id);

            /* save the hash lookup in a relatively common case */
            if (proxy->group->id != group_id)
775
              group = g_dbus_menu_group_get_from_path (proxy->group->path, group_id);
776
            else
777
              group = g_dbus_menu_group_ref (proxy->group);
778

779
            link = g_dbus_menu_model_get_from_group (group, menu_id);
780 781 782

            g_hash_table_insert (*table, g_strdup (key), link);

783
            g_dbus_menu_group_unref (group);
784 785 786
          }
      }
  }
Allison Karlitskaya's avatar
Allison Karlitskaya committed
787 788 789
}

static void
790
g_dbus_menu_model_finalize (GObject *object)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
791
{
792
  GDBusMenuModel *proxy = G_DBUS_MENU_MODEL (object);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
793 794

  if (proxy->active)
795
    g_dbus_menu_group_deactivate (proxy->group);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
796 797

  g_hash_table_remove (proxy->group->proxies, GINT_TO_POINTER (proxy->id));
798
  g_dbus_menu_group_unref (proxy->group);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
799

800
  G_OBJECT_CLASS (g_dbus_menu_model_parent_class)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
801 802 803 804
    ->finalize (object);
}

static void
805
g_dbus_menu_model_init (GDBusMenuModel *proxy)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
806 807 808 809
{
}

static void
810
g_dbus_menu_model_class_init (GDBusMenuModelClass *class)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
811 812 813
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);

814 815 816 817
  class->is_mutable = g_dbus_menu_model_is_mutable;
  class->get_n_items = g_dbus_menu_model_get_n_items;
  class->get_item_attributes = g_dbus_menu_model_get_item_attributes;
  class->get_item_links = g_dbus_menu_model_get_item_links;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
818

819
  object_class->finalize = g_dbus_menu_model_finalize;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
820 821 822
}

static void
823 824 825 826 827
g_dbus_menu_model_changed (GDBusMenuModel *proxy,
                           GSequence      *items,
                           gint            position,
                           gint            removed,
                           gint            added)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
828 829 830 831 832 833 834
{
  proxy->items = items;

  if (proxy->active && (removed || added))
    g_menu_model_items_changed (G_MENU_MODEL (proxy), position, removed, added);
}

835 836 837
static GDBusMenuModel *
g_dbus_menu_model_get_from_group (GDBusMenuGroup *group,
                                  guint           menu_id)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
838
{
839
  GDBusMenuModel *proxy;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
840 841 842 843 844 845 846

  proxy = g_hash_table_lookup (group->proxies, GINT_TO_POINTER (menu_id));
  if (proxy)
    g_object_ref (proxy);

  if (proxy == NULL)
    {
847
      proxy = g_object_new (G_TYPE_DBUS_MENU_MODEL, NULL);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
848 849
      proxy->items = g_hash_table_lookup (group->menus, GINT_TO_POINTER (menu_id));
      g_hash_table_insert (group->proxies, GINT_TO_POINTER (menu_id), proxy);
850
      proxy->group = g_dbus_menu_group_ref (group);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
851 852 853 854 855 856 857
      proxy->id = menu_id;
    }

  return proxy;
}

/**
858
 * g_dbus_menu_model_get:
Allison Karlitskaya's avatar
Allison Karlitskaya committed
859
 * @connection: a #GDBusConnection
860 861
 * @bus_name: (nullable): the bus name which exports the menu model
 *     or %NULL if @connection is not a message bus connection
Allison Karlitskaya's avatar
Allison Karlitskaya committed
862 863
 * @object_path: the object path at which the menu model is exported
 *
864
 * Obtains a #GDBusMenuModel for the menu model which is exported
Allison Karlitskaya's avatar
Allison Karlitskaya committed
865 866
 * at the given @bus_name and @object_path.
 *
867 868 869 870 871 872
 * The thread default main context is taken at the time of this call.
 * All signals on the menu model (and any linked models) are reported
 * with respect to this context.  All calls on the returned menu model
 * (and linked models) must also originate from this same context, with
 * the thread default main context unchanged.
 *
Matthias Clasen's avatar
Matthias Clasen committed
873 874 875 876
 * Returns: (transfer full): a #GDBusMenuModel object. Free with
 *     g_object_unref().
 *
 * Since: 2.32
Allison Karlitskaya's avatar
Allison Karlitskaya committed
877
 */
878 879 880 881
GDBusMenuModel *
g_dbus_menu_model_get (GDBusConnection *connection,
                       const gchar     *bus_name,
                       const gchar     *object_path)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
882
{
883 884
  GDBusMenuGroup *group;
  GDBusMenuModel *proxy;
885 886
  GMainContext *context;

887 888
  g_return_val_if_fail (bus_name != NULL || g_dbus_connection_get_unique_name (connection) == NULL, NULL);

889 890 891
  context = g_main_context_get_thread_default ();
  if (context == NULL)
    context = g_main_context_default ();
Allison Karlitskaya's avatar
Allison Karlitskaya committed
892

893
  group = g_dbus_menu_group_get (context, connection, bus_name, object_path, 0);
894 895
  proxy = g_dbus_menu_model_get_from_group (group, 0);
  g_dbus_menu_group_unref (group);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
896 897 898 899

  return proxy;
}

900
#if 0
Matthias Clasen's avatar
Matthias Clasen committed
901 902 903
static void
dump_proxy (gpointer key, gpointer value, gpointer data)
{
904
  GDBusMenuModel *proxy = value;
Matthias Clasen's avatar
Matthias Clasen committed
905 906 907 908 909 910 911 912

  g_print ("    menu %d refcount %d active %d\n",
           proxy->id, G_OBJECT (proxy)->ref_count, proxy->active);
}

static void
dump_group (gpointer key, gpointer value, gpointer data)
{
913
  GDBusMenuGroup *group = value;
Matthias Clasen's avatar
Matthias Clasen committed
914 915 916 917 918 919 920 921 922 923 924

  g_print ("  group %d refcount %d state %d active %d\n",
           group->id, group->ref_count, group->state, group->active);

  g_hash_table_foreach (group->proxies, dump_proxy, NULL);
}

static void
dump_path (gpointer key, gpointer value, gpointer data)
{
  PathIdentifier *pid = key;
925
  GDBusMenuPath *path = value;
Matthias Clasen's avatar
Matthias Clasen committed
926 927 928 929 930 931

  g_print ("%s active %d\n", pid->object_path, path->active);
  g_hash_table_foreach (path->groups, dump_group, NULL);
}

void
932
g_dbus_menu_model_dump (void)
Matthias Clasen's avatar
Matthias Clasen committed
933
{
934
  g_hash_table_foreach (g_dbus_menu_paths, dump_path, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
935 936
}

937
#endif
Matthias Clasen's avatar
Matthias Clasen committed
938

Allison Karlitskaya's avatar
Allison Karlitskaya committed
939 940
/* Epilogue {{{1 */
/* vim:set foldmethod=marker: */