]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-chat.c
Completely rework the theming system to make easy implementing themes based on other...
[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 <libmissioncontrol/mission-control.h>
38 #include <telepathy-glib/util.h>
39
40 #include <libempathy/empathy-log-manager.h>
41 #include <libempathy/empathy-contact-list.h>
42 #include <libempathy/empathy-utils.h>
43
44 #include "empathy-chat.h"
45 #include "empathy-conf.h"
46 #include "empathy-spell.h"
47 #include "empathy-spell-dialog.h"
48 #include "empathy-contact-list-store.h"
49 #include "empathy-contact-list-view.h"
50 #include "empathy-contact-menu.h"
51 #include "empathy-theme-manager.h"
52 #include "empathy-smiley-manager.h"
53 #include "empathy-ui-utils.h"
54
55 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
56 #include <libempathy/empathy-debug.h>
57
58 #define CHAT_DIR_CREATE_MODE  (S_IRUSR | S_IWUSR | S_IXUSR)
59 #define CHAT_FILE_CREATE_MODE (S_IRUSR | S_IWUSR)
60 #define IS_ENTER(v) (v == GDK_Return || v == GDK_ISO_Enter || v == GDK_KP_Enter)
61 #define MAX_INPUT_HEIGHT 150
62 #define COMPOSING_STOP_TIMEOUT 5
63
64 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChat)
65 typedef struct {
66         EmpathyTpChat     *tp_chat;
67         McAccount         *account;
68         gchar             *id;
69         gchar             *name;
70         gchar             *subject;
71         EmpathyContact    *remote_contact;
72
73         EmpathyLogManager *log_manager;
74         MissionControl    *mc;
75         GSList            *sent_messages;
76         gint               sent_messages_index;
77         GList             *compositors;
78         GCompletion       *completion;
79         guint              composing_stop_timeout_id;
80         guint              block_events_timeout_id;
81         TpHandleType       handle_type;
82         gpointer           token;
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_status_changed_cb (MissionControl           *mc,
171                         TpConnectionStatus        status,
172                         McPresence                presence,
173                         TpConnectionStatusReason  reason,
174                         const gchar              *unique_name,
175                         EmpathyChat              *chat)
176 {
177         EmpathyChatPriv *priv = GET_PRIV (chat);
178         McAccount       *account;
179
180         account = mc_account_lookup (unique_name);
181
182         if (status == TP_CONNECTION_STATUS_CONNECTED && !priv->tp_chat &&
183             empathy_account_equal (account, priv->account) &&
184             priv->handle_type != TP_HANDLE_TYPE_NONE) {
185                 TpConnection *connection;
186
187                 DEBUG ("Account reconnected, request a new Text channel");
188                 connection = mission_control_get_tpconnection (mc, account, NULL);
189                 tp_connection_run_until_ready (connection, FALSE, NULL, NULL);
190                 empathy_connection_request_channel (connection, -1,
191                                                     TP_IFACE_CHANNEL_TYPE_TEXT,
192                                                     priv->handle_type,
193                                                     priv->id, TRUE,
194                                                     NULL, NULL, NULL, NULL);
195                 g_object_unref (connection);
196         }
197
198         g_object_unref (account);
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 (G_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_cb (EmpathyTpChat  *tp_chat,
458                           EmpathyMessage *message,
459                           EmpathyChat    *chat)
460 {
461         EmpathyChatPriv *priv = GET_PRIV (chat);
462         EmpathyContact  *sender;
463
464         sender = empathy_message_get_sender (message);
465
466         DEBUG ("Appending new message from %s (%d)",
467                 empathy_contact_get_name (sender),
468                 empathy_contact_get_handle (sender));
469
470         if (priv->id) {
471                 gboolean is_chatroom;
472
473                 is_chatroom = priv->handle_type == TP_HANDLE_TYPE_ROOM;
474                 empathy_log_manager_add_message (priv->log_manager,
475                                                  priv->id, is_chatroom,
476                                                  message);
477         }
478
479         empathy_chat_view_append_message (chat->view, message);
480
481         /* We received a message so the contact is no more composing */
482         chat_state_changed_cb (tp_chat, sender,
483                                TP_CHANNEL_CHAT_STATE_ACTIVE,
484                                chat);
485
486         g_signal_emit (chat, signals[NEW_MESSAGE], 0, message);
487 }
488
489 static void
490 chat_send_error_cb (EmpathyTpChat          *tp_chat,
491                     EmpathyMessage         *message,
492                     TpChannelTextSendError  error_code,
493                     EmpathyChat            *chat)
494 {
495         const gchar *error;
496         gchar       *str;
497
498         switch (error_code) {
499         case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
500                 error = _("offline");
501                 break;
502         case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
503                 error = _("invalid contact");
504                 break;
505         case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
506                 error = _("permission denied");
507                 break;
508         case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
509                 error = _("too long message");
510                 break;
511         case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
512                 error = _("not implemented");
513                 break;
514         default:
515                 error = _("unknown");
516                 break;
517         }
518
519         str = g_strdup_printf (_("Error sending message '%s': %s"),
520                                empathy_message_get_body (message),
521                                error);
522         empathy_chat_view_append_event (chat->view, str);
523         g_free (str);
524 }
525
526 static void
527 chat_property_changed_cb (EmpathyTpChat *tp_chat,
528                           const gchar   *name,
529                           GValue        *value,
530                           EmpathyChat   *chat)
531 {
532         EmpathyChatPriv *priv = GET_PRIV (chat);
533
534         if (!tp_strdiff (name, "subject")) {
535                 g_free (priv->subject);
536                 priv->subject = g_value_dup_string (value);
537                 g_object_notify (G_OBJECT (chat), "subject");
538
539                 if (G_STR_EMPTY (priv->subject)) {
540                         gtk_widget_hide (priv->hbox_topic);
541                 } else {
542                         gtk_label_set_text (GTK_LABEL (priv->label_topic), priv->subject);
543                         gtk_widget_show (priv->hbox_topic);
544                 }
545                 if (priv->block_events_timeout_id == 0) {
546                         gchar *str;
547
548                         if (!G_STR_EMPTY (priv->subject)) {
549                                 str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
550                         } else {
551                                 str = g_strdup (_("No topic defined"));
552                         }
553                         empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, str);
554                         g_free (str);
555                 }
556         }
557         else if (!tp_strdiff (name, "name")) {
558                 g_free (priv->name);
559                 priv->name = g_value_dup_string (value);
560                 g_object_notify (G_OBJECT (chat), "name");
561         }
562 }
563
564 static gboolean
565 chat_get_is_command (const gchar *str)
566 {
567         g_return_val_if_fail (str != NULL, FALSE);
568
569         if (str[0] != '/') {
570                 return FALSE;
571         }
572
573         if (g_str_has_prefix (str, "/me")) {
574                 return TRUE;
575         }
576         else if (g_str_has_prefix (str, "/nick")) {
577                 return TRUE;
578         }
579         else if (g_str_has_prefix (str, "/topic")) {
580                 return TRUE;
581         }
582
583         return FALSE;
584 }
585
586 static void
587 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
588                                    EmpathyChat    *chat)
589 {
590         EmpathyChatPriv *priv;
591         GtkTextIter     start, end;
592         gchar          *str;
593         gboolean        spell_checker = FALSE;
594
595         priv = GET_PRIV (chat);
596
597         if (gtk_text_buffer_get_char_count (buffer) == 0) {
598                 chat_composing_stop (chat);
599         } else {
600                 chat_composing_start (chat);
601         }
602
603         empathy_conf_get_bool (empathy_conf_get (),
604                               EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
605                               &spell_checker);
606
607         gtk_text_buffer_get_start_iter (buffer, &start);
608
609         if (!spell_checker) {
610                 gtk_text_buffer_get_end_iter (buffer, &end);
611                 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
612                 return;
613         }
614
615         if (!empathy_spell_supported ()) {
616                 return;
617         }
618
619         /* NOTE: this is really inefficient, we shouldn't have to
620            reiterate the whole buffer each time and check each work
621            every time. */
622         while (TRUE) {
623                 gboolean correct = FALSE;
624
625                 /* if at start */
626                 if (gtk_text_iter_is_start (&start)) {
627                         end = start;
628
629                         if (!gtk_text_iter_forward_word_end (&end)) {
630                                 /* no whole word yet */
631                                 break;
632                         }
633                 } else {
634                         if (!gtk_text_iter_forward_word_end (&end)) {
635                                 /* must be the end of the buffer */
636                                 break;
637                         }
638
639                         start = end;
640                         gtk_text_iter_backward_word_start (&start);
641                 }
642
643                 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
644
645                 /* spell check string */
646                 if (!chat_get_is_command (str)) {
647                         correct = empathy_spell_check (str);
648                 } else {
649                         correct = TRUE;
650                 }
651
652                 if (!correct) {
653                         gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
654                 } else {
655                         gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
656                 }
657
658                 g_free (str);
659
660                 /* set start iter to the end iters position */
661                 start = end;
662         }
663 }
664
665 static gboolean
666 chat_input_key_press_event_cb (GtkWidget   *widget,
667                                GdkEventKey *event,
668                                EmpathyChat *chat)
669 {
670         EmpathyChatPriv *priv;
671         GtkAdjustment  *adj;
672         gdouble         val;
673         GtkWidget      *text_view_sw;
674
675         priv = GET_PRIV (chat);
676
677         /* Catch ctrl+up/down so we can traverse messages we sent */
678         if ((event->state & GDK_CONTROL_MASK) && 
679             (event->keyval == GDK_Up || 
680              event->keyval == GDK_Down)) {
681                 GtkTextBuffer *buffer;
682                 const gchar   *str;
683
684                 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
685
686                 if (event->keyval == GDK_Up) {
687                         str = chat_sent_message_get_next (chat);
688                 } else {
689                         str = chat_sent_message_get_last (chat);
690                 }
691
692                 g_signal_handlers_block_by_func (buffer, 
693                                                  chat_input_text_buffer_changed_cb,
694                                                  chat);
695                 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
696                 g_signal_handlers_unblock_by_func (buffer, 
697                                                    chat_input_text_buffer_changed_cb,
698                                                    chat);
699
700                 return TRUE;    
701         }
702
703         /* Catch enter but not ctrl/shift-enter */
704         if (IS_ENTER (event->keyval) &&
705             !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
706                 GtkTextView *view;
707
708                 /* This is to make sure that kinput2 gets the enter. And if
709                  * it's handled there we shouldn't send on it. This is because
710                  * kinput2 uses Enter to commit letters. See:
711                  * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
712                  */
713
714                 view = GTK_TEXT_VIEW (chat->input_text_view);
715                 if (gtk_im_context_filter_keypress (view->im_context, event)) {
716                         GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
717                         return TRUE;
718                 }
719
720                 chat_input_text_view_send (chat);
721                 return TRUE;
722         }
723
724         text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
725
726         if (IS_ENTER (event->keyval) &&
727             (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
728                 /* Newline for shift/control-enter. */
729                 return FALSE;
730         }
731         if (!(event->state & GDK_CONTROL_MASK) &&
732             event->keyval == GDK_Page_Up) {
733                 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
734                 gtk_adjustment_set_value (adj, adj->value - adj->page_size);
735                 return TRUE;
736         }
737         if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
738             event->keyval == GDK_Page_Down) {
739                 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
740                 val = MIN (adj->value + adj->page_size, adj->upper - adj->page_size);
741                 gtk_adjustment_set_value (adj, val);
742                 return TRUE;
743         }
744         if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
745             event->keyval == GDK_Tab) {
746                 GtkTextBuffer *buffer;
747                 GtkTextIter    start, current;
748                 gchar         *nick, *completed;
749                 GList         *list, *completed_list;
750                 gboolean       is_start_of_buffer;
751
752                 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
753                 gtk_text_buffer_get_iter_at_mark (buffer, &current, gtk_text_buffer_get_insert (buffer));
754
755                 /* Get the start of the nick to complete. */
756                 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
757                 gtk_text_iter_backward_word_start (&start);
758                 is_start_of_buffer = gtk_text_iter_is_start (&start);
759
760                 list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
761                 g_completion_add_items (priv->completion, list);
762
763                 nick = gtk_text_buffer_get_text (buffer, &start, &current, FALSE);
764                 completed_list = g_completion_complete (priv->completion,
765                                                         nick,
766                                                         &completed);
767
768                 g_free (nick);
769
770                 if (completed) {
771                         guint        len;
772                         const gchar *text;
773                         gchar       *complete_char = NULL;
774
775                         gtk_text_buffer_delete (buffer, &start, &current);
776
777                         len = g_list_length (completed_list);
778
779                         if (len == 1) {
780                                 /* If we only have one hit, use that text
781                                  * instead of the text in completed since the
782                                  * completed text will use the typed string
783                                  * which might be cased all wrong.
784                                  * Fixes #120876
785                                  * */
786                                 text = empathy_contact_get_name (completed_list->data);
787                         } else {
788                                 text = completed;
789                         }
790
791                         gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
792
793                         if (len == 1 && is_start_of_buffer &&
794                             empathy_conf_get_string (empathy_conf_get (),
795                                                      EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR,
796                                                      &complete_char) &&
797                             complete_char != NULL) {
798                                 gtk_text_buffer_insert_at_cursor (buffer,
799                                                                   complete_char,
800                                                                   strlen (complete_char));
801                                 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
802                                 g_free (complete_char);
803                         }
804
805                         g_free (completed);
806                 }
807
808                 g_completion_clear_items (priv->completion);
809
810                 g_list_foreach (list, (GFunc) g_object_unref, NULL);
811                 g_list_free (list);
812
813                 return TRUE;
814         }
815
816         return FALSE;
817 }
818
819 static gboolean
820 chat_text_view_focus_in_event_cb (GtkWidget  *widget,
821                                   GdkEvent   *event,
822                                   EmpathyChat *chat)
823 {
824         gtk_widget_grab_focus (chat->input_text_view);
825
826         return TRUE;
827 }
828
829 static gboolean
830 chat_input_set_size_request_idle (gpointer sw)
831 {
832         gtk_widget_set_size_request (sw, -1, MAX_INPUT_HEIGHT);
833
834         return FALSE;
835 }
836
837 static void
838 chat_input_size_request_cb (GtkWidget      *widget,
839                             GtkRequisition *requisition,
840                             EmpathyChat    *chat)
841 {
842         EmpathyChatPriv *priv = GET_PRIV (chat);
843         GtkWidget       *sw;
844
845         sw = gtk_widget_get_parent (widget);
846         if (requisition->height >= MAX_INPUT_HEIGHT && !priv->has_input_vscroll) {
847                 g_idle_add (chat_input_set_size_request_idle, sw);
848                 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
849                                                 GTK_POLICY_NEVER,
850                                                 GTK_POLICY_ALWAYS);
851                 priv->has_input_vscroll = TRUE;
852         }
853
854         if (requisition->height < MAX_INPUT_HEIGHT && priv->has_input_vscroll) {
855                 gtk_widget_set_size_request (sw, -1, -1);
856                 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
857                                                 GTK_POLICY_NEVER,
858                                                 GTK_POLICY_NEVER);
859                 priv->has_input_vscroll = FALSE;
860         }
861 }
862
863 static void
864 chat_input_realize_cb (GtkWidget   *widget,
865                        EmpathyChat *chat)
866 {
867         DEBUG ("Setting focus to the input text view");
868         gtk_widget_grab_focus (widget);
869 }
870
871 static void
872 chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
873                                 EmpathySmiley        *smiley,
874                                 gpointer              user_data)
875 {
876         EmpathyChat   *chat = EMPATHY_CHAT (user_data);
877         GtkTextBuffer *buffer;
878         GtkTextIter    iter;
879
880         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
881
882         gtk_text_buffer_get_end_iter (buffer, &iter);
883         gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
884
885         gtk_text_buffer_get_end_iter (buffer, &iter);
886         gtk_text_buffer_insert (buffer, &iter, " ", -1);
887 }
888
889 typedef struct {
890         EmpathyChat  *chat;
891         gchar       *word;
892
893         GtkTextIter  start;
894         GtkTextIter  end;
895 } EmpathyChatSpell;
896
897 static EmpathyChatSpell *
898 chat_spell_new (EmpathyChat  *chat,
899                 const gchar *word,
900                 GtkTextIter  start,
901                 GtkTextIter  end)
902 {
903         EmpathyChatSpell *chat_spell;
904
905         chat_spell = g_slice_new0 (EmpathyChatSpell);
906
907         chat_spell->chat = g_object_ref (chat);
908         chat_spell->word = g_strdup (word);
909         chat_spell->start = start;
910         chat_spell->end = end;
911
912         return chat_spell;
913 }
914
915 static void
916 chat_spell_free (EmpathyChatSpell *chat_spell)
917 {
918         g_object_unref (chat_spell->chat);
919         g_free (chat_spell->word);
920         g_slice_free (EmpathyChatSpell, chat_spell);
921 }
922
923 static void
924 chat_text_check_word_spelling_cb (GtkMenuItem     *menuitem,
925                                   EmpathyChatSpell *chat_spell)
926 {
927         empathy_spell_dialog_show (chat_spell->chat,
928                                   &chat_spell->start,
929                                   &chat_spell->end,
930                                   chat_spell->word);
931 }
932
933 static void
934 chat_text_send_cb (GtkMenuItem *menuitem,
935                    EmpathyChat *chat)
936 {
937         chat_input_text_view_send (chat);
938 }
939
940 static void
941 chat_input_populate_popup_cb (GtkTextView *view,
942                               GtkMenu     *menu,
943                               EmpathyChat *chat)
944 {
945         EmpathyChatPriv      *priv;
946         GtkTextBuffer        *buffer;
947         GtkTextTagTable      *table;
948         GtkTextTag           *tag;
949         gint                  x, y;
950         GtkTextIter           iter, start, end;
951         GtkWidget            *item;
952         gchar                *str = NULL;
953         EmpathyChatSpell      *chat_spell;
954         EmpathySmileyManager *smiley_manager;
955         GtkWidget            *smiley_menu;
956         GtkWidget            *image;
957
958         priv = GET_PRIV (chat);
959         buffer = gtk_text_view_get_buffer (view);
960
961         /* Add the emoticon menu. */
962         item = gtk_separator_menu_item_new ();
963         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
964         gtk_widget_show (item);
965
966         item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
967         image = gtk_image_new_from_icon_name ("face-smile",
968                                               GTK_ICON_SIZE_MENU);
969         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
970         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
971         gtk_widget_show (item);
972
973         smiley_manager = empathy_smiley_manager_new ();
974         smiley_menu = empathy_smiley_menu_new (smiley_manager,
975                                                chat_insert_smiley_activate_cb,
976                                                chat);
977         gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
978         g_object_unref (smiley_manager);
979
980         /* Add the Send menu item. */
981         gtk_text_buffer_get_bounds (buffer, &start, &end);
982         str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
983         if (!G_STR_EMPTY (str)) {
984                 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
985                 g_signal_connect (G_OBJECT (item), "activate",
986                                   G_CALLBACK (chat_text_send_cb), chat);
987                 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
988                 gtk_widget_show (item);
989         }
990         str = NULL;
991
992         /* Add the spell check menu item. */
993         table = gtk_text_buffer_get_tag_table (buffer);
994         tag = gtk_text_tag_table_lookup (table, "misspelled");
995         gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
996         gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
997                                                GTK_TEXT_WINDOW_WIDGET,
998                                                x, y,
999                                                &x, &y);
1000         gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
1001         start = end = iter;
1002         if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
1003             gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
1004
1005                 str = gtk_text_buffer_get_text (buffer,
1006                                                 &start, &end, FALSE);
1007         }
1008         if (!G_STR_EMPTY (str)) {
1009                 chat_spell = chat_spell_new (chat, str, start, end);
1010                 g_object_set_data_full (G_OBJECT (menu),
1011                                         "chat_spell", chat_spell,
1012                                         (GDestroyNotify) chat_spell_free);
1013
1014                 item = gtk_separator_menu_item_new ();
1015                 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1016                 gtk_widget_show (item);
1017
1018                 item = gtk_image_menu_item_new_with_mnemonic (_("_Check Word Spelling..."));
1019                 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
1020                                                       GTK_ICON_SIZE_MENU);
1021                 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1022                 g_signal_connect (item,
1023                                   "activate",
1024                                   G_CALLBACK (chat_text_check_word_spelling_cb),
1025                                   chat_spell);
1026                 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1027                 gtk_widget_show (item);
1028         }
1029 }
1030
1031 static void
1032 chat_add_logs (EmpathyChat *chat)
1033 {
1034         EmpathyChatPriv *priv = GET_PRIV (chat);
1035         gboolean         is_chatroom;
1036         GList           *messages, *l;
1037         guint            num_messages;
1038         guint            i;
1039
1040         if (!priv->id) {
1041                 return;
1042         }
1043
1044         /* Turn off scrolling temporarily */
1045         empathy_chat_view_scroll (chat->view, FALSE);
1046
1047         /* Add messages from last conversation */
1048         is_chatroom = priv->handle_type == TP_HANDLE_TYPE_ROOM;
1049         messages = empathy_log_manager_get_last_messages (priv->log_manager,
1050                                                           priv->account,
1051                                                           priv->id,
1052                                                           is_chatroom);
1053         num_messages  = g_list_length (messages);
1054
1055         /* Only keep the 10 last messages */
1056         for (i = 0; num_messages - i > 10; i++) {
1057                 EmpathyMessage *message;
1058
1059                 message = messages->data;
1060                 messages = g_list_remove (messages, message);
1061                 g_object_unref (message);
1062         }
1063
1064         for (l = messages; l; l = l->next) {
1065                 empathy_chat_view_append_message (chat->view, l->data);
1066                 g_object_unref (l->data);
1067         }
1068         g_list_free (messages);
1069
1070         /* Turn back on scrolling */
1071         empathy_chat_view_scroll (chat->view, TRUE);
1072 }
1073
1074 static gint
1075 chat_contacts_completion_func (const gchar *s1,
1076                                const gchar *s2,
1077                                gsize        n)
1078 {
1079         gchar *tmp, *nick1, *nick2;
1080         gint   ret;
1081
1082         if (s1 == s2) {
1083                 return 0;
1084         }
1085         if (!s1 || !s2) {
1086                 return s1 ? -1 : +1;
1087         }
1088
1089         tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
1090         nick1 = g_utf8_casefold (tmp, -1);
1091         g_free (tmp);
1092
1093         tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
1094         nick2 = g_utf8_casefold (tmp, -1);
1095         g_free (tmp);
1096
1097         ret = strncmp (nick1, nick2, n);
1098
1099         g_free (nick1);
1100         g_free (nick2);
1101
1102         return ret;
1103 }
1104
1105 static void
1106 chat_members_changed_cb (EmpathyTpChat  *tp_chat,
1107                          EmpathyContact *contact,
1108                          EmpathyContact *actor,
1109                          guint           reason,
1110                          gchar          *message,
1111                          gboolean        is_member,
1112                          EmpathyChat    *chat)
1113 {
1114         EmpathyChatPriv *priv = GET_PRIV (chat);
1115
1116         if (priv->block_events_timeout_id == 0) {
1117                 gchar *str;
1118
1119                 empathy_contact_run_until_ready (contact,
1120                                                  EMPATHY_CONTACT_READY_NAME,
1121                                                  NULL);
1122
1123                 if (is_member) {
1124                         str = g_strdup_printf (_("%s has joined the room"),
1125                                                empathy_contact_get_name (contact));
1126                 } else {
1127                         str = g_strdup_printf (_("%s has left the room"),
1128                                                empathy_contact_get_name (contact));
1129                 }
1130                 empathy_chat_view_append_event (chat->view, str);
1131                 g_free (str);
1132         }
1133 }
1134
1135 static gboolean
1136 chat_reset_size_request (gpointer widget)
1137 {
1138         gtk_widget_set_size_request (widget, -1, -1);
1139
1140         return FALSE;
1141 }
1142
1143 static void
1144 chat_set_show_contacts (EmpathyChat *chat, gboolean show)
1145 {
1146         EmpathyChatPriv *priv = GET_PRIV (chat);
1147
1148         if (!priv->scrolled_window_contacts) {
1149                 return;
1150         }
1151
1152         if (show) {
1153                 EmpathyContactListStore *store;
1154                 gint                     min_width;
1155
1156                 /* We are adding the contact list to the chat, we don't want the
1157                  * chat view to become too small. If the chat view is already
1158                  * smaller than 250 make sure that size won't change. If the
1159                  * chat view is bigger the contact list will take some space on
1160                  * it but we make sure the chat view don't become smaller than
1161                  * 250. Relax the size request once the resize is done */
1162                 min_width = MIN (priv->vbox_left->allocation.width, 250);
1163                 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
1164                 g_idle_add (chat_reset_size_request, priv->vbox_left);
1165
1166                 if (priv->contacts_width > 0) {
1167                         gtk_paned_set_position (GTK_PANED (priv->hpaned),
1168                                                 priv->contacts_width);
1169                 }
1170
1171                 store = empathy_contact_list_store_new (EMPATHY_CONTACT_LIST (priv->tp_chat));
1172                 priv->contact_list_view = GTK_WIDGET (empathy_contact_list_view_new (store,
1173                         EMPATHY_CONTACT_LIST_FEATURE_NONE,
1174                         EMPATHY_CONTACT_FEATURE_CHAT |
1175                         EMPATHY_CONTACT_FEATURE_CALL |
1176                         EMPATHY_CONTACT_FEATURE_LOG |
1177                         EMPATHY_CONTACT_FEATURE_INFO));
1178                 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
1179                                    priv->contact_list_view);
1180                 gtk_widget_show (priv->contact_list_view);
1181                 gtk_widget_show (priv->scrolled_window_contacts);
1182                 g_object_unref (store);
1183         } else {
1184                 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
1185                 gtk_widget_hide (priv->scrolled_window_contacts);
1186                 if (priv->contact_list_view) {
1187                         gtk_widget_destroy (priv->contact_list_view);
1188                         priv->contact_list_view = NULL;
1189                 }
1190         }
1191 }
1192
1193 static void
1194 chat_remote_contact_changed_cb (EmpathyChat *chat)
1195 {
1196         EmpathyChatPriv *priv = GET_PRIV (chat);
1197
1198         if (priv->remote_contact) {
1199                 g_object_unref (priv->remote_contact);
1200                 priv->remote_contact = NULL;
1201         }
1202
1203         priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
1204         if (priv->remote_contact) {
1205                 g_object_ref (priv->remote_contact);
1206                 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
1207                 g_free (priv->id);
1208                 priv->id = g_strdup (empathy_contact_get_id (priv->remote_contact));
1209         }
1210         else if (priv->tp_chat) {
1211                 TpChannel *channel;
1212
1213                 channel = empathy_tp_chat_get_channel (priv->tp_chat);
1214                 g_object_get (channel, "handle-type", &priv->handle_type, NULL);
1215                 g_free (priv->id);
1216                 priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
1217         }
1218
1219         chat_set_show_contacts (chat, priv->remote_contact == NULL);
1220
1221         g_object_notify (G_OBJECT (chat), "remote-contact");
1222         g_object_notify (G_OBJECT (chat), "id");
1223 }
1224
1225 static void
1226 chat_destroy_cb (EmpathyTpChat *tp_chat,
1227                  EmpathyChat   *chat)
1228 {
1229         EmpathyChatPriv *priv;
1230
1231         priv = GET_PRIV (chat);
1232
1233         if (!priv->tp_chat) {
1234                 return;
1235         }
1236
1237         g_object_unref (priv->tp_chat);
1238         priv->tp_chat = NULL;
1239         g_object_notify (G_OBJECT (chat), "tp-chat");
1240
1241         empathy_chat_view_append_event (chat->view, _("Disconnected"));
1242         gtk_widget_set_sensitive (chat->input_text_view, FALSE);
1243         chat_set_show_contacts (chat, FALSE);
1244 }
1245
1246 static void
1247 chat_create_ui (EmpathyChat *chat)
1248 {
1249         EmpathyChatPriv *priv = GET_PRIV (chat);
1250         GladeXML        *glade;
1251         GList           *list = NULL; 
1252         gchar           *filename;
1253         GtkTextBuffer   *buffer;
1254
1255         filename = empathy_file_lookup ("empathy-chat.glade",
1256                                         "libempathy-gtk");
1257         glade = empathy_glade_get_file (filename,
1258                                         "chat_widget",
1259                                         NULL,
1260                                         "chat_widget", &priv->widget,
1261                                         "hpaned", &priv->hpaned,
1262                                         "vbox_left", &priv->vbox_left,
1263                                         "scrolled_window_chat", &priv->scrolled_window_chat,
1264                                         "scrolled_window_input", &priv->scrolled_window_input,
1265                                         "hbox_topic", &priv->hbox_topic,
1266                                         "label_topic", &priv->label_topic,
1267                                         "scrolled_window_contacts", &priv->scrolled_window_contacts,
1268                                         NULL);
1269         g_free (filename);
1270         g_object_unref (glade);
1271
1272         /* Add message view. */
1273         chat->view = empathy_theme_manager_create_view (empathy_theme_manager_get ());
1274         g_signal_connect (chat->view, "focus_in_event",
1275                           G_CALLBACK (chat_text_view_focus_in_event_cb),
1276                           chat);
1277         gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
1278                            GTK_WIDGET (chat->view));
1279         gtk_widget_show (GTK_WIDGET (chat->view));
1280
1281         /* Add input GtkTextView */
1282         chat->input_text_view = g_object_new (GTK_TYPE_TEXT_VIEW,
1283                                               "pixels-above-lines", 2,
1284                                               "pixels-below-lines", 2,
1285                                               "pixels-inside-wrap", 1,
1286                                               "right-margin", 2,
1287                                               "left-margin", 2,
1288                                               "wrap-mode", GTK_WRAP_WORD_CHAR,
1289                                               NULL);
1290         g_signal_connect (chat->input_text_view, "key-press-event",
1291                           G_CALLBACK (chat_input_key_press_event_cb),
1292                           chat);
1293         g_signal_connect (chat->input_text_view, "size-request",
1294                           G_CALLBACK (chat_input_size_request_cb),
1295                           chat);
1296         g_signal_connect (chat->input_text_view, "realize",
1297                           G_CALLBACK (chat_input_realize_cb),
1298                           chat);
1299         g_signal_connect (chat->input_text_view, "populate-popup",
1300                           G_CALLBACK (chat_input_populate_popup_cb),
1301                           chat);
1302         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1303         g_signal_connect (buffer, "changed",
1304                           G_CALLBACK (chat_input_text_buffer_changed_cb),
1305                           chat);
1306         gtk_text_buffer_create_tag (buffer, "misspelled",
1307                                     "underline", PANGO_UNDERLINE_ERROR,
1308                                     NULL);
1309         gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
1310                            chat->input_text_view);
1311         gtk_widget_show (chat->input_text_view);
1312
1313         /* Create contact list */
1314         chat_set_show_contacts (chat, priv->remote_contact == NULL);
1315
1316         /* Initialy hide the topic, will be shown if not empty */
1317         gtk_widget_hide (priv->hbox_topic);
1318
1319         /* Set widget focus order */
1320         list = g_list_append (NULL, priv->scrolled_window_input);
1321         gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
1322         g_list_free (list);
1323
1324         list = g_list_append (NULL, priv->vbox_left);
1325         list = g_list_append (list, priv->scrolled_window_contacts);
1326         gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
1327         g_list_free (list);
1328
1329         list = g_list_append (NULL, priv->hpaned);
1330         list = g_list_append (list, priv->hbox_topic);
1331         gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
1332         g_list_free (list);
1333
1334         /* Add the main widget in the chat widget */
1335         gtk_container_add (GTK_CONTAINER (chat), priv->widget);
1336 }
1337
1338 static void
1339 chat_size_request (GtkWidget      *widget,
1340                    GtkRequisition *requisition)
1341 {
1342   GtkBin *bin = GTK_BIN (widget);
1343
1344   requisition->width = GTK_CONTAINER (widget)->border_width * 2;
1345   requisition->height = GTK_CONTAINER (widget)->border_width * 2;
1346
1347   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
1348     {
1349       GtkRequisition child_requisition;
1350       
1351       gtk_widget_size_request (bin->child, &child_requisition);
1352
1353       requisition->width += child_requisition.width;
1354       requisition->height += child_requisition.height;
1355     }
1356 }
1357
1358 static void
1359 chat_size_allocate (GtkWidget     *widget,
1360                     GtkAllocation *allocation)
1361 {
1362   GtkBin *bin = GTK_BIN (widget);
1363   GtkAllocation child_allocation;
1364   
1365   widget->allocation = *allocation;
1366
1367   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
1368     {
1369       child_allocation.x = allocation->x + GTK_CONTAINER (widget)->border_width;
1370       child_allocation.y = allocation->y + GTK_CONTAINER (widget)->border_width;
1371       child_allocation.width = MAX (allocation->width - GTK_CONTAINER (widget)->border_width * 2, 0);
1372       child_allocation.height = MAX (allocation->height - GTK_CONTAINER (widget)->border_width * 2, 0);
1373
1374       gtk_widget_size_allocate (bin->child, &child_allocation);
1375     }
1376 }
1377
1378 static void
1379 chat_finalize (GObject *object)
1380 {
1381         EmpathyChat     *chat;
1382         EmpathyChatPriv *priv;
1383
1384         chat = EMPATHY_CHAT (object);
1385         priv = GET_PRIV (chat);
1386
1387         DEBUG ("Finalized: %p", object);
1388
1389         g_slist_foreach (priv->sent_messages, (GFunc) g_free, NULL);
1390         g_slist_free (priv->sent_messages);
1391
1392         g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
1393         g_list_free (priv->compositors);
1394
1395         chat_composing_remove_timeout (chat);
1396
1397         empathy_disconnect_account_status_changed (priv->token);
1398         g_object_unref (priv->mc);
1399         g_object_unref (priv->log_manager);
1400
1401         if (priv->tp_chat) {
1402                 g_object_unref (priv->tp_chat);
1403         }
1404         if (priv->account) {
1405                 g_object_unref (priv->account);
1406         }
1407         if (priv->remote_contact) {
1408                 g_object_unref (priv->remote_contact);
1409         }
1410
1411         if (priv->block_events_timeout_id) {
1412                 g_source_remove (priv->block_events_timeout_id);
1413         }
1414
1415         g_free (priv->id);
1416         g_free (priv->name);
1417         g_free (priv->subject);
1418
1419         G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
1420 }
1421
1422 static void
1423 chat_constructed (GObject *object)
1424 {
1425         EmpathyChat *chat = EMPATHY_CHAT (object);
1426
1427         chat_create_ui (chat);
1428         chat_add_logs (chat);
1429 }
1430
1431 static void
1432 empathy_chat_class_init (EmpathyChatClass *klass)
1433 {
1434         GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1435         GObjectClass   *object_class = G_OBJECT_CLASS (klass);
1436
1437         object_class->finalize = chat_finalize;
1438         object_class->get_property = chat_get_property;
1439         object_class->set_property = chat_set_property;
1440         object_class->constructed = chat_constructed;
1441
1442         widget_class->size_request = chat_size_request;
1443         widget_class->size_allocate = chat_size_allocate;
1444
1445         g_object_class_install_property (object_class,
1446                                          PROP_TP_CHAT,
1447                                          g_param_spec_object ("tp-chat",
1448                                                               "Empathy tp chat",
1449                                                               "The tp chat object",
1450                                                               EMPATHY_TYPE_TP_CHAT,
1451                                                               G_PARAM_CONSTRUCT |
1452                                                               G_PARAM_READWRITE));
1453         g_object_class_install_property (object_class,
1454                                          PROP_ACCOUNT,
1455                                          g_param_spec_object ("account",
1456                                                               "Account of the chat",
1457                                                               "The account of the chat",
1458                                                               MC_TYPE_ACCOUNT,
1459                                                               G_PARAM_READABLE));
1460         g_object_class_install_property (object_class,
1461                                          PROP_ID,
1462                                          g_param_spec_string ("id",
1463                                                               "Chat's id",
1464                                                               "The id of the chat",
1465                                                               NULL,
1466                                                               G_PARAM_READABLE));
1467         g_object_class_install_property (object_class,
1468                                          PROP_NAME,
1469                                          g_param_spec_string ("name",
1470                                                               "Chat's name",
1471                                                               "The name of the chat",
1472                                                               NULL,
1473                                                               G_PARAM_READABLE));
1474         g_object_class_install_property (object_class,
1475                                          PROP_SUBJECT,
1476                                          g_param_spec_string ("subject",
1477                                                               "Chat's subject",
1478                                                               "The subject or topic of the chat",
1479                                                               NULL,
1480                                                               G_PARAM_READABLE));
1481         g_object_class_install_property (object_class,
1482                                          PROP_REMOTE_CONTACT,
1483                                          g_param_spec_object ("remote-contact",
1484                                                               "The remote contact",
1485                                                               "The remote contact is any",
1486                                                               EMPATHY_TYPE_CONTACT,
1487                                                               G_PARAM_READABLE));
1488
1489         signals[COMPOSING] =
1490                 g_signal_new ("composing",
1491                               G_OBJECT_CLASS_TYPE (object_class),
1492                               G_SIGNAL_RUN_LAST,
1493                               0,
1494                               NULL, NULL,
1495                               g_cclosure_marshal_VOID__BOOLEAN,
1496                               G_TYPE_NONE,
1497                               1, G_TYPE_BOOLEAN);
1498
1499         signals[NEW_MESSAGE] =
1500                 g_signal_new ("new-message",
1501                               G_OBJECT_CLASS_TYPE (object_class),
1502                               G_SIGNAL_RUN_LAST,
1503                               0,
1504                               NULL, NULL,
1505                               g_cclosure_marshal_VOID__OBJECT,
1506                               G_TYPE_NONE,
1507                               1, EMPATHY_TYPE_MESSAGE);
1508
1509         g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
1510 }
1511
1512 static gboolean
1513 chat_block_events_timeout_cb (gpointer data)
1514 {
1515         EmpathyChatPriv *priv = GET_PRIV (data);
1516
1517         priv->block_events_timeout_id = 0;
1518
1519         return FALSE;
1520 }
1521
1522 static void
1523 empathy_chat_init (EmpathyChat *chat)
1524 {
1525         EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
1526                 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
1527
1528         chat->priv = priv;
1529         priv->log_manager = empathy_log_manager_new ();
1530         priv->contacts_width = -1;
1531         priv->sent_messages = NULL;
1532         priv->sent_messages_index = -1;
1533         priv->mc = empathy_mission_control_new ();
1534
1535         priv->token = empathy_connect_to_account_status_changed (priv->mc,
1536                                                    G_CALLBACK (chat_status_changed_cb),
1537                                                    chat, NULL);
1538
1539         /* Block events for some time to avoid having "has come online" or
1540          * "joined" messages. */
1541         priv->block_events_timeout_id =
1542                 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
1543
1544         /* Add nick name completion */
1545         priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_name);
1546         g_completion_set_compare (priv->completion, chat_contacts_completion_func);
1547 }
1548
1549 EmpathyChat *
1550 empathy_chat_new (EmpathyTpChat *tp_chat)
1551 {
1552         return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
1553 }
1554
1555 EmpathyTpChat *
1556 empathy_chat_get_tp_chat (EmpathyChat *chat)
1557 {
1558         EmpathyChatPriv *priv = GET_PRIV (chat);
1559
1560         g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1561
1562         return priv->tp_chat;
1563 }
1564
1565 void
1566 empathy_chat_set_tp_chat (EmpathyChat   *chat,
1567                           EmpathyTpChat *tp_chat)
1568 {
1569         EmpathyChatPriv *priv = GET_PRIV (chat);
1570
1571         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1572         g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
1573         g_return_if_fail (empathy_tp_chat_is_ready (tp_chat));
1574
1575         if (priv->tp_chat) {
1576                 return;
1577         }
1578
1579         if (priv->account) {
1580                 g_object_unref (priv->account);
1581         }
1582
1583         priv->tp_chat = g_object_ref (tp_chat);
1584         priv->account = g_object_ref (empathy_tp_chat_get_account (tp_chat));
1585
1586         g_signal_connect (tp_chat, "message-received",
1587                           G_CALLBACK (chat_message_received_cb),
1588                           chat);
1589         g_signal_connect (tp_chat, "send-error",
1590                           G_CALLBACK (chat_send_error_cb),
1591                           chat);
1592         g_signal_connect (tp_chat, "chat-state-changed",
1593                           G_CALLBACK (chat_state_changed_cb),
1594                           chat);
1595         g_signal_connect (tp_chat, "property-changed",
1596                           G_CALLBACK (chat_property_changed_cb),
1597                           chat);
1598         g_signal_connect (tp_chat, "members-changed",
1599                           G_CALLBACK (chat_members_changed_cb),
1600                           chat);
1601         g_signal_connect_swapped (tp_chat, "notify::remote-contact",
1602                                   G_CALLBACK (chat_remote_contact_changed_cb),
1603                                   chat);
1604         g_signal_connect (tp_chat, "destroy",
1605                           G_CALLBACK (chat_destroy_cb),
1606                           chat);
1607
1608         chat_remote_contact_changed_cb (chat);
1609
1610         if (chat->input_text_view) {
1611                 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
1612                 if (priv->block_events_timeout_id == 0) {
1613                         empathy_chat_view_append_event (chat->view, _("Connected"));
1614                 }
1615         }
1616
1617         empathy_tp_chat_set_acknowledge (priv->tp_chat, TRUE);
1618         empathy_tp_chat_emit_pendings (priv->tp_chat);
1619
1620         g_object_notify (G_OBJECT (chat), "tp-chat");
1621         g_object_notify (G_OBJECT (chat), "id");
1622         g_object_notify (G_OBJECT (chat), "account");
1623 }
1624
1625 McAccount *
1626 empathy_chat_get_account (EmpathyChat *chat)
1627 {
1628         EmpathyChatPriv *priv = GET_PRIV (chat);
1629
1630         g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1631
1632         return priv->account;
1633 }
1634
1635 const gchar *
1636 empathy_chat_get_id (EmpathyChat *chat)
1637 {
1638         EmpathyChatPriv *priv = GET_PRIV (chat);
1639
1640         g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1641
1642         return priv->id;
1643 }
1644
1645 const gchar *
1646 empathy_chat_get_name (EmpathyChat *chat)
1647 {
1648         EmpathyChatPriv *priv = GET_PRIV (chat);
1649         const gchar *ret;
1650
1651         g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1652
1653         ret = priv->name;
1654         if (!ret && priv->remote_contact) {
1655                 ret = empathy_contact_get_name (priv->remote_contact);
1656         }
1657
1658         if (!ret)
1659                 ret = priv->id;
1660
1661         return ret ? ret : _("Conversation");
1662 }
1663
1664 const gchar *
1665 empathy_chat_get_subject (EmpathyChat *chat)
1666 {
1667         EmpathyChatPriv *priv = GET_PRIV (chat);
1668
1669         g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1670
1671         return priv->subject;
1672 }
1673
1674 EmpathyContact *
1675 empathy_chat_get_remote_contact (EmpathyChat *chat)
1676 {
1677         EmpathyChatPriv *priv = GET_PRIV (chat);
1678
1679         g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1680
1681         return priv->remote_contact;
1682 }
1683
1684 guint
1685 empathy_chat_get_members_count (EmpathyChat *chat)
1686 {
1687         EmpathyChatPriv *priv = GET_PRIV (chat);
1688
1689         g_return_val_if_fail (EMPATHY_IS_CHAT (chat), 0);
1690
1691         if (priv->tp_chat) {
1692                 return empathy_tp_chat_get_members_count (priv->tp_chat);
1693         }
1694
1695         return 0;
1696 }
1697
1698 GtkWidget *
1699 empathy_chat_get_contact_menu (EmpathyChat *chat)
1700 {
1701         EmpathyChatPriv *priv = GET_PRIV (chat);
1702         GtkWidget       *menu = NULL;
1703
1704         g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1705
1706         if (priv->remote_contact) {
1707                 menu = empathy_contact_menu_new (priv->remote_contact,
1708                                                  EMPATHY_CONTACT_FEATURE_CALL |
1709                                                  EMPATHY_CONTACT_FEATURE_LOG |
1710                                                  EMPATHY_CONTACT_FEATURE_INFO);
1711         }
1712         else if (priv->contact_list_view) {
1713                 EmpathyContactListView *view;
1714
1715                 view = EMPATHY_CONTACT_LIST_VIEW (priv->contact_list_view);
1716                 menu = empathy_contact_list_view_get_contact_menu (view);
1717         }
1718
1719         return menu;
1720 }
1721
1722 void
1723 empathy_chat_clear (EmpathyChat *chat)
1724 {
1725         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1726
1727         empathy_chat_view_clear (chat->view);
1728 }
1729
1730 void
1731 empathy_chat_scroll_down (EmpathyChat *chat)
1732 {
1733         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1734
1735         empathy_chat_view_scroll_down (chat->view);
1736 }
1737
1738 void
1739 empathy_chat_cut (EmpathyChat *chat)
1740 {
1741         GtkTextBuffer *buffer;
1742
1743         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1744
1745         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1746         if (gtk_text_buffer_get_has_selection (buffer)) {
1747                 GtkClipboard *clipboard;
1748
1749                 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1750
1751                 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
1752         }
1753 }
1754
1755 void
1756 empathy_chat_copy (EmpathyChat *chat)
1757 {
1758         GtkTextBuffer *buffer;
1759
1760         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1761
1762         if (empathy_chat_view_get_has_selection (chat->view)) {
1763                 empathy_chat_view_copy_clipboard (chat->view);
1764                 return;
1765         }
1766
1767         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1768         if (gtk_text_buffer_get_has_selection (buffer)) {
1769                 GtkClipboard *clipboard;
1770
1771                 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1772
1773                 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1774         }
1775 }
1776
1777 void
1778 empathy_chat_paste (EmpathyChat *chat)
1779 {
1780         GtkTextBuffer *buffer;
1781         GtkClipboard  *clipboard;
1782
1783         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1784
1785         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1786         clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1787
1788         gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
1789 }
1790
1791 void
1792 empathy_chat_correct_word (EmpathyChat  *chat,
1793                           GtkTextIter *start,
1794                           GtkTextIter *end,
1795                           const gchar *new_word)
1796 {
1797         GtkTextBuffer *buffer;
1798
1799         g_return_if_fail (chat != NULL);
1800         g_return_if_fail (new_word != NULL);
1801
1802         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1803
1804         gtk_text_buffer_delete (buffer, start, end);
1805         gtk_text_buffer_insert (buffer, start,
1806                                 new_word,
1807                                 -1);
1808 }
1809
1810 gboolean
1811 empathy_chat_is_room (EmpathyChat *chat)
1812 {
1813         EmpathyChatPriv *priv = GET_PRIV (chat);
1814
1815         g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
1816
1817         return (priv->handle_type == TP_HANDLE_TYPE_ROOM);
1818 }
1819