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