]> git.0d.be Git - empathy.git/blobdiff - libempathy/empathy-tp-chat.c
Fix indentation Fix not returning the contact in tp_contact_list_find()
[empathy.git] / libempathy / empathy-tp-chat.c
index 9ddad869d35b7cc23fdc41abed83b657e33a421c..8e8a52acad2219f5bde44bbde78aba34f4ab6b18 100644 (file)
 
 #include <config.h>
 
-#include <libtelepathy/tp-helpers.h>
+#include <string.h>
+
 #include <libtelepathy/tp-chan-type-text-gen.h>
 #include <libtelepathy/tp-chan-iface-chat-state-gen.h>
+#include <libtelepathy/tp-conn.h>
+#include <libtelepathy/tp-helpers.h>
 
 #include "empathy-tp-chat.h"
 #include "empathy-contact-manager.h"
-#include "empathy-contact-list.h"
-#include "empathy-session.h"
+#include "empathy-tp-contact-list.h"
 #include "empathy-marshal.h"
 #include "gossip-debug.h"
 #include "gossip-time.h"
+#include "gossip-utils.h"
 
 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
                       EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv))
 #define DEBUG_DOMAIN "TpChat"
 
 struct _EmpathyTpChatPriv {
-       EmpathyContactList *list;
-       TpChan             *tp_chan;
-       DBusGProxy         *text_iface;
-       DBusGProxy         *chat_state_iface;
+       EmpathyTpContactList  *list;
+       EmpathyContactManager *manager;
+       McAccount             *account;
+       gchar                 *id;
+       MissionControl        *mc;
+
+       TpChan                *tp_chan;
+       DBusGProxy            *text_iface;
+       DBusGProxy            *chat_state_iface;
 };
 
