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 void gossip_group_chat_set_topic (GossipGroupChat *chat);
96 static const gchar * group_chat_get_name (GossipChat *chat);
97 static gchar * group_chat_get_tooltip (GossipChat *chat);
98 static const gchar * group_chat_get_status_icon_name (GossipChat *chat);
99 static GtkWidget * group_chat_get_widget (GossipChat *chat);
100 static gboolean group_chat_is_group_chat (GossipChat *chat);
101 /*static gboolean group_chat_key_press_event (GtkWidget *widget,
103 GossipGroupChat *chat);*/
104 static gint group_chat_contacts_completion_func (const gchar *s1,
108 G_DEFINE_TYPE (GossipGroupChat, gossip_group_chat, GOSSIP_TYPE_CHAT)
111 gossip_group_chat_class_init (GossipGroupChatClass *klass)
113 GObjectClass *object_class;
114 GossipChatClass *chat_class;
116 object_class = G_OBJECT_CLASS (klass);
117 chat_class = GOSSIP_CHAT_CLASS (klass);
119 object_class->finalize = group_chat_finalize;
121 chat_class->get_name = group_chat_get_name;
122 chat_class->get_tooltip = group_chat_get_tooltip;
123 chat_class->get_status_icon_name = group_chat_get_status_icon_name;
124 chat_class->get_widget = group_chat_get_widget;
125 chat_class->is_group_chat = group_chat_is_group_chat;
127 g_type_class_add_private (object_class, sizeof (GossipGroupChatPriv));
131 gossip_group_chat_init (GossipGroupChat *chat)
133 GossipGroupChatPriv *priv;
134 GossipChatView *chatview;
136 priv = GET_PRIV (chat);
138 priv->contacts_visible = TRUE;
140 chatview = GOSSIP_CHAT_VIEW (GOSSIP_CHAT (chat)->view);
141 gossip_chat_view_set_is_group_chat (chatview, TRUE);
143 group_chat_create_ui (chat);
147 group_chat_finalize (GObject *object)
149 GossipGroupChat *chat;
150 GossipGroupChatPriv *priv;
152 gossip_debug (DEBUG_DOMAIN, "Finalized:%p", object);
154 chat = GOSSIP_GROUP_CHAT (object);
155 priv = GET_PRIV (chat);
158 g_free (priv->topic);
159 g_object_unref (priv->store);
160 g_object_unref (priv->tp_chat);
161 g_completion_free (priv->completion);
163 G_OBJECT_CLASS (gossip_group_chat_parent_class)->finalize (object);
167 gossip_group_chat_new (McAccount *account,
170 GossipGroupChat *chat;
171 GossipGroupChatPriv *priv;
173 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
174 g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
176 chat = g_object_new (GOSSIP_TYPE_GROUP_CHAT, NULL);
178 priv = GET_PRIV (chat);
180 priv->tp_chat = empathy_tp_chatroom_new (account, tp_chan);
181 gossip_chat_set_tp_chat (GOSSIP_CHAT (chat), EMPATHY_TP_CHAT (priv->tp_chat));
182 GOSSIP_CHAT (chat)->account = g_object_ref (account);
184 /* FIXME: Ask the user before accepting */
185 empathy_tp_chatroom_accept_invitation (priv->tp_chat);
187 /* Create contact list */
188 priv->store = gossip_contact_list_store_new (EMPATHY_CONTACT_LIST (priv->tp_chat));
189 priv->view = gossip_contact_list_view_new (priv->store);
190 gossip_contact_list_store_set_show_offline (priv->store, TRUE);
191 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
192 GTK_WIDGET (priv->view));
193 gtk_widget_show (GTK_WIDGET (priv->view));
195 g_signal_connect (priv->tp_chat, "contact-added",
196 G_CALLBACK (group_chat_contact_added_cb),
198 g_signal_connect (priv->tp_chat, "contact-removed",
199 G_CALLBACK (group_chat_contact_removed_cb),
201 /* g_signal_connect (priv->tp_chat, "chatroom-topic-changed",
202 G_CALLBACK (group_chat_topic_changed_cb),
204 g_signal_connect (priv->tp_chat, "contact-info-changed",
205 G_CALLBACK (group_chat_contact_info_changed_cb),
212 gossip_group_chat_get_show_contacts (GossipChat *chat)
214 GossipGroupChat *group_chat;
215 GossipGroupChatPriv *priv;
217 g_return_val_if_fail (GOSSIP_IS_GROUP_CHAT (chat), FALSE);
219 group_chat = GOSSIP_GROUP_CHAT (chat);
220 priv = GET_PRIV (group_chat);
222 return priv->contacts_visible;
226 gossip_group_chat_set_show_contacts (GossipChat *chat,
229 GossipGroupChat *group_chat;
230 GossipGroupChatPriv *priv;
232 g_return_if_fail (GOSSIP_IS_GROUP_CHAT (chat));
234 group_chat = GOSSIP_GROUP_CHAT (chat);
235 priv = GET_PRIV (group_chat);
237 priv->contacts_visible = show;
240 gtk_widget_show (priv->scrolled_window_contacts);
241 gtk_paned_set_position (GTK_PANED (priv->hpaned),
242 priv->contacts_width);
244 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
245 gtk_widget_hide (priv->scrolled_window_contacts);
250 group_chat_create_ui (GossipGroupChat *chat)
252 GossipGroupChatPriv *priv;
256 priv = GET_PRIV (chat);
258 glade = gossip_glade_get_file ("gossip-group-chat.glade",
261 "group_chat_widget", &priv->widget,
262 "hpaned", &priv->hpaned,
263 "vbox_left", &priv->vbox_left,
264 "scrolled_window_chat", &priv->scrolled_window_chat,
265 "scrolled_window_input", &priv->scrolled_window_input,
266 "hbox_topic", &priv->hbox_topic,
267 "label_topic", &priv->label_topic,
268 "scrolled_window_contacts", &priv->scrolled_window_contacts,
271 gossip_glade_connect (glade,
273 "group_chat_widget", "destroy", group_chat_widget_destroy_cb,
276 g_object_unref (glade);
278 g_object_set_data (G_OBJECT (priv->widget), "chat", chat);
280 /* Add room GtkTextView. */
281 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
282 GTK_WIDGET (GOSSIP_CHAT (chat)->view));
283 gtk_widget_show (GTK_WIDGET (GOSSIP_CHAT (chat)->view));
285 /* Add input GtkTextView */
286 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
287 GOSSIP_CHAT (chat)->input_text_view);
288 gtk_widget_show (GOSSIP_CHAT (chat)->input_text_view);
290 /* Add nick name completion */
291 priv->completion = g_completion_new (NULL);
292 g_completion_set_compare (priv->completion,
293 group_chat_contacts_completion_func);
295 /* Set widget focus order */
296 list = g_list_append (NULL, priv->scrolled_window_input);
297 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
300 list = g_list_append (NULL, priv->vbox_left);
301 list = g_list_append (list, priv->scrolled_window_contacts);
302 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
305 list = g_list_append (NULL, priv->hpaned);
306 list = g_list_append (list, priv->hbox_topic);
307 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
312 group_chat_widget_destroy_cb (GtkWidget *widget,
313 GossipGroupChat *chat)
315 gossip_debug (DEBUG_DOMAIN, "Destroyed");
317 g_object_unref (chat);
321 group_chat_contact_added_cb (EmpathyTpChatroom *tp_chat,
322 GossipContact *contact,
323 GossipGroupChat *chat)
325 GossipGroupChatPriv *priv;
328 priv = GET_PRIV (chat);
330 str = g_strdup_printf (_("%s has joined the room"),
331 gossip_contact_get_name (contact));
332 gossip_chat_view_append_event (GOSSIP_CHAT (chat)->view, str);
337 group_chat_contact_removed_cb (EmpathyTpChatroom *tp_chat,
338 GossipContact *contact,
339 GossipGroupChat *chat)
341 GossipGroupChatPriv *priv;
344 priv = GET_PRIV (chat);
346 str = g_strdup_printf (_("%s has left the room"),
347 gossip_contact_get_name (contact));
348 gossip_chat_view_append_event (GOSSIP_CHAT (chat)->view, str);
353 group_chat_topic_changed_cb (EmpathyTpChatroom *tp_chat,
354 const gchar *new_topic,
355 GossipGroupChat *chat)
357 GossipGroupChatPriv *priv;
360 priv = GET_PRIV (chat);
362 gossip_debug (DEBUG_DOMAIN, "Topic changed by to:'%s'", new_topic);
364 g_free (priv->topic);
365 priv->topic = g_strdup (new_topic);
367 gtk_label_set_text (GTK_LABEL (priv->label_topic), new_topic);
369 str = g_strdup_printf (_("Topic set to: %s"), new_topic);
370 gossip_chat_view_append_event (GOSSIP_CHAT (chat)->view, str);
375 group_chat_topic_entry_activate_cb (GtkWidget *entry,
378 gtk_dialog_response (dialog, GTK_RESPONSE_OK);
382 group_chat_topic_response_cb (GtkWidget *dialog,
384 GossipGroupChat *chat)
386 if (response == GTK_RESPONSE_OK) {
390 entry = g_object_get_data (G_OBJECT (dialog), "entry");
391 topic = gtk_entry_get_text (GTK_ENTRY (entry));
393 if (!G_STR_EMPTY (topic)) {
394 GossipGroupChatPriv *priv;
396 priv = GET_PRIV (chat);
398 empathy_tp_chatroom_set_topic (priv->tp_chat, topic);
402 gtk_widget_destroy (dialog);
406 gossip_group_chat_set_topic (GossipGroupChat *chat)
408 GossipGroupChatPriv *priv;
409 GossipChatWindow *chat_window;
410 GtkWidget *chat_dialog;
416 priv = GET_PRIV (chat);
418 chat_window = gossip_chat_get_window (GOSSIP_CHAT (chat));
419 chat_dialog = gossip_chat_window_get_dialog (chat_window);
421 dialog = gtk_message_dialog_new (GTK_WINDOW (chat_dialog),
423 GTK_MESSAGE_QUESTION,
424 GTK_BUTTONS_OK_CANCEL,
425 _("Enter the new topic you want to set for this room:"));
427 topic = gtk_label_get_text (GTK_LABEL (priv->label_topic));
429 hbox = gtk_hbox_new (FALSE, 0);
430 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
431 hbox, FALSE, TRUE, 4);
433 entry = gtk_entry_new ();
434 gtk_entry_set_text (GTK_ENTRY (entry), topic);
435 gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
437 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 4);
439 g_object_set (GTK_MESSAGE_DIALOG (dialog)->label, "use-markup", TRUE, NULL);
440 g_object_set_data (G_OBJECT (dialog), "entry", entry);
442 g_signal_connect (entry, "activate",
443 G_CALLBACK (group_chat_topic_entry_activate_cb),
445 g_signal_connect (dialog, "response",
446 G_CALLBACK (group_chat_topic_response_cb),
449 gtk_widget_show_all (dialog);
453 group_chat_get_name (GossipChat *chat)
455 GossipGroupChat *group_chat;
456 GossipGroupChatPriv *priv;
458 g_return_val_if_fail (GOSSIP_IS_GROUP_CHAT (chat), NULL);
460 group_chat = GOSSIP_GROUP_CHAT (chat);
461 priv = GET_PRIV (group_chat);
467 group_chat_get_tooltip (GossipChat *chat)
469 GossipGroupChat *group_chat;
470 GossipGroupChatPriv *priv;
472 g_return_val_if_fail (GOSSIP_IS_GROUP_CHAT (chat), NULL);
474 group_chat = GOSSIP_GROUP_CHAT (chat);
475 priv = GET_PRIV (group_chat);
480 topic = g_strdup_printf (_("Topic: %s"), priv->topic);
481 tmp = g_strdup_printf ("%s\n%s", priv->name, topic);
487 return g_strdup (priv->name);
491 group_chat_get_status_icon_name (GossipChat *chat)
493 return EMPATHY_IMAGE_GROUP_MESSAGE;
497 group_chat_get_widget (GossipChat *chat)
499 GossipGroupChat *group_chat;
500 GossipGroupChatPriv *priv;
502 g_return_val_if_fail (GOSSIP_IS_GROUP_CHAT (chat), NULL);
504 group_chat = GOSSIP_GROUP_CHAT (chat);
505 priv = GET_PRIV (group_chat);
511 group_chat_is_group_chat (GossipChat *chat)
513 g_return_val_if_fail (GOSSIP_IS_GROUP_CHAT (chat), FALSE);
519 group_chat_key_press_event (GtkWidget *widget,
521 GossipGroupChat *chat)
523 GossipGroupChatPriv *priv;
526 GtkTextBuffer *buffer;
527 GtkTextIter start, current;
528 gchar *nick, *completed;
530 GList *list, *l, *completed_list;
531 gboolean is_start_of_buffer;
533 priv = GET_PRIV (chat);
535 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
536 (event->state & GDK_SHIFT_MASK) != GDK_SHIFT_MASK &&
537 event->keyval == GDK_Tab) {
538 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (GOSSIP_CHAT (chat)->input_text_view));
539 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
541 /* Get the start of the nick to complete. */
542 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
543 gtk_text_iter_backward_word_start (&start);
544 is_start_of_buffer = gtk_text_iter_is_start (&start);
546 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
548 g_completion_clear_items (priv->completion);
552 list = group_chat_get_nick_list (chat);
554 g_completion_add_items (priv->completion, list);
556 completed_list = g_completion_complete (priv->completion,
566 gtk_text_buffer_delete (buffer, &start, ¤t);
568 len = g_list_length (completed_list);
571 /* If we only have one hit, use that text
572 * instead of the text in completed since the
573 * completed text will use the typed string
574 * which might be cased all wrong.
577 text = (gchar *) completed_list->data;
582 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
585 if (is_start_of_buffer) {
586 gtk_text_buffer_insert_at_cursor (buffer, ", ", 2);
593 g_completion_clear_items (priv->completion);
595 for (l = list; l; l = l->next) {
609 group_chat_contacts_completion_func (const gchar *s1,
613 gchar *tmp, *nick1, *nick2;
616 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
617 nick1 = g_utf8_casefold (tmp, -1);
620 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
621 nick2 = g_utf8_casefold (tmp, -1);
624 ret = strncmp (nick1, nick2, n);