]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-private-chat.c
Add a gconf key to define the char to be added for tab completion. Fixes
[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 <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 static void
85 empathy_private_chat_class_init (EmpathyPrivateChatClass *klass)
86 {
87         GObjectClass    *object_class = G_OBJECT_CLASS (klass);
88         EmpathyChatClass *chat_class = EMPATHY_CHAT_CLASS (klass);
89
90         object_class->finalize = private_chat_finalize;
91
92         chat_class->get_name             = private_chat_get_name;
93         chat_class->get_tooltip          = private_chat_get_tooltip;
94         chat_class->get_status_icon_name = private_chat_get_status_icon_name;
95         chat_class->get_widget           = private_chat_get_widget;
96         chat_class->set_tp_chat          = NULL;
97
98         g_type_class_add_private (object_class, sizeof (EmpathyPrivateChatPriv));
99 }
100
101 static void
102 empathy_private_chat_init (EmpathyPrivateChat *chat)
103 {
104         EmpathyPrivateChatPriv *priv;
105
106         priv = GET_PRIV (chat);
107
108         priv->is_online = FALSE;
109
110         private_chat_create_ui (chat);
111 }
112
113 static void
114 private_chat_finalize (GObject *object)
115 {
116         EmpathyPrivateChat     *chat;
117         EmpathyPrivateChatPriv *priv;
118         
119         chat = EMPATHY_PRIVATE_CHAT (object);
120         priv = GET_PRIV (chat);
121
122         g_signal_handlers_disconnect_by_func (priv->contact,
123                                               private_chat_contact_updated_cb,
124                                               chat);
125         g_signal_handlers_disconnect_by_func (priv->contact,
126                                               private_chat_contact_presence_updated_cb,
127                                               chat);
128
129         if (priv->contact) {
130                 g_object_unref (priv->contact);
131         }
132         if (priv->factory) {
133                 g_object_unref (priv->factory);
134         }
135         g_free (priv->name);
136
137         G_OBJECT_CLASS (empathy_private_chat_parent_class)->finalize (object);
138 }
139
140 static void
141 private_chat_create_ui (EmpathyPrivateChat *chat)
142 {
143         GladeXML              *glade;
144         EmpathyPrivateChatPriv *priv;
145         GtkWidget             *input_text_view_sw;
146
147         priv = GET_PRIV (chat);
148
149         glade = empathy_glade_get_file ("empathy-chat.glade",
150                                        "chat_widget",
151                                        NULL,
152                                       "chat_widget", &priv->widget,
153                                       "chat_view_sw", &priv->text_view_sw,
154                                       "input_text_view_sw", &input_text_view_sw,
155                                        NULL);
156
157         empathy_glade_connect (glade,
158                               chat,
159                               "chat_widget", "destroy", private_chat_widget_destroy_cb,
160                               NULL);
161
162         g_object_unref (glade);
163
164         g_object_set_data (G_OBJECT (priv->widget), "chat", g_object_ref (chat));
165
166         gtk_container_add (GTK_CONTAINER (priv->text_view_sw),
167                            GTK_WIDGET (EMPATHY_CHAT (chat)->view));
168         gtk_widget_show (GTK_WIDGET (EMPATHY_CHAT (chat)->view));
169
170         gtk_container_add (GTK_CONTAINER (input_text_view_sw),
171                            EMPATHY_CHAT (chat)->input_text_view);
172         gtk_widget_show (EMPATHY_CHAT (chat)->input_text_view);
173 }
174
175 static void
176 private_chat_contact_presence_updated_cb (EmpathyContact     *contact,
177                                           GParamSpec         *param,
178                                           EmpathyPrivateChat *chat)
179 {
180         EmpathyPrivateChatPriv *priv;
181
182         priv = GET_PRIV (chat);
183
184         empathy_debug (DEBUG_DOMAIN, "Presence update for contact: %s",
185                       empathy_contact_get_id (contact));
186
187         if (!empathy_contact_is_online (contact)) {
188                 if (priv->is_online) {
189                         gchar *msg;
190
191                         msg = g_strdup_printf (_("%s went offline"),
192                                                empathy_contact_get_name (priv->contact));
193                         empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, msg);
194                         g_free (msg);
195                 }
196
197                 priv->is_online = FALSE;
198
199                 g_signal_emit_by_name (chat, "composing", FALSE);
200
201         } else {
202                 if (!priv->is_online) {
203                         gchar *msg;
204
205                         msg = g_strdup_printf (_("%s has come online"),
206                                                empathy_contact_get_name (priv->contact));
207                         empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, msg);
208                         g_free (msg);
209                 }
210
211                 priv->is_online = TRUE;
212
213                 /* If offline message is not supported by CM we need to
214                  * request a new Text Channel. */
215                 if (!empathy_chat_is_connected (EMPATHY_CHAT (chat))) {
216                         MissionControl *mc;
217
218                         mc = empathy_mission_control_new ();
219                         mission_control_request_channel (mc,
220                                                          empathy_contact_get_account (contact),
221                                                          TP_IFACE_CHANNEL_TYPE_TEXT,
222                                                          empathy_contact_get_handle (contact),
223                                                          TP_HANDLE_TYPE_CONTACT,
224                                                          NULL, NULL);
225                         g_object_unref (mc);
226                 }
227         }
228
229         g_signal_emit_by_name (chat, "status-changed");
230 }
231
232 static void
233 private_chat_contact_updated_cb (EmpathyContact     *contact,
234                                  GParamSpec        *param,
235                                  EmpathyPrivateChat *chat)
236 {
237         EmpathyPrivateChatPriv *priv;
238
239         priv = GET_PRIV (chat);
240
241         if (strcmp (priv->name, empathy_contact_get_name (contact)) != 0) {
242                 g_free (priv->name);
243                 priv->name = g_strdup (empathy_contact_get_name (contact));
244                 g_signal_emit_by_name (chat, "name-changed", priv->name);
245         }
246 }
247
248 static void
249 private_chat_widget_destroy_cb (GtkWidget         *widget,
250                                 EmpathyPrivateChat *chat)
251 {
252         empathy_debug (DEBUG_DOMAIN, "Destroyed");
253
254         g_object_unref (chat);
255 }
256
257 static const gchar *
258 private_chat_get_name (EmpathyChat *chat)
259 {
260         EmpathyPrivateChatPriv *priv;
261
262         g_return_val_if_fail (EMPATHY_IS_PRIVATE_CHAT (chat), NULL);
263
264         priv = GET_PRIV (chat);
265
266         return priv->name;
267 }
268
269 static gchar *
270 private_chat_get_tooltip (EmpathyChat *chat)
271 {
272         EmpathyPrivateChatPriv *priv;
273         const gchar           *status;
274
275         g_return_val_if_fail (EMPATHY_IS_PRIVATE_CHAT (chat), NULL);
276
277         priv = GET_PRIV (chat);
278
279         status = empathy_contact_get_status (priv->contact);
280
281         return g_strdup_printf ("%s\n%s",
282                                 empathy_contact_get_id (priv->contact),
283                                 status);
284 }
285
286 static const gchar *
287 private_chat_get_status_icon_name (EmpathyChat *chat)
288 {
289         EmpathyPrivateChatPriv *priv;
290
291         g_return_val_if_fail (EMPATHY_IS_PRIVATE_CHAT (chat), NULL);
292
293         priv = GET_PRIV (chat);
294
295         return empathy_icon_name_for_contact (priv->contact);
296 }
297
298 EmpathyContact *
299 empathy_private_chat_get_contact (EmpathyPrivateChat *chat)
300 {
301         EmpathyPrivateChatPriv *priv;
302
303         g_return_val_if_fail (EMPATHY_IS_PRIVATE_CHAT (chat), NULL);
304
305         priv = GET_PRIV (chat);
306
307         return priv->contact;
308 }
309
310 static GtkWidget *
311 private_chat_get_widget (EmpathyChat *chat)
312 {
313         EmpathyPrivateChatPriv *priv;
314
315         priv = GET_PRIV (chat);
316
317         return priv->widget;
318 }
319
320 static void
321 private_chat_setup (EmpathyPrivateChat *chat,
322                     EmpathyContact     *contact,
323                     EmpathyTpChat     *tp_chat)
324 {
325         EmpathyPrivateChatPriv *priv;
326
327         priv = GET_PRIV (chat);
328
329         EMPATHY_CHAT (chat)->account = g_object_ref (empathy_contact_get_account (contact));
330         priv->contact = g_object_ref (contact);
331         priv->name = g_strdup (empathy_contact_get_name (contact));
332
333         empathy_chat_set_tp_chat (EMPATHY_CHAT (chat), tp_chat);
334
335         g_signal_connect (priv->contact, 
336                           "notify::name",
337                           G_CALLBACK (private_chat_contact_updated_cb),
338                           chat);
339         g_signal_connect (priv->contact, 
340                           "notify::presence",
341                           G_CALLBACK (private_chat_contact_presence_updated_cb),
342                           chat);
343
344         priv->is_online = empathy_contact_is_online (priv->contact);
345 }
346
347 EmpathyPrivateChat *
348 empathy_private_chat_new (McAccount *account,
349                           TpChan    *tp_chan)
350 {
351         EmpathyPrivateChat     *chat;
352         EmpathyPrivateChatPriv *priv;
353         EmpathyTpChat          *tp_chat;
354         EmpathyContact         *contact;
355
356         g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
357         g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
358
359         chat = g_object_new (EMPATHY_TYPE_PRIVATE_CHAT, NULL);
360         priv = GET_PRIV (chat);
361
362         priv->factory = empathy_contact_factory_new ();
363         contact = empathy_contact_factory_get_from_handle (priv->factory,
364                                                            account,
365                                                            tp_chan->handle);
366
367         tp_chat = empathy_tp_chat_new (account, tp_chan);
368         private_chat_setup (chat, contact, tp_chat);
369
370         g_object_unref (tp_chat);
371         g_object_unref (contact);
372
373         return chat;
374 }
375
376 EmpathyPrivateChat *
377 empathy_private_chat_new_with_contact (EmpathyContact *contact)
378 {
379         EmpathyPrivateChat     *chat;
380         EmpathyPrivateChatPriv *priv;
381         EmpathyTpChat          *tp_chat;
382
383         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
384
385         chat = g_object_new (EMPATHY_TYPE_PRIVATE_CHAT, NULL);
386
387         priv = GET_PRIV (chat);
388         priv->factory = empathy_contact_factory_new ();
389
390         tp_chat = empathy_tp_chat_new_with_contact (contact);
391         private_chat_setup (chat, contact, tp_chat);
392         g_object_unref (tp_chat);
393
394         return chat;
395 }
396