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