From: Xavier Claessens Date: Sat, 19 Apr 2008 21:04:04 +0000 (+0000) Subject: Add tubes, tube and tube handler objects X-Git-Url: https://git.0d.be/?p=empathy.git;a=commitdiff_plain;h=e67348e46348936033e2230fed35ee06930f9031 Add tubes, tube and tube handler objects svn path=/trunk/; revision=972 --- diff --git a/libempathy/Makefile.am b/libempathy/Makefile.am index e18088cf..f3530994 100644 --- a/libempathy/Makefile.am +++ b/libempathy/Makefile.am @@ -39,7 +39,10 @@ libempathy_la_SOURCES = \ empathy-log-manager.c \ empathy-irc-network-manager.c \ empathy-irc-network.c \ - empathy-irc-server.c + empathy-irc-server.c \ + empathy-tubes.c \ + empathy-tube.c \ + empathy-tube-handler.c # do not distribute generated files nodist_libempathy_la_SOURCES =\ @@ -77,7 +80,10 @@ libempathy_headers = \ empathy-log-manager.h \ empathy-irc-network-manager.h \ empathy-irc-network.h \ - empathy-irc-server.h + empathy-irc-server.h \ + empathy-tubes.h \ + empathy-tube.h \ + empathy-tube-handler.h libempathy_includedir = $(includedir)/libempathy/ libempathy_include_HEADERS = \ diff --git a/libempathy/empathy-tube-handler.c b/libempathy/empathy-tube-handler.c new file mode 100644 index 00000000..88095fec --- /dev/null +++ b/libempathy/empathy-tube-handler.c @@ -0,0 +1,184 @@ +/* + * Copyright (C) 2008 Collabora 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.1 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: Xavier Claessens + * Elliot Fairweather + */ + +#include + +#include +#include +#include +#include + +#include + +#include "empathy-debug.h" +#include "empathy-tube.h" +#include "empathy-tube-handler.h" +#include "empathy-tubes.h" + +#define DEBUG_DOMAIN "EmpathyTubeHandler" + + +enum +{ + NEW_TUBE, + LAST_SIGNAL +}; + +static void empathy_tube_handler_iface_init (EmpSvcTubeHandlerClass *klass); +static guint signals[LAST_SIGNAL]; + +G_DEFINE_TYPE_WITH_CODE (EmpathyTubeHandler, empathy_tube_handler, + G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE (EMP_TYPE_SVC_TUBE_HANDLER, + empathy_tube_handler_iface_init)) + +typedef struct +{ + EmpathyTubeHandler *thandler; + gchar *bus_name; + gchar *connection; + gchar *channel; + guint handle_type; + guint handle; + guint id; +} IdleData; + +static gboolean +empathy_tube_handler_handle_tube_idle_cb (gpointer data) +{ + IdleData *idle_data = data; + TpConnection *connection; + TpChannel *channel; + EmpathyTubes *tubes; + EmpathyTube *tube; + static TpDBusDaemon *daemon = NULL; + + if (!daemon) + daemon = tp_dbus_daemon_new (tp_get_bus ()); + + connection = tp_connection_new (daemon, idle_data->bus_name, + idle_data->connection, NULL); + channel = tp_channel_new (connection, idle_data->channel, + TP_IFACE_CHANNEL_TYPE_TUBES, idle_data->handle_type, + idle_data->handle, NULL); + tp_channel_run_until_ready (channel, NULL, NULL); + + tubes = empathy_tubes_new (channel); + tube = empathy_tubes_get_tube (tubes, idle_data->id); + + empathy_debug (DEBUG_DOMAIN, "New tube to be handled"); + + g_signal_emit (idle_data->thandler, signals[NEW_TUBE], 0, tube); + + g_object_unref (tube); + g_object_unref (tubes); + g_object_unref (channel); + g_object_unref (connection); + g_free (idle_data->bus_name); + g_free (idle_data->connection); + g_free (idle_data->channel); + g_slice_free (IdleData, idle_data); + + return FALSE; +} + + +static void +empathy_tube_handler_handle_tube (EmpSvcTubeHandler *self, + const gchar *bus_name, + const gchar *connection, + const gchar *channel, + guint handle_type, + guint handle, + guint id, + DBusGMethodInvocation *context) +{ + EmpathyTubeHandler *thandler = EMPATHY_TUBE_HANDLER (self); + IdleData *data = g_slice_new (IdleData); + + data->thandler = thandler; + data->bus_name = g_strdup (bus_name); + data->connection = g_strdup (connection); + data->channel = g_strdup (channel); + data->handle_type = handle_type; + data->handle = handle; + data->id = id; + + g_idle_add_full (G_PRIORITY_HIGH, empathy_tube_handler_handle_tube_idle_cb, + data, NULL); + + emp_svc_tube_handler_return_from_handle_tube (context); +} + + +static void +empathy_tube_handler_class_init (EmpathyTubeHandlerClass *klass) +{ + signals[NEW_TUBE] = + g_signal_new ("new-tube", G_OBJECT_CLASS_TYPE (klass), + G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__OBJECT, + G_TYPE_NONE, 1, EMPATHY_TYPE_TUBE); +} + + +static void +empathy_tube_handler_iface_init (EmpSvcTubeHandlerClass *klass) +{ + emp_svc_tube_handler_implement_handle_tube (klass, + empathy_tube_handler_handle_tube); +} + + +static void +empathy_tube_handler_init (EmpathyTubeHandler *thandler) +{ +} + + +EmpathyTubeHandler * +empathy_tube_handler_new (const gchar *bus_name, + const gchar *object_path) +{ + EmpathyTubeHandler *thandler; + DBusGProxy *proxy; + guint result; + GError *error = NULL; + + proxy = dbus_g_proxy_new_for_name (tp_get_bus (), DBUS_SERVICE_DBUS, + DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS); + + if (!dbus_g_proxy_call (proxy, "RequestName", &error, + G_TYPE_STRING, bus_name, G_TYPE_UINT, DBUS_NAME_FLAG_DO_NOT_QUEUE, + G_TYPE_INVALID, G_TYPE_UINT, &result, G_TYPE_INVALID)) + { + empathy_debug (DEBUG_DOMAIN, "Failed to request name: %s", + error ? error->message : "No error given"); + g_clear_error (&error); + return NULL; + } + + g_object_unref (proxy); + + thandler = g_object_new (EMPATHY_TYPE_TUBE_HANDLER, NULL); + dbus_g_connection_register_g_object (tp_get_bus (), object_path, + G_OBJECT (thandler)); + + return thandler; +} diff --git a/libempathy/empathy-tube-handler.h b/libempathy/empathy-tube-handler.h new file mode 100644 index 00000000..027e8f8e --- /dev/null +++ b/libempathy/empathy-tube-handler.h @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2008 Collabora 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.1 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: Xavier Claessens + * Elliot Fairweather + */ + +#ifndef __EMPATHY_TUBE_HANDLER_H__ +#define __EMPATHY_TUBE_HANDLER_H__ + +#include + +G_BEGIN_DECLS + +#define EMPATHY_TYPE_TUBE_HANDLER (empathy_tube_handler_get_type ()) +#define EMPATHY_TUBE_HANDLER(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), \ + EMPATHY_TYPE_TUBE_HANDLER, EmpathyTubeHandler)) +#define EMPATHY_TUBE_HANDLER_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), \ + EMPATHY_TYPE_TUBE_HANDLER, EmpathyTubeHandlerClass)) +#define EMPATHY_IS_TUBE_HANDLER(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), \ + EMPATHY_TYPE_TUBE_HANDLER)) +#define EMPATHY_IS_TUBE_HANDLER_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), \ + EMPATHY_TYPE_TUBE_HANDLER)) +#define EMPATHY_TUBE_HANDLER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), \ + EMPATHY_TYPE_TUBE_HANDLER, EmpathyTubeHandlerClass)) + +typedef struct _EmpathyTubeHandler EmpathyTubeHandler; +typedef struct _EmpathyTubeHandlerClass EmpathyTubeHandlerClass; + +struct _EmpathyTubeHandler { + GObject parent; +}; + +struct _EmpathyTubeHandlerClass { + GObjectClass parent_class; +}; + +GType empathy_tube_handler_get_type (void) G_GNUC_CONST; +EmpathyTubeHandler *empathy_tube_handler_new (const gchar *bus_name, + const gchar *object_path); + +G_END_DECLS + +#endif /* __EMPATHY_TUBE_HANDLER_H__ */ diff --git a/libempathy/empathy-tube.c b/libempathy/empathy-tube.c new file mode 100644 index 00000000..e4081c30 --- /dev/null +++ b/libempathy/empathy-tube.c @@ -0,0 +1,494 @@ +/* + * Copyright (C) 2008 Collabora 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.1 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: Guillaume Desmottes + * Elliot Fairweather + */ + + +#include +#include + +#include "empathy-contact.h" +#include "empathy-contact-factory.h" +#include "empathy-debug.h" +#include "empathy-enum-types.h" +#include "empathy-tubes.h" +#include "empathy-tube.h" +#include "empathy-utils.h" + +#define DEBUG_DOMAIN "EmpathyTube" + +#define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_TUBE, \ + EmpathyTubePriv)) + +typedef struct _EmpathyTubePriv EmpathyTubePriv; + +struct _EmpathyTubePriv +{ + EmpathyTubes *tubes; + guint id; + guint initiator; + guint type; + const gchar *service; + GHashTable *parameters; + guint state; + EmpathyContact *initiator_contact; + gboolean closed; +}; + +enum +{ + LAST_SIGNAL +}; + +enum +{ + PROP_0, + PROP_TUBES, + PROP_ID, + PROP_INITIATOR, + PROP_TYPE, + PROP_SERVICE, + PROP_PARAMETERS, + PROP_STATE, + PROP_INITIATOR_CONTACT, + PROP_CLOSED +}; + +G_DEFINE_TYPE (EmpathyTube, empathy_tube, G_TYPE_OBJECT) + + +static void +empathy_tube_closed_cb (EmpathyTubes *tubes, + guint id, + gpointer data) +{ + EmpathyTube *tube = EMPATHY_TUBE (data); + EmpathyTubePriv *priv = GET_PRIV (tube); + + if (id != priv->id) + return; + + empathy_debug (DEBUG_DOMAIN, "Tube closed"); + + priv->closed = TRUE; +} + +static void +empathy_tube_state_changed_cb (EmpathyTubes *tubes, + guint id, + guint state, + gpointer data) +{ + EmpathyTube *tube = EMPATHY_TUBE (data); + EmpathyTubePriv *priv = GET_PRIV (tube); + + if (id != priv->id) + return; + + empathy_debug (DEBUG_DOMAIN, "Tube state changed"); + priv->state = state; +} + + +static void +empathy_tube_stream_tube_new_connection_cb (EmpathyTubes *tubes, + guint id, + guint handle, + gpointer data) +{ + EmpathyTube *tube = EMPATHY_TUBE (data); + EmpathyTubePriv *priv = GET_PRIV (tube); + + if (id != priv->id) + return; + + empathy_debug (DEBUG_DOMAIN, "Stream tube new connection"); +} + + +static void +empathy_tube_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + EmpathyTubePriv *priv = GET_PRIV (object); + + switch (prop_id) + { + case PROP_TUBES: + priv->tubes = g_value_dup_object (value); + break; + case PROP_ID: + priv->id = g_value_get_uint (value); + break; + case PROP_INITIATOR: + priv->initiator = g_value_get_uint (value); + break; + case PROP_TYPE: + priv->type = g_value_get_uint (value); + break; + case PROP_SERVICE: + priv->service = g_value_dup_string (value); + break; + case PROP_PARAMETERS: + priv->parameters = g_value_dup_boxed (value); + break; + case PROP_STATE: + priv->state = g_value_get_uint (value); + break; + case PROP_CLOSED: + priv->closed = g_value_get_boolean (value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + + +static void +empathy_tube_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + EmpathyTubePriv *priv = GET_PRIV (object); + + switch (prop_id) + { + case PROP_TUBES: + g_value_set_object (value, priv->tubes); + break; + case PROP_ID: + g_value_set_uint (value, priv->id); + break; + case PROP_INITIATOR: + g_value_set_uint (value, priv->initiator); + break; + case PROP_TYPE: + g_value_set_uint (value, priv->type); + break; + case PROP_SERVICE: + g_value_set_string (value, priv->service); + break; + case PROP_PARAMETERS: + g_value_set_boxed (value, priv->parameters); + break; + case PROP_STATE: + g_value_set_uint (value, priv->state); + break; + case PROP_INITIATOR_CONTACT: + g_value_set_object (value, priv->initiator_contact); + break; + case PROP_CLOSED: + g_value_set_boolean (value, priv->closed); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + + +static GObject * +empathy_tube_constructor (GType type, + guint n_props, + GObjectConstructParam *props) +{ + GObject *self = G_OBJECT_CLASS (empathy_tube_parent_class)->constructor + (type, n_props, props); + EmpathyTubePriv *priv = GET_PRIV (self); + TpChannel *channel; + TpConnection *connection; + MissionControl *mc = empathy_mission_control_new (); + EmpathyContactFactory *factory = empathy_contact_factory_new (); + McAccount *account; + + g_object_get (G_OBJECT (priv->tubes), "channel", &channel, NULL); + g_object_get (G_OBJECT (channel), "connection", &connection, NULL); + + account = mission_control_get_account_for_tpconnection (mc, connection, NULL); + + g_assert (connection != NULL); + g_assert (priv->initiator != 0); + priv->initiator_contact = + g_object_ref (empathy_contact_factory_get_from_handle (factory, + account, priv->initiator)); + g_assert (priv->initiator_contact != NULL); + + g_signal_connect (priv->tubes, "tube-closed", + G_CALLBACK (empathy_tube_closed_cb), (gpointer) self); + g_signal_connect (priv->tubes, "tube-state-changed", + G_CALLBACK (empathy_tube_state_changed_cb), (gpointer) self); + g_signal_connect (priv->tubes, "stream-tube-new-connection", + G_CALLBACK (empathy_tube_stream_tube_new_connection_cb), + (gpointer) self); + + priv->closed = FALSE; + + g_object_unref (connection); + g_object_unref (channel); + g_object_unref (mc); + + return self; +} + + +static void +empathy_tube_dispose (GObject *object) +{ + EmpathyTube *tube = EMPATHY_TUBE (object); + EmpathyTubePriv *priv = GET_PRIV (tube); + + empathy_debug (DEBUG_DOMAIN, "Disposing: %p", object); + + if (priv->tubes) + { + g_signal_handlers_disconnect_by_func (priv->tubes, + empathy_tube_closed_cb, object); + g_signal_handlers_disconnect_by_func (priv->tubes, + empathy_tube_state_changed_cb, object); + g_signal_handlers_disconnect_by_func (priv->tubes, + empathy_tube_stream_tube_new_connection_cb, object); + g_object_unref (priv->tubes); + priv->tubes = NULL; + } + + g_object_unref (priv->initiator_contact); + + (G_OBJECT_CLASS (empathy_tube_parent_class)->dispose) (object); +} + + +static void +empathy_tube_finalize (GObject *object) +{ + empathy_debug (DEBUG_DOMAIN, "Finalizing: %p", object); + + G_OBJECT_CLASS (empathy_tube_parent_class)->finalize (object); +} + + +static void +empathy_tube_class_init (EmpathyTubeClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->constructor = empathy_tube_constructor; + object_class->dispose = empathy_tube_dispose; + object_class->finalize = empathy_tube_finalize; + object_class->set_property = empathy_tube_set_property; + object_class->get_property = empathy_tube_get_property; + + g_object_class_install_property (object_class, PROP_TUBES, + g_param_spec_object ("tubes", "tubes", "tubes", EMPATHY_TYPE_TUBES, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); + + g_object_class_install_property (object_class, PROP_ID, + g_param_spec_uint ("id", "id", "id", 0, G_MAXUINT, 0, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | + G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)); + + g_object_class_install_property (object_class, PROP_INITIATOR, + g_param_spec_uint ("initiator", "initiator", "initiator", + 0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | + G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)); + + g_object_class_install_property (object_class, PROP_TYPE, + g_param_spec_uint ("type", "type", "type", 0, G_MAXUINT, 0, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | + G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)); + + g_object_class_install_property (object_class, PROP_SERVICE, + g_param_spec_string ("service", "service", "service", NULL, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | + G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)); + + g_object_class_install_property (object_class, PROP_PARAMETERS, + g_param_spec_boxed ("parameters", "parameters", "parameters", + G_TYPE_HASH_TABLE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | + G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)); + + g_object_class_install_property (object_class, PROP_STATE, + g_param_spec_uint ("state", "state", "state", 0, G_MAXUINT, 0, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | + G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)); + + g_object_class_install_property (object_class, PROP_INITIATOR_CONTACT, + g_param_spec_object ("initiator-contact", "initiator contact", + "initiator contact", EMPATHY_TYPE_CONTACT, G_PARAM_READABLE | + G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)); + + g_object_class_install_property (object_class, PROP_CLOSED, + g_param_spec_boolean ("closed", "closed", "closed", FALSE, + G_PARAM_READABLE | G_PARAM_STATIC_NAME | + G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)); + + g_type_class_add_private (klass, sizeof (EmpathyTubePriv)); +} + + +static void +empathy_tube_init (EmpathyTube *tubes) +{ +} + + +void +empathy_tube_close (EmpathyTube *tube) +{ + EmpathyTubePriv *priv = GET_PRIV (tube); + TpChannel *channel; + GError *error = NULL; + + empathy_debug (DEBUG_DOMAIN, "Closing tube - id: %d", priv->id); + + g_object_get (G_OBJECT (priv->tubes), "channel", &channel, NULL); + + if (!tp_cli_channel_type_tubes_run_close_tube (channel, -1, priv->id, + &error, NULL)) + { + empathy_debug (DEBUG_DOMAIN, "Couldn't close tube: %s", error->message); + g_clear_error (&error); + } + + g_object_unref (channel); +} + + +static void +empathy_tube_accept_stream_tube (EmpathyTube *tube, + TpSocketAddressType address_type, + TpSocketAccessControl access_type, + GValue *control_param) +{ + EmpathyTubePriv *priv = GET_PRIV (tube); + GValue *socket = g_new0 (GValue, 1); + TpChannel *channel; + GError *error = NULL; + + empathy_debug (DEBUG_DOMAIN, "Accepting stream tube - id: %d", priv->id); + + g_object_get (G_OBJECT (priv->tubes), "channel", &channel, NULL); + + if (!tp_cli_channel_type_tubes_run_accept_stream_tube (channel, -1, priv->id, + address_type, access_type, control_param, &socket, &error, NULL)) + { + empathy_debug (DEBUG_DOMAIN, "Error accepting tube: %s", error->message); + g_clear_error (&error); + } + + g_object_unref (channel); + g_free (socket); +} + + +void +empathy_tube_accept_stream_tube_unix (EmpathyTube *tube) +{ + GValue *control_param = g_new0 (GValue, 1); + + empathy_debug (DEBUG_DOMAIN, "Accepting stream tube - UNIX, localhost"); + + g_value_init (control_param, G_TYPE_STRING); + empathy_tube_accept_stream_tube (tube, TP_SOCKET_ADDRESS_TYPE_UNIX, + TP_SOCKET_ACCESS_CONTROL_LOCALHOST, control_param); + + g_free (control_param); +} + + +void +empathy_tube_accept_stream_tube_ipv4 (EmpathyTube *tube) +{ + GValue *control_param = g_new0 (GValue, 1); + + empathy_debug (DEBUG_DOMAIN, "Accepting stream tube - IPv4, localhost"); + + g_value_init (control_param, G_TYPE_STRING); + empathy_tube_accept_stream_tube (tube, TP_SOCKET_ADDRESS_TYPE_IPV4, + TP_SOCKET_ACCESS_CONTROL_LOCALHOST, control_param); + + g_free (control_param); +} + + +gchar * +empathy_tube_get_stream_tube_socket_unix (EmpathyTube *tube) +{ + EmpathyTubePriv *priv = GET_PRIV (tube); + GValue *address = g_new0 (GValue, 1); + guint address_type; + gchar *address_name = NULL; + TpChannel *channel; + GError *error = NULL; + + empathy_debug (DEBUG_DOMAIN, "Getting stream tube socket address"); + + g_object_get (G_OBJECT (priv->tubes), "channel", &channel, NULL); + + if (!tp_cli_channel_type_tubes_run_get_stream_tube_socket_address (channel, + -1, priv->id, &address_type, &address, &error, NULL)) + { + empathy_debug (DEBUG_DOMAIN, "Couldn't get socket address: %s", + error->message); + g_clear_error (&error); + return NULL; + } + + dbus_g_type_struct_get (address, 0, &address_name, G_MAXUINT); + + empathy_debug (DEBUG_DOMAIN, "UNIX Socket - %s", address_name); + + g_object_unref (channel); + g_free (address); + return address_name; +} + + +void +empathy_tube_get_stream_tube_socket_ipv4 (EmpathyTube *tube, + gchar **hostname, + guint *port) +{ + EmpathyTubePriv *priv = GET_PRIV (tube); + GValue *address = g_new0 (GValue, 1); + guint address_type; + TpChannel *channel; + GError *error = NULL; + + empathy_debug (DEBUG_DOMAIN, "Getting stream tube socket address"); + + g_object_get (G_OBJECT (priv->tubes), "channel", &channel, NULL); + + if (!tp_cli_channel_type_tubes_run_get_stream_tube_socket_address (channel, + -1, priv->id, &address_type, &address, &error, NULL)) + { + empathy_debug (DEBUG_DOMAIN, "Couldn't get socket address: %s", + error->message); + g_clear_error (&error); + return; + } + + dbus_g_type_struct_get (address, 0, hostname, 1, port, G_MAXUINT); + + g_object_unref (channel); + g_free (address); +} diff --git a/libempathy/empathy-tube.h b/libempathy/empathy-tube.h new file mode 100644 index 00000000..98974aa1 --- /dev/null +++ b/libempathy/empathy-tube.h @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2008 Collabora 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.1 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: Guillaume Desmottes + * Elliot Fairweather + */ + +#ifndef __EMPATHY_TUBE_H__ +#define __EMPATHY_TUBE_H__ + +#include + +G_BEGIN_DECLS + +#define EMPATHY_TYPE_TUBE (empathy_tube_get_type ()) +#define EMPATHY_TUBE(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), \ + EMPATHY_TYPE_TUBE, EmpathyTube)) +#define EMPATHY_TUBE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), \ + EMPATHY_TYPE_TUBE, EmpathyTubeClass)) +#define EMPATHY_IS_TUBE(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), \ + EMPATHY_TYPE_TUBE)) +#define EMPATHY_IS_TUBE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), \ + EMPATHY_TYPE_TUBE)) +#define EMPATHY_TUBE_GET_CLASS(object) (G_TYPE_INSTANCE_GET_CLASS ((object), \ + EMPATHY_TYPE_TUBE, EmpathyTubeClass)) + +typedef struct _EmpathyTube EmpathyTube; +typedef struct _EmpathyTubeClass EmpathyTubeClass; + +struct _EmpathyTube { + GObject parent; +}; + +struct _EmpathyTubeClass { + GObjectClass parent_class; +}; + +GType empathy_tube_get_type (void) G_GNUC_CONST; + +void empathy_tube_close (EmpathyTube *tube); +void empathy_tube_accept_stream_tube_ipv4 (EmpathyTube *tube); +void empathy_tube_accept_stream_tube_unix (EmpathyTube *tube); +void empathy_tube_get_stream_tube_socket_ipv4 (EmpathyTube *tube, + gchar **hostname, guint *port); +gchar * empathy_tube_get_stream_tube_socket_unix (EmpathyTube *tube); + +G_END_DECLS + +#endif /* __EMPATHY_TUBE_H__ */ diff --git a/libempathy/empathy-tubes.c b/libempathy/empathy-tubes.c new file mode 100644 index 00000000..4b72a8da --- /dev/null +++ b/libempathy/empathy-tubes.c @@ -0,0 +1,418 @@ +/* + * Copyright (C) 2008 Collabora 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.1 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: Elliot Fairweather + * Guillaume Desmottes + */ + +#include +#include + +#include + +#include "empathy-contact.h" +#include "empathy-debug.h" +#include "empathy-marshal.h" +#include "empathy-tp-contact-factory.h" +#include "empathy-tube.h" +#include "empathy-tubes.h" +#include "empathy-utils.h" + +#define DEBUG_DOMAIN "EmpathyTubes" + +#define GET_PRIV(object) (G_TYPE_INSTANCE_GET_PRIVATE \ + ((object), EMPATHY_TYPE_TUBES, EmpathyTubesPriv)) + +typedef struct _EmpathyTubesPriv EmpathyTubesPriv; + +struct _EmpathyTubesPriv +{ + TpChannel *channel; +}; + +enum +{ + NEW_TUBE, + TUBE_CLOSED, + TUBE_STATE_CHANGED, + STREAM_TUBE_NEW_CONNECTION, + LAST_SIGNAL +}; + +enum +{ + PROP_0, + PROP_CHANNEL +}; + +static guint signals[LAST_SIGNAL]; + +G_DEFINE_TYPE (EmpathyTubes, empathy_tubes, G_TYPE_OBJECT) + + +static void +empathy_tubes_new_tube_cb (TpChannel *channel, + guint id, + guint initiator, + guint type, + const gchar *service, + GHashTable *params, + guint state, + gpointer data, + GObject *tubes) +{ + EmpathyTube *tube; + + empathy_debug (DEBUG_DOMAIN, + "New tube - id: %d, initiator: %d, type: %d, service: %s, state: %d", + id, initiator, type, service, state); + tube = g_object_new (EMPATHY_TYPE_TUBE, "tubes", tubes, "id", id, + "initiator", initiator, "type", type, "service", service, + "parameters", params, "state", state, NULL); + g_signal_emit (tubes, signals[NEW_TUBE], 0, tube); +} + + +static void +empathy_tubes_tube_closed_cb (TpChannel *channel, + guint id, + gpointer data, + GObject *tubes) +{ + empathy_debug (DEBUG_DOMAIN, "Tube closed - id: %d", id); + g_signal_emit (tubes, signals[TUBE_CLOSED], 0, id); +} + + +static void +empathy_tubes_stream_tube_new_connection_cb (TpChannel *channel, + guint id, + guint handle, + gpointer data, + GObject *tubes) +{ + empathy_debug (DEBUG_DOMAIN, + "Stream tube new connection - id: %d, handle: %d", id, handle); + g_signal_emit (tubes, signals[STREAM_TUBE_NEW_CONNECTION], 0, + id, handle); +} + + +static void +empathy_tubes_tube_state_changed_cb (TpChannel *channel, + guint id, + guint state, + gpointer data, + GObject *tubes) +{ + empathy_debug (DEBUG_DOMAIN, "Tube state changed - id: %d, state: %d", + id, state); + g_signal_emit (tubes, signals[TUBE_STATE_CHANGED], 0, id, state); +} + + +static void +empathy_tubes_channel_closed_cb (TpChannel *channel, + gpointer data) +{ + EmpathyTubes *tubes = EMPATHY_TUBES (data); + EmpathyTubesPriv *priv = GET_PRIV (tubes); + + empathy_debug (DEBUG_DOMAIN, "Channel closed"); + + g_signal_handlers_disconnect_by_func (priv->channel, + empathy_tubes_channel_closed_cb, tubes); + + // disconnect tubes interface signals? +} + + +static void +empathy_tubes_dispose (GObject *object) +{ + EmpathyTubes *tubes = EMPATHY_TUBES (object); + EmpathyTubesPriv *priv = GET_PRIV (tubes); + + empathy_debug (DEBUG_DOMAIN, "Disposing: %p", object); + + if (priv->channel) + { + g_signal_handlers_disconnect_by_func (priv->channel, + empathy_tubes_channel_closed_cb, object); + g_object_unref (priv->channel); + priv->channel = NULL; + } + + (G_OBJECT_CLASS (empathy_tubes_parent_class)->dispose) (object); +} + + +static void +empathy_tubes_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + EmpathyTubes *tubes = EMPATHY_TUBES (object); + EmpathyTubesPriv *priv = GET_PRIV (tubes); + + switch (prop_id) + { + case PROP_CHANNEL: + priv->channel = g_value_dup_object (value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + + +static void +empathy_tubes_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + EmpathyTubes *tubes = EMPATHY_TUBES (object); + EmpathyTubesPriv *priv = GET_PRIV (tubes); + + switch (prop_id) + { + case PROP_CHANNEL: + g_value_set_object (value, priv->channel); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + + +static GObject * +empathy_tubes_constructor (GType type, + guint n_construct_params, + GObjectConstructParam *construct_params) +{ + GObject *object = G_OBJECT_CLASS (empathy_tubes_parent_class)->constructor + (type, n_construct_params, construct_params); + EmpathyTubes *tubes = EMPATHY_TUBES (object); + EmpathyTubesPriv *priv = GET_PRIV (tubes); + + g_signal_connect (priv->channel, "closed", + G_CALLBACK (empathy_tubes_channel_closed_cb), tubes); + + tp_cli_channel_type_tubes_connect_to_new_tube (priv->channel, + empathy_tubes_new_tube_cb, NULL, NULL, G_OBJECT (tubes), NULL); + tp_cli_channel_type_tubes_connect_to_tube_closed (priv->channel, + empathy_tubes_tube_closed_cb, NULL, NULL, G_OBJECT (tubes), NULL); + tp_cli_channel_type_tubes_connect_to_tube_state_changed (priv->channel, + empathy_tubes_tube_state_changed_cb, NULL, NULL, G_OBJECT (tubes), NULL); + tp_cli_channel_type_tubes_connect_to_stream_tube_new_connection + (priv->channel, empathy_tubes_stream_tube_new_connection_cb, + NULL, NULL, G_OBJECT (tubes), NULL); + + object = G_OBJECT (tubes); + return object; +} + + +static void empathy_tubes_class_init (EmpathyTubesClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->constructor = empathy_tubes_constructor; + object_class->dispose = empathy_tubes_dispose; + object_class->set_property = empathy_tubes_set_property; + object_class->get_property = empathy_tubes_get_property; + + g_type_class_add_private (klass, sizeof (EmpathyTubesPriv)); + + signals[NEW_TUBE] = + g_signal_new ("new-tube", G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__OBJECT, + G_TYPE_NONE, 1, EMPATHY_TYPE_TUBE); + signals[TUBE_CLOSED] = + g_signal_new ("tube-closed", G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__UINT, + G_TYPE_NONE, 1, G_TYPE_UINT); + signals[TUBE_STATE_CHANGED] = + g_signal_new ("tube-state-changed", G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, 0, NULL, NULL, _empathy_marshal_VOID__UINT_UINT, + G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT); + signals[STREAM_TUBE_NEW_CONNECTION] = + g_signal_new ("stream-tube-new-connection", G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, 0, NULL, NULL, _empathy_marshal_VOID__UINT_UINT, + G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT); + + g_object_class_install_property (object_class, PROP_CHANNEL, + g_param_spec_object ("channel", "channel", "channel", + TP_TYPE_CHANNEL, G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | + G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)); +} + + +static void +empathy_tubes_init (EmpathyTubes *empathy_tubes) +{ +} + + +EmpathyTubes * +empathy_tubes_new (TpChannel *channel) +{ + return g_object_new (empathy_tubes_get_type (), "channel", channel, NULL); +} + + +guint +empathy_tubes_offer_stream_tube_ipv4 (EmpathyTubes *tubes, + gchar *host, + guint port, + gchar *service) +{ + EmpathyTubesPriv *priv = GET_PRIV (tubes); + GError *error = NULL; + GHashTable *params; + GValue *address; + GValue *control_param; + guint id; + + empathy_debug (DEBUG_DOMAIN, "Offering a new stream tube"); + + params = g_hash_table_new (g_str_hash, g_str_equal); + + address = g_new0 (GValue, 1); + g_value_init (address, + dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING, G_TYPE_UINT, + G_TYPE_INVALID)); + g_value_take_boxed (address, + dbus_g_type_specialized_construct (dbus_g_type_get_struct ("GValueArray", + G_TYPE_STRING, G_TYPE_UINT, G_TYPE_INVALID))); + dbus_g_type_struct_set (address, 0, host, 1, port, G_MAXUINT); + + /* localhost access control, variant is ignored */ + control_param = g_new0 (GValue, 1); + g_value_init (control_param, G_TYPE_STRING); + + if (!tp_cli_channel_type_tubes_run_offer_stream_tube (priv->channel, -1, + service, params, TP_SOCKET_ADDRESS_TYPE_IPV4, address, + TP_SOCKET_ACCESS_CONTROL_LOCALHOST, control_param, &id, &error, NULL)) + { + empathy_debug (DEBUG_DOMAIN, "Couldn't offer tube: %s", error->message); + g_clear_error (&error); + return 0; + } + + return id; +} + + +GSList * +empathy_tubes_list_tubes (EmpathyTubes *tubes) +{ + EmpathyTubesPriv *priv = GET_PRIV (tubes); + GPtrArray *tube_infos; + GError *error; + GSList *list = NULL; + guint i; + + if (!tp_cli_channel_type_tubes_run_list_tubes (priv->channel, -1, + &tube_infos, &error, NULL)) + { + empathy_debug (DEBUG_DOMAIN, "Couldn't list tubes: %s", error->message); + g_clear_error (&error); + return NULL; + } + + for (i = 0; i < tube_infos->len; i++) + { + GValueArray *values; + guint id; + guint initiator; + guint type; + const gchar *service; + GHashTable *params; + guint state; + + values = g_ptr_array_index (tube_infos, i); + id = g_value_get_uint (g_value_array_get_nth (values, 0)); + initiator = g_value_get_uint (g_value_array_get_nth (values, 1)); + type = g_value_get_uint (g_value_array_get_nth (values, 2)); + service = g_value_get_string (g_value_array_get_nth (values, 3)); + params = g_value_get_boxed (g_value_array_get_nth (values, 4)); + state = g_value_get_uint (g_value_array_get_nth (values, 5)); + + empathy_debug (DEBUG_DOMAIN, + "Listing tubes - %d, id: %d, initiator: %d, service: %s, " + "state: %d, type: %d", + i + 1, id, initiator, service, state, type); + + list = g_slist_prepend (list, + g_object_new (EMPATHY_TYPE_TUBE, "tubes", tubes, "id", id, + "initiator", initiator, "type", type, "service", service, + "parameters", params, "state", state, NULL)); + + g_value_array_free (values); + } + + g_ptr_array_free (tube_infos, TRUE); + return list; +} + + +EmpathyTube * +empathy_tubes_get_tube (EmpathyTubes *tubes, + guint tube_id) +{ + // maybe this function should call list tubes directly? + EmpathyTube *tube = NULL; + GSList *tube_list, *list; + tube_list = empathy_tubes_list_tubes (tubes); + + for (list = tube_list; list != NULL; list = g_slist_next (list)) + { + EmpathyTube *t = EMPATHY_TUBE (list->data); + guint id; + g_object_get (G_OBJECT (t), "id", &id, NULL); + + if (id == tube_id) + tube = g_object_ref (t); + + g_object_unref (t); + } + + g_slist_free (tube_list); + return tube; +} + + +void +empathy_tubes_close (EmpathyTubes *tubes) +{ + EmpathyTubesPriv *priv = GET_PRIV (tubes); + GError *error = NULL; + + empathy_debug (DEBUG_DOMAIN, "Closing channel"); + + if (!tp_cli_channel_run_close (priv->channel, -1, &error, NULL)) + { + empathy_debug (DEBUG_DOMAIN, "Error closing channel: %s", + error ? error->message : "No error given"); + g_clear_error (&error); + } +} diff --git a/libempathy/empathy-tubes.h b/libempathy/empathy-tubes.h new file mode 100644 index 00000000..9debc80d --- /dev/null +++ b/libempathy/empathy-tubes.h @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2008 Collabora 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.1 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: Elliot Fairweather + */ + +#ifndef __EMPATHY_TUBES_H__ +#define __EMPATHY_TUBES_H__ + +#include + +#include +//#include +//#include + +//#include "empathy-tubes-types.h" +#include "empathy-tube.h" + +G_BEGIN_DECLS + +#define EMPATHY_TYPE_TUBES (empathy_tubes_get_type ()) +#define EMPATHY_TUBES(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), \ + EMPATHY_TYPE_TUBES, EmpathyTubes)) +#define EMPATHY_TUBES_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), \ + EMPATHY_TYPE_TUBES, EmpathyTubesClass)) +#define EMPATHY_IS_TUBES(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), \ + EMPATHY_TYPE_TUBES)) +#define EMPATHY_IS_TUBES_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), \ + EMPATHY_TYPE_TUBES)) +#define EMPATHY_TUBES_GET_CLASS(object) (G_TYPE_INSTANCE_GET_CLASS ((object), \ + EMPATHY_TYPE_TUBES, EmpathyTubesClass)) + +typedef struct _EmpathyTubes EmpathyTubes; +typedef struct _EmpathyTubesClass EmpathyTubesClass; + +struct _EmpathyTubes { + GObject parent; +}; + +struct _EmpathyTubesClass { + GObjectClass parent_class; +}; + +GType empathy_tubes_get_type (void) G_GNUC_CONST; +//EmpathyTubes *empathy_tubes_new (TpConn *connection, TpChan *channel); +EmpathyTubes *empathy_tubes_new (TpChannel *channel); + +guint empathy_tubes_offer_stream_tube_ipv4 (EmpathyTubes *tubes, gchar *host, + guint port, gchar *service); +void empathy_tubes_close (EmpathyTubes *tubes); +GSList *empathy_tubes_list_tubes (EmpathyTubes *tubes); +EmpathyTube *empathy_tubes_get_tube (EmpathyTubes *tubes, guint tube_id); + +G_END_DECLS + +#endif /* __EMPATHY_TUBES_H__ */