]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-group-chat.c
Conflicts:
[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 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 #include <libempathy/empathy-conf.h>
44
45 #include "empathy-group-chat.h"
46 #include "empathy-chat.h"
47 #include "empathy-chat-view.h"
48 #include "empathy-contact-list-store.h"
49 #include "empathy-contact-list-view.h"
50 //#include "empathy-chat-invite.h"
51 //#include "empathy-sound.h"
52 #include "empathy-images.h"
53 #include "empathy-ui-utils.h"
54 #include "empathy-preferences.h"
55
56 #define DEBUG_DOMAIN "GroupChat"
57
58 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_GROUP_CHAT, EmpathyGroupChatPriv))
59
60 struct _EmpathyGroupChatPriv {
61         EmpathyContactListStore *store;
62         EmpathyContactListView  *view;
63         EmpathyTpChatroom      *tp_chat;
64
65         GtkWidget              *widget;
66         GtkWidget              *hpaned;
67         GtkWidget              *vbox_left;
68         GtkWidget              *scrolled_window_chat;
69         GtkWidget              *scrolled_window_input;
70         GtkWidget              *scrolled_window_contacts;
71         GtkWidget              *hbox_topic;
72         GtkWidget              *label_topic;
73
74         gchar                  *topic;
75         gchar                  *name;
76         GCompletion            *completion;
77
78         gint                    contacts_width;
79         gboolean                contacts_visible;
80 };
81
82 static void          group_chat_finalize                 (GObject           *object);
83 static void          group_chat_create_ui                (EmpathyGroupChat  *chat);
84 static void          group_chat_widget_destroy_cb        (GtkWidget         *widget,
85                                                           EmpathyGroupChat  *chat);
86 static void          group_chat_members_changed_cb       (EmpathyTpChatroom *tp_chat,
87                                                           EmpathyContact    *contact,
88                                                           EmpathyContact    *actor,
89                                                           guint              reason,
90                                                           gchar             *message,
91                                                           gboolean           is_member,
92                                                           EmpathyGroupChat  *chat);
93 static void          group_chat_topic_entry_activate_cb  (GtkWidget         *entry,
94                                                           GtkDialog         *dialog);
95 static void          group_chat_topic_response_cb        (GtkWidget         *dialog,
96                                                           gint               response,                        
97                                                           EmpathyGroupChat  *chat);
98 static const gchar * group_chat_get_name                 (EmpathyChat       *chat);
99 static gchar *       group_chat_get_tooltip              (EmpathyChat       *chat);
100 static const gchar * group_chat_get_status_icon_name     (EmpathyChat       *chat);
101 static GtkWidget *   group_chat_get_widget               (EmpathyChat       *chat);
102 static gboolean      group_chat_is_group_chat            (EmpathyChat       *chat);
103 static void          group_chat_set_tp_chat              (EmpathyChat       *chat,
104                                                           EmpathyTpChat     *tp_chat);
105 static void          group_chat_subject_notify_cb        (EmpathyTpChat     *tp_chat,
106                                                           GParamSpec        *param,
107                                                           EmpathyGroupChat  *chat);
108 static void          group_chat_name_notify_cb           (EmpathyTpChat     *tp_chat,
109                                                           GParamSpec        *param,
110                                                           EmpathyGroupChat  *chat);
111 static gboolean      group_chat_key_press_event          (EmpathyChat       *chat,
112                                                           GdkEventKey       *event);
113 static gint          group_chat_contacts_completion_func (const gchar       *s1,
114                                                           const gchar       *s2,
115                                                           gsize              n);
116
117 G_DEFINE_TYPE (EmpathyGroupChat, empathy_group_chat, EMPATHY_TYPE_CHAT)
118
119 static void
120 empathy_group_chat_class_init (EmpathyGroupChatClass *klass)
121 {
122         GObjectClass    *object_class;
123         EmpathyChatClass *chat_class;
124
125         object_class = G_OBJECT_CLASS (klass);
126         chat_class = EMPATHY_CHAT_CLASS (klass);
127
128         object_class->finalize           = group_chat_finalize;
129
130         chat_class->get_name             = group_chat_get_name;
131         chat_class->get_tooltip          = group_chat_get_tooltip;
132         chat_class->get_status_icon_name = group_chat_get_status_icon_name;
133         chat_class->get_widget           = group_chat_get_widget;
134         chat_class->is_group_chat        = group_chat_is_group_chat;
135         chat_class->set_tp_chat          = group_chat_set_tp_chat;
136         chat_class->key_press_event      = group_chat_key_press_event;
137
138         g_type_class_add_private (object_class, sizeof (EmpathyGroupChatPriv));
139 }
140
141 static void
142 empathy_group_chat_init (EmpathyGroupChat *chat)
143 {
144         EmpathyGroupChatPriv *priv;
145         EmpathyChatView      *chatview;
146
147         priv = GET_PRIV (chat);
148
149         priv->contacts_visible = TRUE;
150
151         chatview = EMPATHY_CHAT_VIEW (EMPATHY_CHAT (chat)->view);
152         empathy_chat_view_set_is_group_chat (chatview, TRUE);
153
154         group_chat_create_ui (chat);
155 }
156
157 static void
158 group_chat_finalize (GObject *object)
159 {
160         EmpathyGroupChat     *chat;
161         EmpathyGroupChatPriv *priv;
162
163         empathy_debug (DEBUG_DOMAIN, "Finalized:%p", object);
164
165         chat = EMPATHY_GROUP_CHAT (object);
166         priv = GET_PRIV (chat);
167         
168         g_free (priv->name);
169         g_free (priv->topic);
170         g_object_unref (priv->store);
171         g_object_unref (priv->tp_chat); 
172         g_completion_free (priv->completion);
173
174         G_OBJECT_CLASS (empathy_group_chat_parent_class)->finalize (object);
175 }
176
177 EmpathyGroupChat *
178 empathy_group_chat_new (McAccount *account,
179                         TpChan    *tp_chan)
180 {
181         EmpathyGroupChat     *chat;
182         EmpathyGroupChatPriv *priv;
183
184         g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
185         g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
186
187         chat = g_object_new (EMPATHY_TYPE_GROUP_CHAT, NULL);
188
189         priv = GET_PRIV (chat);
190
191         EMPATHY_CHAT (chat)->account = g_object_ref (account);
192         priv->tp_chat = empathy_tp_chatroom_new (account, tp_chan);
193         empathy_chat_set_tp_chat (EMPATHY_CHAT (chat), EMPATHY_TP_CHAT (priv->tp_chat));
194
195         return chat;
196 }
197
198 gboolean
199 empathy_group_chat_get_show_contacts (EmpathyGroupChat *chat)
200 {
201         EmpathyGroupChat     *group_chat;
202         EmpathyGroupChatPriv *priv;
203
204         g_return_val_if_fail (EMPATHY_IS_GROUP_CHAT (chat), FALSE);
205
206         group_chat = EMPATHY_GROUP_CHAT (chat);
207         priv = GET_PRIV (group_chat);
208
209         return priv->contacts_visible;
210 }
211
212 void
213 empathy_group_chat_set_show_contacts (EmpathyGroupChat *chat,
214                                      gboolean         show)
215 {
216         EmpathyGroupChat     *group_chat;
217         EmpathyGroupChatPriv *priv;
218
219         g_return_if_fail (EMPATHY_IS_GROUP_CHAT (chat));
220
221         group_chat = EMPATHY_GROUP_CHAT (chat);
222         priv = GET_PRIV (group_chat);
223
224         priv->contacts_visible = show;
225
226         if (show) {
227                 gtk_widget_show (priv->scrolled_window_contacts);
228                 gtk_paned_set_position (GTK_PANED (priv->hpaned),
229                                         priv->contacts_width);
230         } else {
231                 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
232                 gtk_widget_hide (priv->scrolled_window_contacts);
233         }
234 }
235
236 void
237 empathy_group_chat_set_topic (EmpathyGroupChat *chat)
238 {
239         EmpathyGroupChatPriv *priv;
240         EmpathyChatWindow    *chat_window;
241         GtkWidget           *chat_dialog;
242         GtkWidget           *dialog;
243         GtkWidget           *entry;
244         GtkWidget           *hbox;
245         const gchar         *topic;
246
247         g_return_if_fail (EMPATHY_IS_GROUP_CHAT (chat));
248
249         priv = GET_PRIV (chat);
250
251         chat_window = empathy_chat_get_window (EMPATHY_CHAT (chat));
252         chat_dialog = empathy_chat_window_get_dialog (chat_window);
253
254         dialog = gtk_message_dialog_new (GTK_WINDOW (chat_dialog),
255                                          0,
256                                          GTK_MESSAGE_QUESTION,
257                                          GTK_BUTTONS_OK_CANCEL,
258                                          _("Enter the new topic you want to set for this room:"));
259
260         topic = gtk_label_get_text (GTK_LABEL (priv->label_topic));
261
262         hbox = gtk_hbox_new (FALSE, 0);
263         gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
264                             hbox, FALSE, TRUE, 4);
265
266         entry = gtk_entry_new ();
267         gtk_entry_set_text (GTK_ENTRY (entry), topic);
268         gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
269                     
270         gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 4);
271
272         g_object_set (GTK_MESSAGE_DIALOG (dialog)->label, "use-markup", TRUE, NULL);
273         g_object_set_data (G_OBJECT (dialog), "entry", entry);
274
275         g_signal_connect (entry, "activate",
276                           G_CALLBACK (group_chat_topic_entry_activate_cb),
277                           dialog);
278         g_signal_connect (dialog, "response",
279                           G_CALLBACK (group_chat_topic_response_cb),
280                           chat);
281
282         gtk_widget_show_all (dialog);
283 }
284
285 static void
286 group_chat_create_ui (EmpathyGroupChat *chat)
287 {
288         EmpathyGroupChatPriv *priv;
289         GladeXML            *glade;
290         GList               *list = NULL; 
291
292         priv = GET_PRIV (chat);
293
294         glade = empathy_glade_get_file ("empathy-group-chat.glade",
295                                        "group_chat_widget",
296                                        NULL,
297                                        "group_chat_widget", &priv->widget,
298                                        "hpaned", &priv->hpaned,
299                                        "vbox_left", &priv->vbox_left,
300                                        "scrolled_window_chat", &priv->scrolled_window_chat,
301                                        "scrolled_window_input", &priv->scrolled_window_input,
302                                        "hbox_topic", &priv->hbox_topic,
303                                        "label_topic", &priv->label_topic,
304                                        "scrolled_window_contacts", &priv->scrolled_window_contacts,
305                                        NULL);
306
307         empathy_glade_connect (glade,
308                               chat,
309                               "group_chat_widget", "destroy", group_chat_widget_destroy_cb,
310                               NULL);
311
312         g_object_unref (glade);
313
314         g_object_set_data (G_OBJECT (priv->widget), "chat", g_object_ref (chat));
315
316         /* Add room GtkTextView. */
317         gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
318                            GTK_WIDGET (EMPATHY_CHAT (chat)->view));
319         gtk_widget_show (GTK_WIDGET (EMPATHY_CHAT (chat)->view));
320
321         /* Add input GtkTextView */
322         gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
323                            EMPATHY_CHAT (chat)->input_text_view);
324         gtk_widget_show (EMPATHY_CHAT (chat)->input_text_view);
325
326         /* Add nick name completion */
327         priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_name);
328         g_completion_set_compare (priv->completion,
329                                   group_chat_contacts_completion_func);
330
331         /* Set widget focus order */
332         list = g_list_append (NULL, priv->scrolled_window_input);
333         gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
334         g_list_free (list);
335
336         list = g_list_append (NULL, priv->vbox_left);
337         list = g_list_append (list, priv->scrolled_window_contacts);
338         gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
339         g_list_free (list);
340
341         list = g_list_append (NULL, priv->hpaned);
342         list = g_list_append (list, priv->hbox_topic);
343         gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
344         g_list_free (list);
345 }
346
347 static void
348 group_chat_widget_destroy_cb (GtkWidget       *widget,
349                               EmpathyGroupChat *chat)
350 {
351         empathy_debug (DEBUG_DOMAIN, "Destroyed");
352
353         g_object_unref (chat);
354 }
355
356 static void
357 group_chat_members_changed_cb (EmpathyTpChatroom *tp_chat,
358                                EmpathyContact     *contact,
359                                EmpathyContact     *actor,
360                                guint               reason,
361                                gchar              *message,
362                                gboolean            is_member,
363                                EmpathyGroupChat   *chat)
364 {
365         EmpathyGroupChatPriv *priv;
366         gchar                *str;
367
368         priv = GET_PRIV (chat);
369
370         if (is_member) {
371                 str = g_strdup_printf (_("%s has joined the room"),
372                                        empathy_contact_get_name (contact));
373         } else {
374                 str = g_strdup_printf (_("%s has left the room"),
375                                        empathy_contact_get_name (contact));
376         }
377         empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, str);
378         g_free (str);
379 }
380
381 static void
382 group_chat_topic_entry_activate_cb (GtkWidget *entry,
383                                     GtkDialog *dialog)
384 {
385         gtk_dialog_response (dialog, GTK_RESPONSE_OK);
386 }
387
388 static void
389 group_chat_topic_response_cb (GtkWidget       *dialog,
390                               gint             response,                              
391                               EmpathyGroupChat *chat)
392 {
393         if (response == GTK_RESPONSE_OK) {
394                 GtkWidget   *entry;
395                 const gchar *topic;
396
397                 entry = g_object_get_data (G_OBJECT (dialog), "entry");
398                 topic = gtk_entry_get_text (GTK_ENTRY (entry));
399                 
400                 if (!G_STR_EMPTY (topic)) {
401                         EmpathyGroupChatPriv *priv;
402
403                         priv = GET_PRIV (chat);
404
405                         empathy_tp_chatroom_set_topic (priv->tp_chat, topic);
406                 }
407         }
408
409         gtk_widget_destroy (dialog);
410 }
411
412 static const gchar *
413 group_chat_get_name (EmpathyChat *chat)
414 {
415         EmpathyGroupChat     *group_chat;
416         EmpathyGroupChatPriv *priv;
417
418         g_return_val_if_fail (EMPATHY_IS_GROUP_CHAT (chat), NULL);
419
420         group_chat = EMPATHY_GROUP_CHAT (chat);
421         priv = GET_PRIV (group_chat);
422
423         if (!priv->name) {
424                 const gchar *id;
425                 const gchar *server;
426
427                 id = empathy_chat_get_id (chat);
428                 server = strstr (id, "@");
429
430                 if (server) {
431                         priv->name = g_strndup (id, server - id);
432                 } else {
433                         priv->name = g_strdup (id);
434                 } 
435         }
436
437         return priv->name;
438 }
439
440 static gchar *
441 group_chat_get_tooltip (EmpathyChat *chat)
442 {
443         EmpathyGroupChat     *group_chat;
444         EmpathyGroupChatPriv *priv;
445
446         g_return_val_if_fail (EMPATHY_IS_GROUP_CHAT (chat), NULL);
447
448         group_chat = EMPATHY_GROUP_CHAT (chat);
449         priv = GET_PRIV (group_chat);
450
451         if (priv->topic) {
452                 gchar *topic, *tmp;
453
454                 topic = g_strdup_printf (_("Topic: %s"), priv->topic);
455                 tmp = g_strdup_printf ("%s\n%s", priv->name, topic);
456                 g_free (topic);
457
458                 return tmp;
459         }
460
461         return g_strdup (priv->name);
462 }
463
464 static const gchar *
465 group_chat_get_status_icon_name (EmpathyChat *chat)
466 {
467         return EMPATHY_IMAGE_GROUP_MESSAGE;
468 }
469
470 static GtkWidget *
471 group_chat_get_widget (EmpathyChat *chat)
472 {
473         EmpathyGroupChat     *group_chat;
474         EmpathyGroupChatPriv *priv;
475
476         g_return_val_if_fail (EMPATHY_IS_GROUP_CHAT (chat), NULL);
477
478         group_chat = EMPATHY_GROUP_CHAT (chat);
479         priv = GET_PRIV (group_chat);
480
481         return priv->widget;
482 }
483
484 static gboolean
485 group_chat_is_group_chat (EmpathyChat *chat)
486 {
487         g_return_val_if_fail (EMPATHY_IS_GROUP_CHAT (chat), FALSE);
488
489         return TRUE;
490 }
491
492 static void
493 group_chat_set_tp_chat (EmpathyChat    *chat,
494                         EmpathyTpChat *tp_chat)
495 {
496         EmpathyGroupChat     *group_chat;
497         EmpathyGroupChatPriv *priv;
498
499         g_return_if_fail (EMPATHY_IS_GROUP_CHAT (chat));
500
501         group_chat = EMPATHY_GROUP_CHAT (chat);
502         priv = GET_PRIV (group_chat);
503
504         /* Free all resources related to tp_chat */
505         if (priv->tp_chat) {
506                 g_object_unref (priv->tp_chat);
507                 priv->tp_chat = NULL;
508         }
509         if (priv->view) {
510                 gtk_widget_destroy (GTK_WIDGET (priv->view));
511                 g_object_unref (priv->store);
512         }
513         g_free (priv->name);
514         g_free (priv->topic);
515         priv->name = NULL;
516         priv->topic = NULL;
517
518         if (!tp_chat) {
519                 /* We are no more connected */
520                 gtk_widget_set_sensitive (priv->hbox_topic, FALSE);
521                 gtk_widget_set_sensitive (priv->scrolled_window_contacts, FALSE);
522                 return;
523         }
524
525         /* We are connected */
526         gtk_widget_set_sensitive (priv->hbox_topic, TRUE);
527         gtk_widget_set_sensitive (priv->scrolled_window_contacts, TRUE);
528
529         priv->tp_chat = g_object_ref (tp_chat);
530
531         if (empathy_tp_chatroom_get_invitation (priv->tp_chat, NULL, NULL)) {
532                 empathy_tp_chatroom_accept_invitation (priv->tp_chat);
533         }
534
535         /* Create contact list */
536         priv->store = empathy_contact_list_store_new (EMPATHY_CONTACT_LIST (priv->tp_chat));
537         priv->view = empathy_contact_list_view_new (priv->store);
538         empathy_contact_list_view_set_interactive (priv->view, TRUE);
539         gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
540                            GTK_WIDGET (priv->view));
541         gtk_widget_show (GTK_WIDGET (priv->view));
542
543         /* Connect signals */
544         g_signal_connect (priv->tp_chat, "members-changed",
545                           G_CALLBACK (group_chat_members_changed_cb),
546                           chat);
547         g_signal_connect (priv->tp_chat, "notify::subject",
548                           G_CALLBACK (group_chat_subject_notify_cb),
549                           chat);
550         g_signal_connect (priv->tp_chat, "notify::name",
551                           G_CALLBACK (group_chat_name_notify_cb),
552                           chat);
553 }
554
555 static void
556 group_chat_subject_notify_cb (EmpathyTpChat   *tp_chat,
557                               GParamSpec      *param,
558                               EmpathyGroupChat *chat)
559 {
560         EmpathyGroupChatPriv *priv;
561         gchar                *str = NULL;
562
563         priv = GET_PRIV (chat);
564
565         g_object_get (priv->tp_chat, "subject", &str, NULL);
566         if (!tp_strdiff (priv->topic, str)) {
567                 g_free (str);
568                 return;
569         }
570
571         g_free (priv->topic);
572         priv->topic = str;
573         gtk_label_set_text (GTK_LABEL (priv->label_topic), priv->topic);
574
575         if (!G_STR_EMPTY (priv->topic)) {
576                 str = g_strdup_printf (_("Topic set to: %s"), priv->topic);
577         } else {
578                 str = g_strdup (_("No topic defined"));
579         }
580         empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, str);
581         g_free (str);
582 }
583
584 static void
585 group_chat_name_notify_cb (EmpathyTpChat   *tp_chat,
586                            GParamSpec      *param,
587                            EmpathyGroupChat *chat)
588 {
589         EmpathyGroupChatPriv *priv;
590
591         priv = GET_PRIV (chat);
592
593         g_free (priv->name);
594         g_object_get (priv->tp_chat, "name", &priv->name, NULL);
595 }
596
597 static gboolean
598 group_chat_key_press_event (EmpathyChat *chat,
599                             GdkEventKey *event)
600 {
601         EmpathyGroupChatPriv *priv = GET_PRIV (chat);
602
603         if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
604             event->keyval == GDK_Tab) {
605                 GtkTextBuffer *buffer;
606                 GtkTextIter    start, current;
607                 gchar         *nick, *completed;
608                 GList         *list, *completed_list;
609                 gboolean       is_start_of_buffer;
610
611                 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
612                 gtk_text_buffer_get_iter_at_mark (buffer, &current, gtk_text_buffer_get_insert (buffer));
613
614                 /* Get the start of the nick to complete. */
615                 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
616                 gtk_text_iter_backward_word_start (&start);
617                 is_start_of_buffer = gtk_text_iter_is_start (&start);
618
619                 list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
620                 g_completion_add_items (priv->completion, list);
621
622                 nick = gtk_text_buffer_get_text (buffer, &start, &current, FALSE);
623                 completed_list = g_completion_complete (priv->completion,
624                                                         nick,
625                                                         &completed);
626
627                 g_free (nick);
628
629                 if (completed) {
630                         guint        len;
631                         const gchar *text;
632                         gchar       *complete_char = NULL;
633
634                         gtk_text_buffer_delete (buffer, &start, &current);
635
636                         len = g_list_length (completed_list);
637
638                         if (len == 1) {
639                                 /* If we only have one hit, use that text
640                                  * instead of the text in completed since the
641                                  * completed text will use the typed string
642                                  * which might be cased all wrong.
643                                  * Fixes #120876
644                                  * */
645                                 text = empathy_contact_get_name (completed_list->data);
646                         } else {
647                                 text = completed;
648                         }
649
650                         gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
651
652                         if (len == 1 && is_start_of_buffer &&
653                             empathy_conf_get_string (empathy_conf_get (),
654                                                      EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR,
655                                                      &complete_char) &&
656                             complete_char != NULL) {
657                                 gtk_text_buffer_insert_at_cursor (buffer,
658                                                                   complete_char,
659                                                                   strlen (complete_char));
660                                 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
661                                 g_free (complete_char);
662                         }
663
664                         g_free (completed);
665                 }
666
667                 g_completion_clear_items (priv->completion);
668
669                 g_list_foreach (list, (GFunc) g_object_unref, NULL);
670                 g_list_free (list);
671
672                 return TRUE;
673         }
674
675         return FALSE;
676 }
677
678 static gint
679 group_chat_contacts_completion_func (const gchar *s1,
680                                      const gchar *s2,
681                                      gsize        n)
682 {
683         gchar *tmp, *nick1, *nick2;
684         gint   ret;
685
686         tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
687         nick1 = g_utf8_casefold (tmp, -1);
688         g_free (tmp);
689
690         tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
691         nick2 = g_utf8_casefold (tmp, -1);
692         g_free (tmp);
693
694         ret = strncmp (nick1, nick2, n);
695
696         g_free (nick1);
697         g_free (nick2);
698
699         return ret;
700 }
701