gdbusmenumodel.c 28.4 KB
Newer Older
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright © 2011 Canonical Ltd.
 *
 * 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
 * licence, 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: Ryan Lortie <desrt@desrt.ca>
 */

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 32 33
 * @short_description: A D-Bus GMenuModel implementation
 * @see_also: <link linkend="gio-GMenuModel-exporter">GMenuModel Exporter</link>
 *
34
 * #GDBusMenuModel is an implementation of #GMenuModel that can be used
Allison Karlitskaya's avatar
Allison Karlitskaya committed
35
 * as a proxy for a menu model that is exported over D-Bus with
Matthias Clasen's avatar
Matthias Clasen committed
36
 * g_dbus_connection_export_menu_model().
Allison Karlitskaya's avatar
Allison Karlitskaya committed
37 38 39 40 41
 */

/*
 * There are 3 main (quasi-)classes involved here:
 *
42 43 44
 *   - GDBusMenuPath
 *   - GDBusMenuGroup
 *   - GDBusMenuModel
Allison Karlitskaya's avatar
Allison Karlitskaya committed
45 46 47 48
 *
 * Each of these classes exists as a parameterised singleton keyed to a
 * particular thing:
 *
49
 *   - GDBusMenuPath represents a D-Bus object path on a particular
50 51
 *     unique bus name on a particular GDBusConnection and in a
 *     particular GMainContext.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
52
 *
53 54
 *   - GDBusMenuGroup represents a particular group on a particular
 *     GDBusMenuPath.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
55
 *
56 57
 *   - GDBusMenuModel represents a particular menu within a particular
 *     GDBusMenuGroup.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
58 59 60 61
 *
 * There are also two (and a half) utility structs:
 *
 *  - PathIdentifier and ConstPathIdentifier
62
 *  - GDBusMenuModelItem
Allison Karlitskaya's avatar
Allison Karlitskaya committed
63
 *
64 65 66 67
 * 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
68 69 70
 *
 * We have a 3-level hierarchy of hashtables:
 *
71 72
 *   - a global hashtable (g_dbus_menu_paths) maps from PathIdentifier
 *     to GDBusMenuPath
Allison Karlitskaya's avatar
Allison Karlitskaya committed
73
 *
74 75
 *   - each GDBusMenuPath has a hashtable mapping from guint (group
 *     number) to GDBusMenuGroup
Allison Karlitskaya's avatar
Allison Karlitskaya committed
76
 *
77 78
 *   - each GDBusMenuGroup has a hashtable mapping from guint (menu
 *     number) to GDBusMenuModel.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
79 80
 *
 * In this way, each quintuplet of (connection, bus name, object path,
81
 * group id, menu id) maps to a single GDBusMenuModel instance that can be
Allison Karlitskaya's avatar
Allison Karlitskaya committed
82 83
 * located via 3 hashtable lookups.
 *
84 85
 * 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
86 87 88
 * GObject).  The hashtables do not hold references -- rather, when the
 * last reference is dropped, the object is removed from the hashtable.
 *
89
 * The hard references go in the other direction: GDBusMenuModel is created
Allison Karlitskaya's avatar
Allison Karlitskaya committed
90
 * as the user requests it and only exists as long as the user holds a
91 92 93
 * 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
94 95
 *
 * In addition to refcounts, each object has an 'active' variable (ints
96
 * for GDBusMenuPath and GDBusMenuGroup, boolean for GDBusMenuModel).
Allison Karlitskaya's avatar
Allison Karlitskaya committed
97
 *
98
 *   - GDBusMenuModel is inactive when created and becomes active only when
Allison Karlitskaya's avatar
Allison Karlitskaya committed
99
 *     first queried for information.  This prevents extra work from
100
 *     happening just by someone acquiring a GDBusMenuModel (and not
Allison Karlitskaya's avatar
Allison Karlitskaya committed
101 102
 *     actually trying to display it yet).
 *
103 104
 *   - 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
105 106 107 108 109
 *     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.
 *
110 111
 *   - 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
112 113 114 115 116
 *     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.
 *
117
 * When active, GDBusMenuPath gets incoming signals when changes occur.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
118
 * If the change signal mentions a group for which we currently have an
119
 * active GDBusMenuGroup, the change signal is passed along to that
Allison Karlitskaya's avatar
Allison Karlitskaya committed
120 121
 * group.  If the group is inactive, the change signal is ignored.
 *
122 123 124
 * 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
125 126 127
 * 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
128 129
 * 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
130
 *
131
 * GDBusMenuModelItem is just a pair of hashtables, one for the attributes
132 133
 * 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
134
 * turned into a GDBusMenuModel at the point that the user pulls it through
135
 * the API.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
136 137
 *
 * Following the "empty is the same as non-existent" rule, the hashtable
138
 * of GSequence of GDBusMenuModelItem holds NULL for empty menus.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
139
 *
140 141 142
 * 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
143 144 145 146
 * interface.  It also emits the "items-changed" signal if it is active
 * and it was told that the contents of the GSequence changed.
 */

