gedit-message-bus.c 33.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * gedit-message-bus.h
 * This file is part of gedit
 *
 * Copyright (C) 2008-2010 - Jesse van den Kieboom
 *
 * gedit is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * gedit is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gedit; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

23 24 25 26 27 28 29 30 31 32
#include "gedit-message-bus.h"

#include <string.h>
#include <stdarg.h>
#include <gobject/gvaluecollector.h>

/**
 * GeditMessageCallback:
 * @bus: the #GeditMessageBus on which the message was sent
 * @message: the #GeditMessage which was sent
Garrett Regier's avatar
Garrett Regier committed
33
 * @user_data: the supplied user data when connecting the callback
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
 *
 * Callback signature used for connecting callback functions to be called
 * when a message is received (see gedit_message_bus_connect()).
 *
 */

/**
 * SECTION:gedit-message-bus
 * @short_description: internal message communication bus
 * @include: gedit/gedit-message-bus.h
 *
 * gedit has a communication bus very similar to DBus. Its primary use is to
 * allow easy communication between plugins, but it can also be used to expose
 * gedit functionality to external applications by providing DBus bindings for
 * the internal gedit message bus.
 *
 * There are two different communication busses available. The default bus
 * (see gedit_message_bus_get_default()) is an application wide communication
 * bus. In addition, each #GeditWindow has a separate, private bus
 * (see gedit_window_get_message_bus()). This makes it easier for plugins to
 * communicate to other plugins in the same window.
 *
 * The concept of the message bus is very simple. You can register a message
 * type on the bus, specified as a Method at a specific Object Path with a
 * certain set of Method Arguments. You can then connect callback functions
 * for this message type on the bus. Whenever a message with the Object Path
 * and Method for which callbacks are connected is sent over the bus, the
 * callbacks are called. There is no distinction between Methods and Signals
 * (signals are simply messages where sender and receiver have switched places).
 *
 * <example>
 * <title>Registering a message type</title>
 * <programlisting>
 * GeditMessageBus *bus = gedit_message_bus_get_default ();
 *
 * // Register 'method' at '/plugins/example' with one required
 * // string argument 'arg1'
Garrett Regier's avatar
Garrett Regier committed
71 72
 * gedit_message_bus_register (bus, EXAMPLE_TYPE_METHOD_MESSAGE,
 *                             "/plugins/example", "method");
73 74 75 76 77 78 79 80
 * </programlisting>
 * </example>
 * <example>
 * <title>Connecting a callback</title>
 * <programlisting>
 * static void
 * example_method_cb (GeditMessageBus *bus,
 *                    GeditMessage    *message,
Garrett Regier's avatar
Garrett Regier committed
81
 *                    gpointer         user_data)
82 83
 * {
 * 	gchar *arg1 = NULL;
84
 *
85 86 87 88 89 90
 * 	gedit_message_get (message, "arg1", &arg1, NULL);
 * 	g_message ("Evoked /plugins/example.method with: %s", arg1);
 * 	g_free (arg1);
 * }
 *
 * GeditMessageBus *bus = gedit_message_bus_get_default ();
91 92
 *
 * guint id = gedit_message_bus_connect (bus,
93 94 95 96
 *                                       "/plugins/example", "method",
 *                                       example_method_cb,
 *                                       NULL,
 *                                       NULL);
97
 *
98 99 100 101 102 103 104
 * </programlisting>
 * </example>
 * <example>
 * <title>Sending a message</title>
 * <programlisting>
 * GeditMessageBus *bus = gedit_message_bus_get_default ();
 *
105 106 107
 * gedit_message_bus_send (bus,
 *                         "/plugins/example", "method",
 *                         "arg1", "Hello World",
108 109 110
 *                         NULL);
 * </programlisting>
 * </example>
111 112 113
 *
 * Since: 2.25.3
 *
114
 */
115

116 117 118 119 120
typedef struct
{
	gchar *object_path;
	gchar *method;

121 122 123 124 125 126 127
	gchar *identifier;
} MessageIdentifier;

typedef struct
{
	MessageIdentifier *identifier;

128 129 130 131 132 133 134 135 136 137
	GList *listeners;
} Message;

typedef struct
{
	guint id;
	gboolean blocked;

	GDestroyNotify destroy_data;
	GeditMessageCallback callback;
Garrett Regier's avatar
Garrett Regier committed
138
	gpointer user_data;
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
} Listener;

typedef struct
{
	Message *message;
	GList *listener;
} IdMap;

struct _GeditMessageBusPrivate
{
	GHashTable *messages;
	GHashTable *idmap;

	GList *message_queue;
	guint idle_id;

	guint next_id;
156

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
	GHashTable *types; /* mapping from identifier to GeditMessageType */
};

/* signals */
enum
{
	DISPATCH,
	REGISTERED,
	UNREGISTERED,
	LAST_SIGNAL
};

static guint message_bus_signals[LAST_SIGNAL];

static void gedit_message_bus_dispatch_real (GeditMessageBus *bus,
172
                                             GeditMessage    *message);
173