-static void empathy_tp_chat_class_init (EmpathyTpChatClass *klass);
-static void empathy_tp_chat_init       (EmpathyTpChat      *chat);
-static void tp_chat_finalize           (GObject            *object);
-static void tp_chat_destroy_cb         (TpChan             *text_chan,
-                                       EmpathyTpChat      *chat);
-static void tp_chat_received_cb        (DBusGProxy         *text_iface,
-                                       guint               message_id,
-                                       guint               timestamp,
-                                       guint               from_handle,
-                                       guint               message_type,
-                                       guint               message_flags,
-                                       gchar              *message_body,
-                                       EmpathyTpChat      *chat);
-static void tp_chat_sent_cb            (DBusGProxy         *text_iface,
-                                       guint               timestamp,
-                                       guint               message_type,
-                                       gchar              *message_body,
-                                       EmpathyTpChat      *chat);
-static void tp_chat_state_changed_cb   (DBusGProxy         *chat_state_iface,
-                                       guint               handle,
-                                       EmpathyTpChatState  state,
-                                       EmpathyTpChat      *chat);
-static void tp_chat_emit_message       (EmpathyTpChat      *chat,
-                                       guint               type,
-                                       guint               timestamp,
-                                       guint               from_handle,
-                                       const gchar        *message_body);
+static void      empathy_tp_chat_class_init (EmpathyTpChatClass        *klass);
+static void      empathy_tp_chat_init       (EmpathyTpChat             *chat);
+static void      tp_chat_finalize           (GObject                   *object);
+static GObject * tp_chat_constructor        (GType                      type,
+                                            guint                      n_props,
+                                            GObjectConstructParam     *props);
+static void      tp_chat_get_property       (GObject                   *object,
+                                            guint                      param_id,
+                                            GValue                    *value,
+                                            GParamSpec                *pspec);
+static void      tp_chat_set_property       (GObject                   *object,
+                                            guint                      param_id,
+                                            const GValue              *value,
+                                            GParamSpec                *pspec);
+static void      tp_chat_destroy_cb         (TpChan                    *text_chan,
+                                            EmpathyTpChat             *chat);
+static void      tp_chat_closed_cb          (TpChan                    *text_chan,
+                                            EmpathyTpChat             *chat);
+static void      tp_chat_received_cb        (DBusGProxy                *text_iface,
+                                            guint                      message_id,
+                                            guint                      timestamp,
+                                            guint                      from_handle,
+                                            guint                      message_type,
+                                            guint                      message_flags,
+                                            gchar                     *message_body,
+                                            EmpathyTpChat             *chat);
+static void      tp_chat_sent_cb            (DBusGProxy                *text_iface,
+                                            guint                      timestamp,
+                                            guint                      message_type,
+                                            gchar                     *message_body,
+                                            EmpathyTpChat             *chat);
+static void      tp_chat_state_changed_cb   (DBusGProxy                *chat_state_iface,
+                                            guint                      handle,
+                                            TelepathyChannelChatState  state,
+                                            EmpathyTpChat             *chat);
+static void      tp_chat_emit_message       (EmpathyTpChat             *chat,
+                                            guint                      type,
+                                            guint                      timestamp,
+                                            guint                      from_handle,
+                                            const gchar               *message_body);
+enum {
+       PROP_0,
+       PROP_ACCOUNT,
+       PROP_TP_CHAN
+};
 
 enum {
        MESSAGE_RECEIVED,
@@ -91,6 +117,27 @@ empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
        object_class->finalize = tp_chat_finalize;
+       object_class->constructor = tp_chat_constructor;
+       object_class->get_property = tp_chat_get_property;
+       object_class->set_property = tp_chat_set_property;
+
+       g_object_class_install_property (object_class,
+                                        PROP_ACCOUNT,
+                                        g_param_spec_object ("account",
+                                                             "channel Account",
+                                                             "The account associated with the channel",
+                                                             MC_TYPE_ACCOUNT,
+                                                             G_PARAM_READWRITE |
+                                                             G_PARAM_CONSTRUCT_ONLY));
+
+       g_object_class_install_property (object_class,
+                                        PROP_TP_CHAN,
+                                        g_param_spec_object ("tp-chan",
+                                                             "telepathy channel",
+                                                             "The text channel for the chat",
+                                                             TELEPATHY_CHAN_TYPE,
+                                                             G_PARAM_READWRITE |
+                                                             G_PARAM_CONSTRUCT_ONLY));
 
        signals[MESSAGE_RECEIVED] =
                g_signal_new ("message-received",
@@ -157,40 +204,51 @@ tp_chat_finalize (GObject *object)
                g_object_unref (priv->tp_chan);
        }
 
+       if (priv->manager) {
+               g_object_unref (priv->manager);
+       }
        if (priv->list) {
                g_object_unref (priv->list);
        }
+       if (priv->account) {
+               g_object_unref (priv->account);
+       }
+       if (priv->mc) {
+               g_object_unref (priv->mc);
+       }
+       g_free (priv->id);
 
        G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
 }
 
