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