174
G_DEFINE_TYPE_WITH_PRIVATE (GeditMessageBus, gedit_message_bus, G_TYPE_OBJECT)
175

176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
static MessageIdentifier *
message_identifier_new (const gchar *object_path,
                        const gchar *method)
{
	MessageIdentifier *ret;

	ret = g_slice_new (MessageIdentifier);

	ret->object_path = g_strdup (object_path);
	ret->method = g_strdup (method);

	ret->identifier = gedit_message_type_identifier (object_path, method);

	return ret;
}

static void
message_identifier_free (MessageIdentifier *identifier)
{
	g_free (identifier->object_path);
	g_free (identifier->method);
	g_free (identifier->identifier);

	g_slice_free (MessageIdentifier, identifier);
}

static guint
message_identifier_hash (gconstpointer id)
{
	return g_str_hash (((MessageIdentifier *)id)->identifier);
}

static gboolean
message_identifier_equal (gconstpointer id1,
                          gconstpointer id2)
{
	return g_str_equal (((MessageIdentifier *)id1)->identifier,
	                    ((MessageIdentifier *)id2)->identifier);
}

216 217 218 219
static void
listener_free (Listener *listener)
{
	if (listener->destroy_data)
220
	{
Garrett Regier's avatar
Garrett Regier committed
221
		listener->destroy_data (listener->user_data);
222
	}
223

224
	g_slice_free (Listener, listener);
225 226 227 228 229
}

static void
message_free (Message *message)
{
230 231
	message_identifier_free (message->identifier);

232
	g_list_free_full (message->listeners, (GDestroyNotify) listener_free);
233
	g_slice_free (Message, message);
234 235 236 237 238
}

static void
message_queue_free (GList *queue)
{
239
	g_list_free_full (queue, g_object_unref);
240 241 242 243 244 245
}

static void
gedit_message_bus_finalize (GObject *object)
{
	GeditMessageBus *bus = GEDIT_MESSAGE_BUS (object);
246

247
	if (bus->priv->idle_id != 0)
248
	{
249
		g_source_remove (bus->priv->idle_id);
250 251
	}

252 253 254 255 256
	message_queue_free (bus->priv->message_queue);

	g_hash_table_destroy (bus->priv->messages);
	g_hash_table_destroy (bus->priv->idmap);
	g_hash_table_destroy (bus->priv->types);
257

258 259 260 261 262 263 264
	G_OBJECT_CLASS (gedit_message_bus_parent_class)->finalize (object);
}

static void
gedit_message_bus_class_init (GeditMessageBusClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
265

266
	object_class->finalize = gedit_message_bus_finalize;
267

268 269 270 271 272 273 274 275
	klass->dispatch = gedit_message_bus_dispatch_real;

	/**
	 * GeditMessageBus::dispatch:
	 * @bus: a #GeditMessageBus
	 * @message: the #GeditMessage to dispatch
	 *
	 * The "dispatch" signal is emitted when a message is to be dispatched.
276
	 * The message is dispatched in the default handler of this signal.
277 278
	 * Primary use of this signal is to customize the dispatch of a message
	 * (for instance to automatically dispatch all messages over DBus).
279
	 *
280 281
	 */
	message_bus_signals[DISPATCH] =
282 283 284 285
		g_signal_new ("dispatch",
		              G_OBJECT_CLASS_TYPE (object_class),
		              G_SIGNAL_RUN_LAST,
		              G_STRUCT_OFFSET (GeditMessageBusClass, dispatch),
Paolo Borelli's avatar
Paolo Borelli committed
286
		              NULL, NULL, NULL,
287 288 289
		              G_TYPE_NONE,
		              1,
		              GEDIT_TYPE_MESSAGE);
290 291 292 293

	/**
	 * GeditMessageBus::registered:
	 * @bus: a #GeditMessageBus
Garrett Regier's avatar
Garrett Regier committed
294 295
	 * @object_path: the registered object path.
	 * @method: the registered method
296 297 298 299 300 301
	 *
	 * The "registered" signal is emitted when a message has been registered
	 * on the bus.
	 *
	 */
	message_bus_signals[REGISTERED] =
302 303 304 305
		g_signal_new ("registered",
		              G_OBJECT_CLASS_TYPE (object_class),
		              G_SIGNAL_RUN_LAST,
		              G_STRUCT_OFFSET (GeditMessageBusClass, registered),
Paolo Borelli's avatar
Paolo Borelli committed
306
		              NULL, NULL, NULL,
307 308 309 310
		              G_TYPE_NONE,
		              2,
		              G_TYPE_STRING,
		              G_TYPE_STRING);
311 312 313 314

	/**
	 * GeditMessageBus::unregistered:
	 * @bus: a #GeditMessageBus
Garrett Regier's avatar
Garrett Regier committed
315 316
	 * @object_path: the unregistered object path.
	 * @method: the unregistered method
317
	 *
318
	 * The "unregistered" signal is emitted when a message has been
319 320 321 322
	 * unregistered from the bus.
	 *
	 */
	message_bus_signals[UNREGISTERED] =
323 324 325 326
		g_signal_new ("unregistered",
		              G_OBJECT_CLASS_TYPE (object_class),
		              G_SIGNAL_RUN_LAST,
		              G_STRUCT_OFFSET (GeditMessageBusClass, unregistered),
Paolo Borelli's avatar
Paolo Borelli committed
327
		              NULL, NULL, NULL,
328 329 330 331
		              G_TYPE_NONE,
		              2,
		              G_TYPE_STRING,
		              G_TYPE_STRING);
332 333 334 335
}

