]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-private-chat.c
Fix a leaked ref.
[empathy.git] / libempathy-gtk / empathy-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 <libempathy/empathy-debug.h>
37 #include <libempathy/empathy-tp-chat.h>
38 #include <libempathy/empathy-tp-contact-list.h>
39 #include <libempathy/empathy-contact-manager.h>
40
41 #include "empathy-private-chat.h"
42 #include "empathy-chat-view.h"
43 #include "empathy-chat.h"
44 #include "empathy-preferences.h"
45 //#include "empathy-sound.h"
46 #include "empathy-images.h"
47 #include "empathy-ui-utils.h"
48
49 #define DEBUG_DOMAIN "PrivateChat"
50
51 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_PRIVATE_CHAT, EmpathyPrivateChatPriv))
52
53 struct _EmpathyPrivateChatPriv {   
54         EmpathyContact *contact;
55         gchar          *name;
56         gboolean        is_online;
57         GtkWidget      *widget;
58         GtkWidget      *text_view_sw;
59 };
60
61 static void           empathy_private_chat_class_init            (EmpathyPrivateChatClass *klass);
62 static void           empathy_private_chat_init                  (EmpathyPrivateChat      *chat);
63 static void           private_chat_finalize                     (GObject                *object);
64 static void           private_chat_create_ui                    (EmpathyPrivateChat      *chat);
65 static void           private_chat_contact_presence_updated_cb  (EmpathyContact          *contact,
66                                                                  GParamSpec             *param,
67                                                                  EmpathyPrivateChat      *chat);
68 static void           private_chat_contact_updated_cb           (EmpathyContact          *contact,
69                                                                  GParamSpec             *param,
70                                                                  EmpathyPrivateChat      *chat);
71 static void           private_chat_widget_destroy_cb            (GtkWidget              *widget,
72                                                                  EmpathyPrivateChat      *chat);
73 static const gchar *  private_chat_get_name                     (EmpathyChat             *chat);
74 static gchar *        private_chat_get_tooltip                  (EmpathyChat             *chat);
75 static const gchar *  private_chat_get_status_icon_name         (EmpathyChat             *chat);
76 static GtkWidget *    private_chat_get_widget                   (EmpathyChat             *chat);
77
78 G_DEFINE_TYPE (EmpathyPrivateChat, empathy_private_chat, EMPATHY_TYPE_CHAT);
79
80 static void
81 empathy_private_chat_class_init (EmpathyPrivateChatClass *klass)
82 {
83         GObjectClass    *object_class = G_OBJECT_CLASS (klass);
84         EmpathyChatClass *chat_class = EMPATHY_CHAT_CLASS (klass);
85
86         object_class->finalize = private_chat_finalize;
87
88         chat_class->get_name             = private_chat_get_name;
89         chat_class->get_tooltip          = private_chat_get_tooltip;
90         chat_class->get_status_icon_name = private_chat_get_status_icon_name;
91         chat_class->get_widget           = private_chat_get_widget;
92         chat_class->set_tp_chat          = NULL;
93
94         g_type_class_add_private (object_class, sizeof (EmpathyPrivateChatPriv));
95 }
96
97 static void
98 empathy_private_chat_init (EmpathyPrivateChat *chat)
99 {
100         EmpathyPrivateChatPriv *priv;
101
102         priv = GET_PRIV (chat);
103
104         priv->is_online = FALSE;
105
106         private_chat_create_ui (chat);
107 }
108
109 static void
110 private_chat_finalize (GObject *object)
111 {
112         EmpathyPrivateChat     *chat;
113         EmpathyPrivateChatPriv *priv;
114         
115         chat = EMPATHY_PRIVATE_CHAT (object);
116         priv = GET_PRIV (chat);
117
118         g_signal_handlers_disconnect_by_func (priv->contact,
119                                               private_chat_contact_updated_cb,
120                                               chat);
121         g_signal_handlers_disconnect_by_func (priv->contact,
122                                               private_chat_contact_presence_updated_cb,
123                                               chat);
124
125         if (priv->contact) {
126                 g_object_unref (priv->contact);
127         }
128
129         g_free (priv->name);
130
131         G_OBJECT_CLASS (empathy_private_chat_parent_class)->finalize (object);
132 }
133
134 static void
135 private_chat_create_ui (EmpathyPrivateChat *chat)
136 {
137         GladeXML              *glade;
138         EmpathyPrivateChatPriv *priv;
139         GtkWidget             *input_text_view_sw;
140
141         priv = GET_PRIV (chat);
142
143         glade = empathy_glade_get_file ("empathy-chat.glade",
144                                        "chat_widget",
145                                        NULL,
146                                       "chat_widget", &priv->widget,
147                                       "chat_view_sw", &priv->text_view_sw,
148                                       "input_text_view_sw", &input_text_view_sw,
149                                        NULL);
150
151         empathy_glade_connect (glade,
152                               chat,
153                               "chat_widget", "destroy", private_chat_widget_destroy_cb,
154                               NULL);
155
156         g_object_unref (glade);
157
158         g_object_set_data (G_OBJECT (priv->widget), "chat", g_object_ref (chat));
159
160         gtk_container_add (GTK_CONTAINER (priv->text_view_sw),
161                            GTK_WIDGET (EMPATHY_CHAT (chat)->view));
162         gtk_widget_show (GTK_WIDGET (EMPATHY_CHAT (chat)->view));
163
164         gtk_container_add (GTK_CONTAINER (input_text_view_sw),
165                            EMPATHY_CHAT (chat)->input_text_view);
166         gtk_widget_show (EMPATHY_CHAT (chat)->input_text_view);
167 }
168
169 static void
170 private_chat_contact_presence_updated_cb (EmpathyContact     *contact,
171                                           GParamSpec        *param,
172                                           EmpathyPrivateChat *chat)
173 {
174         EmpathyPrivateChatPriv *priv;
175
176         priv = GET_PRIV (chat);
177
178         empathy_debug (DEBUG_DOMAIN, "Presence update for contact: %s",
179                       empathy_contact_get_id (contact));
180
181         if (!empathy_contact_is_online (contact)) {
182                 if (priv->is_online) {
183                         gchar *msg;
184
185                         msg = g_strdup_printf (_("%s went offline"),
186                                                empathy_contact_get_name (priv->contact));
187                         empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, msg);
188                         g_free (msg);
189                 }
190
191                 priv->is_online = FALSE;
192
193                 g_signal_emit_by_name (chat, "composing", FALSE);
194
195         } else {
196                 if (!priv->is_online) {
197                         gchar *msg;
198
199                         msg = g_strdup_printf (_("%s has come online"),
200                                                empathy_contact_get_name (priv->contact));
201                         empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, msg);
202                         g_free (msg);
203                 }
204
205                 priv->is_online = TRUE;
206         }
207
208         g_signal_emit_by_name (chat, "status-changed");
209 }
210
211 static void
212 private_chat_contact_updated_cb (EmpathyContact     *contact,
213                                  GParamSpec        *param,
214                                  EmpathyPrivateChat *chat)
215 {
216         EmpathyPrivateChatPriv *priv;
217
218         priv = GET_PRIV (chat);
219
220         if (strcmp (priv->name, empathy_contact_get_name (contact)) != 0) {
221                 g_free (priv->name);
222                 priv->name = g_strdup (empathy_contact_get_name (contact));
223                 g_signal_emit_by_name (chat, "name-changed", priv->name);
224         }
225 }
226
227 static void
228 private_chat_widget_destroy_cb (GtkWidget         *widget,
229                                 EmpathyPrivateChat *chat)
230 {
231         empathy_debug (DEBUG_DOMAIN, "Destroyed");
232
233         g_object_unref (chat);
234 }
235
236 static const gchar *
237 private_chat_get_name (EmpathyChat *chat)
238 {
239         EmpathyPrivateChatPriv *priv;
240
241         g_return_val_if_fail (EMPATHY_IS_PRIVATE_CHAT (chat), NULL);
242
243         priv = GET_PRIV (chat);
244
245         return priv->name;
246 }
247
248 static gchar *
249 private_chat_get_tooltip (EmpathyChat *chat)
250 {
251         EmpathyPrivateChatPriv *priv;
252         const gchar           *status;
253
254         g_return_val_if_fail (EMPATHY_IS_PRIVATE_CHAT (chat), NULL);
255
256         priv = GET_PRIV (chat);
257
258         status = empathy_contact_get_status (priv->contact);
259
260         return g_strdup_printf ("%s\n%s",
261                                 empathy_contact_get_id (priv->contact),
262                                 status);
263 }
264
265 static const gchar *
266 private_chat_get_status_icon_name (EmpathyChat *chat)
267 {
268         EmpathyPrivateChatPriv *priv;
269
270         g_return_val_if_fail (EMPATHY_IS_PRIVATE_CHAT (chat), NULL);
271
272         priv = GET_PRIV (chat);
273
274         return empathy_icon_name_for_contact (priv->contact);
275 }
276
277 EmpathyContact *
278 empathy_private_chat_get_contact (EmpathyPrivateChat *chat)
279 {
280         EmpathyPrivateChatPriv *priv;
281
282         g_return_val_if_fail (EMPATHY_IS_PRIVATE_CHAT (chat), NULL);
283
284         priv = GET_PRIV (chat);
285
286         return priv->contact;
287 }
288
289 static GtkWidget *
290 private_chat_get_widget (EmpathyChat *chat)
291 {
292         EmpathyPrivateChatPriv *priv;
293
294         priv = GET_PRIV (chat);
295
296         return priv->widget;
297 }
298
299 static void
300 private_chat_setup (EmpathyPrivateChat *chat,
301                     EmpathyContact     *contact,
302                     EmpathyTpChat     *tp_chat)
303 {
304         EmpathyPrivateChatPriv *priv;
305
306         priv = GET_PRIV (chat);
307
308         EMPATHY_CHAT (chat)->account = g_object_ref (empathy_contact_get_account (contact));
309         priv->contact = g_object_ref (contact);
310         priv->name = g_strdup (empathy_contact_get_name (contact));
311
312         empathy_chat_set_tp_chat (EMPATHY_CHAT (chat), tp_chat);
313
314         g_signal_connect (priv->contact, 
315                           "notify::name",
316                           G_CALLBACK (private_chat_contact_updated_cb),
317                           chat);
318         g_signal_connect (priv->contact, 
319                           "notify::presence",
320                           G_CALLBACK (private_chat_contact_presence_updated_cb),
321                           chat);
322
323         priv->is_online = empathy_contact_is_online (priv->contact);
324 }
325
326 EmpathyPrivateChat *
327 empathy_private_chat_new (McAccount *account,
328                           TpChan    *tp_chan)
329 {
330         EmpathyPrivateChat    *chat;
331         EmpathyTpChat         *tp_chat;
332         EmpathyContactManager *manager;
333         EmpathyTpContactList  *list;
334         EmpathyContact        *contact;
335
336         g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
337         g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
338
339         manager = empathy_contact_manager_new ();
340         list = empathy_contact_manager_get_list (manager, account);
341         contact = empathy_tp_contact_list_get_from_handle (list, tp_chan->handle);
342
343         chat = g_object_new (EMPATHY_TYPE_PRIVATE_CHAT, NULL);
344         tp_chat = empathy_tp_chat_new (account, tp_chan);
345
346         private_chat_setup (chat, contact, tp_chat);
347
348         g_object_unref (tp_chat);
349         g_object_unref (contact);
350         g_object_unref (manager);
351
352         return chat;
353 }
354
355 EmpathyPrivateChat *
356 empathy_private_chat_new_with_contact (EmpathyContact *contact)
357 {
358         EmpathyPrivateChat *chat;
359         EmpathyTpChat      *tp_chat;
360
361         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
362
363         chat = g_object_new (EMPATHY_TYPE_PRIVATE_CHAT, NULL);
364         tp_chat = empathy_tp_chat_new_with_contact (contact);
365
366         private_chat_setup (chat, contact, tp_chat);
367         g_object_unref (tp_chat);
368
369         return chat;
370 }
371