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