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 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>
32 #include <glade/glade.h>
33 #include <glib/gi18n.h>
35 #include <libempathy/empathy-tp-chat.h>
36 #include <libempathy/empathy-tp-chatroom.h>
37 #include <libempathy/gossip-contact.h>
38 #include <libempathy/gossip-utils.h>
39 #include <libempathy/gossip-debug.h>
41 #include "gossip-group-chat.h"
42 #include "gossip-chat.h"
43 #include "gossip-chat-view.h"
44 #include "gossip-contact-list-store.h"
45 #include "gossip-contact-list-view.h"
46 //#include "gossip-chat-invite.h"
47 //#include "gossip-sound.h"
48 #include "empathy-images.h"
49 #include "gossip-ui-utils.h"
51 #define DEBUG_DOMAIN "GroupChat"
53 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GOSSIP_TYPE_GROUP_CHAT, GossipGroupChatPriv))
55 struct _GossipGroupChatPriv {
56 GossipContactListStore *store;
57 GossipContactListView *view;
58 EmpathyTpChatroom *tp_chat;
63 GtkWidget *scrolled_window_chat;
64 GtkWidget *scrolled_window_input;
65 GtkWidget *scrolled_window_contacts;
66 GtkWidget *hbox_topic;
67 GtkWidget *label_topic;
71 GCompletion *completion;
74 gboolean contacts_visible;
77 static void group_chat_finalize (GObject *object);
78 static void group_chat_create_ui (GossipGroupChat *chat);
79 static void group_chat_widget_destroy_cb (GtkWidget *widget,
80 GossipGroupChat *chat);
81 static void group_chat_contact_added_cb (EmpathyTpChatroom *tp_chat,
82 GossipContact *contact,
83 GossipGroupChat *chat);
84 static void group_chat_contact_removed_cb (EmpathyTpChatroom *tp_chat,
85 GossipContact *contact,
86 GossipGroupChat *chat);
87 /*static void group_chat_topic_changed_cb (EmpathyTpChatroom *tp_chat,
88 const gchar *new_topic,
89 GossipGroupChat *chat);*/
90 static void group_chat_topic_entry_activate_cb (GtkWidget *entry,
92 static void group_chat_topic_response_cb (GtkWidget *dialog,
94 GossipGroupChat *chat);
95 static const gchar * group_chat_get_name (GossipChat *chat);
96 static gchar * group_chat_get_tooltip (GossipChat *chat);
97 static const gchar * group_chat_get_status_icon_name (GossipChat *chat);
98 static GtkWidget * group_chat_get_widget (GossipChat *chat);
99 static gboolean group_chat_is_group_chat (GossipChat *chat);
100 static void group_chat_set_tp_chat (GossipChat *chat,
101 EmpathyTpChat *tp_chat);
102 static void group_chat_subject_notify_cb (EmpathyTpChat *tp_chat,
104 GossipGroupChat *chat);
105 static void group_chat_name_notify_cb (EmpathyTpChat *tp_chat,
107 GossipGroupChat *chat);
108 /*static gboolean group_chat_key_press_event (GtkWidget *widget,
110 GossipGroupChat *chat);*/
111 static gint group_chat_contacts_completion_func (const gchar *s1,
115 G_DEFINE_TYPE (GossipGroupChat, gossip_group_chat, GOSSIP_TYPE_CHAT)
118 gossip_group_chat_class_init (GossipGroupChatClass *klass)
120 GObjectClass *object_class;
121 GossipChatClass *chat_class;
123 object_class = G_OBJECT_CLASS (klass);
124 chat_class = GOSSIP_CHAT_CLASS (klass);
126 object_class->finalize = group_chat_finalize;
128 chat_class->get_name = group_chat_get_name;
129 chat_class->get_tooltip = group_chat_get_tooltip;
130 chat_class->get_status_icon_name = group_chat_get_status_icon_name;
131 chat_class->get_widget = group_chat_get_widget;
132 chat_class->is_group_chat = group_chat_is_group_chat;
133 chat_class->set_tp_chat = group_chat_set_tp_chat;
135 g_type_class_add_private (object_class, sizeof (GossipGroupChatPriv));
139 gossip_group_chat_init (GossipGroupChat *chat)
141 GossipGroupChatPriv *priv;
142 GossipChatView *chatview;
144 priv = GET_PRIV (chat);
146 priv->contacts_visible = TRUE;
148 chatview = GOSSIP_CHAT_VIEW (GOSSIP_CHAT (chat)->view);
149 gossip_chat_view_set_is_group_chat (chatview, TRUE);
151 group_chat_create_ui (chat);
155 group_chat_finalize (GObject *object)
157 GossipGroupChat *chat;
158 GossipGroupChatPriv *priv;
160 gossip_debug (DEBUG_DOMAIN, "Finalized:%p", object);
162 chat = GOSSIP_GROUP_CHAT (object);
163 priv = GET_PRIV (chat);
166 g_free (priv->topic);
167 g_object_unref (priv->store);
168 g_object_unref (priv->tp_chat);
169 g_completion_free (priv->completion);
171 G_OBJECT_CLASS (gossip_group_chat_parent_class)->finalize (object);
175 gossip_group_chat_new (McAccount *account,
178 GossipGroupChat *chat;
179 GossipGroupChatPriv *priv;
181 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
182 g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
184 chat = g_object_new (GOSSIP_TYPE_GROUP_CHAT, NULL);
186 priv = GET_PRIV (chat);
188 GOSSIP_CHAT (chat)->account = g_object_ref (account);
189 priv->tp_chat = empathy_tp_chatroom_new (account, tp_chan);
190 gossip_chat_set_tp_chat (GOSSIP_CHAT (chat), EMPATHY_TP_CHAT (priv->tp_chat));
196 gossip_group_chat_get_show_contacts (GossipGroupChat *chat)
198 GossipGroupChat *group_chat;
199 GossipGroupChatPriv *priv;
201 g_return_val_if_fail (GOSSIP_IS_GROUP_CHAT (chat), FALSE);
203 group_chat = GOSSIP_GROUP_CHAT (chat);
204 priv = GET_PRIV (group_chat);
206 return priv->contacts_visible;
210 gossip_group_chat_set_show_contacts (GossipGroupChat *chat,
213 GossipGroupChat *group_chat;
214 GossipGroupChatPriv *priv;
216 g_return_if_fail (GOSSIP_IS_GROUP_CHAT (chat));
218 group_chat = GOSSIP_GROUP_CHAT (chat);
219 priv = GET_PRIV (group_chat);
221 priv->contacts_visible = show;
224 gtk_widget_show (priv->scrolled_window_contacts);
225 gtk_paned_set_position (GTK_PANED (priv->hpaned),
226 priv->contacts_width);
228 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
229 gtk_widget_hide (priv->scrolled_window_contacts);
234 gossip_group_chat_set_topic (GossipGroupChat *chat)
236 GossipGroupChatPriv *priv;
237 GossipChatWindow *chat_window;
238 GtkWidget *chat_dialog;
244 g_return_if_fail (GOSSIP_IS_GROUP_CHAT (chat));
246 priv = GET_PRIV (chat);
248 chat_window = gossip_chat_get_window (GOSSIP_CHAT (chat));
249 chat_dialog = gossip_chat_window_get_dialog (chat_window);
251 dialog = gtk_message_dialog_new (GTK_WINDOW (chat_dialog),
253 GTK_MESSAGE_QUESTION,
254 GTK_BUTTONS_OK_CANCEL,
255 _("Enter the new topic you want to set for this room:"));
257 topic = gtk_label_get_text (GTK_LABEL (priv->label_topic));
259 hbox = gtk_hbox_new (FALSE, 0);
260 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
261 hbox, FALSE, TRUE, 4);
263 entry = gtk_entry_new ();
264 gtk_entry_set_text (GTK_ENTRY (entry), topic);
265 gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
267 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 4);
269 g_object_set (GTK_MESSAGE_DIALOG (dialog)->label, "use-markup", TRUE, NULL);
270 g_object_set_data (G_OBJECT (dialog), "entry", entry);
272 g_signal_connect (entry, "activate",
273 G_CALLBACK (group_chat_topic_entry_activate_cb),
275 g_signal_connect (dialog, "response",
276 G_CALLBACK (group_chat_topic_response_cb),
279 gtk_widget_show_all (dialog);
283 group_chat_create_ui (GossipGroupChat *chat)
285 GossipGroupChatPriv *priv;
289 priv = GET_PRIV (chat);
291 glade = gossip_glade_get_file ("gossip-group-chat.glade",
294 "group_chat_widget", &priv->widget,
295 "hpaned", &priv->hpaned,
296 "vbox_left", &priv->vbox_left,
297 "scrolled_window_chat", &priv->scrolled_window_chat,
298 "scrolled_window_input", &priv->scrolled_window_input,
299 "hbox_topic", &priv->hbox_topic,
300 "label_topic", &priv->label_topic,
301 "scrolled_window_contacts", &priv->scrolled_window_contacts,
304 gossip_glade_connect (glade,
306 "group_chat_widget", "destroy", group_chat_widget_destroy_cb,
309 g_object_unref (glade);
311 g_object_set_data (G_OBJECT (priv->widget), "chat", g_object_ref (chat));
313 /* Add room GtkTextView. */
314 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
315 GTK_WIDGET (GOSSIP_CHAT (chat)->view));
316 gtk_widget_show (GTK_WIDGET (GOSSIP_CHAT (chat)->view));
318 /* Add input GtkTextView */
319 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
320 GOSSIP_CHAT (chat)->input_text_view);
321 gtk_widget_show (GOSSIP_CHAT (chat)->input_text_view);
323 /* Add nick name completion */
324 priv->completion = g_completion_new (NULL);
325 g_completion_set_compare (priv->completion,
326 group_chat_contacts_completion_func);
328 /* Set widget focus order */
329 list = g_list_append (NULL, priv->scrolled_window_input);
330 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
333 list = g_list_append (NULL, priv->vbox_left);
334 list = g_list_append (list, priv->scrolled_window_contacts);
335 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
338 list = g_list_append (NULL, priv->hpaned);
339 list = g_list_append (list, priv->hbox_topic);
340 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
345 group_chat_widget_destroy_cb (GtkWidget *widget,
346 GossipGroupChat *chat)
348 gossip_debug (DEBUG_DOMAIN, "Destroyed");
350 g_object_unref (chat);
354 group_chat_contact_added_cb (EmpathyTpChatroom *tp_chat,
355 GossipContact *contact,
356 GossipGroupChat *chat)
358 GossipGroupChatPriv *priv;
361 priv = GET_PRIV (chat);
363 str = g_strdup_printf (_("%s has joined the room"),
364 gossip_contact_get_name (contact));
365 gossip_chat_view_append_event (GOSSIP_CHAT (chat)->view, str);
370 group_chat_contact_removed_cb (EmpathyTpChatroom *tp_chat,
371 GossipContact *contact,
372 GossipGroupChat *chat)
374 GossipGroupChatPriv *priv;
377 priv = GET_PRIV (chat);
379 str = g_strdup_printf (_("%s has left the room"),
380 gossip_contact_get_name (contact));
381 gossip_chat_view_append_event (GOSSIP_CHAT (chat)->view, str);
386 group_chat_topic_entry_activate_cb (GtkWidget *entry,
389 gtk_dialog_response (dialog, GTK_RESPONSE_OK);
393 group_chat_topic_response_cb (GtkWidget *dialog,
395 GossipGroupChat *chat)
397 if (response == GTK_RESPONSE_OK) {
401 entry = g_object_get_data (G_OBJECT (dialog), "entry");
402 topic = gtk_entry_get_text (GTK_ENTRY (entry));
404 if (!G_STR_EMPTY (topic)) {
405 GossipGroupChatPriv *priv;
407 priv = GET_PRIV (chat);
409 empathy_tp_chatroom_set_topic (priv->tp_chat, topic);
413 gtk_widget_destroy (dialog);
417 group_chat_get_name (GossipChat *chat)
419 GossipGroupChat *group_chat;
420 GossipGroupChatPriv *priv;
422 g_return_val_if_fail (GOSSIP_IS_GROUP_CHAT (chat), NULL);
424 group_chat = GOSSIP_GROUP_CHAT (chat);
425 priv = GET_PRIV (group_chat);
431 id = gossip_chat_get_id (chat);
432 server = strstr (id, "@");
435 priv->name = g_strndup (id, server - id);
437 priv->name = g_strdup (id);
445 group_chat_get_tooltip (GossipChat *chat)
447 GossipGroupChat *group_chat;
448 GossipGroupChatPriv *priv;
450 g_return_val_if_fail (GOSSIP_IS_GROUP_CHAT (chat), NULL);
452 group_chat = GOSSIP_GROUP_CHAT (chat);
453 priv = GET_PRIV (group_chat);
458 topic = g_strdup_printf (_("Topic: %s"), priv->topic);
459 tmp = g_strdup_printf ("%s\n%s", priv->name, topic);
465 return g_strdup (priv->name);
469 group_chat_get_status_icon_name (GossipChat *chat)
471 return EMPATHY_IMAGE_GROUP_MESSAGE;
475 group_chat_get_widget (GossipChat *chat)
477 GossipGroupChat *group_chat;
478 GossipGroupChatPriv *priv;
480 g_return_val_if_fail (GOSSIP_IS_GROUP_CHAT (chat), NULL);
482 group_chat = GOSSIP_GROUP_CHAT (chat);
483 priv = GET_PRIV (group_chat);
489 group_chat_is_group_chat (GossipChat *chat)
491 g_return_val_if_fail (GOSSIP_IS_GROUP_CHAT (chat), FALSE);
497 group_chat_set_tp_chat (GossipChat *chat,
498 EmpathyTpChat *tp_chat)
500 GossipGroupChat *group_chat;
501 GossipGroupChatPriv *priv;
503 g_return_if_fail (GOSSIP_IS_GROUP_CHAT (chat));
505 group_chat = GOSSIP_GROUP_CHAT (chat);
506 priv = GET_PRIV (group_chat);
508 /* Free all resources related to tp_chat */
510 g_object_unref (priv->tp_chat);
511 priv->tp_chat = NULL;
514 gtk_widget_destroy (GTK_WIDGET (priv->view));
515 g_object_unref (priv->store);
518 g_free (priv->topic);
523 /* We are no more connected */
524 gtk_widget_set_sensitive (priv->hbox_topic, FALSE);
525 gtk_widget_set_sensitive (priv->scrolled_window_contacts, FALSE);
529 /* We are connected */
530 gtk_widget_set_sensitive (priv->hbox_topic, TRUE);
531 gtk_widget_set_sensitive (priv->scrolled_window_contacts, TRUE);
533 priv->tp_chat = g_object_ref (tp_chat);
535 /* FIXME: Ask the user before accepting */
536 empathy_tp_chatroom_accept_invitation (priv->tp_chat);
538 /* Create contact list */
539 priv->store = gossip_contact_list_store_new (EMPATHY_CONTACT_LIST (priv->tp_chat));
540 priv->view = gossip_contact_list_view_new (priv->store);
541 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
542 GTK_WIDGET (priv->view));
543 gtk_widget_show (GTK_WIDGET (priv->view));
545 /* Connect signals */
546 g_signal_connect (priv->tp_chat, "contact-added",
547 G_CALLBACK (group_chat_contact_added_cb),
549 g_signal_connect (priv->tp_chat, "contact-removed",
550 G_CALLBACK (group_chat_contact_removed_cb),
552 g_signal_connect (priv->tp_chat, "notify::subject",
553 G_CALLBACK (group_chat_subject_notify_cb),
555 g_signal_connect (priv->tp_chat, "notify::name",
556 G_CALLBACK (group_chat_name_notify_cb),
561 group_chat_subject_notify_cb (EmpathyTpChat *tp_chat,
563 GossipGroupChat *chat)
565 GossipGroupChatPriv *priv;
568 priv = GET_PRIV (chat);
570 g_free (priv->topic);
572 g_object_get (priv->tp_chat, "subject", &priv->topic, NULL);
573 gtk_label_set_text (GTK_LABEL (priv->label_topic), priv->topic);
575 str = g_strdup_printf (_("Topic set to: %s"), priv->topic);
576 gossip_chat_view_append_event (GOSSIP_CHAT (chat)->view, str);
581 group_chat_name_notify_cb (EmpathyTpChat *tp_chat,
583 GossipGroupChat *chat)
585 GossipGroupChatPriv *priv;
587 priv = GET_PRIV (chat);
590 g_object_get (priv->tp_chat, "name", &priv->name, NULL);
595 group_chat_key_press_event (GtkWidget *widget,
597 GossipGroupChat *chat)
599 GossipGroupChatPriv *priv;
602 GtkTextBuffer *buffer;
603 GtkTextIter start, current;
604 gchar *nick, *completed;
606 GList *list, *l, *completed_list;
607 gboolean is_start_of_buffer;
609 priv = GET_PRIV (chat);
611 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
612 (event->state & GDK_SHIFT_MASK) != GDK_SHIFT_MASK &&
613 event->keyval == GDK_Tab) {
614 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (GOSSIP_CHAT (chat)->input_text_view));
615 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
617 /* Get the start of the nick to complete. */
618 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
619 gtk_text_iter_backward_word_start (&start);
620 is_start_of_buffer = gtk_text_iter_is_start (&start);
622 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
624 g_completion_clear_items (priv->completion);
628 list = group_chat_get_nick_list (chat);
630 g_completion_add_items (priv->completion, list);
632 completed_list = g_completion_complete (priv->completion,
642 gtk_text_buffer_delete (buffer, &start, ¤t);
644 len = g_list_length (completed_list);
647 /* If we only have one hit, use that text
648 * instead of the text in completed since the
649 * completed text will use the typed string
650 * which might be cased all wrong.
653 text = (gchar *) completed_list->data;
658 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
661 if (is_start_of_buffer) {
662 gtk_text_buffer_insert_at_cursor (buffer, ", ", 2);
669 g_completion_clear_items (priv->completion);
671 for (l = list; l; l = l->next) {
685 group_chat_contacts_completion_func (const gchar *s1,
689 gchar *tmp, *nick1, *nick2;
692 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
693 nick1 = g_utf8_casefold (tmp, -1);
696 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
697 nick2 = g_utf8_casefold (tmp, -1);
700 ret = strncmp (nick1, nick2, n);