147 148
typedef struct _GDBusMenuGroup GDBusMenuGroup;
typedef struct _GDBusMenuPath GDBusMenuPath;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
149

150
static void                     g_dbus_menu_group_changed               (GDBusMenuGroup  *group,
Allison Karlitskaya's avatar
Allison Karlitskaya committed
151 152 153 154
                                                                         guint            menu_id,
                                                                         gint             position,
                                                                         gint             removed,
                                                                         GVariant        *added);
155
static void                     g_dbus_menu_model_changed               (GDBusMenuModel  *proxy,
Allison Karlitskaya's avatar
Allison Karlitskaya committed
156 157 158 159
                                                                         GSequence       *items,
                                                                         gint             position,
                                                                         gint             removed,
                                                                         gint             added);
160
static GDBusMenuGroup *         g_dbus_menu_group_get_from_path         (GDBusMenuPath   *path,
Allison Karlitskaya's avatar
Allison Karlitskaya committed
161
                                                                         guint            group_id);
162
static GDBusMenuModel *         g_dbus_menu_model_get_from_group        (GDBusMenuGroup  *group,
Allison Karlitskaya's avatar
Allison Karlitskaya committed
163 164 165 166 167
                                                                         guint            menu_id);

/* PathIdentifier {{{1 */
typedef struct
{
168
  GMainContext *context;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
169 170 171 172 173 174 175
  GDBusConnection *connection;
  gchar *bus_name;
  gchar *object_path;
} PathIdentifier;

typedef const struct
{
176
  GMainContext *context;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
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
  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 &&
         g_str_equal (id_a->bus_name, id_b->bus_name) &&
         g_str_equal (id_a->object_path, id_b->object_path);
}

static void
path_identifier_free (PathIdentifier *id)
{
205
  g_main_context_unref (id->context);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
206 207 208 209 210 211 212 213 214 215 216 217 218
  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);
219
  id->context = g_main_context_ref (cid->context);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
220 221 222 223 224 225 226
  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;
}

227
/* GDBusMenuPath {{{1 */
Allison Karlitskaya's avatar
Allison Karlitskaya committed
228

