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