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