static Message *
message_new (GeditMessageBus *bus,
336 337
             const gchar     *object_path,
             const gchar     *method)
338
{
339 340 341
	Message *message = g_slice_new (Message);

	message->identifier = message_identifier_new (object_path, method);
342 343
	message->listeners = NULL;

344 345 346 347
	g_hash_table_insert (bus->priv->messages,
	                     message->identifier,
	                     message);

348 349 350 351 352
	return message;
}

static Message *
lookup_message (GeditMessageBus *bus,
353 354 355
                const gchar      *object_path,
                const gchar      *method,
                gboolean          create)
356
{
357
	MessageIdentifier *identifier;
358
	Message *message;
359 360 361 362

	identifier = message_identifier_new (object_path, method);
	message = g_hash_table_lookup (bus->priv->messages, identifier);
	message_identifier_free (identifier);
363 364

	if (!message && !create)
365
	{
366
		return NULL;
367 368
	}

369
	if (!message)
370
	{
371
		message = message_new (bus, object_path, method);
372 373
	}

374 375 376 377 378
	return message;
}

static guint
add_listener (GeditMessageBus      *bus,
379 380 381 382
              Message		   *message,
              GeditMessageCallback  callback,
              gpointer		    user_data,
              GDestroyNotify        destroy_data)
383 384 385
{
	Listener *listener;
	IdMap *idmap;
386 387

	listener = g_slice_new (Listener);
388 389
	listener->id = ++bus->priv->next_id;
	listener->callback = callback;
Garrett Regier's avatar
Garrett Regier committed
390
	listener->user_data = user_data;
391 392 393 394
	listener->blocked = FALSE;
	listener->destroy_data = destroy_data;

	message->listeners = g_list_append (message->listeners, listener);
395

396 397 398 399
	idmap = g_new (IdMap, 1);
	idmap->message = message;
	idmap->listener = g_list_last (message->listeners);

400 401
	g_hash_table_insert (bus->priv->idmap, GINT_TO_POINTER (listener->id), idmap);

402 403 404 405 406
	return listener->id;
}

static void
remove_listener (GeditMessageBus *bus,
407 408
                 Message         *message,
                 GList		 *listener)
409 410
{
	Listener *lst;
411

412
	lst = (Listener *)listener->data;
413

414 415 416 417 418 419
	/* remove from idmap */
	g_hash_table_remove (bus->priv->idmap, GINT_TO_POINTER (lst->id));
	listener_free (lst);

	/* remove from list of listeners */
	message->listeners = g_list_delete_link (message->listeners, listener);
420

421 422 423
	if (!message->listeners)
	{
		/* remove message because it does not have any listeners */
424
		g_hash_table_remove (bus->priv->messages, message->identifier);
425 426 427 428 429
	}
}

static void
block_listener (GeditMessageBus *bus,
430 431
                Message         *message,
                GList           *listener)
432 433
{
	Listener *lst;
434 435

	lst = listener->data;
436 437 438 439 440
	lst->blocked = TRUE;
}

static void
unblock_listener (GeditMessageBus *bus,
441 442
                  Message         *message,
                  GList           *listener)
443 444
{
	Listener *lst;
445 446

	lst = listener->data;
447 448 449 450 451
	lst->blocked = FALSE;
}

static void
dispatch_message_real (GeditMessageBus *bus,
452 453
                       Message         *msg,
                       GeditMessage    *message)
454 455
{
	GList *item;
456

457 458 459
	for (item = msg->listeners; item; item = item->next)
	{
		Listener *listener = (Listener *)item->data;
460

461
		if (!listener->blocked)
462
		{
Garrett Regier's avatar
Garrett Regier committed
463
			listener->callback (bus, message, listener->user_data);
464
		}
465 466 467 468 469
	}
}

static void
gedit_message_bus_dispatch_real (GeditMessageBus *bus,
470
                                 GeditMessage    *message)
471 472 473 474
{
	const gchar *object_path;
	const gchar *method;
	Message *msg;
475

476 477 478
	object_path = gedit_message_get_object_path (message);
	method = gedit_message_get_method (message);

479 480 481
	g_return_if_fail (object_path != NULL);
	g_return_if_fail (method != NULL);

482
	msg = lookup_message (bus, object_path, method, FALSE);
483

484
	if (msg)
485
	{
486
		dispatch_message_real (bus, msg, message);
487
	}
488 489 490 491
}

static void
dispatch_message (GeditMessageBus *bus,
492
                  GeditMessage    *message)
493
{
494
	g_signal_emit (bus, message_bus_signals[DISPATCH], 0, message);
495 496 497 498 499 500 501
}

