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