]> git.0d.be Git - empathy.git/blob - libempathy-gtk/gossip-chat.c
179f8f40986e8b55828c853059e566fd9a19e5fc
[empathy.git] / libempathy-gtk / gossip-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 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 #include <glade/glade.h>
37
38 #include <libempathy/empathy-contact-manager.h>
39 #include <libempathy/gossip-debug.h>
40 #include <libempathy/gossip-utils.h>
41 #include <libempathy/gossip-conf.h>
42 #include <libempathy/empathy-marshal.h>
43
44 #include "gossip-chat.h"
45 #include "gossip-chat-window.h"
46 #include "gossip-geometry.h"
47 #include "gossip-preferences.h"
48 #include "gossip-spell.h"
49 //#include "gossip-spell-dialog.h"
50 #include "gossip-ui-utils.h"
51
52 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GOSSIP_TYPE_CHAT, GossipChatPriv))
53
54 #define DEBUG_DOMAIN "Chat"
55
56 #define CHAT_DIR_CREATE_MODE  (S_IRUSR | S_IWUSR | S_IXUSR)
57 #define CHAT_FILE_CREATE_MODE (S_IRUSR | S_IWUSR)
58
59 #define IS_ENTER(v) (v == GDK_Return || v == GDK_ISO_Enter || v == GDK_KP_Enter)
60
61 #define MAX_INPUT_HEIGHT 150
62
63 #define COMPOSING_STOP_TIMEOUT 5
64
65 struct _GossipChatPriv {
66         EmpathyContactManager *manager;
67         EmpathyTpChat         *tp_chat;
68         GossipChatWindow      *window;
69         GtkTooltips           *tooltips;
70         guint                  composing_stop_timeout_id;
71         gboolean               sensitive;
72         gchar                 *id;
73         GSList                *sent_messages;
74         gint                   sent_messages_index;
75         GList                 *compositors;
76         /* Used to automatically shrink a window that has temporarily
77          * grown due to long input. 
78          */
79         gint                   padding_height;
80         gint                   default_window_height;
81         gint                   last_input_height;
82         gboolean               vscroll_visible;
83 };
84
85 typedef struct {
86         GossipChat  *chat;
87         gchar       *word;
88
89         GtkTextIter  start;
90         GtkTextIter  end;
91 } GossipChatSpell;
92
93 static void             gossip_chat_class_init            (GossipChatClass *klass);
94 static void             gossip_chat_init                  (GossipChat      *chat);
95 static void             chat_finalize                     (GObject         *object);
96 static void             chat_destroy_cb                   (EmpathyTpChat   *tp_chat,
97                                                            GossipChat      *chat);
98 static void             chat_send                         (GossipChat      *chat,
99                                                            const gchar     *msg);
100 static void             chat_input_text_view_send         (GossipChat      *chat);
101 static void             chat_message_received_cb          (EmpathyTpChat   *tp_chat,
102                                                            GossipMessage   *message,
103                                                            GossipChat      *chat);
104 void                    chat_sent_message_add             (GossipChat      *chat,
105                                                            const gchar     *str);
106 const gchar *           chat_sent_message_get_next        (GossipChat      *chat);
107 const gchar *           chat_sent_message_get_last        (GossipChat      *chat);
108 static gboolean         chat_input_key_press_event_cb     (GtkWidget       *widget,
109                                                            GdkEventKey     *event,
110                                                            GossipChat      *chat);
111 static void             chat_input_text_buffer_changed_cb (GtkTextBuffer   *buffer,
112                                                            GossipChat      *chat);
113 static gboolean         chat_text_view_focus_in_event_cb  (GtkWidget       *widget,
114                                                            GdkEvent        *event,
115                                                            GossipChat      *chat);
116 static void             chat_text_view_scroll_hide_cb     (GtkWidget       *widget,
117                                                            GossipChat      *chat);
118 static void             chat_text_view_size_allocate_cb   (GtkWidget       *widget,
119                                                            GtkAllocation   *allocation,
120                                                            GossipChat      *chat);
121 static void             chat_text_view_realize_cb         (GtkWidget       *widget,
122                                                            GossipChat      *chat);
123 static void             chat_text_populate_popup_cb       (GtkTextView     *view,
124                                                            GtkMenu         *menu,
125                                                            GossipChat      *chat);
126 static void             chat_text_check_word_spelling_cb  (GtkMenuItem     *menuitem,
127                                                            GossipChatSpell *chat_spell);
128 static GossipChatSpell *chat_spell_new                    (GossipChat      *chat,
129                                                            const gchar     *word,
130                                                            GtkTextIter      start,
131                                                            GtkTextIter      end);
132 static void             chat_spell_free                   (GossipChatSpell *chat_spell);
133 static void             chat_composing_start              (GossipChat      *chat);
134 static void             chat_composing_stop               (GossipChat      *chat);
135 static void             chat_composing_remove_timeout     (GossipChat      *chat);
136 static gboolean         chat_composing_stop_timeout_cb    (GossipChat      *chat);
137 static void             chat_state_changed_cb             (EmpathyTpChat   *tp_chat,
138                                                            GossipContact   *contact,
139                                                            TelepathyChannelChatState  state,
140                                                            GossipChat      *chat);
141
142 enum {
143         COMPOSING,
144         NEW_MESSAGE,
145         NAME_CHANGED,
146         STATUS_CHANGED,
147         LAST_SIGNAL
148 };
149
150 static guint signals[LAST_SIGNAL] = { 0 };
151
152 G_DEFINE_TYPE (GossipChat, gossip_chat, G_TYPE_OBJECT);
153
154 static void
155 gossip_chat_class_init (GossipChatClass *klass)
156 {
157         GObjectClass *object_class;
158
159         object_class = G_OBJECT_CLASS (klass);
160
161         object_class->finalize = chat_finalize;
162
163         signals[COMPOSING] =
164                 g_signal_new ("composing",
165                               G_OBJECT_CLASS_TYPE (object_class),
166                               G_SIGNAL_RUN_LAST,
167                               0,
168                               NULL, NULL,
169                               g_cclosure_marshal_VOID__BOOLEAN,
170                               G_TYPE_NONE,
171                               1, G_TYPE_BOOLEAN);
172
173         signals[NEW_MESSAGE] =
174                 g_signal_new ("new-message",
175                               G_OBJECT_CLASS_TYPE (object_class),
176                               G_SIGNAL_RUN_LAST,
177                               0,
178                               NULL, NULL,
179                               empathy_marshal_VOID__OBJECT_BOOLEAN,
180                               G_TYPE_NONE,
181                               2, GOSSIP_TYPE_MESSAGE, G_TYPE_BOOLEAN);
182
183         signals[NAME_CHANGED] =
184                 g_signal_new ("name-changed",
185                               G_OBJECT_CLASS_TYPE (object_class),
186                               G_SIGNAL_RUN_LAST,
187                               0,
188                               NULL, NULL,
189                               g_cclosure_marshal_VOID__POINTER,
190                               G_TYPE_NONE,
191                               1, G_TYPE_POINTER);
192
193         signals[STATUS_CHANGED] =
194                 g_signal_new ("status-changed",
195                               G_OBJECT_CLASS_TYPE (object_class),
196                               G_SIGNAL_RUN_LAST,
197                               0,
198                               NULL, NULL,
199                               g_cclosure_marshal_VOID__VOID,
200                               G_TYPE_NONE,
201                               0);
202
203         g_type_class_add_private (object_class, sizeof (GossipChatPriv));
204 }
205
206 static void
207 gossip_chat_init (GossipChat *chat)
208 {
209         GossipChatPriv *priv;
210         GtkTextBuffer  *buffer;
211
212         chat->view = gossip_chat_view_new ();
213         chat->input_text_view = gtk_text_view_new ();
214
215         chat->is_first_char = TRUE;
216
217         g_object_set (chat->input_text_view,
218                       "pixels-above-lines", 2,
219                       "pixels-below-lines", 2,
220                       "pixels-inside-wrap", 1,
221                       "right-margin", 2,
222                       "left-margin", 2,
223                       "wrap-mode", GTK_WRAP_WORD_CHAR,
224                       NULL);
225
226         priv = GET_PRIV (chat);
227
228         priv->manager = empathy_contact_manager_new ();
229         priv->tooltips = gtk_tooltips_new ();
230         priv->default_window_height = -1;
231         priv->vscroll_visible = FALSE;
232         priv->sensitive = TRUE;
233         priv->sent_messages = NULL;
234         priv->sent_messages_index = -1;
235
236         g_signal_connect (chat->input_text_view,
237                           "key_press_event",
238                           G_CALLBACK (chat_input_key_press_event_cb),
239                           chat);
240
241         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
242         g_signal_connect (buffer,
243                           "changed",
244                           G_CALLBACK (chat_input_text_buffer_changed_cb),
245                           chat);
246         g_signal_connect (chat->view,
247                           "focus_in_event",
248                           G_CALLBACK (chat_text_view_focus_in_event_cb),
249                           chat);
250
251         g_signal_connect (chat->input_text_view,
252                           "size_allocate",
253                           G_CALLBACK (chat_text_view_size_allocate_cb),
254                           chat);
255
256         g_signal_connect (chat->input_text_view,
257                           "realize",
258                           G_CALLBACK (chat_text_view_realize_cb),
259                           chat);
260
261         g_signal_connect (GTK_TEXT_VIEW (chat->input_text_view),
262                           "populate_popup",
263                           G_CALLBACK (chat_text_populate_popup_cb),
264                           chat);
265
266         /* create misspelt words identification tag */
267         gtk_text_buffer_create_tag (buffer,
268                                     "misspelled",
269                                     "underline", PANGO_UNDERLINE_ERROR,
270                                     NULL);
271 }
272
273 static void
274 chat_finalize (GObject *object)
275 {
276         GossipChat     *chat;
277         GossipChatPriv *priv;
278
279         chat = GOSSIP_CHAT (object);
280         priv = GET_PRIV (chat);
281
282         gossip_debug (DEBUG_DOMAIN, "Finalized: %p", object);
283
284         g_slist_foreach (priv->sent_messages, (GFunc) g_free, NULL);
285         g_slist_free (priv->sent_messages);
286
287         g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
288         g_list_free (priv->compositors);
289
290         chat_composing_remove_timeout (chat);
291         g_object_unref (chat->account);
292         g_object_unref (priv->manager);
293
294         if (priv->tp_chat) {
295                 g_object_unref (priv->tp_chat);
296         }
297
298         g_free (priv->id);
299
300         G_OBJECT_CLASS (gossip_chat_parent_class)->finalize (object);
301 }
302
303 static void
304 chat_destroy_cb (EmpathyTpChat *tp_chat,
305                  GossipChat    *chat)
306 {
307         GossipChatPriv *priv;
308         GtkWidget      *widget;
309
310         priv = GET_PRIV (chat);
311
312         if (priv->tp_chat) {
313                 g_object_unref (priv->tp_chat);
314                 priv->tp_chat = NULL;
315         }
316
317         gossip_chat_view_append_event (chat->view, _("Disconnected"));
318
319         widget = gossip_chat_get_widget (chat);
320         gtk_widget_set_sensitive (widget, FALSE);
321         priv->sensitive = FALSE;
322 }
323
324 static void
325 chat_send (GossipChat  *chat,
326            const gchar *msg)
327 {
328         GossipChatPriv   *priv;
329         //GossipLogManager *log_manager;
330         GossipMessage    *message;
331         GossipContact    *own_contact;
332
333         priv = GET_PRIV (chat);
334
335         if (G_STR_EMPTY (msg)) {
336                 return;
337         }
338
339         chat_sent_message_add (chat, msg);
340
341         if (g_str_has_prefix (msg, "/clear")) {
342                 gossip_chat_view_clear (chat->view);
343                 return;
344         }
345
346         /* FIXME: gossip_app_set_not_away ();*/
347
348         own_contact = gossip_chat_get_own_contact (chat);
349         message = gossip_message_new (msg);
350         gossip_message_set_sender (message, own_contact);
351
352         //FIXME: log_manager = gossip_session_get_log_manager (gossip_app_get_session ());
353         //gossip_log_message_for_contact (log_manager, message, FALSE);
354
355         empathy_tp_chat_send (priv->tp_chat, message);
356
357         g_object_unref (message);
358 }
359
360 static void
361 chat_input_text_view_send (GossipChat *chat)
362 {
363         GossipChatPriv *priv;
364         GtkTextBuffer  *buffer;
365         GtkTextIter     start, end;
366         gchar          *msg;
367
368         priv = GET_PRIV (chat);
369
370         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
371
372         gtk_text_buffer_get_bounds (buffer, &start, &end);
373         msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
374
375         /* clear the input field */
376         gtk_text_buffer_set_text (buffer, "", -1);
377
378         chat_send (chat, msg);
379
380         g_free (msg);
381
382         chat->is_first_char = TRUE;
383 }
384
385 static void
386 chat_message_received_cb (EmpathyTpChat *tp_chat,
387                           GossipMessage *message,
388                           GossipChat    *chat)
389 {
390         GossipChatPriv *priv;
391         //GossipLogManager      *log_manager;
392         GossipContact         *sender;
393
394         priv = GET_PRIV (chat);
395
396         sender = gossip_message_get_sender (message);
397         gossip_debug (DEBUG_DOMAIN, "Appending message ('%s')",
398                       gossip_contact_get_name (sender));
399
400 /*FIXME:
401         log_manager = gossip_session_get_log_manager (gossip_app_get_session ());
402         gossip_log_message_for_contact (log_manager, message, TRUE);
403 */
404         gossip_chat_view_append_message (chat->view, message);
405
406         if (gossip_chat_should_play_sound (chat)) {
407                 // FIXME: gossip_sound_play (GOSSIP_SOUND_CHAT);
408         }
409
410         g_signal_emit (chat, signals[NEW_MESSAGE], 0, message, FALSE);
411 }
412
413 void 
414 chat_sent_message_add (GossipChat  *chat,
415                        const gchar *str)
416 {
417         GossipChatPriv *priv;
418         GSList         *list;
419         GSList         *item;
420
421         priv = GET_PRIV (chat);
422
423         /* Save the sent message in our repeat buffer */
424         list = priv->sent_messages;
425         
426         /* Remove any other occurances of this msg */
427         while ((item = g_slist_find_custom (list, str, (GCompareFunc) strcmp)) != NULL) {
428                 list = g_slist_remove_link (list, item);
429                 g_free (item->data);
430                 g_slist_free1 (item);
431         }
432
433         /* Trim the list to the last 10 items */
434         while (g_slist_length (list) > 10) {
435                 item = g_slist_last (list);
436                 if (item) {
437                         list = g_slist_remove_link (list, item);
438                         g_free (item->data);
439                         g_slist_free1 (item);
440                 }
441         }
442
443         /* Add new message */
444         list = g_slist_prepend (list, g_strdup (str));
445
446         /* Set list and reset the index */
447         priv->sent_messages = list;
448         priv->sent_messages_index = -1;
449 }
450
451 const gchar *
452 chat_sent_message_get_next (GossipChat *chat)
453 {
454         GossipChatPriv *priv;
455         gint            max;
456         
457         priv = GET_PRIV (chat);
458
459         if (!priv->sent_messages) {
460                 gossip_debug (DEBUG_DOMAIN, 
461                               "No sent messages, next message is NULL");
462                 return NULL;
463         }
464
465         max = g_slist_length (priv->sent_messages) - 1;
466
467         if (priv->sent_messages_index < max) {
468                 priv->sent_messages_index++;
469         }
470         
471         gossip_debug (DEBUG_DOMAIN, 
472                       "Returning next message index:%d",
473                       priv->sent_messages_index);
474
475         return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
476 }
477
478 const gchar *
479 chat_sent_message_get_last (GossipChat *chat)
480 {
481         GossipChatPriv *priv;
482
483         g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
484
485         priv = GET_PRIV (chat);
486         
487         if (!priv->sent_messages) {
488                 gossip_debug (DEBUG_DOMAIN, 
489                               "No sent messages, last message is NULL");
490                 return NULL;
491         }
492
493         if (priv->sent_messages_index >= 0) {
494                 priv->sent_messages_index--;
495         }
496
497         gossip_debug (DEBUG_DOMAIN, 
498                       "Returning last message index:%d",
499                       priv->sent_messages_index);
500
501         return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
502 }
503
504 static gboolean
505 chat_input_key_press_event_cb (GtkWidget   *widget,
506                                GdkEventKey *event,
507                                GossipChat  *chat)
508 {
509         GossipChatPriv *priv;
510         GtkAdjustment  *adj;
511         gdouble         val;
512         GtkWidget      *text_view_sw;
513
514         priv = GET_PRIV (chat);
515
516         if (event->keyval == GDK_Tab && !(event->state & GDK_CONTROL_MASK)) {
517                 return TRUE;
518         }
519
520         /* Catch ctrl+up/down so we can traverse messages we sent */
521         if ((event->state & GDK_CONTROL_MASK) && 
522             (event->keyval == GDK_Up || 
523              event->keyval == GDK_Down)) {
524                 GtkTextBuffer *buffer;
525                 const gchar   *str;
526
527                 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
528
529                 if (event->keyval == GDK_Up) {
530                         str = chat_sent_message_get_next (chat);
531                 } else {
532                         str = chat_sent_message_get_last (chat);
533                 }
534
535                 g_signal_handlers_block_by_func (buffer, 
536                                                  chat_input_text_buffer_changed_cb,
537                                                  chat);
538                 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
539                 g_signal_handlers_unblock_by_func (buffer, 
540                                                    chat_input_text_buffer_changed_cb,
541                                                    chat);
542
543                 return TRUE;    
544         }
545
546         /* Catch enter but not ctrl/shift-enter */
547         if (IS_ENTER (event->keyval) && !(event->state & GDK_SHIFT_MASK)) {
548                 GtkTextView *view;
549
550                 /* This is to make sure that kinput2 gets the enter. And if
551                  * it's handled there we shouldn't send on it. This is because
552                  * kinput2 uses Enter to commit letters. See:
553                  * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
554                  */
555
556                 view = GTK_TEXT_VIEW (chat->input_text_view);
557                 if (gtk_im_context_filter_keypress (view->im_context, event)) {
558                         GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
559                         return TRUE;
560                 }
561
562                 chat_input_text_view_send (chat);
563                 return TRUE;
564         }
565
566         text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
567
568         if (IS_ENTER (event->keyval) && (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
569                 /* Newline for shift-enter. */
570                 return FALSE;
571         }
572         else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
573                  event->keyval == GDK_Page_Up) {
574                 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
575                 gtk_adjustment_set_value (adj, adj->value - adj->page_size);
576
577                 return TRUE;
578         }
579         else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
580                  event->keyval == GDK_Page_Down) {
581                 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
582                 val = MIN (adj->value + adj->page_size, adj->upper - adj->page_size);
583                 gtk_adjustment_set_value (adj, val);
584
585                 return TRUE;
586         }
587
588         return FALSE;
589 }
590
591 static gboolean
592 chat_text_view_focus_in_event_cb (GtkWidget  *widget,
593                                   GdkEvent   *event,
594                                   GossipChat *chat)
595 {
596         gtk_widget_grab_focus (chat->input_text_view);
597
598         return TRUE;
599 }
600
601 static void
602 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
603                                    GossipChat    *chat)
604 {
605         GossipChatPriv *priv;
606         GtkTextIter     start, end;
607         gchar          *str;
608         gboolean        spell_checker = FALSE;
609
610         priv = GET_PRIV (chat);
611
612         if (gtk_text_buffer_get_char_count (buffer) == 0) {
613                 chat_composing_stop (chat);
614         } else {
615                 chat_composing_start (chat);
616         }
617
618         gossip_conf_get_bool (gossip_conf_get (),
619                               GOSSIP_PREFS_CHAT_SPELL_CHECKER_ENABLED,
620                               &spell_checker);
621
622         if (chat->is_first_char) {
623                 GtkRequisition  req;
624                 gint            window_height;
625                 GtkWidget      *dialog;
626                 GtkAllocation  *allocation;
627
628                 /* Save the window's size */
629                 dialog = gossip_chat_window_get_dialog (priv->window);
630                 gtk_window_get_size (GTK_WINDOW (dialog),
631                                      NULL, &window_height);
632
633                 gtk_widget_size_request (chat->input_text_view, &req);
634
635                 allocation = &GTK_WIDGET (chat->view)->allocation;
636
637                 priv->default_window_height = window_height;
638                 priv->last_input_height = req.height;
639                 priv->padding_height = window_height - req.height - allocation->height;
640
641                 chat->is_first_char = FALSE;
642         }
643
644         gtk_text_buffer_get_start_iter (buffer, &start);
645
646         if (!spell_checker) {
647                 gtk_text_buffer_get_end_iter (buffer, &end);
648                 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
649                 return;
650         }
651
652         if (!gossip_spell_supported ()) {
653                 return;
654         }
655
656         /* NOTE: this is really inefficient, we shouldn't have to
657            reiterate the whole buffer each time and check each work
658            every time. */
659         while (TRUE) {
660                 gboolean correct = FALSE;
661
662                 /* if at start */
663                 if (gtk_text_iter_is_start (&start)) {
664                         end = start;
665
666                         if (!gtk_text_iter_forward_word_end (&end)) {
667                                 /* no whole word yet */
668                                 break;
669                         }
670                 } else {
671                         if (!gtk_text_iter_forward_word_end (&end)) {
672                                 /* must be the end of the buffer */
673                                 break;
674                         }
675
676                         start = end;
677                         gtk_text_iter_backward_word_start (&start);
678                 }
679
680                 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
681
682                 /* spell check string */
683                 if (!gossip_chat_get_is_command (str)) {
684                         correct = gossip_spell_check (str);
685                 } else {
686                         correct = TRUE;
687                 }
688
689                 if (!correct) {
690                         gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
691                 } else {
692                         gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
693                 }
694
695                 g_free (str);
696
697                 /* set start iter to the end iters position */
698                 start = end;
699         }
700 }
701
702 typedef struct {
703         GtkWidget *window;
704         gint       width;
705         gint       height;
706 } ChangeSizeData;
707
708 static gboolean
709 chat_change_size_in_idle_cb (ChangeSizeData *data)
710 {
711         gtk_window_resize (GTK_WINDOW (data->window),
712                            data->width, data->height);
713
714         return FALSE;
715 }
716
717 static void
718 chat_text_view_scroll_hide_cb (GtkWidget  *widget,
719                                GossipChat *chat)
720 {
721         GossipChatPriv *priv;
722         GtkWidget      *sw;
723
724         priv = GET_PRIV (chat);
725
726         priv->vscroll_visible = FALSE;
727         g_signal_handlers_disconnect_by_func (widget, chat_text_view_scroll_hide_cb, chat);
728
729         sw = gtk_widget_get_parent (chat->input_text_view);
730         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
731                                         GTK_POLICY_NEVER,
732                                         GTK_POLICY_NEVER);
733         g_object_set (sw, "height-request", -1, NULL);
734 }
735
736 static void
737 chat_text_view_size_allocate_cb (GtkWidget     *widget,
738                                  GtkAllocation *allocation,
739                                  GossipChat    *chat)
740 {
741         GossipChatPriv *priv;
742         gint            width;
743         GtkWidget      *dialog;
744         ChangeSizeData *data;
745         gint            window_height;
746         gint            new_height;
747         GtkAllocation  *view_allocation;
748         gint            current_height;
749         gint            diff;
750         GtkWidget      *sw;
751
752         priv = GET_PRIV (chat);
753
754         if (priv->default_window_height <= 0) {
755                 return;
756         }
757
758         sw = gtk_widget_get_parent (widget);
759         if (sw->allocation.height >= MAX_INPUT_HEIGHT && !priv->vscroll_visible) {
760                 GtkWidget *vscroll;
761
762                 priv->vscroll_visible = TRUE;
763                 gtk_widget_set_size_request (sw, sw->allocation.width, MAX_INPUT_HEIGHT);
764                 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
765                                                 GTK_POLICY_NEVER,
766                                                 GTK_POLICY_AUTOMATIC);
767                 vscroll = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (sw));
768                 g_signal_connect (vscroll, "hide",
769                                   G_CALLBACK (chat_text_view_scroll_hide_cb),
770                                   chat);
771         }
772
773         if (priv->last_input_height <= allocation->height) {
774                 priv->last_input_height = allocation->height;
775                 return;
776         }
777
778         diff = priv->last_input_height - allocation->height;
779         priv->last_input_height = allocation->height;
780
781         view_allocation = &GTK_WIDGET (chat->view)->allocation;
782
783         dialog = gossip_chat_window_get_dialog (priv->window);
784         gtk_window_get_size (GTK_WINDOW (dialog), NULL, &current_height);
785
786         new_height = view_allocation->height + priv->padding_height + allocation->height - diff;
787
788         if (new_height <= priv->default_window_height) {
789                 window_height = priv->default_window_height;
790         } else {
791                 window_height = new_height;
792         }
793
794         if (current_height <= window_height) {
795                 return;
796         }
797
798         /* Restore the window's size */
799         gtk_window_get_size (GTK_WINDOW (dialog), &width, NULL);
800
801         data = g_new0 (ChangeSizeData, 1);
802         data->window = dialog;
803         data->width  = width;
804         data->height = window_height;
805
806         g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
807                          (GSourceFunc) chat_change_size_in_idle_cb,
808                          data, g_free);
809 }
810
811 static void
812 chat_text_view_realize_cb (GtkWidget  *widget,
813                            GossipChat *chat)
814 {
815         gossip_debug (DEBUG_DOMAIN, "Setting focus to the input text view");
816         gtk_widget_grab_focus (widget);
817 }
818
819 static void
820 chat_insert_smiley_activate_cb (GtkWidget  *menuitem,
821                                 GossipChat *chat)
822 {
823         GtkTextBuffer *buffer;
824         GtkTextIter    iter;
825         const gchar   *smiley;
826
827         smiley = g_object_get_data (G_OBJECT (menuitem), "smiley_text");
828
829         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
830
831         gtk_text_buffer_get_end_iter (buffer, &iter);
832         gtk_text_buffer_insert (buffer, &iter, smiley, -1);
833
834         gtk_text_buffer_get_end_iter (buffer, &iter);
835         gtk_text_buffer_insert (buffer, &iter, " ", -1);
836 }
837
838 static void
839 chat_text_populate_popup_cb (GtkTextView *view,
840                              GtkMenu     *menu,
841                              GossipChat  *chat)
842 {
843         GossipChatPriv  *priv;
844         GtkTextBuffer   *buffer;
845         GtkTextTagTable *table;
846         GtkTextTag      *tag;
847         gint             x, y;
848         GtkTextIter      iter, start, end;
849         GtkWidget       *item;
850         gchar           *str = NULL;
851         GossipChatSpell *chat_spell;
852         GtkWidget       *smiley_menu;
853
854         priv = GET_PRIV (chat);
855
856         /* Add the emoticon menu. */
857         item = gtk_separator_menu_item_new ();
858         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
859         gtk_widget_show (item);
860
861         item = gtk_menu_item_new_with_mnemonic (_("Insert Smiley"));
862         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
863         gtk_widget_show (item);
864
865         smiley_menu = gossip_chat_view_get_smiley_menu (
866                 G_CALLBACK (chat_insert_smiley_activate_cb),
867                 chat,
868                 priv->tooltips);
869         gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
870
871         /* Add the spell check menu item. */
872         buffer = gtk_text_view_get_buffer (view);
873         table = gtk_text_buffer_get_tag_table (buffer);
874
875         tag = gtk_text_tag_table_lookup (table, "misspelled");
876
877         gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
878
879         gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
880                                                GTK_TEXT_WINDOW_WIDGET,
881                                                x, y,
882                                                &x, &y);
883
884         gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
885
886         start = end = iter;
887
888         if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
889             gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
890
891                 str = gtk_text_buffer_get_text (buffer,
892                                                 &start, &end, FALSE);
893         }
894
895         if (G_STR_EMPTY (str)) {
896                 return;
897         }
898
899         chat_spell = chat_spell_new (chat, str, start, end);
900
901         g_object_set_data_full (G_OBJECT (menu),
902                                 "chat_spell", chat_spell,
903                                 (GDestroyNotify) chat_spell_free);
904
905         item = gtk_separator_menu_item_new ();
906         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
907         gtk_widget_show (item);
908
909         item = gtk_menu_item_new_with_mnemonic (_("_Check Word Spelling..."));
910         g_signal_connect (item,
911                           "activate",
912                           G_CALLBACK (chat_text_check_word_spelling_cb),
913                           chat_spell);
914         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
915         gtk_widget_show (item);
916 }
917
918 static void
919 chat_text_check_word_spelling_cb (GtkMenuItem     *menuitem,
920                                   GossipChatSpell *chat_spell)
921 {
922 /*FIXME:        gossip_spell_dialog_show (chat_spell->chat,
923                                   chat_spell->start,
924                                   chat_spell->end,
925                                   chat_spell->word);*/
926 }
927
928 static GossipChatSpell *
929 chat_spell_new (GossipChat  *chat,
930                 const gchar *word,
931                 GtkTextIter  start,
932                 GtkTextIter  end)
933 {
934         GossipChatSpell *chat_spell;
935
936         chat_spell = g_new0 (GossipChatSpell, 1);
937
938         chat_spell->chat = g_object_ref (chat);
939         chat_spell->word = g_strdup (word);
940         chat_spell->start = start;
941         chat_spell->end = end;
942
943         return chat_spell;
944 }
945
946 static void
947 chat_spell_free (GossipChatSpell *chat_spell)
948 {
949         g_object_unref (chat_spell->chat);
950         g_free (chat_spell->word);
951         g_free (chat_spell);
952 }
953
954 static void
955 chat_composing_start (GossipChat *chat)
956 {
957         GossipChatPriv *priv;
958
959         priv = GET_PRIV (chat);
960
961         if (priv->composing_stop_timeout_id) {
962                 /* Just restart the timeout */
963                 chat_composing_remove_timeout (chat);
964         } else {
965                 empathy_tp_chat_set_state (priv->tp_chat,
966                                            TP_CHANNEL_CHAT_STATE_COMPOSING);
967         }
968
969         priv->composing_stop_timeout_id = g_timeout_add (
970                 1000 * COMPOSING_STOP_TIMEOUT,
971                 (GSourceFunc) chat_composing_stop_timeout_cb,
972                 chat);
973 }
974
975 static void
976 chat_composing_stop (GossipChat *chat)
977 {
978         GossipChatPriv *priv;
979
980         priv = GET_PRIV (chat);
981
982         chat_composing_remove_timeout (chat);
983         empathy_tp_chat_set_state (priv->tp_chat,
984                                    TP_CHANNEL_CHAT_STATE_ACTIVE);
985 }
986
987 static void
988 chat_composing_remove_timeout (GossipChat *chat)
989 {
990         GossipChatPriv *priv;
991
992         priv = GET_PRIV (chat);
993
994         if (priv->composing_stop_timeout_id) {
995                 g_source_remove (priv->composing_stop_timeout_id);
996                 priv->composing_stop_timeout_id = 0;
997         }
998 }
999
1000 static gboolean
1001 chat_composing_stop_timeout_cb (GossipChat *chat)
1002 {
1003         GossipChatPriv *priv;
1004
1005         priv = GET_PRIV (chat);
1006
1007         priv->composing_stop_timeout_id = 0;
1008         empathy_tp_chat_set_state (priv->tp_chat,
1009                                    TP_CHANNEL_CHAT_STATE_PAUSED);
1010
1011         return FALSE;
1012 }
1013
1014 static void
1015 chat_state_changed_cb (EmpathyTpChat             *tp_chat,
1016                        GossipContact             *contact,
1017                        TelepathyChannelChatState  state,
1018                        GossipChat                *chat)
1019 {
1020         GossipChatPriv *priv;
1021         GList          *l;
1022         gboolean        was_composing;
1023
1024         priv = GET_PRIV (chat);
1025
1026         was_composing = (priv->compositors != NULL);
1027
1028         /* Find the contact in the list. After that l is the list elem or NULL */
1029         for (l = priv->compositors; l; l = l->next) {
1030                 if (gossip_contact_equal (contact, l->data)) {
1031                         break;
1032                 }
1033         }
1034
1035         switch (state) {
1036         case TP_CHANNEL_CHAT_STATE_GONE:
1037         case TP_CHANNEL_CHAT_STATE_INACTIVE:
1038         case TP_CHANNEL_CHAT_STATE_ACTIVE:
1039                 /* Contact is not composing */
1040                 if (l) {
1041                         priv->compositors = g_list_remove_link (priv->compositors, l);
1042                         g_object_unref (l->data);
1043                         g_list_free1 (l);
1044                 }
1045                 break;
1046         case TP_CHANNEL_CHAT_STATE_PAUSED:
1047         case TP_CHANNEL_CHAT_STATE_COMPOSING:
1048                 /* Contact is composing */
1049                 if (!l) {
1050                         priv->compositors = g_list_prepend (priv->compositors,
1051                                                             g_object_ref (contact));
1052                 }
1053                 break;
1054         default:
1055                 g_assert_not_reached ();
1056         }
1057
1058         gossip_debug (DEBUG_DOMAIN, "Was composing: %s now composing: %s",
1059                       was_composing ? "yes" : "no",
1060                       priv->compositors ? "yes" : "no");
1061
1062         if ((was_composing && !priv->compositors) ||
1063             (!was_composing && priv->compositors)) {
1064                 /* Composing state changed */
1065                 g_signal_emit (chat, signals[COMPOSING], 0,
1066                                (gboolean) priv->compositors);
1067         }
1068 }
1069
1070 gboolean
1071 gossip_chat_get_is_command (const gchar *str)
1072 {
1073         g_return_val_if_fail (str != NULL, FALSE);
1074
1075         if (str[0] != '/') {
1076                 return FALSE;
1077         }
1078
1079         if (g_str_has_prefix (str, "/me")) {
1080                 return TRUE;
1081         }
1082         else if (g_str_has_prefix (str, "/nick")) {
1083                 return TRUE;
1084         }
1085         else if (g_str_has_prefix (str, "/topic")) {
1086                 return TRUE;
1087         }
1088
1089         return FALSE;
1090 }
1091
1092 void
1093 gossip_chat_correct_word (GossipChat  *chat,
1094                           GtkTextIter  start,
1095                           GtkTextIter  end,
1096                           const gchar *new_word)
1097 {
1098         GtkTextBuffer *buffer;
1099
1100         g_return_if_fail (chat != NULL);
1101         g_return_if_fail (new_word != NULL);
1102
1103         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1104
1105         gtk_text_buffer_delete (buffer, &start, &end);
1106         gtk_text_buffer_insert (buffer, &start,
1107                                 new_word,
1108                                 -1);
1109 }
1110
1111 const gchar *
1112 gossip_chat_get_name (GossipChat *chat)
1113 {
1114         g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1115
1116         if (GOSSIP_CHAT_GET_CLASS (chat)->get_name) {
1117                 return GOSSIP_CHAT_GET_CLASS (chat)->get_name (chat);
1118         }
1119
1120         return NULL;
1121 }
1122
1123 gchar *
1124 gossip_chat_get_tooltip (GossipChat *chat)
1125 {
1126         g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1127
1128         if (GOSSIP_CHAT_GET_CLASS (chat)->get_tooltip) {
1129                 return GOSSIP_CHAT_GET_CLASS (chat)->get_tooltip (chat);
1130         }
1131
1132         return NULL;
1133 }
1134
1135 const gchar *
1136 gossip_chat_get_status_icon_name (GossipChat *chat)
1137 {
1138         g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1139
1140         if (GOSSIP_CHAT_GET_CLASS (chat)->get_status_icon_name) {
1141                 return GOSSIP_CHAT_GET_CLASS (chat)->get_status_icon_name (chat);
1142         }
1143
1144         return NULL;
1145 }
1146
1147 GossipContact *
1148 gossip_chat_get_contact (GossipChat *chat)
1149 {
1150         g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1151
1152         if (GOSSIP_CHAT_GET_CLASS (chat)->get_contact) {
1153                 return GOSSIP_CHAT_GET_CLASS (chat)->get_contact (chat);
1154         }
1155
1156         return NULL;
1157 }
1158 GossipContact *
1159 gossip_chat_get_own_contact (GossipChat *chat)
1160 {
1161         GossipChatPriv *priv;
1162
1163         g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1164
1165         priv = GET_PRIV (chat);
1166
1167         return empathy_contact_manager_get_own (priv->manager, chat->account);
1168 }
1169
1170 GtkWidget *
1171 gossip_chat_get_widget (GossipChat *chat)
1172 {
1173         g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1174
1175         if (GOSSIP_CHAT_GET_CLASS (chat)->get_widget) {
1176                 return GOSSIP_CHAT_GET_CLASS (chat)->get_widget (chat);
1177         }
1178
1179         return NULL;
1180 }
1181
1182 gboolean
1183 gossip_chat_is_group_chat (GossipChat *chat)
1184 {
1185         g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1186
1187         if (GOSSIP_CHAT_GET_CLASS (chat)->is_group_chat) {
1188                 return GOSSIP_CHAT_GET_CLASS (chat)->is_group_chat (chat);
1189         }
1190
1191         return FALSE;
1192 }
1193
1194 gboolean 
1195 gossip_chat_is_connected (GossipChat *chat)
1196 {
1197         GossipChatPriv *priv;
1198
1199         g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1200
1201         priv = GET_PRIV (chat);
1202
1203         return (priv->tp_chat != NULL);
1204 }
1205
1206 gboolean
1207 gossip_chat_get_show_contacts (GossipChat *chat)
1208 {
1209         g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1210
1211         if (GOSSIP_CHAT_GET_CLASS (chat)->get_show_contacts) {
1212                 return GOSSIP_CHAT_GET_CLASS (chat)->get_show_contacts (chat);
1213         }
1214
1215         return FALSE;
1216 }
1217
1218 void
1219 gossip_chat_set_show_contacts (GossipChat *chat,
1220                                gboolean    show)
1221 {
1222         g_return_if_fail (GOSSIP_IS_CHAT (chat));
1223
1224         if (GOSSIP_CHAT_GET_CLASS (chat)->set_show_contacts) {
1225                 GOSSIP_CHAT_GET_CLASS (chat)->set_show_contacts (chat, show);
1226         }
1227 }
1228
1229 void
1230 gossip_chat_save_geometry (GossipChat *chat,
1231                            gint        x,
1232                            gint        y,
1233                            gint        w,
1234                            gint        h)
1235 {
1236         gossip_geometry_save (gossip_chat_get_id (chat), x, y, w, h);
1237 }
1238
1239 void
1240 gossip_chat_load_geometry (GossipChat *chat,
1241                            gint       *x,
1242                            gint       *y,
1243                            gint       *w,
1244                            gint       *h)
1245 {
1246         gossip_geometry_load (gossip_chat_get_id (chat), x, y, w, h);
1247 }
1248
1249 void
1250 gossip_chat_set_tp_chat (GossipChat    *chat,
1251                          EmpathyTpChat *tp_chat)
1252 {
1253         GossipChatPriv *priv;
1254         GtkWidget      *widget;
1255
1256         g_return_if_fail (GOSSIP_IS_CHAT (chat));
1257         g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
1258
1259         priv = GET_PRIV (chat);
1260
1261         if (tp_chat == priv->tp_chat) {
1262                 return;
1263         }
1264
1265         if (priv->tp_chat) {
1266                 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1267                                                       chat_message_received_cb,
1268                                                       chat);
1269                 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1270                                                       chat_destroy_cb,
1271                                                       chat);
1272                 g_object_unref (priv->tp_chat);
1273         }
1274
1275         g_free (priv->id);
1276         priv->tp_chat = g_object_ref (tp_chat);
1277         priv->id = g_strdup (empathy_tp_chat_get_id (tp_chat));
1278
1279         g_signal_connect (tp_chat, "message-received",
1280                           G_CALLBACK (chat_message_received_cb),
1281                           chat);
1282         g_signal_connect (tp_chat, "chat-state-changed",
1283                           G_CALLBACK (chat_state_changed_cb),
1284                           chat);
1285         g_signal_connect (tp_chat, "destroy",
1286                           G_CALLBACK (chat_destroy_cb),
1287                           chat);
1288
1289         empathy_tp_chat_request_pending (tp_chat);
1290
1291         if (!priv->sensitive) {
1292                 widget = gossip_chat_get_widget (chat);
1293                 gtk_widget_set_sensitive (widget, TRUE);
1294                 gossip_chat_view_append_event (chat->view, _("Connected"));
1295                 priv->sensitive = TRUE;
1296         }
1297 }
1298
1299 const gchar *
1300 gossip_chat_get_id (GossipChat *chat)
1301 {
1302         GossipChatPriv *priv;
1303
1304         priv = GET_PRIV (chat);
1305
1306         return priv->id;
1307 }
1308
1309 void
1310 gossip_chat_clear (GossipChat *chat)
1311 {
1312         g_return_if_fail (GOSSIP_IS_CHAT (chat));
1313
1314         gossip_chat_view_clear (chat->view);
1315 }
1316
1317 void
1318 gossip_chat_set_window (GossipChat       *chat,
1319                         GossipChatWindow *window)
1320 {
1321         GossipChatPriv *priv;
1322
1323         priv = GET_PRIV (chat);
1324         priv->window = window;
1325 }
1326
1327 GossipChatWindow *
1328 gossip_chat_get_window (GossipChat *chat)
1329 {
1330         GossipChatPriv *priv;
1331
1332         priv = GET_PRIV (chat);
1333
1334         return priv->window;
1335 }
1336
1337 void
1338 gossip_chat_scroll_down (GossipChat *chat)
1339 {
1340         g_return_if_fail (GOSSIP_IS_CHAT (chat));
1341
1342         gossip_chat_view_scroll_down (chat->view);
1343 }
1344
1345 void
1346 gossip_chat_cut (GossipChat *chat)
1347 {
1348         GtkTextBuffer *buffer;
1349
1350         g_return_if_fail (GOSSIP_IS_CHAT (chat));
1351
1352         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1353         if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1354                 GtkClipboard *clipboard;
1355
1356                 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1357
1358                 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
1359         }
1360 }
1361
1362 void
1363 gossip_chat_copy (GossipChat *chat)
1364 {
1365         GtkTextBuffer *buffer;
1366
1367         g_return_if_fail (GOSSIP_IS_CHAT (chat));
1368
1369         if (gossip_chat_view_get_selection_bounds (chat->view, NULL, NULL)) {
1370                 gossip_chat_view_copy_clipboard (chat->view);
1371                 return;
1372         }
1373
1374         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1375         if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1376                 GtkClipboard *clipboard;
1377
1378                 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1379
1380                 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1381         }
1382 }
1383
1384 void
1385 gossip_chat_paste (GossipChat *chat)
1386 {
1387         GtkTextBuffer *buffer;
1388         GtkClipboard  *clipboard;
1389
1390         g_return_if_fail (GOSSIP_IS_CHAT (chat));
1391
1392         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1393         clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1394
1395         gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
1396 }
1397
1398 void
1399 gossip_chat_present (GossipChat *chat)
1400 {
1401         GossipChatPriv *priv;
1402
1403         g_return_if_fail (GOSSIP_IS_CHAT (chat));
1404
1405         priv = GET_PRIV (chat);
1406
1407         if (priv->window == NULL) {
1408                 GossipChatWindow *window;
1409
1410                 window = gossip_chat_window_get_default ();
1411                 if (!window) {
1412                         window = gossip_chat_window_new ();
1413                 }
1414
1415                 gossip_chat_window_add_chat (window, chat);
1416         }
1417
1418         gossip_chat_window_switch_to_chat (priv->window, chat);
1419         gossip_window_present (
1420                 GTK_WINDOW (gossip_chat_window_get_dialog (priv->window)),
1421                 TRUE);
1422
1423         gtk_widget_grab_focus (chat->input_text_view); 
1424 }
1425
1426 gboolean
1427 gossip_chat_should_play_sound (GossipChat *chat)
1428 {
1429         GossipChatWindow *window;
1430         gboolean          play = TRUE;
1431
1432         g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1433
1434         window = gossip_chat_get_window (chat);
1435         if (!window) {
1436                 return TRUE;
1437         }
1438
1439         play = !gossip_chat_window_has_focus (window);
1440
1441         return play;
1442 }
1443
1444 gboolean
1445 gossip_chat_should_highlight_nick (GossipMessage *message)
1446 {
1447         GossipContact *my_contact;
1448         const gchar   *msg, *to;
1449         gchar         *cf_msg, *cf_to;
1450         gchar         *ch;
1451         gboolean       ret_val;
1452
1453         g_return_val_if_fail (GOSSIP_IS_MESSAGE (message), FALSE);
1454
1455         gossip_debug (DEBUG_DOMAIN, "Highlighting nickname");
1456
1457         ret_val = FALSE;
1458
1459         msg = gossip_message_get_body (message);
1460         if (!msg) {
1461                 return FALSE;
1462         }
1463
1464         my_contact = gossip_get_own_contact_from_contact (gossip_message_get_sender (message));
1465         to = gossip_contact_get_name (my_contact);
1466         if (!to) {
1467                 return FALSE;
1468         }
1469
1470         cf_msg = g_utf8_casefold (msg, -1);
1471         cf_to = g_utf8_casefold (to, -1);
1472
1473         ch = strstr (cf_msg, cf_to);
1474         if (ch == NULL) {
1475                 goto finished;
1476         }
1477
1478         if (ch != cf_msg) {
1479                 /* Not first in the message */
1480                 if ((*(ch - 1) != ' ') &&
1481                     (*(ch - 1) != ',') &&
1482                     (*(ch - 1) != '.')) {
1483                         goto finished;
1484                 }
1485         }
1486
1487         ch = ch + strlen (cf_to);
1488         if (ch >= cf_msg + strlen (cf_msg)) {
1489                 ret_val = TRUE;
1490                 goto finished;
1491         }
1492
1493         if ((*ch == ' ') ||
1494             (*ch == ',') ||
1495             (*ch == '.') ||
1496             (*ch == ':')) {
1497                 ret_val = TRUE;
1498                 goto finished;
1499         }
1500
1501 finished:
1502         g_free (cf_msg);
1503         g_free (cf_to);
1504
1505         return ret_val;
1506 }
1507