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