]> git.0d.be Git - empathy.git/blob - libempathy-gtk/gossip-chat.c
[darcs-to-svn @ Handling /say msg and remove useless debug messages]
[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
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
537         /* Catch enter but not ctrl/shift-enter */
538         if (IS_ENTER (event->keyval) && !(event->state & GDK_SHIFT_MASK)) {
539                 GtkTextView *view;
540
541                 /* This is to make sure that kinput2 gets the enter. And if
542                  * it's handled there we shouldn't send on it. This is because
543                  * kinput2 uses Enter to commit letters. See:
544                  * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
545                  */
546
547                 view = GTK_TEXT_VIEW (chat->input_text_view);
548                 if (gtk_im_context_filter_keypress (view->im_context, event)) {
549                         GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
550                         return TRUE;
551                 }
552
553                 chat_input_text_view_send (chat);
554                 return TRUE;
555         }
556
557         text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
558
559         if (IS_ENTER (event->keyval) && (event->state & GDK_SHIFT_MASK)) {
560                 /* Newline for shift-enter. */
561                 return FALSE;
562         }
563         else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
564                  event->keyval == GDK_Page_Up) {
565                 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
566                 gtk_adjustment_set_value (adj, adj->value - adj->page_size);
567
568                 return TRUE;
569         }
570         else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
571                  event->keyval == GDK_Page_Down) {
572                 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
573                 val = MIN (adj->value + adj->page_size, adj->upper - adj->page_size);
574                 gtk_adjustment_set_value (adj, val);
575
576                 return TRUE;
577         }
578
579         return FALSE;
580 }
581
582 static gboolean
583 chat_text_view_focus_in_event_cb (GtkWidget  *widget,
584                                   GdkEvent   *event,
585                                   GossipChat *chat)
586 {
587         gtk_widget_grab_focus (chat->input_text_view);
588
589         return TRUE;
590 }
591
592 static void
593 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
594                                    GossipChat    *chat)
595 {
596         GossipChatPriv *priv;
597         GtkTextIter     start, end;
598         gchar          *str;
599         gboolean        spell_checker = FALSE;
600
601         priv = GET_PRIV (chat);
602
603         if (gtk_text_buffer_get_char_count (buffer) == 0) {
604                 chat_composing_stop (chat);
605         } else {
606                 chat_composing_start (chat);
607         }
608
609         gossip_conf_get_bool (gossip_conf_get (),
610                               GOSSIP_PREFS_CHAT_SPELL_CHECKER_ENABLED,
611                               &spell_checker);
612
613         if (chat->is_first_char) {
614                 GtkRequisition  req;
615                 gint            window_height;
616                 GtkWidget      *dialog;
617                 GtkAllocation  *allocation;
618
619                 /* Save the window's size */
620                 dialog = gossip_chat_window_get_dialog (priv->window);
621                 gtk_window_get_size (GTK_WINDOW (dialog),
622                                      NULL, &window_height);
623
624                 gtk_widget_size_request (chat->input_text_view, &req);
625
626                 allocation = &GTK_WIDGET (chat->view)->allocation;
627
628                 priv->default_window_height = window_height;
629                 priv->last_input_height = req.height;
630                 priv->padding_height = window_height - req.height - allocation->height;
631
632                 chat->is_first_char = FALSE;
633         }
634
635         gtk_text_buffer_get_start_iter (buffer, &start);
636
637         if (!spell_checker) {
638                 gtk_text_buffer_get_end_iter (buffer, &end);
639                 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
640                 return;
641         }
642
643         if (!gossip_spell_supported ()) {
644                 return;
645         }
646
647         /* NOTE: this is really inefficient, we shouldn't have to
648            reiterate the whole buffer each time and check each work
649            every time. */
650         while (TRUE) {
651                 gboolean correct = FALSE;
652
653                 /* if at start */
654                 if (gtk_text_iter_is_start (&start)) {
655                         end = start;
656
657                         if (!gtk_text_iter_forward_word_end (&end)) {
658                                 /* no whole word yet */
659                                 break;
660                         }
661                 } else {
662                         if (!gtk_text_iter_forward_word_end (&end)) {
663                                 /* must be the end of the buffer */
664                                 break;
665                         }
666
667                         start = end;
668                         gtk_text_iter_backward_word_start (&start);
669                 }
670
671                 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
672
673                 /* spell check string */
674                 if (!gossip_chat_get_is_command (str)) {
675                         correct = gossip_spell_check (str);
676                 } else {
677                         correct = TRUE;
678                 }
679
680                 if (!correct) {
681                         gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
682                 } else {
683                         gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
684                 }
685
686                 g_free (str);
687
688                 /* set start iter to the end iters position */
689                 start = end;
690         }
691 }
692
693 typedef struct {
694         GtkWidget *window;
695         gint       width;
696         gint       height;
697 } ChangeSizeData;
698
699 static gboolean
700 chat_change_size_in_idle_cb (ChangeSizeData *data)
701 {
702         gtk_window_resize (GTK_WINDOW (data->window),
703                            data->width, data->height);
704
705         return FALSE;
706 }
707
708 static void
709 chat_text_view_scroll_hide_cb (GtkWidget  *widget,
710                                GossipChat *chat)
711 {
712         GossipChatPriv *priv;
713         GtkWidget      *sw;
714
715         priv = GET_PRIV (chat);
716
717         priv->vscroll_visible = FALSE;
718         g_signal_handlers_disconnect_by_func (widget, chat_text_view_scroll_hide_cb, chat);
719
720         sw = gtk_widget_get_parent (chat->input_text_view);
721         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
722                                         GTK_POLICY_NEVER,
723                                         GTK_POLICY_NEVER);
724         g_object_set (sw, "height-request", -1, NULL);
725 }
726
727 static void
728 chat_text_view_size_allocate_cb (GtkWidget     *widget,
729                                  GtkAllocation *allocation,
730                                  GossipChat    *chat)
731 {
732         GossipChatPriv *priv;
733         gint            width;
734         GtkWidget      *dialog;
735         ChangeSizeData *data;
736         gint            window_height;
737         gint            new_height;
738         GtkAllocation  *view_allocation;
739         gint            current_height;
740         gint            diff;
741         GtkWidget      *sw;
742
743         priv = GET_PRIV (chat);
744
745         if (priv->default_window_height <= 0) {
746                 return;
747         }
748
749         sw = gtk_widget_get_parent (widget);
750         if (sw->allocation.height >= MAX_INPUT_HEIGHT && !priv->vscroll_visible) {
751                 GtkWidget *vscroll;
752
753                 priv->vscroll_visible = TRUE;
754                 gtk_widget_set_size_request (sw, sw->allocation.width, MAX_INPUT_HEIGHT);
755                 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
756                                                 GTK_POLICY_NEVER,
757                                                 GTK_POLICY_AUTOMATIC);
758                 vscroll = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (sw));
759                 g_signal_connect (vscroll, "hide",
760                                   G_CALLBACK (chat_text_view_scroll_hide_cb),
761                                   chat);
762         }
763
764         if (priv->last_input_height <= allocation->height) {
765                 priv->last_input_height = allocation->height;
766                 return;
767         }
768
769         diff = priv->last_input_height - allocation->height;
770         priv->last_input_height = allocation->height;
771
772         view_allocation = &GTK_WIDGET (chat->view)->allocation;
773
774         dialog = gossip_chat_window_get_dialog (priv->window);
775         gtk_window_get_size (GTK_WINDOW (dialog), NULL, &current_height);
776
777         new_height = view_allocation->height + priv->padding_height + allocation->height - diff;
778
779         if (new_height <= priv->default_window_height) {
780                 window_height = priv->default_window_height;
781         } else {
782                 window_height = new_height;
783         }
784
785         if (current_height <= window_height) {
786                 return;
787         }
788
789         /* Restore the window's size */
790         gtk_window_get_size (GTK_WINDOW (dialog), &width, NULL);
791
792         data = g_new0 (ChangeSizeData, 1);
793         data->window = dialog;
794         data->width  = width;
795         data->height = window_height;
796
797         g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
798                          (GSourceFunc) chat_change_size_in_idle_cb,
799                          data, g_free);
800 }
801
802 static void
803 chat_text_view_realize_cb (GtkWidget  *widget,
804                            GossipChat *chat)
805 {
806         gossip_debug (DEBUG_DOMAIN, "Setting focus to the input text view");
807         gtk_widget_grab_focus (widget);
808 }
809
810 static void
811 chat_insert_smiley_activate_cb (GtkWidget  *menuitem,
812                                 GossipChat *chat)
813 {
814         GtkTextBuffer *buffer;
815         GtkTextIter    iter;
816         const gchar   *smiley;
817
818         smiley = g_object_get_data (G_OBJECT (menuitem), "smiley_text");
819
820         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
821
822         gtk_text_buffer_get_end_iter (buffer, &iter);
823         gtk_text_buffer_insert (buffer, &iter, smiley, -1);
824
825         gtk_text_buffer_get_end_iter (buffer, &iter);
826         gtk_text_buffer_insert (buffer, &iter, " ", -1);
827 }
828
829 static void
830 chat_text_populate_popup_cb (GtkTextView *view,
831                              GtkMenu     *menu,
832                              GossipChat  *chat)
833 {
834         GossipChatPriv  *priv;
835         GtkTextBuffer   *buffer;
836         GtkTextTagTable *table;
837         GtkTextTag      *tag;
838         gint             x, y;
839         GtkTextIter      iter, start, end;
840         GtkWidget       *item;
841         gchar           *str = NULL;
842         GossipChatSpell *chat_spell;
843         GtkWidget       *smiley_menu;
844
845         priv = GET_PRIV (chat);
846
847         /* Add the emoticon menu. */
848         item = gtk_separator_menu_item_new ();
849         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
850         gtk_widget_show (item);
851
852         item = gtk_menu_item_new_with_mnemonic (_("Insert Smiley"));
853         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
854         gtk_widget_show (item);
855
856         smiley_menu = gossip_chat_view_get_smiley_menu (
857                 G_CALLBACK (chat_insert_smiley_activate_cb),
858                 chat,
859                 priv->tooltips);
860         gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
861
862         /* Add the spell check menu item. */
863         buffer = gtk_text_view_get_buffer (view);
864         table = gtk_text_buffer_get_tag_table (buffer);
865
866         tag = gtk_text_tag_table_lookup (table, "misspelled");
867
868         gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
869
870         gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
871                                                GTK_TEXT_WINDOW_WIDGET,
872                                                x, y,
873                                                &x, &y);
874
875         gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
876
877         start = end = iter;
878
879         if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
880             gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
881
882                 str = gtk_text_buffer_get_text (buffer,
883                                                 &start, &end, FALSE);
884         }
885
886         if (G_STR_EMPTY (str)) {
887                 return;
888         }
889
890         chat_spell = chat_spell_new (chat, str, start, end);
891
892         g_object_set_data_full (G_OBJECT (menu),
893                                 "chat_spell", chat_spell,
894                                 (GDestroyNotify) chat_spell_free);
895
896         item = gtk_separator_menu_item_new ();
897         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
898         gtk_widget_show (item);
899
900         item = gtk_menu_item_new_with_mnemonic (_("_Check Word Spelling..."));
901         g_signal_connect (item,
902                           "activate",
903                           G_CALLBACK (chat_text_check_word_spelling_cb),
904                           chat_spell);
905         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
906         gtk_widget_show (item);
907 }
908
909 static void
910 chat_text_check_word_spelling_cb (GtkMenuItem     *menuitem,
911                                   GossipChatSpell *chat_spell)
912 {
913 /*FIXME:        gossip_spell_dialog_show (chat_spell->chat,
914                                   chat_spell->start,
915                                   chat_spell->end,
916                                   chat_spell->word);*/
917 }
918
919 static GossipChatSpell *
920 chat_spell_new (GossipChat  *chat,
921                 const gchar *word,
922                 GtkTextIter  start,
923                 GtkTextIter  end)
924 {
925         GossipChatSpell *chat_spell;
926
927         chat_spell = g_new0 (GossipChatSpell, 1);
928
929         chat_spell->chat = g_object_ref (chat);
930         chat_spell->word = g_strdup (word);
931         chat_spell->start = start;
932         chat_spell->end = end;
933
934         return chat_spell;
935 }
936
937 static void
938 chat_spell_free (GossipChatSpell *chat_spell)
939 {
940         g_object_unref (chat_spell->chat);
941         g_free (chat_spell->word);
942         g_free (chat_spell);
943 }
944
945 static void
946 chat_composing_start (GossipChat *chat)
947 {
948         GossipChatPriv *priv;
949
950         priv = GET_PRIV (chat);
951
952         if (priv->composing_stop_timeout_id) {
953                 /* Just restart the timeout */
954                 chat_composing_remove_timeout (chat);
955         } else {
956         /* FIXME:
957                 gossip_session_send_composing (gossip_app_get_session (),
958                                                priv->contact, TRUE);
959                                               */
960         }
961
962         priv->composing_stop_timeout_id = g_timeout_add (
963                 1000 * COMPOSING_STOP_TIMEOUT,
964                 (GSourceFunc) chat_composing_stop_timeout_cb,
965                 chat);
966 }
967
968 static void
969 chat_composing_stop (GossipChat *chat)
970 {
971         GossipChatPriv *priv;
972
973         priv = GET_PRIV (chat);
974
975         chat_composing_remove_timeout (chat);
976         /* FIXME:
977         gossip_session_send_composing (gossip_app_get_session (),
978                                        priv->contact, FALSE);*/
979 }
980
981 static void
982 chat_composing_remove_timeout (GossipChat *chat)
983 {
984         GossipChatPriv *priv;
985
986         priv = GET_PRIV (chat);
987
988         if (priv->composing_stop_timeout_id) {
989                 g_source_remove (priv->composing_stop_timeout_id);
990                 priv->composing_stop_timeout_id = 0;
991         }
992 }
993
994 static gboolean
995 chat_composing_stop_timeout_cb (GossipChat *chat)
996 {
997         GossipChatPriv *priv;
998
999         priv = GET_PRIV (chat);
1000
1001         priv->composing_stop_timeout_id = 0;
1002         /* FIXME:
1003         gossip_session_send_composing (gossip_app_get_session (),
1004                                        priv->contact, FALSE);*/
1005
1006         return FALSE;
1007 }
1008
1009 gboolean
1010 gossip_chat_get_is_command (const gchar *str)
1011 {
1012         g_return_val_if_fail (str != NULL, FALSE);
1013
1014         if (str[0] != '/') {
1015                 return FALSE;
1016         }
1017
1018         if (g_str_has_prefix (str, "/me")) {
1019                 return TRUE;
1020         }
1021         else if (g_str_has_prefix (str, "/nick")) {
1022                 return TRUE;
1023         }
1024         else if (g_str_has_prefix (str, "/topic")) {
1025                 return TRUE;
1026         }
1027
1028         return FALSE;
1029 }
1030
1031 void
1032 gossip_chat_correct_word (GossipChat  *chat,
1033                           GtkTextIter  start,
1034                           GtkTextIter  end,
1035                           const gchar *new_word)
1036 {
1037         GtkTextBuffer *buffer;
1038
1039         g_return_if_fail (chat != NULL);
1040         g_return_if_fail (new_word != NULL);
1041
1042         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1043
1044         gtk_text_buffer_delete (buffer, &start, &end);
1045         gtk_text_buffer_insert (buffer, &start,
1046                                 new_word,
1047                                 -1);
1048 }
1049
1050 const gchar *
1051 gossip_chat_get_name (GossipChat *chat)
1052 {
1053         g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1054
1055         if (GOSSIP_CHAT_GET_CLASS (chat)->get_name) {
1056                 return GOSSIP_CHAT_GET_CLASS (chat)->get_name (chat);
1057         }
1058
1059         return NULL;
1060 }
1061
1062 gchar *
1063 gossip_chat_get_tooltip (GossipChat *chat)
1064 {
1065         g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1066
1067         if (GOSSIP_CHAT_GET_CLASS (chat)->get_tooltip) {
1068                 return GOSSIP_CHAT_GET_CLASS (chat)->get_tooltip (chat);
1069         }
1070
1071         return NULL;
1072 }
1073
1074 GdkPixbuf *
1075 gossip_chat_get_status_pixbuf (GossipChat *chat)
1076 {
1077         g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1078
1079         if (GOSSIP_CHAT_GET_CLASS (chat)->get_status_pixbuf) {
1080                 return GOSSIP_CHAT_GET_CLASS (chat)->get_status_pixbuf (chat);
1081         }
1082
1083         return NULL;
1084 }
1085
1086 GossipContact *
1087 gossip_chat_get_contact (GossipChat *chat)
1088 {
1089         g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1090
1091         if (GOSSIP_CHAT_GET_CLASS (chat)->get_contact) {
1092                 return GOSSIP_CHAT_GET_CLASS (chat)->get_contact (chat);
1093         }
1094
1095         return NULL;
1096 }
1097 GossipContact *
1098 gossip_chat_get_own_contact (GossipChat *chat)
1099 {
1100         EmpathyContactManager *manager;
1101
1102         g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1103
1104         manager = empathy_session_get_contact_manager ();
1105
1106         return empathy_contact_manager_get_own (manager, chat->account);
1107 }
1108
1109 GtkWidget *
1110 gossip_chat_get_widget (GossipChat *chat)
1111 {
1112         g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1113
1114         if (GOSSIP_CHAT_GET_CLASS (chat)->get_widget) {
1115                 return GOSSIP_CHAT_GET_CLASS (chat)->get_widget (chat);
1116         }
1117
1118         return NULL;
1119 }
1120
1121 gboolean
1122 gossip_chat_is_group_chat (GossipChat *chat)
1123 {
1124         g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1125
1126         if (GOSSIP_CHAT_GET_CLASS (chat)->is_group_chat) {
1127                 return GOSSIP_CHAT_GET_CLASS (chat)->is_group_chat (chat);
1128         }
1129
1130         return FALSE;
1131 }
1132
1133 gboolean 
1134 gossip_chat_is_connected (GossipChat *chat)
1135 {
1136         GossipChatPriv *priv;
1137
1138         g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1139
1140         priv = GET_PRIV (chat);
1141
1142         return (priv->tp_chat != NULL);
1143 }
1144
1145 gboolean
1146 gossip_chat_get_show_contacts (GossipChat *chat)
1147 {
1148         g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1149
1150         if (GOSSIP_CHAT_GET_CLASS (chat)->get_show_contacts) {
1151                 return GOSSIP_CHAT_GET_CLASS (chat)->get_show_contacts (chat);
1152         }
1153
1154         return FALSE;
1155 }
1156
1157 void
1158 gossip_chat_set_show_contacts (GossipChat *chat,
1159                                gboolean    show)
1160 {
1161         g_return_if_fail (GOSSIP_IS_CHAT (chat));
1162
1163         if (GOSSIP_CHAT_GET_CLASS (chat)->set_show_contacts) {
1164                 GOSSIP_CHAT_GET_CLASS (chat)->set_show_contacts (chat, show);
1165         }
1166 }
1167
1168 void
1169 gossip_chat_save_geometry (GossipChat *chat,
1170                            gint        x,
1171                            gint        y,
1172                            gint        w,
1173                            gint        h)
1174 {
1175         gossip_geometry_save (gossip_chat_get_id (chat), x, y, w, h);
1176 }
1177
1178 void
1179 gossip_chat_load_geometry (GossipChat *chat,
1180                            gint       *x,
1181                            gint       *y,
1182                            gint       *w,
1183                            gint       *h)
1184 {
1185         gossip_geometry_load (gossip_chat_get_id (chat), x, y, w, h);
1186 }
1187
1188 void
1189 gossip_chat_set_tp_chat (GossipChat    *chat,
1190                          EmpathyTpChat *tp_chat)
1191 {
1192         GossipChatPriv *priv;
1193         GtkWidget      *widget;
1194
1195         g_return_if_fail (GOSSIP_IS_CHAT (chat));
1196         g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
1197
1198         priv = GET_PRIV (chat);
1199
1200         if (tp_chat == priv->tp_chat) {
1201                 return;
1202         }
1203
1204         if (priv->tp_chat) {
1205                 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1206                                                       chat_message_received_cb,
1207                                                       chat);
1208                 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1209                                                       chat_destroy_cb,
1210                                                       chat);
1211                 g_object_unref (priv->tp_chat);
1212         }
1213
1214         g_free (priv->id);
1215         priv->tp_chat = g_object_ref (tp_chat);
1216         priv->id = g_strdup (empathy_tp_chat_get_id (tp_chat));
1217
1218         g_signal_connect (tp_chat, "message-received",
1219                           G_CALLBACK (chat_message_received_cb),
1220                           chat);
1221         g_signal_connect (tp_chat, "destroy",
1222                           G_CALLBACK (chat_destroy_cb),
1223                           chat);
1224
1225         empathy_tp_chat_request_pending (tp_chat);
1226
1227         if (!priv->sensitive) {
1228                 widget = gossip_chat_get_widget (chat);
1229                 gtk_widget_set_sensitive (widget, TRUE);
1230                 gossip_chat_view_append_event (chat->view, _("Connected"));
1231                 priv->sensitive = TRUE;
1232         }
1233 }
1234
1235 const gchar *
1236 gossip_chat_get_id (GossipChat *chat)
1237 {
1238         GossipChatPriv *priv;
1239
1240         priv = GET_PRIV (chat);
1241
1242         return priv->id;
1243 }
1244
1245 void
1246 gossip_chat_clear (GossipChat *chat)
1247 {
1248         g_return_if_fail (GOSSIP_IS_CHAT (chat));
1249
1250         gossip_chat_view_clear (chat->view);
1251 }
1252
1253 void
1254 gossip_chat_set_window (GossipChat       *chat,
1255                         GossipChatWindow *window)
1256 {
1257         GossipChatPriv *priv;
1258
1259         priv = GET_PRIV (chat);
1260         priv->window = window;
1261 }
1262
1263 GossipChatWindow *
1264 gossip_chat_get_window (GossipChat *chat)
1265 {
1266         GossipChatPriv *priv;
1267
1268         priv = GET_PRIV (chat);
1269
1270         return priv->window;
1271 }
1272
1273 void
1274 gossip_chat_scroll_down (GossipChat *chat)
1275 {
1276         g_return_if_fail (GOSSIP_IS_CHAT (chat));
1277
1278         gossip_chat_view_scroll_down (chat->view);
1279 }
1280
1281 void
1282 gossip_chat_cut (GossipChat *chat)
1283 {
1284         GtkTextBuffer *buffer;
1285
1286         g_return_if_fail (GOSSIP_IS_CHAT (chat));
1287
1288         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1289         if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1290                 GtkClipboard *clipboard;
1291
1292                 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1293
1294                 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
1295         }
1296 }
1297
1298 void
1299 gossip_chat_copy (GossipChat *chat)
1300 {
1301         GtkTextBuffer *buffer;
1302
1303         g_return_if_fail (GOSSIP_IS_CHAT (chat));
1304
1305         if (gossip_chat_view_get_selection_bounds (chat->view, NULL, NULL)) {
1306                 gossip_chat_view_copy_clipboard (chat->view);
1307                 return;
1308         }
1309
1310         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1311         if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1312                 GtkClipboard *clipboard;
1313
1314                 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1315
1316                 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1317         }
1318 }
1319
1320 void
1321 gossip_chat_paste (GossipChat *chat)
1322 {
1323         GtkTextBuffer *buffer;
1324         GtkClipboard  *clipboard;
1325
1326         g_return_if_fail (GOSSIP_IS_CHAT (chat));
1327
1328         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1329         clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1330
1331         gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
1332 }
1333
1334 void
1335 gossip_chat_present (GossipChat *chat)
1336 {
1337         GossipChatPriv *priv;
1338
1339         g_return_if_fail (GOSSIP_IS_CHAT (chat));
1340
1341         priv = GET_PRIV (chat);
1342
1343         if (priv->window == NULL) {
1344                 GossipChatWindow *window;
1345
1346                 window = gossip_chat_window_get_default ();
1347                 if (!window) {
1348                         window = gossip_chat_window_new ();
1349                 }
1350
1351                 gossip_chat_window_add_chat (window, chat);
1352         }
1353
1354         gossip_chat_window_switch_to_chat (priv->window, chat);
1355         gossip_window_present (
1356                 GTK_WINDOW (gossip_chat_window_get_dialog (priv->window)),
1357                 TRUE);
1358
1359         gtk_widget_grab_focus (chat->input_text_view); 
1360 }
1361
1362 gboolean
1363 gossip_chat_should_play_sound (GossipChat *chat)
1364 {
1365         GossipChatWindow *window;
1366         gboolean          play = TRUE;
1367
1368         g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1369
1370         window = gossip_chat_get_window (GOSSIP_CHAT (chat));
1371         if (!window) {
1372                 return TRUE;
1373         }
1374
1375         play = !gossip_chat_window_has_focus (window);
1376
1377         return play;
1378 }
1379
1380 gboolean
1381 gossip_chat_should_highlight_nick (GossipMessage *message)
1382 {
1383         GossipContact *my_contact;
1384         const gchar   *msg, *to;
1385         gchar         *cf_msg, *cf_to;
1386         gchar         *ch;
1387         gboolean       ret_val;
1388
1389         g_return_val_if_fail (GOSSIP_IS_MESSAGE (message), FALSE);
1390
1391         gossip_debug (DEBUG_DOMAIN, "Highlighting nickname");
1392
1393         ret_val = FALSE;
1394
1395         msg = gossip_message_get_body (message);
1396         if (!msg) {
1397                 return FALSE;
1398         }
1399
1400         my_contact = gossip_get_own_contact_from_contact (gossip_message_get_sender (message));
1401         to = gossip_contact_get_name (my_contact);
1402         if (!to) {
1403                 return FALSE;
1404         }
1405
1406         cf_msg = g_utf8_casefold (msg, -1);
1407         cf_to = g_utf8_casefold (to, -1);
1408
1409         ch = strstr (cf_msg, cf_to);
1410         if (ch == NULL) {
1411                 goto finished;
1412         }
1413
1414         if (ch != cf_msg) {
1415                 /* Not first in the message */
1416                 if ((*(ch - 1) != ' ') &&
1417                     (*(ch - 1) != ',') &&
1418                     (*(ch - 1) != '.')) {
1419                         goto finished;
1420                 }
1421         }
1422
1423         ch = ch + strlen (cf_to);
1424         if (ch >= cf_msg + strlen (cf_msg)) {
1425                 ret_val = TRUE;
1426                 goto finished;
1427         }
1428
1429         if ((*ch == ' ') ||
1430             (*ch == ',') ||
1431             (*ch == '.') ||
1432             (*ch == ':')) {
1433                 ret_val = TRUE;
1434                 goto finished;
1435         }
1436
1437 finished:
1438         g_free (cf_msg);
1439         g_free (cf_to);
1440
1441         return ret_val;
1442 }
1443