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