]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-chat.c
empathy-chat.c: input history - Using GList pointer instead of gint index.
[empathy.git] / libempathy-gtk / empathy-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-2008 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., 51 Franklin St, Fifth Floor,
19  * Boston, MA  02110-1301  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-lib.h>
35 #include <gtk/gtk.h>
36
37 #include <telepathy-glib/util.h>
38
39 #include <libempathy/empathy-account-manager.h>
40 #include <libempathy/empathy-log-manager.h>
41 #include <libempathy/empathy-contact-list.h>
42 #include <libempathy/empathy-utils.h>
43 #include <libempathy/empathy-dispatcher.h>
44
45 #include "empathy-chat.h"
46 #include "empathy-conf.h"
47 #include "empathy-spell.h"
48 #include "empathy-contact-list-store.h"
49 #include "empathy-contact-list-view.h"
50 #include "empathy-contact-menu.h"
51 #include "empathy-theme-manager.h"
52 #include "empathy-smiley-manager.h"
53 #include "empathy-ui-utils.h"
54
55 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
56 #include <libempathy/empathy-debug.h>
57
58 #define CHAT_DIR_CREATE_MODE  (S_IRUSR | S_IWUSR | S_IXUSR)
59 #define CHAT_FILE_CREATE_MODE (S_IRUSR | S_IWUSR)
60 #define IS_ENTER(v) (v == GDK_Return || v == GDK_ISO_Enter || v == GDK_KP_Enter)
61 #define MAX_INPUT_HEIGHT 150
62 #define COMPOSING_STOP_TIMEOUT 5
63
64 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChat)
65 typedef struct {
66         EmpathyTpChat     *tp_chat;
67         EmpathyAccount    *account;
68         gchar             *id;
69         gchar             *name;
70         gchar             *subject;
71         EmpathyContact    *remote_contact;
72         gboolean           show_contacts;
73
74         EmpathyLogManager *log_manager;
75         EmpathyAccountManager *account_manager;
76         GList             *input_history;
77         GList             *input_history_current;
78         GList             *compositors;
79         GCompletion       *completion;
80         guint              composing_stop_timeout_id;
81         guint              block_events_timeout_id;
82         TpHandleType       handle_type;
83         gint               contacts_width;
84         gboolean           has_input_vscroll;
85
86         GtkWidget         *widget;
87         GtkWidget         *hpaned;
88         GtkWidget         *vbox_left;
89         GtkWidget         *scrolled_window_chat;
90         GtkWidget         *scrolled_window_input;
91         GtkWidget         *scrolled_window_contacts;
92         GtkWidget         *hbox_topic;
93         GtkWidget         *label_topic;
94         GtkWidget         *contact_list_view;
95 } EmpathyChatPriv;
96
97 typedef struct {
98         gchar *text; /* Original message that was specified
99                       * upon entry creation. */
100         gchar *modified_text; /* Message that was modified by user.
101                                * When no modifications were made, it is NULL */
102 } InputHistoryEntry;
103
104 enum {
105         COMPOSING,
106         NEW_MESSAGE,
107         LAST_SIGNAL
108 };
109
110 enum {
111         PROP_0,
112         PROP_TP_CHAT,
113         PROP_ACCOUNT,
114         PROP_ID,
115         PROP_NAME,
116         PROP_SUBJECT,
117         PROP_REMOTE_CONTACT,
118         PROP_SHOW_CONTACTS,
119 };
120
121 static guint signals[LAST_SIGNAL] = { 0 };
122
123 G_DEFINE_TYPE (EmpathyChat, empathy_chat, GTK_TYPE_BIN);
124
125 static void
126 chat_get_property (GObject    *object,
127                    guint       param_id,
128                    GValue     *value,
129                    GParamSpec *pspec)
130 {
131         EmpathyChat *chat = EMPATHY_CHAT (object);
132         EmpathyChatPriv *priv = GET_PRIV (object);
133
134         switch (param_id) {
135         case PROP_TP_CHAT:
136                 g_value_set_object (value, priv->tp_chat);
137                 break;
138         case PROP_ACCOUNT:
139                 g_value_set_object (value, priv->account);
140                 break;
141         case PROP_NAME:
142                 g_value_set_string (value, empathy_chat_get_name (chat));
143                 break;
144         case PROP_ID:
145                 g_value_set_string (value, priv->id);
146                 break;
147         case PROP_SUBJECT:
148                 g_value_set_string (value, priv->subject);
149                 break;
150         case PROP_REMOTE_CONTACT:
151                 g_value_set_object (value, priv->remote_contact);
152                 break;
153         case PROP_SHOW_CONTACTS:
154                 g_value_set_boolean (value, priv->show_contacts);
155                 break;
156         default:
157                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
158                 break;
159         };
160 }
161
162 static void
163 chat_set_property (GObject      *object,
164                    guint         param_id,
165                    const GValue *value,
166                    GParamSpec   *pspec)
167 {
168         EmpathyChat *chat = EMPATHY_CHAT (object);
169
170         switch (param_id) {
171         case PROP_TP_CHAT:
172                 empathy_chat_set_tp_chat (chat, EMPATHY_TP_CHAT (g_value_get_object (value)));
173                 break;
174         case PROP_SHOW_CONTACTS:
175                 empathy_chat_set_show_contacts (chat, g_value_get_boolean (value));
176                 break;
177         default:
178                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
179                 break;
180         };
181 }
182
183 static void
184 chat_connect_channel_reconnected (EmpathyDispatchOperation *dispatch,
185                                   const GError             *error,
186                                   gpointer                  user_data)
187 {
188         EmpathyChat *chat = EMPATHY_CHAT (user_data);
189         EmpathyTpChat *tpchat;
190
191         if (error != NULL) {
192                 empathy_chat_view_append_event (chat->view,
193                         _("Failed to reconnect this chat"));
194                 return;
195         }
196
197         tpchat = EMPATHY_TP_CHAT (
198                 empathy_dispatch_operation_get_channel_wrapper (dispatch));
199
200         if (empathy_dispatch_operation_claim (dispatch)) {
201                 empathy_chat_set_tp_chat (chat, tpchat);
202         }
203 }
204
205 static void
206 chat_new_connection_cb (EmpathyAccountManager *manager,
207                         TpConnection *connection,
208                         EmpathyChat *chat)
209 {
210         EmpathyChatPriv *priv = GET_PRIV (chat);
211         EmpathyAccount *account;
212
213         account = empathy_account_manager_get_account_for_connection (manager,
214                 connection);
215         if (!priv->tp_chat && account == priv->account &&
216             priv->handle_type != TP_HANDLE_TYPE_NONE &&
217             !EMP_STR_EMPTY (priv->id)) {
218
219                 DEBUG ("Account reconnected, request a new Text channel");
220
221                 switch (priv->handle_type) {
222                         case TP_HANDLE_TYPE_CONTACT:
223                                 empathy_dispatcher_chat_with_contact_id (
224                                         connection, priv->id,
225                                         chat_connect_channel_reconnected,
226                                         chat);
227                                 break;
228                         case TP_HANDLE_TYPE_ROOM:
229                                 empathy_dispatcher_join_muc (connection,
230                                         priv->id,
231                                         chat_connect_channel_reconnected,
232                                         chat);
233                                 break;
234                         default:
235                                 g_assert_not_reached ();
236                                 break;
237                 }
238         }
239 }
240
241 static void
242 chat_composing_remove_timeout (EmpathyChat *chat)
243 {
244         EmpathyChatPriv *priv;
245
246         priv = GET_PRIV (chat);
247
248         if (priv->composing_stop_timeout_id) {
249                 g_source_remove (priv->composing_stop_timeout_id);
250                 priv->composing_stop_timeout_id = 0;
251         }
252 }
253
254 static gboolean
255 chat_composing_stop_timeout_cb (EmpathyChat *chat)
256 {
257         EmpathyChatPriv *priv;
258
259         priv = GET_PRIV (chat);
260
261         priv->composing_stop_timeout_id = 0;
262         empathy_tp_chat_set_state (priv->tp_chat,
263                                    TP_CHANNEL_CHAT_STATE_PAUSED);
264
265         return FALSE;
266 }
267
268 static void
269 chat_composing_start (EmpathyChat *chat)
270 {
271         EmpathyChatPriv *priv;
272
273         priv = GET_PRIV (chat);
274
275         if (priv->composing_stop_timeout_id) {
276                 /* Just restart the timeout */
277                 chat_composing_remove_timeout (chat);
278         } else {
279                 empathy_tp_chat_set_state (priv->tp_chat,
280                                            TP_CHANNEL_CHAT_STATE_COMPOSING);
281         }
282
283         priv->composing_stop_timeout_id = g_timeout_add_seconds (
284                 COMPOSING_STOP_TIMEOUT,
285                 (GSourceFunc) chat_composing_stop_timeout_cb,
286                 chat);
287 }
288
289 static void
290 chat_composing_stop (EmpathyChat *chat)
291 {
292         EmpathyChatPriv *priv;
293
294         priv = GET_PRIV (chat);
295
296         chat_composing_remove_timeout (chat);
297         empathy_tp_chat_set_state (priv->tp_chat,
298                                    TP_CHANNEL_CHAT_STATE_ACTIVE);
299 }
300
301 static gint
302 chat_input_history_entry_cmp (InputHistoryEntry *entry,
303                               const gchar *text)
304 {
305         if (!tp_strdiff (entry->text, text)) {
306                 if (entry->modified_text != NULL) {
307                         /* Modified entry and single string cannot be equal. */
308                         return 1;
309                 }
310                 return 0;
311         }
312         return 1;
313 }
314
315 static InputHistoryEntry *
316 chat_input_history_entry_new_with_text (const gchar *text)
317 {
318         InputHistoryEntry *entry;
319         entry = g_slice_new0 (InputHistoryEntry);
320         entry->text = g_strdup (text);
321
322         return entry;
323 }
324
325 static void
326 chat_input_history_entry_free (InputHistoryEntry *entry)
327 {
328         g_free (entry->text);
329         g_free (entry->modified_text);
330         g_slice_free (InputHistoryEntry, entry);
331 }
332
333 static void
334 chat_input_history_entry_revert (InputHistoryEntry *entry)
335 {
336         g_free (entry->modified_text);
337         entry->modified_text = NULL;
338 }
339
340 static void
341 chat_input_history_entry_update_text (InputHistoryEntry *entry,
342                                       const gchar *text)
343 {
344         gchar *old;
345
346         if (!tp_strdiff (text, entry->text)) {
347                 g_free (entry->modified_text);
348                 entry->modified_text = NULL;
349                 return;
350         }
351
352         old = entry->modified_text;
353         entry->modified_text = g_strdup (text);
354         g_free (old);
355 }
356
357 static const gchar *
358 chat_input_history_entry_get_text (InputHistoryEntry *entry)
359 {
360         if (entry == NULL) {
361                 return NULL;
362         }
363
364         if (entry->modified_text != NULL) {
365                 return entry->modified_text;
366         }
367         return entry->text;
368 }
369
370 static GList *
371 chat_input_history_remove_item (GList *list,
372                                 GList *item)
373 {
374         list = g_list_remove_link (list, item);
375         chat_input_history_entry_free (item->data);
376         g_list_free_1 (item);
377         return list;
378 }
379
380 static void
381 chat_input_history_revert (EmpathyChat *chat)
382 {
383         EmpathyChatPriv   *priv;
384         GList             *list;
385         GList             *item1;
386         GList             *item2;
387         InputHistoryEntry *entry;
388
389         priv = GET_PRIV (chat);
390         list = priv->input_history;
391
392         if (list == NULL) {
393                 DEBUG ("No input history");
394                 return;
395         }
396
397         /* Delete temporary entry */
398         if (priv->input_history_current != NULL) {
399                 item1 = list;
400                 list = chat_input_history_remove_item (list, item1);
401                 if (priv->input_history_current == item1) {
402                         /* Removed temporary entry was current entry */
403                         priv->input_history = list;
404                         priv->input_history_current = NULL;
405                         return;
406                 }
407         }
408         else {
409                 /* There is no entry to revert */
410                 return;
411         }
412
413         /* Restore the current history entry to original value */
414         item1 = priv->input_history_current;
415         entry = item1->data;
416         chat_input_history_entry_revert (entry);
417
418         /* Remove restored entry if there is other occurance before this entry */
419         item2 = g_list_find_custom (list, chat_input_history_entry_get_text (entry),
420                                     (GCompareFunc) chat_input_history_entry_cmp);
421         if (item2 != item1) {
422                 list = chat_input_history_remove_item (list, item1);
423         }
424         else {
425                 /* Remove other occurance of the restored entry */
426                 item2 = g_list_find_custom (item1->next,
427                                             chat_input_history_entry_get_text (entry),
428                                             (GCompareFunc) chat_input_history_entry_cmp);
429                 if (item2 != NULL) {
430                         list = chat_input_history_remove_item (list, item2);
431                 }
432         }
433
434         priv->input_history_current = NULL;
435         priv->input_history = list;
436 }
437
438 static void
439 chat_input_history_add (EmpathyChat  *chat,
440                         const gchar *str,
441                         gboolean temporary)
442 {
443         EmpathyChatPriv   *priv;
444         GList             *list;
445         GList             *item;
446         InputHistoryEntry *entry;
447
448         priv = GET_PRIV (chat);
449
450         list = priv->input_history;
451
452         /* Remove any other occurances of this entry, if not temporary */
453         if (!temporary) {
454                 while ((item = g_list_find_custom (list, str,
455                     (GCompareFunc) chat_input_history_entry_cmp)) != NULL) {
456                         list = chat_input_history_remove_item (list, item);
457                 }
458
459                 /* Trim the list to the last 10 items */
460                 while (g_list_length (list) > 10) {
461                         item = g_list_last (list);
462                         if (item != NULL) {
463                                 list = chat_input_history_remove_item (list, item);
464                         }
465                 }
466         }
467
468
469
470         /* Add new entry */
471         entry = chat_input_history_entry_new_with_text (str);
472         list = g_list_prepend (list, entry);
473
474         /* Set the list and the current item pointer */
475         priv->input_history = list;
476         if (temporary) {
477                 priv->input_history_current = list;
478         }
479         else {
480                 priv->input_history_current = NULL;
481         }
482 }
483
484 static const gchar *
485 chat_input_history_get_next (EmpathyChat *chat)
486 {
487         EmpathyChatPriv *priv;
488         GList           *item;
489         const gchar     *msg;
490
491         priv = GET_PRIV (chat);
492
493         if (priv->input_history == NULL) {
494                 DEBUG ("No input history, next entry is NULL");
495                 return NULL;
496         }
497         g_assert (priv->input_history_current != NULL);
498
499         if ((item = g_list_next (priv->input_history_current)) == NULL)
500         {
501                 item = priv->input_history_current;
502         }
503
504         msg = chat_input_history_entry_get_text (item->data);
505
506         DEBUG ("Returning next entry: '%s'", msg);
507
508         priv->input_history_current = item;
509
510         return msg;
511 }
512
513 static const gchar *
514 chat_input_history_get_prev (EmpathyChat *chat)
515 {
516         EmpathyChatPriv *priv;
517         GList           *item;
518         const gchar     *msg;
519
520         g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
521
522         priv = GET_PRIV (chat);
523
524         if (priv->input_history == NULL) {
525                 DEBUG ("No input history, previous entry is NULL");
526                 return NULL;
527         }
528
529         if (priv->input_history_current == NULL)
530         {
531                 return NULL;
532         }
533         else if ((item = g_list_previous (priv->input_history_current)) == NULL)
534         {
535                 item = priv->input_history_current;
536         }
537
538         msg = chat_input_history_entry_get_text (item->data);
539
540         DEBUG ("Returning previous entry: '%s'", msg);
541
542         priv->input_history_current = item;
543
544         return msg;
545 }
546
547 static void
548 chat_input_history_update (EmpathyChat *chat,
549                            GtkTextBuffer *buffer)
550 {
551         EmpathyChatPriv      *priv;
552         GtkTextIter           start, end;
553         gchar                *text;
554         InputHistoryEntry    *entry;
555
556         priv = GET_PRIV (chat);
557
558         gtk_text_buffer_get_bounds (buffer, &start, &end);
559         text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
560
561         if (priv->input_history_current == NULL) {
562                 /* Add the current text temporarily to the history */
563                 chat_input_history_add (chat, text, TRUE);
564                 g_free (text);
565                 return;
566         }
567
568         /* Save the changes in the history */
569         entry = priv->input_history_current->data;
570         if (tp_strdiff (chat_input_history_entry_get_text (entry), text)) {
571                 chat_input_history_entry_update_text (entry, text);
572         }
573
574         g_free (text);
575 }
576
577 static void
578 chat_send (EmpathyChat  *chat,
579            const gchar *msg)
580 {
581         EmpathyChatPriv *priv;
582         EmpathyMessage  *message;
583
584         if (EMP_STR_EMPTY (msg)) {
585                 return;
586         }
587
588         priv = GET_PRIV (chat);
589
590         chat_input_history_add (chat, msg, FALSE);
591
592         if (strcmp (msg, "/clear") == 0) {
593                 empathy_chat_view_clear (chat->view);
594                 return;
595         }
596
597         message = empathy_message_new_from_entry (msg);
598
599         if (message == NULL) {
600                 empathy_chat_view_append_event (chat->view,
601                         _("Unsupported command"));
602         } else {
603                 empathy_tp_chat_send (priv->tp_chat, message);
604                 g_object_unref (message);
605         }
606 }
607
608 static void
609 chat_input_text_view_send (EmpathyChat *chat)
610 {
611         EmpathyChatPriv *priv;
612         GtkTextBuffer  *buffer;
613         GtkTextIter     start, end;
614         gchar          *msg;
615
616         priv = GET_PRIV (chat);
617
618         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
619
620         gtk_text_buffer_get_bounds (buffer, &start, &end);
621         msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
622
623         /* clear the input field */
624         gtk_text_buffer_set_text (buffer, "", -1);
625         /* delete input history modifications */
626         chat_input_history_revert (chat);
627
628         chat_send (chat, msg);
629         g_free (msg);
630 }
631
632 static void
633 chat_state_changed_cb (EmpathyTpChat      *tp_chat,
634                        EmpathyContact     *contact,
635                        TpChannelChatState  state,
636                        EmpathyChat        *chat)
637 {
638         EmpathyChatPriv *priv;
639         GList          *l;
640         gboolean        was_composing;
641
642         priv = GET_PRIV (chat);
643
644         if (empathy_contact_is_user (contact)) {
645                 /* We don't care about our own chat state */
646                 return;
647         }
648
649         was_composing = (priv->compositors != NULL);
650
651         /* Find the contact in the list. After that l is the list elem or NULL */
652         for (l = priv->compositors; l; l = l->next) {
653                 if (contact == l->data) {
654                         break;
655                 }
656         }
657
658         switch (state) {
659         case TP_CHANNEL_CHAT_STATE_GONE:
660         case TP_CHANNEL_CHAT_STATE_INACTIVE:
661         case TP_CHANNEL_CHAT_STATE_PAUSED:
662         case TP_CHANNEL_CHAT_STATE_ACTIVE:
663                 /* Contact is not composing */
664                 if (l) {
665                         priv->compositors = g_list_remove_link (priv->compositors, l);
666                         g_object_unref (l->data);
667                         g_list_free1 (l);
668                 }
669                 break;
670         case TP_CHANNEL_CHAT_STATE_COMPOSING:
671                 /* Contact is composing */
672                 if (!l) {
673                         priv->compositors = g_list_prepend (priv->compositors,
674                                                             g_object_ref (contact));
675                 }
676                 break;
677         default:
678                 g_assert_not_reached ();
679         }
680
681         DEBUG ("Was composing: %s now composing: %s",
682                 was_composing ? "yes" : "no",
683                 priv->compositors ? "yes" : "no");
684
685         if ((was_composing && !priv->compositors) ||
686             (!was_composing && priv->compositors)) {
687                 /* Composing state changed */
688                 g_signal_emit (chat, signals[COMPOSING], 0,
689                                priv->compositors != NULL);
690         }
691 }
692
693 static void
694 chat_message_received (EmpathyChat *chat, EmpathyMessage *message)
695 {
696         EmpathyChatPriv *priv = GET_PRIV (chat);
697         EmpathyContact  *sender;
698
699         sender = empathy_message_get_sender (message);
700
701         DEBUG ("Appending new message from %s (%d)",
702                 empathy_contact_get_name (sender),
703                 empathy_contact_get_handle (sender));
704
705         empathy_chat_view_append_message (chat->view, message);
706
707         /* We received a message so the contact is no longer composing */
708         chat_state_changed_cb (priv->tp_chat, sender,
709                                TP_CHANNEL_CHAT_STATE_ACTIVE,
710                                chat);
711
712         g_signal_emit (chat, signals[NEW_MESSAGE], 0, message);
713 }
714
715 static void
716 chat_message_received_cb (EmpathyTpChat  *tp_chat,
717                           EmpathyMessage *message,
718                           EmpathyChat    *chat)
719 {
720         chat_message_received (chat, message);
721         empathy_tp_chat_acknowledge_message (tp_chat, message);
722 }
723
724 static void
725 chat_send_error_cb (EmpathyTpChat          *tp_chat,
726                     const gchar            *message_body,
727                     TpChannelTextSendError  error_code,
728                     EmpathyChat            *chat)
729 {
730         const gchar *error;
731         gchar       *str;
732
733         switch (error_code) {
734         case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
735                 error = _("offline");
736                 break;
737         case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
738                 error = _("invalid contact");
739                 break;
740         case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
741                 error = _("permission denied");
742                 break;
743         case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
744                 error = _("too long message");
745                 break;
746         case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
747                 error = _("not implemented");
748                 break;
749         default:
750                 error = _("unknown");
751                 break;
752         }
753
754         str = g_strdup_printf (_("Error sending message '%s': %s"),
755                                message_body,
756                                error);
757         empathy_chat_view_append_event (chat->view, str);
758         g_free (str);
759 }
760
761 static void
762 chat_property_changed_cb (EmpathyTpChat *tp_chat,
763                           const gchar   *name,
764                           GValue        *value,
765                           EmpathyChat   *chat)
766 {
767         EmpathyChatPriv *priv = GET_PRIV (chat);
768
769         if (!tp_strdiff (name, "subject")) {
770                 g_free (priv->subject);
771                 priv->subject = g_value_dup_string (value);
772                 g_object_notify (G_OBJECT (chat), "subject");
773
774                 if (EMP_STR_EMPTY (priv->subject)) {
775                         gtk_widget_hide (priv->hbox_topic);
776                 } else {
777                         gtk_label_set_text (GTK_LABEL (priv->label_topic), priv->subject);
778                         gtk_widget_show (priv->hbox_topic);
779                 }
780                 if (priv->block_events_timeout_id == 0) {
781                         gchar *str;
782
783                         if (!EMP_STR_EMPTY (priv->subject)) {
784                                 str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
785                         } else {
786                                 str = g_strdup (_("No topic defined"));
787                         }
788                         empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, str);
789                         g_free (str);
790                 }
791         }
792         else if (!tp_strdiff (name, "name")) {
793                 g_free (priv->name);
794                 priv->name = g_value_dup_string (value);
795                 g_object_notify (G_OBJECT (chat), "name");
796         }
797 }
798
799 static void
800 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
801                                    EmpathyChat    *chat)
802 {
803         EmpathyChatPriv *priv;
804         GtkTextIter     start, end;
805         gchar          *str;
806         gboolean        spell_checker = FALSE;
807
808         priv = GET_PRIV (chat);
809
810         if (gtk_text_buffer_get_char_count (buffer) == 0) {
811                 chat_composing_stop (chat);
812         } else {
813                 chat_composing_start (chat);
814         }
815
816         empathy_conf_get_bool (empathy_conf_get (),
817                            EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
818                            &spell_checker);
819
820         gtk_text_buffer_get_start_iter (buffer, &start);
821
822         if (!spell_checker) {
823                 gtk_text_buffer_get_end_iter (buffer, &end);
824                 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
825                 return;
826         }
827
828         if (!empathy_spell_supported ()) {
829                 return;
830         }
831
832         /* NOTE: this is really inefficient, we shouldn't have to
833            reiterate the whole buffer each time and check each work
834            every time. */
835         while (TRUE) {
836                 gboolean correct = FALSE;
837
838                 /* if at start */
839                 if (gtk_text_iter_is_start (&start)) {
840                         end = start;
841
842                         if (!gtk_text_iter_forward_word_end (&end)) {
843                                 /* no whole word yet */
844                                 break;
845                         }
846                 } else {
847                         if (!gtk_text_iter_forward_word_end (&end)) {
848                                 /* must be the end of the buffer */
849                                 break;
850                         }
851
852                         start = end;
853                         gtk_text_iter_backward_word_start (&start);
854                 }
855
856                 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
857
858                 /* spell check string if not a command */
859                 if (str[0] != '/') {
860                         correct = empathy_spell_check (str);
861                 } else {
862                         correct = TRUE;
863                 }
864
865                 if (!correct) {
866                         gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
867                 } else {
868                         gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
869                 }
870
871                 g_free (str);
872
873                 /* set start iter to the end iters position */
874                 start = end;
875         }
876 }
877
878 static gboolean
879 chat_input_key_press_event_cb (GtkWidget   *widget,
880                                GdkEventKey *event,
881                                EmpathyChat *chat)
882 {
883         EmpathyChatPriv *priv;
884         GtkAdjustment  *adj;
885         gdouble         val;
886         GtkWidget      *text_view_sw;
887
888         priv = GET_PRIV (chat);
889
890         /* Catch ctrl+up/down so we can traverse messages we sent */
891         if ((event->state & GDK_CONTROL_MASK) &&
892             (event->keyval == GDK_Up ||
893              event->keyval == GDK_Down)) {
894                 GtkTextBuffer *buffer;
895                 const gchar   *str;
896
897                 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
898                 chat_input_history_update (chat, buffer);
899
900                 if (event->keyval == GDK_Up) {
901                         str = chat_input_history_get_next (chat);
902                 } else {
903                         str = chat_input_history_get_prev (chat);
904                 }
905
906                 g_signal_handlers_block_by_func (buffer,
907                                                  chat_input_text_buffer_changed_cb,
908                                                  chat);
909                 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
910                 g_signal_handlers_unblock_by_func (buffer,
911                                                    chat_input_text_buffer_changed_cb,
912                                                    chat);
913
914                 return TRUE;
915         }
916
917         /* Catch enter but not ctrl/shift-enter */
918         if (IS_ENTER (event->keyval) &&
919             !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
920                 GtkTextView *view;
921
922                 /* This is to make sure that kinput2 gets the enter. And if
923                  * it's handled there we shouldn't send on it. This is because
924                  * kinput2 uses Enter to commit letters. See:
925                  * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
926                  */
927
928                 view = GTK_TEXT_VIEW (chat->input_text_view);
929                 if (gtk_im_context_filter_keypress (view->im_context, event)) {
930                         GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
931                         return TRUE;
932                 }
933
934                 chat_input_text_view_send (chat);
935                 return TRUE;
936         }
937
938         text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
939
940         if (IS_ENTER (event->keyval) &&
941             (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
942                 /* Newline for shift/control-enter. */
943                 return FALSE;
944         }
945         if (!(event->state & GDK_CONTROL_MASK) &&
946             event->keyval == GDK_Page_Up) {
947                 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
948                 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) - gtk_adjustment_get_page_size (adj));
949                 return TRUE;
950         }
951         if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
952             event->keyval == GDK_Page_Down) {
953                 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
954                 val = MIN (gtk_adjustment_get_value (adj) + gtk_adjustment_get_page_size (adj),
955                            gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj));
956                 gtk_adjustment_set_value (adj, val);
957                 return TRUE;
958         }
959         if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
960             event->keyval == GDK_Tab) {
961                 GtkTextBuffer *buffer;
962                 GtkTextIter    start, current;
963                 gchar         *nick, *completed;
964                 GList         *list, *completed_list;
965                 gboolean       is_start_of_buffer;
966
967                 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
968                 gtk_text_buffer_get_iter_at_mark (buffer, &current, gtk_text_buffer_get_insert (buffer));
969
970                 /* Get the start of the nick to complete. */
971                 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
972                 gtk_text_iter_backward_word_start (&start);
973                 is_start_of_buffer = gtk_text_iter_is_start (&start);
974
975                 list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
976                 g_completion_add_items (priv->completion, list);
977
978                 nick = gtk_text_buffer_get_text (buffer, &start, &current, FALSE);
979                 completed_list = g_completion_complete (priv->completion,
980                                                         nick,
981                                                         &completed);
982
983                 g_free (nick);
984
985                 if (completed) {
986                         guint        len;
987                         const gchar *text;
988                         gchar       *complete_char = NULL;
989
990                         gtk_text_buffer_delete (buffer, &start, &current);
991
992                         len = g_list_length (completed_list);
993
994                         if (len == 1) {
995                                 /* If we only have one hit, use that text
996                                  * instead of the text in completed since the
997                                  * completed text will use the typed string
998                                  * which might be cased all wrong.
999                                  * Fixes #120876
1000                                  * */
1001                                 text = empathy_contact_get_name (completed_list->data);
1002                         } else {
1003                                 text = completed;
1004                         }
1005
1006                         gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
1007
1008                         if (len == 1 && is_start_of_buffer &&
1009                             empathy_conf_get_string (empathy_conf_get (),
1010                                                      EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR,
1011                                                      &complete_char) &&
1012                             complete_char != NULL) {
1013                                 gtk_text_buffer_insert_at_cursor (buffer,
1014                                                                   complete_char,
1015                                                                   strlen (complete_char));
1016                                 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
1017                                 g_free (complete_char);
1018                         }
1019
1020                         g_free (completed);
1021                 }
1022
1023                 g_completion_clear_items (priv->completion);
1024
1025                 g_list_foreach (list, (GFunc) g_object_unref, NULL);
1026                 g_list_free (list);
1027
1028                 return TRUE;
1029         }
1030
1031         return FALSE;
1032 }
1033
1034 static gboolean
1035 chat_text_view_focus_in_event_cb (GtkWidget  *widget,
1036                                   GdkEvent   *event,
1037                                   EmpathyChat *chat)
1038 {
1039         gtk_widget_grab_focus (chat->input_text_view);
1040
1041         return TRUE;
1042 }
1043
1044 static gboolean
1045 chat_input_set_size_request_idle (gpointer sw)
1046 {
1047         gtk_widget_set_size_request (sw, -1, MAX_INPUT_HEIGHT);
1048
1049         return FALSE;
1050 }
1051
1052 static void
1053 chat_input_size_request_cb (GtkWidget      *widget,
1054                             GtkRequisition *requisition,
1055                             EmpathyChat    *chat)
1056 {
1057         EmpathyChatPriv *priv = GET_PRIV (chat);
1058         GtkWidget       *sw;
1059
1060         sw = gtk_widget_get_parent (widget);
1061         if (requisition->height >= MAX_INPUT_HEIGHT && !priv->has_input_vscroll) {
1062                 g_idle_add (chat_input_set_size_request_idle, sw);
1063                 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
1064                                                 GTK_POLICY_NEVER,
1065                                                 GTK_POLICY_ALWAYS);
1066                 priv->has_input_vscroll = TRUE;
1067         }
1068
1069         if (requisition->height < MAX_INPUT_HEIGHT && priv->has_input_vscroll) {
1070                 gtk_widget_set_size_request (sw, -1, -1);
1071                 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
1072                                                 GTK_POLICY_NEVER,
1073                                                 GTK_POLICY_NEVER);
1074                 priv->has_input_vscroll = FALSE;
1075         }
1076 }
1077
1078 static void
1079 chat_input_realize_cb (GtkWidget   *widget,
1080                        EmpathyChat *chat)
1081 {
1082         DEBUG ("Setting focus to the input text view");
1083         gtk_widget_grab_focus (widget);
1084 }
1085
1086 static void
1087 chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
1088                                 EmpathySmiley        *smiley,
1089                                 gpointer              user_data)
1090 {
1091         EmpathyChat   *chat = EMPATHY_CHAT (user_data);
1092         GtkTextBuffer *buffer;
1093         GtkTextIter    iter;
1094
1095         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1096
1097         gtk_text_buffer_get_end_iter (buffer, &iter);
1098         gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
1099
1100         gtk_text_buffer_get_end_iter (buffer, &iter);
1101         gtk_text_buffer_insert (buffer, &iter, " ", -1);
1102 }
1103
1104 typedef struct {
1105         EmpathyChat  *chat;
1106         gchar       *word;
1107
1108         GtkTextIter  start;
1109         GtkTextIter  end;
1110 } EmpathyChatSpell;
1111
1112 static EmpathyChatSpell *
1113 chat_spell_new (EmpathyChat  *chat,
1114                 const gchar *word,
1115                 GtkTextIter  start,
1116                 GtkTextIter  end)
1117 {
1118         EmpathyChatSpell *chat_spell;
1119
1120         chat_spell = g_slice_new0 (EmpathyChatSpell);
1121
1122         chat_spell->chat = g_object_ref (chat);
1123         chat_spell->word = g_strdup (word);
1124         chat_spell->start = start;
1125         chat_spell->end = end;
1126
1127         return chat_spell;
1128 }
1129
1130 static void
1131 chat_spell_free (EmpathyChatSpell *chat_spell)
1132 {
1133         g_object_unref (chat_spell->chat);
1134         g_free (chat_spell->word);
1135         g_slice_free (EmpathyChatSpell, chat_spell);
1136 }
1137
1138 static void
1139 chat_spelling_menu_activate_cb (GtkMenuItem     *menu_item,
1140                                                 EmpathyChatSpell *chat_spell)
1141 {
1142     empathy_chat_correct_word (chat_spell->chat,
1143                                &(chat_spell->start),
1144                                &(chat_spell->end),
1145                                gtk_menu_item_get_label (menu_item));
1146 }
1147
1148 static GtkWidget *
1149 chat_spelling_build_menu (EmpathyChatSpell *chat_spell)
1150 {
1151     GtkWidget *menu, *menu_item;
1152     GList     *suggestions, *l;
1153
1154     menu = gtk_menu_new ();
1155     suggestions = empathy_spell_get_suggestions (chat_spell->word);
1156     if (suggestions == NULL) {
1157         menu_item = gtk_menu_item_new_with_label (_("(No Suggestions)"));
1158         gtk_widget_set_sensitive (menu_item, FALSE);
1159         gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
1160     } else {
1161         for (l = suggestions; l; l = l->next) {
1162             menu_item = gtk_menu_item_new_with_label (l->data);
1163             g_signal_connect (G_OBJECT (menu_item),
1164                           "activate",
1165                           G_CALLBACK (chat_spelling_menu_activate_cb),
1166                           chat_spell);
1167             gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
1168         }
1169     }
1170     empathy_spell_free_suggestions (suggestions);
1171
1172     gtk_widget_show_all (menu);
1173
1174     return menu;
1175 }
1176
1177 static void
1178 chat_text_send_cb (GtkMenuItem *menuitem,
1179                    EmpathyChat *chat)
1180 {
1181         chat_input_text_view_send (chat);
1182 }
1183
1184 static void
1185 chat_input_populate_popup_cb (GtkTextView *view,
1186                               GtkMenu     *menu,
1187                               EmpathyChat *chat)
1188 {
1189         EmpathyChatPriv      *priv;
1190         GtkTextBuffer        *buffer;
1191         GtkTextTagTable      *table;
1192         GtkTextTag           *tag;
1193         gint                  x, y;
1194         GtkTextIter           iter, start, end;
1195         GtkWidget            *item;
1196         gchar                *str = NULL;
1197         EmpathyChatSpell     *chat_spell;
1198         GtkWidget            *spell_menu;
1199         EmpathySmileyManager *smiley_manager;
1200         GtkWidget            *smiley_menu;
1201         GtkWidget            *image;
1202
1203         priv = GET_PRIV (chat);
1204         buffer = gtk_text_view_get_buffer (view);
1205
1206         /* Add the emoticon menu. */
1207         item = gtk_separator_menu_item_new ();
1208         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1209         gtk_widget_show (item);
1210
1211         item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
1212         image = gtk_image_new_from_icon_name ("face-smile",
1213                                               GTK_ICON_SIZE_MENU);
1214         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1215         gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1216         gtk_widget_show (item);
1217
1218         smiley_manager = empathy_smiley_manager_dup_singleton ();
1219         smiley_menu = empathy_smiley_menu_new (smiley_manager,
1220                                                chat_insert_smiley_activate_cb,
1221                                                chat);
1222         gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
1223         g_object_unref (smiley_manager);
1224
1225         /* Add the Send menu item. */
1226         gtk_text_buffer_get_bounds (buffer, &start, &end);
1227         str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1228         if (!EMP_STR_EMPTY (str)) {
1229                 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
1230                 g_signal_connect (G_OBJECT (item), "activate",
1231                                   G_CALLBACK (chat_text_send_cb), chat);
1232                 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1233                 gtk_widget_show (item);
1234         }
1235         str = NULL;
1236
1237         /* Add the spell check menu item. */
1238         table = gtk_text_buffer_get_tag_table (buffer);
1239         tag = gtk_text_tag_table_lookup (table, "misspelled");
1240         gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
1241         gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
1242                                                GTK_TEXT_WINDOW_WIDGET,
1243                                                x, y,
1244                                                &x, &y);
1245         gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
1246         start = end = iter;
1247         if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
1248             gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
1249
1250                 str = gtk_text_buffer_get_text (buffer,
1251                                                 &start, &end, FALSE);
1252         }
1253         if (!EMP_STR_EMPTY (str)) {
1254                 chat_spell = chat_spell_new (chat, str, start, end);
1255                 g_object_set_data_full (G_OBJECT (menu),
1256                                         "chat_spell", chat_spell,
1257                                         (GDestroyNotify) chat_spell_free);
1258
1259                 item = gtk_separator_menu_item_new ();
1260                 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1261                 gtk_widget_show (item);
1262
1263                 item = gtk_image_menu_item_new_with_mnemonic (_("_Spelling Suggestions"));
1264                 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
1265                                                       GTK_ICON_SIZE_MENU);
1266                 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1267
1268                 spell_menu = chat_spelling_build_menu (chat_spell);
1269                 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), spell_menu);
1270
1271                 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1272                 gtk_widget_show (item);
1273         }
1274 }
1275
1276 static gboolean
1277 chat_log_filter (EmpathyMessage *message,
1278                  gpointer user_data)
1279 {
1280         EmpathyChat *chat = (EmpathyChat *) user_data;
1281         EmpathyChatPriv *priv = GET_PRIV (chat);
1282         const GList *pending;
1283
1284         pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
1285
1286         for (; pending; pending = g_list_next (pending)) {
1287                 if (empathy_message_equal (message, pending->data)) {
1288                         return FALSE;
1289                 }
1290         }
1291
1292         return TRUE;
1293 }
1294
1295 static void
1296 chat_add_logs (EmpathyChat *chat)
1297 {
1298         EmpathyChatPriv *priv = GET_PRIV (chat);
1299         gboolean         is_chatroom;
1300         GList           *messages, *l;
1301
1302         if (!priv->id) {
1303                 return;
1304         }
1305
1306         /* Turn off scrolling temporarily */
1307         empathy_chat_view_scroll (chat->view, FALSE);
1308
1309         /* Add messages from last conversation */
1310         is_chatroom = priv->handle_type == TP_HANDLE_TYPE_ROOM;
1311
1312         messages = empathy_log_manager_get_filtered_messages (priv->log_manager,
1313                                                               priv->account,
1314                                                               priv->id,
1315                                                               is_chatroom,
1316                                                               5,
1317                                                               chat_log_filter,
1318                                                               chat);
1319
1320         for (l = messages; l; l = g_list_next (l)) {
1321                 empathy_chat_view_append_message (chat->view, l->data);
1322                 g_object_unref (l->data);
1323         }
1324
1325         g_list_free (messages);
1326
1327         /* Turn back on scrolling */
1328         empathy_chat_view_scroll (chat->view, TRUE);
1329 }
1330
1331 static gint
1332 chat_contacts_completion_func (const gchar *s1,
1333                                const gchar *s2,
1334                                gsize        n)
1335 {
1336         gchar *tmp, *nick1, *nick2;
1337         gint   ret;
1338
1339         if (s1 == s2) {
1340                 return 0;
1341         }
1342         if (!s1 || !s2) {
1343                 return s1 ? -1 : +1;
1344         }
1345
1346         tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
1347         nick1 = g_utf8_casefold (tmp, -1);
1348         g_free (tmp);
1349
1350         tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
1351         nick2 = g_utf8_casefold (tmp, -1);
1352         g_free (tmp);
1353
1354         ret = strncmp (nick1, nick2, n);
1355
1356         g_free (nick1);
1357         g_free (nick2);
1358
1359         return ret;
1360 }
1361
1362 static gchar *
1363 build_part_message (guint           reason,
1364                     const gchar    *name,
1365                     EmpathyContact *actor,
1366                     const gchar    *message)
1367 {
1368         GString *s = g_string_new ("");
1369         const gchar *actor_name = NULL;
1370
1371         if (actor != NULL) {
1372                 actor_name = empathy_contact_get_name (actor);
1373         }
1374
1375         /* Having an actor only really makes sense for a few actions... */
1376         switch (reason) {
1377         case TP_CHANNEL_GROUP_CHANGE_REASON_OFFLINE:
1378                 g_string_append_printf (s, _("%s has disconnected"), name);
1379                 break;
1380         case TP_CHANNEL_GROUP_CHANGE_REASON_KICKED:
1381                 if (actor_name != NULL) {
1382                         /* translators: reverse the order of these arguments
1383                          * if the kicked should come before the kicker in your locale.
1384                          */
1385                         g_string_append_printf (s, _("%1$s was kicked by %2$s"),
1386                                 name, actor_name);
1387                 } else {
1388                         g_string_append_printf (s, _("%s was kicked"), name);
1389                 }
1390                 break;
1391         case TP_CHANNEL_GROUP_CHANGE_REASON_BANNED:
1392                 if (actor_name != NULL) {
1393                         /* translators: reverse the order of these arguments
1394                          * if the banned should come before the banner in your locale.
1395                          */
1396                         g_string_append_printf (s, _("%1$s was banned by %2$s"),
1397                                 name, actor_name);
1398                 } else {
1399                         g_string_append_printf (s, _("%s was banned"), name);
1400                 }
1401                 break;
1402         default:
1403                 g_string_append_printf (s, _("%s has left the room"), name);
1404         }
1405
1406         if (!EMP_STR_EMPTY (message)) {
1407                 /* Note to translators: this string is appended to
1408                  * notifications like "foo has left the room", with the message
1409                  * given by the user living the room. If this poses a problem,
1410                  * please let us know. :-)
1411                  */
1412                 g_string_append_printf (s, _(" (%s)"), message);
1413         }
1414
1415         return g_string_free (s, FALSE);
1416 }
1417
1418 static void
1419 chat_members_changed_cb (EmpathyTpChat  *tp_chat,
1420                          EmpathyContact *contact,
1421                          EmpathyContact *actor,
1422                          guint           reason,
1423                          gchar          *message,
1424                          gboolean        is_member,
1425                          EmpathyChat    *chat)
1426 {
1427         EmpathyChatPriv *priv = GET_PRIV (chat);
1428         const gchar *name = empathy_contact_get_name (contact);
1429         gchar *str;
1430
1431         if (priv->block_events_timeout_id != 0)
1432                 return;
1433
1434         if (is_member) {
1435                 str = g_strdup_printf (_("%s has joined the room"),
1436                                        name);
1437         } else {
1438                 str = build_part_message (reason, name, actor, message);
1439         }
1440
1441         empathy_chat_view_append_event (chat->view, str);
1442         g_free (str);
1443 }
1444
1445 static gboolean
1446 chat_reset_size_request (gpointer widget)
1447 {
1448         gtk_widget_set_size_request (widget, -1, -1);
1449
1450         return FALSE;
1451 }
1452
1453 static void
1454 chat_update_contacts_visibility (EmpathyChat *chat)
1455 {
1456         EmpathyChatPriv *priv = GET_PRIV (chat);
1457         gboolean show;
1458
1459         show = priv->remote_contact == NULL && priv->show_contacts;
1460
1461         if (!priv->scrolled_window_contacts) {
1462                 return;
1463         }
1464
1465         if (show && priv->contact_list_view == NULL) {
1466                 EmpathyContactListStore *store;
1467                 gint                     min_width;
1468
1469                 /* We are adding the contact list to the chat, we don't want the
1470                  * chat view to become too small. If the chat view is already
1471                  * smaller than 250 make sure that size won't change. If the
1472                  * chat view is bigger the contact list will take some space on
1473                  * it but we make sure the chat view don't become smaller than
1474                  * 250. Relax the size request once the resize is done */
1475                 min_width = MIN (priv->vbox_left->allocation.width, 250);
1476                 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
1477                 g_idle_add (chat_reset_size_request, priv->vbox_left);
1478
1479                 if (priv->contacts_width > 0) {
1480                         gtk_paned_set_position (GTK_PANED (priv->hpaned),
1481                                                 priv->contacts_width);
1482                 }
1483
1484                 store = empathy_contact_list_store_new (EMPATHY_CONTACT_LIST (priv->tp_chat));
1485                 priv->contact_list_view = GTK_WIDGET (empathy_contact_list_view_new (store,
1486                         EMPATHY_CONTACT_LIST_FEATURE_CONTACT_TOOLTIP,
1487                         EMPATHY_CONTACT_FEATURE_CHAT |
1488                         EMPATHY_CONTACT_FEATURE_CALL |
1489                         EMPATHY_CONTACT_FEATURE_LOG |
1490                         EMPATHY_CONTACT_FEATURE_INFO));
1491                 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
1492                                    priv->contact_list_view);
1493                 gtk_widget_show (priv->contact_list_view);
1494                 gtk_widget_show (priv->scrolled_window_contacts);
1495                 g_object_unref (store);
1496         } else if (!show) {
1497                 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
1498                 gtk_widget_hide (priv->scrolled_window_contacts);
1499                 if (priv->contact_list_view != NULL) {
1500                         gtk_widget_destroy (priv->contact_list_view);
1501                         priv->contact_list_view = NULL;
1502                 }
1503         }
1504 }
1505
1506 void
1507 empathy_chat_set_show_contacts (EmpathyChat *chat,
1508                                 gboolean     show)
1509 {
1510         EmpathyChatPriv *priv = GET_PRIV (chat);
1511
1512         priv->show_contacts = show;
1513
1514         chat_update_contacts_visibility (chat);
1515
1516         g_object_notify (G_OBJECT (chat), "show-contacts");
1517 }
1518
1519 static void
1520 chat_remote_contact_changed_cb (EmpathyChat *chat)
1521 {
1522         EmpathyChatPriv *priv = GET_PRIV (chat);
1523
1524         if (priv->remote_contact != NULL) {
1525                 g_object_unref (priv->remote_contact);
1526                 priv->remote_contact = NULL;
1527         }
1528
1529         g_free (priv->id);
1530
1531         priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
1532         priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
1533         if (priv->remote_contact != NULL) {
1534                 g_object_ref (priv->remote_contact);
1535                 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
1536         }
1537         else if (priv->tp_chat != NULL) {
1538                 TpChannel *channel;
1539
1540                 channel = empathy_tp_chat_get_channel (priv->tp_chat);
1541                 g_object_get (channel, "handle-type", &priv->handle_type, NULL);
1542         }
1543
1544         chat_update_contacts_visibility (chat);
1545
1546         g_object_notify (G_OBJECT (chat), "remote-contact");
1547         g_object_notify (G_OBJECT (chat), "id");
1548 }
1549
1550 static void
1551 chat_destroy_cb (EmpathyTpChat *tp_chat,
1552                  EmpathyChat   *chat)
1553 {
1554         EmpathyChatPriv *priv;
1555
1556         priv = GET_PRIV (chat);
1557
1558         if (!priv->tp_chat) {
1559                 return;
1560         }
1561
1562         chat_composing_remove_timeout (chat);
1563         g_object_unref (priv->tp_chat);
1564         priv->tp_chat = NULL;
1565         g_object_notify (G_OBJECT (chat), "tp-chat");
1566
1567         empathy_chat_view_append_event (chat->view, _("Disconnected"));
1568         gtk_widget_set_sensitive (chat->input_text_view, FALSE);
1569         empathy_chat_set_show_contacts (chat, FALSE);
1570 }
1571
1572 static void
1573 show_pending_messages (EmpathyChat *chat) {
1574         EmpathyChatPriv *priv = GET_PRIV (chat);
1575         const GList *messages, *l;
1576
1577         if (chat->view == NULL || priv->tp_chat == NULL)
1578                 return;
1579
1580         messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
1581
1582         for (l = messages; l != NULL ; l = g_list_next (l)) {
1583                 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
1584                 chat_message_received (chat, message);
1585         }
1586         empathy_tp_chat_acknowledge_messages (priv->tp_chat, messages);
1587 }
1588
1589 static void
1590 chat_create_ui (EmpathyChat *chat)
1591 {
1592         EmpathyChatPriv *priv = GET_PRIV (chat);
1593         GtkBuilder      *gui;
1594         GList           *list = NULL;
1595         gchar           *filename;
1596         GtkTextBuffer   *buffer;
1597
1598         filename = empathy_file_lookup ("empathy-chat.ui",
1599                                         "libempathy-gtk");
1600         gui = empathy_builder_get_file (filename,
1601                                         "chat_widget", &priv->widget,
1602                                         "hpaned", &priv->hpaned,
1603                                         "vbox_left", &priv->vbox_left,
1604                                         "scrolled_window_chat", &priv->scrolled_window_chat,
1605                                         "scrolled_window_input", &priv->scrolled_window_input,
1606                                         "hbox_topic", &priv->hbox_topic,
1607                                         "label_topic", &priv->label_topic,
1608                                         "scrolled_window_contacts", &priv->scrolled_window_contacts,
1609                                         NULL);
1610         g_free (filename);
1611
1612         /* Add message view. */
1613         chat->view = empathy_theme_manager_create_view (empathy_theme_manager_get ());
1614         g_signal_connect (chat->view, "focus_in_event",
1615                           G_CALLBACK (chat_text_view_focus_in_event_cb),
1616                           chat);
1617         gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
1618                            GTK_WIDGET (chat->view));
1619         gtk_widget_show (GTK_WIDGET (chat->view));
1620
1621         /* Add input GtkTextView */
1622         chat->input_text_view = g_object_new (GTK_TYPE_TEXT_VIEW,
1623                                               "pixels-above-lines", 2,
1624                                               "pixels-below-lines", 2,
1625                                               "pixels-inside-wrap", 1,
1626                                               "right-margin", 2,
1627                                               "left-margin", 2,
1628                                               "wrap-mode", GTK_WRAP_WORD_CHAR,
1629                                               NULL);
1630         g_signal_connect (chat->input_text_view, "key-press-event",
1631                           G_CALLBACK (chat_input_key_press_event_cb),
1632                           chat);
1633         g_signal_connect (chat->input_text_view, "size-request",
1634                           G_CALLBACK (chat_input_size_request_cb),
1635                           chat);
1636         g_signal_connect (chat->input_text_view, "realize",
1637                           G_CALLBACK (chat_input_realize_cb),
1638                           chat);
1639         g_signal_connect (chat->input_text_view, "populate-popup",
1640                           G_CALLBACK (chat_input_populate_popup_cb),
1641                           chat);
1642         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1643         g_signal_connect (buffer, "changed",
1644                           G_CALLBACK (chat_input_text_buffer_changed_cb),
1645                           chat);
1646         gtk_text_buffer_create_tag (buffer, "misspelled",
1647                                     "underline", PANGO_UNDERLINE_ERROR,
1648                                     NULL);
1649         gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
1650                            chat->input_text_view);
1651         gtk_widget_show (chat->input_text_view);
1652
1653         /* Create contact list */
1654         chat_update_contacts_visibility (chat);
1655
1656         /* Initialy hide the topic, will be shown if not empty */
1657         gtk_widget_hide (priv->hbox_topic);
1658
1659         /* Set widget focus order */
1660         list = g_list_append (NULL, priv->scrolled_window_input);
1661         gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
1662         g_list_free (list);
1663
1664         list = g_list_append (NULL, priv->vbox_left);
1665         list = g_list_append (list, priv->scrolled_window_contacts);
1666         gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
1667         g_list_free (list);
1668
1669         list = g_list_append (NULL, priv->hpaned);
1670         list = g_list_append (list, priv->hbox_topic);
1671         gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
1672         g_list_free (list);
1673
1674         /* Add the main widget in the chat widget */
1675         gtk_container_add (GTK_CONTAINER (chat), priv->widget);
1676         g_object_unref (gui);
1677 }
1678
1679 static void
1680 chat_size_request (GtkWidget      *widget,
1681                    GtkRequisition *requisition)
1682 {
1683   GtkBin *bin = GTK_BIN (widget);
1684   GtkWidget *child;
1685
1686   requisition->width = gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2;
1687   requisition->height = gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2;
1688
1689   child = gtk_bin_get_child (bin);
1690
1691   if (child && GTK_WIDGET_VISIBLE (child))
1692     {
1693       GtkRequisition child_requisition;
1694
1695       gtk_widget_size_request (child, &child_requisition);
1696
1697       requisition->width += child_requisition.width;
1698       requisition->height += child_requisition.height;
1699     }
1700 }
1701
1702 static void
1703 chat_size_allocate (GtkWidget     *widget,
1704                     GtkAllocation *allocation)
1705 {
1706   GtkBin *bin = GTK_BIN (widget);
1707   GtkAllocation child_allocation;
1708   GtkWidget *child;
1709
1710   widget->allocation = *allocation;
1711
1712   child = gtk_bin_get_child (bin);
1713
1714   if (child && GTK_WIDGET_VISIBLE (child))
1715     {
1716       child_allocation.x = allocation->x + gtk_container_get_border_width (GTK_CONTAINER (widget));
1717       child_allocation.y = allocation->y + gtk_container_get_border_width (GTK_CONTAINER (widget));
1718       child_allocation.width = MAX (allocation->width - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
1719       child_allocation.height = MAX (allocation->height - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
1720
1721       gtk_widget_size_allocate (child, &child_allocation);
1722     }
1723 }
1724
1725 static void
1726 chat_finalize (GObject *object)
1727 {
1728         EmpathyChat     *chat;
1729         EmpathyChatPriv *priv;
1730
1731         chat = EMPATHY_CHAT (object);
1732         priv = GET_PRIV (chat);
1733
1734         DEBUG ("Finalized: %p", object);
1735
1736         g_list_foreach (priv->input_history, (GFunc) chat_input_history_entry_free, NULL);
1737         g_list_free (priv->input_history);
1738
1739         g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
1740         g_list_free (priv->compositors);
1741
1742         chat_composing_remove_timeout (chat);
1743
1744         g_signal_handlers_disconnect_by_func (priv->account_manager,
1745                                               chat_new_connection_cb, object);
1746
1747         g_object_unref (priv->account_manager);
1748         g_object_unref (priv->log_manager);
1749
1750         if (priv->tp_chat) {
1751                 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1752                         chat_destroy_cb, chat);
1753                 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1754                         chat_message_received_cb, chat);
1755                 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1756                         chat_send_error_cb, chat);
1757                 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1758                         chat_state_changed_cb, chat);
1759                 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1760                         chat_property_changed_cb, chat);
1761                 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1762                         chat_members_changed_cb, chat);
1763                 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1764                         chat_remote_contact_changed_cb, chat);
1765                 empathy_tp_chat_close (priv->tp_chat);
1766                 g_object_unref (priv->tp_chat);
1767         }
1768         if (priv->account) {
1769                 g_object_unref (priv->account);
1770         }
1771         if (priv->remote_contact) {
1772                 g_object_unref (priv->remote_contact);
1773         }
1774
1775         if (priv->block_events_timeout_id) {
1776                 g_source_remove (priv->block_events_timeout_id);
1777         }
1778
1779         g_free (priv->id);
1780         g_free (priv->name);
1781         g_free (priv->subject);
1782         g_completion_free (priv->completion);
1783
1784         G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
1785 }
1786
1787 static void
1788 chat_constructed (GObject *object)
1789 {
1790         EmpathyChat *chat = EMPATHY_CHAT (object);
1791
1792         chat_create_ui (chat);
1793         chat_add_logs (chat);
1794         show_pending_messages (chat);
1795 }
1796
1797 static void
1798 empathy_chat_class_init (EmpathyChatClass *klass)
1799 {
1800         GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1801         GObjectClass   *object_class = G_OBJECT_CLASS (klass);
1802
1803         object_class->finalize = chat_finalize;
1804         object_class->get_property = chat_get_property;
1805         object_class->set_property = chat_set_property;
1806         object_class->constructed = chat_constructed;
1807
1808         widget_class->size_request = chat_size_request;
1809         widget_class->size_allocate = chat_size_allocate;
1810
1811         g_object_class_install_property (object_class,
1812                                          PROP_TP_CHAT,
1813                                          g_param_spec_object ("tp-chat",
1814                                                               "Empathy tp chat",
1815                                                               "The tp chat object",
1816                                                               EMPATHY_TYPE_TP_CHAT,
1817                                                               G_PARAM_CONSTRUCT |
1818                                                               G_PARAM_READWRITE |
1819                                                               G_PARAM_STATIC_STRINGS));
1820         g_object_class_install_property (object_class,
1821                                          PROP_ACCOUNT,
1822                                          g_param_spec_object ("account",
1823                                                               "Account of the chat",
1824                                                               "The account of the chat",
1825                                                               EMPATHY_TYPE_ACCOUNT,
1826                                                               G_PARAM_READABLE |
1827                                                               G_PARAM_STATIC_STRINGS));
1828         g_object_class_install_property (object_class,
1829                                          PROP_ID,
1830                                          g_param_spec_string ("id",
1831                                                               "Chat's id",
1832                                                               "The id of the chat",
1833                                                               NULL,
1834                                                               G_PARAM_READABLE |
1835                                                               G_PARAM_STATIC_STRINGS));
1836         g_object_class_install_property (object_class,
1837                                          PROP_NAME,
1838                                          g_param_spec_string ("name",
1839                                                               "Chat's name",
1840                                                               "The name of the chat",
1841                                                               NULL,
1842                                                               G_PARAM_READABLE |
1843                                                               G_PARAM_STATIC_STRINGS));
1844         g_object_class_install_property (object_class,
1845                                          PROP_SUBJECT,
1846                                          g_param_spec_string ("subject",
1847                                                               "Chat's subject",
1848                                                               "The subject or topic of the chat",
1849                                                               NULL,
1850                                                               G_PARAM_READABLE |
1851                                                               G_PARAM_STATIC_STRINGS));
1852         g_object_class_install_property (object_class,
1853                                          PROP_REMOTE_CONTACT,
1854                                          g_param_spec_object ("remote-contact",
1855                                                               "The remote contact",
1856                                                               "The remote contact is any",
1857                                                               EMPATHY_TYPE_CONTACT,
1858                                                               G_PARAM_READABLE |
1859                                                               G_PARAM_STATIC_STRINGS));
1860         g_object_class_install_property (object_class,
1861                                          PROP_SHOW_CONTACTS,
1862                                          g_param_spec_boolean ("show-contacts",
1863                                                                "Contacts' visibility",
1864                                                                "The visibility of the contacts' list",
1865                                                                TRUE,
1866                                                                G_PARAM_READWRITE |
1867                                                                G_PARAM_STATIC_STRINGS));
1868
1869         signals[COMPOSING] =
1870                 g_signal_new ("composing",
1871                               G_OBJECT_CLASS_TYPE (object_class),
1872                               G_SIGNAL_RUN_LAST,
1873                               0,
1874                               NULL, NULL,
1875                               g_cclosure_marshal_VOID__BOOLEAN,
1876                               G_TYPE_NONE,
1877                               1, G_TYPE_BOOLEAN);
1878
1879         signals[NEW_MESSAGE] =
1880                 g_signal_new ("new-message",
1881                               G_OBJECT_CLASS_TYPE (object_class),
1882                               G_SIGNAL_RUN_LAST,
1883                               0,
1884                               NULL, NULL,
1885                               g_cclosure_marshal_VOID__OBJECT,
1886                               G_TYPE_NONE,
1887                               1, EMPATHY_TYPE_MESSAGE);
1888
1889         g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
1890 }
1891
1892 static gboolean
1893 chat_block_events_timeout_cb (gpointer data)
1894 {
1895         EmpathyChatPriv *priv = GET_PRIV (data);
1896
1897         priv->block_events_timeout_id = 0;
1898
1899         return FALSE;
1900 }
1901
1902 static void
1903 empathy_chat_init (EmpathyChat *chat)
1904 {
1905         EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
1906                 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
1907
1908         chat->priv = priv;
1909         priv->log_manager = empathy_log_manager_dup_singleton ();
1910         priv->contacts_width = -1;
1911         priv->input_history = NULL;
1912         priv->input_history_current = NULL;
1913         priv->account_manager = empathy_account_manager_dup_singleton ();
1914
1915         g_signal_connect (priv->account_manager,
1916                           "new-connection",
1917                           G_CALLBACK (chat_new_connection_cb),
1918                           chat);
1919
1920         empathy_conf_get_bool (empathy_conf_get (),
1921                                EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS,
1922                                &priv->show_contacts);
1923
1924         /* Block events for some time to avoid having "has come online" or
1925          * "joined" messages. */
1926         priv->block_events_timeout_id =
1927                 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
1928
1929         /* Add nick name completion */
1930         priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_name);
1931         g_completion_set_compare (priv->completion, chat_contacts_completion_func);
1932 }
1933
1934 EmpathyChat *
1935 empathy_chat_new (EmpathyTpChat *tp_chat)
1936 {
1937         return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
1938 }
1939
1940 EmpathyTpChat *
1941 empathy_chat_get_tp_chat (EmpathyChat *chat)
1942 {
1943         EmpathyChatPriv *priv = GET_PRIV (chat);
1944
1945         g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1946
1947         return priv->tp_chat;
1948 }
1949
1950 void
1951 empathy_chat_set_tp_chat (EmpathyChat   *chat,
1952                           EmpathyTpChat *tp_chat)
1953 {
1954         EmpathyChatPriv *priv = GET_PRIV (chat);
1955         TpConnection    *connection;
1956
1957         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1958         g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
1959         g_return_if_fail (empathy_tp_chat_is_ready (tp_chat));
1960
1961         if (priv->tp_chat) {
1962                 return;
1963         }
1964
1965         if (priv->account) {
1966                 g_object_unref (priv->account);
1967         }
1968
1969         priv->tp_chat = g_object_ref (tp_chat);
1970         connection = empathy_tp_chat_get_connection (priv->tp_chat);
1971         priv->account = empathy_account_manager_get_account_for_connection (
1972                                                              priv->account_manager,
1973                                                              connection);
1974         g_object_ref (priv->account);
1975
1976         g_signal_connect (tp_chat, "destroy",
1977                           G_CALLBACK (chat_destroy_cb),
1978                           chat);
1979         g_signal_connect (tp_chat, "message-received",
1980                           G_CALLBACK (chat_message_received_cb),
1981                           chat);
1982         g_signal_connect (tp_chat, "send-error",
1983                           G_CALLBACK (chat_send_error_cb),
1984                           chat);
1985         g_signal_connect (tp_chat, "chat-state-changed",
1986                           G_CALLBACK (chat_state_changed_cb),
1987                           chat);
1988         g_signal_connect (tp_chat, "property-changed",
1989                           G_CALLBACK (chat_property_changed_cb),
1990                           chat);
1991         g_signal_connect (tp_chat, "members-changed",
1992                           G_CALLBACK (chat_members_changed_cb),
1993                           chat);
1994         g_signal_connect_swapped (tp_chat, "notify::remote-contact",
1995                                   G_CALLBACK (chat_remote_contact_changed_cb),
1996                                   chat);
1997
1998         chat_remote_contact_changed_cb (chat);
1999
2000         if (chat->input_text_view) {
2001                 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
2002                 if (priv->block_events_timeout_id == 0) {
2003                         empathy_chat_view_append_event (chat->view, _("Connected"));
2004                 }
2005         }
2006
2007         g_object_notify (G_OBJECT (chat), "tp-chat");
2008         g_object_notify (G_OBJECT (chat), "id");
2009         g_object_notify (G_OBJECT (chat), "account");
2010
2011         /* This is a noop when tp-chat is set at object construction time and causes
2012          * the pending messages to be show when it's set on the object after it has
2013          * been created */
2014         show_pending_messages (chat);
2015 }
2016
2017 EmpathyAccount *
2018 empathy_chat_get_account (EmpathyChat *chat)
2019 {
2020         EmpathyChatPriv *priv = GET_PRIV (chat);
2021
2022         g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2023
2024         return priv->account;
2025 }
2026
2027 const gchar *
2028 empathy_chat_get_id (EmpathyChat *chat)
2029 {
2030         EmpathyChatPriv *priv = GET_PRIV (chat);
2031
2032         g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2033
2034         return priv->id;
2035 }
2036
2037 const gchar *
2038 empathy_chat_get_name (EmpathyChat *chat)
2039 {
2040         EmpathyChatPriv *priv = GET_PRIV (chat);
2041         const gchar *ret;
2042
2043         g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2044
2045         ret = priv->name;
2046         if (!ret && priv->remote_contact) {
2047                 ret = empathy_contact_get_name (priv->remote_contact);
2048         }
2049
2050         if (!ret)
2051                 ret = priv->id;
2052
2053         return ret ? ret : _("Conversation");
2054 }
2055
2056 const gchar *
2057 empathy_chat_get_subject (EmpathyChat *chat)
2058 {
2059         EmpathyChatPriv *priv = GET_PRIV (chat);
2060
2061         g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2062
2063         return priv->subject;
2064 }
2065
2066 EmpathyContact *
2067 empathy_chat_get_remote_contact (EmpathyChat *chat)
2068 {
2069         EmpathyChatPriv *priv = GET_PRIV (chat);
2070
2071         g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2072
2073         return priv->remote_contact;
2074 }
2075
2076 GtkWidget *
2077 empathy_chat_get_contact_menu (EmpathyChat *chat)
2078 {
2079         EmpathyChatPriv *priv = GET_PRIV (chat);
2080         GtkWidget       *menu = NULL;
2081
2082         g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2083
2084         if (priv->remote_contact) {
2085                 menu = empathy_contact_menu_new (priv->remote_contact,
2086                                                  EMPATHY_CONTACT_FEATURE_CALL |
2087                                                  EMPATHY_CONTACT_FEATURE_LOG |
2088                                                  EMPATHY_CONTACT_FEATURE_INFO);
2089         }
2090         else if (priv->contact_list_view) {
2091                 EmpathyContactListView *view;
2092
2093                 view = EMPATHY_CONTACT_LIST_VIEW (priv->contact_list_view);
2094                 menu = empathy_contact_list_view_get_contact_menu (view);
2095         }
2096
2097         return menu;
2098 }
2099
2100 void
2101 empathy_chat_clear (EmpathyChat *chat)
2102 {
2103         g_return_if_fail (EMPATHY_IS_CHAT (chat));
2104
2105         empathy_chat_view_clear (chat->view);
2106 }
2107
2108 void
2109 empathy_chat_scroll_down (EmpathyChat *chat)
2110 {
2111         g_return_if_fail (EMPATHY_IS_CHAT (chat));
2112
2113         empathy_chat_view_scroll_down (chat->view);
2114 }
2115
2116 void
2117 empathy_chat_cut (EmpathyChat *chat)
2118 {
2119         GtkTextBuffer *buffer;
2120
2121         g_return_if_fail (EMPATHY_IS_CHAT (chat));
2122
2123         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2124         if (gtk_text_buffer_get_has_selection (buffer)) {
2125                 GtkClipboard *clipboard;
2126
2127                 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
2128
2129                 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
2130         }
2131 }
2132
2133 void
2134 empathy_chat_copy (EmpathyChat *chat)
2135 {
2136         GtkTextBuffer *buffer;
2137
2138         g_return_if_fail (EMPATHY_IS_CHAT (chat));
2139
2140         if (empathy_chat_view_get_has_selection (chat->view)) {
2141                 empathy_chat_view_copy_clipboard (chat->view);
2142                 return;
2143         }
2144
2145         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2146         if (gtk_text_buffer_get_has_selection (buffer)) {
2147                 GtkClipboard *clipboard;
2148
2149                 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
2150
2151                 gtk_text_buffer_copy_clipboard (buffer, clipboard);
2152         }
2153 }
2154
2155 void
2156 empathy_chat_paste (EmpathyChat *chat)
2157 {
2158         GtkTextBuffer *buffer;
2159         GtkClipboard  *clipboard;
2160         EmpathyChatPriv *priv;
2161
2162         g_return_if_fail (EMPATHY_IS_CHAT (chat));
2163
2164         priv = GET_PRIV (chat);
2165
2166         if (priv->tp_chat == NULL ||
2167             !GTK_WIDGET_IS_SENSITIVE (chat->input_text_view))
2168                 return;
2169
2170         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2171         clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
2172
2173         gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
2174 }
2175
2176 void
2177 empathy_chat_correct_word (EmpathyChat  *chat,
2178                           GtkTextIter *start,
2179                           GtkTextIter *end,
2180                           const gchar *new_word)
2181 {
2182         GtkTextBuffer *buffer;
2183
2184         g_return_if_fail (chat != NULL);
2185         g_return_if_fail (new_word != NULL);
2186
2187         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2188
2189         gtk_text_buffer_delete (buffer, start, end);
2190         gtk_text_buffer_insert (buffer, start,
2191                                 new_word,
2192                                 -1);
2193 }
2194
2195 gboolean
2196 empathy_chat_is_room (EmpathyChat *chat)
2197 {
2198         EmpathyChatPriv *priv = GET_PRIV (chat);
2199
2200         g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
2201
2202         return (priv->handle_type == TP_HANDLE_TYPE_ROOM);
2203 }
2204