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