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