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