]> git.0d.be Git - empathy.git/blob - libempathy-gtk/gossip-chat.c
[darcs-to-svn @ sync with Gossip SVN 2323]
[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
70         GtkTooltips           *tooltips;
71         guint                  composing_stop_timeout_id;
72         gboolean               sensitive;
73         gchar                 *id;
74         GSList                *sent_messages;
75         gint                   sent_messages_index;
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 chat_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         chat_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         chat_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         chat_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         chat_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         chat_composing_remove_timeout (chat);
288         g_object_unref (chat->account);
289         g_object_unref (priv->manager);
290
291         if (priv->tp_chat) {
292                 g_object_unref (priv->tp_chat);
293         }
294
295         g_free (priv->id);
296
297         G_OBJECT_CLASS (gossip_chat_parent_class)->finalize (object);
298 }
299
300 static void
301 chat_destroy_cb (EmpathyTpChat *tp_chat,
302                  GossipChat    *chat)
303 {
304         GossipChatPriv *priv;
305         GtkWidget      *widget;
306
307         priv = GET_PRIV (chat);
308
309         if (priv->tp_chat) {
310                 g_object_unref (priv->tp_chat);
311                 priv->tp_chat = NULL;
312         }
313
314         gossip_chat_view_append_event (chat->view, _("Disconnected"));
315
316         widget = gossip_chat_get_widget (chat);
317         gtk_widget_set_sensitive (widget, FALSE);
318         priv->sensitive = FALSE;
319 }
320
321 static void
322 chat_send (GossipChat  *chat,
323            const gchar *msg)
324 {
325         GossipChatPriv   *priv;
326         //GossipLogManager *log_manager;
327         GossipMessage    *message;
328         GossipContact    *own_contact;
329
330         priv = GET_PRIV (chat);
331
332         if (G_STR_EMPTY (msg)) {
333                 return;
334         }
335
336         chat_sent_message_add (chat, msg);
337
338         if (g_str_has_prefix (msg, "/clear")) {
339                 gossip_chat_view_clear (chat->view);
340                 return;
341         }
342
343         /* FIXME: gossip_app_set_not_away ();*/
344
345         own_contact = gossip_chat_get_own_contact (chat);
346         message = gossip_message_new (msg);
347         gossip_message_set_sender (message, own_contact);
348
349         //FIXME: log_manager = gossip_session_get_log_manager (gossip_app_get_session ());
350         //gossip_log_message_for_contact (log_manager, message, FALSE);
351
352         empathy_tp_chat_send (priv->tp_chat, message);
353
354         g_object_unref (message);
355 }
356
357 static void
358 chat_input_text_view_send (GossipChat *chat)
359 {
360         GossipChatPriv *priv;
361         GtkTextBuffer  *buffer;
362         GtkTextIter     start, end;
363         gchar          *msg;
364
365         priv = GET_PRIV (chat);
366
367         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
368
369         gtk_text_buffer_get_bounds (buffer, &start, &end);
370         msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
371
372         /* clear the input field */
373         gtk_text_buffer_set_text (buffer, "", -1);
374
375         chat_send (chat, msg);
376
377         g_free (msg);
378
379         chat->is_first_char = TRUE;
380 }
381
382 static void
383 chat_message_received_cb (EmpathyTpChat *tp_chat,
384                           GossipMessage *message,
385                           GossipChat    *chat)
386 {
387         GossipChatPriv *priv;
388         //GossipLogManager      *log_manager;
389         GossipContact         *sender;
390
391         priv = GET_PRIV (chat);
392
393         sender = gossip_message_get_sender (message);
394         gossip_debug (DEBUG_DOMAIN, "Appending message ('%s')",
395                       gossip_contact_get_name (sender));
396
397 /*FIXME:
398         log_manager = gossip_session_get_log_manager (gossip_app_get_session ());
399         gossip_log_message_for_contact (log_manager, message, TRUE);
400 */
401         gossip_chat_view_append_message (chat->view, message);
402
403         if (gossip_chat_should_play_sound (chat)) {
404                 // FIXME: gossip_sound_play (GOSSIP_SOUND_CHAT);
405         }
406
407         g_signal_emit_by_name (chat, "new-message", message, FALSE);
408 }
409
410 void 
411 chat_sent_message_add (GossipChat  *chat,
412                        const gchar *str)
413 {
414         GossipChatPriv *priv;
415         GSList         *list;
416         GSList         *item;
417
418         priv = GET_PRIV (chat);
419
420         /* Save the sent message in our repeat buffer */
421         list = priv->sent_messages;
422         
423         /* Remove any other occurances of this msg */
424         while ((item = g_slist_find_custom (list, str, (GCompareFunc) strcmp)) != NULL) {
425                 list = g_slist_remove_link (list, item);
426                 g_free (item->data);
427                 g_slist_free1 (item);
428         }
429
430         /* Trim the list to the last 10 items */
431         while (g_slist_length (list) > 10) {
432                 item = g_slist_last (list);
433                 if (item) {
434                         list = g_slist_remove_link (list, item);
435                         g_free (item->data);
436                         g_slist_free1 (item);
437                 }
438         }
439
440         /* Add new message */
441         list = g_slist_prepend (list, g_strdup (str));
442
443         /* Set list and reset the index */
444         priv->sent_messages = list;
445         priv->sent_messages_index = -1;
446 }
447
448 const gchar *
449 chat_sent_message_get_next (GossipChat *chat)
450 {
451         GossipChatPriv *priv;
452         gint            max;
453         
454         priv = GET_PRIV (chat);
455
456         if (!priv->sent_messages) {
457                 gossip_debug (DEBUG_DOMAIN, 
458                               "No sent messages, next message is NULL");
459                 return NULL;
460         }
461
462         max = g_slist_length (priv->sent_messages) - 1;
463
464         if (priv->sent_messages_index < max) {
465                 priv->sent_messages_index++;
466         }
467         
468         gossip_debug (DEBUG_DOMAIN, 
469                       "Returning next message index:%d",
470                       priv->sent_messages_index);
471
472         return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
473 }
474
475 const gchar *
476 chat_sent_message_get_last (GossipChat *chat)
477 {
478         GossipChatPriv *priv;
479
480         g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
481
482         priv = GET_PRIV (chat);
483         
484         if (!priv->sent_messages) {
485                 gossip_debug (DEBUG_DOMAIN, 
486                               "No sent messages, last message is NULL");
487                 return NULL;
488         }
489
490         if (priv->sent_messages_index >= 0) {
491                 priv->sent_messages_index--;
492         }
493
494         gossip_debug (DEBUG_DOMAIN, 
495                       "Returning last message index:%d",
496                       priv->sent_messages_index);
497
498         return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
499 }
500
501 static gboolean
502 chat_input_key_press_event_cb (GtkWidget   *widget,
503                                GdkEventKey *event,
504                                GossipChat  *chat)
505 {
506         GossipChatPriv *priv;
507         GtkAdjustment  *adj;
508         gdouble         val;
509         GtkWidget      *text_view_sw;
510
511         priv = GET_PRIV (chat);
512
513         if (event->keyval == GDK_Tab && !(event->state & GDK_CONTROL_MASK)) {
514                 return TRUE;
515         }
516
517         /* Catch ctrl+up/down so we can traverse messages we sent */
518         if ((event->state & GDK_CONTROL_MASK) && 
519             (event->keyval == GDK_Up || 
520              event->keyval == GDK_Down)) {
521                 GtkTextBuffer *buffer;
522                 const gchar   *str;
523
524                 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
525
526                 if (event->keyval == GDK_Up) {
527                         str = chat_sent_message_get_next (chat);
528                 } else {
529                         str = chat_sent_message_get_last (chat);
530                 }
531
532                 g_signal_handlers_block_by_func (buffer, 
533                                                  chat_input_text_buffer_changed_cb,
534                                                  chat);
535                 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
536                 g_signal_handlers_unblock_by_func (buffer, 
537                                                    chat_input_text_buffer_changed_cb,
538                                                    chat);
539
540                 return TRUE;    
541         }
542
543         /* Catch enter but not ctrl/shift-enter */
544         if (IS_ENTER (event->keyval) && !(event->state & GDK_SHIFT_MASK)) {
545                 GtkTextView *view;
546
547                 /* This is to make sure that kinput2 gets the enter. And if
548                  * it's handled there we shouldn't send on it. This is because
549                  * kinput2 uses Enter to commit letters. See:
550                  * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
551                  */
552
553                 view = GTK_TEXT_VIEW (chat->input_text_view);
554                 if (gtk_im_context_filter_keypress (view->im_context, event)) {
555                         GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
556                         return TRUE;
557                 }
558
559                 chat_input_text_view_send (chat);
560                 return TRUE;
561         }
562
563         text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
564
565         if (IS_ENTER (event->keyval) && (event->state & GDK_SHIFT_MASK)) {
566                 /* Newline for shift-enter. */
567                 return FALSE;
568         }
569         else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
570                  event->keyval == GDK_Page_Up) {
571                 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
572                 gtk_adjustment_set_value (adj, adj->value - adj->page_size);
573
574                 return TRUE;
575         }
576         else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
577                  event->keyval == GDK_Page_Down) {
578                 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
579                 val = MIN (adj->value + adj->page_size, adj->upper - adj->page_size);
580                 gtk_adjustment_set_value (adj, val);
581
582                 return TRUE;
583         }
584
585         return FALSE;
586 }
587
588 static gboolean
589 chat_text_view_focus_in_event_cb (GtkWidget  *widget,
590                                   GdkEvent   *event,
591                                   GossipChat *chat)
592 {
593         gtk_widget_grab_focus (chat->input_text_view);
594
595         return TRUE;
596 }
597
598 static void
599 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
600                                    GossipChat    *chat)
601 {
602         GossipChatPriv *priv;
603         GtkTextIter     start, end;
604         gchar          *str;
605         gboolean        spell_checker = FALSE;
606
607         priv = GET_PRIV (chat);
608
609         if (gtk_text_buffer_get_char_count (buffer) == 0) {
610                 chat_composing_stop (chat);
611         } else {
612                 chat_composing_start (chat);
613         }
614
615         gossip_conf_get_bool (gossip_conf_get (),
616                               GOSSIP_PREFS_CHAT_SPELL_CHECKER_ENABLED,
617                               &spell_checker);
618
619         if (chat->is_first_char) {
620                 GtkRequisition  req;
621                 gint            window_height;
622                 GtkWidget      *dialog;
623                 GtkAllocation  *allocation;
624
625                 /* Save the window's size */
626                 dialog = gossip_chat_window_get_dialog (priv->window);
627                 gtk_window_get_size (GTK_WINDOW (dialog),
628                                      NULL, &window_height);
629
630                 gtk_widget_size_request (chat->input_text_view, &req);
631
632                 allocation = &GTK_WIDGET (chat->view)->allocation;
633
634                 priv->default_window_height = window_height;
635                 priv->last_input_height = req.height;
636                 priv->padding_height = window_height - req.height - allocation->height;
637
638                 chat->is_first_char = FALSE;
639         }
640
641         gtk_text_buffer_get_start_iter (buffer, &start);
642
643         if (!spell_checker) {
644                 gtk_text_buffer_get_end_iter (buffer, &end);
645                 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
646                 return;
647         }
648
649         if (!gossip_spell_supported ()) {
650                 return;
651         }
652
653         /* NOTE: this is really inefficient, we shouldn't have to
654            reiterate the whole buffer each time and check each work
655            every time. */
656         while (TRUE) {
657                 gboolean correct = FALSE;
658
659                 /* if at start */
660                 if (gtk_text_iter_is_start (&start)) {
661                         end = start;
662
663                         if (!gtk_text_iter_forward_word_end (&end)) {
664                                 /* no whole word yet */
665                                 break;
666                         }
667                 } else {
668                         if (!gtk_text_iter_forward_word_end (&end)) {
669                                 /* must be the end of the buffer */
670                                 break;
671                         }
672
673                         start = end;
674                         gtk_text_iter_backward_word_start (&start);
675                 }
676
677                 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
678
679                 /* spell check string */
680                 if (!gossip_chat_get_is_command (str)) {
681                         correct = gossip_spell_check (str);
682                 } else {
683                         correct = TRUE;
684                 }
685
686                 if (!correct) {
687                         gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
688                 } else {
689                         gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
690                 }
691
692                 g_free (str);
693
694                 /* set start iter to the end iters position */
695                 start = end;
696         }
697 }
698
699 typedef struct {
700         GtkWidget *window;
701         gint       width;
702         gint       height;
703 } ChangeSizeData;
704
705 static gboolean
706 chat_change_size_in_idle_cb (ChangeSizeData *data)
707 {
708         gtk_window_resize (GTK_WINDOW (data->window),
709                            data->width, data->height);
710
711         return FALSE;
712 }
713
714 static void
715 chat_text_view_scroll_hide_cb (GtkWidget  *widget,
716                                GossipChat *chat)
717 {
718         GossipChatPriv *priv;
719         GtkWidget      *sw;
720
721         priv = GET_PRIV (chat);
722
723         priv->vscroll_visible = FALSE;
724         g_signal_handlers_disconnect_by_func (widget, chat_text_view_scroll_hide_cb, chat);
725
726         sw = gtk_widget_get_parent (chat->input_text_view);
727         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
728                                         GTK_POLICY_NEVER,
729                                         GTK_POLICY_NEVER);
730         g_object_set (sw, "height-request", -1, NULL);
731 }
732
733 static void
734 chat_text_view_size_allocate_cb (GtkWidget     *widget,
735                                  GtkAllocation *allocation,
736                                  GossipChat    *chat)
737 {
738         GossipChatPriv *priv;
739         gint            width;
740         GtkWidget      *dialog;
741         ChangeSizeData *data;
742         gint            window_height;
743         gint            new_height;
744         GtkAllocation  *view_allocation;
745         gint            current_height;
746         gint            diff;
747         GtkWidget      *sw;
748
749         priv = GET_PRIV (chat);
750
751         if (priv->default_window_height <= 0) {
752                 return;
753         }
754
755         sw = gtk_widget_get_parent (widget);
756         if (sw->allocation.height >= MAX_INPUT_HEIGHT && !priv->vscroll_visible) {
757                 GtkWidget *vscroll;
758
759                 priv->vscroll_visible = TRUE;
760                 gtk_widget_set_size_request (sw, sw->allocation.width, MAX_INPUT_HEIGHT);
761                 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
762                                                 GTK_POLICY_NEVER,
763                                                 GTK_POLICY_AUTOMATIC);
764                 vscroll = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (sw));
765                 g_signal_connect (vscroll, "hide",
766                                   G_CALLBACK (chat_text_view_scroll_hide_cb),
767                                   chat);
768         }
769
770         if (priv->last_input_height <= allocation->height) {
771                 priv->last_input_height = allocation->height;
772                 return;
773         }
774
775         diff = priv->last_input_height - allocation->height;
776         priv->last_input_height = allocation->height;
777
778         view_allocation = &GTK_WIDGET (chat->view)->allocation;
779
780         dialog = gossip_chat_window_get_dialog (priv->window);
781         gtk_window_get_size (GTK_WINDOW (dialog), NULL, &current_height);
782
783         new_height = view_allocation->height + priv->padding_height + allocation->height - diff;
784
785         if (new_height <= priv->default_window_height) {
786                 window_height = priv->default_window_height;
787         } else {
788                 window_height = new_height;
789         }
790
791         if (current_height <= window_height) {
792                 return;
793         }
794
795         /* Restore the window's size */
796         gtk_window_get_size (GTK_WINDOW (dialog), &width, NULL);
797
798         data = g_new0 (ChangeSizeData, 1);
799         data->window = dialog;
800         data->width  = width;
801         data->height = window_height;
802
803         g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
804                          (GSourceFunc) chat_change_size_in_idle_cb,
805                          data, g_free);
806 }
807
808 static void
809 chat_text_view_realize_cb (GtkWidget  *widget,
810                            GossipChat *chat)
811 {
812         gossip_debug (DEBUG_DOMAIN, "Setting focus to the input text view");
813         gtk_widget_grab_focus (widget);
814 }
815
816 static void
817 chat_insert_smiley_activate_cb (GtkWidget  *menuitem,
818                                 GossipChat *chat)
819 {
820         GtkTextBuffer *buffer;
821         GtkTextIter    iter;
822         const gchar   *smiley;
823
824         smiley = g_object_get_data (G_OBJECT (menuitem), "smiley_text");
825
826         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
827
828         gtk_text_buffer_get_end_iter (buffer, &iter);
829         gtk_text_buffer_insert (buffer, &iter, smiley, -1);
830
831         gtk_text_buffer_get_end_iter (buffer, &iter);
832         gtk_text_buffer_insert (buffer, &iter, " ", -1);
833 }
834
835 static void
836 chat_text_populate_popup_cb (GtkTextView *view,
837                              GtkMenu     *menu,
838                              GossipChat  *chat)
839 {
840         GossipChatPriv  *priv;
841         GtkTextBuffer   *buffer;
842         GtkTextTagTable *table;
843         GtkTextTag      *tag;
844         gint             x, y;
845         GtkTextIter      iter, start, end;
846         GtkWidget       *item;
847         gchar           *str = NULL;
848         GossipChatSpell *chat_spell;
849         GtkWidget       *smiley_menu;
850
851         priv = GET_PRIV (chat);
852
853         /* Add the emoticon menu. */
854         item = gtk_separator_menu_item_new ();
855         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
856         gtk_widget_show (item);
857
858         item = gtk_menu_item_new_with_mnemonic (_("Insert Smiley"));
859         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
860         gtk_widget_show (item);
861
862         smiley_menu = gossip_chat_view_get_smiley_menu (
863                 G_CALLBACK (chat_insert_smiley_activate_cb),
864                 chat,
865                 priv->tooltips);
866         gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
867
868         /* Add the spell check menu item. */
869         buffer = gtk_text_view_get_buffer (view);
870         table = gtk_text_buffer_get_tag_table (buffer);
871
872         tag = gtk_text_tag_table_lookup (table, "misspelled");
873
874         gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
875
876         gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
877                                                GTK_TEXT_WINDOW_WIDGET,
878                                                x, y,
879                                                &x, &y);
880
881         gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
882
883         start = end = iter;
884
885         if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
886             gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
887
888                 str = gtk_text_buffer_get_text (buffer,
889                                                 &start, &end, FALSE);
890         }
891
892         if (G_STR_EMPTY (str)) {
893                 return;
894         }
895
896         chat_spell = chat_spell_new (chat, str, start, end);
897
898         g_object_set_data_full (G_OBJECT (menu),
899                                 "chat_spell", chat_spell,
900                                 (GDestroyNotify) chat_spell_free);
901
902         item = gtk_separator_menu_item_new ();
903         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
904         gtk_widget_show (item);
905
906         item = gtk_menu_item_new_with_mnemonic (_("_Check Word Spelling..."));
907         g_signal_connect (item,
908                           "activate",
909                           G_CALLBACK (chat_text_check_word_spelling_cb),
910                           chat_spell);
911         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
912         gtk_widget_show (item);
913 }
914
915 static void
916 chat_text_check_word_spelling_cb (GtkMenuItem     *menuitem,
917                                   GossipChatSpell *chat_spell)
918 {
919 /*FIXME:        gossip_spell_dialog_show (chat_spell->chat,
920                                   chat_spell->start,
921                                   chat_spell->end,
922                                   chat_spell->word);*/
923 }
924
925 static GossipChatSpell *
926 chat_spell_new (GossipChat  *chat,
927                 const gchar *word,
928                 GtkTextIter  start,
929                 GtkTextIter  end)
930 {
931         GossipChatSpell *chat_spell;
932
933         chat_spell = g_new0 (GossipChatSpell, 1);
934
935         chat_spell->chat = g_object_ref (chat);
936         chat_spell->word = g_strdup (word);
937         chat_spell->start = start;
938         chat_spell->end = end;
939
940         return chat_spell;
941 }
942
943 static void
944 chat_spell_free (GossipChatSpell *chat_spell)
945 {
946         g_object_unref (chat_spell->chat);
947         g_free (chat_spell->word);
948         g_free (chat_spell);
949 }
950
951 static void
952 chat_composing_start (GossipChat *chat)
953 {
954         GossipChatPriv *priv;
955
956         priv = GET_PRIV (chat);
957
958         if (priv->composing_stop_timeout_id) {
959                 /* Just restart the timeout */
960                 chat_composing_remove_timeout (chat);
961         } else {
962                 empathy_tp_chat_set_state (priv->tp_chat,
963                                            TP_CHANNEL_CHAT_STATE_COMPOSING);
964         }
965
966         priv->composing_stop_timeout_id = g_timeout_add (
967                 1000 * COMPOSING_STOP_TIMEOUT,
968                 (GSourceFunc) chat_composing_stop_timeout_cb,
969                 chat);
970 }
971
972 static void
973 chat_composing_stop (GossipChat *chat)
974 {
975         GossipChatPriv *priv;
976
977         priv = GET_PRIV (chat);
978
979         chat_composing_remove_timeout (chat);
980         empathy_tp_chat_set_state (priv->tp_chat,
981                                    TP_CHANNEL_CHAT_STATE_ACTIVE);
982 }
983
984 static void
985 chat_composing_remove_timeout (GossipChat *chat)
986 {
987         GossipChatPriv *priv;
988
989         priv = GET_PRIV (chat);
990
991         if (priv->composing_stop_timeout_id) {
992                 g_source_remove (priv->composing_stop_timeout_id);
993                 priv->composing_stop_timeout_id = 0;
994         }
995 }
996
997 static gboolean
998 chat_composing_stop_timeout_cb (GossipChat *chat)
999 {
1000         GossipChatPriv *priv;
1001
1002         priv = GET_PRIV (chat);
1003
1004         priv->composing_stop_timeout_id = 0;
1005         empathy_tp_chat_set_state (priv->tp_chat,
1006                                    TP_CHANNEL_CHAT_STATE_PAUSED);
1007
1008         return FALSE;
1009 }
1010
1011 static void
1012 chat_state_changed_cb (EmpathyTpChat             *tp_chat,
1013                        GossipContact             *contact,
1014                        TelepathyChannelChatState  state,
1015                        GossipChat                *chat)
1016 {
1017         /* FIXME: not yet implemented */
1018 }
1019
1020 gboolean
1021 gossip_chat_get_is_command (const gchar *str)
1022 {
1023         g_return_val_if_fail (str != NULL, FALSE);
1024
1025         if (str[0] != '/') {
1026                 return FALSE;
1027         }
1028
1029         if (g_str_has_prefix (str, "/me")) {
1030                 return TRUE;
1031         }
1032         else if (g_str_has_prefix (str, "/nick")) {
1033                 return TRUE;
1034         }
1035         else if (g_str_has_prefix (str, "/topic")) {
1036                 return TRUE;
1037         }
1038
1039         return FALSE;
1040 }
1041
1042 void
1043 gossip_chat_correct_word (GossipChat  *chat,
1044                           GtkTextIter  start,
1045                           GtkTextIter  end,
1046                           const gchar *new_word)
1047 {
1048         GtkTextBuffer *buffer;
1049
1050         g_return_if_fail (chat != NULL);
1051         g_return_if_fail (new_word != NULL);
1052
1053         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1054
1055         gtk_text_buffer_delete (buffer, &start, &end);
1056         gtk_text_buffer_insert (buffer, &start,
1057                                 new_word,
1058                                 -1);
1059 }
1060
1061 const gchar *
1062 gossip_chat_get_name (GossipChat *chat)
1063 {
1064         g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1065
1066         if (GOSSIP_CHAT_GET_CLASS (chat)->get_name) {
1067                 return GOSSIP_CHAT_GET_CLASS (chat)->get_name (chat);
1068         }
1069
1070         return NULL;
1071 }
1072
1073 gchar *
1074 gossip_chat_get_tooltip (GossipChat *chat)
1075 {
1076         g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1077
1078         if (GOSSIP_CHAT_GET_CLASS (chat)->get_tooltip) {
1079                 return GOSSIP_CHAT_GET_CLASS (chat)->get_tooltip (chat);
1080         }
1081
1082         return NULL;
1083 }
1084
1085 const gchar *
1086 gossip_chat_get_status_icon_name (GossipChat *chat)
1087 {
1088         g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1089
1090         if (GOSSIP_CHAT_GET_CLASS (chat)->get_status_icon_name) {
1091                 return GOSSIP_CHAT_GET_CLASS (chat)->get_status_icon_name (chat);
1092         }
1093
1094         return NULL;
1095 }
1096
1097 GossipContact *
1098 gossip_chat_get_contact (GossipChat *chat)
1099 {
1100         g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1101
1102         if (GOSSIP_CHAT_GET_CLASS (chat)->get_contact) {
1103                 return GOSSIP_CHAT_GET_CLASS (chat)->get_contact (chat);
1104         }
1105
1106         return NULL;
1107 }
1108 GossipContact *
1109 gossip_chat_get_own_contact (GossipChat *chat)
1110 {
1111         GossipChatPriv *priv;
1112
1113         g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1114
1115         priv = GET_PRIV (chat);
1116
1117         return empathy_contact_manager_get_own (priv->manager, chat->account);
1118 }
1119
1120 GtkWidget *
1121 gossip_chat_get_widget (GossipChat *chat)
1122 {
1123         g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1124
1125         if (GOSSIP_CHAT_GET_CLASS (chat)->get_widget) {
1126                 return GOSSIP_CHAT_GET_CLASS (chat)->get_widget (chat);
1127         }
1128
1129         return NULL;
1130 }
1131
1132 gboolean
1133 gossip_chat_is_group_chat (GossipChat *chat)
1134 {
1135         g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1136
1137         if (GOSSIP_CHAT_GET_CLASS (chat)->is_group_chat) {
1138                 return GOSSIP_CHAT_GET_CLASS (chat)->is_group_chat (chat);
1139         }
1140
1141         return FALSE;
1142 }
1143
1144 gboolean 
1145 gossip_chat_is_connected (GossipChat *chat)
1146 {
1147         GossipChatPriv *priv;
1148
1149         g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1150
1151         priv = GET_PRIV (chat);
1152
1153         return (priv->tp_chat != NULL);
1154 }
1155
1156 gboolean
1157 gossip_chat_get_show_contacts (GossipChat *chat)
1158 {
1159         g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1160
1161         if (GOSSIP_CHAT_GET_CLASS (chat)->get_show_contacts) {
1162                 return GOSSIP_CHAT_GET_CLASS (chat)->get_show_contacts (chat);
1163         }
1164
1165         return FALSE;
1166 }
1167
1168 void
1169 gossip_chat_set_show_contacts (GossipChat *chat,
1170                                gboolean    show)
1171 {
1172         g_return_if_fail (GOSSIP_IS_CHAT (chat));
1173
1174         if (GOSSIP_CHAT_GET_CLASS (chat)->set_show_contacts) {
1175                 GOSSIP_CHAT_GET_CLASS (chat)->set_show_contacts (chat, show);
1176         }
1177 }
1178
1179 void
1180 gossip_chat_save_geometry (GossipChat *chat,
1181                            gint        x,
1182                            gint        y,
1183                            gint        w,
1184                            gint        h)
1185 {
1186         gossip_geometry_save (gossip_chat_get_id (chat), x, y, w, h);
1187 }
1188
1189 void
1190 gossip_chat_load_geometry (GossipChat *chat,
1191                            gint       *x,
1192                            gint       *y,
1193                            gint       *w,
1194                            gint       *h)
1195 {
1196         gossip_geometry_load (gossip_chat_get_id (chat), x, y, w, h);
1197 }
1198
1199 void
1200 gossip_chat_set_tp_chat (GossipChat    *chat,
1201                          EmpathyTpChat *tp_chat)
1202 {
1203         GossipChatPriv *priv;
1204         GtkWidget      *widget;
1205
1206         g_return_if_fail (GOSSIP_IS_CHAT (chat));
1207         g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
1208
1209         priv = GET_PRIV (chat);
1210
1211         if (tp_chat == priv->tp_chat) {
1212                 return;
1213         }
1214
1215         if (priv->tp_chat) {
1216                 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1217                                                       chat_message_received_cb,
1218                                                       chat);
1219                 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1220                                                       chat_destroy_cb,
1221                                                       chat);
1222                 g_object_unref (priv->tp_chat);
1223         }
1224
1225         g_free (priv->id);
1226         priv->tp_chat = g_object_ref (tp_chat);
1227         priv->id = g_strdup (empathy_tp_chat_get_id (tp_chat));
1228
1229         g_signal_connect (tp_chat, "message-received",
1230                           G_CALLBACK (chat_message_received_cb),
1231                           chat);
1232         g_signal_connect (tp_chat, "chat-state-changed",
1233                           G_CALLBACK (chat_state_changed_cb),
1234                           chat);
1235         g_signal_connect (tp_chat, "destroy",
1236                           G_CALLBACK (chat_destroy_cb),
1237                           chat);
1238
1239         empathy_tp_chat_request_pending (tp_chat);
1240
1241         if (!priv->sensitive) {
1242                 widget = gossip_chat_get_widget (chat);
1243                 gtk_widget_set_sensitive (widget, TRUE);
1244                 gossip_chat_view_append_event (chat->view, _("Connected"));
1245                 priv->sensitive = TRUE;
1246         }
1247 }
1248
1249 const gchar *
1250 gossip_chat_get_id (GossipChat *chat)
1251 {
1252         GossipChatPriv *priv;
1253
1254         priv = GET_PRIV (chat);
1255
1256         return priv->id;
1257 }
1258
1259 void
1260 gossip_chat_clear (GossipChat *chat)
1261 {
1262         g_return_if_fail (GOSSIP_IS_CHAT (chat));
1263
1264         gossip_chat_view_clear (chat->view);
1265 }
1266
1267 void
1268 gossip_chat_set_window (GossipChat       *chat,
1269                         GossipChatWindow *window)
1270 {
1271         GossipChatPriv *priv;
1272
1273         priv = GET_PRIV (chat);
1274         priv->window = window;
1275 }
1276
1277 GossipChatWindow *
1278 gossip_chat_get_window (GossipChat *chat)
1279 {
1280         GossipChatPriv *priv;
1281
1282         priv = GET_PRIV (chat);
1283
1284         return priv->window;
1285 }
1286
1287 void
1288 gossip_chat_scroll_down (GossipChat *chat)
1289 {
1290         g_return_if_fail (GOSSIP_IS_CHAT (chat));
1291
1292         gossip_chat_view_scroll_down (chat->view);
1293 }
1294
1295 void
1296 gossip_chat_cut (GossipChat *chat)
1297 {
1298         GtkTextBuffer *buffer;
1299
1300         g_return_if_fail (GOSSIP_IS_CHAT (chat));
1301
1302         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1303         if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1304                 GtkClipboard *clipboard;
1305
1306                 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1307
1308                 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
1309         }
1310 }
1311
1312 void
1313 gossip_chat_copy (GossipChat *chat)
1314 {
1315         GtkTextBuffer *buffer;
1316
1317         g_return_if_fail (GOSSIP_IS_CHAT (chat));
1318
1319         if (gossip_chat_view_get_selection_bounds (chat->view, NULL, NULL)) {
1320                 gossip_chat_view_copy_clipboard (chat->view);
1321                 return;
1322         }
1323
1324         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1325         if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1326                 GtkClipboard *clipboard;
1327
1328                 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1329
1330                 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1331         }
1332 }
1333
1334 void
1335 gossip_chat_paste (GossipChat *chat)
1336 {
1337         GtkTextBuffer *buffer;
1338         GtkClipboard  *clipboard;
1339
1340         g_return_if_fail (GOSSIP_IS_CHAT (chat));
1341
1342         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1343         clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1344
1345         gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
1346 }
1347
1348 void
1349 gossip_chat_present (GossipChat *chat)
1350 {
1351         GossipChatPriv *priv;
1352
1353         g_return_if_fail (GOSSIP_IS_CHAT (chat));
1354
1355         priv = GET_PRIV (chat);
1356
1357         if (priv->window == NULL) {
1358                 GossipChatWindow *window;
1359
1360                 window = gossip_chat_window_get_default ();
1361                 if (!window) {
1362                         window = gossip_chat_window_new ();
1363                 }
1364
1365                 gossip_chat_window_add_chat (window, chat);
1366         }
1367
1368         gossip_chat_window_switch_to_chat (priv->window, chat);
1369         gossip_window_present (
1370                 GTK_WINDOW (gossip_chat_window_get_dialog (priv->window)),
1371                 TRUE);
1372
1373         gtk_widget_grab_focus (chat->input_text_view); 
1374 }
1375
1376 gboolean
1377 gossip_chat_should_play_sound (GossipChat *chat)
1378 {
1379         GossipChatWindow *window;
1380         gboolean          play = TRUE;
1381
1382         g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1383
1384         window = gossip_chat_get_window (chat);
1385         if (!window) {
1386                 return TRUE;
1387         }
1388
1389         play = !gossip_chat_window_has_focus (window);
1390
1391         return play;
1392 }
1393
1394 gboolean
1395 gossip_chat_should_highlight_nick (GossipMessage *message)
1396 {
1397         GossipContact *my_contact;
1398         const gchar   *msg, *to;
1399         gchar         *cf_msg, *cf_to;
1400         gchar         *ch;
1401         gboolean       ret_val;
1402
1403         g_return_val_if_fail (GOSSIP_IS_MESSAGE (message), FALSE);
1404
1405         gossip_debug (DEBUG_DOMAIN, "Highlighting nickname");
1406
1407         ret_val = FALSE;
1408
1409         msg = gossip_message_get_body (message);
1410         if (!msg) {
1411                 return FALSE;
1412         }
1413
1414         my_contact = gossip_get_own_contact_from_contact (gossip_message_get_sender (message));
1415         to = gossip_contact_get_name (my_contact);
1416         if (!to) {
1417                 return FALSE;
1418         }
1419
1420         cf_msg = g_utf8_casefold (msg, -1);
1421         cf_to = g_utf8_casefold (to, -1);
1422
1423         ch = strstr (cf_msg, cf_to);
1424         if (ch == NULL) {
1425                 goto finished;
1426         }
1427
1428         if (ch != cf_msg) {
1429                 /* Not first in the message */
1430                 if ((*(ch - 1) != ' ') &&
1431                     (*(ch - 1) != ',') &&
1432                     (*(ch - 1) != '.')) {
1433                         goto finished;
1434                 }
1435         }
1436
1437         ch = ch + strlen (cf_to);
1438         if (ch >= cf_msg + strlen (cf_msg)) {
1439                 ret_val = TRUE;
1440                 goto finished;
1441         }
1442
1443         if ((*ch == ' ') ||
1444             (*ch == ',') ||
1445             (*ch == '.') ||
1446             (*ch == ':')) {
1447                 ret_val = TRUE;
1448                 goto finished;
1449         }
1450
1451 finished:
1452         g_free (cf_msg);
1453         g_free (cf_to);
1454
1455         return ret_val;
1456 }
1457