]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-chat.c
hindi translation committed by Rajesh Ranjan
[empathy.git] / libempathy-gtk / empathy-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 #include <stdlib.h>
32
33 #include <gdk/gdkkeysyms.h>
34 #include <glib/gi18n.h>
35 #include <gtk/gtk.h>
36
37 #include <libmissioncontrol/mission-control.h>
38 #include <telepathy-glib/util.h>
39
40 #include <libempathy/empathy-log-manager.h>
41 #include <libempathy/empathy-contact-list.h>
42 #include <libempathy/empathy-utils.h>
43
44 #include "empathy-chat.h"
45 #include "empathy-conf.h"
46 #include "empathy-spell.h"
47 #include "empathy-spell-dialog.h"
48 #include "empathy-contact-list-store.h"
49 #include "empathy-contact-list-view.h"
50 #include "empathy-contact-menu.h"
51 #include "empathy-ui-utils.h"
52
53 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
54 #include <libempathy/empathy-debug.h>
55
56 #define CHAT_DIR_CREATE_MODE  (S_IRUSR | S_IWUSR | S_IXUSR)
57 #define CHAT_FILE_CREATE_MODE (S_IRUSR | S_IWUSR)
58 #define IS_ENTER(v) (v == GDK_Return || v == GDK_ISO_Enter || v == GDK_KP_Enter)
59 #define MAX_INPUT_HEIGHT 150
60 #define COMPOSING_STOP_TIMEOUT 5
61
62 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChat)
63 typedef struct {
64         EmpathyTpChat     *tp_chat;
65         McAccount         *account;
66         gchar             *id;
67         gchar             *name;
68         gchar             *subject;
69         EmpathyContact    *remote_contact;
70
71         EmpathyLogManager *log_manager;
72         MissionControl    *mc;
73         GSList            *sent_messages;
74         gint               sent_messages_index;
75         GList             *compositors;
76         GCompletion       *completion;
77         guint              composing_stop_timeout_id;
78         guint              block_events_timeout_id;
79         TpHandleType       handle_type;
80         gpointer           token;
81         gint               contacts_width;
82         gboolean           has_input_vscroll;
83
84         GtkWidget         *widget;
85         GtkWidget         *hpaned;
86         GtkWidget         *vbox_left;
87         GtkWidget         *scrolled_window_chat;
88         GtkWidget         *scrolled_window_input;
89         GtkWidget         *scrolled_window_contacts;
90         GtkWidget         *hbox_topic;
91         GtkWidget         *label_topic;
92         GtkWidget         *contact_list_view;
93 } EmpathyChatPriv;
94
95 enum {
96         COMPOSING,
97         NEW_MESSAGE,
98         LAST_SIGNAL
99 };
100
101 enum {
102         PROP_0,
103         PROP_TP_CHAT,
104         PROP_ACCOUNT,
105         PROP_ID,
106         PROP_NAME,
107         PROP_SUBJECT,
108         PROP_REMOTE_CONTACT,
109 };
110
111 static guint signals[LAST_SIGNAL] = { 0 };
112
113 G_DEFINE_TYPE (EmpathyChat, empathy_chat, GTK_TYPE_BIN);
114
115 static void
116 chat_get_property (GObject    *object,
117                    guint       param_id,
118                    GValue     *value,
119                    GParamSpec *pspec)
120 {
121         EmpathyChatPriv *priv = GET_PRIV (object);
122
123         switch (param_id) {
124         case PROP_TP_CHAT:
125                 g_value_set_object (value, priv->tp_chat);
126                 break;
127         case PROP_ACCOUNT:
128                 g_value_set_object (value, priv->account);
129                 break;
130         case PROP_NAME:
131                 g_value_set_string (value, priv->name);
132                 break;
133         case PROP_ID:
134                 g_value_set_string (value, priv->id);
135                 break;
136         case PROP_SUBJECT:
137                 g_value_set_string (value, priv->subject);
138                 break;
139         case PROP_REMOTE_CONTACT:
140                 g_value_set_object (value, priv->remote_contact);
141                 break;
142         default:
143                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
144                 break;
145         };
146 }
147
148 static void
149 chat_set_property (GObject      *object,
150                    guint         param_id,
151                    const GValue *value,
152                    GParamSpec   *pspec)
153 {
154         EmpathyChat *chat = EMPATHY_CHAT (object);
155
156         switch (param_id) {
157         case PROP_TP_CHAT:
158                 empathy_chat_set_tp_chat (chat, EMPATHY_TP_CHAT (g_value_get_object (value)));
159                 break;
160         default:
161                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
162                 break;
163         };
164 }
165
166 static void
167 chat_status_changed_cb (MissionControl           *mc,
168                         TpConnectionStatus        status,
169                         McPresence                presence,
170                         TpConnectionStatusReason  reason,
171                         const gchar              *unique_name,
172                         EmpathyChat              *chat)
173 {
174         EmpathyChatPriv *priv = GET_PRIV (chat);
175         McAccount       *account;
176
177         account = mc_account_lookup (unique_name);
178
179         if (status == TP_CONNECTION_STATUS_CONNECTED && !priv->tp_chat &&
180             empathy_account_equal (account, priv->account) &&
181             priv->handle_type != TP_HANDLE_TYPE_NONE) {
182                 TpConnection *connection;
183
184                 DEBUG ("Account reconnected, request a new Text channel");
185                 connection = mission_control_get_tpconnection (mc, account, NULL);
186                 tp_connection_run_until_ready (connection, FALSE, NULL, NULL);
187                 empathy_connection_request_channel (connection, -1,
188                                                     TP_IFACE_CHANNEL_TYPE_TEXT,
189                                                     priv->handle_type,
190                                                     priv->id, TRUE,
191                                                     NULL, NULL, NULL, NULL);
192                 g_object_unref (connection);
193         }
194
195         g_object_unref (account);
196 }
197
198 static void
199 chat_composing_remove_timeout (EmpathyChat *chat)
200 {
201         EmpathyChatPriv *priv;
202
203         priv = GET_PRIV (chat);
204
205         if (priv->composing_stop_timeout_id) {
206                 g_source_remove (priv->composing_stop_timeout_id);
207                 priv->composing_stop_timeout_id = 0;
208         }
209 }
210
211 static gboolean
212 chat_composing_stop_timeout_cb (EmpathyChat *chat)
213 {
214         EmpathyChatPriv *priv;
215
216         priv = GET_PRIV (chat);
217
218         priv->composing_stop_timeout_id = 0;
219         empathy_tp_chat_set_state (priv->tp_chat,
220                                    TP_CHANNEL_CHAT_STATE_PAUSED);
221
222         return FALSE;
223 }
224
225 static void
226 chat_composing_start (EmpathyChat *chat)
227 {
228         EmpathyChatPriv *priv;
229
230         priv = GET_PRIV (chat);
231
232         if (priv->composing_stop_timeout_id) {
233                 /* Just restart the timeout */
234                 chat_composing_remove_timeout (chat);
235         } else {
236                 empathy_tp_chat_set_state (priv->tp_chat,
237                                            TP_CHANNEL_CHAT_STATE_COMPOSING);
238         }
239
240         priv->composing_stop_timeout_id = g_timeout_add_seconds (
241                 COMPOSING_STOP_TIMEOUT,
242                 (GSourceFunc) chat_composing_stop_timeout_cb,
243                 chat);
244 }
245
246 static void
247 chat_composing_stop (EmpathyChat *chat)
248 {
249         EmpathyChatPriv *priv;
250
251         priv = GET_PRIV (chat);
252
253         chat_composing_remove_timeout (chat);
254         empathy_tp_chat_set_state (priv->tp_chat,
255                                    TP_CHANNEL_CHAT_STATE_ACTIVE);
256 }
257
258 static void 
259 chat_sent_message_add (EmpathyChat  *chat,
260                        const gchar *str)
261 {
262         EmpathyChatPriv *priv;
263         GSList         *list;
264         GSList         *item;
265
266         priv = GET_PRIV (chat);
267
268         /* Save the sent message in our repeat buffer */
269         list = priv->sent_messages;
270         
271         /* Remove any other occurances of this msg */
272         while ((item = g_slist_find_custom (list, str, (GCompareFunc) strcmp)) != NULL) {
273                 list = g_slist_remove_link (list, item);
274                 g_free (item->data);
275                 g_slist_free1 (item);
276         }
277
278         /* Trim the list to the last 10 items */
279         while (g_slist_length (list) > 10) {
280                 item = g_slist_last (list);
281                 if (item) {
282                         list = g_slist_remove_link (list, item);
283                         g_free (item->data);
284                         g_slist_free1 (item);
285                 }
286         }
287
288         /* Add new message */
289         list = g_slist_prepend (list, g_strdup (str));
290
291         /* Set list and reset the index */
292         priv->sent_messages = list;
293         priv->sent_messages_index = -1;
294 }
295
296 static const gchar *
297 chat_sent_message_get_next (EmpathyChat *chat)
298 {
299         EmpathyChatPriv *priv;
300         gint            max;
301         
302         priv = GET_PRIV (chat);
303
304         if (!priv->sent_messages) {
305                 DEBUG ("No sent messages, next message is NULL");
306                 return NULL;
307         }
308
309         max = g_slist_length (priv->sent_messages) - 1;
310
311         if (priv->sent_messages_index < max) {
312                 priv->sent_messages_index++;
313         }
314         
315         DEBUG ("Returning next message index:%d", priv->sent_messages_index);
316
317         return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
318 }
319
320 static const gchar *
321 chat_sent_message_get_last (EmpathyChat *chat)
322 {
323         EmpathyChatPriv *priv;
324
325         g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
326
327         priv = GET_PRIV (chat);
328         
329         if (!priv->sent_messages) {
330                 DEBUG ("No sent messages, last message is NULL");
331                 return NULL;
332         }
333
334         if (priv->sent_messages_index >= 0) {
335                 priv->sent_messages_index--;
336         }
337
338         DEBUG ("Returning last message index:%d", priv->sent_messages_index);
339
340         return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
341 }
342
343 static void
344 chat_send (EmpathyChat  *chat,
345            const gchar *msg)
346 {
347         EmpathyChatPriv *priv;
348         EmpathyMessage  *message;
349
350         priv = GET_PRIV (chat);
351
352         if (G_STR_EMPTY (msg)) {
353                 return;
354         }
355
356         chat_sent_message_add (chat, msg);
357
358         if (g_str_has_prefix (msg, "/clear")) {
359                 empathy_chat_view_clear (chat->view);
360                 return;
361         }
362
363         message = empathy_message_new (msg);
364
365         empathy_tp_chat_send (priv->tp_chat, message);
366
367         g_object_unref (message);
368 }
369
370 static void
371 chat_input_text_view_send (EmpathyChat *chat)
372 {
373         EmpathyChatPriv *priv;
374         GtkTextBuffer  *buffer;
375         GtkTextIter     start, end;
376         gchar          *msg;
377
378         priv = GET_PRIV (chat);
379
380         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
381
382         gtk_text_buffer_get_bounds (buffer, &start, &end);
383         msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
384
385         /* clear the input field */
386         gtk_text_buffer_set_text (buffer, "", -1);
387
388         chat_send (chat, msg);
389         g_free (msg);
390 }
391
392 static void
393 chat_state_changed_cb (EmpathyTpChat      *tp_chat,
394                        EmpathyContact     *contact,
395                        TpChannelChatState  state,
396                        EmpathyChat        *chat)
397 {
398         EmpathyChatPriv *priv;
399         GList          *l;
400         gboolean        was_composing;
401
402         priv = GET_PRIV (chat);
403
404         if (empathy_contact_is_user (contact)) {
405                 /* We don't care about our own chat state */
406                 return;
407         }
408
409         was_composing = (priv->compositors != NULL);
410
411         /* Find the contact in the list. After that l is the list elem or NULL */
412         for (l = priv->compositors; l; l = l->next) {
413                 if (contact == l->data) {
414                         break;
415                 }
416         }
417
418         switch (state) {
419         case TP_CHANNEL_CHAT_STATE_GONE:
420         case TP_CHANNEL_CHAT_STATE_INACTIVE:
421         case TP_CHANNEL_CHAT_STATE_PAUSED:
422         case TP_CHANNEL_CHAT_STATE_ACTIVE:
423                 /* Contact is not composing */
424                 if (l) {
425                         priv->compositors = g_list_remove_link (priv->compositors, l);
426                         g_object_unref (l->data);
427                         g_list_free1 (l);
428                 }
429                 break;
430         case TP_CHANNEL_CHAT_STATE_COMPOSING:
431                 /* Contact is composing */
432                 if (!l) {
433                         priv->compositors = g_list_prepend (priv->compositors,
434                                                             g_object_ref (contact));
435                 }
436                 break;
437         default:
438                 g_assert_not_reached ();
439         }
440
441         DEBUG ("Was composing: %s now composing: %s",
442                 was_composing ? "yes" : "no",
443                 priv->compositors ? "yes" : "no");
444
445         if ((was_composing && !priv->compositors) ||
446             (!was_composing && priv->compositors)) {
447                 /* Composing state changed */
448                 g_signal_emit (chat, signals[COMPOSING], 0,
449                                priv->compositors != NULL);
450         }
451 }
452
453 static void
454 chat_message_received_cb (EmpathyTpChat  *tp_chat,
455                           EmpathyMessage *message,
456                           EmpathyChat    *chat)
457 {
458         EmpathyChatPriv *priv = GET_PRIV (chat);
459         EmpathyContact  *sender;
460
461         sender = empathy_message_get_sender (message);
462
463         DEBUG ("Appending new message from %s (%d)",
464                 empathy_contact_get_name (sender),
465                 empathy_contact_get_handle (sender));
466
467         if (priv->id) {
468                 gboolean is_chatroom;
469
470                 is_chatroom = priv->handle_type == TP_HANDLE_TYPE_ROOM;
471                 empathy_log_manager_add_message (priv->log_manager,
472                                                  priv->id, is_chatroom,
473                                                  message);
474         }
475
476         empathy_chat_view_append_message (chat->view, message);
477
478         /* We received a message so the contact is no more composing */
479         chat_state_changed_cb (tp_chat, sender,
480                                TP_CHANNEL_CHAT_STATE_ACTIVE,
481                                chat);
482
483         g_signal_emit (chat, signals[NEW_MESSAGE], 0, message);
484 }
485
486 static void
487 chat_send_error_cb (EmpathyTpChat          *tp_chat,
488                     EmpathyMessage         *message,
489                     TpChannelTextSendError  error_code,
490                     EmpathyChat            *chat)
491 {
492         const gchar *error;
493         gchar       *str;
494
495         switch (error_code) {
496         case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
497                 error = _("offline");
498                 break;
499         case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
500                 error = _("invalid contact");
501                 break;
502         case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
503                 error = _("permission denied");
504                 break;
505         case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
506                 error = _("too long message");
507                 break;
508         case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
509                 error = _("not implemented");
510                 break;
511         default:
512                 error = _("unknown");
513                 break;
514         }
515
516         str = g_strdup_printf (_("Error sending message '%s': %s"),
517                                empathy_message_get_body (message),
518                                error);
519         empathy_chat_view_append_event (chat->view, str);
520         g_free (str);
521 }
522
523 static void
524 chat_property_changed_cb (EmpathyTpChat *tp_chat,
525                           const gchar   *name,
526                           GValue        *value,
527                           EmpathyChat   *chat)
528 {
529         EmpathyChatPriv *priv = GET_PRIV (chat);
530
531         if (!tp_strdiff (name, "subject")) {
532                 g_free (priv->subject);
533                 priv->subject = g_value_dup_string (value);
534                 g_object_notify (G_OBJECT (chat), "subject");
535
536                 if (G_STR_EMPTY (priv->subject)) {
537                         gtk_widget_hide (priv->hbox_topic);
538                 } else {
539                         gtk_label_set_text (GTK_LABEL (priv->label_topic), priv->subject);
540                         gtk_widget_show (priv->hbox_topic);
541                 }
542                 if (priv->block_events_timeout_id == 0) {
543                         gchar *str;
544
545                         if (!G_STR_EMPTY (priv->subject)) {
546                                 str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
547                         } else {
548                                 str = g_strdup (_("No topic defined"));
549                         }
550                         empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, str);
551                         g_free (str);
552                 }
553         }
554         else if (!tp_strdiff (name, "name")) {
555                 g_free (priv->name);
556                 priv->name = g_value_dup_string (value);
557                 g_object_notify (G_OBJECT (chat), "name");
558         }
559 }
560
561 static gboolean
562 chat_get_is_command (const gchar *str)
563 {
564         g_return_val_if_fail (str != NULL, FALSE);
565
566         if (str[0] != '/') {
567                 return FALSE;
568         }
569
570         if (g_str_has_prefix (str, "/me")) {
571                 return TRUE;
572         }
573         else if (g_str_has_prefix (str, "/nick")) {
574                 return TRUE;
575         }
576         else if (g_str_has_prefix (str, "/topic")) {
577                 return TRUE;
578         }
579
580         return FALSE;
581 }
582
583 static void
584 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
585                                    EmpathyChat    *chat)
586 {
587         EmpathyChatPriv *priv;
588         GtkTextIter     start, end;
589         gchar          *str;
590         gboolean        spell_checker = FALSE;
591
592         priv = GET_PRIV (chat);
593
594         if (gtk_text_buffer_get_char_count (buffer) == 0) {
595                 chat_composing_stop (chat);
596         } else {
597                 chat_composing_start (chat);
598         }
599
600         empathy_conf_get_bool (empathy_conf_get (),
601                               EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
602                               &spell_checker);
603
604         gtk_text_buffer_get_start_iter (buffer, &start);
605
606         if (!spell_checker) {
607                 gtk_text_buffer_get_end_iter (buffer, &end);
608                 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
609                 return;
610         }
611
612         if (!empathy_spell_supported ()) {
613                 return;
614         }
615
616         /* NOTE: this is really inefficient, we shouldn't have to
617            reiterate the whole buffer each time and check each work
618            every time. */
619         while (TRUE) {
620                 gboolean correct = FALSE;
621
622                 /* if at start */
623                 if (gtk_text_iter_is_start (&start)) {
624                         end = start;
625
626                         if (!gtk_text_iter_forward_word_end (&end)) {
627                                 /* no whole word yet */
628                                 break;
629                         }
630                 } else {
631                         if (!gtk_text_iter_forward_word_end (&end)) {
632                                 /* must be the end of the buffer */
633                                 break;
634                         }
635
636                         start = end;
637                         gtk_text_iter_backward_word_start (&start);
638                 }
639
640                 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
641
642                 /* spell check string */
643                 if (!chat_get_is_command (str)) {
644                         correct = empathy_spell_check (str);
645                 } else {
646                         correct = TRUE;
647                 }
648
649                 if (!correct) {
650                         gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
651                 } else {
652                         gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
653                 }
654
655                 g_free (str);
656
657                 /* set start iter to the end iters position */
658                 start = end;
659         }
660 }
661
662 static gboolean
663 chat_input_key_press_event_cb (GtkWidget   *widget,
664                                GdkEventKey *event,
665                                EmpathyChat *chat)
666 {
667         EmpathyChatPriv *priv;
668         GtkAdjustment  *adj;
669         gdouble         val;
670         GtkWidget      *text_view_sw;
671
672         priv = GET_PRIV (chat);
673
674         /* Catch ctrl+up/down so we can traverse messages we sent */
675         if ((event->state & GDK_CONTROL_MASK) && 
676             (event->keyval == GDK_Up || 
677              event->keyval == GDK_Down)) {
678                 GtkTextBuffer *buffer;
679                 const gchar   *str;
680
681                 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
682
683                 if (event->keyval == GDK_Up) {
684                         str = chat_sent_message_get_next (chat);
685                 } else {
686                         str = chat_sent_message_get_last (chat);
687                 }
688
689                 g_signal_handlers_block_by_func (buffer, 
690                                                  chat_input_text_buffer_changed_cb,
691                                                  chat);
692                 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
693                 g_signal_handlers_unblock_by_func (buffer, 
694                                                    chat_input_text_buffer_changed_cb,
695                                                    chat);
696
697                 return TRUE;    
698         }
699
700         /* Catch enter but not ctrl/shift-enter */
701         if (IS_ENTER (event->keyval) &&
702             !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
703                 GtkTextView *view;
704
705                 /* This is to make sure that kinput2 gets the enter. And if
706                  * it's handled there we shouldn't send on it. This is because
707                  * kinput2 uses Enter to commit letters. See:
708                  * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
709                  */
710
711                 view = GTK_TEXT_VIEW (chat->input_text_view);
712                 if (gtk_im_context_filter_keypress (view->im_context, event)) {
713                         GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
714                         return TRUE;
715                 }
716
717                 chat_input_text_view_send (chat);
718                 return TRUE;
719         }
720
721         text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
722
723         if (IS_ENTER (event->keyval) &&
724             (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
725                 /* Newline for shift/control-enter. */
726                 return FALSE;
727         }
728         if (!(event->state & GDK_CONTROL_MASK) &&
729             event->keyval == GDK_Page_Up) {
730                 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
731                 gtk_adjustment_set_value (adj, adj->value - adj->page_size);
732                 return TRUE;
733         }
734         if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
735             event->keyval == GDK_Page_Down) {
736                 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
737                 val = MIN (adj->value + adj->page_size, adj->upper - adj->page_size);
738                 gtk_adjustment_set_value (adj, val);
739                 return TRUE;
740         }
741         if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
742             event->keyval == GDK_Tab) {
743                 GtkTextBuffer *buffer;
744                 GtkTextIter    start, current;
745                 gchar         *nick, *completed;
746                 GList         *list, *completed_list;
747                 gboolean       is_start_of_buffer;
748
749                 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
750                 gtk_text_buffer_get_iter_at_mark (buffer, &current, gtk_text_buffer_get_insert (buffer));
751
752                 /* Get the start of the nick to complete. */
753                 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
754                 gtk_text_iter_backward_word_start (&start);
755                 is_start_of_buffer = gtk_text_iter_is_start (&start);
756
757                 list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
758                 g_completion_add_items (priv->completion, list);
759
760                 nick = gtk_text_buffer_get_text (buffer, &start, &current, FALSE);
761                 completed_list = g_completion_complete (priv->completion,
762                                                         nick,
763                                                         &completed);
764
765                 g_free (nick);
766
767                 if (completed) {
768                         guint        len;
769                         const gchar *text;
770                         gchar       *complete_char = NULL;
771
772                         gtk_text_buffer_delete (buffer, &start, &current);
773
774                         len = g_list_length (completed_list);
775
776                         if (len == 1) {
777                                 /* If we only have one hit, use that text
778                                  * instead of the text in completed since the
779                                  * completed text will use the typed string
780                                  * which might be cased all wrong.
781                                  * Fixes #120876
782                                  * */
783                                 text = empathy_contact_get_name (completed_list->data);
784                         } else {
785                                 text = completed;
786                         }
787
788                         gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
789
790                         if (len == 1 && is_start_of_buffer &&
791                             empathy_conf_get_string (empathy_conf_get (),
792                                                      EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR,
793                                                      &complete_char) &&
794                             complete_char != NULL) {
795                                 gtk_text_buffer_insert_at_cursor (buffer,
796                                                                   complete_char,
797                                                                   strlen (complete_char));
798                                 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
799                                 g_free (complete_char);
800                         }
801
802                         g_free (completed);
803                 }
804
805                 g_completion_clear_items (priv->completion);
806
807                 g_list_foreach (list, (GFunc) g_object_unref, NULL);
808                 g_list_free (list);
809
810                 return TRUE;
811         }
812
813         return FALSE;
814 }
815
816 static gboolean
817 chat_text_view_focus_in_event_cb (GtkWidget  *widget,
818                                   GdkEvent   *event,
819                                   EmpathyChat *chat)
820 {
821         gtk_widget_grab_focus (chat->input_text_view);
822
823         return TRUE;
824 }
825
826 static gboolean
827 chat_input_set_size_request_idle (gpointer sw)
828 {
829         gtk_widget_set_size_request (sw, -1, MAX_INPUT_HEIGHT);
830
831         return FALSE;
832 }
833
834 static void
835 chat_input_size_request_cb (GtkWidget      *widget,
836                             GtkRequisition *requisition,
837                             EmpathyChat    *chat)
838 {
839         EmpathyChatPriv *priv = GET_PRIV (chat);
840         GtkWidget       *sw;
841
842         sw = gtk_widget_get_parent (widget);
843         if (requisition->height >= MAX_INPUT_HEIGHT && !priv->has_input_vscroll) {
844                 g_idle_add (chat_input_set_size_request_idle, sw);
845                 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
846                                                 GTK_POLICY_NEVER,
847                                                 GTK_POLICY_ALWAYS);
848                 priv->has_input_vscroll = TRUE;
849         }
850
851         if (requisition->height < MAX_INPUT_HEIGHT && priv->has_input_vscroll) {
852                 gtk_widget_set_size_request (sw, -1, -1);
853                 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
854                                                 GTK_POLICY_NEVER,
855                                                 GTK_POLICY_NEVER);
856                 priv->has_input_vscroll = FALSE;
857         }
858 }
859
860 static void
861 chat_input_realize_cb (GtkWidget   *widget,
862                        EmpathyChat *chat)
863 {
864         DEBUG ("Setting focus to the input text view");
865         gtk_widget_grab_focus (widget);
866 }
867
868 static void
869 chat_insert_smiley_activate_cb (GtkWidget   *menuitem,
870                                 EmpathyChat *chat)
871 {
872         GtkTextBuffer *buffer;
873         GtkTextIter    iter;
874         const gchar   *smiley;
875
876         smiley = g_object_get_data (G_OBJECT (menuitem), "smiley_text");
877
878         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
879
880         gtk_text_buffer_get_end_iter (buffer, &iter);
881         gtk_text_buffer_insert (buffer, &iter, smiley, -1);
882
883         gtk_text_buffer_get_end_iter (buffer, &iter);
884         gtk_text_buffer_insert (buffer, &iter, " ", -1);
885 }
886
887 typedef struct {
888         EmpathyChat  *chat;
889         gchar       *word;
890
891         GtkTextIter  start;
892         GtkTextIter  end;
893 } EmpathyChatSpell;
894
895 static EmpathyChatSpell *
896 chat_spell_new (EmpathyChat  *chat,
897                 const gchar *word,
898                 GtkTextIter  start,
899                 GtkTextIter  end)
900 {
901         EmpathyChatSpell *chat_spell;
902
903         chat_spell = g_slice_new0 (EmpathyChatSpell);
904
905         chat_spell->chat = g_object_ref (chat);
906         chat_spell->word = g_strdup (word);
907         chat_spell->start = start;
908         chat_spell->end = end;
909
910         return chat_spell;
911 }
912
913 static void
914 chat_spell_free (EmpathyChatSpell *chat_spell)
915 {
916         g_object_unref (chat_spell->chat);
917         g_free (chat_spell->word);
918         g_slice_free (EmpathyChatSpell, chat_spell);
919 }
920
921 static void
922 chat_text_check_word_spelling_cb (GtkMenuItem     *menuitem,
923                                   EmpathyChatSpell *chat_spell)
924 {
925         empathy_spell_dialog_show (chat_spell->chat,
926                                   &chat_spell->start,
927                                   &chat_spell->end,
928                                   chat_spell->word);
929 }
930
931 static void
932 chat_text_send_cb (GtkMenuItem *menuitem,
933                    EmpathyChat *chat)
934 {
935         chat_input_text_view_send (chat);
936 }
937
938 static void
939 chat_input_populate_popup_cb (GtkTextView *view,
940                               GtkMenu     *menu,
941                               EmpathyChat *chat)
942 {
943         EmpathyChatPriv  *priv;
944         GtkTextBuffer   *buffer;
945         GtkTextTagTable *table;
946         GtkTextTag      *tag;
947         gint             x, y;
948         GtkTextIter      iter, start, end;
949         GtkWidget       *item;
950         gchar           *str = NULL;
951         EmpathyChatSpell *chat_spell;
952         GtkWidget       *smiley_menu;
953         GtkWidget       *image;
954
955         priv = GET_PRIV (chat);
956         buffer = gtk_text_view_get_buffer (view);
957
958         /* Add the emoticon menu. */
959         item = gtk_separator_menu_item_new ();
960         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
961         gtk_widget_show (item);
962
963         item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
964         image = gtk_image_new_from_icon_name ("face-smile",
965                                               GTK_ICON_SIZE_MENU);
966         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
967         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
968         gtk_widget_show (item);
969
970         smiley_menu = empathy_chat_view_get_smiley_menu (
971                 G_CALLBACK (chat_insert_smiley_activate_cb),
972                 chat);
973         gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
974
975         /* Add the Send menu item. */
976         gtk_text_buffer_get_bounds (buffer, &start, &end);
977         str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
978         if (!G_STR_EMPTY (str)) {
979                 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
980                 g_signal_connect (G_OBJECT (item), "activate",
981                                   G_CALLBACK (chat_text_send_cb), chat);
982                 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
983                 gtk_widget_show (item);
984         }
985         str = NULL;
986
987         /* Add the spell check menu item. */
988         table = gtk_text_buffer_get_tag_table (buffer);
989         tag = gtk_text_tag_table_lookup (table, "misspelled");
990         gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
991         gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
992                                                GTK_TEXT_WINDOW_WIDGET,
993                                                x, y,
994                                                &x, &y);
995         gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
996         start = end = iter;
997         if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
998             gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
999
1000                 str = gtk_text_buffer_get_text (buffer,
1001                                                 &start, &end, FALSE);
1002         }
1003         if (!G_STR_EMPTY (str)) {
1004                 chat_spell = chat_spell_new (chat, str, start, end);
1005                 g_object_set_data_full (G_OBJECT (menu),
1006                                         "chat_spell", chat_spell,
1007                                         (GDestroyNotify) chat_spell_free);
1008
1009                 item = gtk_separator_menu_item_new ();
1010                 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1011                 gtk_widget_show (item);
1012
1013                 item = gtk_image_menu_item_new_with_mnemonic (_("_Check Word Spelling..."));
1014                 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
1015                                                       GTK_ICON_SIZE_MENU);
1016                 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1017                 g_signal_connect (item,
1018                                   "activate",
1019                                   G_CALLBACK (chat_text_check_word_spelling_cb),
1020                                   chat_spell);
1021                 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1022                 gtk_widget_show (item);
1023         }
1024 }
1025
1026 static void
1027 chat_add_logs (EmpathyChat *chat)
1028 {
1029         EmpathyChatPriv *priv = GET_PRIV (chat);
1030         gboolean         is_chatroom;
1031         GList           *messages, *l;
1032         guint            num_messages;
1033         guint            i;
1034
1035         if (!priv->id) {
1036                 return;
1037         }
1038
1039         /* Turn off scrolling temporarily */
1040         empathy_chat_view_scroll (chat->view, FALSE);
1041
1042         /* Add messages from last conversation */
1043         is_chatroom = priv->handle_type == TP_HANDLE_TYPE_ROOM;
1044         messages = empathy_log_manager_get_last_messages (priv->log_manager,
1045                                                           priv->account,
1046                                                           priv->id,
1047                                                           is_chatroom);
1048         num_messages  = g_list_length (messages);
1049
1050         /* Only keep the 10 last messages */
1051         for (i = 0; num_messages - i > 10; i++) {
1052                 EmpathyMessage *message;
1053
1054                 message = messages->data;
1055                 messages = g_list_remove (messages, message);
1056                 g_object_unref (message);
1057         }
1058
1059         for (l = messages; l; l = l->next) {
1060                 empathy_chat_view_append_message (chat->view, l->data);
1061                 g_object_unref (l->data);
1062         }
1063         g_list_free (messages);
1064
1065         /* Turn back on scrolling */
1066         empathy_chat_view_scroll (chat->view, TRUE);
1067 }
1068
1069 static gint
1070 chat_contacts_completion_func (const gchar *s1,
1071                                const gchar *s2,
1072                                gsize        n)
1073 {
1074         gchar *tmp, *nick1, *nick2;
1075         gint   ret;
1076
1077         if (s1 == s2) {
1078                 return 0;
1079         }
1080         if (!s1 || !s2) {
1081                 return s1 ? -1 : +1;
1082         }
1083
1084         tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
1085         nick1 = g_utf8_casefold (tmp, -1);
1086         g_free (tmp);
1087
1088         tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
1089         nick2 = g_utf8_casefold (tmp, -1);
1090         g_free (tmp);
1091
1092         ret = strncmp (nick1, nick2, n);
1093
1094         g_free (nick1);
1095         g_free (nick2);
1096
1097         return ret;
1098 }
1099
1100 static void
1101 chat_members_changed_cb (EmpathyTpChat  *tp_chat,
1102                          EmpathyContact *contact,
1103                          EmpathyContact *actor,
1104                          guint           reason,
1105                          gchar          *message,
1106                          gboolean        is_member,
1107                          EmpathyChat    *chat)
1108 {
1109         EmpathyChatPriv *priv = GET_PRIV (chat);
1110
1111         if (priv->block_events_timeout_id == 0) {
1112                 gchar *str;
1113
1114                 empathy_contact_run_until_ready (contact,
1115                                                  EMPATHY_CONTACT_READY_NAME,
1116                                                  NULL);
1117
1118                 if (is_member) {
1119                         str = g_strdup_printf (_("%s has joined the room"),
1120                                                empathy_contact_get_name (contact));
1121                 } else {
1122                         str = g_strdup_printf (_("%s has left the room"),
1123                                                empathy_contact_get_name (contact));
1124                 }
1125                 empathy_chat_view_append_event (chat->view, str);
1126                 g_free (str);
1127         }
1128 }
1129
1130 static gboolean
1131 chat_reset_size_request (gpointer widget)
1132 {
1133         gtk_widget_set_size_request (widget, -1, -1);
1134
1135         return FALSE;
1136 }
1137
1138 static void
1139 chat_set_show_contacts (EmpathyChat *chat, gboolean show)
1140 {
1141         EmpathyChatPriv *priv = GET_PRIV (chat);
1142
1143         if (!priv->scrolled_window_contacts) {
1144                 return;
1145         }
1146
1147         if (show) {
1148                 EmpathyContactListStore *store;
1149                 gint                     min_width;
1150
1151                 /* We are adding the contact list to the chat, we don't want the
1152                  * chat view to become too small. If the chat view is already
1153                  * smaller than 250 make sure that size won't change. If the
1154                  * chat view is bigger the contact list will take some space on
1155                  * it but we make sure the chat view don't become smaller than
1156                  * 250. Relax the size request once the resize is done */
1157                 min_width = MIN (priv->vbox_left->allocation.width, 250);
1158                 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
1159                 g_idle_add (chat_reset_size_request, priv->vbox_left);
1160
1161                 if (priv->contacts_width > 0) {
1162                         gtk_paned_set_position (GTK_PANED (priv->hpaned),
1163                                                 priv->contacts_width);
1164                 }
1165
1166                 store = empathy_contact_list_store_new (EMPATHY_CONTACT_LIST (priv->tp_chat));
1167                 priv->contact_list_view = GTK_WIDGET (empathy_contact_list_view_new (store,
1168                         EMPATHY_CONTACT_LIST_FEATURE_NONE,
1169                         EMPATHY_CONTACT_FEATURE_CHAT |
1170                         EMPATHY_CONTACT_FEATURE_CALL |
1171                         EMPATHY_CONTACT_FEATURE_LOG |
1172                         EMPATHY_CONTACT_FEATURE_INFO));
1173                 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
1174                                    priv->contact_list_view);
1175                 gtk_widget_show (priv->contact_list_view);
1176                 gtk_widget_show (priv->scrolled_window_contacts);
1177                 g_object_unref (store);
1178         } else {
1179                 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
1180                 gtk_widget_hide (priv->scrolled_window_contacts);
1181                 if (priv->contact_list_view) {
1182                         gtk_widget_destroy (priv->contact_list_view);
1183                         priv->contact_list_view = NULL;
1184                 }
1185         }
1186 }
1187
1188 static void
1189 chat_remote_contact_changed_cb (EmpathyChat *chat)
1190 {
1191         EmpathyChatPriv *priv = GET_PRIV (chat);
1192
1193         if (priv->remote_contact) {
1194                 g_object_unref (priv->remote_contact);
1195                 priv->remote_contact = NULL;
1196         }
1197
1198         priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
1199         if (priv->remote_contact) {
1200                 g_object_ref (priv->remote_contact);
1201                 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
1202                 g_free (priv->id);
1203                 priv->id = g_strdup (empathy_contact_get_id (priv->remote_contact));
1204         }
1205         else if (priv->tp_chat) {
1206                 TpChannel *channel;
1207
1208                 channel = empathy_tp_chat_get_channel (priv->tp_chat);
1209                 g_object_get (channel, "handle-type", &priv->handle_type, NULL);
1210                 g_free (priv->id);
1211                 priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
1212         }
1213
1214         chat_set_show_contacts (chat, priv->remote_contact == NULL);
1215
1216         g_object_notify (G_OBJECT (chat), "remote-contact");
1217         g_object_notify (G_OBJECT (chat), "id");
1218 }
1219
1220 static void
1221 chat_destroy_cb (EmpathyTpChat *tp_chat,
1222                  EmpathyChat   *chat)
1223 {
1224         EmpathyChatPriv *priv;
1225
1226         priv = GET_PRIV (chat);
1227
1228         if (!priv->tp_chat) {
1229                 return;
1230         }
1231
1232         g_object_unref (priv->tp_chat);
1233         priv->tp_chat = NULL;
1234         g_object_notify (G_OBJECT (chat), "tp-chat");
1235
1236         empathy_chat_view_append_event (chat->view, _("Disconnected"));
1237         gtk_widget_set_sensitive (chat->input_text_view, FALSE);
1238         chat_set_show_contacts (chat, FALSE);
1239 }
1240
1241 static void
1242 chat_create_ui (EmpathyChat *chat)
1243 {
1244         EmpathyChatPriv *priv = GET_PRIV (chat);
1245         GladeXML        *glade;
1246         GList           *list = NULL; 
1247         gchar           *filename;
1248         GtkTextBuffer   *buffer;
1249
1250         filename = empathy_file_lookup ("empathy-chat.glade",
1251                                         "libempathy-gtk");
1252         glade = empathy_glade_get_file (filename,
1253                                         "chat_widget",
1254                                         NULL,
1255                                         "chat_widget", &priv->widget,
1256                                         "hpaned", &priv->hpaned,
1257                                         "vbox_left", &priv->vbox_left,
1258                                         "scrolled_window_chat", &priv->scrolled_window_chat,
1259                                         "scrolled_window_input", &priv->scrolled_window_input,
1260                                         "hbox_topic", &priv->hbox_topic,
1261                                         "label_topic", &priv->label_topic,
1262                                         "scrolled_window_contacts", &priv->scrolled_window_contacts,
1263                                         NULL);
1264         g_free (filename);
1265         g_object_unref (glade);
1266
1267         /* Add message GtkTextView. */
1268         chat->view = empathy_chat_view_new ();
1269         g_signal_connect (chat->view, "focus_in_event",
1270                           G_CALLBACK (chat_text_view_focus_in_event_cb),
1271                           chat);
1272         gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
1273                            GTK_WIDGET (chat->view));
1274         gtk_widget_show (GTK_WIDGET (chat->view));
1275
1276         /* Add input GtkTextView */
1277         chat->input_text_view = g_object_new (GTK_TYPE_TEXT_VIEW,
1278                                               "pixels-above-lines", 2,
1279                                               "pixels-below-lines", 2,
1280                                               "pixels-inside-wrap", 1,
1281                                               "right-margin", 2,
1282                                               "left-margin", 2,
1283                                               "wrap-mode", GTK_WRAP_WORD_CHAR,
1284                                               NULL);
1285         g_signal_connect (chat->input_text_view, "key-press-event",
1286                           G_CALLBACK (chat_input_key_press_event_cb),
1287                           chat);
1288         g_signal_connect (chat->input_text_view, "size-request",
1289                           G_CALLBACK (chat_input_size_request_cb),
1290                           chat);
1291         g_signal_connect (chat->input_text_view, "realize",
1292                           G_CALLBACK (chat_input_realize_cb),
1293                           chat);
1294         g_signal_connect (chat->input_text_view, "populate-popup",
1295                           G_CALLBACK (chat_input_populate_popup_cb),
1296                           chat);
1297         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1298         g_signal_connect (buffer, "changed",
1299                           G_CALLBACK (chat_input_text_buffer_changed_cb),
1300                           chat);
1301         gtk_text_buffer_create_tag (buffer, "misspelled",
1302                                     "underline", PANGO_UNDERLINE_ERROR,
1303                                     NULL);
1304         gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
1305                            chat->input_text_view);
1306         gtk_widget_show (chat->input_text_view);
1307
1308         /* Create contact list */
1309         chat_set_show_contacts (chat, priv->remote_contact == NULL);
1310
1311         /* Initialy hide the topic, will be shown if not empty */
1312         gtk_widget_hide (priv->hbox_topic);
1313
1314         /* Set widget focus order */
1315         list = g_list_append (NULL, priv->scrolled_window_input);
1316         gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
1317         g_list_free (list);
1318
1319         list = g_list_append (NULL, priv->vbox_left);
1320         list = g_list_append (list, priv->scrolled_window_contacts);
1321         gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
1322         g_list_free (list);
1323
1324         list = g_list_append (NULL, priv->hpaned);
1325         list = g_list_append (list, priv->hbox_topic);
1326         gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
1327         g_list_free (list);
1328
1329         /* Add the main widget in the chat widget */
1330         gtk_container_add (GTK_CONTAINER (chat), priv->widget);
1331 }
1332
1333 static void
1334 chat_size_request (GtkWidget      *widget,
1335                    GtkRequisition *requisition)
1336 {
1337   GtkBin *bin = GTK_BIN (widget);
1338
1339   requisition->width = GTK_CONTAINER (widget)->border_width * 2;
1340   requisition->height = GTK_CONTAINER (widget)->border_width * 2;
1341
1342   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
1343     {
1344       GtkRequisition child_requisition;
1345       
1346       gtk_widget_size_request (bin->child, &child_requisition);
1347
1348       requisition->width += child_requisition.width;
1349       requisition->height += child_requisition.height;
1350     }
1351 }
1352
1353 static void
1354 chat_size_allocate (GtkWidget     *widget,
1355                     GtkAllocation *allocation)
1356 {
1357   GtkBin *bin = GTK_BIN (widget);
1358   GtkAllocation child_allocation;
1359   
1360   widget->allocation = *allocation;
1361
1362   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
1363     {
1364       child_allocation.x = allocation->x + GTK_CONTAINER (widget)->border_width;
1365       child_allocation.y = allocation->y + GTK_CONTAINER (widget)->border_width;
1366       child_allocation.width = MAX (allocation->width - GTK_CONTAINER (widget)->border_width * 2, 0);
1367       child_allocation.height = MAX (allocation->height - GTK_CONTAINER (widget)->border_width * 2, 0);
1368
1369       gtk_widget_size_allocate (bin->child, &child_allocation);
1370     }
1371 }
1372
1373 static void
1374 chat_finalize (GObject *object)
1375 {
1376         EmpathyChat     *chat;
1377         EmpathyChatPriv *priv;
1378
1379         chat = EMPATHY_CHAT (object);
1380         priv = GET_PRIV (chat);
1381
1382         DEBUG ("Finalized: %p", object);
1383
1384         g_slist_foreach (priv->sent_messages, (GFunc) g_free, NULL);
1385         g_slist_free (priv->sent_messages);
1386
1387         g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
1388         g_list_free (priv->compositors);
1389
1390         chat_composing_remove_timeout (chat);
1391
1392         empathy_disconnect_account_status_changed (priv->token);
1393         g_object_unref (priv->mc);
1394         g_object_unref (priv->log_manager);
1395
1396         if (priv->tp_chat) {
1397                 g_object_unref (priv->tp_chat);
1398         }
1399         if (priv->account) {
1400                 g_object_unref (priv->account);
1401         }
1402         if (priv->remote_contact) {
1403                 g_object_unref (priv->remote_contact);
1404         }
1405
1406         if (priv->block_events_timeout_id) {
1407                 g_source_remove (priv->block_events_timeout_id);
1408         }
1409
1410         g_free (priv->id);
1411         g_free (priv->name);
1412         g_free (priv->subject);
1413
1414         G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
1415 }
1416
1417 static void
1418 chat_constructed (GObject *object)
1419 {
1420         EmpathyChat *chat = EMPATHY_CHAT (object);
1421
1422         chat_create_ui (chat);
1423         chat_add_logs (chat);
1424 }
1425
1426 static void
1427 empathy_chat_class_init (EmpathyChatClass *klass)
1428 {
1429         GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1430         GObjectClass   *object_class = G_OBJECT_CLASS (klass);
1431
1432         object_class->finalize = chat_finalize;
1433         object_class->get_property = chat_get_property;
1434         object_class->set_property = chat_set_property;
1435         object_class->constructed = chat_constructed;
1436
1437         widget_class->size_request = chat_size_request;
1438         widget_class->size_allocate = chat_size_allocate;
1439
1440         g_object_class_install_property (object_class,
1441                                          PROP_TP_CHAT,
1442                                          g_param_spec_object ("tp-chat",
1443                                                               "Empathy tp chat",
1444                                                               "The tp chat object",
1445                                                               EMPATHY_TYPE_TP_CHAT,
1446                                                               G_PARAM_CONSTRUCT |
1447                                                               G_PARAM_READWRITE));
1448         g_object_class_install_property (object_class,
1449                                          PROP_ACCOUNT,
1450                                          g_param_spec_object ("account",
1451                                                               "Account of the chat",
1452                                                               "The account of the chat",
1453                                                               MC_TYPE_ACCOUNT,
1454                                                               G_PARAM_READABLE));
1455         g_object_class_install_property (object_class,
1456                                          PROP_ID,
1457                                          g_param_spec_string ("id",
1458                                                               "Chat's id",
1459                                                               "The id of the chat",
1460                                                               NULL,
1461                                                               G_PARAM_READABLE));
1462         g_object_class_install_property (object_class,
1463                                          PROP_NAME,
1464                                          g_param_spec_string ("name",
1465                                                               "Chat's name",
1466                                                               "The name of the chat",
1467                                                               NULL,
1468                                                               G_PARAM_READABLE));
1469         g_object_class_install_property (object_class,
1470                                          PROP_SUBJECT,
1471                                          g_param_spec_string ("subject",
1472                                                               "Chat's subject",
1473                                                               "The subject or topic of the chat",
1474                                                               NULL,
1475                                                               G_PARAM_READABLE));
1476         g_object_class_install_property (object_class,
1477                                          PROP_REMOTE_CONTACT,
1478                                          g_param_spec_object ("remote-contact",
1479                                                               "The remote contact",
1480                                                               "The remote contact is any",
1481                                                               EMPATHY_TYPE_CONTACT,
1482                                                               G_PARAM_READABLE));
1483
1484         signals[COMPOSING] =
1485                 g_signal_new ("composing",
1486                               G_OBJECT_CLASS_TYPE (object_class),
1487                               G_SIGNAL_RUN_LAST,
1488                               0,
1489                               NULL, NULL,
1490                               g_cclosure_marshal_VOID__BOOLEAN,
1491                               G_TYPE_NONE,
1492                               1, G_TYPE_BOOLEAN);
1493
1494         signals[NEW_MESSAGE] =
1495                 g_signal_new ("new-message",
1496                               G_OBJECT_CLASS_TYPE (object_class),
1497                               G_SIGNAL_RUN_LAST,
1498                               0,
1499                               NULL, NULL,
1500                               g_cclosure_marshal_VOID__OBJECT,
1501                               G_TYPE_NONE,
1502                               1, EMPATHY_TYPE_MESSAGE);
1503
1504         g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
1505 }
1506
1507 static gboolean
1508 chat_block_events_timeout_cb (gpointer data)
1509 {
1510         EmpathyChatPriv *priv = GET_PRIV (data);
1511
1512         priv->block_events_timeout_id = 0;
1513
1514         return FALSE;
1515 }
1516
1517 static void
1518 empathy_chat_init (EmpathyChat *chat)
1519 {
1520         EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
1521                 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
1522
1523         chat->priv = priv;
1524         priv->log_manager = empathy_log_manager_new ();
1525         priv->contacts_width = -1;
1526         priv->sent_messages = NULL;
1527         priv->sent_messages_index = -1;
1528         priv->mc = empathy_mission_control_new ();
1529
1530         priv->token = empathy_connect_to_account_status_changed (priv->mc,
1531                                                    G_CALLBACK (chat_status_changed_cb),
1532                                                    chat, NULL);
1533
1534         /* Block events for some time to avoid having "has come online" or
1535          * "joined" messages. */
1536         priv->block_events_timeout_id =
1537                 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
1538
1539         /* Add nick name completion */
1540         priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_name);
1541         g_completion_set_compare (priv->completion, chat_contacts_completion_func);
1542 }
1543
1544 EmpathyChat *
1545 empathy_chat_new (EmpathyTpChat *tp_chat)
1546 {
1547         return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
1548 }
1549
1550 EmpathyTpChat *
1551 empathy_chat_get_tp_chat (EmpathyChat *chat)
1552 {
1553         EmpathyChatPriv *priv = GET_PRIV (chat);
1554
1555         g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1556
1557         return priv->tp_chat;
1558 }
1559
1560 void
1561 empathy_chat_set_tp_chat (EmpathyChat   *chat,
1562                           EmpathyTpChat *tp_chat)
1563 {
1564         EmpathyChatPriv *priv = GET_PRIV (chat);
1565
1566         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1567         g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
1568         g_return_if_fail (empathy_tp_chat_is_ready (tp_chat));
1569
1570         if (priv->tp_chat) {
1571                 return;
1572         }
1573
1574         if (priv->account) {
1575                 g_object_unref (priv->account);
1576         }
1577
1578         priv->tp_chat = g_object_ref (tp_chat);
1579         priv->account = g_object_ref (empathy_tp_chat_get_account (tp_chat));
1580
1581         g_signal_connect (tp_chat, "message-received",
1582                           G_CALLBACK (chat_message_received_cb),
1583                           chat);
1584         g_signal_connect (tp_chat, "send-error",
1585                           G_CALLBACK (chat_send_error_cb),
1586                           chat);
1587         g_signal_connect (tp_chat, "chat-state-changed",
1588                           G_CALLBACK (chat_state_changed_cb),
1589                           chat);
1590         g_signal_connect (tp_chat, "property-changed",
1591                           G_CALLBACK (chat_property_changed_cb),
1592                           chat);
1593         g_signal_connect (tp_chat, "members-changed",
1594                           G_CALLBACK (chat_members_changed_cb),
1595                           chat);
1596         g_signal_connect_swapped (tp_chat, "notify::remote-contact",
1597                                   G_CALLBACK (chat_remote_contact_changed_cb),
1598                                   chat);
1599         g_signal_connect (tp_chat, "destroy",
1600                           G_CALLBACK (chat_destroy_cb),
1601                           chat);
1602
1603         chat_remote_contact_changed_cb (chat);
1604
1605         if (chat->input_text_view) {
1606                 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
1607                 if (priv->block_events_timeout_id == 0) {
1608                         empathy_chat_view_append_event (chat->view, _("Connected"));
1609                 }
1610         }
1611
1612         empathy_tp_chat_set_acknowledge (priv->tp_chat, TRUE);
1613         empathy_tp_chat_emit_pendings (priv->tp_chat);
1614
1615         g_object_notify (G_OBJECT (chat), "tp-chat");
1616         g_object_notify (G_OBJECT (chat), "id");
1617         g_object_notify (G_OBJECT (chat), "account");
1618 }
1619
1620 McAccount *
1621 empathy_chat_get_account (EmpathyChat *chat)
1622 {
1623         EmpathyChatPriv *priv = GET_PRIV (chat);
1624
1625         g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1626
1627         return priv->account;
1628 }
1629
1630 const gchar *
1631 empathy_chat_get_id (EmpathyChat *chat)
1632 {
1633         EmpathyChatPriv *priv = GET_PRIV (chat);
1634
1635         g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1636
1637         return priv->id;
1638 }
1639
1640 const gchar *
1641 empathy_chat_get_name (EmpathyChat *chat)
1642 {
1643         EmpathyChatPriv *priv = GET_PRIV (chat);
1644
1645         g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1646
1647         return priv->name;
1648 }
1649
1650 const gchar *
1651 empathy_chat_get_subject (EmpathyChat *chat)
1652 {
1653         EmpathyChatPriv *priv = GET_PRIV (chat);
1654
1655         g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1656
1657         return priv->subject;
1658 }
1659
1660 EmpathyContact *
1661 empathy_chat_get_remote_contact (EmpathyChat *chat)
1662 {
1663         EmpathyChatPriv *priv = GET_PRIV (chat);
1664
1665         g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1666
1667         return priv->remote_contact;
1668 }
1669
1670 guint
1671 empathy_chat_get_members_count (EmpathyChat *chat)
1672 {
1673         EmpathyChatPriv *priv = GET_PRIV (chat);
1674
1675         g_return_val_if_fail (EMPATHY_IS_CHAT (chat), 0);
1676
1677         if (priv->tp_chat) {
1678                 return empathy_tp_chat_get_members_count (priv->tp_chat);
1679         }
1680
1681         return 0;
1682 }
1683
1684 GtkWidget *
1685 empathy_chat_get_contact_menu (EmpathyChat *chat)
1686 {
1687         EmpathyChatPriv *priv = GET_PRIV (chat);
1688         GtkWidget       *menu = NULL;
1689
1690         g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1691
1692         if (priv->remote_contact) {
1693                 menu = empathy_contact_menu_new (priv->remote_contact,
1694                                                  EMPATHY_CONTACT_FEATURE_CALL |
1695                                                  EMPATHY_CONTACT_FEATURE_LOG |
1696                                                  EMPATHY_CONTACT_FEATURE_INFO);
1697         }
1698         else if (priv->contact_list_view) {
1699                 EmpathyContactListView *view;
1700
1701                 view = EMPATHY_CONTACT_LIST_VIEW (priv->contact_list_view);
1702                 menu = empathy_contact_list_view_get_contact_menu (view);
1703         }
1704
1705         return menu;
1706 }
1707
1708 void
1709 empathy_chat_clear (EmpathyChat *chat)
1710 {
1711         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1712
1713         empathy_chat_view_clear (chat->view);
1714 }
1715
1716 void
1717 empathy_chat_scroll_down (EmpathyChat *chat)
1718 {
1719         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1720
1721         empathy_chat_view_scroll_down (chat->view);
1722 }
1723
1724 void
1725 empathy_chat_cut (EmpathyChat *chat)
1726 {
1727         GtkTextBuffer *buffer;
1728
1729         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1730
1731         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1732         if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1733                 GtkClipboard *clipboard;
1734
1735                 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1736
1737                 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
1738         }
1739 }
1740
1741 void
1742 empathy_chat_copy (EmpathyChat *chat)
1743 {
1744         GtkTextBuffer *buffer;
1745
1746         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1747
1748         if (empathy_chat_view_get_selection_bounds (chat->view, NULL, NULL)) {
1749                 empathy_chat_view_copy_clipboard (chat->view);
1750                 return;
1751         }
1752
1753         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1754         if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1755                 GtkClipboard *clipboard;
1756
1757                 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1758
1759                 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1760         }
1761 }
1762
1763 void
1764 empathy_chat_paste (EmpathyChat *chat)
1765 {
1766         GtkTextBuffer *buffer;
1767         GtkClipboard  *clipboard;
1768
1769         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1770
1771         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1772         clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1773
1774         gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
1775 }
1776
1777 void
1778 empathy_chat_correct_word (EmpathyChat  *chat,
1779                           GtkTextIter *start,
1780                           GtkTextIter *end,
1781                           const gchar *new_word)
1782 {
1783         GtkTextBuffer *buffer;
1784
1785         g_return_if_fail (chat != NULL);
1786         g_return_if_fail (new_word != NULL);
1787
1788         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1789
1790         gtk_text_buffer_delete (buffer, start, end);
1791         gtk_text_buffer_insert (buffer, start,
1792                                 new_word,
1793                                 -1);
1794 }
1795
1796 gboolean
1797 empathy_chat_is_room (EmpathyChat *chat)
1798 {
1799         EmpathyChatPriv *priv = GET_PRIV (chat);
1800
1801         g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
1802
1803         return (priv->handle_type == TP_HANDLE_TYPE_ROOM);
1804 }
1805