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