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