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