static gboolean
idle_dispatch (GeditMessageBus *bus)
{
	GList *list;
	GList *item;
502

503 504 505 506 507 508 509
	/* make sure to set idle_id to 0 first so that any new async messages
	   will be queued properly */
	bus->priv->idle_id = 0;

	/* reverse queue to get correct delivery order */
	list = g_list_reverse (bus->priv->message_queue);
	bus->priv->message_queue = NULL;
510

511 512 513
	for (item = list; item; item = item->next)
	{
		GeditMessage *msg = GEDIT_MESSAGE (item->data);
514

515 516
		dispatch_message (bus, msg);
	}
517

518 519 520 521 522 523 524
	message_queue_free (list);
	return FALSE;
}

typedef void (*MatchCallback) (GeditMessageBus *, Message *, GList *);

static void
525 526 527
process_by_id (GeditMessageBus *bus,
               guint            id,
               MatchCallback    processor)
528 529
{
	IdMap *idmap;
530

531
	idmap = (IdMap *)g_hash_table_lookup (bus->priv->idmap, GINT_TO_POINTER (id));
532

533 534 535 536 537
	if (idmap == NULL)
	{
		g_warning ("No handler registered with id `%d'", id);
		return;
	}
538

539 540 541 542 543
	processor (bus, idmap->message, idmap->listener);
}

static void
process_by_match (GeditMessageBus      *bus,
544 545 546 547 548
                  const gchar          *object_path,
                  const gchar          *method,
                  GeditMessageCallback  callback,
                  gpointer              user_data,
                  MatchCallback         processor)
549 550 551
{
	Message *message;
	GList *item;
552

553
	message = lookup_message (bus, object_path, method, FALSE);
554

555 556 557 558 559
	if (!message)
	{
		g_warning ("No such handler registered for %s.%s", object_path, method);
		return;
	}
560

561 562 563
	for (item = message->listeners; item; item = item->next)
	{
		Listener *listener = (Listener *)item->data;
564 565

		if (listener->callback == callback &&
Garrett Regier's avatar
Garrett Regier committed
566
		    listener->user_data == user_data)
567 568 569 570 571
		{
			processor (bus, message, item);
			return;
		}
	}
572

573 574 575
	g_warning ("No such handler registered for %s.%s", object_path, method);
}

576 577 578 579 580 581
static void
free_type (gpointer data)
{
	g_slice_free (GType, data);
}

582 583 584
static void
gedit_message_bus_init (GeditMessageBus *self)
{
585
	self->priv = gedit_message_bus_get_instance_private (self);
586 587 588 589 590

	self->priv->messages = g_hash_table_new_full (message_identifier_hash,
	                                              message_identifier_equal,
	                                              NULL,
	                                              (GDestroyNotify) message_free);
591 592

	self->priv->idmap = g_hash_table_new_full (g_direct_hash,
593 594 595 596 597 598 599 600
	                                           g_direct_equal,
	                                           NULL,
	                                           (GDestroyNotify) g_free);

	self->priv->types = g_hash_table_new_full (message_identifier_hash,
	                                           message_identifier_equal,
	                                           (GDestroyNotify) message_identifier_free,
	                                           (GDestroyNotify) free_type);
601 602 603 604 605 606 607
}

/**
 * gedit_message_bus_get_default:
 *
 * Get the default application #GeditMessageBus.
 *
608
 * Return value: (transfer none): the default #GeditMessageBus
609 610 611 612 613 614
 *
 */
GeditMessageBus *
gedit_message_bus_get_default (void)
{
	static GeditMessageBus *default_bus = NULL;
615

616 617 618
	if (G_UNLIKELY (default_bus == NULL))
	{
		default_bus = g_object_new (GEDIT_TYPE_MESSAGE_BUS, NULL);
619

620
		g_object_add_weak_pointer (G_OBJECT (default_bus),
621
		                           (gpointer) &default_bus);
622
	}
623

624 625 626 627 628
	return default_bus;
}

/**
 * gedit_message_bus_new:
629
 *
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
 * Create a new message bus. Use gedit_message_bus_get_default() to get the
 * default, application wide, message bus. Creating a new bus is useful for
 * associating a specific bus with for instance a #GeditWindow.
 *
 * Return value: a new #GeditMessageBus
 *
 */
GeditMessageBus *
gedit_message_bus_new (void)
{
	return GEDIT_MESSAGE_BUS (g_object_new (GEDIT_TYPE_MESSAGE_BUS, NULL));
}

/**
 * gedit_message_bus_lookup:
 * @bus: a #GeditMessageBus
 * @object_path: the object path
 * @method: the method
 *
649
 * Get the registered #GeditMessageType for @method at @object_path. The
650 651 652 653 654 655
 * returned #GeditMessageType is owned by the bus and should not be unreffed.
 *
 * Return value: the registered #GeditMessageType or %NULL if no message type
 *               is registered for @method at @object_path
 *
 */
656
GType
657
gedit_message_bus_lookup (GeditMessageBus *bus,
658 659
                          const gchar	  *object_path,
                          const gchar	  *method)