229
struct _GDBusMenuPath
Allison Karlitskaya's avatar
Allison Karlitskaya committed
230 231 232 233 234 235 236 237 238
{
  PathIdentifier *id;
  gint ref_count;

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

239
static GHashTable *g_dbus_menu_paths;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
240

241 242
static GDBusMenuPath *
g_dbus_menu_path_ref (GDBusMenuPath *path)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
243 244 245 246 247 248 249
{
  path->ref_count++;

  return path;
}

static void
250
g_dbus_menu_path_unref (GDBusMenuPath *path)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
251 252 253
{
  if (--path->ref_count == 0)
    {
254
      g_hash_table_remove (g_dbus_menu_paths, path->id);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
255 256 257
      g_hash_table_unref (path->groups);
      path_identifier_free (path->id);

258
      g_slice_free (GDBusMenuPath, path);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
259 260 261 262
    }
}

static void
263 264 265 266 267 268 269 270 271
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
272 273 274 275 276 277 278 279 280 281 282 283 284
  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))
    {
285
      GDBusMenuGroup *group;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
286 287 288 289

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

      if (group != NULL)
290
        g_dbus_menu_group_changed (group, menu_id, position, removes, adds);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
291 292 293 294 295
    }
  g_variant_iter_free (iter);
}

static void
296
g_dbus_menu_path_activate (GDBusMenuPath *path)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
297 298 299 300 301
{
  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,
302
                                                         g_dbus_menu_path_signal, path, NULL);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
303 304 305
}

static void
306
g_dbus_menu_path_deactivate (GDBusMenuPath *path)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
307 308 309 310 311
{
  if (--path->active == 0)
    g_dbus_connection_signal_unsubscribe (path->id->connection, path->watch_id);
}

312
static GDBusMenuPath *
313 314
g_dbus_menu_path_get (GMainContext    *context,
                      GDBusConnection *connection,
315 316
                      const gchar     *bus_name,
                      const gchar     *object_path)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
317
{
318
  ConstPathIdentifier cid = { context, connection, bus_name, object_path };
319
  GDBusMenuPath *path;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
320

321 322
  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
323

324
  path = g_hash_table_lookup (g_dbus_menu_paths, &cid);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
325 326 327

  if (path == NULL)
    {
328
      path = g_slice_new (GDBusMenuPath);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
329 330 331 332 333
      path->id = path_identifier_new (&cid);
      path->groups = g_hash_table_new (NULL, NULL);
      path->ref_count = 0;
      path->active = 0;

334
      g_hash_table_insert (g_dbus_menu_paths, path->id, path);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
335 336
    }

337
  return g_dbus_menu_path_ref (path);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
338 339
}

340
/* GDBusMenuGroup, GDBusMenuModelItem {{{1 */
Allison Karlitskaya's avatar
Allison Karlitskaya committed
341 342 343 344 345 346 347
typedef enum
{
  GROUP_OFFLINE,
  GROUP_PENDING,
  GROUP_ONLINE
} GroupStatus;

348
struct _GDBusMenuGroup
Allison Karlitskaya's avatar
Allison Karlitskaya committed
349
{
350
  GDBusMenuPath *path;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
351 352
  guint id;

353
  GHashTable *proxies; /* uint -> unowned GDBusMenuModel */
Allison Karlitskaya's avatar
Allison Karlitskaya committed
354 355 356 357 358 359 360 361 362 363
  GHashTable *menus;   /* uint -> owned GSequence */
  gint ref_count;
  GroupStatus state;
  gint active;
};

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

366 367
static GDBusMenuGroup *
g_dbus_menu_group_ref (GDBusMenuGroup *group)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
368 369 370 371 372 373 374
{
  group->ref_count++;

  return group;
}

static void
375
g_dbus_menu_group_unref (GDBusMenuGroup *group)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
376 377 378 379 380 381 382 383
{
  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);
384
      g_hash_table_unref (group->menus);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
385

386
      g_dbus_menu_path_unref (group->path);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
387

388
      g_slice_free (GDBusMenuGroup, group);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
389 390 391 392
    }
}

static void
393
g_dbus_menu_model_item_free (gpointer data)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
394
{
395
  GDBusMenuModelItem *item = data;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
396 397 398 399

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

400
  g_slice_free (GDBusMenuModelItem, item);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
401 402
}

