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 GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GOSSIP_TYPE_PRIVATE_CHAT, GossipPrivateChatPriv))
54 struct _GossipPrivateChatPriv {
55 GossipContact *contact;
61 GtkWidget *text_view_sw;
64 static void gossip_private_chat_class_init (GossipPrivateChatClass *klass);
65 static void gossip_private_chat_init (GossipPrivateChat *chat);
66 static void private_chat_finalize (GObject *object);
67 static void private_chat_create_ui (GossipPrivateChat *chat);
68 static void private_chat_contact_presence_updated_cb (GossipContact *contact,
70 GossipPrivateChat *chat);
71 static void private_chat_contact_updated_cb (GossipContact *contact,
73 GossipPrivateChat *chat);
74 static void private_chat_widget_destroy_cb (GtkWidget *widget,
75 GossipPrivateChat *chat);
76 static const gchar * private_chat_get_name (GossipChat *chat);
77 static gchar * private_chat_get_tooltip (GossipChat *chat);
78 static const gchar * private_chat_get_status_icon_name (GossipChat *chat);
79 static GtkWidget * private_chat_get_widget (GossipChat *chat);
81 G_DEFINE_TYPE (GossipPrivateChat, gossip_private_chat, GOSSIP_TYPE_CHAT);
84 gossip_private_chat_class_init (GossipPrivateChatClass *klass)
86 GObjectClass *object_class = G_OBJECT_CLASS (klass);
87 GossipChatClass *chat_class = GOSSIP_CHAT_CLASS (klass);
89 object_class->finalize = private_chat_finalize;
91 chat_class->get_name = private_chat_get_name;
92 chat_class->get_tooltip = private_chat_get_tooltip;
93 chat_class->get_status_icon_name = private_chat_get_status_icon_name;
94 chat_class->get_widget = private_chat_get_widget;
96 g_type_class_add_private (object_class, sizeof (GossipPrivateChatPriv));
100 gossip_private_chat_init (GossipPrivateChat *chat)
102 GossipPrivateChatPriv *priv;
104 priv = GET_PRIV (chat);
106 priv->is_online = FALSE;
108 private_chat_create_ui (chat);
113 private_chat_finalize (GObject *object)
115 GossipPrivateChat *chat;
116 GossipPrivateChatPriv *priv;
118 chat = GOSSIP_PRIVATE_CHAT (object);
119 priv = GET_PRIV (chat);
121 g_signal_handlers_disconnect_by_func (priv->contact,
122 private_chat_contact_updated_cb,
124 g_signal_handlers_disconnect_by_func (priv->contact,
125 private_chat_contact_presence_updated_cb,
129 g_object_unref (priv->contact);
134 G_OBJECT_CLASS (gossip_private_chat_parent_class)->finalize (object);
138 private_chat_create_ui (GossipPrivateChat *chat)
141 GossipPrivateChatPriv *priv;
142 GtkWidget *input_text_view_sw;
144 priv = GET_PRIV (chat);
146 glade = gossip_glade_get_file ("gossip-chat.glade",
149 "chat_widget", &priv->widget,
150 "chat_view_sw", &priv->text_view_sw,
151 "input_text_view_sw", &input_text_view_sw,
154 gossip_glade_connect (glade,
156 "chat_widget", "destroy", private_chat_widget_destroy_cb,
159 g_object_unref (glade);
161 g_object_set_data (G_OBJECT (priv->widget), "chat", g_object_ref (chat));
163 gtk_container_add (GTK_CONTAINER (priv->text_view_sw),
164 GTK_WIDGET (GOSSIP_CHAT (chat)->view));
165 gtk_widget_show (GTK_WIDGET (GOSSIP_CHAT (chat)->view));
167 gtk_container_add (GTK_CONTAINER (input_text_view_sw),
168 GOSSIP_CHAT (chat)->input_text_view);
169 gtk_widget_show (GOSSIP_CHAT (chat)->input_text_view);
173 private_chat_contact_presence_updated_cb (GossipContact *contact,
175 GossipPrivateChat *chat)
177 GossipPrivateChatPriv *priv;
179 priv = GET_PRIV (chat);
181 gossip_debug (DEBUG_DOMAIN, "Presence update for contact: %s",
182 gossip_contact_get_id (contact));
184 if (!gossip_contact_is_online (contact)) {
185 if (priv->is_online) {
188 msg = g_strdup_printf (_("%s went offline"),
189 gossip_contact_get_name (priv->contact));
190 gossip_chat_view_append_event (GOSSIP_CHAT (chat)->view, msg);
194 priv->is_online = FALSE;
196 g_signal_emit_by_name (chat, "composing", FALSE);
199 if (!priv->is_online) {
202 msg = g_strdup_printf (_("%s has come online"),
203 gossip_contact_get_name (priv->contact));
204 gossip_chat_view_append_event (GOSSIP_CHAT (chat)->view, msg);
208 priv->is_online = TRUE;
211 g_signal_emit_by_name (chat, "status-changed");
215 private_chat_contact_updated_cb (GossipContact *contact,
217 GossipPrivateChat *chat)
219 GossipPrivateChatPriv *priv;
221 priv = GET_PRIV (chat);
223 if (strcmp (priv->name, gossip_contact_get_name (contact)) != 0) {
225 priv->name = g_strdup (gossip_contact_get_name (contact));
226 g_signal_emit_by_name (chat, "name-changed", priv->name);
231 private_chat_widget_destroy_cb (GtkWidget *widget,
232 GossipPrivateChat *chat)
234 gossip_debug (DEBUG_DOMAIN, "Destroyed");
236 g_object_unref (chat);
240 private_chat_get_name (GossipChat *chat)
242 GossipPrivateChatPriv *priv;
244 g_return_val_if_fail (GOSSIP_IS_PRIVATE_CHAT (chat), NULL);
246 priv = GET_PRIV (chat);
252 private_chat_get_tooltip (GossipChat *chat)
254 GossipPrivateChatPriv *priv;
257 g_return_val_if_fail (GOSSIP_IS_PRIVATE_CHAT (chat), NULL);
259 priv = GET_PRIV (chat);
261 status = gossip_contact_get_status (priv->contact);
263 return g_strdup_printf ("%s\n%s",
264 gossip_contact_get_id (priv->contact),
269 private_chat_get_status_icon_name (GossipChat *chat)
271 GossipPrivateChatPriv *priv;
273 g_return_val_if_fail (GOSSIP_IS_PRIVATE_CHAT (chat), NULL);
275 priv = GET_PRIV (chat);
277 return gossip_icon_name_for_contact (priv->contact);
281 gossip_private_chat_get_contact (GossipPrivateChat *chat)
283 GossipPrivateChatPriv *priv;
285 g_return_val_if_fail (GOSSIP_IS_PRIVATE_CHAT (chat), NULL);
287 priv = GET_PRIV (chat);
289 return priv->contact;
293 private_chat_get_widget (GossipChat *chat)
295 GossipPrivateChatPriv *priv;
297 priv = GET_PRIV (chat);
303 private_chat_setup (GossipPrivateChat *chat,
304 GossipContact *contact,
305 EmpathyTpChat *tp_chat)
307 GossipPrivateChatPriv *priv;
309 priv = GET_PRIV (chat);
311 gossip_chat_set_tp_chat (GOSSIP_CHAT (chat), tp_chat);
313 priv->contact = g_object_ref (contact);
314 GOSSIP_CHAT (chat)->account = g_object_ref (gossip_contact_get_account (contact));
316 priv->name = g_strdup (gossip_contact_get_name (contact));
318 g_signal_connect (priv->contact,
320 G_CALLBACK (private_chat_contact_updated_cb),
322 g_signal_connect (priv->contact,
324 G_CALLBACK (private_chat_contact_presence_updated_cb),
327 priv->is_online = gossip_contact_is_online (priv->contact);
331 gossip_private_chat_new (GossipContact *contact)
333 GossipPrivateChat *chat;
334 EmpathyTpChat *tp_chat;
336 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), NULL);
338 chat = g_object_new (GOSSIP_TYPE_PRIVATE_CHAT, NULL);
339 tp_chat = empathy_tp_chat_new_with_contact (contact);
341 private_chat_setup (chat, contact, tp_chat);
342 g_object_unref (tp_chat);
348 gossip_private_chat_new_with_channel (GossipContact *contact,
351 GossipPrivateChat *chat;
352 EmpathyTpChat *tp_chat;
355 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), NULL);
356 g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
358 account = gossip_contact_get_account (contact);
359 chat = g_object_new (GOSSIP_TYPE_PRIVATE_CHAT, NULL);
360 tp_chat = empathy_tp_chat_new (account, tp_chan);
362 private_chat_setup (chat, contact, tp_chat);
363 g_object_unref (tp_chat);