660
{
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
	MessageIdentifier *identifier;
	GType *message_type;

	g_return_val_if_fail (GEDIT_IS_MESSAGE_BUS (bus), G_TYPE_INVALID);
	g_return_val_if_fail (object_path != NULL, G_TYPE_INVALID);
	g_return_val_if_fail (method != NULL, G_TYPE_INVALID);

	identifier = message_identifier_new (object_path, method);
	message_type = g_hash_table_lookup (bus->priv->types, identifier);
	message_identifier_free (identifier);

	if (!message_type)
	{
		return G_TYPE_INVALID;
	}
	else
	{
		return *message_type;
	}
680 681 682 683 684
}

/**
 * gedit_message_bus_register:
 * @bus: a #GeditMessageBus
685
 * @message_type: the message type
686 687 688 689
 * @object_path: the object path
 * @method: the method to register
 *
 * Register a message on the bus. A message must be registered on the bus before
690 691
 * it can be send. This function registers the type for @method at
 * @object_path.
692 693 694 695
 *
 * This function emits a #GeditMessageBus::registered signal.
 *
 */
696
void
697
gedit_message_bus_register (GeditMessageBus *bus,
698 699 700
                            GType            message_type,
                            const gchar     *object_path,
                            const gchar	    *method)
701
{
702 703 704 705 706 707
	MessageIdentifier *identifier;
	GType *ntype;

	g_return_if_fail (GEDIT_IS_MESSAGE_BUS (bus));
	g_return_if_fail (gedit_message_is_valid_object_path (object_path));
	g_return_if_fail (g_type_is_a (message_type, GEDIT_TYPE_MESSAGE));
708 709 710

	if (gedit_message_bus_is_registered (bus, object_path, method))
	{
711 712 713
		g_warning ("Message type for '%s.%s' is already registered",
		           object_path,
		           method);
714 715
	}

716 717 718 719 720 721 722 723 724 725 726 727 728 729
	identifier = message_identifier_new (object_path, method);
	ntype = g_slice_new (GType);

	*ntype = message_type;

	g_hash_table_insert (bus->priv->types,
	                     identifier,
	                     ntype);

	g_signal_emit (bus,
	               message_bus_signals[REGISTERED],
	               0,
	               object_path,
	               method);
730 731
}

732 733
static void
gedit_message_bus_unregister_real (GeditMessageBus  *bus,
734 735 736
                                   const gchar      *object_path,
                                   const gchar      *method,
                                   gboolean          remove_from_store)
737
{
738 739 740 741 742 743 744 745 746 747 748 749 750
	MessageIdentifier *identifier;

	identifier = message_identifier_new (object_path, method);

	if (!remove_from_store || g_hash_table_remove (bus->priv->types,
	                                               identifier))
	{
		g_signal_emit (bus,
		               message_bus_signals[UNREGISTERED],
		               0,
		               object_path,
		               method);
	}
751

752
	message_identifier_free (identifier);
753 754
}

755 756 757
/**
 * gedit_message_bus_unregister:
 * @bus: a #GeditMessageBus
758 759
 * @object_path: the object path
 * @method: the method
760
 *
761
 * Unregisters a previously registered message type. This is especially useful
762 763 764 765 766 767 768
 * for plugins which should unregister message types when they are deactivated.
 *
 * This function emits the #GeditMessageBus::unregistered signal.
 *
 */
void
gedit_message_bus_unregister (GeditMessageBus  *bus,
769 770
                              const gchar      *object_path,
                              const gchar      *method)
771 772
{
	g_return_if_fail (GEDIT_IS_MESSAGE_BUS (bus));
773 774 775 776 777 778 779
	g_return_if_fail (object_path != NULL);
	g_return_if_fail (method != NULL);

	gedit_message_bus_unregister_real (bus,
	                                   object_path,
	                                   method,
	                                   TRUE);
780 781
}

782
typedef struct
783 784 785 786 787
{
	GeditMessageBus *bus;
	const gchar *object_path;
} UnregisterInfo;

788
static gboolean
789 790 791
unregister_each (MessageIdentifier *identifier,
                 GType             *gtype,
                 UnregisterInfo    *info)
792
{
793 794 795 796 797 798 799
	if (g_strcmp0 (identifier->object_path, info->object_path) == 0)
	{
		gedit_message_bus_unregister_real (info->bus,
		                                   identifier->object_path,
		                                   identifier->method,
		                                   FALSE);

800
		return TRUE;
801
	}
802

803
	return FALSE;
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
}

/**
 * gedit_message_bus_unregister_all:
 * @bus: a #GeditMessageBus
 * @object_path: the object path
 *
 * Unregisters all message types for @object_path. This is especially useful for
 * plugins which should unregister message types when they are deactivated.
 *
 * This function emits the #GeditMessageBus::unregistered signal for all
 * unregistered message types.
 *
 */
void
gedit_message_bus_unregister_all (GeditMessageBus *bus,
820
                                  const gchar     *object_path)
