1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2002-2007 Imendio AB
4 * Copyright (C) 2007-2008 Collabora Ltd.
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.
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.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Authors: Mikael Hallendal <micke@imendio.com>
22 * Richard Hult <richard@imendio.com>
23 * Martyn Russell <martyn@imendio.com>
24 * Xavier Claessens <xclaesse@gmail.com>
31 #include <gdk/gdkkeysyms.h>
33 #include <glade/glade.h>
34 #include <glib/gi18n.h>
36 #include <telepathy-glib/util.h>
38 #include <libempathy/empathy-tp-chat.h>
39 #include <libempathy/empathy-tp-chatroom.h>
40 #include <libempathy/empathy-contact.h>
41 #include <libempathy/empathy-utils.h>
42 #include <libempathy/empathy-debug.h>
44 #include "empathy-group-chat.h"
45 #include "empathy-chat.h"
46 #include "empathy-chat-view.h"
47 #include "empathy-contact-list-store.h"
48 #include "empathy-contact-list-view.h"
49 //#include "empathy-chat-invite.h"
50 //#include "empathy-sound.h"
51 #include "empathy-images.h"
52 #include "empathy-ui-utils.h"
53 #include "empathy-conf.h"
55 #define DEBUG_DOMAIN "GroupChat"
57 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_GROUP_CHAT, EmpathyGroupChatPriv))
59 struct _EmpathyGroupChatPriv {
60 EmpathyContactListStore *store;
61 EmpathyContactListView *view;
62 EmpathyTpChatroom *tp_chat;
67 GtkWidget *scrolled_window_chat;
68 GtkWidget *scrolled_window_input;
69 GtkWidget *scrolled_window_contacts;
70 GtkWidget *hbox_topic;
71 GtkWidget *label_topic;
75 GCompletion *completion;
78 gboolean contacts_visible;
81 static void group_chat_finalize (GObject *object);
82 static void group_chat_create_ui (EmpathyGroupChat *chat);
83 static void group_chat_widget_destroy_cb (GtkWidget *widget,
84 EmpathyGroupChat *chat);
85 static void group_chat_members_changed_cb (EmpathyTpChatroom *tp_chat,
86 EmpathyContact *contact,
87 EmpathyContact *actor,
91 EmpathyGroupChat *chat);
92 static const gchar * group_chat_get_name (EmpathyChat *chat);
93 static gchar * group_chat_get_tooltip (EmpathyChat *chat);
94 static const gchar * group_chat_get_status_icon_name (EmpathyChat *chat);
95 static GtkWidget * group_chat_get_widget (EmpathyChat *chat);
96 static gboolean group_chat_is_group_chat (EmpathyChat *chat);
97 static void group_chat_set_tp_chat (EmpathyChat *chat,
98 EmpathyTpChat *tp_chat);
99 static gboolean group_chat_key_press_event (EmpathyChat *chat,
101 static gint group_chat_contacts_completion_func (const gchar *s1,
105 G_DEFINE_TYPE (EmpathyGroupChat, empathy_group_chat, EMPATHY_TYPE_CHAT)
108 empathy_group_chat_class_init (EmpathyGroupChatClass *klass)
110 GObjectClass *object_class;
111 EmpathyChatClass *chat_class;
113 object_class = G_OBJECT_CLASS (klass);
114 chat_class = EMPATHY_CHAT_CLASS (klass);
116 object_class->finalize = group_chat_finalize;
118 chat_class->get_name = group_chat_get_name;
119 chat_class->get_tooltip = group_chat_get_tooltip;
120 chat_class->get_status_icon_name = group_chat_get_status_icon_name;
121 chat_class->get_widget = group_chat_get_widget;
122 chat_class->is_group_chat = group_chat_is_group_chat;
123 chat_class->set_tp_chat = group_chat_set_tp_chat;
124 chat_class->key_press_event = group_chat_key_press_event;
126 g_type_class_add_private (object_class, sizeof (EmpathyGroupChatPriv));
130 empathy_group_chat_init (EmpathyGroupChat *chat)
132 EmpathyGroupChatPriv *priv;
133 EmpathyChatView *chatview;
135 priv = GET_PRIV (chat);
137 priv->contacts_visible = TRUE;
139 chatview = EMPATHY_CHAT_VIEW (EMPATHY_CHAT (chat)->view);
140 empathy_chat_view_set_is_group_chat (chatview, TRUE);
142 group_chat_create_ui (chat);
146 group_chat_finalize (GObject *object)
148 EmpathyGroupChat *chat;
149 EmpathyGroupChatPriv *priv;
151 empathy_debug (DEBUG_DOMAIN, "Finalized:%p", object);
153 chat = EMPATHY_GROUP_CHAT (object);
154 priv = GET_PRIV (chat);
157 g_free (priv->topic);
159 g_object_unref (priv->store);
162 g_object_unref (priv->tp_chat);
164 g_completion_free (priv->completion);
166 G_OBJECT_CLASS (empathy_group_chat_parent_class)->finalize (object);
170 empathy_group_chat_new (EmpathyTpChatroom *tp_chat)
172 EmpathyGroupChat *chat;
174 g_return_val_if_fail (EMPATHY_IS_TP_CHAT (tp_chat), NULL);
176 chat = g_object_new (EMPATHY_TYPE_GROUP_CHAT,
184 empathy_group_chat_get_show_contacts (EmpathyGroupChat *chat)
186 EmpathyGroupChat *group_chat;
187 EmpathyGroupChatPriv *priv;
189 g_return_val_if_fail (EMPATHY_IS_GROUP_CHAT (chat), FALSE);
191 group_chat = EMPATHY_GROUP_CHAT (chat);
192 priv = GET_PRIV (group_chat);
194 return priv->contacts_visible;
198 empathy_group_chat_set_show_contacts (EmpathyGroupChat *chat,
201 EmpathyGroupChat *group_chat;
202 EmpathyGroupChatPriv *priv;
204 g_return_if_fail (EMPATHY_IS_GROUP_CHAT (chat));
206 group_chat = EMPATHY_GROUP_CHAT (chat);
207 priv = GET_PRIV (group_chat);
209 priv->contacts_visible = show;
212 gtk_widget_show (priv->scrolled_window_contacts);
213 gtk_paned_set_position (GTK_PANED (priv->hpaned),
214 priv->contacts_width);
216 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
217 gtk_widget_hide (priv->scrolled_window_contacts);
222 group_chat_topic_response_cb (GtkWidget *dialog,
224 EmpathyGroupChat *chat)
226 if (response == GTK_RESPONSE_OK) {
230 entry = g_object_get_data (G_OBJECT (dialog), "entry");
231 topic = gtk_entry_get_text (GTK_ENTRY (entry));
233 if (!G_STR_EMPTY (topic)) {
234 EmpathyGroupChatPriv *priv;
235 GValue value = {0, };
237 priv = GET_PRIV (chat);
239 g_value_init (&value, G_TYPE_STRING);
240 g_value_set_string (&value, topic);
241 empathy_tp_chat_set_property (EMPATHY_TP_CHAT (priv->tp_chat),
243 g_value_unset (&value);
247 gtk_widget_destroy (dialog);
251 group_chat_topic_entry_activate_cb (GtkWidget *entry,
254 gtk_dialog_response (dialog, GTK_RESPONSE_OK);
258 empathy_group_chat_set_topic (EmpathyGroupChat *chat)
260 EmpathyGroupChatPriv *priv;
267 priv = GET_PRIV (chat);
269 g_return_if_fail (EMPATHY_IS_GROUP_CHAT (chat));
271 parent = empathy_get_toplevel_window (empathy_chat_get_widget (EMPATHY_CHAT (chat)));
272 dialog = gtk_message_dialog_new (GTK_WINDOW (parent),
274 GTK_MESSAGE_QUESTION,
275 GTK_BUTTONS_OK_CANCEL,
276 _("Enter the new topic you want to set for this room:"));
278 topic = gtk_label_get_text (GTK_LABEL (priv->label_topic));
280 hbox = gtk_hbox_new (FALSE, 0);
281 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
282 hbox, FALSE, TRUE, 4);
284 entry = gtk_entry_new ();
285 gtk_entry_set_text (GTK_ENTRY (entry), topic);
286 gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
288 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 4);
290 g_object_set (GTK_MESSAGE_DIALOG (dialog)->label, "use-markup", TRUE, NULL);
291 g_object_set_data (G_OBJECT (dialog), "entry", entry);
293 g_signal_connect (entry, "activate",
294 G_CALLBACK (group_chat_topic_entry_activate_cb),
296 g_signal_connect (dialog, "response",
297 G_CALLBACK (group_chat_topic_response_cb),
300 gtk_widget_show_all (dialog);
304 group_chat_create_ui (EmpathyGroupChat *chat)
306 EmpathyGroupChatPriv *priv;
310 priv = GET_PRIV (chat);
312 glade = empathy_glade_get_file ("empathy-group-chat.glade",
315 "group_chat_widget", &priv->widget,
316 "hpaned", &priv->hpaned,
317 "vbox_left", &priv->vbox_left,
318 "scrolled_window_chat", &priv->scrolled_window_chat,
319 "scrolled_window_input", &priv->scrolled_window_input,
320 "hbox_topic", &priv->hbox_topic,
321 "label_topic", &priv->label_topic,
322 "scrolled_window_contacts", &priv->scrolled_window_contacts,
325 empathy_glade_connect (glade,
327 "group_chat_widget", "destroy", group_chat_widget_destroy_cb,
330 g_object_unref (glade);
332 g_object_set_data (G_OBJECT (priv->widget), "chat", g_object_ref (chat));
334 /* Add room GtkTextView. */
335 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
336 GTK_WIDGET (EMPATHY_CHAT (chat)->view));
337 gtk_widget_show (GTK_WIDGET (EMPATHY_CHAT (chat)->view));
339 /* Add input GtkTextView */
340 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
341 EMPATHY_CHAT (chat)->input_text_view);
342 gtk_widget_show (EMPATHY_CHAT (chat)->input_text_view);
344 /* Add nick name completion */
345 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_name);
346 g_completion_set_compare (priv->completion,
347 group_chat_contacts_completion_func);
349 /* Set widget focus order */
350 list = g_list_append (NULL, priv->scrolled_window_input);
351 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
354 list = g_list_append (NULL, priv->vbox_left);
355 list = g_list_append (list, priv->scrolled_window_contacts);
356 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
359 list = g_list_append (NULL, priv->hpaned);
360 list = g_list_append (list, priv->hbox_topic);
361 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
366 group_chat_widget_destroy_cb (GtkWidget *widget,
367 EmpathyGroupChat *chat)
369 empathy_debug (DEBUG_DOMAIN, "Destroyed");
371 g_object_unref (chat);
375 group_chat_members_changed_cb (EmpathyTpChatroom *tp_chat,
376 EmpathyContact *contact,
377 EmpathyContact *actor,
381 EmpathyGroupChat *chat)
383 if (!EMPATHY_CHAT (chat)->block_events) {
386 str = g_strdup_printf (_("%s has joined the room"),
387 empathy_contact_get_name (contact));
389 str = g_strdup_printf (_("%s has left the room"),
390 empathy_contact_get_name (contact));
392 empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, str);
398 group_chat_get_name (EmpathyChat *chat)
400 EmpathyGroupChat *group_chat;
401 EmpathyGroupChatPriv *priv;
403 g_return_val_if_fail (EMPATHY_IS_GROUP_CHAT (chat), NULL);
405 group_chat = EMPATHY_GROUP_CHAT (chat);
406 priv = GET_PRIV (group_chat);
412 id = empathy_chat_get_id (chat);
413 server = strstr (id, "@");
416 priv->name = g_strndup (id, server - id);
418 priv->name = g_strdup (id);
426 group_chat_get_tooltip (EmpathyChat *chat)
428 EmpathyGroupChat *group_chat;
429 EmpathyGroupChatPriv *priv;
431 g_return_val_if_fail (EMPATHY_IS_GROUP_CHAT (chat), NULL);
433 group_chat = EMPATHY_GROUP_CHAT (chat);
434 priv = GET_PRIV (group_chat);
439 topic = g_strdup_printf (_("Topic: %s"), priv->topic);
440 tmp = g_strdup_printf ("%s\n%s", priv->name, topic);
446 return g_strdup (priv->name);
450 group_chat_get_status_icon_name (EmpathyChat *chat)
452 return EMPATHY_IMAGE_GROUP_MESSAGE;
456 group_chat_get_widget (EmpathyChat *chat)
458 EmpathyGroupChat *group_chat;
459 EmpathyGroupChatPriv *priv;
461 g_return_val_if_fail (EMPATHY_IS_GROUP_CHAT (chat), NULL);
463 group_chat = EMPATHY_GROUP_CHAT (chat);
464 priv = GET_PRIV (group_chat);
470 group_chat_is_group_chat (EmpathyChat *chat)
472 g_return_val_if_fail (EMPATHY_IS_GROUP_CHAT (chat), FALSE);
478 group_chat_property_changed_cb (EmpathyTpChat *tp_chat,
481 EmpathyGroupChat *chat)
483 EmpathyGroupChatPriv *priv;
484 const gchar *str = NULL;
486 priv = GET_PRIV (chat);
488 /* FIXME: this is ugly, should use properties on EmpathyChat obj */
490 if (!tp_strdiff (name, "name")) {
491 str = g_value_get_string (value);
493 priv->name = g_strdup (str);
497 if (tp_strdiff (name, "subject")) {
501 str = g_value_get_string (value);
502 if (!tp_strdiff (priv->topic, str)) {
506 g_free (priv->topic);
507 priv->topic = g_strdup (str);
508 gtk_label_set_text (GTK_LABEL (priv->label_topic), priv->topic);
510 if (!EMPATHY_CHAT (chat)->block_events) {
513 if (!G_STR_EMPTY (priv->topic)) {
514 string = g_strdup_printf (_("Topic set to: %s"), priv->topic);
516 string = g_strdup (_("No topic defined"));
518 empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, string);
524 group_chat_set_tp_chat (EmpathyChat *chat,
525 EmpathyTpChat *tp_chat)
527 EmpathyGroupChat *group_chat;
528 EmpathyGroupChatPriv *priv;
530 g_return_if_fail (EMPATHY_IS_GROUP_CHAT (chat));
532 group_chat = EMPATHY_GROUP_CHAT (chat);
533 priv = GET_PRIV (group_chat);
535 /* Free all resources related to tp_chat */
537 g_object_unref (priv->tp_chat);
538 priv->tp_chat = NULL;
541 gtk_widget_destroy (GTK_WIDGET (priv->view));
542 g_object_unref (priv->store);
545 g_free (priv->topic);
550 /* We are no more connected */
551 gtk_widget_set_sensitive (priv->hbox_topic, FALSE);
552 gtk_widget_set_sensitive (priv->scrolled_window_contacts, FALSE);
556 /* We are connected */
557 gtk_widget_set_sensitive (priv->hbox_topic, TRUE);
558 gtk_widget_set_sensitive (priv->scrolled_window_contacts, TRUE);
560 priv->tp_chat = g_object_ref (tp_chat);
562 if (empathy_tp_chatroom_get_invitation (priv->tp_chat, NULL, NULL)) {
563 empathy_tp_chatroom_accept_invitation (priv->tp_chat);
566 /* Create contact list */
567 priv->store = empathy_contact_list_store_new (EMPATHY_CONTACT_LIST (priv->tp_chat));
568 priv->view = empathy_contact_list_view_new (priv->store,
569 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_CHAT |
570 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_CALL |
571 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_LOG |
572 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_FT |
573 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_INVITE |
574 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_INFO);
576 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
577 GTK_WIDGET (priv->view));
578 gtk_widget_show (GTK_WIDGET (priv->view));
580 /* Connect signals */
581 g_signal_connect (priv->tp_chat, "members-changed",
582 G_CALLBACK (group_chat_members_changed_cb),
584 g_signal_connect (priv->tp_chat, "property-changed",
585 G_CALLBACK (group_chat_property_changed_cb),
590 group_chat_key_press_event (EmpathyChat *chat,
593 EmpathyGroupChatPriv *priv = GET_PRIV (chat);
595 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
596 event->keyval == GDK_Tab) {
597 GtkTextBuffer *buffer;
598 GtkTextIter start, current;
599 gchar *nick, *completed;
600 GList *list, *completed_list;
601 gboolean is_start_of_buffer;
603 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
604 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
606 /* Get the start of the nick to complete. */
607 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
608 gtk_text_iter_backward_word_start (&start);
609 is_start_of_buffer = gtk_text_iter_is_start (&start);
611 list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
612 g_completion_add_items (priv->completion, list);
614 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
615 completed_list = g_completion_complete (priv->completion,
624 gchar *complete_char = NULL;
626 gtk_text_buffer_delete (buffer, &start, ¤t);
628 len = g_list_length (completed_list);
631 /* If we only have one hit, use that text
632 * instead of the text in completed since the
633 * completed text will use the typed string
634 * which might be cased all wrong.
637 text = empathy_contact_get_name (completed_list->data);
642 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
644 if (len == 1 && is_start_of_buffer &&
645 empathy_conf_get_string (empathy_conf_get (),
646 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR,
648 complete_char != NULL) {
649 gtk_text_buffer_insert_at_cursor (buffer,
651 strlen (complete_char));
652 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
653 g_free (complete_char);
659 g_completion_clear_items (priv->completion);
661 g_list_foreach (list, (GFunc) g_object_unref, NULL);
671 group_chat_contacts_completion_func (const gchar *s1,
675 gchar *tmp, *nick1, *nick2;
678 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
679 nick1 = g_utf8_casefold (tmp, -1);
682 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
683 nick2 = g_utf8_casefold (tmp, -1);
686 ret = strncmp (nick1, nick2, n);