## GeditMessageBus GeditMessageBus — internal message communication bus |

## Synopsis

    #include <gedit/gedit-message-bus.h>

                        GeditMessageBus;
    void                (*GeditMessageCallback)             (GeditMessageBus *bus,
                                                             GeditMessage *message,
                                                             gpointer userdata);
    GeditMessageBus *   gedit_message_bus_get_default       (void);
    GeditMessageBus *   gedit_message_bus_new               (void);
    GeditMessageType *  gedit_message_bus_lookup            (GeditMessageBus *bus,
                                                             const gchar *object_path,
                                                             const gchar *method);
    GeditMessageType *  gedit_message_bus_register          (GeditMessageBus *bus,
                                                             const gchar *object_path,
                                                             const gchar *method,
                                                             guint num_optional,
                                                             ...);
    void                gedit_message_bus_unregister        (GeditMessageBus *bus,
                                                             GeditMessageType *message_type);
    void                gedit_message_bus_unregister_all    (GeditMessageBus *bus,
                                                             const gchar *object_path);
    gboolean            gedit_message_bus_is_registered     (GeditMessageBus *bus,
                                                             const gchar *object_path,
                                                             const gchar *method);
    void                gedit_message_bus_foreach           (GeditMessageBus *bus,
                                                             GeditMessageBusForeach func,
                                                             gpointer userdata);
    guint               gedit_message_bus_connect           (GeditMessageBus *bus,
                                                             const gchar *object_path,
                                                             const gchar *method,
                                                             GeditMessageCallback callback,
                                                             gpointer userdata,
                                                             GDestroyNotify destroy_data);
    void                gedit_message_bus_disconnect        (GeditMessageBus *bus,
                                                             guint id);
    void                gedit_message_bus_disconnect_by_func
                                                            (GeditMessageBus *bus,
                                                             const gchar *object_path,
                                                             const gchar *method,
                                                             GeditMessageCallback callback,
                                                             gpointer userdata);
    void                gedit_message_bus_block             (GeditMessageBus *bus,
                                                             guint id);
    void                gedit_message_bus_block_by_func     (GeditMessageBus *bus,
                                                             const gchar *object_path,
                                                             const gchar *method,
                                                             GeditMessageCallback callback,
                                                             gpointer userdata);
    void                gedit_message_bus_unblock           (GeditMessageBus *bus,
                                                             guint id);
    void                gedit_message_bus_unblock_by_func   (GeditMessageBus *bus,
                                                             const gchar *object_path,
                                                             const gchar *method,
                                                             GeditMessageCallback callback,
                                                             gpointer userdata);
    void                gedit_message_bus_send_message      (GeditMessageBus *bus,
                                                             GeditMessage *message);
    void                gedit_message_bus_send_message_sync (GeditMessageBus *bus,
                                                             GeditMessage *message);
    void                gedit_message_bus_send              (GeditMessageBus *bus,
                                                             const gchar *object_path,
                                                             const gchar *method,
                                                             ...);
    GeditMessage *      gedit_message_bus_send_sync         (GeditMessageBus *bus,
                                                             const gchar *object_path,
                                                             const gchar *method,
                                                             ...);

## Object Hierarchy

      GObject
       +----GeditMessageBus

## Signals

      "dispatch"                                       : Run Last
      "registered"                                     : Run Last
      "unregistered"                                   : Run Last

## Description

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 1. Registering a message type

    1
    2
    3
    4
    5
    6
    7
    8
    GeditMessageBus *bus = gedit_message_bus_get_default ();

    // Register 'method' at '/plugins/example' with one required
    // string argument 'arg1'
    GeditMessageType *message_type = gedit_message_bus_register ("/plugins/example", "method",
                                                                 0,
                                                                 "arg1", G_TYPE_STRING,
                                                                 NULL);

