]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-chat.c
Unify EmpathyTpChatroom and EmpathyTpChat.
[empathy.git] / libempathy / empathy-tp-chat.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007-2008 Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  * 
19  * Authors: Xavier Claessens <xclaesse@gmail.com>
20  */
21
22 #include <config.h>
23
24 #include <string.h>
25
26 #include <telepathy-glib/channel.h>
27 #include <telepathy-glib/dbus.h>
28 #include <telepathy-glib/util.h>
29
30 #include "empathy-tp-chat.h"
31 #include "empathy-contact-factory.h"
32 #include "empathy-contact-list.h"
33 #include "empathy-marshal.h"
34 #include "empathy-debug.h"
35 #include "empathy-time.h"
36 #include "empathy-utils.h"
37
38 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
39                        EMPATHY_TYPE_TP_CHAT, EmpathyTpChatPriv))
40
41 #define DEBUG_DOMAIN "TpChat"
42
43 struct _EmpathyTpChatPriv {
44         EmpathyContactFactory *factory;
45         EmpathyContact        *user;
46         EmpathyContact        *initiator;
47         EmpathyTpGroup        *group;
48         McAccount             *account;
49         TpChannel             *channel;
50         gchar                 *id;
51         MissionControl        *mc;
52         gboolean               acknowledge;
53         TpChan                *tp_chan;
54         gboolean               had_pending_messages;
55         GSList                *message_queue;
56         gboolean               had_properties_list;
57         GPtrArray             *properties;
58 };
59
60 typedef struct {
61         gchar          *name;
62         guint           id;
63         TpPropertyFlags flags;
64         GValue         *value;
65 } TpChatProperty;
66
67 static void empathy_tp_chat_class_init (EmpathyTpChatClass      *klass);
68 static void empathy_tp_chat_init       (EmpathyTpChat           *chat);
69 static void tp_chat_iface_init         (EmpathyContactListIface *iface);
70
71 enum {
72         PROP_0,
73         PROP_ACCOUNT,
74         PROP_TP_CHAN,
75         PROP_CHANNEL,
76         PROP_ACKNOWLEDGE,
77 };
78
79 enum {
80         MESSAGE_RECEIVED,
81         SEND_ERROR,
82         CHAT_STATE_CHANGED,
83         PROPERTY_CHANGED,
84         DESTROY,
85         LAST_SIGNAL
86 };
87
88 static guint signals[LAST_SIGNAL];
89
90 G_DEFINE_TYPE_WITH_CODE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT,
91                          G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
92                                                 tp_chat_iface_init));
93
94 static void
95 tp_chat_invalidated_cb (TpProxy       *proxy,
96                         guint          domain,
97                         gint           code,
98                         gchar         *message,
99                         EmpathyTpChat *chat)
100 {
101         EmpathyTpChatPriv *priv = GET_PRIV (chat);
102
103         empathy_debug (DEBUG_DOMAIN, "Channel invalidated: %s", message);
104
105         g_object_unref (priv->channel);
106         g_object_unref (priv->tp_chan);
107         priv->channel = NULL;
108         priv->tp_chan = NULL;
109
110         g_signal_emit (chat, signals[DESTROY], 0);
111 }
112
113 static void
114 tp_chat_async_cb (TpChannel *proxy,
115                   const GError *error,
116                   gpointer user_data,
117                   GObject *weak_object)
118 {
119         if (error) {
120                 empathy_debug (DEBUG_DOMAIN, "Error %s: %s",
121                                user_data, error->message);
122         }
123 }
124
125 static void
126 tp_chat_member_added_cb (EmpathyTpGroup *group,
127                          EmpathyContact *contact,
128                          EmpathyContact *actor,
129                          guint           reason,
130                          const gchar    *message,
131                          EmpathyTpChat  *chat)
132 {
133         g_signal_emit_by_name (chat, "members-changed",
134                                contact, actor, reason, message,
135                                TRUE);
136 }
137
138 static void
139 tp_chat_member_removed_cb (EmpathyTpGroup *group,
140                            EmpathyContact *contact,
141                            EmpathyContact *actor,
142                            guint           reason,
143                            const gchar    *message,
144                            EmpathyTpChat  *chat)
145 {
146         g_signal_emit_by_name (chat, "members-changed",
147                                contact, actor, reason, message,
148                                FALSE);
149 }
150 static void
151 tp_chat_local_pending_cb  (EmpathyTpGroup *group,
152                            EmpathyContact *contact,
153                            EmpathyContact *actor,
154                            guint           reason,
155                            const gchar    *message,
156                            EmpathyTpChat  *chat)
157 {
158         g_signal_emit_by_name (chat, "pendings-changed",
159                                contact, actor, reason, message,
160                                TRUE);
161 }
162
163 static void
164 tp_chat_add (EmpathyContactList *list,
165              EmpathyContact     *contact,
166              const gchar        *message)
167 {
168         EmpathyTpChatPriv *priv = GET_PRIV (list);
169
170         g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
171         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
172
173         if (priv->group) {
174                 empathy_tp_group_add_member (priv->group, contact, message);
175         }
176 }
177
178 static void
179 tp_chat_remove (EmpathyContactList *list,
180                 EmpathyContact     *contact,
181                 const gchar        *message)
182 {
183         EmpathyTpChatPriv *priv = GET_PRIV (list);
184
185         g_return_if_fail (EMPATHY_IS_TP_CHAT (list));
186         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
187
188         if (priv->group) {
189                 empathy_tp_group_remove_member (priv->group, contact, message);
190         }
191 }
192
193 static GList *
194 tp_chat_get_members (EmpathyContactList *list)
195 {
196         EmpathyTpChatPriv *priv = GET_PRIV (list);
197         GList             *members = NULL;
198
199         g_return_val_if_fail (EMPATHY_IS_TP_CHAT (list), NULL);
200
201         if (priv->group) {
202                 members = empathy_tp_group_get_members (priv->group);
203         } else {
204                 members = g_list_prepend (members, g_object_ref (priv->user));
205                 members = g_list_prepend (members, g_object_ref (priv->initiator));
206         }
207
208         return members;
209 }
210
211 static EmpathyMessage *
212 tp_chat_build_message (EmpathyTpChat *chat,
213                        guint          type,
214                        guint          timestamp,
215                        guint          from_handle,
216                        const gchar   *message_body)
217 {
218         EmpathyTpChatPriv *priv;
219         EmpathyMessage    *message;
220         EmpathyContact    *sender;
221
222         priv = GET_PRIV (chat);
223
224         if (from_handle == 0) {
225                 sender = g_object_ref (priv->user);
226         } else {
227                 sender = empathy_contact_factory_get_from_handle (priv->factory,
228                                                                   priv->account,
229                                                                   from_handle);
230         }
231
232         message = empathy_message_new (message_body);
233         empathy_message_set_type (message, type);
234         empathy_message_set_sender (message, sender);
235         empathy_message_set_receiver (message, priv->user);
236         empathy_message_set_timestamp (message, timestamp);
237
238         g_object_unref (sender);
239
240         return message;
241 }
242
243 static void
244 tp_chat_sender_ready_notify_cb (EmpathyContact *contact,
245                                 GParamSpec     *param_spec,
246                                 EmpathyTpChat  *chat)
247 {
248         EmpathyTpChatPriv   *priv = GET_PRIV (chat);
249         EmpathyMessage      *message;
250         EmpathyContactReady  ready;
251         EmpathyContact      *sender;
252         gboolean             removed = FALSE;
253
254         /* Emit all messages queued until we find a message with not
255          * ready sender. When leaving this loop, sender is the first not ready
256          * contact queued and removed tells if at least one message got removed
257          * from the queue. */
258         while (priv->message_queue) {
259                 message = priv->message_queue->data;
260                 sender = empathy_message_get_sender (message);
261                 ready = empathy_contact_get_ready (sender);
262
263                 if (!(ready & EMPATHY_CONTACT_READY_NAME)) {
264                         break;
265                 }
266
267                 empathy_debug (DEBUG_DOMAIN, "Queued message ready");
268                 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
269                 priv->message_queue = g_slist_remove (priv->message_queue,
270                                                       message);
271                 g_object_unref (message);
272                 removed = TRUE;
273         }
274
275         if (removed) {
276                 g_signal_handlers_disconnect_by_func (contact,
277                                                       tp_chat_sender_ready_notify_cb,
278                                                       chat);
279
280                 if (priv->message_queue) {
281                         g_signal_connect (sender, "notify::ready",
282                                           G_CALLBACK (tp_chat_sender_ready_notify_cb),
283                                           chat);
284                 }
285         }
286 }
287
288 static void
289 tp_chat_emit_or_queue_message (EmpathyTpChat  *chat,
290                                EmpathyMessage *message)
291 {
292         EmpathyTpChatPriv   *priv = GET_PRIV (chat);
293         EmpathyContact      *sender;
294         EmpathyContactReady  ready;
295
296         if (priv->message_queue != NULL) {
297                 empathy_debug (DEBUG_DOMAIN, "Message queue not empty");
298                 priv->message_queue = g_slist_append (priv->message_queue,
299                                                       g_object_ref (message));
300                 return;
301         }
302
303         sender = empathy_message_get_sender (message);
304         ready = empathy_contact_get_ready (sender);
305         if (ready & EMPATHY_CONTACT_READY_NAME) {
306                 empathy_debug (DEBUG_DOMAIN, "Message queue empty and sender ready");
307                 g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
308                 return;
309         }
310
311         empathy_debug (DEBUG_DOMAIN, "Sender not ready");
312         priv->message_queue = g_slist_append (priv->message_queue, 
313                                               g_object_ref (message));
314         g_signal_connect (sender, "notify::ready",
315                           G_CALLBACK (tp_chat_sender_ready_notify_cb),
316                           chat);
317 }
318
319 static void
320 tp_chat_received_cb (TpChannel   *channel,
321                      guint        message_id,
322                      guint        timestamp,
323                      guint        from_handle,
324                      guint        message_type,
325                      guint        message_flags,
326                      const gchar *message_body,
327                      gpointer     user_data,
328                      GObject     *chat)
329 {
330         EmpathyTpChatPriv *priv = GET_PRIV (chat);
331         EmpathyMessage    *message;
332
333         if (!priv->had_pending_messages) {
334                 return;
335         }
336  
337         empathy_debug (DEBUG_DOMAIN, "Message received: %s", message_body);
338
339         message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
340                                          message_type,
341                                          timestamp,
342                                          from_handle,
343                                          message_body);
344
345         tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
346         g_object_unref (message);
347
348         if (priv->acknowledge) {
349                 GArray *message_ids;
350
351                 message_ids = g_array_new (FALSE, FALSE, sizeof (guint));
352                 g_array_append_val (message_ids, message_id);
353                 tp_cli_channel_type_text_call_acknowledge_pending_messages (priv->channel,
354                                                                             -1,
355                                                                             message_ids,
356                                                                             tp_chat_async_cb,
357                                                                             "acknowledging pending messages",
358                                                                             NULL,
359                                                                             chat);
360                 g_array_free (message_ids, TRUE);
361         }
362 }
363
364 static void
365 tp_chat_sent_cb (TpChannel   *channel,
366                  guint        timestamp,
367                  guint        message_type,
368                  const gchar *message_body,
369                  gpointer     user_data,
370                  GObject     *chat)
371 {
372         EmpathyMessage *message;
373
374         empathy_debug (DEBUG_DOMAIN, "Message sent: %s", message_body);
375
376         message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
377                                          message_type,
378                                          timestamp,
379                                          0,
380                                          message_body);
381
382         tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
383         g_object_unref (message);
384 }
385
386 static void
387 tp_chat_send_error_cb (TpChannel   *channel,
388                        guint        error_code,
389                        guint        timestamp,
390                        guint        message_type,
391                        const gchar *message_body,
392                        gpointer     user_data,
393                        GObject     *chat)
394 {
395         EmpathyMessage *message;
396
397         empathy_debug (DEBUG_DOMAIN, "Message sent error: %s (%d)",
398                        message_body, error_code);
399
400         message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
401                                          message_type,
402                                          timestamp,
403                                          0,
404                                          message_body);
405
406         g_signal_emit (chat, signals[SEND_ERROR], 0, message, error_code);
407         g_object_unref (message);
408 }
409
410 static void
411 tp_chat_state_changed_cb (TpChannel *channel,
412                           guint      handle,
413                           guint      state,
414                           gpointer   user_data,
415                           GObject   *chat)
416 {
417         EmpathyTpChatPriv *priv = GET_PRIV (chat);
418         EmpathyContact    *contact;
419
420         contact = empathy_contact_factory_get_from_handle (priv->factory,
421                                                            priv->account,
422                                                            handle);
423
424         empathy_debug (DEBUG_DOMAIN, "Chat state changed for %s (%d): %d",
425                       empathy_contact_get_name (contact),
426                       handle, state);
427
428         g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
429         g_object_unref (contact);
430 }
431
432 static void
433 tp_chat_list_pending_messages_cb (TpChannel       *channel,
434                                   const GPtrArray *messages_list,
435                                   const GError    *error,
436                                   gpointer         user_data,
437                                   GObject         *chat)
438 {
439         EmpathyTpChatPriv *priv = GET_PRIV (chat);
440         guint              i;
441
442         priv->had_pending_messages = TRUE;
443
444         if (error) {
445                 empathy_debug (DEBUG_DOMAIN, "Error listing pending messages: %s",
446                                error->message);
447                 return;
448         }
449
450         for (i = 0; i < messages_list->len; i++) {
451                 EmpathyMessage *message;
452                 GValueArray    *message_struct;
453                 const gchar    *message_body;
454                 guint           message_id;
455                 guint           timestamp;
456                 guint           from_handle;
457                 guint           message_type;
458                 guint           message_flags;
459
460                 message_struct = g_ptr_array_index (messages_list, i);
461
462                 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
463                 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
464                 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
465                 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
466                 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
467                 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
468
469                 empathy_debug (DEBUG_DOMAIN, "Message pending: %s", message_body);
470
471                 message = tp_chat_build_message (EMPATHY_TP_CHAT (chat),
472                                                  message_type,
473                                                  timestamp,
474                                                  from_handle,
475                                                  message_body);
476
477                 tp_chat_emit_or_queue_message (EMPATHY_TP_CHAT (chat), message);
478                 g_object_unref (message);
479         }
480 }
481
482 static void
483 tp_chat_property_flags_changed_cb (TpProxy         *proxy,
484                                    const GPtrArray *properties,
485                                    gpointer         user_data,
486                                    GObject         *chat)
487 {
488         EmpathyTpChatPriv *priv = GET_PRIV (chat);
489         guint              i, j;
490
491         if (!priv->had_properties_list || !properties) {
492                 return;
493         }
494
495         for (i = 0; i < properties->len; i++) {
496                 GValueArray    *prop_struct;
497                 TpChatProperty *property;
498                 guint           id;
499                 guint           flags;
500
501                 prop_struct = g_ptr_array_index (properties, i);
502                 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
503                 flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 1));
504
505                 for (j = 0; j < priv->properties->len; j++) {
506                         property = g_ptr_array_index (priv->properties, j);
507                         if (property->id == id) {
508                                 property->flags = flags;
509                                 empathy_debug (DEBUG_DOMAIN,
510                                                "property %s flags changed: %d",
511                                                property->name, property->flags);
512                                 break;
513                         }
514                 }
515         }
516 }
517
518 static void
519 tp_chat_properties_changed_cb (TpProxy         *proxy,
520                                const GPtrArray *properties,
521                                gpointer         user_data,
522                                GObject         *chat)
523 {
524         EmpathyTpChatPriv *priv = GET_PRIV (chat);
525         guint              i, j;
526
527         if (!priv->had_properties_list || !properties) {
528                 return;
529         }
530
531         for (i = 0; i < properties->len; i++) {
532                 GValueArray    *prop_struct;
533                 TpChatProperty *property;
534                 guint           id;
535                 GValue         *src_value;
536
537                 prop_struct = g_ptr_array_index (properties, i);
538                 id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
539                 src_value = g_value_get_boxed (g_value_array_get_nth (prop_struct, 1));
540
541                 for (j = 0; j < priv->properties->len; j++) {
542                         property = g_ptr_array_index (priv->properties, j);
543                         if (property->id == id) {
544                                 if (property->value) {
545                                         g_value_copy (src_value, property->value);
546                                 } else {
547                                         property->value = tp_g_value_slice_dup (src_value);
548                                 }
549
550                                 empathy_debug (DEBUG_DOMAIN, "property %s changed",
551                                                property->name);
552                                 g_signal_emit (chat, signals[PROPERTY_CHANGED], 0,
553                                                property->name, property->value);
554                                 break;
555                         }
556                 }
557         }
558 }
559
560 static void
561 tp_chat_get_properties_cb (TpProxy         *proxy,
562                            const GPtrArray *properties,
563                            const GError    *error,
564                            gpointer         user_data,
565                            GObject         *chat)
566 {
567         if (error) {
568                 empathy_debug (DEBUG_DOMAIN, "Error getting properties: %s",
569                                error->message);
570                 return;
571         }
572
573         tp_chat_properties_changed_cb (proxy, properties, user_data, chat);
574 }
575
576 static void
577 tp_chat_list_properties_cb (TpProxy         *proxy,
578                             const GPtrArray *properties,
579                             const GError    *error,
580                             gpointer         user_data,
581                             GObject         *chat)
582 {
583         EmpathyTpChatPriv *priv = GET_PRIV (chat);
584         GArray            *ids;
585         guint              i;
586
587         priv->had_properties_list = TRUE;
588
589         if (error) {
590                 empathy_debug (DEBUG_DOMAIN, "Error listing properties: %s",
591                                error->message);
592                 return;
593         }
594
595         ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), properties->len);
596         priv->properties = g_ptr_array_sized_new (properties->len);
597         for (i = 0; i < properties->len; i++) {
598                 GValueArray    *prop_struct;
599                 TpChatProperty *property;
600
601                 prop_struct = g_ptr_array_index (properties, i);
602                 property = g_slice_new0 (TpChatProperty);
603                 property->id = g_value_get_uint (g_value_array_get_nth (prop_struct, 0));
604                 property->name = g_value_dup_string (g_value_array_get_nth (prop_struct, 1));
605                 property->flags = g_value_get_uint (g_value_array_get_nth (prop_struct, 3));
606
607                 empathy_debug (DEBUG_DOMAIN, "Adding property name=%s id=%d flags=%d",
608                                property->name, property->id, property->flags);
609                 g_ptr_array_add (priv->properties, property);
610                 if (property->flags & TP_PROPERTY_FLAG_READ) {
611                         g_array_append_val (ids, property->id);
612                 }
613         }
614
615         tp_cli_properties_interface_call_get_properties (proxy, -1,
616                                                          ids,
617                                                          tp_chat_get_properties_cb,
618                                                          NULL, NULL,
619                                                          chat);
620
621         g_array_free (ids, TRUE);
622 }
623
624 void
625 empathy_tp_chat_set_property (EmpathyTpChat *chat,
626                               const gchar   *name,
627                               const GValue  *value)
628 {
629         EmpathyTpChatPriv *priv = GET_PRIV (chat);
630         TpChatProperty    *property;
631         guint              i;
632
633         for (i = 0; i < priv->properties->len; i++) {
634                 property = g_ptr_array_index (priv->properties, i);
635                 if (!tp_strdiff (property->name, name)) {
636                         GPtrArray   *properties;
637                         GValueArray *prop;
638                         GValue       id = {0, };
639                         GValue       dest_value = {0, };
640
641                         if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
642                                 break;
643                         }
644
645                         g_value_init (&id, G_TYPE_UINT);
646                         g_value_init (&dest_value, G_TYPE_VALUE);
647                         g_value_set_uint (&id, property->id);
648                         g_value_set_boxed (&dest_value, value);
649
650                         prop = g_value_array_new (2);
651                         g_value_array_append (prop, &id);
652                         g_value_array_append (prop, &dest_value);
653
654                         properties = g_ptr_array_sized_new (1);
655                         g_ptr_array_add (properties, prop);
656
657                         empathy_debug (DEBUG_DOMAIN, "Set property %s", name);
658                         tp_cli_properties_interface_call_set_properties (priv->channel, -1,
659                                                                          properties,
660                                                                          (tp_cli_properties_interface_callback_for_set_properties)
661                                                                          tp_chat_async_cb,
662                                                                          "Seting property", NULL,
663                                                                          G_OBJECT (chat));
664
665                         g_ptr_array_free (properties, TRUE);
666                         g_value_array_free (prop);
667
668                         break;
669                 }
670         }
671 }
672
673 static gboolean
674 tp_chat_channel_ready_cb (EmpathyTpChat *chat)
675 {
676         EmpathyTpChatPriv *priv = GET_PRIV (chat);
677
678         empathy_debug (DEBUG_DOMAIN, "Channel ready");
679
680         tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
681                                                              priv->acknowledge,
682                                                              tp_chat_list_pending_messages_cb,
683                                                              NULL, NULL,
684                                                              G_OBJECT (chat));
685         tp_cli_properties_interface_call_list_properties (priv->channel, -1,
686                                                           tp_chat_list_properties_cb,
687                                                           NULL, NULL,
688                                                           G_OBJECT (chat));
689
690
691         tp_cli_channel_type_text_connect_to_received (priv->channel,
692                                                       tp_chat_received_cb,
693                                                       NULL, NULL,
694                                                       G_OBJECT (chat), NULL);
695         tp_cli_channel_type_text_connect_to_sent (priv->channel,
696                                                   tp_chat_sent_cb,
697                                                   NULL, NULL,
698                                                   G_OBJECT (chat), NULL);
699         tp_cli_channel_type_text_connect_to_send_error (priv->channel,
700                                                         tp_chat_send_error_cb,
701                                                         NULL, NULL,
702                                                         G_OBJECT (chat), NULL);
703         tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
704                                                                            tp_chat_state_changed_cb,
705                                                                            NULL, NULL,
706                                                                            G_OBJECT (chat), NULL);
707         tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
708                                                                            tp_chat_state_changed_cb,
709                                                                            NULL, NULL,
710                                                                            G_OBJECT (chat), NULL);
711         tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
712                                                                    tp_chat_properties_changed_cb,
713                                                                    NULL, NULL,
714                                                                    G_OBJECT (chat), NULL);
715         tp_cli_properties_interface_connect_to_property_flags_changed (priv->channel,
716                                                                        tp_chat_property_flags_changed_cb,
717                                                                        NULL, NULL,
718                                                                        G_OBJECT (chat), NULL);
719
720         return FALSE;
721 }
722
723 static void
724 tp_chat_finalize (GObject *object)
725 {
726         EmpathyTpChatPriv *priv = GET_PRIV (object);
727         guint              i;
728
729         if (priv->acknowledge && priv->channel) {
730                 empathy_debug (DEBUG_DOMAIN, "Closing channel...");
731                 tp_cli_channel_call_close (priv->channel, -1,
732                                            tp_chat_async_cb,
733                                            "closing channel", NULL,
734                                            NULL);
735         }
736
737         if (priv->channel) {
738                 g_signal_handlers_disconnect_by_func (priv->channel,
739                                                       tp_chat_invalidated_cb,
740                                                       object);
741                 g_object_unref (priv->channel);
742         }
743         if (priv->tp_chan) {
744                 g_object_unref (priv->tp_chan);
745         }
746
747         if (priv->properties) {
748                 for (i = 0; i < priv->properties->len; i++) {
749                         TpChatProperty *property;
750
751                         property = g_ptr_array_index (priv->properties, i);
752                         g_free (property->name);
753                         if (property->value) {
754                                 tp_g_value_slice_free (property->value);
755                         }
756                         g_slice_free (TpChatProperty, property);
757                 }
758                 g_ptr_array_free (priv->properties, TRUE);
759         }
760
761         g_object_unref (priv->factory);
762         g_object_unref (priv->user);
763         g_object_unref (priv->account);
764         g_object_unref (priv->mc);
765         g_free (priv->id);
766
767         G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
768 }
769
770 static GObject *
771 tp_chat_constructor (GType                  type,
772                      guint                  n_props,
773                      GObjectConstructParam *props)
774 {
775         GObject           *chat;
776         EmpathyTpChatPriv *priv;
777         gboolean           channel_ready;
778
779         chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
780
781         priv = GET_PRIV (chat);
782         priv->factory = empathy_contact_factory_new ();
783         priv->user = empathy_contact_factory_get_user (priv->factory, priv->account);
784         priv->mc = empathy_mission_control_new ();
785
786         g_signal_connect (priv->channel, "invalidated",
787                           G_CALLBACK (tp_chat_invalidated_cb),
788                           chat);
789
790         g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
791         if (channel_ready) {
792                 tp_chat_channel_ready_cb (EMPATHY_TP_CHAT (chat));
793         } else {
794                 g_signal_connect_swapped (priv->channel, "notify::channel-ready",
795                                           G_CALLBACK (tp_chat_channel_ready_cb),
796                                           chat);
797         }
798
799         if (tp_proxy_has_interface_by_id (priv->channel,
800                                           TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP)) {
801                 priv->group = empathy_tp_group_new (priv->account, priv->tp_chan);
802
803                 g_signal_connect (priv->group, "member-added",
804                                   G_CALLBACK (tp_chat_member_added_cb),
805                                   chat);
806                 g_signal_connect (priv->group, "member-removed",
807                                   G_CALLBACK (tp_chat_member_removed_cb),
808                                   chat);
809                 g_signal_connect (priv->group, "local-pending",
810                                   G_CALLBACK (tp_chat_local_pending_cb),
811                                   chat);
812         } else {
813                 priv->initiator = empathy_contact_factory_get_from_handle (priv->factory,
814                                                                            priv->account,
815                                                                            priv->tp_chan->handle);
816         }
817
818         return chat;
819 }
820
821 static void
822 tp_chat_get_property (GObject    *object,
823                       guint       param_id,
824                       GValue     *value,
825                       GParamSpec *pspec)
826 {
827         EmpathyTpChatPriv *priv = GET_PRIV (object);
828
829         switch (param_id) {
830         case PROP_ACCOUNT:
831                 g_value_set_object (value, priv->account);
832                 break;
833         case PROP_TP_CHAN:
834                 g_value_set_object (value, priv->tp_chan);
835                 break;
836         case PROP_CHANNEL:
837                 g_value_set_object (value, priv->channel);
838                 break;
839         case PROP_ACKNOWLEDGE:
840                 g_value_set_boolean (value, priv->acknowledge);
841                 break;
842         default:
843                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
844                 break;
845         };
846 }
847
848 static void
849 tp_chat_set_property (GObject      *object,
850                       guint         param_id,
851                       const GValue *value,
852                       GParamSpec   *pspec)
853 {
854         EmpathyTpChatPriv *priv = GET_PRIV (object);
855
856         switch (param_id) {
857         case PROP_ACCOUNT:
858                 priv->account = g_object_ref (g_value_get_object (value));
859                 break;
860         case PROP_TP_CHAN:
861                 priv->tp_chan = g_object_ref (g_value_get_object (value));
862                 break;
863         case PROP_CHANNEL:
864                 priv->channel = g_object_ref (g_value_get_object (value));
865                 break;
866         case PROP_ACKNOWLEDGE:
867                 priv->acknowledge = g_value_get_boolean (value);
868                 break;
869         default:
870                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
871                 break;
872         };
873 }
874
875 static void
876 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
877 {
878         GObjectClass *object_class = G_OBJECT_CLASS (klass);
879
880         object_class->finalize = tp_chat_finalize;
881         object_class->constructor = tp_chat_constructor;
882         object_class->get_property = tp_chat_get_property;
883         object_class->set_property = tp_chat_set_property;
884
885         /* Construct properties */
886         g_object_class_install_property (object_class,
887                                          PROP_ACCOUNT,
888                                          g_param_spec_object ("account",
889                                                               "channel Account",
890                                                               "The account associated with the channel",
891                                                               MC_TYPE_ACCOUNT,
892                                                               G_PARAM_READWRITE |
893                                                               G_PARAM_CONSTRUCT_ONLY));
894         g_object_class_install_property (object_class,
895                                          PROP_TP_CHAN,
896                                          g_param_spec_object ("tp-chan",
897                                                               "telepathy channel",
898                                                               "The text channel for the chat",
899                                                               TELEPATHY_CHAN_TYPE,
900                                                               G_PARAM_READWRITE |
901                                                               G_PARAM_CONSTRUCT_ONLY));
902
903         g_object_class_install_property (object_class,
904                                          PROP_CHANNEL,
905                                          g_param_spec_object ("channel",
906                                                               "telepathy channel",
907                                                               "The text channel for the chat",
908                                                               TP_TYPE_CHANNEL,
909                                                               G_PARAM_READWRITE |
910                                                               G_PARAM_CONSTRUCT_ONLY));
911
912         g_object_class_install_property (object_class,
913                                          PROP_ACKNOWLEDGE,
914                                          g_param_spec_boolean ("acknowledge",
915                                                                "acknowledge messages",
916                                                                "Wheter or not received messages should be acknowledged",
917                                                                FALSE,
918                                                                G_PARAM_READWRITE |
919                                                                G_PARAM_CONSTRUCT_ONLY));
920
921         /* Signals */
922         signals[MESSAGE_RECEIVED] =
923                 g_signal_new ("message-received",
924                               G_TYPE_FROM_CLASS (klass),
925                               G_SIGNAL_RUN_LAST,
926                               0,
927                               NULL, NULL,
928                               g_cclosure_marshal_VOID__OBJECT,
929                               G_TYPE_NONE,
930                               1, EMPATHY_TYPE_MESSAGE);
931
932         signals[SEND_ERROR] =
933                 g_signal_new ("send-error",
934                               G_TYPE_FROM_CLASS (klass),
935                               G_SIGNAL_RUN_LAST,
936                               0,
937                               NULL, NULL,
938                               _empathy_marshal_VOID__OBJECT_UINT,
939                               G_TYPE_NONE,
940                               2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
941
942         signals[CHAT_STATE_CHANGED] =
943                 g_signal_new ("chat-state-changed",
944                               G_TYPE_FROM_CLASS (klass),
945                               G_SIGNAL_RUN_LAST,
946                               0,
947                               NULL, NULL,
948                               _empathy_marshal_VOID__OBJECT_UINT,
949                               G_TYPE_NONE,
950                               2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
951
952         signals[PROPERTY_CHANGED] =
953                 g_signal_new ("property-changed",
954                               G_TYPE_FROM_CLASS (klass),
955                               G_SIGNAL_RUN_LAST,
956                               0,
957                               NULL, NULL,
958                               _empathy_marshal_VOID__STRING_BOXED,
959                               G_TYPE_NONE,
960                               2, G_TYPE_STRING, G_TYPE_VALUE);
961
962         signals[DESTROY] =
963                 g_signal_new ("destroy",
964                               G_TYPE_FROM_CLASS (klass),
965                               G_SIGNAL_RUN_LAST,
966                               0,
967                               NULL, NULL,
968                               g_cclosure_marshal_VOID__VOID,
969                               G_TYPE_NONE,
970                               0);
971
972         g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
973 }
974
975 static void
976 empathy_tp_chat_init (EmpathyTpChat *chat)
977 {
978 }
979
980 static void
981 tp_chat_iface_init (EmpathyContactListIface *iface)
982 {
983         iface->add         = tp_chat_add;
984         iface->remove      = tp_chat_remove;
985         iface->get_members = tp_chat_get_members;
986 }
987
988 EmpathyTpChat *
989 empathy_tp_chat_new (McAccount *account,
990                      TpChan    *tp_chan,
991                      gboolean   acknowledge)
992 {
993         EmpathyTpChat  *chat;
994         TpChannel      *channel;
995         TpConnection   *connection;
996         MissionControl *mc;
997         TpConn         *tp_conn;
998
999         mc = empathy_mission_control_new ();
1000         tp_conn = mission_control_get_connection (mc, account, NULL);
1001         connection = tp_conn_dup_connection (tp_conn);
1002         channel = tp_chan_dup_channel (tp_chan, connection, NULL);
1003
1004         chat = g_object_new (EMPATHY_TYPE_TP_CHAT, 
1005                              "account", account,
1006                              "channel", channel,
1007                              "tp-chan", tp_chan,
1008                              "acknowledge", acknowledge,
1009                              NULL);
1010
1011         g_object_unref (channel);
1012         g_object_unref (tp_conn);
1013         g_object_unref (connection);
1014         g_object_unref (mc);
1015
1016         return chat;
1017 }
1018
1019 EmpathyTpChat *
1020 empathy_tp_chat_new_with_contact (EmpathyContact *contact)
1021 {
1022         EmpathyTpChat  *chat;
1023         MissionControl *mc;
1024         McAccount      *account;
1025         TpConn         *tp_conn;
1026         TpChan         *text_chan;
1027         const gchar    *bus_name;
1028         guint           handle;
1029
1030         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1031
1032         mc = empathy_mission_control_new ();
1033         account = empathy_contact_get_account (contact);
1034
1035         if (mission_control_get_connection_status (mc, account, NULL) != 0) {
1036                 /* The account is not connected. */
1037                 return NULL;
1038         }
1039
1040         tp_conn = mission_control_get_connection (mc, account, NULL);
1041         g_return_val_if_fail (tp_conn != NULL, NULL);
1042         bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (tp_conn));
1043         handle = empathy_contact_get_handle (contact);
1044
1045         text_chan = tp_conn_new_channel (tp_get_bus (),
1046                                          tp_conn,
1047                                          bus_name,
1048                                          TP_IFACE_CHANNEL_TYPE_TEXT,
1049                                          TP_HANDLE_TYPE_CONTACT,
1050                                          handle,
1051                                          TRUE);
1052
1053         chat = empathy_tp_chat_new (account, text_chan, TRUE);
1054
1055         g_object_unref (tp_conn);
1056         g_object_unref (text_chan);
1057         g_object_unref (mc);
1058
1059         return chat;
1060 }
1061
1062 TpChan *
1063 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
1064 {
1065         EmpathyTpChatPriv *priv;
1066
1067         g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1068
1069         priv = GET_PRIV (chat);
1070
1071         return priv->tp_chan;
1072 }
1073
1074 McAccount *
1075 empathy_tp_chat_get_account (EmpathyTpChat *chat)
1076 {
1077         EmpathyTpChatPriv *priv;
1078
1079         g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1080
1081         priv = GET_PRIV (chat);
1082
1083         return priv->account;
1084 }
1085
1086 void
1087 empathy_tp_chat_send (EmpathyTpChat *chat,
1088                       EmpathyMessage *message)
1089 {
1090         EmpathyTpChatPriv  *priv;
1091         const gchar        *message_body;
1092         EmpathyMessageType  message_type;
1093
1094         g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1095         g_return_if_fail (EMPATHY_IS_MESSAGE (message));
1096
1097         priv = GET_PRIV (chat);
1098
1099         message_body = empathy_message_get_body (message);
1100         message_type = empathy_message_get_type (message);
1101
1102         empathy_debug (DEBUG_DOMAIN, "Sending message: %s", message_body);
1103         tp_cli_channel_type_text_call_send (priv->channel, -1,
1104                                             message_type,
1105                                             message_body,
1106                                             tp_chat_async_cb,
1107                                             "sending message", NULL,
1108                                             G_OBJECT (chat));
1109 }
1110
1111 void
1112 empathy_tp_chat_set_state (EmpathyTpChat      *chat,
1113                            TpChannelChatState  state)
1114 {
1115         EmpathyTpChatPriv *priv;
1116
1117         g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
1118
1119         priv = GET_PRIV (chat);
1120
1121         empathy_debug (DEBUG_DOMAIN, "Set state: %d", state);
1122         tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
1123                                                                  state,
1124                                                                  tp_chat_async_cb,
1125                                                                  "setting chat state",
1126                                                                  NULL,
1127                                                                  G_OBJECT (chat));
1128 }
1129
1130 const gchar *
1131 empathy_tp_chat_get_id (EmpathyTpChat *chat)
1132 {
1133         EmpathyTpChatPriv *priv;
1134
1135         g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1136
1137         priv = GET_PRIV (chat);
1138
1139         if (!priv->id) {
1140                 priv->id = empathy_inspect_channel (priv->account, priv->tp_chan);
1141         }
1142
1143         return priv->id;
1144 }
1145