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