821 822 823 824 825 826
{
	UnregisterInfo info = {bus, object_path};

	g_return_if_fail (GEDIT_IS_MESSAGE_BUS (bus));
	g_return_if_fail (object_path != NULL);

827 828 829
	g_hash_table_foreach_remove (bus->priv->types,
	                             (GHRFunc)unregister_each,
	                             &info);
830 831 832 833 834 835 836 837
}

/**
 * gedit_message_bus_is_registered:
 * @bus: a #GeditMessageBus
 * @object_path: the object path
 * @method: the method
 *
838
 * Check whether a message type @method at @object_path is registered on the
839 840
 * bus.
 *
841
 * Return value: %TRUE if the @method at @object_path is a registered message
842 843 844 845
 *               type on the bus
 *
 */
gboolean
Garrett Regier's avatar
Garrett Regier committed
846
gedit_message_bus_is_registered (GeditMessageBus  *bus,
847 848
                                 const gchar	  *object_path,
                                 const gchar      *method)
849
{
850
	MessageIdentifier *identifier;
851
	gboolean ret;
852

853 854 855 856
	g_return_val_if_fail (GEDIT_IS_MESSAGE_BUS (bus), FALSE);
	g_return_val_if_fail (object_path != NULL, FALSE);
	g_return_val_if_fail (method != NULL, FALSE);

857
	identifier = message_identifier_new (object_path, method);
858
	ret = g_hash_table_lookup (bus->priv->types, identifier) != NULL;
859 860
	message_identifier_free (identifier);

861 862 863
	return ret;
}

864 865 866
typedef struct
{
	GeditMessageBusForeach func;
Garrett Regier's avatar
Garrett Regier committed
867
	gpointer user_data;
868 869 870
} ForeachInfo;

static void
871 872 873
foreach_type (MessageIdentifier *identifier,
              GType              *message_type,
              ForeachInfo       *info)
874
{
875 876 877
	info->func (identifier->object_path,
	            identifier->method,
	            info->user_data);
878 879 880 881
}

/**
 * gedit_message_bus_foreach:
Sébastien Wilmet's avatar
Sébastien Wilmet committed
882
 * @bus: the #GeditMessageBus
883
 * @func: (scope call): the callback function
Garrett Regier's avatar
Garrett Regier committed
884
 * @user_data: the user data to supply to the callback function
885 886 887 888
 *
 * Calls @func for each message type registered on the bus
 *
 */
889
void
890
gedit_message_bus_foreach (GeditMessageBus        *bus,
891 892
                           GeditMessageBusForeach  func,
                           gpointer		   user_data)
893
{
Garrett Regier's avatar
Garrett Regier committed
894
	ForeachInfo info = {func, user_data};
895

896 897 898 899 900 901
	g_return_if_fail (GEDIT_IS_MESSAGE_BUS (bus));
	g_return_if_fail (func != NULL);

	g_hash_table_foreach (bus->priv->types, (GHFunc)foreach_type, &info);
}

902 903 904 905 906 907
/**
 * gedit_message_bus_connect:
 * @bus: a #GeditMessageBus
 * @object_path: the object path
 * @method: the method
 * @callback: function to be called when message @method at @object_path is sent
908 909
 * @user_data: (allow-none): user_data to use for the callback
 * @destroy_data: (allow-none): function to evoke with @user_data as argument when @user_data
910 911 912 913 914 915 916 917 918
 *                needs to be freed
 *
 * Connect a callback handler to be evoked when message @method at @object_path
 * is sent over the bus.
 *
 * Return value: the callback identifier
 *
 */
guint
919 920 921 922 923 924
gedit_message_bus_connect (GeditMessageBus	*bus,
                           const gchar		*object_path,
                           const gchar		*method,
                           GeditMessageCallback  callback,
                           gpointer		 user_data,
                           GDestroyNotify	 destroy_data)
925 926 927 928 929 930 931
{
	Message *message;

	g_return_val_if_fail (GEDIT_IS_MESSAGE_BUS (bus), 0);
	g_return_val_if_fail (object_path != NULL, 0);
	g_return_val_if_fail (method != NULL, 0);
	g_return_val_if_fail (callback != NULL, 0);
932

933 934
	/* lookup the message and create if it does not exist yet */
	message = lookup_message (bus, object_path, method, TRUE);
935

Garrett Regier's avatar
Garrett Regier committed
936
	return add_listener (bus, message, callback, user_data, destroy_data);
937 938 939 940 941 942 943 944 945 946 947 948
}

/**
 * gedit_message_bus_disconnect:
 * @bus: a #GeditMessageBus
 * @id: the callback id as returned by gedit_message_bus_connect()
 *
 * Disconnects a previously connected message callback.
 *
 */
void
gedit_message_bus_disconnect (GeditMessageBus *bus,
949
                              guint            id)
950 951
{
	g_return_if_fail (GEDIT_IS_MESSAGE_BUS (bus));
952

953 954 955 956 957 958 959 960
	process_by_id (bus, id, remove_listener);
}

/**
 * gedit_message_bus_disconnect_by_func:
 * @bus: a #GeditMessageBus
 * @object_path: the object path
 * @method: the method
961
 * @callback: (scope call): the connected callback
Garrett Regier's avatar
Garrett Regier committed
962
 * @user_data: the user_data with which the callback was connected
963
 *
964 965
 * Disconnects a previously connected message callback by matching the
 * provided callback function and user_data. See also
966 967 968 969 970
 * gedit_message_bus_disconnect().
 *
 */