Example 2. Connecting a callback

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    static void
    example_method_cb (GeditMessageBus *bus,
                       GeditMessage    *message,
                       gpointer         userdata)
    {
        gchar *arg1 = NULL;

        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 ();

    guint id = gedit_message_bus_connect (bus,
                                          "/plugins/example", "method",
                                          example_method_cb,
                                          NULL,
                                          NULL);

Example 3. Sending a message

    1
    2
    3
    4
    5
    6
    GeditMessageBus *bus = gedit_message_bus_get_default ();

    gedit_message_bus_send (bus,
                            "/plugins/example", "method",
                            "arg1", "Hello World",
                            NULL);

## Details

### GeditMessageBus

    typedef struct _GeditMessageBus GeditMessageBus;

### GeditMessageCallback ()

    void                (*GeditMessageCallback)             (GeditMessageBus *bus,
                                                             GeditMessage *message,
                                                             gpointer userdata);

Callback signature used for connecting callback functions to be called when a message is received (see `gedit_message_bus_connect()`).

`bus` : | the GeditMessageBus on which the message was sent
`message` : | the GeditMessage which was sent
`userdata` : | the supplied user data when connecting the callback

### gedit_message_bus_get_default ()

    GeditMessageBus *   gedit_message_bus_get_default       (void);

Get the default application GeditMessageBus.

Returns : | the default GeditMessageBus

### gedit_message_bus_new ()

    GeditMessageBus *   gedit_message_bus_new               (void);

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.

Returns : | a new GeditMessageBus

### gedit_message_bus_lookup ()

    GeditMessageType *  gedit_message_bus_lookup            (GeditMessageBus *bus,
                                                             const gchar *object_path,
                                                             const gchar *method);

Get the registered GeditMessageType for `method` at `object_path`. The returned GeditMessageType is owned by the bus and should not be unreffed.

`bus` : | a GeditMessageBus
`object_path` : | the object path
`method` : | the method
Returns : | the registered GeditMessageType or `NULL` if no message type is registered for `method` at `object_path`

### gedit_message_bus_register ()

    GeditMessageType *  gedit_message_bus_register          (GeditMessageBus *bus,
                                                             const gchar *object_path,
                                                             const gchar *method,
                                                             guint num_optional,
                                                             ...);

Register a message on the bus. A message must be registered on the bus before it can be send. This function registers the type arguments for `method` at `object_path`. The arguments are specified with the variable arguments which should contain pairs of const gchar *key and GType terminated by `NULL`. The last `num_optional` arguments are registered as optional (and are thus not required when sending a message).

This function emits a "registered" signal.

`bus` : | a GeditMessageBus
`object_path` : | the object path
`method` : | the method to register
`num_optional` : | the number of optional arguments
`...` : | NULL terminated list of key/gtype method argument pairs
Returns : | the registered GeditMessageType. The returned reference is owned by the bus. If you want to keep it alive after unregistering, use `gedit_message_type_ref()`.

### gedit_message_bus_unregister ()

    void                gedit_message_bus_unregister        (GeditMessageBus *bus,
                                                             GeditMessageType *message_type);

Unregisters a previously registered message type. This is especially useful for plugins which should unregister message types when they are deactivated.

This function emits the "unregistered" signal.

`bus` : | a GeditMessageBus
`message_type` : | the GeditMessageType to unregister

### gedit_message_bus_unregister_all ()

    void                gedit_message_bus_unregister_all    (GeditMessageBus *bus,
                                                             const gchar *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 "unregistered" signal for all unregistered message types.

`bus` : | a GeditMessageBus
`object_path` : | the object path

### gedit_message_bus_is_registered ()

    gboolean            gedit_message_bus_is_registered     (GeditMessageBus *bus,
                                                             const gchar *object_path,
                                                             const gchar *method);

Check whether a message type `method` at `object_path` is registered on the bus.

`bus` : | a GeditMessageBus
`object_path` : | the object path
`method` : | the method
Returns : | `TRUE` if the `method` at `object_path` is a registered message type on the bus

### gedit_message_bus_foreach ()

    void                gedit_message_bus_foreach           (GeditMessageBus *bus,
                                                             GeditMessageBusForeach func,
                                                             gpointer userdata);

Calls `func` for each message type registered on the bus

`bus` : | the GeditMessagebus
`func` : | the callback function
`userdata` : | the user data to supply to the callback function

### gedit_message_bus_connect ()

    guint               gedit_message_bus_connect           (GeditMessageBus *bus,
                                                             const gchar *object_path,
                                                             const gchar *method,
                                                             GeditMessageCallback callback,
                                                             gpointer userdata,
                                                             GDestroyNotify destroy_data);

Connect a callback handler to be evoked when message `method` at `object_path` is sent over the bus.

`bus` : | a GeditMessageBus
`object_path` : | the object path
`method` : | the method
`callback` : | function to be called when message `method` at `object_path` is sent
`userdata` : | userdata to use for the callback
`destroy_data` : | function to evoke with `userdata` as argument when `userdata` needs to be freed
Returns : | the callback identifier

### gedit_message_bus_disconnect ()

    void                gedit_message_bus_disconnect        (GeditMessageBus *bus,
                                                             guint id);

Disconnects a previously connected message callback.

`bus` : | a GeditMessageBus
`id` : | the callback id as returned by `gedit_message_bus_connect()`

### gedit_message_bus_disconnect_by_func ()

    void                gedit_message_bus_disconnect_by_func
                                                            (GeditMessageBus *bus,
                                                             const gchar *object_path,
                                                             const gchar *method,
                                                             GeditMessageCallback callback,
                                                             gpointer userdata);

Disconnects a previously connected message callback by matching the provided callback function and userdata. See also `gedit_message_bus_disconnect()`.

`bus` : | a GeditMessageBus
`object_path` : | the object path
`method` : | the method
`callback` : | the connected callback
`userdata` : | the userdata with which the callback was connected

### gedit_message_bus_block ()

    void                gedit_message_bus_block             (GeditMessageBus *bus,
                                                             guint id);

Blocks evoking the callback specified by `id`. Unblock the callback by using `gedit_message_bus_unblock()`.

`bus` : | a GeditMessageBus
`id` : | the callback id

### gedit_message_bus_block_by_func ()

    void                gedit_message_bus_block_by_func     (GeditMessageBus *bus,
                                                             const gchar *object_path,
                                                             const gchar *method,
                                                             GeditMessageCallback callback,
                                                             gpointer userdata);

Blocks evoking the callback that matches provided `callback` and `userdata`. Unblock the callback using `gedit_message_unblock_by_func()`.

`bus` : | a GeditMessageBus
`object_path` : | the object path
`method` : | the method
`callback` : | the callback to block
`userdata` : | the userdata with which the callback was connected

### gedit_message_bus_unblock ()

    void                gedit_message_bus_unblock           (GeditMessageBus *bus,
                                                             guint id);

Unblocks the callback specified by `id`.

`bus` : | a GeditMessageBus
`id` : | the callback id

### gedit_message_bus_unblock_by_func ()

    void                gedit_message_bus_unblock_by_func   (GeditMessageBus *bus,
                                                             const gchar *object_path,
                                                             const gchar *method,
                                                             GeditMessageCallback callback,
                                                             gpointer userdata);

Unblocks the callback that matches provided `callback` and `userdata`.

`bus` : | a GeditMessageBus
`object_path` : | the object path
`method` : | the method
`callback` : | the callback to block
`userdata` : | the userdata with which the callback was connected

### gedit_message_bus_send_message ()

    void                gedit_message_bus_send_message      (GeditMessageBus *bus,
                                                             GeditMessage *message);

This sends the provided `message` asynchronously over the bus. To send a message synchronously, use `gedit_message_bus_send_message_sync()`. The convenience function `gedit_message_bus_send()` can be used to easily send a message without constructing the message object explicitly first.

`bus` : | a GeditMessageBus
`message` : | the message to send

### gedit_message_bus_send_message_sync ()

    void                gedit_message_bus_send_message_sync (GeditMessageBus *bus,
                                                             GeditMessage *message);

This sends the provided `message` synchronously over the bus. To send a message asynchronously, use `gedit_message_bus_send_message()`. The convenience function `gedit_message_bus_send_sync()` can be used to easily send a message without constructing the message object explicitly first.

`bus` : | a GeditMessageBus
`message` : | the message to send

### gedit_message_bus_send ()

    void                gedit_message_bus_send              (GeditMessageBus *bus,
                                                             const gchar *object_path,
                                                             const gchar *method,
                                                             ...);

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. To send a message synchronously use `gedit_message_bus_send_sync()`.

`bus` : | a GeditMessageBus
`object_path` : | the object path
`method` : | the method
`...` : | NULL terminated list of key/value pairs

### gedit_message_bus_send_sync ()

    GeditMessage *      gedit_message_bus_send_sync         (GeditMessageBus *bus,
                                                             const gchar *object_path,
                                                             const gchar *method,
                                                             ...);

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 arguments. To send a message asynchronously use `gedit_message_bus_send()`.

`bus` : | a GeditMessageBus
`object_path` : | the object path
`method` : | the method
`...` : | NULL terminated list of key/value pairs
Returns : | the constructed GeditMessage. The caller owns a reference to the GeditMessage and should call `g_object_unref()` when it is no longer needed

## Signal Details

### The `"dispatch"` signal

    void                user_function                      (GeditMessageBus *bus,
                                                            GeditMessage    *message,
                                                            gpointer         user_data)      : Run Last

The "dispatch" signal is emitted when a message is to be dispatched. The message is dispatched in the default handler of this signal. Primary use of this signal is to customize the dispatch of a message (for instance to automatically dispatch all messages over DBus). 2

`bus` : | a GeditMessageBus
`message` : | the GeditMessage to dispatch
`user_data` : | user data set when the signal handler was connected.

### The `"registered"` signal

    void                user_function                      (GeditMessageBus  *bus,
                                                            GeditMessageType *message_type,
                                                            gpointer          user_data)         : Run Last

The "registered" signal is emitted when a message has been registered on the bus.

`bus` : | a GeditMessageBus
`message_type` : | the registered GeditMessageType
`user_data` : | user data set when the signal handler was connected.

### The `"unregistered"` signal

    void                user_function                      (GeditMessageBus  *bus,
                                                            GeditMessageType *message_type,
                                                            gpointer          user_data)         : Run Last

The "unregistered" signal is emitted when a message has been unregistered from the bus.

`bus` : | a GeditMessageBus
`message_type` : | the unregistered GeditMessageType
`user_data` : | user data set when the signal handler was connected.