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