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