]> git.0d.be Git - empathy.git/blob - libempathy-gtk/gossip-private-chat.c
a3bdbb6be2c6ccf0cd3a5355655129820075448a
[empathy.git] / libempathy-gtk / gossip-private-chat.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2002-2007 Imendio AB
4  * Copyright (C) 2007 Collabora Ltd.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  * Authors: Mikael Hallendal <micke@imendio.com>
22  *          Richard Hult <richard@imendio.com>
23  *          Martyn Russell <martyn@imendio.com>
24  *          Geert-Jan Van den Bogaerde <geertjan@gnome.org>
25  *          Xavier Claessens <xclaesse@gmail.com>
26  */
27
28 #include "config.h"
29
30 #include <string.h>
31
32 #include <gtk/gtk.h>
33 #include <glade/glade.h>
34 #include <glib/gi18n.h>
35
36 #include <libmissioncontrol/mc-account.h>
37
38 #include <libempathy/gossip-debug.h>
39 #include <libempathy/empathy-tp-chat.h>
40 //#include <libgossip/gossip-log.h>
41
42 #include "gossip-private-chat.h"
43 #include "gossip-chat-view.h"
44 #include "gossip-chat.h"
45 #include "gossip-preferences.h"
46 //#include "gossip-sound.h"
47 #include "empathy-images.h"
48 #include "gossip-ui-utils.h"
49
50 #define DEBUG_DOMAIN "PrivateChat"
51
52 #define COMPOSING_STOP_TIMEOUT 5
53
54 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GOSSIP_TYPE_PRIVATE_CHAT, GossipPrivateChatPriv))
55
56 struct _GossipPrivateChatPriv {   
57         GossipContact *contact;
58         gchar         *name;
59
60         guint          scroll_idle_id;
61         gboolean       is_online;
62
63         GtkWidget     *widget;
64         GtkWidget     *text_view_sw;
65 };
66
67 static void           gossip_private_chat_class_init            (GossipPrivateChatClass *klass);
68 static void           gossip_private_chat_init                  (GossipPrivateChat      *chat);
69 static void           private_chat_finalize                     (GObject                *object);
70 static void           private_chat_create_ui                    (GossipPrivateChat      *chat);
71 static void           private_chat_contact_presence_updated_cb  (GossipContact          *contact,
72                                                                  GParamSpec             *param,
73                                                                  GossipPrivateChat      *chat);
74 static void           private_chat_contact_updated_cb           (GossipContact          *contact,
75                                                                  GParamSpec             *param,
76                                                                  GossipPrivateChat      *chat);
77 static void           private_chat_widget_destroy_cb            (GtkWidget              *widget,
78                                                                  GossipPrivateChat      *chat);
79 static const gchar *  private_chat_get_name                     (GossipChat             *chat);
80 static gchar *        private_chat_get_tooltip                  (GossipChat             *chat);
81 static const gchar *  private_chat_get_status_icon_name         (GossipChat             *chat);
82 static GossipContact *private_chat_get_contact                  (GossipChat             *chat);
83 static GtkWidget *    private_chat_get_widget                   (GossipChat             *chat);
84
85 G_DEFINE_TYPE (GossipPrivateChat, gossip_private_chat, GOSSIP_TYPE_CHAT);
86
87 static void
88 gossip_private_chat_class_init (GossipPrivateChatClass *klass)
89 {
90         GObjectClass    *object_class = G_OBJECT_CLASS (klass);
91         GossipChatClass *chat_class = GOSSIP_CHAT_CLASS (klass);
92
93         object_class->finalize = private_chat_finalize;
94
95         chat_class->get_name             = private_chat_get_name;
96         chat_class->get_tooltip          = private_chat_get_tooltip;
97         chat_class->get_status_icon_name = private_chat_get_status_icon_name;
98         chat_class->get_contact          = private_chat_get_contact;
99         chat_class->get_widget           = private_chat_get_widget;
100         chat_class->get_show_contacts    = NULL;
101         chat_class->set_show_contacts    = NULL;
102         chat_class->is_group_chat        = NULL;
103
104         g_type_class_add_private (object_class, sizeof (GossipPrivateChatPriv));
105 }
106
107 static void
108 gossip_private_chat_init (GossipPrivateChat *chat)
109 {
110         GossipPrivateChatPriv *priv;
111
112         priv = GET_PRIV (chat);
113
114         priv->is_online = FALSE;
115
116         private_chat_create_ui (chat);
117
118 }
119
120 static void
121 private_chat_finalize (GObject *object)
122 {
123         GossipPrivateChat     *chat;
124         GossipPrivateChatPriv *priv;
125         
126         chat = GOSSIP_PRIVATE_CHAT (object);
127         priv = GET_PRIV (chat);
128
129         g_signal_handlers_disconnect_by_func (priv->contact,
130                                               private_chat_contact_updated_cb,
131                                               chat);
132         g_signal_handlers_disconnect_by_func (priv->contact,
133                                               private_chat_contact_presence_updated_cb,
134                                               chat);
135
136         if (priv->contact) {
137                 g_object_unref (priv->contact);
138         }
139
140         if (priv->scroll_idle_id) {
141                 g_source_remove (priv->scroll_idle_id);
142         }
143
144         g_free (priv->name);
145
146         G_OBJECT_CLASS (gossip_private_chat_parent_class)->finalize (object);
147 }
148
149 static void
150 private_chat_create_ui (GossipPrivateChat *chat)
151 {
152         GladeXML              *glade;
153         GossipPrivateChatPriv *priv;
154         GtkWidget             *input_text_view_sw;
155
156         priv = GET_PRIV (chat);
157
158         glade = gossip_glade_get_file ("gossip-chat.glade",
159                                        "chat_widget",
160                                        NULL,
161                                       "chat_widget", &priv->widget,
162                                       "chat_view_sw", &priv->text_view_sw,
163                                       "input_text_view_sw", &input_text_view_sw,
164                                        NULL);
165
166         gossip_glade_connect (glade,
167                               chat,
168                               "chat_widget", "destroy", private_chat_widget_destroy_cb,
169                               NULL);
170
171         g_object_unref (glade);
172
173         g_object_set_data (G_OBJECT (priv->widget), "chat", g_object_ref (chat));
174
175         gtk_container_add (GTK_CONTAINER (priv->text_view_sw),
176                            GTK_WIDGET (GOSSIP_CHAT (chat)->view));
177         gtk_widget_show (GTK_WIDGET (GOSSIP_CHAT (chat)->view));
178
179         gtk_container_add (GTK_CONTAINER (input_text_view_sw),
180                            GOSSIP_CHAT (chat)->input_text_view);
181         gtk_widget_show (GOSSIP_CHAT (chat)->input_text_view);
182 }
183
184 static void
185 private_chat_contact_presence_updated_cb (GossipContact     *contact,
186                                           GParamSpec        *param,
187                                           GossipPrivateChat *chat)
188 {
189         GossipPrivateChatPriv *priv;
190
191         priv = GET_PRIV (chat);
192
193         gossip_debug (DEBUG_DOMAIN, "Presence update for contact: %s",
194                       gossip_contact_get_id (contact));
195
196         if (!gossip_contact_is_online (contact)) {
197                 if (priv->is_online) {
198                         gchar *msg;
199
200                         msg = g_strdup_printf (_("%s went offline"),
201                                                gossip_contact_get_name (priv->contact));
202                         gossip_chat_view_append_event (GOSSIP_CHAT (chat)->view, msg);
203                         g_free (msg);
204                 }
205
206                 priv->is_online = FALSE;
207
208                 g_signal_emit_by_name (chat, "composing", FALSE);
209
210         } else {
211                 if (!priv->is_online) {
212                         gchar *msg;
213
214                         msg = g_strdup_printf (_("%s has come online"),
215                                                gossip_contact_get_name (priv->contact));
216                         gossip_chat_view_append_event (GOSSIP_CHAT (chat)->view, msg);
217                         g_free (msg);
218                 }
219
220                 priv->is_online = TRUE;
221         }
222
223         g_signal_emit_by_name (chat, "status-changed");
224 }
225
226 static void
227 private_chat_contact_updated_cb (GossipContact     *contact,
228                                  GParamSpec        *param,
229                                  GossipPrivateChat *chat)
230 {
231         GossipPrivateChatPriv *priv;
232
233         priv = GET_PRIV (chat);
234
235         if (strcmp (priv->name, gossip_contact_get_name (contact)) != 0) {
236                 g_free (priv->name);
237                 priv->name = g_strdup (gossip_contact_get_name (contact));
238                 g_signal_emit_by_name (chat, "name-changed", priv->name);
239         }
240 }
241
242 static void
243 private_chat_widget_destroy_cb (GtkWidget         *widget,
244                                 GossipPrivateChat *chat)
245 {
246         gossip_debug (DEBUG_DOMAIN, "Destroyed");
247
248         g_object_unref (chat);
249 }
250
251 static const gchar *
252 private_chat_get_name (GossipChat *chat)
253 {
254         GossipPrivateChatPriv *priv;
255
256         g_return_val_if_fail (GOSSIP_IS_PRIVATE_CHAT (chat), NULL);
257
258         priv = GET_PRIV (chat);
259
260         return priv->name;
261 }
262
263 static gchar *
264 private_chat_get_tooltip (GossipChat *chat)
265 {
266         GossipPrivateChatPriv *priv;
267         GossipContact         *contact;
268         const gchar           *status;
269
270         g_return_val_if_fail (GOSSIP_IS_PRIVATE_CHAT (chat), NULL);
271
272         priv = GET_PRIV (chat);
273
274         contact = gossip_chat_get_contact (chat);
275         status = gossip_contact_get_status (contact);
276
277         return g_strdup_printf ("%s\n%s",
278                                 gossip_contact_get_id (contact),
279                                 status);
280 }
281
282 static const gchar *
283 private_chat_get_status_icon_name (GossipChat *chat)
284 {
285         GossipPrivateChatPriv *priv;
286         GossipContact         *contact;
287
288         g_return_val_if_fail (GOSSIP_IS_PRIVATE_CHAT (chat), NULL);
289
290         priv = GET_PRIV (chat);
291
292         contact = gossip_chat_get_contact (chat);
293
294         return gossip_icon_name_for_contact (contact);
295 }
296
297 static GossipContact *
298 private_chat_get_contact (GossipChat *chat)
299 {
300         GossipPrivateChatPriv *priv;
301
302         g_return_val_if_fail (GOSSIP_IS_PRIVATE_CHAT (chat), NULL);
303
304         priv = GET_PRIV (chat);
305
306         return priv->contact;
307 }
308
309 static GtkWidget *
310 private_chat_get_widget (GossipChat *chat)
311 {
312         GossipPrivateChatPriv *priv;
313
314         priv = GET_PRIV (chat);
315
316         return priv->widget;
317 }
318
319 /* Scroll down after the back-log has been received. */
320 static gboolean
321 private_chat_scroll_down_idle_func (GossipChat *chat)
322 {
323         GossipPrivateChatPriv *priv;
324
325         priv = GET_PRIV (chat);
326
327         gossip_chat_scroll_down (chat);
328         g_object_unref (chat);
329
330         priv->scroll_idle_id = 0;
331
332         return FALSE;
333 }
334
335 static void
336 private_chat_setup (GossipPrivateChat *chat,
337                     GossipContact     *contact,
338                     EmpathyTpChat     *tp_chat)
339 {
340         GossipPrivateChatPriv *priv;
341         //GossipLogManager      *log_manager;
342         GossipChatView        *view;
343 /*      GossipContact         *sender;
344         GossipMessage         *message;
345         GList                 *messages, *l;
346         gint                   num_messages, i;*/
347
348         priv = GET_PRIV (chat);
349
350         gossip_chat_set_tp_chat (GOSSIP_CHAT (chat), tp_chat);
351
352         priv->contact = g_object_ref (contact);
353         GOSSIP_CHAT (chat)->account = g_object_ref (gossip_contact_get_account (contact));
354
355         priv->name = g_strdup (gossip_contact_get_name (contact));
356
357         g_signal_connect (priv->contact, 
358                           "notify::name",
359                           G_CALLBACK (private_chat_contact_updated_cb),
360                           chat);
361         g_signal_connect (priv->contact, 
362                           "notify::presence",
363                           G_CALLBACK (private_chat_contact_presence_updated_cb),
364                           chat);
365
366         view = GOSSIP_CHAT (chat)->view;
367
368         /* Turn off scrolling temporarily */
369         gossip_chat_view_scroll (view, FALSE);
370 #if 0
371 FIXME:
372         /* Add messages from last conversation */
373         log_manager = gossip_session_get_log_manager (gossip_app_get_session ());
374         messages = gossip_log_get_last_for_contact (log_manager, priv->contact);
375         num_messages  = g_list_length (messages);
376
377         for (l = messages, i = 0; l; l = l->next, i++) {
378                 message = l->data;
379
380                 if (num_messages - i > 10) {
381                         continue;
382                 }
383
384                 sender = gossip_message_get_sender (message);
385                 if (gossip_contact_equal (priv->own_contact, sender)) {
386                         gossip_chat_view_append_message_from_self (view,
387                                                                    message,
388                                                                    priv->own_contact,
389                                                                    priv->own_avatar);
390                 } else {
391                         gossip_chat_view_append_message_from_other (view,
392                                                                     message,
393                                                                     sender,
394                                                                     priv->other_avatar);
395                 }
396         }
397
398         g_list_foreach (messages, (GFunc) g_object_unref, NULL);
399         g_list_free (messages);
400 #endif
401         /* Turn back on scrolling */
402         gossip_chat_view_scroll (view, TRUE);
403
404         /* Scroll to the most recent messages, we reference the chat
405          * for the duration of the scroll func.
406          */
407         priv->scroll_idle_id = g_idle_add ((GSourceFunc) 
408                                            private_chat_scroll_down_idle_func, 
409                                            g_object_ref (chat));
410
411         priv->is_online = gossip_contact_is_online (priv->contact);
412 }
413
414 GossipPrivateChat *
415 gossip_private_chat_new (GossipContact *contact)
416 {
417         GossipPrivateChat *chat;
418         EmpathyTpChat     *tp_chat;
419
420         g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), NULL);
421
422         chat = g_object_new (GOSSIP_TYPE_PRIVATE_CHAT, NULL);
423         tp_chat = empathy_tp_chat_new_with_contact (contact);
424
425         private_chat_setup (chat, contact, tp_chat);
426         g_object_unref (tp_chat);
427
428         return chat;
429 }
430
431 GossipPrivateChat *
432 gossip_private_chat_new_with_channel (GossipContact *contact,
433                                       TpChan        *tp_chan)
434 {
435         GossipPrivateChat *chat;
436         EmpathyTpChat     *tp_chat;
437         McAccount         *account;
438
439         g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), NULL);
440         g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
441
442         account = gossip_contact_get_account (contact);
443         chat = g_object_new (GOSSIP_TYPE_PRIVATE_CHAT, NULL);
444         tp_chat = empathy_tp_chat_new (account, tp_chan);
445
446         private_chat_setup (chat, contact, tp_chat);
447         g_object_unref (tp_chat);
448
449         return chat;
450 }
451