Move modules that make no sense to be used in other applicaton from libempathy-gtk...
[empathy.git] / libempathy-gtk / empathy-group-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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
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>
25  */
26
27 #include "config.h"
28
29 #include <string.h>
30
31 #include <gdk/gdkkeysyms.h>
32 #include <gtk/gtk.h>
33 #include <glade/glade.h>
34 #include <glib/gi18n.h>
35
36 #include <telepathy-glib/util.h>
37
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>
43
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"
54
55 #define DEBUG_DOMAIN "GroupChat"
56
57 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_GROUP_CHAT, EmpathyGroupChatPriv))
58
59 struct _EmpathyGroupChatPriv {
60         EmpathyContactListStore *store;
61         EmpathyContactListView  *view;
62         EmpathyTpChatroom      *tp_chat;
63
64         GtkWidget              *widget;
65         GtkWidget              *hpaned;
66         GtkWidget              *vbox_left;
67         GtkWidget              *scrolled_window_chat;
68         GtkWidget              *scrolled_window_input;
69         GtkWidget              *scrolled_window_contacts;
70         GtkWidget              *hbox_topic;
71         GtkWidget              *label_topic;
72
73         gchar                  *topic;
74         gchar                  *name;
75         GCompletion            *completion;
76
77         gint                    contacts_width;
78         gboolean                contacts_visible;
79 };
80
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,
88                                                           guint              reason,
89                                                           gchar             *message,
90                                                           gboolean           is_member,
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,
100                                                           GdkEventKey       *event);
101 static gint          group_chat_contacts_completion_func (const gchar       *s1,
102                                                           const gchar       *s2,
103                                                           gsize              n);
104
105 G_DEFINE_TYPE (EmpathyGroupChat, empathy_group_chat, EMPATHY_TYPE_CHAT)
106
107 static void
108 empathy_group_chat_class_init (EmpathyGroupChatClass *klass)
109 {
110         GObjectClass    *object_class;
111         EmpathyChatClass *chat_class;
112
113         object_class = G_OBJECT_CLASS (klass);
114         chat_class = EMPATHY_CHAT_CLASS (klass);
115
116         object_class->finalize           = group_chat_finalize;
117
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;
125
126         g_type_class_add_private (object_class, sizeof (EmpathyGroupChatPriv));
127 }
128
129 static void
130 empathy_group_chat_init (EmpathyGroupChat *chat)
131 {
132         EmpathyGroupChatPriv *priv;
133         EmpathyChatView      *chatview;
134
135         priv = GET_PRIV (chat);
136
137         priv->contacts_visible = TRUE;
138
139         chatview = EMPATHY_CHAT_VIEW (EMPATHY_CHAT (chat)->view);
140         empathy_chat_view_set_is_group_chat (chatview, TRUE);
141
142         group_chat_create_ui (chat);
143 }
144
145 static void
146 group_chat_finalize (GObject *object)
147 {
148         EmpathyGroupChat     *chat;
149         EmpathyGroupChatPriv *priv;
150
151         empathy_debug (DEBUG_DOMAIN, "Finalized:%p", object);
152
153         chat = EMPATHY_GROUP_CHAT (object);
154         priv = GET_PRIV (chat);
155         
156         g_free (priv->name);
157         g_free (priv->topic);
158         if (priv->store) {
159                 g_object_unref (priv->store);
160         }
161         if (priv->tp_chat) {
162                 g_object_unref (priv->tp_chat); 
163         }
164         g_completion_free (priv->completion);
165
166         G_OBJECT_CLASS (empathy_group_chat_parent_class)->finalize (object);
167 }
168
169 EmpathyGroupChat *
170 empathy_group_chat_new (EmpathyTpChatroom *tp_chat)
171 {
172         EmpathyGroupChat *chat;
173
174         g_return_val_if_fail (EMPATHY_IS_TP_CHAT (tp_chat), NULL);
175
176         chat = g_object_new (EMPATHY_TYPE_GROUP_CHAT,
177                              "tp-chat", tp_chat,
178                              NULL);
179
180         return chat;
181 }
182
183 gboolean
184 empathy_group_chat_get_show_contacts (EmpathyGroupChat *chat)
185 {
186         EmpathyGroupChat     *group_chat;
187         EmpathyGroupChatPriv *priv;
188
189         g_return_val_if_fail (EMPATHY_IS_GROUP_CHAT (chat), FALSE);
190
191         group_chat = EMPATHY_GROUP_CHAT (chat);
192         priv = GET_PRIV (group_chat);
193
194         return priv->contacts_visible;
195 }
196
197 void
198 empathy_group_chat_set_show_contacts (EmpathyGroupChat *chat,
199                                      gboolean         show)
200 {
201         EmpathyGroupChat     *group_chat;
202         EmpathyGroupChatPriv *priv;
203
204         g_return_if_fail (EMPATHY_IS_GROUP_CHAT (chat));
205
206         group_chat = EMPATHY_GROUP_CHAT (chat);
207         priv = GET_PRIV (group_chat);
208
209         priv->contacts_visible = show;
210
211         if (show) {
212                 gtk_widget_show (priv->scrolled_window_contacts);
213                 gtk_paned_set_position (GTK_PANED (priv->hpaned),
214                                         priv->contacts_width);
215         } else {
216                 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
217                 gtk_widget_hide (priv->scrolled_window_contacts);
218         }
219 }
220
221 static void
222 group_chat_topic_response_cb (GtkWidget        *dialog,
223                               gint              response,
224                               EmpathyGroupChat *chat)
225 {
226         if (response == GTK_RESPONSE_OK) {
227                 GtkWidget   *entry;
228                 const gchar *topic;
229
230                 entry = g_object_get_data (G_OBJECT (dialog), "entry");
231                 topic = gtk_entry_get_text (GTK_ENTRY (entry));
232                 
233                 if (!G_STR_EMPTY (topic)) {
234                         EmpathyGroupChatPriv *priv;
235                         GValue                value = {0, };
236
237                         priv = GET_PRIV (chat);
238
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),
242                                                       "subject", &value);
243                         g_value_unset (&value);
244                 }
245         }
246
247         gtk_widget_destroy (dialog);
248 }
249
250 static void
251 group_chat_topic_entry_activate_cb (GtkWidget *entry,
252                                     GtkDialog *dialog)
253 {
254         gtk_dialog_response (dialog, GTK_RESPONSE_OK);
255 }
256
257 void
258 empathy_group_chat_set_topic (EmpathyGroupChat *chat)
259 {
260         EmpathyGroupChatPriv *priv;
261         GtkWindow            *parent;
262         GtkWidget            *dialog;
263         GtkWidget            *entry;
264         GtkWidget            *hbox;
265         const gchar          *topic;
266
267         priv = GET_PRIV (chat);
268
269         g_return_if_fail (EMPATHY_IS_GROUP_CHAT (chat));
270
271         parent = empathy_get_toplevel_window (empathy_chat_get_widget (EMPATHY_CHAT (chat)));
272         dialog = gtk_message_dialog_new (GTK_WINDOW (parent),
273                                          0,
274                                          GTK_MESSAGE_QUESTION,
275                                          GTK_BUTTONS_OK_CANCEL,
276                                          _("Enter the new topic you want to set for this room:"));
277
278         topic = gtk_label_get_text (GTK_LABEL (priv->label_topic));
279
280         hbox = gtk_hbox_new (FALSE, 0);
281         gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
282                             hbox, FALSE, TRUE, 4);
283
284         entry = gtk_entry_new ();
285         gtk_entry_set_text (GTK_ENTRY (entry), topic);
286         gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
287                     
288         gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 4);
289
290         g_object_set (GTK_MESSAGE_DIALOG (dialog)->label, "use-markup", TRUE, NULL);
291         g_object_set_data (G_OBJECT (dialog), "entry", entry);
292
293         g_signal_connect (entry, "activate",
294                           G_CALLBACK (group_chat_topic_entry_activate_cb),
295                           dialog);
296         g_signal_connect (dialog, "response",
297                           G_CALLBACK (group_chat_topic_response_cb),
298                           chat);
299
300         gtk_widget_show_all (dialog);
301 }
302
303 static void
304 group_chat_create_ui (EmpathyGroupChat *chat)
305 {
306         EmpathyGroupChatPriv *priv;
307         GladeXML            *glade;
308         GList               *list = NULL; 
309
310         priv = GET_PRIV (chat);
311
312         glade = empathy_glade_get_file ("empathy-group-chat.glade",
313                                        "group_chat_widget",
314                                        NULL,
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,
323                                        NULL);
324
325         empathy_glade_connect (glade,
326                               chat,
327                               "group_chat_widget", "destroy", group_chat_widget_destroy_cb,
328                               NULL);
329
330         g_object_unref (glade);
331
332         g_object_set_data (G_OBJECT (priv->widget), "chat", g_object_ref (chat));
333
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));
338
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);
343
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);
348
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);
352         g_list_free (list);
353
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);
357         g_list_free (list);
358
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);
362         g_list_free (list);
363 }
364
365 static void
366 group_chat_widget_destroy_cb (GtkWidget       *widget,
367                               EmpathyGroupChat *chat)
368 {
369         empathy_debug (DEBUG_DOMAIN, "Destroyed");
370
371         g_object_unref (chat);
372 }
373
374 static void
375 group_chat_members_changed_cb (EmpathyTpChatroom *tp_chat,
376                                EmpathyContact     *contact,
377                                EmpathyContact     *actor,
378                                guint               reason,
379                                gchar              *message,
380                                gboolean            is_member,
381                                EmpathyGroupChat   *chat)
382 {
383         if (!EMPATHY_CHAT (chat)->block_events) {
384                 gchar *str;
385                 if (is_member) {
386                         str = g_strdup_printf (_("%s has joined the room"),
387                                                empathy_contact_get_name (contact));
388                 } else {
389                         str = g_strdup_printf (_("%s has left the room"),
390                                                empathy_contact_get_name (contact));
391                 }
392                 empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, str);
393                 g_free (str);
394         }
395 }
396
397 static const gchar *
398 group_chat_get_name (EmpathyChat *chat)
399 {
400         EmpathyGroupChat     *group_chat;
401         EmpathyGroupChatPriv *priv;
402
403         g_return_val_if_fail (EMPATHY_IS_GROUP_CHAT (chat), NULL);
404
405         group_chat = EMPATHY_GROUP_CHAT (chat);
406         priv = GET_PRIV (group_chat);
407
408         if (!priv->name) {
409                 const gchar *id;
410                 const gchar *server;
411
412                 id = empathy_chat_get_id (chat);
413                 server = strstr (id, "@");
414
415                 if (server) {
416                         priv->name = g_strndup (id, server - id);
417                 } else {
418                         priv->name = g_strdup (id);
419                 } 
420         }
421
422         return priv->name;
423 }
424
425 static gchar *
426 group_chat_get_tooltip (EmpathyChat *chat)
427 {
428         EmpathyGroupChat     *group_chat;
429         EmpathyGroupChatPriv *priv;
430
431         g_return_val_if_fail (EMPATHY_IS_GROUP_CHAT (chat), NULL);
432
433         group_chat = EMPATHY_GROUP_CHAT (chat);
434         priv = GET_PRIV (group_chat);
435
436         if (priv->topic) {
437                 gchar *topic, *tmp;
438
439                 topic = g_strdup_printf (_("Topic: %s"), priv->topic);
440                 tmp = g_strdup_printf ("%s\n%s", priv->name, topic);
441                 g_free (topic);
442
443                 return tmp;
444         }
445
446         return g_strdup (priv->name);
447 }
448
449 static const gchar *
450 group_chat_get_status_icon_name (EmpathyChat *chat)
451 {
452         return EMPATHY_IMAGE_GROUP_MESSAGE;
453 }
454
455 static GtkWidget *
456 group_chat_get_widget (EmpathyChat *chat)
457 {
458         EmpathyGroupChat     *group_chat;
459         EmpathyGroupChatPriv *priv;
460
461         g_return_val_if_fail (EMPATHY_IS_GROUP_CHAT (chat), NULL);
462
463         group_chat = EMPATHY_GROUP_CHAT (chat);
464         priv = GET_PRIV (group_chat);
465
466         return priv->widget;
467 }
468
469 static gboolean
470 group_chat_is_group_chat (EmpathyChat *chat)
471 {
472         g_return_val_if_fail (EMPATHY_IS_GROUP_CHAT (chat), FALSE);
473
474         return TRUE;
475 }
476
477 static void
478 group_chat_property_changed_cb (EmpathyTpChat    *tp_chat,
479                                 gchar            *name,
480                                 GValue           *value,
481                                 EmpathyGroupChat *chat)
482 {
483         EmpathyGroupChatPriv *priv;
484         const gchar          *str = NULL;
485
486         priv = GET_PRIV (chat);
487
488         /* FIXME: this is ugly, should use properties on EmpathyChat obj */
489
490         if (!tp_strdiff (name, "name")) {
491                 str = g_value_get_string (value);
492                 g_free (priv->name);
493                 priv->name = g_strdup (str);
494                 return;
495         }
496
497         if (tp_strdiff (name, "subject")) {
498                 return;
499         }
500
501         str = g_value_get_string (value);
502         if (!tp_strdiff (priv->topic, str)) {
503                 return;
504         }
505
506         g_free (priv->topic);
507         priv->topic = g_strdup (str);
508         gtk_label_set_text (GTK_LABEL (priv->label_topic), priv->topic);
509
510         if (!EMPATHY_CHAT (chat)->block_events) {
511                 gchar *string;
512
513                 if (!G_STR_EMPTY (priv->topic)) {
514                         string = g_strdup_printf (_("Topic set to: %s"), priv->topic);
515                 } else {
516                         string = g_strdup (_("No topic defined"));
517                 }
518                 empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, string);
519                 g_free (string);
520         }
521 }
522
523 static void
524 group_chat_set_tp_chat (EmpathyChat    *chat,
525                         EmpathyTpChat *tp_chat)
526 {
527         EmpathyGroupChat     *group_chat;
528         EmpathyGroupChatPriv *priv;
529
530         g_return_if_fail (EMPATHY_IS_GROUP_CHAT (chat));
531
532         group_chat = EMPATHY_GROUP_CHAT (chat);
533         priv = GET_PRIV (group_chat);
534
535         /* Free all resources related to tp_chat */
536         if (priv->tp_chat) {
537                 g_object_unref (priv->tp_chat);
538                 priv->tp_chat = NULL;
539         }
540         if (priv->view) {
541                 gtk_widget_destroy (GTK_WIDGET (priv->view));
542                 g_object_unref (priv->store);
543         }
544         g_free (priv->name);
545         g_free (priv->topic);
546         priv->name = NULL;
547         priv->topic = NULL;
548
549         if (!tp_chat) {
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);
553                 return;
554         }
555
556         /* We are connected */
557         gtk_widget_set_sensitive (priv->hbox_topic, TRUE);
558         gtk_widget_set_sensitive (priv->scrolled_window_contacts, TRUE);
559
560         priv->tp_chat = g_object_ref (tp_chat);
561
562         if (empathy_tp_chatroom_get_invitation (priv->tp_chat, NULL, NULL)) {
563                 empathy_tp_chatroom_accept_invitation (priv->tp_chat);
564         }
565
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);
575
576         gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
577                            GTK_WIDGET (priv->view));
578         gtk_widget_show (GTK_WIDGET (priv->view));
579
580         /* Connect signals */
581         g_signal_connect (priv->tp_chat, "members-changed",
582                           G_CALLBACK (group_chat_members_changed_cb),
583                           chat);
584         g_signal_connect (priv->tp_chat, "property-changed",
585                           G_CALLBACK (group_chat_property_changed_cb),
586                           chat);
587 }
588
589 static gboolean
590 group_chat_key_press_event (EmpathyChat *chat,
591                             GdkEventKey *event)
592 {
593         EmpathyGroupChatPriv *priv = GET_PRIV (chat);
594
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;
602
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, &current, gtk_text_buffer_get_insert (buffer));
605
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);
610
611                 list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
612                 g_completion_add_items (priv->completion, list);
613
614                 nick = gtk_text_buffer_get_text (buffer, &start, &current, FALSE);
615                 completed_list = g_completion_complete (priv->completion,
616                                                         nick,
617                                                         &completed);
618
619                 g_free (nick);
620
621                 if (completed) {
622                         guint        len;
623                         const gchar *text;
624                         gchar       *complete_char = NULL;
625
626                         gtk_text_buffer_delete (buffer, &start, &current);
627
628                         len = g_list_length (completed_list);
629
630                         if (len == 1) {
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.
635                                  * Fixes #120876
636                                  * */
637                                 text = empathy_contact_get_name (completed_list->data);
638                         } else {
639                                 text = completed;
640                         }
641
642                         gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
643
644                         if (len == 1 && is_start_of_buffer &&
645                             empathy_conf_get_string (empathy_conf_get (),
646                                                      EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR,
647                                                      &complete_char) &&
648                             complete_char != NULL) {
649                                 gtk_text_buffer_insert_at_cursor (buffer,
650                                                                   complete_char,
651                                                                   strlen (complete_char));
652                                 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
653                                 g_free (complete_char);
654                         }
655
656                         g_free (completed);
657                 }
658
659                 g_completion_clear_items (priv->completion);
660
661                 g_list_foreach (list, (GFunc) g_object_unref, NULL);
662                 g_list_free (list);
663
664                 return TRUE;
665         }
666
667         return FALSE;
668 }
669
670 static gint
671 group_chat_contacts_completion_func (const gchar *s1,
672                                      const gchar *s2,
673                                      gsize        n)
674 {
675         gchar *tmp, *nick1, *nick2;
676         gint   ret;
677
678         tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
679         nick1 = g_utf8_casefold (tmp, -1);
680         g_free (tmp);
681
682         tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
683         nick2 = g_utf8_casefold (tmp, -1);
684         g_free (tmp);
685
686         ret = strncmp (nick1, nick2, n);
687
688         g_free (nick1);
689         g_free (nick2);
690
691         return ret;
692 }
693