1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2002-2007 Imendio AB
4 * Copyright (C) 2007 Collabora Ltd.
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.
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.
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.
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>
33 #include <glade/glade.h>
34 #include <glib/gi18n.h>
36 #include <libmissioncontrol/mc-account.h>
38 #include <libempathy/gossip-debug.h>
39 #include <libempathy/empathy-tp-chat.h>
40 //#include <libgossip/gossip-log.h>
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"
50 #define DEBUG_DOMAIN "PrivateChat"
52 #define COMPOSING_STOP_TIMEOUT 5
54 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GOSSIP_TYPE_PRIVATE_CHAT, GossipPrivateChatPriv))
56 struct _GossipPrivateChatPriv {
57 GossipContact *contact;
64 GtkWidget *text_view_sw;
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,
73 GossipPrivateChat *chat);
74 static void private_chat_contact_updated_cb (GossipContact *contact,
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);
85 G_DEFINE_TYPE (GossipPrivateChat, gossip_private_chat, GOSSIP_TYPE_CHAT);
88 gossip_private_chat_class_init (GossipPrivateChatClass *klass)
90 GObjectClass *object_class = G_OBJECT_CLASS (klass);
91 GossipChatClass *chat_class = GOSSIP_CHAT_CLASS (klass);
93 object_class->finalize = private_chat_finalize;
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;
104 g_type_class_add_private (object_class, sizeof (GossipPrivateChatPriv));
108 gossip_private_chat_init (GossipPrivateChat *chat)
110 GossipPrivateChatPriv *priv;
112 priv = GET_PRIV (chat);
114 priv->is_online = FALSE;
116 private_chat_create_ui (chat);
121 private_chat_finalize (GObject *object)
123 GossipPrivateChat *chat;
124 GossipPrivateChatPriv *priv;
126 chat = GOSSIP_PRIVATE_CHAT (object);
127 priv = GET_PRIV (chat);
129 g_signal_handlers_disconnect_by_func (priv->contact,
130 private_chat_contact_updated_cb,
132 g_signal_handlers_disconnect_by_func (priv->contact,
133 private_chat_contact_presence_updated_cb,
137 g_object_unref (priv->contact);
140 if (priv->scroll_idle_id) {
141 g_source_remove (priv->scroll_idle_id);
146 G_OBJECT_CLASS (gossip_private_chat_parent_class)->finalize (object);
150 private_chat_create_ui (GossipPrivateChat *chat)
153 GossipPrivateChatPriv *priv;
154 GtkWidget *input_text_view_sw;
156 priv = GET_PRIV (chat);
158 glade = gossip_glade_get_file ("gossip-chat.glade",
161 "chat_widget", &priv->widget,
162 "chat_view_sw", &priv->text_view_sw,
163 "input_text_view_sw", &input_text_view_sw,
166 gossip_glade_connect (glade,
168 "chat_widget", "destroy", private_chat_widget_destroy_cb,
171 g_object_unref (glade);
173 g_object_set_data (G_OBJECT (priv->widget), "chat", g_object_ref (chat));
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));
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);
185 private_chat_contact_presence_updated_cb (GossipContact *contact,
187 GossipPrivateChat *chat)
189 GossipPrivateChatPriv *priv;
191 priv = GET_PRIV (chat);
193 gossip_debug (DEBUG_DOMAIN, "Presence update for contact: %s",
194 gossip_contact_get_id (contact));
196 if (!gossip_contact_is_online (contact)) {
197 if (priv->is_online) {
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);
206 priv->is_online = FALSE;
208 g_signal_emit_by_name (chat, "composing", FALSE);
211 if (!priv->is_online) {
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);
220 priv->is_online = TRUE;
223 g_signal_emit_by_name (chat, "status-changed");
227 private_chat_contact_updated_cb (GossipContact *contact,
229 GossipPrivateChat *chat)
231 GossipPrivateChatPriv *priv;
233 priv = GET_PRIV (chat);
235 if (strcmp (priv->name, gossip_contact_get_name (contact)) != 0) {
237 priv->name = g_strdup (gossip_contact_get_name (contact));
238 g_signal_emit_by_name (chat, "name-changed", priv->name);
243 private_chat_widget_destroy_cb (GtkWidget *widget,
244 GossipPrivateChat *chat)
246 gossip_debug (DEBUG_DOMAIN, "Destroyed");
248 g_object_unref (chat);
252 private_chat_get_name (GossipChat *chat)
254 GossipPrivateChatPriv *priv;
256 g_return_val_if_fail (GOSSIP_IS_PRIVATE_CHAT (chat), NULL);
258 priv = GET_PRIV (chat);
264 private_chat_get_tooltip (GossipChat *chat)
266 GossipPrivateChatPriv *priv;
267 GossipContact *contact;
270 g_return_val_if_fail (GOSSIP_IS_PRIVATE_CHAT (chat), NULL);
272 priv = GET_PRIV (chat);
274 contact = gossip_chat_get_contact (chat);
275 status = gossip_contact_get_status (contact);
277 return g_strdup_printf ("%s\n%s",
278 gossip_contact_get_id (contact),
283 private_chat_get_status_icon_name (GossipChat *chat)
285 GossipPrivateChatPriv *priv;
286 GossipContact *contact;
288 g_return_val_if_fail (GOSSIP_IS_PRIVATE_CHAT (chat), NULL);
290 priv = GET_PRIV (chat);
292 contact = gossip_chat_get_contact (chat);
294 return gossip_icon_name_for_contact (contact);
297 static GossipContact *
298 private_chat_get_contact (GossipChat *chat)
300 GossipPrivateChatPriv *priv;
302 g_return_val_if_fail (GOSSIP_IS_PRIVATE_CHAT (chat), NULL);
304 priv = GET_PRIV (chat);
306 return priv->contact;
310 private_chat_get_widget (GossipChat *chat)
312 GossipPrivateChatPriv *priv;
314 priv = GET_PRIV (chat);
319 /* Scroll down after the back-log has been received. */
321 private_chat_scroll_down_idle_func (GossipChat *chat)
323 GossipPrivateChatPriv *priv;
325 priv = GET_PRIV (chat);
327 gossip_chat_scroll_down (chat);
328 g_object_unref (chat);
330 priv->scroll_idle_id = 0;
336 private_chat_setup (GossipPrivateChat *chat,
337 GossipContact *contact,
338 EmpathyTpChat *tp_chat)
340 GossipPrivateChatPriv *priv;
341 //GossipLogManager *log_manager;
342 GossipChatView *view;
343 /* GossipContact *sender;
344 GossipMessage *message;
346 gint num_messages, i;*/
348 priv = GET_PRIV (chat);
350 gossip_chat_set_tp_chat (GOSSIP_CHAT (chat), tp_chat);
352 priv->contact = g_object_ref (contact);
353 GOSSIP_CHAT (chat)->account = g_object_ref (gossip_contact_get_account (contact));
355 priv->name = g_strdup (gossip_contact_get_name (contact));
357 g_signal_connect (priv->contact,
359 G_CALLBACK (private_chat_contact_updated_cb),
361 g_signal_connect (priv->contact,
363 G_CALLBACK (private_chat_contact_presence_updated_cb),
366 view = GOSSIP_CHAT (chat)->view;
368 /* Turn off scrolling temporarily */
369 gossip_chat_view_scroll (view, FALSE);
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);
377 for (l = messages, i = 0; l; l = l->next, i++) {
380 if (num_messages - i > 10) {
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,
391 gossip_chat_view_append_message_from_other (view,
398 g_list_foreach (messages, (GFunc) g_object_unref, NULL);
399 g_list_free (messages);
401 /* Turn back on scrolling */
402 gossip_chat_view_scroll (view, TRUE);
404 /* Scroll to the most recent messages, we reference the chat
405 * for the duration of the scroll func.
407 priv->scroll_idle_id = g_idle_add ((GSourceFunc)
408 private_chat_scroll_down_idle_func,
409 g_object_ref (chat));
411 priv->is_online = gossip_contact_is_online (priv->contact);
415 gossip_private_chat_new (GossipContact *contact)
417 GossipPrivateChat *chat;
418 EmpathyTpChat *tp_chat;
420 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), NULL);
422 chat = g_object_new (GOSSIP_TYPE_PRIVATE_CHAT, NULL);
423 tp_chat = empathy_tp_chat_new_with_contact (contact);
425 private_chat_setup (chat, contact, tp_chat);
426 g_object_unref (tp_chat);
432 gossip_private_chat_new_with_channel (GossipContact *contact,
435 GossipPrivateChat *chat;
436 EmpathyTpChat *tp_chat;
439 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), NULL);
440 g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
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);
446 private_chat_setup (chat, contact, tp_chat);
447 g_object_unref (tp_chat);