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