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