-EmpathyTpChat *
-empathy_tp_chat_new (McAccount *account,
-                    TpChan    *tp_chan)
+static GObject *
+tp_chat_constructor (GType                  type,
+                    guint                  n_props,
+                    GObjectConstructParam *props)
 {
-       EmpathyTpChatPriv     *priv;
-       EmpathyTpChat         *chat;
-       EmpathyContactManager *manager;
+       GObject           *chat;
+       EmpathyTpChatPriv *priv;
 
-       g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
-       g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
+       chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
 
-       chat = g_object_new (EMPATHY_TYPE_TP_CHAT, NULL);
        priv = GET_PRIV (chat);
 
-       manager = empathy_session_get_contact_manager ();
-       priv->list = empathy_contact_manager_get_list (manager, account);
-       priv->tp_chan = g_object_ref (tp_chan);
+       priv->manager = empathy_contact_manager_new ();
+       priv->list = empathy_contact_manager_get_list (priv->manager, priv->account);
+       priv->mc = gossip_mission_control_new ();
        g_object_ref (priv->list);
 
-       priv->text_iface = tp_chan_get_interface (tp_chan,
+       priv->text_iface = tp_chan_get_interface (priv->tp_chan,
                                                  TELEPATHY_CHAN_IFACE_TEXT_QUARK);
-       priv->chat_state_iface = tp_chan_get_interface (tp_chan,
+       priv->chat_state_iface = tp_chan_get_interface (priv->tp_chan,
                                                        TELEPATHY_CHAN_IFACE_CHAT_STATE_QUARK);
 
        g_signal_connect (priv->tp_chan, "destroy",
                          G_CALLBACK (tp_chat_destroy_cb),
                          chat);
+       dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->tp_chan), "Closed",
+                                    G_CALLBACK (tp_chat_closed_cb),
+                                    chat, NULL);
        dbus_g_proxy_connect_signal (priv->text_iface, "Received",
                                     G_CALLBACK (tp_chat_received_cb),
                                     chat, NULL);
@@ -208,6 +266,62 @@ empathy_tp_chat_new (McAccount *account,
        return chat;
 }
 
+static void
+tp_chat_get_property (GObject    *object,
+                     guint       param_id,
+                     GValue     *value,
+                     GParamSpec *pspec)
+{
+       EmpathyTpChatPriv *priv;
+
+       priv = GET_PRIV (object);
+
+       switch (param_id) {
+       case PROP_ACCOUNT:
+               g_value_set_object (value, priv->account);
+               break;
+       case PROP_TP_CHAN:
+               g_value_set_object (value, priv->tp_chan);
+               break;
+       default:
+               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
+               break;
+       };
+}
+
+static void
+tp_chat_set_property (GObject      *object,
+                     guint         param_id,
+                     const GValue *value,
+                     GParamSpec   *pspec)
+{
+       EmpathyTpChatPriv *priv;
+
+       priv = GET_PRIV (object);
+
+       switch (param_id) {
+       case PROP_ACCOUNT:
+               priv->account = g_object_ref (g_value_get_object (value));
+               break;
+       case PROP_TP_CHAN:
+               priv->tp_chan = g_object_ref (g_value_get_object (value));
+               break;
+       default:
+               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
+               break;
+       };
+}
+
+EmpathyTpChat *
+empathy_tp_chat_new (McAccount *account,
+                    TpChan    *tp_chan)
+{
+       return g_object_new (EMPATHY_TYPE_TP_CHAT, 
+                            "account", account,
+                            "tp-chan", tp_chan,
+                            NULL);
+}
+
 EmpathyTpChat *
 empathy_tp_chat_new_with_contact (GossipContact *contact)
 {
@@ -221,7 +335,7 @@ empathy_tp_chat_new_with_contact (GossipContact *contact)
 
        g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), NULL);
 
-       mc = empathy_session_get_mission_control ();
+       mc = gossip_mission_control_new ();
        account = gossip_contact_get_account (contact);
 
        if (mission_control_get_connection_status (mc, account, NULL) != 0) {
@@ -246,6 +360,7 @@ empathy_tp_chat_new_with_contact (GossipContact *contact)
 
        g_object_unref (tp_conn);
        g_object_unref (text_chan);
+       g_object_unref (mc);
 
        return chat;
 }
@@ -337,8 +452,8 @@ empathy_tp_chat_send (EmpathyTpChat *chat,
 }
 
 void
-empathy_tp_chat_send_state (EmpathyTpChat      *chat,
-                           EmpathyTpChatState  state)
+empathy_tp_chat_set_state (EmpathyTpChat             *chat,
+                          TelepathyChannelChatState  state)
 {
        EmpathyTpChatPriv *priv;
        GError            *error = NULL;
@@ -360,6 +475,70 @@ empathy_tp_chat_send_state (EmpathyTpChat      *chat,
        }
 }
 
