Blame bus/connection.c

Packit Service 1d8f1c
/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
Packit Service 1d8f1c
/* vim:set et sts=4: */
Packit Service 1d8f1c
/* bus - The Input Bus
Packit Service 1d8f1c
 * Copyright (C) 2008-2010 Peng Huang <shawn.p.huang@gmail.com>
Packit Service 1d8f1c
 * Copyright (C) 2008-2010 Red Hat, Inc.
Packit Service 1d8f1c
 *
Packit Service 1d8f1c
 * This library is free software; you can redistribute it and/or
Packit Service 1d8f1c
 * modify it under the terms of the GNU Lesser General Public
Packit Service 1d8f1c
 * License as published by the Free Software Foundation; either
Packit Service 1d8f1c
 * version 2.1 of the License, or (at your option) any later version.
Packit Service 1d8f1c
 *
Packit Service 1d8f1c
 * This library is distributed in the hope that it will be useful,
Packit Service 1d8f1c
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 1d8f1c
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 1d8f1c
 * Lesser General Public License for more details.
Packit Service 1d8f1c
 *
Packit Service 1d8f1c
 * You should have received a copy of the GNU Lesser General Public
Packit Service 1d8f1c
 * License along with this library; if not, write to the Free Software
Packit Service 1d8f1c
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
Packit Service 1d8f1c
 * USA
Packit Service 1d8f1c
 */
Packit Service 1d8f1c
Packit Service 1d8f1c
#include <unistd.h>
Packit Service 1d8f1c
#include "connection.h"
Packit Service 1d8f1c
#include "matchrule.h"
Packit Service 1d8f1c
Packit Service 1d8f1c
struct _BusConnection {
Packit Service 1d8f1c
    IBusObject parent;
Packit Service 1d8f1c
Packit Service 1d8f1c
    /* instance members */
Packit Service 1d8f1c
Packit Service 1d8f1c
    /* underlying GDBus connetion */
Packit Service 1d8f1c
    GDBusConnection *connection;
Packit Service 1d8f1c
    /* a unique name of the connection like ":1.0" */
Packit Service 1d8f1c
    gchar *unique_name;
Packit Service 1d8f1c
    /* list for well known names */
Packit Service 1d8f1c
    GList  *names;
Packit Service 1d8f1c
Packit Service 1d8f1c
    guint  filter_id;
Packit Service 1d8f1c
};
Packit Service 1d8f1c
Packit Service 1d8f1c
struct _BusConnectionClass {
Packit Service 1d8f1c
  IBusObjectClass parent;
Packit Service 1d8f1c
Packit Service 1d8f1c
  /* class members */
Packit Service 1d8f1c
};
Packit Service 1d8f1c
Packit Service 1d8f1c
/* static guint            _signals[LAST_SIGNAL] = { 0 }; */
Packit Service 1d8f1c
Packit Service 1d8f1c
/* functions prototype */
Packit Service 1d8f1c
static void     bus_connection_destroy      (BusConnection      *connection);
Packit Service 1d8f1c
static void     bus_connection_set_dbus_connection
Packit Service 1d8f1c
                                            (BusConnection      *connection,
Packit Service 1d8f1c
                                             GDBusConnection    *dbus_connection);
Packit Service 1d8f1c
static void     bus_connection_dbus_connection_closed_cb
Packit Service 1d8f1c
                                            (GDBusConnection    *dbus_connection,
Packit Service 1d8f1c
                                             gboolean            remote_peer_vanished,
Packit Service 1d8f1c
                                             GError             *error,
Packit Service 1d8f1c
                                             BusConnection      *connection);
