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