]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-private-chat.c
Make sure we don't expect contact id and handle to be directly ready. Add some _run_u...
[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
38 #include <libempathy/empathy-debug.h>
39 #include <libempathy/empathy-tp-chat.h>
40 #include <libempathy/empathy-tp-contact-list.h>
41 #include <libempathy/empathy-contact-factory.h>
42 #include <libempathy/empathy-utils.h>
43
44 #include "empathy-private-chat.h"
45 #include "empathy-chat-view.h"
46 #include "empathy-chat.h"
47 #include "empathy-preferences.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         empathy_contact_run_until_ready (priv->contact,
109                                          EMPATHY_CONTACT_READY_ID |
110                                          EMPATHY_CONTACT_READY_NAME,
111                                          NULL);
112
113         priv->name = g_strdup (empathy_contact_get_name (priv->contact));
114
115         g_signal_connect (priv->contact, 
116                           "notify::name",
117                           G_CALLBACK (private_chat_contact_updated_cb),
118                           chat);
119         g_signal_connect (priv->contact, 
120                           "notify::presence",
121                           G_CALLBACK (private_chat_contact_presence_updated_cb),
122                           chat);
123
124         priv->is_online = empathy_contact_is_online (priv->contact);
125
126         g_object_unref (tp_chat);
127
128         return chat;
129 }
130
131 static void
132 empathy_private_chat_class_init (EmpathyPrivateChatClass *klass)
133 {
134         GObjectClass    *object_class = G_OBJECT_CLASS (klass);
135         EmpathyChatClass *chat_class = EMPATHY_CHAT_CLASS (klass);
136
137         object_class->finalize = private_chat_finalize;
138         object_class->constructor = private_chat_constructor;
139
140         chat_class->get_name             = private_chat_get_name;
141         chat_class->get_tooltip          = private_chat_get_tooltip;
142         chat_class->get_status_icon_name = private_chat_get_status_icon_name;
143         chat_class->get_widget           = private_chat_get_widget;
144         chat_class->set_tp_chat          = NULL;
145
146         g_type_class_add_private (object_class, sizeof (EmpathyPrivateChatPriv));
147 }
148
149 static void
150 empathy_private_chat_init (EmpathyPrivateChat *chat)
151 {
152         private_chat_create_ui (chat);
153 }
154
155 static void
156 private_chat_finalize (GObject *object)
157 {
158         EmpathyPrivateChat     *chat;
159         EmpathyPrivateChatPriv *priv;
160         
161         chat = EMPATHY_PRIVATE_CHAT (object);
162         priv = GET_PRIV (chat);
163
164         g_signal_handlers_disconnect_by_func (priv->contact,
165                                               private_chat_contact_updated_cb,
166                                               chat);
167         g_signal_handlers_disconnect_by_func (priv->contact,
168                                               private_chat_contact_presence_updated_cb,
169                                               chat);
170
171         if (priv->contact) {
172                 g_object_unref (priv->contact);
173         }
174         if (priv->factory) {
175                 g_object_unref (priv->factory);
176         }
177         g_free (priv->name);
178
179         G_OBJECT_CLASS (empathy_private_chat_parent_class)->finalize (object);
180 }
181
182 static void
183 private_chat_create_ui (EmpathyPrivateChat *chat)
184 {
185         GladeXML              *glade;
186         EmpathyPrivateChatPriv *priv;
187         GtkWidget             *input_text_view_sw;
188
189         priv = GET_PRIV (chat);
190
191         glade = empathy_glade_get_file ("empathy-chat.glade",
192                                        "chat_widget",
193                                        NULL,
194                                       "chat_widget", &priv->widget,
195                                       "chat_view_sw", &priv->text_view_sw,
196                                       "input_text_view_sw", &input_text_view_sw,
197                                        NULL);
198
199         empathy_glade_connect (glade,
200                               chat,
201                               "chat_widget", "destroy", private_chat_widget_destroy_cb,
202                               NULL);
203
204         g_object_unref (glade);
205
206         g_object_set_data (G_OBJECT (priv->widget), "chat", g_object_ref (chat));
207
208         gtk_container_add (GTK_CONTAINER (priv->text_view_sw),
209                            GTK_WIDGET (EMPATHY_CHAT (chat)->view));
210         gtk_widget_show (GTK_WIDGET (EMPATHY_CHAT (chat)->view));
211
212         gtk_container_add (GTK_CONTAINER (input_text_view_sw),
213                            EMPATHY_CHAT (chat)->input_text_view);
214         gtk_widget_show (EMPATHY_CHAT (chat)->input_text_view);
215 }
216
217 static void
218 private_chat_contact_presence_updated_cb (EmpathyContact     *contact,
219                                           GParamSpec         *param,
220                                           EmpathyPrivateChat *chat)
221 {
222         EmpathyPrivateChatPriv *priv;
223
224         priv = GET_PRIV (chat);
225
226         empathy_debug (DEBUG_DOMAIN, "Presence update for contact: %s",
227                       empathy_contact_get_id (contact));
228
229         if (!empathy_contact_is_online (contact)) {
230                 if (priv->is_online && !EMPATHY_CHAT (chat)->block_events) {
231                         gchar *msg;
232
233                         msg = g_strdup_printf (_("%s went offline"),
234                                                empathy_contact_get_name (priv->contact));
235                         empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, msg);
236                         g_free (msg);
237                 }
238
239                 priv->is_online = FALSE;
240
241                 g_signal_emit_by_name (chat, "composing", FALSE);
242
243         } else {
244                 if (!priv->is_online && !EMPATHY_CHAT (chat)->block_events) {
245                         gchar *msg;
246
247                         msg = g_strdup_printf (_("%s has come online"),
248                                                empathy_contact_get_name (priv->contact));
249                         empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, msg);
250                         g_free (msg);
251                 }
252
253                 priv->is_online = TRUE;
254
255                 /* If offline message is not supported by CM we need to
256                  * request a new Text Channel. */
257                 if (!empathy_chat_is_connected (EMPATHY_CHAT (chat))) {
258                         empathy_chat_with_contact (contact);
259                 }
260         }
261
262         g_signal_emit_by_name (chat, "status-changed");
263 }
264
265 static void
266 private_chat_contact_updated_cb (EmpathyContact     *contact,
267                                  GParamSpec        *param,
268                                  EmpathyPrivateChat *chat)
269 {
270         EmpathyPrivateChatPriv *priv;
271
272         priv = GET_PRIV (chat);
273
274         if (strcmp (priv->name, empathy_contact_get_name (contact)) != 0) {
275                 g_free (priv->name);
276                 priv->name = g_strdup (empathy_contact_get_name (contact));
277                 g_signal_emit_by_name (chat, "name-changed", priv->name);
278         }
279 }
280
281 static void
282 private_chat_widget_destroy_cb (GtkWidget         *widget,
283                                 EmpathyPrivateChat *chat)
284 {
285         empathy_debug (DEBUG_DOMAIN, "Destroyed");
286
287         g_object_unref (chat);
288 }
289
290 static const gchar *
291 private_chat_get_name (EmpathyChat *chat)
292 {
293         EmpathyPrivateChatPriv *priv;
294
295         g_return_val_if_fail (EMPATHY_IS_PRIVATE_CHAT (chat), NULL);
296
297         priv = GET_PRIV (chat);
298
299         return priv->name;
300 }
301
302 static gchar *
303 private_chat_get_tooltip (EmpathyChat *chat)
304 {
305         EmpathyPrivateChatPriv *priv;
306         const gchar           *status;
307
308         g_return_val_if_fail (EMPATHY_IS_PRIVATE_CHAT (chat), NULL);
309
310         priv = GET_PRIV (chat);
311
312         status = empathy_contact_get_status (priv->contact);
313
314         return g_strdup_printf ("%s\n%s",
315                                 empathy_contact_get_id (priv->contact),
316                                 status);
317 }
318
319 static const gchar *
320 private_chat_get_status_icon_name (EmpathyChat *chat)
321 {
322         EmpathyPrivateChatPriv *priv;
323
324         g_return_val_if_fail (EMPATHY_IS_PRIVATE_CHAT (chat), NULL);
325
326         priv = GET_PRIV (chat);
327
328         return empathy_icon_name_for_contact (priv->contact);
329 }
330
331 EmpathyContact *
332 empathy_private_chat_get_contact (EmpathyPrivateChat *chat)
333 {
334         EmpathyPrivateChatPriv *priv;
335
336         g_return_val_if_fail (EMPATHY_IS_PRIVATE_CHAT (chat), NULL);
337
338         priv = GET_PRIV (chat);
339
340         return priv->contact;
341 }
342
343 static GtkWidget *
344 private_chat_get_widget (EmpathyChat *chat)
345 {
346         EmpathyPrivateChatPriv *priv;
347
348         priv = GET_PRIV (chat);
349
350         return priv->widget;
351 }
352
353 EmpathyPrivateChat *
354 empathy_private_chat_new (EmpathyTpChat *tp_chat)
355 {
356         EmpathyPrivateChat *chat;
357
358         g_return_val_if_fail (EMPATHY_IS_TP_CHAT (tp_chat), NULL);
359
360         chat = g_object_new (EMPATHY_TYPE_PRIVATE_CHAT,
361                              "tp-chat", tp_chat,
362                              NULL);
363
364         return chat;
365 }
366