]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-chat.c
[darcs-to-svn @ Remove EmpathySession and move all programs into src/]
[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 Collabora Ltd.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program 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  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  * 
20  * Authors: Xavier Claessens <xclaesse@gmail.com>
21  */
22
23 #include <config.h>
24
25 #include <libtelepathy/tp-helpers.h>
26 #include <libtelepathy/tp-chan-type-text-gen.h>
27 #include <libtelepathy/tp-chan-iface-chat-state-gen.h>
28 #include <libtelepathy/tp-conn.h>
29
30 #include "empathy-tp-chat.h"
31 #include "empathy-contact-manager.h"
32 #include "empathy-contact-list.h"
33 #include "empathy-marshal.h"
34 #include "gossip-debug.h"
35 #include "gossip-time.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         EmpathyContactList    *list;
44         EmpathyContactManager *manager;
45         McAccount             *account;
46         gchar                 *id;
47         MissionControl        *mc;
48
49         TpChan                *tp_chan;
50         DBusGProxy            *text_iface;
51         DBusGProxy            *chat_state_iface;
52 };
53
54 static void empathy_tp_chat_class_init (EmpathyTpChatClass *klass);
55 static void empathy_tp_chat_init       (EmpathyTpChat      *chat);
56 static void tp_chat_finalize           (GObject            *object);
57 static void tp_chat_destroy_cb         (TpChan             *text_chan,
58                                         EmpathyTpChat      *chat);
59 static void tp_chat_received_cb        (DBusGProxy         *text_iface,
60                                         guint               message_id,
61                                         guint               timestamp,
62                                         guint               from_handle,
63                                         guint               message_type,
64                                         guint               message_flags,
65                                         gchar              *message_body,
66                                         EmpathyTpChat      *chat);
67 static void tp_chat_sent_cb            (DBusGProxy         *text_iface,
68                                         guint               timestamp,
69                                         guint               message_type,
70                                         gchar              *message_body,
71                                         EmpathyTpChat      *chat);
72 static void tp_chat_state_changed_cb   (DBusGProxy         *chat_state_iface,
73                                         guint               handle,
74                                         EmpathyTpChatState  state,
75                                         EmpathyTpChat      *chat);
76 static void tp_chat_emit_message       (EmpathyTpChat      *chat,
77                                         guint               type,
78                                         guint               timestamp,
79                                         guint               from_handle,
80                                         const gchar        *message_body);
81
82 enum {
83         MESSAGE_RECEIVED,
84         CHAT_STATE_CHANGED,
85         DESTROY,
86         LAST_SIGNAL
87 };
88
89 static guint signals[LAST_SIGNAL];
90
91 G_DEFINE_TYPE (EmpathyTpChat, empathy_tp_chat, G_TYPE_OBJECT);
92
93 static void
94 empathy_tp_chat_class_init (EmpathyTpChatClass *klass)
95 {
96         GObjectClass *object_class = G_OBJECT_CLASS (klass);
97
98         object_class->finalize = tp_chat_finalize;
99
100         signals[MESSAGE_RECEIVED] =
101                 g_signal_new ("message-received",
102                               G_TYPE_FROM_CLASS (klass),
103                               G_SIGNAL_RUN_LAST,
104                               0,
105                               NULL, NULL,
106                               g_cclosure_marshal_VOID__OBJECT,
107                               G_TYPE_NONE,
108                               1, GOSSIP_TYPE_MESSAGE);
109
110         signals[CHAT_STATE_CHANGED] =
111                 g_signal_new ("chat-state-changed",
112                               G_TYPE_FROM_CLASS (klass),
113                               G_SIGNAL_RUN_LAST,
114                               0,
115                               NULL, NULL,
116                               empathy_marshal_VOID__OBJECT_UINT,
117                               G_TYPE_NONE,
118                               2, GOSSIP_TYPE_CONTACT, G_TYPE_UINT);
119
120         signals[DESTROY] =
121                 g_signal_new ("destroy",
122                               G_TYPE_FROM_CLASS (klass),
123                               G_SIGNAL_RUN_LAST,
124                               0,
125                               NULL, NULL,
126                               g_cclosure_marshal_VOID__VOID,
127                               G_TYPE_NONE,
128                               0);
129
130         g_type_class_add_private (object_class, sizeof (EmpathyTpChatPriv));
131 }
132
133 static void
134 empathy_tp_chat_init (EmpathyTpChat *chat)
135 {
136 }
137
138
139 static void
140 tp_chat_finalize (GObject *object)
141 {
142         EmpathyTpChatPriv *priv;
143         EmpathyTpChat     *chat;
144         GError            *error = NULL;
145
146         chat = EMPATHY_TP_CHAT (object);
147         priv = GET_PRIV (chat);
148
149         if (priv->tp_chan) {
150                 gossip_debug (DEBUG_DOMAIN, "Closing channel...");
151
152                 g_signal_handlers_disconnect_by_func (priv->tp_chan,
153                                                       tp_chat_destroy_cb,
154                                                       object);
155
156                 if (!tp_chan_close (DBUS_G_PROXY (priv->tp_chan), &error)) {
157                         gossip_debug (DEBUG_DOMAIN, 
158                                       "Error closing text channel: %s",
159                                       error ? error->message : "No error given");
160                         g_clear_error (&error);
161                 }
162                 g_object_unref (priv->tp_chan);
163         }
164
165         if (priv->manager) {
166                 g_object_unref (priv->manager);
167         }
168         if (priv->list) {
169                 g_object_unref (priv->list);
170         }
171         if (priv->account) {
172                 g_object_unref (priv->account);
173         }
174         if (priv->mc) {
175                 g_object_unref (priv->mc);
176         }
177         g_free (priv->id);
178
179         G_OBJECT_CLASS (empathy_tp_chat_parent_class)->finalize (object);
180 }
181
182 EmpathyTpChat *
183 empathy_tp_chat_new (McAccount *account,
184                      TpChan    *tp_chan)
185 {
186         EmpathyTpChatPriv     *priv;
187         EmpathyTpChat         *chat;
188
189         g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
190         g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
191
192         chat = g_object_new (EMPATHY_TYPE_TP_CHAT, NULL);
193         priv = GET_PRIV (chat);
194
195         priv->manager = empathy_contact_manager_new ();
196         priv->list = empathy_contact_manager_get_list (priv->manager, account);
197         priv->tp_chan = g_object_ref (tp_chan);
198         priv->account = g_object_ref (account);
199         priv->mc = mission_control_new (tp_get_bus ());
200         g_object_ref (priv->list);
201
202         priv->text_iface = tp_chan_get_interface (tp_chan,
203                                                   TELEPATHY_CHAN_IFACE_TEXT_QUARK);
204         priv->chat_state_iface = tp_chan_get_interface (tp_chan,
205                                                         TELEPATHY_CHAN_IFACE_CHAT_STATE_QUARK);
206
207         g_signal_connect (priv->tp_chan, "destroy",
208                           G_CALLBACK (tp_chat_destroy_cb),
209                           chat);
210         dbus_g_proxy_connect_signal (priv->text_iface, "Received",
211                                      G_CALLBACK (tp_chat_received_cb),
212                                      chat, NULL);
213         dbus_g_proxy_connect_signal (priv->text_iface, "Sent",
214                                      G_CALLBACK (tp_chat_sent_cb),
215                                      chat, NULL);
216
217         if (priv->chat_state_iface != NULL) {
218                 dbus_g_proxy_connect_signal (priv->chat_state_iface,
219                                              "ChatStateChanged",
220                                              G_CALLBACK (tp_chat_state_changed_cb),
221                                              chat, NULL);
222         }
223
224         return chat;
225 }
226
227 EmpathyTpChat *
228 empathy_tp_chat_new_with_contact (GossipContact *contact)
229 {
230         EmpathyTpChat  *chat;
231         MissionControl *mc;
232         McAccount      *account;
233         TpConn         *tp_conn;
234         TpChan         *text_chan;
235         const gchar    *bus_name;
236         guint           handle;
237
238         g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), NULL);
239
240         mc = mission_control_new (tp_get_bus ());
241         account = gossip_contact_get_account (contact);
242
243         if (mission_control_get_connection_status (mc, account, NULL) != 0) {
244                 /* The account is not connected, nothing to do. */
245                 return NULL;
246         }
247
248         tp_conn = mission_control_get_connection (mc, account, NULL);
249         g_return_val_if_fail (tp_conn != NULL, NULL);
250         bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (tp_conn));
251         handle = gossip_contact_get_handle (contact);
252
253         text_chan = tp_conn_new_channel (tp_get_bus (),
254                                          tp_conn,
255                                          bus_name,
256                                          TP_IFACE_CHANNEL_TYPE_TEXT,
257                                          TP_HANDLE_TYPE_CONTACT,
258                                          handle,
259                                          TRUE);
260
261         chat = empathy_tp_chat_new (account, text_chan);
262
263         g_object_unref (tp_conn);
264         g_object_unref (text_chan);
265         g_object_unref (mc);
266
267         return chat;
268 }
269
270 void
271 empathy_tp_chat_request_pending (EmpathyTpChat *chat)
272 {
273         EmpathyTpChatPriv *priv;
274         GPtrArray         *messages_list;
275         guint              i;
276         GError            *error = NULL;
277
278         g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
279
280         priv = GET_PRIV (chat);
281
282         /* If we do this call async, don't forget to ignore Received signal
283          * until we get the answer */
284         if (!tp_chan_type_text_list_pending_messages (priv->text_iface,
285                                                       TRUE,
286                                                       &messages_list,
287                                                       &error)) {
288                 gossip_debug (DEBUG_DOMAIN, 
289                               "Error retrieving pending messages: %s",
290                               error ? error->message : "No error given");
291                 g_clear_error (&error);
292                 return;
293         }
294
295         for (i = 0; i < messages_list->len; i++) {
296                 GValueArray *message_struct;
297                 const gchar *message_body;
298                 guint        message_id;
299                 guint        timestamp;
300                 guint        from_handle;
301                 guint        message_type;
302                 guint        message_flags;
303
304                 message_struct = g_ptr_array_index (messages_list, i);
305
306                 message_id = g_value_get_uint (g_value_array_get_nth (message_struct, 0));
307                 timestamp = g_value_get_uint (g_value_array_get_nth (message_struct, 1));
308                 from_handle = g_value_get_uint (g_value_array_get_nth (message_struct, 2));
309                 message_type = g_value_get_uint (g_value_array_get_nth (message_struct, 3));
310                 message_flags = g_value_get_uint (g_value_array_get_nth (message_struct, 4));
311                 message_body = g_value_get_string (g_value_array_get_nth (message_struct, 5));
312
313                 gossip_debug (DEBUG_DOMAIN, "Message pending: %s", message_body);
314
315                 tp_chat_emit_message (chat,
316                                       message_type,
317                                       timestamp,
318                                       from_handle,
319                                       message_body);
320
321                 g_value_array_free (message_struct);
322         }
323
324         g_ptr_array_free (messages_list, TRUE);
325 }
326
327 void
328 empathy_tp_chat_send (EmpathyTpChat *chat,
329                       GossipMessage *message)
330 {
331         EmpathyTpChatPriv *priv;
332         const gchar       *message_body;
333         GossipMessageType  message_type;
334         GError            *error = NULL;
335
336         g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
337         g_return_if_fail (GOSSIP_IS_MESSAGE (message));
338
339         priv = GET_PRIV (chat);
340
341         message_body = gossip_message_get_body (message);
342         message_type = gossip_message_get_type (message);
343
344         gossip_debug (DEBUG_DOMAIN, "Sending message: %s", message_body);
345         if (!tp_chan_type_text_send (priv->text_iface,
346                                      message_type,
347                                      message_body,
348                                      &error)) {
349                 gossip_debug (DEBUG_DOMAIN, 
350                               "Send Error: %s", 
351                               error ? error->message : "No error given");
352                 g_clear_error (&error);
353         }
354 }
355
356 void
357 empathy_tp_chat_send_state (EmpathyTpChat      *chat,
358                             EmpathyTpChatState  state)
359 {
360         EmpathyTpChatPriv *priv;
361         GError            *error = NULL;
362
363         g_return_if_fail (EMPATHY_IS_TP_CHAT (chat));
364
365         priv = GET_PRIV (chat);
366
367         if (priv->chat_state_iface) {
368                 gossip_debug (DEBUG_DOMAIN, "Set state: %d", state);
369                 if (!tp_chan_iface_chat_state_set_chat_state (priv->chat_state_iface,
370                                                               state,
371                                                               &error)) {
372                         gossip_debug (DEBUG_DOMAIN,
373                                       "Set Chat State Error: %s",
374                                       error ? error->message : "No error given");
375                         g_clear_error (&error);
376                 }
377         }
378 }
379
380 const gchar *
381 empathy_tp_chat_get_id (EmpathyTpChat *chat)
382 {
383         EmpathyTpChatPriv  *priv;
384         TpConn             *tp_conn;
385         GArray             *handles;
386         gchar             **names;
387         GError             *error = NULL;
388
389         g_return_val_if_fail (EMPATHY_IS_TP_CHAT (chat), NULL);
390
391         priv = GET_PRIV (chat);
392
393         if (priv->id) {
394                 return priv->id;
395         }
396
397         tp_conn = mission_control_get_connection (priv->mc, priv->account, NULL);
398         handles = g_array_new (FALSE, FALSE, sizeof (guint));
399         g_array_append_val (handles, priv->tp_chan->handle);
400
401         if (!tp_conn_inspect_handles (DBUS_G_PROXY (tp_conn),
402                                       priv->tp_chan->handle_type,
403                                       handles,
404                                       &names,
405                                       &error)) {
406                 gossip_debug (DEBUG_DOMAIN, 
407                               "Couldn't get id: %s",
408                               error ? error->message : "No error given");
409                 g_clear_error (&error);
410                 g_array_free (handles, TRUE);
411                 g_object_unref (tp_conn);
412                 
413                 return NULL;
414         }
415
416         /* A handle name is unique only for a specific account */
417         priv->id = g_strdup_printf ("%s/%s",
418                                     mc_account_get_unique_name (priv->account),
419                                     *names);
420
421         g_strfreev (names);
422         g_object_unref (tp_conn);
423
424         return priv->id;
425 }
426
427 static void
428 tp_chat_destroy_cb (TpChan        *text_chan,
429                     EmpathyTpChat *chat)
430 {
431         EmpathyTpChatPriv *priv;
432
433         priv = GET_PRIV (chat);
434
435         gossip_debug (DEBUG_DOMAIN, "Channel destroyed");
436
437         g_object_unref  (priv->tp_chan);
438         priv->tp_chan = NULL;
439         priv->text_iface = NULL;
440         priv->chat_state_iface = NULL;
441
442         g_signal_emit (chat, signals[DESTROY], 0);
443 }
444
445 static void
446 tp_chat_received_cb (DBusGProxy    *text_iface,
447                      guint          message_id,
448                      guint          timestamp,
449                      guint          from_handle,
450                      guint          message_type,
451                      guint          message_flags,
452                      gchar         *message_body,
453                      EmpathyTpChat *chat)
454 {
455         EmpathyTpChatPriv *priv;
456         GArray            *message_ids;
457
458         priv = GET_PRIV (chat);
459
460         gossip_debug (DEBUG_DOMAIN, "Message received: %s", message_body);
461
462         tp_chat_emit_message (chat,
463                               message_type,
464                               timestamp,
465                               from_handle,
466                               message_body);
467
468         message_ids = g_array_new (FALSE, FALSE, sizeof (guint));
469         g_array_append_val (message_ids, message_id);
470         tp_chan_type_text_acknowledge_pending_messages (priv->text_iface,
471                                                         message_ids, NULL);
472         g_array_free (message_ids, TRUE);
473 }
474
475 static void
476 tp_chat_sent_cb (DBusGProxy    *text_iface,
477                  guint          timestamp,
478                  guint          message_type,
479                  gchar         *message_body,
480                  EmpathyTpChat *chat)
481 {
482         gossip_debug (DEBUG_DOMAIN, "Message sent: %s", message_body);
483
484         tp_chat_emit_message (chat,
485                               message_type,
486                               timestamp,
487                               0,
488                               message_body);
489 }
490
491 static void
492 tp_chat_state_changed_cb (DBusGProxy         *chat_state_iface,
493                           guint               handle,
494                           EmpathyTpChatState  state,
495                           EmpathyTpChat      *chat)
496 {
497         EmpathyTpChatPriv *priv;
498         GossipContact     *contact;
499
500         priv = GET_PRIV (chat);
501
502         contact = empathy_contact_list_get_from_handle (priv->list, handle);
503
504         g_signal_emit (chat, signals[CHAT_STATE_CHANGED], 0, contact, state);
505
506         g_object_unref (contact);
507 }
508
509 static void
510 tp_chat_emit_message (EmpathyTpChat *chat,
511                       guint          type,
512                       guint          timestamp,
513                       guint          from_handle,
514                       const gchar   *message_body)
515 {
516         EmpathyTpChatPriv *priv;
517         GossipMessage     *message;
518         GossipContact     *sender;
519
520         priv = GET_PRIV (chat);
521
522         if (from_handle == 0) {
523                 sender = empathy_contact_list_get_own (priv->list);
524                 g_object_ref (sender);
525         } else {
526                 sender = empathy_contact_list_get_from_handle (priv->list,
527                                                                from_handle);
528         }
529
530         message = gossip_message_new (message_body);
531         gossip_message_set_type (message, type);
532         gossip_message_set_sender (message, sender);
533         gossip_message_set_timestamp (message, (GossipTime) timestamp);
534
535         g_signal_emit (chat, signals[MESSAGE_RECEIVED], 0, message);
536
537         g_object_unref (message);
538         g_object_unref (sender);
539 }
540