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