403 404
static GDBusMenuModelItem *
g_dbus_menu_group_create_item (GVariant *description)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
405
{
406
  GDBusMenuModelItem *item;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
407 408 409 410
  GVariantIter iter;
  const gchar *key;
  GVariant *value;

411
  item = g_slice_new (GDBusMenuModelItem);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
412
  item->attributes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_variant_unref);
413
  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
414 415 416 417

  g_variant_iter_init (&iter, description);
  while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
    if (key[0] == ':')
418 419
      /* 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
420 421 422 423 424 425 426
    else
      g_hash_table_insert (item->attributes, g_strdup (key), g_variant_ref (value));

  return item;
}

/*
427
 * GDBusMenuGroup can be in three states:
Allison Karlitskaya's avatar
Allison Karlitskaya committed
428 429 430 431 432 433 434 435 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
 *
 * 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:
466
 * activation of the GDBusMenuPath).  The signal subscription is always
Allison Karlitskaya's avatar
Allison Karlitskaya committed
467
 * established when transitioning from OFFLINE to PENDING and taken down
Matthias Clasen's avatar
Matthias Clasen committed
468
 * when transitioning to OFFLINE (from either PENDING or ONLINE).
Allison Karlitskaya's avatar
Allison Karlitskaya committed
469 470 471 472 473
 *
 * Since there are two places where we transition to OFFLINE, we split
 * that code out into a separate function.
 */
static void
474
g_dbus_menu_group_go_offline (GDBusMenuGroup *group)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
475
{
476
  g_dbus_menu_path_deactivate (group->path);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
477 478 479 480 481 482 483 484 485 486 487 488
  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
489 490 491
g_dbus_menu_group_start_ready (GObject      *source_object,
                               GAsyncResult *result,
                               gpointer      user_data)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
492 493
{
  GDBusConnection *connection = G_DBUS_CONNECTION (source_object);
494
  GDBusMenuGroup *group = user_data;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
  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)
516
              g_dbus_menu_group_changed (group, menu_id, 0, 0, items);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
517 518 519 520
          g_variant_iter_free (iter);
        }
    }
  else
521
    g_dbus_menu_group_go_offline (group);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
522 523 524 525

  if (reply)
    g_variant_unref (reply);

526
  g_dbus_menu_group_unref (group);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
527 528 529
}

static void
530
g_dbus_menu_group_activate (GDBusMenuGroup *group)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
531 532 533 534 535 536 537
{
  if (group->active++ == 0)
    {
      g_assert (group->state != GROUP_ONLINE);

      if (group->state == GROUP_OFFLINE)
        {
538
          g_dbus_menu_path_activate (group->path);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
539 540 541 542 543 544 545 546

          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,
547 548
                                  g_dbus_menu_group_start_ready,
                                  g_dbus_menu_group_ref (group));
Allison Karlitskaya's avatar
Allison Karlitskaya committed
549 550 551 552 553 554
          group->state = GROUP_PENDING;
        }
    }
}

static void
555
g_dbus_menu_group_deactivate (GDBusMenuGroup *group)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
556 557 558 559 560 561 562 563 564 565 566 567
{
  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);

568
          g_dbus_menu_group_go_offline (group);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
569 570 571 572 573
        }
    }
}

static void
574 575 576 577 578
g_dbus_menu_group_changed (GDBusMenuGroup *group,
                           guint           menu_id,
                           gint            position,
                           gint            removed,
                           GVariant       *added)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
579 580 581
{
  GSequenceIter *point;
  GVariantIter iter;
582
  GDBusMenuModel *proxy;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
  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)
    {
600
      items = g_sequence_new (g_dbus_menu_model_item_free);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
      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))
618
    g_sequence_insert_before (point, g_dbus_menu_group_create_item (item));
Allison Karlitskaya's avatar
Allison Karlitskaya committed
619 620 621 622 623 624 625 626

  if (g_sequence_get_length (items) == 0)
    {
      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))))
627
    g_dbus_menu_model_changed (proxy, items, position, removed, n_added);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
628 629
}

