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