diff options
Diffstat (limited to 'libempathy')
-rw-r--r-- | libempathy/empathy-dispatcher.c | 231 | ||||
-rw-r--r-- | libempathy/empathy-dispatcher.h | 24 | ||||
-rw-r--r-- | libempathy/empathy-utils.c | 145 | ||||
-rw-r--r-- | libempathy/empathy-utils.h | 15 |
4 files changed, 231 insertions, 184 deletions
diff --git a/libempathy/empathy-dispatcher.c b/libempathy/empathy-dispatcher.c index 741b01db6..1d2b9895d 100644 --- a/libempathy/empathy-dispatcher.c +++ b/libempathy/empathy-dispatcher.c @@ -40,6 +40,7 @@ #include "empathy-utils.h" #include "empathy-tube-handler.h" #include "empathy-contact-factory.h" +#include "empathy-tp-group.h" #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER #include <libempathy/empathy-debug.h> @@ -49,7 +50,7 @@ typedef struct { GHashTable *connections; gpointer token; MissionControl *mc; - GHashTable *tubes; + GSList *tubes; } EmpathyDispatcherPriv; G_DEFINE_TYPE (EmpathyDispatcher, empathy_dispatcher, G_TYPE_OBJECT); @@ -203,7 +204,6 @@ dispatcher_tubes_new_tube_cb (TpChannel *channel, gpointer user_data, GObject *dispatcher) { - EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher); static TpDBusDaemon *daemon = NULL; DispatcherTube *tube; McAccount *account; @@ -213,9 +213,8 @@ dispatcher_tubes_new_tube_cb (TpChannel *channel, GError *error = NULL; /* Increase tube count */ - number = GPOINTER_TO_UINT (g_hash_table_lookup (priv->tubes, channel)); - g_hash_table_replace (priv->tubes, g_object_ref (channel), - GUINT_TO_POINTER (++number)); + number = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (channel), "tube-count")); + g_object_set_data (G_OBJECT (channel), "tube-count", GUINT_TO_POINTER (++number)); DEBUG ("Increased tube count for channel %p: %d", channel, number); /* We dispatch only local pending tubes */ @@ -308,17 +307,18 @@ dispatcher_tubes_list_tubes_cb (TpChannel *channel, } static void -dispatcher_tubes_channel_invalidated_cb (TpProxy *proxy, - guint domain, - gint code, - gchar *message, +dispatcher_tubes_channel_invalidated_cb (TpProxy *proxy, + guint domain, + gint code, + gchar *message, EmpathyDispatcher *dispatcher) { EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher); - DEBUG ("Error: %s", message); + DEBUG ("%s", message); - g_hash_table_remove (priv->tubes, proxy); + priv->tubes = g_slist_remove (priv->tubes, proxy); + g_object_unref (proxy); } static void @@ -327,18 +327,16 @@ dispatcher_tubes_tube_closed_cb (TpChannel *channel, gpointer user_data, GObject *dispatcher) { - EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher); - guint number; + guint number; - number = GPOINTER_TO_UINT (g_hash_table_lookup (priv->tubes, channel)); + number = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (channel), "tube-count")); if (number == 1) { DEBUG ("No more tube, closing channel"); tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL); } else if (number > 1) { DEBUG ("Decrease tube count: %d", number); - g_hash_table_replace (priv->tubes, g_object_ref (channel), - GUINT_TO_POINTER (--number)); + g_object_set_data (G_OBJECT (channel), "tube-count", GUINT_TO_POINTER (--number)); } } @@ -348,15 +346,9 @@ dispatcher_tubes_handle_channel (EmpathyDispatcher *dispatcher, { EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher); - if (g_hash_table_lookup (priv->tubes, channel)) { - return; - } - - DEBUG ("Handling new channel"); - - g_hash_table_insert (priv->tubes, g_object_ref (channel), - GUINT_TO_POINTER (0)); + DEBUG ("Called"); + priv->tubes = g_slist_prepend (priv->tubes, g_object_ref (channel)); g_signal_connect (channel, "invalidated", G_CALLBACK (dispatcher_tubes_channel_invalidated_cb), dispatcher); @@ -569,34 +561,24 @@ dispatcher_status_changed_cb (MissionControl *mc, g_object_unref (account); } -static guint -dispatcher_channel_hash (gconstpointer key) -{ - TpProxy *channel = TP_PROXY (key); - - return g_str_hash (channel->object_path); -} - -static gboolean -dispatcher_channel_equal (gconstpointer a, - gconstpointer b) -{ - TpProxy *channel_a = TP_PROXY (a); - TpProxy *channel_b = TP_PROXY (b); - - return g_str_equal (channel_a->object_path, channel_b->object_path); -} - static void dispatcher_finalize (GObject *object) { EmpathyDispatcherPriv *priv = GET_PRIV (object); + GSList *l; empathy_disconnect_account_status_changed (priv->token); g_object_unref (priv->mc); + for (l = priv->tubes; l; l = l->next) { + g_signal_handlers_disconnect_by_func (l->data, + dispatcher_tubes_channel_invalidated_cb, + object); + g_object_unref (l->data); + } + g_slist_free (priv->tubes); + g_hash_table_destroy (priv->connections); - g_hash_table_destroy (priv->tubes); } static void @@ -645,10 +627,6 @@ empathy_dispatcher_init (EmpathyDispatcher *dispatcher) EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv); dispatcher->priv = priv; - priv->tubes = g_hash_table_new_full (dispatcher_channel_hash, - dispatcher_channel_equal, - g_object_unref, NULL); - priv->mc = empathy_mission_control_new (); priv->token = empathy_connect_to_account_status_changed (priv->mc, G_CALLBACK (dispatcher_status_changed_cb), @@ -679,3 +657,162 @@ empathy_dispatcher_new (void) return dispatcher; } +typedef struct { + const gchar *channel_type; + guint handle_type; + guint handle; +} DispatcherRequestData; + +static void +dispatcher_request_channel_cb (TpConnection *connection, + const gchar *object_path, + const GError *error, + gpointer user_data, + GObject *weak_object) +{ + DispatcherRequestData *data = (DispatcherRequestData*) user_data; + + if (error) { + DEBUG ("Error: %s", error->message); + return; + } + + if (dispatcher) { + TpChannel *channel; + + channel = tp_channel_new (connection, object_path, + data->channel_type, + data->handle_type, + data->handle, NULL); + + g_signal_emit (dispatcher, signals[DISPATCH_CHANNEL], 0, channel); + } +} + +void +empathy_dispatcher_call_with_contact (EmpathyContact *contact) +{ + MissionControl *mc; + McAccount *account; + TpConnection *connection; + gchar *object_path; + TpChannel *channel; + EmpathyContactFactory *factory; + EmpathyTpGroup *group; + EmpathyContact *self_contact; + GError *error = NULL; + + g_return_if_fail (EMPATHY_IS_CONTACT (contact)); + + mc = empathy_mission_control_new (); + account = empathy_contact_get_account (contact); + connection = mission_control_get_tpconnection (mc, account, NULL); + tp_connection_run_until_ready (connection, FALSE, NULL, NULL); + g_object_unref (mc); + + /* We abuse of suppress_handler, TRUE means OUTGOING. The channel + * will be catched in EmpathyFilter */ + if (!tp_cli_connection_run_request_channel (connection, -1, + TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA, + TP_HANDLE_TYPE_NONE, + 0, + TRUE, + &object_path, + &error, + NULL)) { + DEBUG ("Couldn't request channel: %s", + error ? error->message : "No error given"); + g_clear_error (&error); + g_object_unref (connection); + return; + } + + channel = tp_channel_new (connection, + object_path, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA, + TP_HANDLE_TYPE_NONE, 0, NULL); + + group = empathy_tp_group_new (channel); + empathy_run_until_ready (group); + + factory = empathy_contact_factory_new (); + self_contact = empathy_contact_factory_get_user (factory, account); + empathy_contact_run_until_ready (self_contact, + EMPATHY_CONTACT_READY_HANDLE, + NULL); + + empathy_tp_group_add_member (group, contact, ""); + empathy_tp_group_add_member (group, self_contact, ""); + + g_object_unref (factory); + g_object_unref (self_contact); + g_object_unref (group); + g_object_unref (connection); + g_object_unref (channel); + g_free (object_path); +} + +void +empathy_dispatcher_call_with_contact_id (McAccount *account, const gchar *contact_id) +{ + EmpathyContactFactory *factory; + EmpathyContact *contact; + + factory = empathy_contact_factory_new (); + contact = empathy_contact_factory_get_from_id (factory, account, contact_id); + empathy_contact_run_until_ready (contact, EMPATHY_CONTACT_READY_HANDLE, NULL); + + empathy_dispatcher_call_with_contact (contact); + + g_object_unref (contact); + g_object_unref (factory); +} + +void +empathy_dispatcher_chat_with_contact (EmpathyContact *contact) +{ + MissionControl *mc; + McAccount *account; + TpConnection *connection; + DispatcherRequestData *data; + + g_return_if_fail (EMPATHY_IS_CONTACT (contact)); + + mc = empathy_mission_control_new (); + account = empathy_contact_get_account (contact); + connection = mission_control_get_tpconnection (mc, account, NULL); + tp_connection_run_until_ready (connection, FALSE, NULL, NULL); + g_object_unref (mc); + + /* We abuse of suppress_handler, TRUE means OUTGOING. */ + data = g_new (DispatcherRequestData, 1); + data->channel_type = TP_IFACE_CHANNEL_TYPE_TEXT; + data->handle_type = TP_HANDLE_TYPE_CONTACT; + data->handle = empathy_contact_get_handle (contact); + tp_cli_connection_call_request_channel (connection, -1, + data->channel_type, + data->handle_type, + data->handle, + TRUE, + dispatcher_request_channel_cb, + data, g_free, + NULL); + g_object_unref (connection); +} + +void +empathy_dispatcher_chat_with_contact_id (McAccount *account, + const gchar *contact_id) +{ + EmpathyContactFactory *factory; + EmpathyContact *contact; + + factory = empathy_contact_factory_new (); + contact = empathy_contact_factory_get_from_id (factory, account, contact_id); + empathy_contact_run_until_ready (contact, EMPATHY_CONTACT_READY_HANDLE, NULL); + + empathy_dispatcher_chat_with_contact (contact); + + g_object_unref (contact); + g_object_unref (factory); +} + diff --git a/libempathy/empathy-dispatcher.h b/libempathy/empathy-dispatcher.h index 0cc59c8b8..b14a460e4 100644 --- a/libempathy/empathy-dispatcher.h +++ b/libempathy/empathy-dispatcher.h @@ -57,15 +57,21 @@ typedef struct { gboolean activatable; } EmpathyDispatcherTube; -GType empathy_dispatcher_get_type (void) G_GNUC_CONST; -EmpathyDispatcher * empathy_dispatcher_new (void); -void empathy_dispatcher_channel_process(EmpathyDispatcher *dispatcher, - TpChannel *channel); -GType empathy_dispatcher_tube_get_type (void); -EmpathyDispatcherTube *empathy_dispatcher_tube_ref (EmpathyDispatcherTube *tube); -void empathy_dispatcher_tube_unref (EmpathyDispatcherTube *tube); -void empathy_dispatcher_tube_process (EmpathyDispatcher *dispatcher, - EmpathyDispatcherTube *tube); +GType empathy_dispatcher_get_type (void) G_GNUC_CONST; +EmpathyDispatcher * empathy_dispatcher_new (void); +void empathy_dispatcher_channel_process (EmpathyDispatcher *dispatcher, + TpChannel *channel); +GType empathy_dispatcher_tube_get_type (void); +EmpathyDispatcherTube *empathy_dispatcher_tube_ref (EmpathyDispatcherTube *tube); +void empathy_dispatcher_tube_unref (EmpathyDispatcherTube *tube); +void empathy_dispatcher_tube_process (EmpathyDispatcher *dispatcher, + EmpathyDispatcherTube *tube); +void empathy_dispatcher_call_with_contact (EmpathyContact *contact); +void empathy_dispatcher_call_with_contact_id (McAccount *account, + const gchar *contact_id); +void empathy_dispatcher_chat_with_contact_id (McAccount *account, + const gchar *contact_id); +void empathy_dispatcher_chat_with_contact (EmpathyContact *contact); G_END_DECLS diff --git a/libempathy/empathy-utils.c b/libempathy/empathy-utils.c index fe6a787bb..f62d28e4c 100644 --- a/libempathy/empathy-utils.c +++ b/libempathy/empathy-utils.c @@ -338,126 +338,6 @@ empathy_mission_control_new (void) return mc; } -void -empathy_call_with_contact (EmpathyContact *contact) -{ - MissionControl *mc; - McAccount *account; - TpConnection *connection; - gchar *object_path; - TpChannel *channel; - EmpathyContactFactory *factory; - EmpathyTpGroup *group; - EmpathyContact *self_contact; - GError *error = NULL; - - g_return_if_fail (EMPATHY_IS_CONTACT (contact)); - - mc = empathy_mission_control_new (); - account = empathy_contact_get_account (contact); - connection = mission_control_get_tpconnection (mc, account, NULL); - tp_connection_run_until_ready (connection, FALSE, NULL, NULL); - g_object_unref (mc); - - /* We abuse of suppress_handler, TRUE means OUTGOING. The channel - * will be catched in EmpathyFilter */ - if (!tp_cli_connection_run_request_channel (connection, -1, - TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA, - TP_HANDLE_TYPE_NONE, - 0, - TRUE, - &object_path, - &error, - NULL)) { - DEBUG ("Couldn't request channel: %s", - error ? error->message : "No error given"); - g_clear_error (&error); - g_object_unref (connection); - return; - } - - channel = tp_channel_new (connection, - object_path, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA, - TP_HANDLE_TYPE_NONE, 0, NULL); - - group = empathy_tp_group_new (channel); - empathy_run_until_ready (group); - - factory = empathy_contact_factory_new (); - self_contact = empathy_contact_factory_get_user (factory, account); - empathy_contact_run_until_ready (self_contact, - EMPATHY_CONTACT_READY_HANDLE, - NULL); - - empathy_tp_group_add_member (group, contact, ""); - empathy_tp_group_add_member (group, self_contact, ""); - - g_object_unref (factory); - g_object_unref (self_contact); - g_object_unref (group); - g_object_unref (connection); - g_object_unref (channel); - g_free (object_path); -} - -void -empathy_call_with_contact_id (McAccount *account, const gchar *contact_id) -{ - EmpathyContactFactory *factory; - EmpathyContact *contact; - - factory = empathy_contact_factory_new (); - contact = empathy_contact_factory_get_from_id (factory, account, contact_id); - empathy_contact_run_until_ready (contact, EMPATHY_CONTACT_READY_HANDLE, NULL); - - empathy_call_with_contact (contact); - - g_object_unref (contact); - g_object_unref (factory); -} - -void -empathy_chat_with_contact (EmpathyContact *contact) -{ - MissionControl *mc; - McAccount *account; - TpConnection *connection; - - g_return_if_fail (EMPATHY_IS_CONTACT (contact)); - - mc = empathy_mission_control_new (); - account = empathy_contact_get_account (contact); - connection = mission_control_get_tpconnection (mc, account, NULL); - tp_connection_run_until_ready (connection, FALSE, NULL, NULL); - g_object_unref (mc); - - /* We abuse of suppress_handler, TRUE means OUTGOING. The channel - * will be catched in EmpathyFilter */ - tp_cli_connection_call_request_channel (connection, -1, - TP_IFACE_CHANNEL_TYPE_TEXT, - TP_HANDLE_TYPE_CONTACT, - empathy_contact_get_handle (contact), - TRUE, - NULL, NULL, NULL, NULL); - g_object_unref (connection); -} - -void -empathy_chat_with_contact_id (McAccount *account, const gchar *contact_id) -{ - EmpathyContactFactory *factory; - EmpathyContact *contact; - - factory = empathy_contact_factory_new (); - contact = empathy_contact_factory_get_from_id (factory, account, contact_id); - empathy_contact_run_until_ready (contact, EMPATHY_CONTACT_READY_HANDLE, NULL); - - empathy_chat_with_contact (contact); - - g_object_unref (contact); - g_object_unref (factory); -} - const gchar * empathy_presence_get_default_message (McPresence presence) { @@ -747,3 +627,28 @@ empathy_disconnect_account_status_changed (gpointer token) data); } +guint +empathy_proxy_hash (gconstpointer key) +{ + TpProxy *proxy = TP_PROXY (key); + + g_return_val_if_fail (TP_IS_PROXY (proxy), 0); + + return g_str_hash (proxy->object_path) + + g_str_hash (proxy->bus_name); +} + +gboolean +empathy_proxy_equal (gconstpointer a, + gconstpointer b) +{ + TpProxy *proxy_a = TP_PROXY (a); + TpProxy *proxy_b = TP_PROXY (b); + + g_return_val_if_fail (TP_IS_PROXY (proxy_a), FALSE); + g_return_val_if_fail (TP_IS_PROXY (proxy_b), FALSE); + + return g_str_equal (proxy_a->object_path, proxy_b->object_path) && + g_str_equal (proxy_a->bus_name, proxy_b->bus_name); +} + diff --git a/libempathy/empathy-utils.h b/libempathy/empathy-utils.h index 800f09d05..92ba44a94 100644 --- a/libempathy/empathy-utils.h +++ b/libempathy/empathy-utils.h @@ -83,12 +83,6 @@ guint empathy_account_hash (gconstpointer key); gboolean empathy_account_equal (gconstpointer a, gconstpointer b); MissionControl *empathy_mission_control_new (void); -void empathy_call_with_contact (EmpathyContact *contact); -void empathy_call_with_contact_id (McAccount *account, - const gchar *contact_id); -void empathy_chat_with_contact (EmpathyContact *contact); -void empathy_chat_with_contact_id (McAccount *account, - const gchar *contact_id); const gchar * empathy_presence_get_default_message (McPresence presence); const gchar * empathy_presence_to_str (McPresence presence); McPresence empathy_presence_from_str (const gchar *str); @@ -104,11 +98,16 @@ void empathy_run_until_ready_full (gpointer object, gpointer user_data, GMainLoop **loop); McAccount * empathy_channel_get_account (TpChannel *channel); -gpointer empathy_connect_to_account_status_changed (MissionControl *mc, +gpointer empathy_connect_to_account_status_changed (MissionControl *mc, GCallback handler, gpointer user_data, GClosureNotify free_func); -void empathy_disconnect_account_status_changed (gpointer token); +void empathy_disconnect_account_status_changed (gpointer token); +gboolean empathy_proxy_equal (gconstpointer a, + gconstpointer b); +guint empathy_proxy_hash (gconstpointer key); + + G_END_DECLS |