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