Packit Service 1d8f1c
static GQuark   bus_connection_quark        (void);
Packit Service 1d8f1c
Packit Service 1d8f1c
#define BUS_CONNECTION_QUARK (bus_connection_quark ())
Packit Service 1d8f1c
Packit Service 1d8f1c
G_DEFINE_TYPE (BusConnection, bus_connection, IBUS_TYPE_OBJECT)
Packit Service 1d8f1c
Packit Service 1d8f1c
static void
Packit Service 1d8f1c
bus_connection_class_init (BusConnectionClass *class)
Packit Service 1d8f1c
{
Packit Service 1d8f1c
    IBusObjectClass *ibus_object_class = IBUS_OBJECT_CLASS (class);
Packit Service 1d8f1c
Packit Service 1d8f1c
    ibus_object_class->destroy = (IBusObjectDestroyFunc) bus_connection_destroy;
Packit Service 1d8f1c
}
Packit Service 1d8f1c
Packit Service 1d8f1c
static void
Packit Service 1d8f1c
bus_connection_init (BusConnection *connection)
Packit Service 1d8f1c
{
Packit Service 1d8f1c
}
Packit Service 1d8f1c
Packit Service 1d8f1c
static void
Packit Service 1d8f1c
bus_connection_destroy (BusConnection *connection)
Packit Service 1d8f1c
{
Packit Service 1d8f1c
    if (connection->connection) {
Packit Service 1d8f1c
        /* disconnect from closed signal */
Packit Service 1d8f1c
        g_signal_handlers_disconnect_by_func (connection->connection,
Packit Service 1d8f1c
                G_CALLBACK (bus_connection_dbus_connection_closed_cb), connection);
Packit Service 1d8f1c
Packit Service 1d8f1c
        /* remove filter */
Packit Service 1d8f1c
        bus_connection_set_filter (connection, NULL, NULL, NULL);
Packit Service 1d8f1c
Packit Service 1d8f1c
        /* disconnect busconnection with dbus connection */
Packit Service 1d8f1c
        g_object_set_qdata ((GObject *)connection->connection, BUS_CONNECTION_QUARK, NULL);
Packit Service 1d8f1c
        if (!g_dbus_connection_is_closed (connection->connection)) {
Packit Service 1d8f1c
            g_dbus_connection_close (connection->connection, NULL, NULL, NULL);
Packit Service 1d8f1c
        }
Packit Service 1d8f1c
        g_object_unref (connection->connection);
Packit Service 1d8f1c
        connection->connection = NULL;
Packit Service 1d8f1c
    }
Packit Service 1d8f1c
Packit Service 1d8f1c
    if (connection->unique_name) {
Packit Service 1d8f1c
        g_free (connection->unique_name);
Packit Service 1d8f1c
        connection->unique_name = NULL;
Packit Service 1d8f1c
    }
Packit Service 1d8f1c
Packit Service 1d8f1c
    g_list_free_full (connection->names, g_free);
Packit Service 1d8f1c
    connection->names = NULL;
Packit Service 1d8f1c
Packit Service 1d8f1c
    IBUS_OBJECT_CLASS(bus_connection_parent_class)->destroy (IBUS_OBJECT (connection));
Packit Service 1d8f1c
}
Packit Service 1d8f1c
Packit Service 1d8f1c
static void
Packit Service 1d8f1c
bus_connection_dbus_connection_closed_cb (GDBusConnection *dbus_connection,
Packit Service 1d8f1c
                                          gboolean         remote_peer_vanished,
Packit Service 1d8f1c
                                          GError          *error,
Packit Service 1d8f1c
                                          BusConnection   *connection)
Packit Service 1d8f1c
{
Packit Service 1d8f1c
    ibus_object_destroy ((IBusObject *) connection);
Packit Service 1d8f1c
}
Packit Service 1d8f1c
Packit Service 1d8f1c
static void
Packit Service 1d8f1c
bus_connection_set_dbus_connection (BusConnection   *connection,
Packit Service 1d8f1c
                                    GDBusConnection *dbus_connection)
Packit Service 1d8f1c
{
Packit Service 1d8f1c
    connection->connection = dbus_connection;
Packit Service 1d8f1c
    g_object_ref (connection->connection);
Packit Service 1d8f1c
    g_object_set_qdata_full ((GObject *) dbus_connection,
Packit Service 1d8f1c
                             BUS_CONNECTION_QUARK,
Packit Service 1d8f1c
                             g_object_ref (connection),
Packit Service 1d8f1c
                             (GDestroyNotify) g_object_unref);
Packit Service 1d8f1c
    g_signal_connect (connection->connection, "closed",
Packit Service 1d8f1c
                G_CALLBACK (bus_connection_dbus_connection_closed_cb), connection);
Packit Service 1d8f1c
}
Packit Service 1d8f1c
Packit Service 1d8f1c
static GQuark
Packit Service 1d8f1c
bus_connection_quark (void)
Packit Service 1d8f1c
{
Packit Service 1d8f1c
    static GQuark quark = 0;
Packit Service 1d8f1c
    if (quark == 0) {
Packit Service 1d8f1c
        quark = g_quark_from_static_string ("BUS_CONNECTION");
Packit Service 1d8f1c
    }
Packit Service 1d8f1c
    return quark;
Packit Service 1d8f1c
}
Packit Service 1d8f1c
Packit Service 1d8f1c
BusConnection *
Packit Service 1d8f1c
bus_connection_new (GDBusConnection *dbus_connection)
Packit Service 1d8f1c
{
Packit Service 1d8f1c
    g_return_val_if_fail (bus_connection_lookup (dbus_connection) == NULL, NULL);
Packit Service 1d8f1c
    BusConnection *connection = BUS_CONNECTION (g_object_new (BUS_TYPE_CONNECTION, NULL));
Packit Service 1d8f1c
    bus_connection_set_dbus_connection (connection, dbus_connection);
Packit Service 1d8f1c
    return connection;
Packit Service 1d8f1c
}
Packit Service 1d8f1c
Packit Service 1d8f1c
BusConnection *
Packit Service 1d8f1c
bus_connection_lookup (GDBusConnection *dbus_connection)
Packit Service 1d8f1c
{
Packit Service 1d8f1c
    g_return_val_if_fail (G_IS_DBUS_CONNECTION (dbus_connection), NULL);
Packit Service 1d8f1c
    return (BusConnection *) g_object_get_qdata ((GObject *) dbus_connection,
Packit Service 1d8f1c
                    BUS_CONNECTION_QUARK);
Packit Service 1d8f1c
}
Packit Service 1d8f1c
Packit Service 1d8f1c
const gchar *
Packit Service 1d8f1c
bus_connection_get_unique_name (BusConnection   *connection)
Packit Service 1d8f1c
{
Packit Service 1d8f1c
    return connection->unique_name;
Packit Service 1d8f1c
}
Packit Service 1d8f1c
Packit Service 1d8f1c
void
Packit Service 1d8f1c
bus_connection_set_unique_name (BusConnection   *connection,
Packit Service 1d8f1c
                                const gchar     *name)