630 631 632
static GDBusMenuGroup *
g_dbus_menu_group_get_from_path (GDBusMenuPath *path,
                                 guint          group_id)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
633
{
634
  GDBusMenuGroup *group;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
635 636 637 638 639

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

  if (group == NULL)
    {
640 641
      group = g_slice_new (GDBusMenuGroup);
      group->path = g_dbus_menu_path_ref (path);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
642 643 644 645 646 647 648 649 650 651
      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);
    }

652
  return g_dbus_menu_group_ref (group);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
653 654
}

655
static GDBusMenuGroup *
656 657
g_dbus_menu_group_get (GMainContext    *context,
                       GDBusConnection *connection,
658 659 660
                       const gchar     *bus_name,
                       const gchar     *object_path,
                       guint            group_id)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
661
{
662 663
  GDBusMenuGroup *group;
  GDBusMenuPath *path;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
664

665
  path = g_dbus_menu_path_get (context, connection, bus_name, object_path);
666 667
  group = g_dbus_menu_group_get_from_path (path, group_id);
  g_dbus_menu_path_unref (path);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
668 669 670 671

  return group;
}

672
/* GDBusMenuModel {{{1 */
Allison Karlitskaya's avatar
Allison Karlitskaya committed
673

674 675
typedef GMenuModelClass GDBusMenuModelClass;
struct _GDBusMenuModel
Allison Karlitskaya's avatar
Allison Karlitskaya committed
676 677 678
{
  GMenuModel parent;

679
  GDBusMenuGroup *group;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
680 681 682 683 684 685
  guint id;

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

686
G_DEFINE_TYPE (GDBusMenuModel, g_dbus_menu_model, G_TYPE_MENU_MODEL)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
687 688

static gboolean
689
g_dbus_menu_model_is_mutable (GMenuModel *model)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
690 691 692 693 694
{
  return TRUE;
}

static gint
695
g_dbus_menu_model_get_n_items (GMenuModel *model)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
696
{
697
  GDBusMenuModel *proxy = G_DBUS_MENU_MODEL (model);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
698 699 700

  if (!proxy->active)
    {
701
      g_dbus_menu_group_activate (proxy->group);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
702 703 704 705 706 707 708
      proxy->active = TRUE;
    }

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

static void
709 710 711
g_dbus_menu_model_get_item_attributes (GMenuModel  *model,
                                       gint         item_index,
                                       GHashTable **table)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
712
{
713 714
  GDBusMenuModel *proxy = G_DBUS_MENU_MODEL (model);
  GDBusMenuModelItem *item;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
  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
730 731 732
g_dbus_menu_model_get_item_links (GMenuModel  *model,
                                  gint         item_index,
                                  GHashTable **table)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
733
{
734 735
  GDBusMenuModel *proxy = G_DBUS_MENU_MODEL (model);
  GDBusMenuModelItem *item;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
736 737 738 739 740 741 742 743 744 745 746
  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);

747 748 749 750 751 752 753 754 755 756 757 758 759
  *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;
760 761
            GDBusMenuGroup *group;
            GDBusMenuModel *link;
762 763 764 765 766

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

            /* save the hash lookup in a relatively common case */
            if (proxy->group->id != group_id)
767
              group = g_dbus_menu_group_get_from_path (proxy->group->path, group_id);
768
            else
769
              group = g_dbus_menu_group_ref (proxy->group);
770

771
            link = g_dbus_menu_model_get_from_group (group, menu_id);
772 773 774

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

775
            g_dbus_menu_group_unref (group);
776 777 778
          }
      }
  }
Allison Karlitskaya's avatar
Allison Karlitskaya committed
779 780 781
}

static void
782
g_dbus_menu_model_finalize (GObject *object)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
783
{
784
  GDBusMenuModel *proxy = G_DBUS_MENU_MODEL (object);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
785 786

  if (proxy->active)
787
    g_dbus_menu_group_deactivate (proxy->group);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
788 789

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

792
  G_OBJECT_CLASS (g_dbus_menu_model_parent_class)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
793 794 795 796
    ->finalize (object);
}

