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