]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-chat.c
Implement _set_property
[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                         g_value_init (&id, G_TYPE_UINT);
516                         g_value_init (&dest_value, G_TYPE_VALUE);
517                         g_value_set_uint (&id, property->id);
518                         g_value_set_boxed (&dest_value, value);
519
520                         prop = g_value_array_new (2);
521                         g_value_array_append (prop, &id);
522                         g_value_array_append (prop, &dest_value);
523
524                         properties = g_ptr_array_sized_new (1);
525                         g_ptr_array_add (properties, prop);
526
527                         empathy_debug (DEBUG_DOMAIN, "Set property %s", name);
528                         tp_cli_properties_interface_call_set_properties (priv->channel, -1,
529                                                                          properties,
530                                                                          (tp_cli_properties_interface_callback_for_set_properties)
531                                                                          tp_chat_async_cb,
532                                                                          "Seting property", NULL,
533                                                                          G_OBJECT (chat));
534
535                         break;
536                 }
537         }
538 }
539
540 static gboolean
541 tp_chat_channel_ready_cb (EmpathyTpChat *chat)
542 {
543         EmpathyTpChatPriv *priv = GET_PRIV (chat);
544
545         empathy_debug (DEBUG_DOMAIN, "Channel ready");
546
547         tp_cli_channel_type_text_call_list_pending_messages (priv->channel, -1,
548                                                              priv->acknowledge,
549                                                              tp_chat_list_pending_messages_cb,
550                                                              NULL, NULL,
551                                                              G_OBJECT (chat));
552         tp_cli_properties_interface_call_list_properties (priv->channel, -1,
553                                                           tp_chat_list_properties_cb,
554                                                           NULL, NULL,
555                                                           G_OBJECT (chat));
556
557
558         tp_cli_channel_type_text_connect_to_received (priv->channel,
559                                                       tp_chat_received_cb,
560                                                       NULL, NULL,
561                                                       G_OBJECT (chat), NULL);
562         tp_cli_channel_type_text_connect_to_sent (priv->channel,
563                                                   tp_chat_sent_cb,
564                                                   NULL, NULL,
565                                                   G_OBJECT (chat), NULL);
566         tp_cli_channel_type_text_connect_to_send_error (priv->channel,
567                                                         tp_chat_send_error_cb,
568                                                         NULL, NULL,
569                                                         G_OBJECT (chat), NULL);
570         tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
571                                                                            tp_chat_state_changed_cb,
572                                                                            NULL, NULL,
573                                                                            G_OBJECT (chat), NULL);
574         tp_cli_channel_interface_chat_state_connect_to_chat_state_changed (priv->channel,
575                                                                            tp_chat_state_changed_cb,
576                                                                            NULL, NULL,
577                                                                            G_OBJECT (chat), NULL);
578         tp_cli_properties_interface_connect_to_properties_changed (priv->channel,
579                                                                    tp_chat_properties_changed_cb,
580                                                                    NULL, NULL,
581                                                                    G_OBJECT (chat), NULL);
582
583         return FALSE;
584 }
585
586 static void
587 tp_chat_finalize (GObject *object)
588 {
589         EmpathyTpChatPriv *priv = GET_PRIV (object);
590         guint              i;
591
592         if (priv->acknowledge && priv->channel) {
593                 empathy_debug (DEBUG_DOMAIN, "Closing channel...");
594                 tp_cli_channel_call_close (priv->channel, -1,
595                                            tp_chat_async_cb,
596                                            "closing channel", NULL,
597                                            NULL);
598         }
599
600         if (priv->channel) {
601                 g_signal_handlers_disconnect_by_func (priv->channel,
602                                                       tp_chat_invalidated_cb,
603                                                       object);
604                 g_object_unref (priv->channel);
605         }
606         if (priv->tp_chan) {
607                 g_object_unref (priv->tp_chan);
608         }
609
610         if (priv->properties) {
611                 for (i = 0; i < priv->properties->len; i++) {
612                         TpChatProperty *property;
613
614                         property = g_ptr_array_index (priv->properties, i);
615                         g_free (property->name);
616                         if (property->value) {
617                                 g_value_unset (property->value);
618                                 g_slice_free (GValue, property->value);
619                         }
620                         g_slice_free (TpChatProperty, property);
621                 }
622                 g_ptr_array_free (priv->properties, TRUE);
623         }
624
625         g_object_unref (priv->factory);
626         g_object_unref (priv->user);
627         g_object_unref (priv->account);
628         g_object_unref (priv->mc);
629         g_free (priv->id);
630
631         G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
632 }
633
634 static GObject *
635 tp_chat_constructor (GType                  type,
636                      guint                  n_props,
637                      GObjectConstructParam *props)
638 {
639         GObject           *chat;
640         EmpathyTpChatPriv *priv;
641         gboolean           channel_ready;
642
643         chat = G_OBJECT_CLASS (empathy_tp_chat_parent_class)->constructor (type, n_props, props);
644
645         priv = GET_PRIV (chat);
646         priv->factory = empathy_contact_factory_new ();
647         priv->user = empathy_contact_factory_get_user (priv->factory, priv->account);
648         priv->mc = empathy_mission_control_new ();
649
650         g_signal_connect (priv->channel, "invalidated",
651                           G_CALLBACK (tp_chat_invalidated_cb),
652                           chat);
653
654         g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
655         if (channel_ready) {
656                 /* FIXME: We do that in a cb to let time to set the acknowledge
657                  * property, this property should be required for construct. */
658                 g_idle_add ((GSourceFunc) tp_chat_channel_ready_cb, chat);
659         } else {
660                 g_signal_connect_swapped (priv->channel, "notify::channel-ready",
661                                           G_CALLBACK (tp_chat_channel_ready_cb),
662                                           chat);
663         }
664
665         return chat;
666 }
667
668 static void
669 tp_chat_get_property (GObject    *object,
670                       guint       param_id,
671                       GValue     *value,
672                       GParamSpec *pspec)
673 {
674         EmpathyTpChatPriv *priv = GET_PRIV (object);
675
676         switch (param_id) {
677         case PROP_ACCOUNT:
678                 g_value_set_object (value, priv->account);
679                 break;
680         case PROP_TP_CHAN:
681                 g_value_set_object (value, priv->tp_chan);
682                 break;
683         case PROP_CHANNEL:
684                 g_value_set_object (value, priv->channel);
685                 break;
686         case PROP_ACKNOWLEDGE:
687                 g_value_set_boolean (value, priv->acknowledge);
688                 break;
689         default:
690                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
691                 break;
692         };
693 }
694
695 static void
696 tp_chat_set_property (GObject      *object,
697                       guint         param_id,
698                       const GValue *value,
699                       GParamSpec   *pspec)
700 {
701         EmpathyTpChatPriv *priv = GET_PRIV (object);
702
703         switch (param_id) {
704         case PROP_ACCOUNT:
705                 priv->account = g_object_ref (g_value_get_object (value));
706                 break;
707         case PROP_TP_CHAN:
708                 priv->tp_chan = g_object_ref (g_value_get_object (value));
709                 break;
710         case PROP_CHANNEL:
711                 priv->channel = g_object_ref (g_value_get_object (value));
712                 break;
713         case PROP_ACKNOWLEDGE:
714                 empathy_tp_chat_set_acknowledge (EMPATHY_TP_CHAT (object),
715                                                  g_value_get_boolean (value));
716                 break;
717         default:
718                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
719                 break;
720         };
721 }
722
723 static void
724 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
725 {
726         GObjectClass *object_class = G_OBJECT_CLASS (klass);
727
728         object_class->finalize = tp_chat_finalize;
729         object_class->constructor = tp_chat_constructor;
730         object_class->get_property = tp_chat_get_property;
731         object_class->set_property = tp_chat_set_property;
732
733         /* Construct properties */
734         g_object_class_install_property (object_class,
735                                          PROP_ACCOUNT,
736                                          g_param_spec_object ("account",
737                                                               "channel Account",
738                                                               "The account associated with the channel",
739                                                               MC_TYPE_ACCOUNT,
740                                                               G_PARAM_READWRITE |
741                                                               G_PARAM_CONSTRUCT_ONLY));
742         g_object_class_install_property (object_class,
743                                          PROP_TP_CHAN,
744                                          g_param_spec_object ("tp-chan",
745                                                               "telepathy channel",
746                                                               "The text channel for the chat",
747                                                               TELEPATHY_CHAN_TYPE,
748                                                               G_PARAM_READWRITE |
749                                                               G_PARAM_CONSTRUCT_ONLY));
750
751         g_object_class_install_property (object_class,
752                                          PROP_CHANNEL,
753                                          g_param_spec_object ("channel",
754                                                               "telepathy channel",
755                                                               "The text channel for the chat",
756                                                               TP_TYPE_CHANNEL,
757                                                               G_PARAM_READWRITE |
758                                                               G_PARAM_CONSTRUCT_ONLY));
759
760         g_object_class_install_property (object_class,
761                                          PROP_ACKNOWLEDGE,
762                                          g_param_spec_boolean ("acknowledge",
763                                                                "acknowledge messages",
764                                                                "Wheter or not received messages should be acknowledged",
765                                                                FALSE,
766                                                                G_PARAM_READWRITE |
767                                                                G_PARAM_CONSTRUCT));
768
769         /* Signals */
770         signals[MESSAGE_RECEIVED] =
771                 g_signal_new ("message-received",
772                               G_TYPE_FROM_CLASS (klass),
773                               G_SIGNAL_RUN_LAST,
774                               0,
775                               NULL, NULL,
776                               g_cclosure_marshal_VOID__OBJECT,
777                               G_TYPE_NONE,
778                               1, EMPATHY_TYPE_MESSAGE);
779
780         signals[SEND_ERROR] =
781                 g_signal_new ("send-error",
782                               G_TYPE_FROM_CLASS (klass),
783                               G_SIGNAL_RUN_LAST,
784                               0,
785                               NULL, NULL,
786                               _empathy_marshal_VOID__OBJECT_UINT,
787                               G_TYPE_NONE,
788                               2, EMPATHY_TYPE_MESSAGE, G_TYPE_UINT);
789
790         signals[CHAT_STATE_CHANGED] =
791                 g_signal_new ("chat-state-changed",
792                               G_TYPE_FROM_CLASS (klass),
793                               G_SIGNAL_RUN_LAST,
794                               0,
795                               NULL, NULL,
796                               _empathy_marshal_VOID__OBJECT_UINT,
797                               G_TYPE_NONE,
798                               2, EMPATHY_TYPE_CONTACT, G_TYPE_UINT);
799
800         signals[PROPERTY_CHANGED] =
801                 g_signal_new ("property-changed",
802                               G_TYPE_FROM_CLASS (klass),
803                               G_SIGNAL_RUN_LAST,
804                               0,
805                               NULL, NULL,
806                               _empathy_marshal_VOID__STRING_BOXED,
807                               G_TYPE_NONE,
808                               2, G_TYPE_STRING, G_TYPE_VALUE);
809
810         signals[DESTROY] =
811                 g_signal_new ("destroy",
812                               G_TYPE_FROM_CLASS (klass),
813                               G_SIGNAL_RUN_LAST,
814                               0,
815                               NULL, NULL,
816                               g_cclosure_marshal_VOID__VOID,
817                               G_TYPE_NONE,
818                               0);
819
820         g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
821 }
822
823 static void
824 empathy_tp_chat_init (EmpathyTpChat *chat)
825 {
826 }
827
828 EmpathyTpChat *
829 empathy_tp_chat_new (McAccount *account,
830                      TpChan    *tp_chan)
831 {
832         EmpathyTpChat  *chat;
833         TpChannel      *channel;
834         TpConnection   *connection;
835         MissionControl *mc;
836         TpConn         *tp_conn;
837
838         mc = empathy_mission_control_new ();
839         tp_conn = mission_control_get_connection (mc, account, NULL);
840         connection = tp_conn_dup_connection (tp_conn);
841         channel = tp_chan_dup_channel (tp_chan, connection, NULL);
842
843         chat = g_object_new (EMPATHY_TYPE_TP_CHAT, 
844                              "account", account,
845                              "channel", channel,
846                              "tp-chan", tp_chan,
847                              NULL);
848
849         g_object_unref (channel);
850         g_object_unref (tp_conn);
851         g_object_unref (connection);
852         g_object_unref (mc);
853
854         return chat;
855 }
856
857 EmpathyTpChat *
858 empathy_tp_chat_new_with_contact (EmpathyContact *contact)
859 {
860         EmpathyTpChat  *chat;
861         MissionControl *mc;
862         McAccount      *account;
863         TpConn         *tp_conn;
864         TpChan         *text_chan;
865         const gchar    *bus_name;
866         guint           handle;
867
868         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
869
870         mc = empathy_mission_control_new ();
871         account = empathy_contact_get_account (contact);
872
873         if (mission_control_get_connection_status (mc, account, NULL) != 0) {
874                 /* The account is not connected. */
875                 return NULL;
876         }
877
878         tp_conn = mission_control_get_connection (mc, account, NULL);
879         g_return_val_if_fail (tp_conn != NULL, NULL);
880         bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (tp_conn));
881         handle = empathy_contact_get_handle (contact);
882
883         text_chan = tp_conn_new_channel (tp_get_bus (),
884                                          tp_conn,
885                                          bus_name,
886                                          TP_IFACE_CHANNEL_TYPE_TEXT,
887                                          TP_HANDLE_TYPE_CONTACT,
888                                          handle,
889                                          TRUE);
890
891         chat = empathy_tp_chat_new (account, text_chan);
892
893         g_object_unref (tp_conn);
894         g_object_unref (text_chan);
895         g_object_unref (mc);
896
897         return chat;
898 }
899
900 gboolean
901 empathy_tp_chat_get_acknowledge (EmpathyTpChat *chat)
902 {
903         EmpathyTpChatPriv *priv;
904
905         g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), FALSE);
906
907         priv = GET_PRIV (chat);
908
909         return priv->acknowledge;
910 }
911
912 void
913 empathy_tp_chat_set_acknowledge (EmpathyTpChat *chat,
914                                  gboolean       acknowledge)
915 {
916         EmpathyTpChatPriv *priv;
917
918         g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
919
920         priv = GET_PRIV (chat);
921
922         priv->acknowledge = acknowledge;
923         g_object_notify (G_OBJECT (chat), "acknowledge");
924 }
925
926 TpChan *
927 empathy_tp_chat_get_channel (EmpathyTpChat *chat)
928 {
929         EmpathyTpChatPriv *priv;
930
931         g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
932
933         priv = GET_PRIV (chat);
934
935         return priv->tp_chan;
936 }
937
938 McAccount *
939 empathy_tp_chat_get_account (EmpathyTpChat *chat)
940 {
941         EmpathyTpChatPriv *priv;
942
943         g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
944
945         priv = GET_PRIV (chat);
946
947         return priv->account;
948 }
949
950 void
951 empathy_tp_chat_send (EmpathyTpChat *chat,
952                       EmpathyMessage *message)
953 {
954         EmpathyTpChatPriv  *priv;
955         const gchar        *message_body;
956         EmpathyMessageType  message_type;
957
958         g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
959         g_return_if_fail (EMPATHY_IS_MESSAGE (message));
960
961         priv = GET_PRIV (chat);
962
963         message_body = empathy_message_get_body (message);
964         message_type = empathy_message_get_type (message);
965
966         empathy_debug (DEBUG_DOMAIN, "Sending message: %s", message_body);
967         tp_cli_channel_type_text_call_send (priv->channel, -1,
968                                             message_type,
969                                             message_body,
970                                             tp_chat_async_cb,
971                                             "sending message", NULL,
972                                             G_OBJECT (chat));
973 }
974
975 void
976 empathy_tp_chat_set_state (EmpathyTpChat      *chat,
977                            TpChannelChatState  state)
978 {
979         EmpathyTpChatPriv *priv;
980
981         g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
982
983         priv = GET_PRIV (chat);
984
985         empathy_debug (DEBUG_DOMAIN, "Set state: %d", state);
986         tp_cli_channel_interface_chat_state_call_set_chat_state (priv->channel, -1,
987                                                                  state,
988                                                                  tp_chat_async_cb,
989                                                                  "setting chat state",
990                                                                  NULL,
991                                                                  G_OBJECT (chat));
992 }
993
994 const gchar *
995 empathy_tp_chat_get_id (EmpathyTpChat *chat)
996 {
997         EmpathyTpChatPriv *priv;
998
999         g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
1000
1001         priv = GET_PRIV (chat);
1002
1003         if (!priv->id) {
1004                 priv->id = empathy_inspect_channel (priv->account, priv->tp_chan);
1005         }
1006
1007         return priv->id;
1008 }
1009