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