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