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