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