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