ibus/bus/connection.c

243 строки
7.8 KiB
C
Исходник Обычный вид История

/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
2009-02-05 05:39:56 +03:00
/* vim:set et sts=4: */
/* bus - The Input Bus
2010-01-09 22:29:59 +03:00
* Copyright (C) 2008-2010 Peng Huang <shawn.p.huang@gmail.com>
* Copyright (C) 2008-2010 Red Hat, Inc.
2009-02-05 05:39:56 +03:00
*
* 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 License, 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.
*/
2009-06-05 16:34:35 +04:00
#include <unistd.h>
2009-02-05 05:39:56 +03:00
#include "connection.h"
#include "matchrule.h"
struct _BusConnection {
IBusObject parent;
/* instance members */
/* underlying GDBus connetion */
GDBusConnection *connection;
/* a unique name of the connection like ":1.0" */
gchar *unique_name;
/* list for well known names */
GList *names;
guint filter_id;
};
struct _BusConnectionClass {
IBusObjectClass parent;
2009-02-05 05:39:56 +03:00
/* class members */
2009-02-05 05:39:56 +03:00
};
/* static guint _signals[LAST_SIGNAL] = { 0 }; */
2009-02-05 05:39:56 +03:00
/* functions prototype */
static void bus_connection_destroy (BusConnection *connection);
static void bus_connection_set_dbus_connection
(BusConnection *connection,
GDBusConnection *dbus_connection);
static void bus_connection_dbus_connection_closed_cb
(GDBusConnection *dbus_connection,
gboolean remote_peer_vanished,
GError *error,
BusConnection *connection);
static GQuark bus_connection_quark (void);
2009-02-05 05:39:56 +03:00
#define BUS_CONNECTION_QUARK (bus_connection_quark ())
G_DEFINE_TYPE (BusConnection, bus_connection, IBUS_TYPE_OBJECT)
2009-02-05 05:39:56 +03:00
static void
2010-11-03 07:37:57 +03:00
bus_connection_class_init (BusConnectionClass *class)
2009-02-05 05:39:56 +03:00
{
2010-11-03 07:37:57 +03:00
IBusObjectClass *ibus_object_class = IBUS_OBJECT_CLASS (class);
2009-02-05 05:39:56 +03:00
ibus_object_class->destroy = (IBusObjectDestroyFunc) bus_connection_destroy;
}
static void
bus_connection_init (BusConnection *connection)
{
}
static void
bus_connection_destroy (BusConnection *connection)
{
if (connection->connection) {
/* disconnect from closed signal */
g_signal_handlers_disconnect_by_func (connection->connection,
G_CALLBACK (bus_connection_dbus_connection_closed_cb), connection);
/* remove filter */
bus_connection_set_filter (connection, NULL, NULL, NULL);
/* disconnect busconnection with dbus connection */
g_object_set_qdata ((GObject *)connection->connection, BUS_CONNECTION_QUARK, NULL);
if (!g_dbus_connection_is_closed (connection->connection)) {
g_dbus_connection_close (connection->connection, NULL, NULL, NULL);
}
g_object_unref (connection->connection);
connection->connection = NULL;
}
2009-02-05 05:39:56 +03:00
2009-06-24 03:33:53 +04:00
if (connection->unique_name) {
g_free (connection->unique_name);
connection->unique_name = NULL;
2009-02-05 05:39:56 +03:00
}
g_list_foreach (connection->names, (GFunc) g_free, NULL);
2009-06-24 03:33:53 +04:00
g_list_free (connection->names);
connection->names = NULL;
IBUS_OBJECT_CLASS(bus_connection_parent_class)->destroy (IBUS_OBJECT (connection));
2009-02-05 05:39:56 +03:00
}
static void
bus_connection_dbus_connection_closed_cb (GDBusConnection *dbus_connection,
gboolean remote_peer_vanished,
GError *error,
BusConnection *connection)
{
ibus_object_destroy ((IBusObject *) connection);
}
static void
bus_connection_set_dbus_connection (BusConnection *connection,
GDBusConnection *dbus_connection)
2009-02-05 05:39:56 +03:00
{
connection->connection = dbus_connection;
g_object_ref (connection->connection);
g_object_set_qdata_full ((GObject *) dbus_connection,
BUS_CONNECTION_QUARK,
g_object_ref (connection),
(GDestroyNotify) g_object_unref);
g_signal_connect (connection->connection, "closed",
G_CALLBACK (bus_connection_dbus_connection_closed_cb), connection);
}
2009-02-05 05:39:56 +03:00
static GQuark
bus_connection_quark (void)
{
2010-11-03 07:37:57 +03:00
static GQuark quark = 0;
if (quark == 0) {
quark = g_quark_from_static_string ("BUS_CONNECTION");
}
return quark;
}
2009-02-05 05:39:56 +03:00
BusConnection *
bus_connection_new (GDBusConnection *dbus_connection)
{
g_return_val_if_fail (bus_connection_lookup (dbus_connection) == NULL, NULL);
BusConnection *connection = BUS_CONNECTION (g_object_new (BUS_TYPE_CONNECTION, NULL));
bus_connection_set_dbus_connection (connection, dbus_connection);
return connection;
2009-02-05 05:39:56 +03:00
}
BusConnection *
bus_connection_lookup (GDBusConnection *dbus_connection)
2009-02-05 05:39:56 +03:00
{
g_return_val_if_fail (G_IS_DBUS_CONNECTION (dbus_connection), NULL);
return (BusConnection *) g_object_get_qdata ((GObject *) dbus_connection,
BUS_CONNECTION_QUARK);
2009-02-05 05:39:56 +03:00
}
const gchar *
bus_connection_get_unique_name (BusConnection *connection)
{
2009-06-24 03:33:53 +04:00
return connection->unique_name;
2009-02-05 05:39:56 +03:00
}
void
bus_connection_set_unique_name (BusConnection *connection,
const gchar *name)
{
g_assert (connection->unique_name == NULL); /* we don't allow rewriting the unique_name. */
2009-06-24 03:33:53 +04:00
connection->unique_name = g_strdup (name);
2009-02-05 05:39:56 +03:00
}
const GList *
bus_connection_get_names (BusConnection *connection)
{
2009-06-24 03:33:53 +04:00
return connection->names;
2009-02-05 05:39:56 +03:00
}
const gchar *
bus_connection_add_name (BusConnection *connection,
const gchar *name)
2009-02-05 05:39:56 +03:00
{
gchar *new_name;
new_name = g_strdup (name);
2009-06-24 03:33:53 +04:00
connection->names = g_list_append (connection->names, new_name);
2009-02-05 05:39:56 +03:00
return new_name;
}
gboolean
bus_connection_remove_name (BusConnection *connection,
const gchar *name)
2009-02-05 05:39:56 +03:00
{
GList *list = g_list_find_custom (connection->names, name, (GCompareFunc) g_strcmp0);
2009-02-05 05:39:56 +03:00
if (list) {
g_free (list->data);
connection->names = g_list_delete_link (connection->names, list);
2009-02-05 05:39:56 +03:00
return TRUE;
}
return FALSE;
}
gboolean
bus_connection_has_name (BusConnection *connection,
const gchar *name)
{
GList *list = g_list_find_custom (connection->names, name, (GCompareFunc) g_strcmp0);
return list != NULL;
}
GDBusConnection *
bus_connection_get_dbus_connection (BusConnection *connection)
{
g_assert (BUS_IS_CONNECTION (connection));
return connection->connection;
}
void
bus_connection_set_filter (BusConnection *connection,
GDBusMessageFilterFunction filter_func,
gpointer user_data,
GDestroyNotify user_data_free_func)
{
g_assert (BUS_IS_CONNECTION (connection));
if (connection->filter_id != 0) {
g_dbus_connection_remove_filter (connection->connection, connection->filter_id);
connection->filter_id = 0;
}
if (filter_func != NULL) {
connection->filter_id = g_dbus_connection_add_filter (connection->connection,
filter_func,
user_data,
user_data_free_func);
/* Note: g_dbus_connection_add_filter seems not to return zero as a valid id. */
}
}