Packit Service 1d8f1c
{
Packit Service 1d8f1c
    g_assert (connection->unique_name == NULL); /* we don't allow rewriting the unique_name. */
Packit Service 1d8f1c
    connection->unique_name = g_strdup (name);
Packit Service 1d8f1c
}
Packit Service 1d8f1c
Packit Service 1d8f1c
const GList *
Packit Service 1d8f1c
bus_connection_get_names (BusConnection   *connection)
Packit Service 1d8f1c
{
Packit Service 1d8f1c
    return connection->names;
Packit Service 1d8f1c
}
Packit Service 1d8f1c
Packit Service 1d8f1c
const gchar *
Packit Service 1d8f1c
bus_connection_add_name (BusConnection     *connection,
Packit Service 1d8f1c
                         const gchar       *name)
Packit Service 1d8f1c
{
Packit Service 1d8f1c
    gchar *new_name;
Packit Service 1d8f1c
Packit Service 1d8f1c
    new_name = g_strdup (name);
Packit Service 1d8f1c
    connection->names = g_list_append (connection->names, new_name);
Packit Service 1d8f1c
Packit Service 1d8f1c
    return new_name;
Packit Service 1d8f1c
}
Packit Service 1d8f1c
Packit Service 1d8f1c
gboolean
Packit Service 1d8f1c
bus_connection_remove_name (BusConnection     *connection,
Packit Service 1d8f1c
                            const gchar       *name)
Packit Service 1d8f1c
{
Packit Service 1d8f1c
    GList *list = g_list_find_custom (connection->names, name, (GCompareFunc) g_strcmp0);
Packit Service 1d8f1c
Packit Service 1d8f1c
    if (list) {
Packit Service 1d8f1c
        g_free (list->data);
Packit Service 1d8f1c
        connection->names = g_list_delete_link (connection->names, list);
Packit Service 1d8f1c
        return TRUE;
Packit Service 1d8f1c
    }
Packit Service 1d8f1c
    return FALSE;
Packit Service 1d8f1c
}
Packit Service 1d8f1c
Packit Service 1d8f1c
gboolean
Packit Service 1d8f1c
bus_connection_has_name (BusConnection     *connection,
Packit Service 1d8f1c
                         const gchar       *name)
Packit Service 1d8f1c
{
Packit Service 1d8f1c
    GList *list = g_list_find_custom (connection->names, name, (GCompareFunc) g_strcmp0);
Packit Service 1d8f1c
    return list != NULL;
Packit Service 1d8f1c
}
Packit Service 1d8f1c
Packit Service 1d8f1c
GDBusConnection *
Packit Service 1d8f1c
bus_connection_get_dbus_connection (BusConnection *connection)
Packit Service 1d8f1c
{
Packit Service 1d8f1c
    g_assert (BUS_IS_CONNECTION (connection));
Packit Service 1d8f1c
    return connection->connection;
Packit Service 1d8f1c
}
Packit Service 1d8f1c
Packit Service 1d8f1c
void
Packit Service 1d8f1c
bus_connection_set_filter (BusConnection             *connection,
Packit Service 1d8f1c
                           GDBusMessageFilterFunction filter_func,
Packit Service 1d8f1c
                           gpointer                   user_data,
Packit Service 1d8f1c
                           GDestroyNotify             user_data_free_func)
Packit Service 1d8f1c
{
Packit Service 1d8f1c
    g_assert (BUS_IS_CONNECTION (connection));
Packit Service 1d8f1c
Packit Service 1d8f1c
    if (connection->filter_id != 0) {
Packit Service 1d8f1c
        g_dbus_connection_remove_filter (connection->connection, connection->filter_id);
Packit Service 1d8f1c
        connection->filter_id = 0;
Packit Service 1d8f1c
    }
Packit Service 1d8f1c
Packit Service 1d8f1c
    if (filter_func != NULL) {
Packit Service 1d8f1c
        connection->filter_id = g_dbus_connection_add_filter (connection->connection,
Packit Service 1d8f1c
                                                              filter_func,
Packit Service 1d8f1c
                                                              user_data,
Packit Service 1d8f1c
                                                              user_data_free_func);
Packit Service 1d8f1c
        /* Note: g_dbus_connection_add_filter seems not to return zero as a valid id. */
Packit Service 1d8f1c
    }
Packit Service 1d8f1c
}