void
gedit_message_bus_disconnect_by_func (GeditMessageBus      *bus,
971 972 973 974
                                      const gchar	   *object_path,
                                      const gchar	   *method,
                                      GeditMessageCallback  callback,
                                      gpointer		    user_data)
975 976
{
	g_return_if_fail (GEDIT_IS_MESSAGE_BUS (bus));
977 978 979 980 981 982 983

	process_by_match (bus,
	                  object_path,
	                  method,
	                  callback,
	                  user_data,
	                  remove_listener);
984 985 986 987 988 989 990 991 992 993 994 995 996
}

/**
 * gedit_message_bus_block:
 * @bus: a #GeditMessageBus
 * @id: the callback id
 *
 * Blocks evoking the callback specified by @id. Unblock the callback by
 * using gedit_message_bus_unblock().
 *
 */
void
gedit_message_bus_block (GeditMessageBus *bus,
997
                         guint		  id)
998 999
{
	g_return_if_fail (GEDIT_IS_MESSAGE_BUS (bus));
1000

1001 1002 1003 1004 1005 1006 1007 1008
	process_by_id (bus, id, block_listener);
}

/**
 * gedit_message_bus_block_by_func:
 * @bus: a #GeditMessageBus
 * @object_path: the object path
 * @method: the method
1009
 * @callback: (scope call): the callback to block
Garrett Regier's avatar
Garrett Regier committed
1010
 * @user_data: the user_data with which the callback was connected
1011
 *
Garrett Regier's avatar
Garrett Regier committed
1012
 * Blocks evoking the callback that matches provided @callback and @user_data.
1013
 * Unblock the callback using gedit_message_bus_unblock_by_func().
1014 1015 1016 1017
 *
 */
void
gedit_message_bus_block_by_func (GeditMessageBus      *bus,
1018 1019 1020 1021
                                 const gchar	      *object_path,
                                 const gchar	      *method,
                                 GeditMessageCallback  callback,
                                 gpointer	       user_data)
1022 1023
{
	g_return_if_fail (GEDIT_IS_MESSAGE_BUS (bus));
1024 1025 1026 1027 1028 1029 1030

	process_by_match (bus,
	                  object_path,
	                  method,
	                  callback,
	                  user_data,
	                  block_listener);
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
}

/**
 * gedit_message_bus_unblock:
 * @bus: a #GeditMessageBus
 * @id: the callback id
 *
 * Unblocks the callback specified by @id.
 *
 */
void
gedit_message_bus_unblock (GeditMessageBus *bus,
1043
                           guint	    id)
1044 1045
{
	g_return_if_fail (GEDIT_IS_MESSAGE_BUS (bus));
1046

1047 1048 1049 1050 1051 1052 1053 1054
	process_by_id (bus, id, unblock_listener);
}

/**
 * gedit_message_bus_unblock_by_func:
 * @bus: a #GeditMessageBus
 * @object_path: the object path
 * @method: the method
1055
 * @callback: (scope call): the callback to block
Garrett Regier's avatar
Garrett Regier committed
1056
 * @user_data: the user_data with which the callback was connected
1057
 *
Garrett Regier's avatar
Garrett Regier committed
1058
 * Unblocks the callback that matches provided @callback and @user_data.
1059 1060 1061 1062
 *
 */
void
gedit_message_bus_unblock_by_func (GeditMessageBus      *bus,
1063 1064 1065 1066
                                   const gchar	        *object_path,
                                   const gchar	        *method,
                                   GeditMessageCallback  callback,
                                   gpointer	         user_data)
1067 1068 1069
{
	g_return_if_fail (GEDIT_IS_MESSAGE_BUS (bus));

1070 1071 1072 1073 1074 1075
	process_by_match (bus,
	                  object_path,
	                  method,
	                  callback,
	                  user_data,
	                  unblock_listener);
1076 1077 1078 1079
}

static void
send_message_real (GeditMessageBus *bus,
1080
                   GeditMessage    *message)
1081
{
1082 1083
	bus->priv->message_queue = g_list_prepend (bus->priv->message_queue,
	                                           g_object_ref (message));
1084 1085

	if (bus->priv->idle_id == 0)
Garrett Regier's avatar
Garrett Regier committed
1086
	{
1087
		bus->priv->idle_id = g_idle_add_full (G_PRIORITY_HIGH,
1088 1089 1090
		                                      (GSourceFunc)idle_dispatch,
		                                      bus,
		                                      NULL);
Garrett Regier's avatar
Garrett Regier committed
1091
	}
1092 1093 1094 1095 1096 1097 1098 1099
}

/**
 * gedit_message_bus_send_message:
 * @bus: a #GeditMessageBus
 * @message: the message to send
 *
 * This sends the provided @message asynchronously over the bus. To send
1100
 * a message synchronously, use gedit_message_bus_send_message_sync(). The
1101 1102 1103 1104 1105 1106
 * convenience function gedit_message_bus_send() can be used to easily send
 * a message without constructing the message object explicitly first.
 *
 */
