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