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