static void
797
g_dbus_menu_model_init (GDBusMenuModel *proxy)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
798 799 800 801
{
}

static void
802
g_dbus_menu_model_class_init (GDBusMenuModelClass *class)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
803 804 805
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);

806 807 808 809
  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
810

811
  object_class->finalize = g_dbus_menu_model_finalize;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
812 813 814
}

static void
815 816 817 818 819
g_dbus_menu_model_changed (GDBusMenuModel *proxy,
                           GSequence      *items,
                           gint            position,
                           gint            removed,
                           gint            added)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
820 821 822 823 824 825 826
{
  proxy->items = items;

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

827 828 829
static GDBusMenuModel *
g_dbus_menu_model_get_from_group (GDBusMenuGroup *group,
                                  guint           menu_id)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
830
{
831
  GDBusMenuModel *proxy;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
832 833 834 835 836 837 838

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

  if (proxy == NULL)
    {
839
      proxy = g_object_new (G_TYPE_DBUS_MENU_MODEL, NULL);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
840 841
      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);
842
      proxy->group = g_dbus_menu_group_ref (group);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
843 844 845 846 847 848 849
      proxy->id = menu_id;
    }

  return proxy;
}

/**
850
 * g_dbus_menu_model_get:
Allison Karlitskaya's avatar
Allison Karlitskaya committed
851 852 853 854
 * @connection: a #GDBusConnection
 * @bus_name: the bus name which exports the menu model
 * @object_path: the object path at which the menu model is exported
 *
855
 * Obtains a #GDBusMenuModel for the menu model which is exported
Allison Karlitskaya's avatar
Allison Karlitskaya committed
856 857
 * at the given @bus_name and @object_path.
 *
858 859 860 861 862 863
 * 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
864 865 866 867
 * Returns: (transfer full): a #GDBusMenuModel object. Free with
 *     g_object_unref().
 *
 * Since: 2.32
Allison Karlitskaya's avatar
Allison Karlitskaya committed
868
 */
869 870 871 872
GDBusMenuModel *
g_dbus_menu_model_get (GDBusConnection *connection,
                       const gchar     *bus_name,
                       const gchar     *object_path)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
873
{
874 875
  GDBusMenuGroup *group;
  GDBusMenuModel *proxy;
876 877 878 879 880
  GMainContext *context;

  context = g_main_context_get_thread_default ();
  if (context == NULL)
    context = g_main_context_default ();
Allison Karlitskaya's avatar
Allison Karlitskaya committed
881

882
  group = g_dbus_menu_group_get (context, connection, bus_name, object_path, 0);
883 884
  proxy = g_dbus_menu_model_get_from_group (group, 0);
  g_dbus_menu_group_unref (group);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
885 886 887 888

  return proxy;
}

889
#if 0
Matthias Clasen's avatar
Matthias Clasen committed
890 891 892
static void
dump_proxy (gpointer key, gpointer value, gpointer data)
{
893
  GDBusMenuModel *proxy = value;
Matthias Clasen's avatar
Matthias Clasen committed
894 895 896 897 898 899 900 901

  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)
{
902
  GDBusMenuGroup *group = value;
Matthias Clasen's avatar
Matthias Clasen committed
903 904 905 906 907 908 909 910 911 912 913

  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;
914
  GDBusMenuPath *path = value;
Matthias Clasen's avatar
Matthias Clasen committed
915 916 917 918 919 920

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

void
921
g_dbus_menu_model_dump (void)
Matthias Clasen's avatar
Matthias Clasen committed
922
{
923
  g_hash_table_foreach (g_dbus_menu_paths, dump_path, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
924 925
}

926
#endif
Matthias Clasen's avatar
Matthias Clasen committed
927

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