+const gchar *
+empathy_tp_chat_get_id (EmpathyTpChat *chat)
+{
+       EmpathyTpChatPriv *priv;
+
+       g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
+
+       priv = GET_PRIV (chat);
+
+       if (priv->id) {
+               return priv->id;
+       }
+
+       priv->id = empathy_tp_chat_build_id (priv->account, priv->tp_chan);
+
+       return priv->id;
+}
+
+gchar *
+empathy_tp_chat_build_id (McAccount *account,
+                         TpChan    *tp_chan)
+{
+       MissionControl *mc;
+       TpConn         *tp_conn;
+       GArray         *handles;
+       gchar         **names;
+       gchar          *id;
+       GError         *error = NULL;
+       
+       g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
+       g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
+
+       mc = gossip_mission_control_new ();
+       tp_conn = mission_control_get_connection (mc, account, NULL);
+       g_object_unref (mc);
+
+       handles = g_array_new (FALSE, FALSE, sizeof (guint));
+       g_array_append_val (handles, tp_chan->handle);
+       if (!tp_conn_inspect_handles (DBUS_G_PROXY (tp_conn),
+                                     tp_chan->handle_type,
+                                     handles,
+                                     &names,
+                                     &error)) {
+               gossip_debug (DEBUG_DOMAIN, 
+                             "Couldn't get id: %s",
+                             error ? error->message : "No error given");
+               g_clear_error (&error);
+               g_array_free (handles, TRUE);
+               g_object_unref (tp_conn);
+               
+               return NULL;
+       }
+
+       /* A handle name is unique only for a specific account */
+       id = g_strdup_printf ("%s/%s",
+                             mc_account_get_unique_name (account),
+                             *names);
+
+       g_strfreev (names);
+       g_object_unref (tp_conn);
+
+       return id;
+}
+
 static void
 tp_chat_destroy_cb (TpChan        *text_chan,
                    EmpathyTpChat *chat)
@@ -368,7 +547,7 @@ tp_chat_destroy_cb (TpChan        *text_chan,
 
        priv = GET_PRIV (chat);
 
-       gossip_debug (DEBUG_DOMAIN, "Channel destroyed");
+       gossip_debug (DEBUG_DOMAIN, "Channel Closed or CM crashed");
 
        g_object_unref  (priv->tp_chan);
        priv->tp_chan = NULL;
@@ -378,6 +557,22 @@ tp_chat_destroy_cb (TpChan        *text_chan,
        g_signal_emit (chat, signals[DESTROY], 0);
 }
 
+static void
+tp_chat_closed_cb (TpChan        *text_chan,
+                  EmpathyTpChat *chat)
+{
+       EmpathyTpChatPriv *priv;
+
+       priv = GET_PRIV (chat);
+
+       /* The channel is closed, do just like if the proxy was destroyed */
+       g_signal_handlers_disconnect_by_func (priv->tp_chan,
+                                             tp_chat_destroy_cb,
+                                             chat);
+       tp_chat_destroy_cb (text_chan, chat);
+
+}
+
 static void
 tp_chat_received_cb (DBusGProxy    *text_iface,
                     guint          message_id,
@@ -425,17 +620,22 @@ tp_chat_sent_cb (DBusGProxy    *text_iface,
 }
 
 static void
-tp_chat_state_changed_cb (DBusGProxy         *chat_state_iface,
-                         guint               handle,
-                         EmpathyTpChatState  state,
-                         EmpathyTpChat      *chat)
+tp_chat_state_changed_cb (DBusGProxy                *chat_state_iface,
+                         guint                      handle,
+                         TelepathyChannelChatState  state,
+                         EmpathyTpChat             *chat)
 {
        EmpathyTpChatPriv *priv;
        GossipContact     *contact;
 
        priv = GET_PRIV (chat);
 
-       contact = empathy_contact_list_get_from_handle (priv->list, handle);
+       contact = empathy_tp_contact_list_get_from_handle (priv->list, handle);
+
+       gossip_debug (DEBUG_DOMAIN, "Chat state changed for %s (%d): %d",
+                     gossip_contact_get_name (contact),
+                     handle,
+                     state);
 
        g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
 
@@ -456,11 +656,11 @@ tp_chat_emit_message (EmpathyTpChat *chat,
        priv = GET_PRIV (chat);
 
        if (from_handle == 0) {
-               sender = empathy_contact_list_get_own (priv->list);
+               sender = empathy_tp_contact_list_get_user (priv->list);
                g_object_ref (sender);
        } else {
-               sender = empathy_contact_list_get_from_handle (priv->list,
-                                                              from_handle);
+               sender = empathy_tp_contact_list_get_from_handle (priv->list,
+                                                                 from_handle);
        }
 
        message = gossip_message_new (message_body);