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