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