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