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