void
gedit_message_bus_send_message (GeditMessageBus *bus,
1107
                                GeditMessage    *message)
1108 1109 1110 1111
{
	g_return_if_fail (GEDIT_IS_MESSAGE_BUS (bus));
	g_return_if_fail (GEDIT_IS_MESSAGE (message));

1112
	send_message_real (bus, message);
1113 1114 1115 1116 1117 1118 1119 1120
}

/**
 * gedit_message_bus_send_message_sync:
 * @bus: a #GeditMessageBus
 * @message: the message to send
 *
 * This sends the provided @message synchronously over the bus. To send
1121
 * a message asynchronously, use gedit_message_bus_send_message(). The
1122 1123 1124 1125 1126 1127
 * convenience function gedit_message_bus_send_sync() can be used to easily send
 * a message without constructing the message object explicitly first.
 *
 */
void
gedit_message_bus_send_message_sync (GeditMessageBus *bus,
1128
                                     GeditMessage    *message)
1129 1130 1131 1132
{
	g_return_if_fail (GEDIT_IS_MESSAGE_BUS (bus));
	g_return_if_fail (GEDIT_IS_MESSAGE (message));

1133
	dispatch_message (bus, message);
1134 1135 1136 1137
}

static GeditMessage *
create_message (GeditMessageBus *bus,
1138 1139 1140 1141
                const gchar     *object_path,
                const gchar     *method,
                const gchar     *first_property,
                va_list          var_args)
1142
{
1143 1144 1145
	GType message_type;
	GeditMessage *msg;

1146
	message_type = gedit_message_bus_lookup (bus, object_path, method);
1147 1148

	if (message_type == G_TYPE_INVALID)
1149
	{
1150 1151 1152 1153
		g_warning ("Could not find message type for '%s.%s'",
		           object_path,
		           method);

1154 1155 1156
		return NULL;
	}

1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
	msg = GEDIT_MESSAGE (g_object_new_valist (message_type,
	                                          first_property,
	                                          var_args));

	if (msg)
	{
		g_object_set (msg,
		              "object_path",
		              object_path,
		              "method",
		              method,
		              NULL);
	}

	return msg;
1172 1173 1174 1175 1176 1177 1178
}

/**
 * gedit_message_bus_send:
 * @bus: a #GeditMessageBus
 * @object_path: the object path
 * @method: the method
1179
 * @first_property: the first property
1180 1181
 * @...: NULL terminated list of key/value pairs
 *
1182 1183 1184
 * This provides a convenient way to quickly send a message @method at
 * @object_path asynchronously over the bus. The variable argument list
 * specifies key (string) value pairs used to construct the message arguments.
1185 1186 1187 1188
 * To send a message synchronously use gedit_message_bus_send_sync().
 */
void
gedit_message_bus_send (GeditMessageBus *bus,
1189 1190 1191 1192
                        const gchar     *object_path,
                        const gchar     *method,
                        const gchar     *first_property,
                        ...)
1193 1194 1195 1196
{
	va_list var_args;
	GeditMessage *message;

1197 1198 1199 1200 1201 1202 1203 1204
	va_start (var_args, first_property);

	message = create_message (bus,
	                          object_path,
	                          method,
	                          first_property,
	                          var_args);

1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
	if (message)
	{
		send_message_real (bus, message);
		g_object_unref (message);
	}
	else
	{
		g_warning ("Could not instantiate message");
	}

	va_end (var_args);
}

/**
 * gedit_message_bus_send_sync:
 * @bus: a #GeditMessageBus
 * @object_path: the object path
 * @method: the method
1223
 * @first_property: the first property
1224
 * @...: (allow-none): %NULL terminated list of key/value pairs
1225
 *
1226 1227 1228
 * This provides a convenient way to quickly send a message @method at
 * @object_path synchronously over the bus. The variable argument list
 * specifies key (string) value pairs used to construct the message
1229 1230
 * arguments. To send a message asynchronously use gedit_message_bus_send().
 *
1231 1232 1233
 * Return value: (allow-none) (transfer full): the constructed #GeditMessage.
 *               The caller owns a reference to the #GeditMessage and should
 *               call g_object_unref() when it is no longer needed.
1234 1235 1236
 */
GeditMessage *
gedit_message_bus_send_sync (GeditMessageBus *bus,
1237 1238 1239 1240
                             const gchar     *object_path,
                             const gchar     *method,
                             const gchar     *first_property,
                             ...)
1241 1242 1243
{
	va_list var_args;
	GeditMessage *message;
1244 1245 1246 1247 1248 1249 1250 1251

	va_start (var_args, first_property);
	message = create_message (bus,
	                          object_path,
	                          method,
	                          first_property,
	                          var_args);

1252
	if (message)
1253 1254 1255
	{
		dispatch_message (bus, message);
	}
1256 1257

	va_end (var_args);
1258

1259 1260 1261
	return message;
}

1262
/* ex:set ts=8 noet: */