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